post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :""; if($ordeCode==''){ return error_show(1004,"参数orderCode 不能为空"); } $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find(); if(empty($order)){ return error_show(1005,"未找到订单数据"); } $retrun =Db::name("sale_return")->where([["orderCode","=",$ordeCode],["is_del","=",0],["status","<",4]]) ->count(); if($retrun>0){ return error_show(1005,"存在未完成退货订单数据"); } if($order['order_type']==3|| $order['order_type']==4){ $goon = Db::name("good_zixun")->where(["spuCode"=>$order['good_code'],"is_del"=>0])->find(); }else { $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left') ->where(['a.skuCode' => $order['skuCode']])->find(); } if($goon==false){ return error_show(1005,"未找到商品数据"); } $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find(); if($supplier==false){ return error_show(1005,"未找到商品供应商数据"); } $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :""; if($errorCode==''){ return error_show(1004,"参数errorCode 不能为空"); } $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find(); if(empty($error)){ return error_show(1005,"未找到退货原因数据"); } $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :""; $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :""; $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):""; if($token==''){ return error_show(105,"参数token不能为空"); } $user =GetUserInfo($token); if(empty($user)||$user['code']!=0){ return error_show(1002,"申请人数据不存在"); } $is_addr=0; $rm= isset($user["data"]['id']) ? $user["data"]['id'] : ""; $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : ""; $returnadr =isset($this->post['returnAddr'])&& !empty($this->post['returnAddr']) ? $this->post['returnAddr']:""; if($returnadr!=''){ $thnum=array_sum(array_column($returnadr,"return_num")); $is_addr=1; } $returnCode=makeNo("RS"); Db::startTrans(); try{ $in = [ "returnCode"=>$returnCode, "orderCode"=>$ordeCode, "good_code"=>$order['good_code'], "good_name"=>$order['good_name'], "apply_id"=>$rm, "apply_name"=>$ri, "cgderid"=>$supplier['personid'], "cgder"=>$supplier['person'], "error_code"=>$errorCode, "num"=>$thnum, "total_fee"=>round($order['sale_price']*$thnum,2), "good_price"=>$order['sale_price'], "platform_id"=>$order['platform_id'], "remark"=>$remark, "order_type"=>$order['order_type'], "is_addr"=>$is_addr, "status"=>$order['is_stock']==1?4:1, "is_del"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $create = Db::name("sale_return")->insert($in,true); if($create>0){ $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"]; ActionLog::logAdd($this->post['token'],$stn,"XSTHD",$in['status'],$in); $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0]; ProcessOrder::AddProcess($this->post['token'],$process); if($returnadr!=""){ $inf=[]; foreach ($returnadr as $val){ $temp=[]; $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find(); if($addrinfo==false){ Db::rollback(); return error_show(1005,"地址信息未找到"); } $send =Db::name('order_out')->where([["addrid","=",$val['id']],["status",">=",2]])->find(); if($send!=false){ Db::rollback(); return error_show(1005,"地址已发货"); } if($order['is_stock']==1){ if ($addrinfo['receipt_quantity'] < $val['return_num']) { Db::rollback(); return error_show(1004, "地址发货数量不足"); } $addrinfo['receipt_quantity'] -= $val['return_num']; $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0; $addrinfo['updatetime'] = date("Y-m-d H:i:s"); $addrup = Db::name("order_addr")->save($addrinfo); if ($addrup == false) { Db::rollback(); return error_show(1004, "地址发货数量更新失败"); } $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find(); if ($out == false) { Db::rollback(); return error_show(1004, "地址发货单数据未找到"); } // if ($send!=false) { if ($out['status'] >= 2) { Db::rollback(); return error_show(1004, "地址发货单已发货"); } if ($out['send_num'] < $val['return_num']) { Db::rollback(); return error_show(1004, "地址发货单发货数量不足"); } $out['send_num'] -= $val['return_num']; $out['updatetime'] = date("Y-m-d H:i:s"); $outup = Db::name("order_out")->save($out); if ($outup == false) { Db::rollback(); return error_show(1004, "地址发货单更新失败"); } $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find(); if ($ordersend['send_num'] < $val['return_num']) { Db::rollback(); return error_show(1004, "发货单发货数量不足"); } $ordersend['send_num'] -= $val['return_num']; $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1; $ordersend['updatetime'] = date("Y-m-d H:i:s"); $sendip = Db::name("order_send")->save($ordersend); if ($sendip == false) { Db::rollback(); return error_show(1004, "发货单更新失败"); } // } } $temp['returnCode']=$returnCode; $temp['orderCode']=$ordeCode; $temp['outCode']=isset($send['outCode'])?$send['outCode']:""; $temp['addrid']=$val['id']; $temp['send_num']=$addrinfo['receipt_quantity']; $temp['return_num']=$val['return_num']; $temp['is_del']=0; $temp['addtime']=date("Y-m-d H:i:s"); $temp['updatetime']=date("Y-m-d H:i:s"); $inf[]=$temp; } $inadd=Db::name("sale_returnaddr")->insertAll($inf); if($inadd==0){ Db::rollback(); return error_show(1005,"退货单新建失败"); } } if($order['is_stock']==1){ if ($order['wsend_num'] < $thnum) { Db::rollback(); return error_show(1004, "销售单未发货数量不足退货"); } $lor=$order['status'] ; $order['wsend_num'] -= $thnum; $order['send_num'] += $thnum; $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1); $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2); $order['th_num'] += $thnum; if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){ $order['status']=3; } $order['th_fee'] += round($thnum * $order['sale_price'], 2); $order['updatetime'] = date("Y-m-d H:i:s"); $uap = Db::name("sale")->save($order); if ($uap == false) { Db::rollback(); return error_show(1005, '销售单订单更新失败'); } ActionLog::logAdd($this->post['token'], [ "order_code" => $order["orderCode"],//出库单号 "status" => $lor,//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "status"//新建create,编辑edit,更改状态status ], "XSQRD", $order['status'], $order); ProcessOrder::AddProcess($this->post['token'], [ "order_type" => 'XSQRD', "order_code" =>$order["orderCode"],//出库单号 "order_id" => $order["id"], "order_status" =>$order['status'],"before_status"=>$lor ]); $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find(); if ($ordernum == false) { Db::rollback(); return error_show(1005, '未找到关联采购单'); } $ordernum['send_num'] -= $thnum; $orderup = Db::name("order_num")->save($ordernum); if ($orderup == false) { Db::rollback(); return error_show(1005, '关联数据更新失败'); } $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find(); if ($cgd == false) { Db::rollback(); return error_show(1005, '未找到采购单数据'); } $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2); $cgd['th_num'] += $thnum; $cgd['updatetime'] = date("Y-m-d H:i:s"); $cgdup = Db::name("purchease_order")->save($cgd); if ($cgdup == false) { Db::rollback(); return error_show(1005, '采购单数据更新失败'); } if ($cgd['bkcode'] != "") { $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "is_del" => 0]) ->find(); if ($bk == false) { Db::rollback(); return error_show(1005, '未找到备库单数据'); } $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find(); if ($orderbk == false) { Db::rollback(); return error_show(1005, '备库单未完全入库'); } $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" => 5, "is_del" => 0]) ->field("sum(send_num)-sum(th_num) as num")->find(); $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num']; $orderbk['merge_num'] = $merge_num['num']; $orderbk['updatetime'] = date("Y-m-d H:i:s"); $orderbkup = Db::name("order_bk")->save($orderbk); if ($orderbkup == false) { Db::rollback(); return error_show(1005, '备库单库存数据释放失败'); } } $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code']]) ->find(); if (empty($stock)) { $stock = [ "spuCode" => $order['good_code'], "wsm_code" => $cgd['wsm_code'], "usable_stock" => 0, "wait_out_stock" => 0, "wait_in_stock" => 0, "total_stock" => 0, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s"), ]; } $stock['usable_stock'] += $thnum; $stock['wait_out_stock'] -= $thnum; $stock['updatetime'] = date("Y-m-d H:i:s"); $st_up = Db::name("good_stock")->save($stock); if ($st_up == false) { return error_show(1005, '可售商品入库失败'); } $good_data[]= ['good_log_code' => $returnCode, "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"]; $good_data[]= ['good_log_code' => $returnCode, "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"]; GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD'); } Db::commit(); return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]); } Db::rollback(); return error_show(1005,"退货单新建失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1005,$e->getMessage()); } } public function list(){ $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1"; $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10"; $where =[['is_del',"=",0]]; $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):""; if($bkcode!=""){ $where[]=['returnCode',"like", "%{$bkcode}%"]; } $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):""; if($status!==""){ $where[]=['status',"=", $status]; } $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode']) :""; if($orderCode!=""){ $where[]=['orderCode',"like", "%{$orderCode}%"]; } $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']) :""; if($apply_name!=""){ $where[]=['apply_name',"like", "%{$apply_name}%"]; } $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:""; if($start!==""){ $where[]=['addtime',">=", $start.' 00:00:00']; } $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:""; if($end!==""){ $where[]=['addtime',"<=", $end.' 23:59:59']; } $role=$this->checkRole(); $condition=''; if(!empty($role['write']) && $this->uid!=""){ // $where[]=["a.apply_id","in",$role['write']]; $condition .="cgderid = {$this->uid} or apply_id in (".implode(',',$role['write']).")"; } $count=Db::name("sale_return")->where($where)->where($condition)->count(); $total = ceil($count/$size); $page = $total>=$page ? $page :$total; $list = Db::name("sale_return")->where($where)->where($condition)->order("addtime desc")->page($page,$size)->select(); // echo Db::name("sale_return")->getLastSql(); $data=[]; foreach ($list as $value){ $value['error_msg']=''; if($value['error_code']!=''){ $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find(); $value['error_msg']= isset($error['result'])?$error['result']:""; } $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find(); $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0; $value['return_total'] =$value['sale_price']*$value['num'] ; $value['total_num'] =$order['good_num'] ; $data[]=$value ; } return app_show(0,"获取成功",['list'=>$data,"count"=>$count]); } public function info(){ $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']) :""; if($code==""){ return error_show(1004,"参数returnCode不能为空"); } $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find(); if(empty($info)){ return error_show(1004,"未找到退货数据"); } $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find(); if($orderinfo['order_type']==3|| $orderinfo['order_type']==4){ $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find(); }else { $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left') ->where(['a.skuCode' => $orderinfo['skuCode']])->find(); } if ($goon==false) { return error_show(1003, "未找到商品数据"); } $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : []; $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0'; $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:''; $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:''; $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:''; $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:''; $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:''; $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0'; $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0'; $info['return_total'] = $info['sale_price']*$info['num'] ; $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0'; $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0'; $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:''; $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0'; $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0'; $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:''; $info['customer_name']=''; if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){ $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find(); $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:""; } $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:''; $info['supplier_name']=''; if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){ $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find(); $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:""; } $info['platform_name']=''; $info['platform_id']=$orderinfo['platform_id']; if($orderinfo['platform_id']!=0){ $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find(); $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:""; } $info['cgd_wsend']=""; $info['cgd_send']=""; $info['cgd_total']=""; if($orderinfo['order_type']==2){ $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find(); $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0; $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0; $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0; } $info['error_msg']=''; if($info['error_code']!=''){ $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find(); $info['error_msg']= isset($error['result'])?$error['result']:""; } if($info['return_wsm']!=""){ $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code") ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find(); } $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:""; // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:""; // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:""; $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray(); $wsm=[]; if(!empty($wsm_return)){ foreach ($wsm_return as $value){ $value['wsm_name']=""; $value['wsm_supplier']=""; $value['wsm_supplierNo']=""; if($value['wsm_code']!=""){ $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code") ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find(); $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:""; $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:""; $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:""; } $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find(); $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0; $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num"); $value['wsm_send'] = $send ?? 0; $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send']; $wsm[]=$value; } } $info['wsminfo']=$wsm; $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray(); $addrinfo=[]; if(!empty($addr)){ foreach ( $addr as $value){ $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find(); $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:""; $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:""; $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:""; $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:""; $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:""; $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:""; $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ; $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ; $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode']])->sum("send_num"); $value['addr_send'] = $send ?? 0; $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send']; $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find(); $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:""; $addrinfo[]=$value; } } $info['addrinfo']=$addrinfo; $info['can']=$int; return app_show(0,"获取成功",$info); } /** * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function delete(){ $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']) :""; if($code==""){ return error_show(1004,"参数returnCode不能为空"); } $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find(); if(empty($info)){ return error_show(1004,"未找到退货数据"); } $info["is_del"]=1; $info["updatetime"]=date("Y-m-d H:i:s"); $del = Db::name("sale_return")->save($info); if($del){ $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"]; ActionLog::logAdd($this->post['token'],$ste,"XSTHD",0,$ste); $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"]; ProcessOrder::workdel($process); return app_show(0,"删除成功"); }else{ return error_show(1004,"删除失败"); } } /**审核 * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function exam(){ $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']) :""; if($code==""){ return error_show(1004,"参数returnCode不能为空"); } $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find(); if(empty($info)){ return error_show(1004,"未找到退货数据"); } $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :""; if($status===""){ return error_show(1004,"参数status不能为空"); } $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :""; $var = $info['status']; $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):""; $apply_id =GetUserInfo($token); if(empty($apply_id)||$apply_id['code']!=0){ return error_show(1002,"申请人数据不存在"); } $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : ""; $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : ""; $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find(); if($orderinfo==false){ return error_show(1004,"未找到订单数据"); } if($status==4){ if($info['is_addr']==1){ $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select() ->toArray(); if(empty($addr)){ return error_show(1004,"未找到发货单地址数据"); } } } if($status==3){ $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):""; if($is_th===""){ return error_show(1004,"参数is_th不能为空"); } $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):""; if($is_th==0){ if($return_wsm===""){ return error_show(1004,"参数return_wsm 不能为空"); } $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find(); if($wsmcode==false){ return error_show(1004,"为找到仓库数据"); } } $info['return_wsm'] =$return_wsm ; $info['is_th'] =$is_th ; } Db::startTrans(); try{ $temp= $info['status']; $info['status'] =$status; $remark!=""? $info['remark'] =$remark:""; $info["updatetime"]=date("Y-m-d H:i:s"); $up = Db::name("sale_return")->save($info); if($up) { $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type"=>"XSTHD",'before_status'=>$temp]; ProcessOrder::AddProcess($this->post['token'], $process); $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"]; ActionLog::logAdd($this->post['token'], $ste, "XSTHD", $status, $info); if ($status == 4) { if ($info['is_addr'] == 1) { if (isset($addr) && !empty($addr)) { foreach ($addr as $value) { $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find(); if ($addrinfo == false) { Db::rollback(); return error_show(1004, "地址数据未找到"); } if ($addrinfo['receipt_quantity'] < $value['return_num']) { Db::rollback(); return error_show(1004, "地址发货数量不足"); } $addrinfo['receipt_quantity'] -= $value['return_num']; $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0; $addrinfo['updatetime'] = date("Y-m-d H:i:s"); $addrup = Db::name("order_addr")->save($addrinfo); if ($addrup == false) { Db::rollback(); return error_show(1004, "地址发货数量更新失败"); } if ($value['outCode'] != "") { $out = Db::name("order_out")->where(["outCode" => $value['outCode'], "is_del" => 0]) ->find(); if ($out == false) { Db::rollback(); return error_show(1004, "地址发货单数据未找到"); } if ($out['status'] >= 2) { Db::rollback(); return error_show(1004, "地址发货单已发货"); } if ($out['send_num'] < $value['return_num']) { Db::rollback(); return error_show(1004, "地址发货单发货数量不足"); } $out['send_num'] -= $value['return_num']; $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0; $out['updatetime'] = date("Y-m-d H:i:s"); $outup = Db::name("order_out")->save($out); if ($outup == false) { Db::rollback(); return error_show(1004, "地址发货单更新失败"); } $ordersend = Db::name("order_send")->where(["outCode" => $value['outCode']])->find(); if ($ordersend['send_num'] < $value['return_num']) { Db::rollback(); return error_show(1004, "发货单发货数量不足"); } $ordersend['send_num'] -= $value['return_num']; $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1; $ordersend['updatetime'] = date("Y-m-d H:i:s"); $sendip = Db::name("order_send")->save($ordersend); if ($sendip == false) { Db::rollback(); return error_show(1004, "发货单更新失败"); } } } } } if ($orderinfo['wsend_num'] < $info['num']) { Db::rollback(); return error_show(1004, "销售单未发货数量不足退货"); } $lor=$orderinfo['status']; $orderinfo['wsend_num'] -= $info['num']; $orderinfo['send_num'] += $info['num']; $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1); $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2); if ($orderinfo['is_stock'] == 1) { $orderinfo['th_num'] += $info['num']; if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){ $orderinfo['status']=3; } $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2); $orderinfo['updatetime'] = date("Y-m-d H:i:s"); $uap = Db::name("sale")->save($orderinfo); if ($uap == false) { Db::rollback(); return error_show(1005, '销售单订单更新失败'); } $ordernum = Db::name("order_num")->where(['orderCode' => $info['orderCode']])->find(); if ($ordernum == false) { Db::rollback(); return error_show(1005, '未找到关联采购单'); } $ordernum['send_num'] -= $info['num']; $orderup = Db::name("order_num")->save($ordernum); if ($orderup == false) { Db::rollback(); return error_show(1005, '关联数据更新失败'); } $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find(); if ($cgd == false) { Db::rollback(); return error_show(1005, '未找到采购单数据'); } $cgd['th_fee'] += round($cgd['good_price'] * $info['num'], 2); $cgd['th_num'] += $info['num']; if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){ $cgd['status']=4; } $cgd['updatetime'] = date("Y-m-d H:i:s"); $cgdup = Db::name("purchease_order")->save($cgd); if ($cgdup == false) { Db::rollback(); return error_show(1005, '采购单数据更新失败'); } if ($cgd['bkcode'] != "") { $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "is_del" => 0]) ->find(); if ($bk == false) { Db::rollback(); return error_show(1005, '未找到备库单数据'); } $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find(); if ($orderbk == false) { Db::rollback(); return error_show(1005, '备库单未完全入库'); } $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" => 5, "is_del" => 0]) ->field("sum(send_num)-sum(th_num) as num")->find(); $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num']; $orderbk['merge_num'] = $merge_num['num']; $orderbk['updatetime'] = date("Y-m-d H:i:s"); $orderbkup = Db::name("order_bk")->save($orderbk); if ($orderbkup == false) { Db::rollback(); return error_show(1005, '备库单库存数据释放失败'); } } } else { if ($info['is_th'] == 1) { $orderinfo['th_num'] += $info['num']; if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){ $orderinfo['status']=3; } $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2); $orderinfo['updatetime'] = date("Y-m-d H:i:s"); $uap = Db::name("sale")->save($orderinfo); if ($uap == false) { Db::rollback(); return error_show(1005, '销售单订单更新失败'); } $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find(); if ($ordernum == false) { Db::rollback(); return error_show(1005, '未找到关联采购单'); } $ordernum['send_num'] -= $info['num']; $orderup = Db::name("order_num")->save($ordernum); if ($orderup == false) { Db::rollback(); return error_show(1005, '关联数据更新失败'); } $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find(); if ($cgd == false) { Db::rollback(); return error_show(1005, '未找到采购单数据'); } $lor=$cgd['status']; if($cgd['wsend_num']<$info['num']){ $cgd['wsend_num']=0; $cgd['send_num'] += $info['num']-$cgd['wsend_num']; }else{ $cgd['wsend_num'] -= $info['num']; $cgd['send_num'] += $info['num']; } $cgd['status'] = $cgd['wsend_num']==0?2:($cgd['send_num']==0?0:1); $cgd['send_status'] =$cgd['wsend_num']==0?3:($cgd['send_num']==0?1:2); $cgd['th_num'] += $info['num']; if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){ $cgd['status']=4; } $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2); $cgd['updatetime'] = date("Y-m-d H:i:s"); $cgdup = Db::name("purchease_order")->save($cgd); if ($cgdup == false) { Db::rollback(); return error_show(1005, '采购单数据更新失败'); } $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find(); if ($stock==false) { Db::rollback(); return error_show(1005, '商品仓库未找到'); } if($stock['wait_in_stock']<$info['num']){ $stock['wait_in_stock']=0; if( $stock['usable_stock']>$info['num']-$stock['wait_in_stock']){ $stock['usable_stock'] -= $info['num']-$stock['wait_in_stock']; }else{ $stock['usable_stock'] = 0; $stock['wait_out_stock']-= $info['num']-$stock['wait_in_stock']- $stock['usable_stock'] ; } $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock']; }else{ $stock['wait_in_stock']-=$info['num']; } $stock['updatetime'] = date("Y-m-d H:i:s"); $st_up = Db::name("good_stock")->save($stock); if ($st_up == false) { return error_show(1005, '可售商品入库失败'); } } else { $orderinfo['th_num'] += $info['num']; if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){ $orderinfo['status']=3; } $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2); $orderinfo['updatetime'] = date("Y-m-d H:i:s"); $uap = Db::name("sale")->save($orderinfo); if ($uap == false) { Db::rollback(); return error_show(1005, '销售单订单更新失败'); } $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find(); if ($ordernum == false) { Db::rollback(); return error_show(1005, '未找到关联采购单'); } $ordernum['send_num'] -= $info['num']; $ordernum['wsend_num'] = $info['num']; $orderup = Db::name("order_num")->save($ordernum); if ($orderup == false) { Db::rollback(); return error_show(1005, '关联数据更新失败'); } $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find(); if ($cgd == false) { Db::rollback(); return error_show(1005, '未找到采购单数据'); } $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find(); if ($stock==false) { Db::rollback(); return error_show(1005, '商品仓库未找到'); } if($stock['usable_stock']+$stock['wait_out_stock']<$info['num']){ Db::rollback(); return error_show(1005, '商品可用库存不足退回数量'); }else{ if($stock['usable_stock']>$info['num']){ $stock['usable_stock'] -= $info['num']; }else{ $stock['usable_stock'] = 0; $stock['wait_out_stock']-= $info['num']- $stock['usable_stock'] ; } $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock']; } $stock['updatetime'] = date("Y-m-d H:i:s"); $st_up = Db::name("good_stock")->save($stock); if ($st_up == false) { return error_show(1005, '可售商品入库失败'); } } } ActionLog::logAdd($this->post['token'], [ "order_code" => $orderinfo["orderCode"],//出库单号 "status" => $lor,//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "status"//新建create,编辑edit,更改状态status ], "XSQRD", $orderinfo['status'], $orderinfo); ProcessOrder::AddProcess($this->post['token'], [ "order_type" => 'XSQRD', "order_code" =>$orderinfo["orderCode"],//出库单号 "order_id" => $orderinfo["id"], "order_status" =>$orderinfo['status'],"before_status"=>$lor ]); if($orderinfo['is_stock'] == 1 || $info['is_th'] == 0){ $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm']; if ($wsmcode == "") { Db::rollback(); return error_show(1005, '未找到退货仓库'); } //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存 $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' => $wsmcode])->find(); if (empty($stock)) { $stock = [ "spuCode" => $info['good_code'], "wsm_code" => $wsmcode, "usable_stock" => 0, "wait_out_stock" => 0, "wait_in_stock" => 0, "total_stock" => 0, "addtime" => date("Y-m-d H:i:s"), "updatetime" => date("Y-m-d H:i:s"), ]; } $stock['usable_stock'] += $info['num']; $stock['updatetime'] = date("Y-m-d H:i:s"); $st_up = Db::name("good_stock")->save($stock); if ($st_up == false) { return error_show(1005, '可售商品入库失败'); } $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $info['num'], "stock_name" => "usable_stock"]; GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD'); } } } Db::commit(); return app_show(0,"更新成功"); }catch (\Exception $e){ Db::rollback(); return error_show(1004,$e->getMessage()."|".$e->getLine()); } } public function zxcreate(){ $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :""; if($ordeCode==''){ return error_show(1004,"参数orderCode 不能为空"); } $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find(); if(empty($order)){ return error_show(1005,"未找到订单数据"); } $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :""; if($errorCode==''){ return error_show(1004,"参数errorCode 不能为空"); } $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find(); if(empty($error)){ return error_show(1005,"未找到退货原因数据"); } $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :""; $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):""; if($token==''){ return error_show(105,"参数token不能为空"); } $user =GetUserInfo($token); if(empty($user)||$user['code']!=0){ return error_show(1002,"申请人数据不存在"); } $rm= isset($user["data"]['id']) ? $user["data"]['id'] : ""; $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : ""; $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):""; if($num==''){ return error_show(1005,"参数num不能为空"); } if($order['wsend_num']<$num){ return error_show(1002,"仓库未发货数量不足退货"); } $returnCode=makeNo("RS"); Db::startTrans(); try{ $in = [ "returnCode"=>$returnCode, "orderCode"=>$ordeCode, "good_code"=>$order['good_code'], "good_name"=>$order['good_name'], "apply_id"=>$rm, "apply_name"=>$ri, "error_code"=>$errorCode, "num"=>$num, "remark"=>$remark, "order_type"=>2, "status"=>0, "is_del"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $create = Db::name("sale_return")->insert($in,true); if($create>0) { $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0]; ProcessOrder::AddProcess($this->post['token'],$process); $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'', "action_type"=>"create"]; ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in); if ($order['send_type'] == 1) { $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type" =>2])->select(); // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]); if(!empty($wsend)){ foreach ($wsend as $value){ $tt = $value['status']; $value['status']=0; $value['updatetime']=date("Y-m-d H:i:s"); $up =Db::name("order_out")->save($value); if($up){ $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt]; ProcessOrder::AddProcess($this->post['token'],$process); $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"]; ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value); }else{ Db::rollback(); return error_show(1005,"退货单新建失败"); } } } } Db::commit(); return error_show(0,"退货单新建成功"); } Db::rollback(); return error_show(1005,"退货单新建失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1005,$e->getMessage()); } } }