setName('handleData') ->setDescription('批量处理C端导入的订单数据'); } //【脚本2】批量处理C端导入的订单数据 protected function execute(Input $input, Output $output) { $order_import_from_c_db = new OIFCModel(); $c_id = $order_import_from_c_db ->where(['is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_wait_relation]) ->lock(true) ->value('id', 0); if (!$c_id) $output->writeln('没有符合条件的记录'); else { Db::startTrans(); try { //先找用户确认信息 $extend_data = Db::name('order_import_from_c_extend') ->where(['order_import_from_c_id' => $c_id['id'], 'is_del' => 0, 'type' => 2]) ->find(); if (empty($extend_data)) { $extend_data = Db::name('order_import_from_c_extend') ->where(['order_import_from_c_id' => $c_id['id'], 'is_del' => 0, 'type' => 1]) ->find(); } if (empty($extend_data)) throw new Exception('没有对应的解析数据'); //复用sale::create()方法 -- start $orderCode = makeNo("QR"); $customer_code = $extend_data['customer_code']; $customer = Db::name("customer_info") ->where(["companyNo" => $customer_code]) ->find(); if ($customer == false) throw new Exception('未找到客户数据'); $supplierNo = $extend_data['supplierNo']; $supplier = Db::name("supplier") ->where(["code" => $supplierNo]) ->find(); if ($supplier == false) throw new Exception('未找到平台供应商数据'); $goodtype = 1;//1正常商品 $order_type = 3;//1销售 2咨询 3平台(C端)导入 $sendtype = 1;//直接发货 $platform_id = $extend_data['platform_codes']; $platform_order = ''; $good_num = $extend_data['num']; $arrtime = $extend_data['platform_time']; $paytime = $extend_data['addtime']; $workNo = '';//业管单号 $ct = Db::name('good_platform') ->alias('a') ->join('good b', 'b.spuCode=a.spuCode', 'left') ->where(['a.spuCode' => $extend_data['spuCode']]) ->field("b.*,a.skuCode,a.platform_code,a.plat_code") ->find(); if ($ct == false) throw new Exception('未找到商品数据'); $goodinfo = $ct; $is_stock = $ct['is_stock']; $spuCode = $ct['spuCode']; $skuCode = $ct['skuCode']; $is_activity = empty($extend_data['activity_name']) ? 0 : 1; if ($goodinfo['is_stock'] == 1) { $stock = Db::name("good_stock") ->alias("a") ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code") ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.supplierNo" => $supplierNo]) ->field("a.id,a.usable_stock,a.wait_out_stock") ->find(); if ($stock == false || $stock['usable_stock'] < $good_num) throw new Exception('库存数量不足'); } $origin = Db::name("good_nake") ->where([["spuCode", "=", $spuCode], ["min_num", "<=", $good_num], ["is_del", "=", 0]]) ->order("min_num desc") ->find(); if ($origin == false) throw new Exception('未找到相关阶梯成本价格'); $origin_price = $origin['nake_total']; $sale_price = $extend_data['price']; if ($goodtype == 1) { $good = Db::name("good_ladder") ->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1]) ->where([["min_num", "<=", $good_num]]) ->order("min_num desc") ->find(); if ($good == false) throw new Exception('未找到相关阶梯价格'); $sale_price = $good['sale_price']; if ($is_activity == 1) { $act = Db::name("activity_info") ->alias("a") ->leftJoin("good_activity b", "a.activity_code=b.activity_code") ->where(["a.skuCode" => $skuCode, "a.id" => $extend_data['activity_id'], "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0]) ->find(); if ($act == false) throw new Exception('未找到相关活动价'); if ($act['moq_num'] > $good_num) throw new Exception('商品不满足活动价起订量' . $act['moq_num']); if ($act['activity_stock'] < $good_num) throw new Exception('商品活动库存剩余' . $act['activity_stock']); $sale_price = $act['activity_price']; } } $cgd = [ "supplierNo" => $ct['supplierNo'], "companyNo" => $supplierNo, "orderCode" => $orderCode, "spuCode" => $ct['spuCode'], "skuCode" => $ct['skuCode'], "good_name" => $ct['good_name'], "sale_price" => $origin_price, "total_fee" => $origin_price * $good_num, "pakge_fee" => $ct['packing_fee'], "cert_fee" => $ct['cert_fee'], "open_fee" => $ct['open_fee'], "cost_fee" => $ct['cost_fee'], "mark_fee" => $ct['mark_fee'], "demo_fee" => $ct['demo_fee'], "good_num" => $good_num, "good_type" => $goodtype, "order_type" => $is_stock == 1 ? 1 : 2, "createrid" => $ct['createrid'], "creater" => $ct['creater'], 'send_way' => 2 ]; $send_num = $extend_data['num']; $remark = $extend_data['order_remark']; $rm = $extend_data['createrid']; $ri = $extend_data['creater']; $data = [ "orderCode" => $orderCode, "good_code" => $spuCode, "skuCode" => $skuCode, "customer_code" => $customer_code, "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '', "good_num" => $good_num, "cat_id" => $goodinfo['cat_id'], "apply_id" => $rm, "apply_name" => $ri, "origin_price" => $origin_price, "sale_price" => $sale_price, "post_fee" => 0, "status" => $send_num == 0 ? 3 : ($good_num == $send_num ? 5 : 4), "send_num" => $send_num, "wsend_num" => $good_num - $send_num, "send_status" => $good_num == $send_num ? 3 : ($send_num == 0 ? 1 : 2), "status" => 0, "send_num" => 0, "wsend_num" => $good_num, "send_status" => 1, "good_type" => $goodtype, "send_type" => $sendtype, "supplierNo" => $supplierNo, "is_del" => 0, "zxNo" => "", "platform_order" => $platform_order, "platform_id" => $platform_id, "remark" => $remark, "is_stock" => $is_stock, "is_activity" => $is_activity === "" ? 0 : $is_activity, "order_type" => $order_type, // "poNo"=>$poNo, 'good_weight' => $ct['weight'], 'gold_price' => $ct['cgd_gold_price'], 'cost_price' => $ct['cost_fee'], 'diff_weight' => 0, 'diff_fee' => 0, "workNo" => $workNo, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s"), 'total_price' => round($sale_price * $good_num, 2), ]; $paytime == "" ? "" : $data['paytime'] = $paytime; $datainfo = Db::name('sale')->insert($data, true); if ($datainfo > 0) { if ($is_activity == 1) { $actup = [ "activity_stock" => $act['activity_stock'] - $good_num, "updatetime" => date("Y-m-d H:i:s") ]; $actupp = Db::name("activity_info") ->where(["skuCode" => $skuCode, "id" => $extend_data['activity_id'], "is_del" => 0, "status" => 1]) ->save($actup); if ($actupp == false) throw new Exception('活动库存修改失败'); } if ($is_stock == 0) { $bol = $this->createCgd($cgd, $rm, $ri); if ($bol == false) throw new Exception('订单创建失败'); } else { $bol = $this->RelaCgd(['orderCode' => $orderCode, "good_num" => $good_num, "spuCode" => $spuCode, "companyNo" => $supplierNo]); if ($bol == false) throw new Exception('库存商品关联采购单失败'); if (isset($stock)) { $stck = [ "usable_stock" => $stock['usable_stock'] - $good_num, "wait_out_stock" => $stock['wait_out_stock'] + $good_num, "updatetime" => date("Y-m-d H:i:s") ]; $upad = Db::name("good_stock") ->where($stock) ->update($stck); if ($upad == false) throw new Exception('库存商品更新库存失败'); //商品变动日志表,good_log_code字段存储采购单号 $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"]; $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $datainfo, "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"]; GoodLog::LogAdd(['id' => $rm, 'nickname' => $ri], $good_data, "XSQRD"); } } if ($sendtype == 1) { $temp = [ 'orderCode' => $orderCode, 'contactor' => $extend_data['contactor'], 'mobile' => $extend_data['mobile'], 'addr' => $extend_data['addr'], 'addr_code' => $extend_data['addr_code'], 'customer_code' => $customer_code, 'receipt_quantity' => $extend_data['num'],//收货数量, 'post_fee' => 0, 'is_del' => 0, 'addtime' => date("Y-m-d H:i:s"), 'updatetime' => date("Y-m-d H:i:s"), 'arrive_time' => $arrtime, ]; $vmp = Db::name('order_addr')->insert($temp, true); if ($vmp > 0) { if ($is_stock == 1) { $order = Db::name("order_num") ->where(["orderCode" => $orderCode, "status" => 1]) ->where([["wsend_num", ">=", 0]]) ->lock(true) ->find(); if ($order == false) throw new Exception('未找到可以发货得采购单数据'); $num = $extend_data['num']; $outCode = makeNo("DF"); $order['wsend_num'] -= $num; $order['send_num'] += $num; $or = Db::name("order_num")->save($order); if ($or == false) throw new Exception('发货地址更新失败'); $tep = [ "cgdNo" => $order['cgdNo'], "outCode" => $outCode, "send_num" => $num, "status" => 1, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $sen = Db::name("order_send")->save($tep); if ($sen == false) throw new Exception('发货地址添加创建失败'); $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find(); if ($cgdinfo == false) throw new Exception('未匹配到采购数据'); $out = [ "orderCode" => $orderCode, "outCode" => $outCode, "apply_id" => $rm, "apply_name" => $ri, "addrid" => $vmp, "post_name" => "", "post_code" => "", "post_fee" => 0, "sendtime" => date("Y-m-d H:i:s"), "send_num" => $num, "check_num" => 0, "error_num" => 0, "wsm_code" => $cgdinfo['wsm_code'], "order_type" => 1, "status" => 1, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $ou = Db::name("order_out")->insert($out); if ($ou == false) throw new Exception('发货地址添加创建失败'); else { //修改状态,添加待办 ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [ "order_code" => $outCode,//出库单号 "status" => 0,//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "create"//新建create,编辑edit,更改状态status ], "CKD", 0, $out); ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [ "order_type" => 'CKD', "order_code" => $outCode,//出库单号 "order_id" => 0, "order_status" => 0 ]); } } } else throw new Exception('发货地址添加失败'); } } //复用sale::create()方法 -- end //处理完成 $order_import_from_c_db ->where('id', $c_id) ->save([ 'status' => $order_import_from_c_db::$status_success, 'updatetime' => date('Y-m-d H:i:s'), 'remark' => '', 'updateid' => 0, 'updater' => 'system' ]); Db::commit(); $output->writeln(date('Y-m-d H:i:s') . '|处理成功'); } catch (\think\Exception $exception) { Db::rollback(); $order_import_from_c_db ->where('id', $c_data['id']) ->save([ 'status' => $order_import_from_c_db::$status_stock_not_enough, 'updatetime' => date('Y-m-d H:i:s'), 'remark' => $exception->getMessage(), 'updateid' => 0, 'updater' => 'system' ]); // 指令输出 $output->writeln(date('Y-m-d H:i:s') . '|处理失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine()); } } } //创建采购单 private function createCgd($data = [], $rm = '0', $ri = '') { $cgdCode = makeNo("CG"); $supplier = Db::name("supplier") ->where(["code" => $data['supplierNo'], "is_del" => 0])->find(); if ($supplier == false) return false; $wsm = Db::name("warehouse_info") ->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "is_del" => 0]) ->find(); if ($wsm == false) { $wsm_code = makeNo("WSM"); $inwsm = [ "wsm_code" => $wsm_code, "name" => $supplier['name'], "wsm_type" => 2, "supplierNo" => $supplier['code'], "addr" => "", "addrs_code" => "", "contactor" => 0, "contactor_name" => 0, "mobile" => "", "position" => "", "companyNo" => $data['companyNo'], "status" => 1, "is_del" => 0, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $in = Db::name("warehouse_info")->insert($inwsm); if ($in == false) return false; } else { $wsm_code = $wsm['wsm_code']; } $cg = [ "cgdNo" => $cgdCode, "bkcode" => "", "wsm_code" => $wsm_code, "cgder" => $supplier['person'], "cgder_id" => $supplier['personid'], "spuCode" => $data['spuCode'], "skuCode" => $data['skuCode'], "good_name" => $data['good_name'], "good_num" => $data['good_num'], "good_price" => $data['sale_price'], "total_fee" => round($data['sale_price'] * $data['good_num'], 2), "pakge_fee" => $data['pakge_fee'], "cert_fee" => $data['cert_fee'], "open_fee" => $data['open_fee'], "teach_fee" => $data['cost_fee'], "mark_fee" => $data['mark_fee'], "demo_fee" => $data['demo_fee'], #"weight"=>$data['weight'], "diff_weight" => "0", "diff_fee" => "0", "gold_price" => "0", "supplierNo" => $data['supplierNo'], "supplier_name" => $supplier['name'], "companyNo" => $data['companyNo'], "send_status" => 1, "send_num" => 0, "wsend_num" => $data['good_num'], "remark" => "", "status" => 0,//0初始化 "lasttime" => date("Y-m-d H:i:s"), "is_del" => 0, "order_type" => $data['order_type'], "good_type" => $data['good_type'], "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $up = Db::name("purchease_order")->insert($cg, true); if ($up) { //修改状态,添加待办 ActionLog::logAdd(['id' => $rm, 'nickname' => $ri], [ "order_code" => $cg['cgdNo'],//销售单code "status" => 0,//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "create"//新建create,编辑edit,更改状态status ], "CGD", $cg['status'], $cg); ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], [ "order_type" => 'CGD', "order_code" => $cg['cgdNo'],//销售单code "order_id" => $up, "order_status" => $cg['status'] ]); $rela = [ "orderCode" => $data['orderCode'], "cgdNo" => $cgdCode, "spuCode" => $data['spuCode'], "good_num" => $data['good_num'], "wsend_num" => $data['good_num'], "send_num" => 0, "wait_num" => 0, "status" => 1, "source" => 2 ]; $re = Db::name("order_num")->save($rela); if ($re == false) return false; else { $stokc = Db::name("good_stock") ->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0]) ->find(); if ($stokc == false) { $stokc = [ "spuCode" => $data['spuCode'], "wsm_code" => $wsm_code, "wait_in_stock" => 0, "wait_out_stock" => 0, "usable_stock" => 0, "intra_stock" => 0, "total_stock" => 0, "status" => 1, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s") ]; $stoc = Db::name("good_stock")->save($stokc); if ($stoc == false) return false; } return true; } } else return false; } //创建..... private function RelaCgd(array $outinfo = []) { $cgd = Db::name("order_bk") ->where([ ["spuCode", "=", $outinfo['spuCode']], ["is_del", "=", 0], ["balance_num", ">=", $outinfo['good_num']], ['companyNo', "=", $outinfo['companyNo']] ]) ->lock(true) ->find(); if ($cgd == false) return false; $cgd['balance_num'] -= $outinfo['good_num']; $cgd['merge_num'] += $outinfo['good_num']; $cgd['updatetime'] = date("Y-m-d H:i:s"); $up = Db::name("order_bk")->save($cgd); if ($up == false) return false; $data = [ "orderCode" => $outinfo['orderCode'], "cgdNo" => $cgd['cgdNo'], "spuCode" => $outinfo['spuCode'], "companyNo" => $outinfo['companyNo'], "good_num" => $outinfo['good_num'], "wsend_num" => $outinfo['good_num'], "send_num" => 0, "wait_num" => $outinfo['good_num'], "status" => 1, "source" => 1, ]; $order = Db::name("order_num")->save($data); if ($order == false) return false; return true; } }