'18K', 2 => '24K', 3 => '白银'];//贵金属种类对应文本 private $cgd_key = 0;//新增到wsm_cgd_caixiao的数组下标,从0开始 private $order_source = 8;//支付渠道 private $Tcode = ''; private $comOrder=null; protected function configure() { $this->setName('split_sale')->setDescription('销售订单拆分'); parent::configure(); } protected function execute(Input $input, Output $output) { $key = 'split_sale_'; $rs = Cache::store('redis')->inc($key); if ($rs!=1){ Cache::store('redis')->dec($key); return true; } try { $this->comOrder=new ComonOrder(); Db::startTrans(); try { $data = Db::name('sale') ->alias('a') ->leftJoin('platform b', 'b.id=a.platform_id') ->leftJoin('good_proof c', 'c.id=a.proof_id') ->field('a.*,b.platform_name,b.use_type,c.proof_url') ->where([ ['a.is_del', '=', 0], ['a.updatetime', '>=', date('Y-m-d H:i:s', time() - 30 * 60)], ['a.pay_id', '<>', 0] ])->cursor(); $userCommon = User::getIns(); $commonOrder=[];//处理采购销售同一数据 $updatePrice=true; foreach ($data as $sale) { //处理采购销售同一数据 $tempOrder=[ "cxCode"=>$sale["orderCode"], "tyCode"=>makeNo("TY"), "orderCode"=>"", "cgdNo"=>"", "order_type"=>$sale['order_type'], "order_source"=>$sale['order_source'], "platform_type"=>Platform::where("id",$sale['platform_id'])->value("use_type",0), "goodNo"=>$sale['good_code'], "goodName"=>$sale['good_name'], "apply_id"=>$sale['apply_id'], "apply_name"=>$sale['apply_name'], "customerName"=>"", "customerNo"=>"", "supplierNo"=>'', "supplierName"=>'', "status"=>$sale['th_num']==$sale['good_num']?3:-1, "companyName"=>$sale['supplierName'], "companyNo"=>$sale['supplierNo'], ]; if( strtotime($sale['addtime']) >= strtotime('2023-07-01 00:00:00'))$this->Tcode ='F'; else $this->Tcode =''; if( strtotime($sale['addtime']) >= strtotime('2024-12-12 23:59:59')) $updatePrice=false; //补充商品信息 if ($sale['order_type'] == 3 ||$sale['order_type'] == 4) { //咨询相关 $good = Db::name('good_zixun')->alias("b") ->field('b.noble_metal,c.brand_name brand,d.unit,b.craft_desc cost_desc,b.good_weight noble_weight,b.tax,0 delivery_day,0 lead_time') ->leftJoin('brand c', 'c.id=b.brand_id') ->leftJoin('unit d', 'd.id=b.good_unit') ->where(['b.spuCode' => $sale['good_code']]) ->findOrEmpty(); } else { $good = Db::name('good') ->field('b.noble_metal,c.brand_name brand,d.unit,b.craft_desc cost_desc,b.noble_weight,b.tax,b.delivery_day,b.lead_time') ->alias('b') ->leftJoin('brand c', 'c.id=b.brand_id') ->leftJoin('unit d', 'd.id=b.good_unit') ->where(['b.is_del' => 0, 'b.spuCode' => $sale['good_code']]) ->findOrEmpty(); } $sale['plan_price'] = Db::name("good_nake")->where(["spuCode"=>$sale['good_code'],"is_del"=>0]) ->order("min_num asc")->value("nake_total",0); $sale['plan_total'] = round($sale['good_num'] * $sale['plan_price'],2); //贵金属分类转换文本 $good['noble_metal'] = $this->noble_metal[$good['noble_metal'] ?? 0] ?? ''; //相关数据合并 $sale = array_merge($sale, $good); //支付渠道相关信息 $pay_rates = Db::name('pay_log') ->where(['is_del' => 0, 'pay_id' => $sale['pay_id'], 'orderCode' => $sale['orderCode']]) ->where( 'companyNo', 'not in',["KH","GYS"]) ->field(true) ->order(['weight' => 'desc']) ->select() ->toArray(); //关联的采购单信息 $cgd = Db::name('order_num') ->alias('a') ->field('b.*,c.addtime bktime,c.apply_id bkcreater') ->leftJoin('purchease_order b', 'b.cgdNo=a.cgdNo') ->leftJoin('purchease c', 'c.bk_code=b.bkcode') ->where('a.orderCode', $sale['orderCode']) ->findOrEmpty(); //采购总金额和销售总金额 if($updatePrice) $cgd_total = $sale_total = $sale['total_price']; else { $cgd_price = $sale_price = $sale['sale_price']; $cgd_total = $sale_total = $sale['total_price']; } //客户 $customer = ['No' => $sale['customer_code'], 'name' => $sale['customerName']]; foreach ($pay_rates as $keys=>$pay_rate) { //生成新的采购单号和销售单号 $cgdNo = makeNo($this->Tcode.'CG'); $tempOrder['cgdNo'] = $cgdNo ; $cgdNo = substr($cgdNo, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT); $orderCode = makeNo($this->Tcode.'QR'); $orderCode = substr($orderCode, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT); $this->i++; //计算渠道业务公司采购单单价。 if($updatePrice)$cgd_total = bcsub($cgd_total, bcmul($sale['total_price'] ?? "0", round($pay_rate['rate'] / 100, 4), 5), 5); else { $cgd_price = bcsub($cgd_price, bcmul($sale_price ?? "0", round($pay_rate['rate'] / 100, 4), 8), 8); $cgd_total =bcmul($cgd_price, $sale['good_num'], 8); } //此次销售单业务公司信息 $supplier = ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName'],"pay_name"=>$pay_rate['pay_name']]; $sale['origin_price'] = $sale['good_num'] > 0 ? bcdiv($cgd_total, $sale['good_num'], 8) : "0"; $this->_handle_sale_caixiao($sale, $orderCode, $cgdNo, $sale_total, $customer, $supplier); $tempOrder['customerName'] = $pay_rate['companyName']; $tempOrder['customerNo'] = $pay_rate['companyNo']; $this->_handle_cgd_caixiao($cgd, $sale, $pay_rate, $cgdNo, $orderCode, $cgd_total,$tempOrder); //下一个渠道公司/原业务公司的 客户信息 $customer= ['No' => $pay_rate['companyNo'], 'name' => $pay_rate['companyName']]; if($updatePrice) $sale_total =$cgd_total; else { $sale_price =$cgd_price; $sale_total = bcmul($cgd_price, $sale['good_num'], 8); } //下一个渠道公司/原业务公司的 销售价未此次的采购单价 //判断是否是最后一个渠道遍历。最后一次需要额外生成一个销售单给原业务公司; if($keys == count($pay_rates)-1){ $orderCode = makeNo($this->Tcode.'QR'); $tempOrder['orderCode'] = $orderCode ; $orderCode = substr($orderCode, 0, -2) . str_pad($this->i, 2, '0', STR_PAD_LEFT); $supplier = ['No' => $sale['supplierNo'], 'name' => $sale['supplierName'],"pay_name"=>$pay_rate['pay_name']]; if(!isset($cgd['cgdNo']) && $sale['is_stock']==1 ){ $origin_total =Db::name('child_bn') ->where([['orderCode','=',$sale['orderCode']],['total_bn_num','>',0],['is_del','=',0]]) ->field("sum(origin_price*total_bn_num) total_price,sum(total_bn_num) num")->findOrEmpty(); if(empty($origin_total)==false && $origin_total['num']>0)$sale['origin_price'] = bcdiv($origin_total['total_price'],$origin_total['num'],8); else $sale['origin_price']=0; }else{ $sale['origin_price'] = $cgd['good_price']; } $this->_handle_sale_caixiao($sale, $orderCode, $cgd['cgdNo'] ?? '', $sale_total,$customer, $supplier,$tempOrder); } //把自己覆盖到上一个记录的供应商记录中 if (isset($this->cgd_insert[$this->cgd_key - 1])) { $temp = $userCommon->handle('hqInfo', ['code' =>$supplier['No']]); $supplier = $temp['data'] ?? []; $this->cgd_insert[$this->cgd_key - 1]['supplierNo'] =$supplier['relation_code']; $this->cgd_insert[$this->cgd_key - 1]['supplier_name'] = $supplier['relation_name']; //处理采购销售同一数据 业务公司的供应商编号数据 $tempOrder['supplierName'] =$supplier['relation_name']; $tempOrder['supplierNo'] = $supplier['relation_code']; $temps = $userCommon->handle('sInfo', ['code' =>$supplier['relation_code']]); $suppliers = $temps['data'] ?? []; $this->cgd_insert[$this->cgd_key - 1]['supplier_persion'] = $suppliers['person'] ?? ''; $this->cgd_insert[$this->cgd_key - 1]['supplier_persionid'] = $suppliers['personid'] ?? 0; } } //清空该变量,以防止多个销售单覆盖数据的情况 // $cgd_insert_tmp_data = array_merge($cgd_insert_tmp_data, $this->cgd_insert); //批量新增改为单次新增 if($this->cgd_insert) Db::name('cgd_caixiao')->insertAll($this->cgd_insert); $this->cgd_insert = []; $this->cgd_key = 0; $date=date('Y-m-d H:i:s'); echo "【{$date}】订单:{$sale['orderCode']}拆单 \r\n"; if(strtotime($sale['addtime']) >= strtotime('2024-02-01 00:00:00'))$commonOrder[]= $tempOrder; } // if ($cgd_insert_tmp_data) Db::name('cgd_caixiao')->insertAll($cgd_insert_tmp_data); if ($this->sale_insert) Db::name('sale_caixiao')->insertAll($this->sale_insert); Db::commit(); $this->OrderSave($commonOrder); // $output->writeln('处理完成'); }catch (\Exception $e) { Db::rollback(); $output->writeln('事务回滚:' . $e->getMessage() . '||' . $e->getFile() . '||' . $e->getLine()); } } catch (\Exception $exception) { $output->writeln('脚本执行出错,' . $exception->getMessage() . '||' . $exception->getFile() . '||' . $exception->getLine()); } Cache::store('redis')->dec($key); } //构建销售单 private function _handle_sale_caixiao(array $sale = [], string $orderCode = '', string $cgdNo = '', string $sale_total ="0.00", array $customer = [], array $supplier = [],array &$tempOrder=[]) { $sale_price = $sale['good_num'] > 0 ? bcdiv($sale_total, $sale['good_num'], 8) : 0; $origin_total =round($sale['origin_price']*$sale['good_num'],8); $tmp_sale = [ 'manager'=>$sale['manager'], 'managerid'=>$sale['managerid'], 'origin_price' =>round( $sale['origin_price'] ?? 0, 8), 'origin_plan_price' =>round( $sale['plan_price'] ?? 0, 8), 'total_origin_price' =>$origin_total, 'total_origin_price_plan' => $sale['plan_total'], 'pay_source' => $supplier['pay_name'], 'sale_price' => round($sale_price, 8), 'total_price' => $sale_total, 'post_fee' => $sale['post_fee'] ?? 0, 'is_diff' => $sale['is_diff'] ?? 0, 'send_num' => $sale['send_num'] ?? 0, 'wsend_num' => $sale['wsend_num'] ?? 0, 'workNo' => $sale['workNo'] ?? '', 'poNo' => $sale['platform_order'] ?? '', 'other_orderNo' => $sale['other_orderNo'], 'th_num' => $sale['th_num'] ?? 0, 'send_type' => $sale['send_type'] ?? 0, 'gold_price' => $sale['gold_price'] ?? 0, 'cost_price' => $sale['cost_price'] ?? 0, 'status' => $sale['status'] ?? 0, 'open_type'=>$sale['open_type'], 'updatetime' => $sale['updatetime'], 'delivery_day' => $sale['delivery_day'] ?? 0, 'th_fee' => round(bcmul($sale_price, $sale['th_num'] ?? "0", 3), 2), 'cost_fee' => $sale['cost_price'] ?? 0, 'diff_fee' => $sale['diff_fee'] ?? 0, 'tax' => $sale['tax'] ?? '', 'diff_weight' => $sale['diff_weight'] ?? 0, 'send_status' => $sale['send_status'] ?? 0, ]; $tmp = Db::name('sale_caixiao') ->field('id,orderCode') ->where(['oldCode' => $sale['orderCode'], 'supplierNo' =>$supplier['No'] ?? '', 'customer_code' =>$customer['No'] ?? '']) ->findOrEmpty(); if (!empty($tmp)) { Db::name('sale_caixiao') ->where('id', $tmp['id']) ->update($tmp_sale); if(isset($tempOrder['orderCode']))$tempOrder['orderCode']= $tmp['orderCode']; } else { if(isset($tempOrder['orderCode']))$tempOrder['orderCode']= $orderCode; $this->sale_insert[] = array_merge($tmp_sale, [ 'orderCode' => $orderCode, 'apply_id' => $sale['apply_id'] ?? 0, 'apply_name' => $sale['apply_name'] ?? '', 'order_type' => $sale['order_type'] ?? 0, 'order_source' => $this->order_source, 'platform_id' => $sale['platform_id'] ?? '', 'platform_type' => $sale['use_type'] ?? '', "platform_name"=>$sale['platform_name']??"", 'good_code' => $sale['good_code'] ?? '', 'cat_id' => $sale['cat_id'] ?? 0, 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE), 'good_name' => $sale['good_name'] ?? '', 'good_num' => $sale['good_num'] ?? 0, 'good_type' => $sale['good_type'] ?? 0, 'is_activity' => $sale['is_activity'] ?? 0, 'is_stock' => $sale['is_stock'] ?? 0, 'arrive_time' => $sale['arrive_timefvc'] ?? '', 'customer_code' => $customer['No'] ?? '', 'customer_name' => $customer['name'] ?? '', 'supplierNo' => $supplier['No'] ?? '', 'supplier_name' => $supplier['name'] ?? '', 'zxNo' => $sale['zxNo'] ?? '', 'proof_id' => $sale['proof_id'] ?? 0, 'proof_url' => $sale['proof_url'] ?? '', 'other_orderNo' => $sale['other_orderNo'], 'paytime' => $sale['paytime'] ?? '', 'workNo' => $sale['workNo'] ?? '', 'poNo' => $sale['platform_order'] ?? '', 'use_order' => $sale['use_order'], 'good_weight' => $sale['good_weight'] ?? 0, 'addtime' => $sale['addtime'], 'noble_metal' => $sale['noble_metal'] ?? '', 'brand' => $sale['brand'] ?? '', 'unit' => $sale['unit'] ?? '', 'cost_desc' => $sale['cost_desc'] ?? '', 'noble_weight' => $sale['noble_weight'] ?? 0, 'tax' => $sale['tax'] ?? '', 'lead_time' => $sale['lead_time'] ?? 0, 'depart' => isset($sale['apply_id']) ? get_company_name_by_uid($sale['apply_id']) : '', 'cgdNo' => $cgdNo, 'total_origin_price' => $origin_total, 'total_origin_price_plan' => $sale['plan_total'], 'pay_id' => $sale['pay_id'], 'pay_source' => $supplier['pay_name'], 'oldCode' => $sale['orderCode'], ]); } } //构建采购单 private function _handle_cgd_caixiao(array $cgd = [], array $sale = [], array $pay_rate = [], string $cgdNo = '', string $orderCode = '', string $cgd_total ="0.00",array &$tempOrder=[]) { $good_price = $sale['good_num'] > 0 ? bcdiv($cgd_total, $sale['good_num'], 8) : 0; $tmp_cgd = [ 'good_price' => round($good_price, 8), 'total_fee' => $cgd_total, 'origin_total' => $cgd_total, 'pakage_fee' => $cgd['pakge_fee'] ?? 0, 'open_fee' => $cgd['open_fee'] ?? 0, 'cert_fee' => $cgd['cert_fee'] ?? 0, 'delivery_fee' => $cgd['delivery_fee'] ?? 0, 'mark_fee' => $cgd['mark_fee'] ?? 0, 'teach_fee' => $cgd['teach_fee'] ?? 0, 'nake_fee' => $cgd['nake_fee'] ?? 0, 'demo_fee' => $cgd['demo_fee'] ?? 0, 'weight' => $cgd['weight'] ?? 0, 'diff_weight' => $sale['diff_weight'] ?? 0, 'diff_fee' => $sale['diff_fee'] ?? 0, 'gold_price' => $sale['gold_price'] ?? 0, 'send_num' => $sale['send_num'] ?? 0, 'wsend_num' => $sale['wsend_num'] ?? 0, 'status' => $cgd['status'] ?? 3, 'order_type' => $sale['order_type'], 'order_source' => $this->order_source, 'good_type' => $sale['good_type'] ?? '', 'last_time' => $cgd['last_time'] ?? '', 'send_type' => $sale['send_type'] ?? '', 'send_status' => $sale['send_status'] ?? '', "open_type"=>$sale['open_type'], 'th_num' => $sale['th_num'] ?? 0, 'th_fee' => round(bcmul($good_price, $sale['th_num'] ?? "0", 8), 2), 'updatetime' => $sale['updatetime'], ]; $tmp = Db::name('cgd_caixiao') ->field('id,cgdNo,supplier_name,supplierNo') ->where(['oldCode' => $sale['orderCode'], 'companyNo' => $pay_rate['companyNo']]) ->findOrEmpty(); if (!empty($tmp)) { Db::name('cgd_caixiao') ->where('id', $tmp['id']) ->update($tmp_cgd); if(isset($tempOrder['cgdNo'])){ $tempOrder['cgdNo']= $tmp['cgdNo']; $tempOrder['supplierName'] =$tmp['supplier_name']; $tempOrder['supplierNo'] = $tmp['supplierNo']; } } else { if(isset($tempOrder['cgdNo']))$tempOrder['cgdNo']=$cgdNo; $this->cgd_insert[$this->cgd_key] = array_merge($tmp_cgd, [ 'cgdNo' => $cgdNo, 'bkcode' => $cgd['bkcode'] ?? '', 'wsm_code' => $cgd['wsm_code'] ?? '', 'cgder' => $sale['cgder'] ?? '', 'cgder_id' => $sale['cgderid'] ?? 0, 'depart' => isset($sale['cgderid']) ? get_company_name_by_uid($sale['cgderid']) : '', 'qrdNo' => $orderCode, 'platform_id' => $sale['platform_id'] ?? '', 'platform_name'=>$sale['platform_name']??'', 'spuCode' => $cgd['spuCode'] ?? $sale['good_code'], 'good_name' => $sale['good_name'], 'skuCode' => $cgd['skuCode'] ?? '', 'good_num' => $sale['good_num'] ?? 0, 'cat_name' => json_encode($this->_get_cat_list($sale['cat_id']), JSON_UNESCAPED_UNICODE), 'companyNo' => $pay_rate['companyNo'], 'companyName' => $pay_rate['companyName'], 'supplierNo' => $sale['supplierNo'], 'supplier_name' => $sale['supplierName'], 'bktime' => $cgd['bktime'] ?? '', 'bkcreater' => $cgd['bkcreater'] ?? '', 'noble_metal' => $sale['noble_metal'] ?? '', 'brand' => $sale['brand'] ?? '', 'unit' => $sale['unit'] ?? '', 'cost_desc' => $sale['cost_desc'] ?? '', 'noble_weight' => $sale['noble_weight'] ?? '', 'tax' => $sale['tax'] ?? '', 'is_stock' => $sale['is_stock'], 'delivery_day' => $sale['delivery_day'] ?? 0, 'lead_time' => $sale['lead_time'] ?? 0, 'is_diff' => $sale['is_diff'], 'addtime' => $sale['addtime'], 'supplier_persion' => '', 'supplier_persionid' => '', 'pay_id' => $sale['pay_id'], 'oldCode' => $sale['orderCode'] ]); $this->cgd_key++; } } //获取分类层级信息,id、cat_name(分类名称)和fund_code(财务核算码) private function _get_cat_list($var, $data = []) { $str = Db::name('cat') ->field('id,cat_name,fund_code,pid') ->where(['id' => $var]) ->findOrEmpty(); if ($str == false) return []; $vmn = []; $vmn['id'] = $str['id']; $vmn['cat_name'] = $str['cat_name']; $vmn['fund_code'] = $str['fund_code']; array_unshift($data, $vmn); if ($str['pid'] == 0) return $data; else return $this->_get_cat_list($str['pid'], $data); } //处理采购销售同一数据 录入结算 统一数据表中 protected function OrderSave($order){ if(empty($order)) return ; foreach ($order as &$item){ $com = $this->comOrder->where('cxCode',$item['cxCode'])->findOrEmpty(); if($com->isEmpty()==false){ $item['id'] = $com->id; $item['tyCode'] = $com->tyCode; if(in_array($com->status,[0,1,2]))$item['status']=$com->status; }else{ $item['id']=null; } } $this->comOrder->saveAll($order); } }