post=$this->request->post(); } 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 =[['a.is_del',"=",0],['b.is_del',"=",0]]; $wsm_in_code=isset($this->post['wsm_in_code']) && $this->post['wsm_in_code']!==""? trim($this->post['wsm_in_code']) :""; if($wsm_in_code!==""){ $where[]=["a.wsm_in_code",'like',"%$wsm_in_code%"]; } $cgdNo= isset($this->post['cgdNo']) && $this->post['cgdNo'] !==""? trim($this->post['cgdNo']) :""; if($cgdNo!==""){ $where[]=['a.cgdNo','like',"%$cgdNo%"]; } $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? trim($this->post['apply_name']):""; if($apply_name!==""){ $where[]=['a.apply_name','like',"%$apply_name%"]; } $apply_id= isset($this->post['apply_id']) && $this->post['apply_id'] !==""? trim($this->post['apply_id']):""; if($apply_id!==""){ $where[]=['a.apply_id','=',$apply_id]; } $status= isset($this->post['status']) && $this->post['status'] !==""? trim($this->post['status']):""; if($status!==""){ $where[]=['a.status','=',$status]; } $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:""; if ($start !=="") { $where[]= ["a.sendtime",'>=',$start]; } $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :""; if($end !==""){ $where[]= ["a.sendtime",'<=',$end]; } $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : ""; if ($order_type !== "") { $where[] = ["b.order_type", '=', $order_type]; } $order_source = isset($this->post['order_source']) && $this->post['order_source'] !== "" ? intval($this->post['order_source']) : ""; if ($order_source !== "") { $where[] = ["b.order_source", '=', $order_source]; } $good_type_code=isset($this->post['good_type_code']) && $this->post['good_type_code']!==""? trim($this->post['good_type_code']):""; if($good_type_code !==""){ $where[]= ["b.spuCode",'like',"%$good_type_code%"]; } $good_name=isset($this->post['good_name']) && $this->post['good_name']!==""? trim($this->post['good_name']):""; if($good_name !==""){ $where[]= ["b.good_name",'like',"%$good_name%"]; } $supplie=isset($this->post['supplier']) && $this->post['supplier']!==""? trim($this->post['supplier']):""; if($supplie !==""){ $where[]= ["b.supplierNo",'like',"%$supplie%"]; } $wsm_code=isset($this->post['wsm_code']) && $this->post['wsm_code']!==""? trim($this->post['wsm_code']):""; if($wsm_code !==""){ $where[]= ["a.wsm_code",'=',$wsm_code]; } $supplierNo =isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']):""; if($supplierNo!=""){ $supplier = Db::name("supplier")->where(["code"=>$supplierNo])->find(); if(empty($supplier)){ return error_show(1004,"未找到供应商信息"); } $wsmcode = Db::name("warehouse_info")->where([ "is_del"=>0,"supplierNo"=>$supplierNo])->column("wsm_code"); $where[]=['a.wsm_code',"in",$wsmcode]; } $condition=[]; $role = $this->checkDataShare(); $hand = resign_hand_user($this->uid,0); if (!empty($role[DataGroupModel::$type_全部])) { $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部])); $condition[]= ["sip.personid","in",$arr]; $condition[]= ["a.apply_id","in",$hand]; $condition[]= ["c.contactor","in",$hand]; // $condition.="sip.personid in (".implode(",", // $role[DataGroupModel::$type_全部]).") or a.apply_id in {$hand} or c.contactor in {$hand}"; } $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : ""; if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)]; //$count = Db::name('purchease_in')->alias('a')->join("purchease_order b","b.cgdNo=a.cgdNo","left") $count = Db::name('purchease_in') ->alias('a') ->join("purchease_order b","b.cgdNo=a.cgdNo","left") ->leftJoin("warehouse_info c","a.wsm_code=c.wsm_code") ->leftJoin('supplier sip', 'sip.code=b.supplierNo') ->where($where) // ->whereOr($condition) ->where(function ($query)use ($condition){$query->whereOr($condition);}) ->count(); $total = ceil($count/$size); $page = $page >= $total ? $total : $page; $list = Db::name('purchease_in') ->alias('a') ->join("purchease_order b","b.cgdNo=a.cgdNo","left") ->leftJoin('supplier sip', 'sip.code=b.supplierNo') ->leftJoin("warehouse_info c","a.wsm_code=c.wsm_code") ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0") ->where($where) ->where(function ($query)use ($condition){$query->whereOr($condition);}) ->page($page,$size) ->field("a.apply_name,a.sendtime,b.order_type,b.order_source,a.status,a.cgdNo, a.wsm_in_code,a.send_num,a.wsm_code,b.good_name,b.spuCode,a.post_company,a.post_code, a.post_fee,a.apply_id,a.wsm_num,a.error_num,a.wait_num,a.status,a.check_name,a.checkid, b.bkcode,b.supplierNo,u.itemid,sip.personid as cgder_id,sip.person as cgder,b.good_createrid,b.good_creater") ->order("a.addtime desc") ->cursor(); $data=[]; foreach ($list as $value){ //获取规格、品牌信息 if ($value['order_type'] == 3) { $temp = Db::name('good_zixun') ->alias('gz') ->field('gz.specinfo,b.brand_name') ->join('brand b', 'b.id=gz.brand_id') ->where(['gz.spuCode' => $value['spuCode'], 'gz.is_del' => 0]) ->find(); $value["brand_name"] = $temp['brand_name']; $value["speclist"] = json_decode($temp['specinfo'], true); } else { $value['brand_name'] = Db::name('good_basic') ->alias('gb') ->join('brand b', 'b.id=gb.brand_id') ->where(['gb.spuCode' => $value['spuCode']]) ->value('b.brand_name'); $spec = Db::name("good_spec") ->where(["spuCode" => $value['spuCode'], "is_del" => 0]) ->select() ->toArray(); $speclist = []; if (!empty($spec)) { foreach ($spec as $val) { $temp = []; $temp['id'] = $val['id']; $temp['spuCode'] = $val['spuCode']; $temp['spec_id'] = $val['spec_id']; $temp['spec_value_id'] = $val['spec_value_id']; $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find(); $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : ""; $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find(); $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : ""; $speclist[] = $temp; } } $value["speclist"] = empty($speclist) ? [] : $speclist; } $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c","k.supplierNo=c.code")->where(["k.wsm_code"=>$value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find(); $value['wsm_name'] = $wsmcode['wsm_name']; $value['wsm_supplier'] = $wsmcode['name']; $value['wsm_supplierNo'] = $wsmcode['code']; $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name')); //是否具有编辑权限 $value['is_allow_update'] = 0; if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1; $data[]=$value; } return app_show(0,"获取成功",['list'=>$data,'count'=>$count]); } public function add(){ $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!=""? trim($this->post['cgdNo']):""; if($cgdNo==""){ return error_show(1005,"参数cgdNo 不能为空"); } $cgdinfo = Db::name("purchease_order")->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find(); if(empty($cgdinfo)){ return error_show(1005,"未找到采购单数据"); } if($cgdinfo['status']==0){ return error_show(1005,"采购单未确认"); } $isreturn = cgd_sale_return($cgdNo); if($isreturn){ return error_show(1005, "关联销售单存在退货单正在进行"); } $wsm_in_code = makeNo("CF"); $bn_code = makeNo("BN"); $wsminfo = Db::name("warehouse_info")->where(["wsm_code"=>$cgdinfo['wsm_code'],"is_del"=>0])->find(); if(empty($wsminfo)){ return error_show(1004,"未找到仓库信息"); } $recep = Db::name('warehouse_addr')->where(["wsm_code"=>$cgdinfo['wsm_code'],"is_del"=>0])->find(); $sendtype = isset($this->post['sendtype'])&&$this->post['sendtype']!="" ? intval($this->post['sendtype']):""; if($sendtype==""){ return error_show(1004,"参数 sendtype 不能为空"); } // $data = GetUserInfo($this->post['token']); // if((!empty($data) && $data['code']!=0) ||empty($data) ){ // return error_show($data['code'],$data['message']); // } // $userinfo = $data['data']; $post_company = isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):""; $post_code = isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):""; $post_fee = isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? $this->post['post_fee']:"0"; $post_time = isset($this->post['post_time'])&&$this->post['post_time']!="" ? $this->post['post_time']:date("Y-m-d H:i:s"); $send_num = isset($this->post['send_num'])&&$this->post['send_num']!="" ? intval($this->post['send_num']):""; $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"1 "; if($send_num==''){ return error_show(1004,"参数 send_num 不能为空或零"); } $wsmin_num=Db::name("purchease_in")->where([["cgdNo","=",$cgdNo],["is_del","=",0],["status","not in",[4,6]]]) ->sum("send_num"); if($send_num>$cgdinfo['wsend_num']-$wsmin_num){ return error_show(1009,"采购单剩余未发货数量不足"); } if($sendtype==2 || $cgdinfo['order_type']!=1){ $status=4; } $orin = [ "wsm_in_code"=>$wsm_in_code, "cgdNo"=>$cgdNo, "bnCode"=>$bn_code, "wsm_code"=>$cgdinfo['wsm_code'], "wsm_reaper"=>isset($recep)&& $recep['wsm_name']!=''? $recep['wsm_name']:"", "send_num"=>$send_num, "post_company"=>$post_company, "post_code"=>$post_code, "post_fee"=>$post_fee, "sendtime"=>$post_time, "apply_id"=>$this->uid, "apply_name"=>$this->uname, "wait_num"=>0, "wsm_num"=>0, "sendtype"=>$sendtype, "status"=>$status, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; Db::startTrans(); try{ $win = Db::name("purchease_in")->insert($orin,true); if($win){ $sto = ["order_code"=>$wsm_in_code,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"]; ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$sto,"RKD",$status,$this->post); if($status==1){ $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); $handle_user_list = implode(',', $uid); } $process=["order_code"=>$wsm_in_code,"order_id"=>$win,"order_status"=>$status,"order_type"=>'RKD',"before_status"=>$status,'holder_id' => $orin['apply_id'],"handle_user_list"=>$handle_user_list??""]; ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process); $order = ["order_code"=>$cgdNo,"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"]; //维护台账信息 Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `cgdNo`='{$cgdNo}'"); $is_stock = Db::name('good_basic') ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']]) ->value('is_stock'); // if($is_stock == 1){ // //库存品,只能由库管操作 // $own_roleid = Db::name('user_role')->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])->value('roleid'); // if (!in_array($own_roleid, config('app.wsm_cgder_role'))) throw new Exception('只能由库管人员操作'); // }else{ //非库存品,只能由供应商负责人操作(开通账号的不能操作) //判断该供应商是否开通账号 //好像status==4里面的判断没用了,但是没时间修改 if($is_stock==0){ //全都只能由供应商负责人操作(开通账号的不能操作) $res = SupplierRelationUserModel::field('id') ->where(['is_del' => SupplierUserModel::$is_del_normal, 'supplierNo' => $cgdinfo['supplierNo'], 'status' => SupplierUserModel::$status_normal]) ->findOrEmpty() ->isEmpty(); if (!$res) throw new Exception('供应商已开通账号,只能由供应商端操作'); } $personid = Db::name('supplier') ->where('code', $cgdinfo['supplierNo']) ->value('personid'); if ($this->uid != $personid) throw new Exception('只能由供应商负责人操作'); // } if($status==1){ Db::commit(); return app_show(0,'新建成功',["wsm_in_code"=>$wsm_in_code]); } if($status==4){ $cgdinfo['wsend_num']-=$send_num; $cgdinfo['send_num']+=$send_num; $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['updatetime']=date("Y-m-d H:i:s"); $up=Db::name("purchease_order")->save($cgdinfo); if($up==false){ Db::rollback(); return error_show(1004,"新建失败"); } $good = Db::name("good_stock")->where(["wsm_code"=>$cgdinfo['wsm_code'],"spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->lock(true)->find(); if(empty($good)) { $good=[ "spuCode"=>$cgdinfo['spuCode'], "wsm_code"=>$cgdinfo['wsm_code'], "usable_stock"=>0, "wait_out_stock"=>0, "wait_in_stock"=>$send_num, "total_stock"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; } $good['wait_in_stock']-=$send_num; $good['usable_stock']+=$send_num; $good['total_stock']=$good['usable_stock']+$good['wait_out_stock']; $good['updatetime'] = date("Y-m-d H:i:s"); $upd= Db::name("good_stock")->save($good); $stockid=$good['id']?? Db::name("good_stock")->getLastInsID(); //商品变动日志表,good_log_code字段存储采购单单号 $good_data=[]; $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"usable_stock"]; $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>2,'stock'=>$send_num,"stock_name"=>"wait_in_stock"]; if($upd){ GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname],$good_data,'RKD'); //todo:: 新增bn编号库存数据修改 if($cgdinfo['order_type']==1){ $bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo['good_price']); if($bnin==false){ Db::rollback(); return error_show(1004,"库存bn数据新增失败"); } $bk =Db::name("order_bk")->where(['cgdNo'=>$cgdinfo['cgdNo']])->find(); if($bk==false){ $bk=[ "cgdNo"=>$cgdinfo['cgdNo'], "spuCode"=>$cgdinfo['spuCode'], "companyNo"=>$cgdinfo['companyNo'], "total_num"=>$send_num, "merge_num"=>0, "balance_num"=>$send_num, "status"=>1, "is_del"=>0, 'addtime'=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $upcgd =Db::name("order_bk")->save($bk); }else{ $where=['id'=>$bk['id'],'total_num'=>$bk['total_num'],'balance_num'=>$bk['balance_num']]; $bk['balance_num']+=$send_num; $bk['total_num']+=$send_num; $bk['updatetime']=date("Y-m-d H:i:s"); $upcgd =Db::name("order_bk")->where($where)->update($bk); } if($upcgd==false){ Db::rollback(); return error_show(1004,"采购单数量更新失败"); } } if($cgdinfo['send_status']==3 && $cgdinfo['order_type']!=1){ $send=Db::name("order_send")->where(["cgdNo"=>$cgdNo,"status"=>1])->column("outCode"); if(!empty($send)){ $out =Db::name("order_out")->where(["outCode"=>$send,"status"=>0,"is_del"=>0])->select() ->toArray(); if(!empty($out)){ $up =Db::name("order_out")->where(["outCode"=>$send,"status"=>0,"is_del"=>0])->update(["status"=>1,'wsm_code'=>$cgdinfo['wsm_code'],"updatetime"=>date('Y-m-d H:i:s')]); if($up==false){ Db::rollback(); return error_show(1004,"出库单出库失败"); }else{ //查询是否有非库存品订单 // $is_stock = Db::name('sale') // ->where([ // ['is_del','=',0], // ['orderCode','in',array_column($out,'orderCode')] // ])->column('is_stock','orderCode'); $supplier = Db::name('supplier') ->field('person,personid') ->where(['code'=>$cgdinfo['supplierNo'],'is_del'=>0]) ->findOrEmpty(); foreach ($out as $k => $v_outCode) { ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [ "order_code" => $v_outCode['outCode'],//出库单号 "status" => 0,//这里的status是之前的值 "action_remark" => '',//备注 "action_type" => "status"//新建create,编辑edit,更改状态status ], "CKD", 1, $this->post); if ($is_stock == 1) { //库存品,推给库管和库管-张凯旋 $roleid = config('app.wsm_cgder_role'); $uids = Db::name('user_role') ->where('is_del', 0) ->whereIn('roleid', $roleid) ->column('uid'); ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [ "order_type" => 'CKD', "order_code" => $v_outCode['outCode'],//出库单号 "order_id" => $v_outCode['id'], "order_status" => 1, "before_status" => 0, // 'wait_id' => $supplier['personid'], // 'wait_name' => $supplier['person'], // 'holder_id' => $supplier['personid'], 'handle_user_list' => implode(',', $uids) ]); } else { //非库存品和采返商品,推给供应商负责人 ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [ "order_type" => 'CKD', "order_code" => $v_outCode['outCode'],//出库单号 "order_id" => $v_outCode['id'], "order_status" => 1, "before_status" => 0, 'wait_id' => $supplier['personid'], 'wait_name' => $supplier['person'], 'holder_id' => $supplier['personid'], ]); } } } $good1 = Db::name("good_stock")->where(["wsm_code"=>$cgdinfo['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find(); if($good1==false){ Db::rollback(); return error_show(1004,"未找到对应商品库存"); } $out_num = intval(array_sum(array_column($out,"send_num"))); if($out_num>$good1['usable_stock']){ Db::rollback(); return error_show(1004,"库存数量不足发货"); } $good1['usable_stock']-=$out_num; $good1['wait_out_stock']+=$out_num; $good1['total_stock']=$good1['usable_stock']+$good1['wait_out_stock']; $good1['updatetime'] = date("Y-m-d H:i:s"); $upd= Db::name("good_stock")->save($good1); if($upd==false){ Db::rollback(); return error_show(1004,"库存数量更新失败"); } $stockid= isset($good['id'])?$good['id']:Db::name("good_stock")->getLastInsID(); $good_data=[]; $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"wait_out_stock"]; $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"usable_stock"]; GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname],$good_data,'RKD'); $sendp=Db::name("order_send")->where(["cgdNo"=>$cgdNo,"outCode"=>$send,"status"=>1])->save (["status"=>2]); if($sendp==false){ Db::rollback(); return error_show(1004,"库存发货失败"); } } } } ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CGD", $cgdinfo['status'],$cgdinfo); $personid = Db::name('supplier') ->where('is_del',0) ->where('code',$cgdinfo['supplierNo']) ->value('personid'); if($cgdinfo['status']==1 || $cgdinfo['status']==2){ // $is_stock = Db::name('good_basic') // ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']]) // ->value('is_stock'); // if($is_stock==1){ // //库存品,推给31库管人员、41库管-张凯旋 // $uid = Db::name('user_role') // ->where([ // ['is_del', '=', 0], // ['roleid', 'in', [31, 41]], // ['status', '=', 1] // ])->column('uid'); // if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作'); //// $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $personid,'handle_user_list'=>implode(',', $uid)]; // // }else{ if($this->uid != $personid)throw new Exception('只能由供应商负责人操作'); // } }//else $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $personid]; ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $personid]); Db::commit(); return app_show(0,'商品入库成功',["wsm_in_code"=>$wsm_in_code]); }else{ Db::rollback(); return error_show(1002,"商品入库失败"); } } Db::commit(); return app_show(0,'新建成功',["wsm_in_code"=>$wsm_in_code]); } Db::rollback(); return error_show(1004,"新建失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1004, $e->getMessage()); } } public function info(){ $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :""; if($incode==""){ return error_show(1004,"参数wsm_in_code 不为空"); } $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find(); if(empty($info)){ return error_show(1004,"未找到数据"); } $dom = Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],'is_del'=>0])->find(); if($dom['order_type']==3){ $goon = Db::name("good_zixun")->where(["spuCode"=>$dom['spuCode'],"is_del"=>0])->find(); }else { $goon =Db::name('good_basic')->where(['spuCode'=>$dom['spuCode']])->find(); } if(empty($goon)){ return error_show(1002,"未找到商品数据"); } $cat= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[]; $info['cgder']=Db::name("supplier")->where(["code"=>$dom['supplierNo']])->value("person",''); $info['spuCode']=$dom['spuCode']; $info['good_name']=$dom['good_name']; $info['skuCode']=$dom['skuCode']; $info['good_num']=$dom['good_num']; $info['good_price']=$dom['good_price']; $info['total_fee']=$dom['total_fee']; $info['diff_fee']=$dom['diff_fee']; $info['diff_weight']=$dom['diff_weight']; $info['gold_price']=$dom['gold_price']; $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code") ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0") ->where(["a.wsm_code"=>$info['wsm_code']])->field("a.name as wsm_name,b.name,b.code,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_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']:""; $info['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:""; $info['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:""; $info['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:""; $info['wsm_addr'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):""; $info['wsm_addr'].=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:""; $check = Db::name("purchease_info")->alias("a")->leftJoin("result_info b","a.error_code=b.result_code")->where (['wsm_in_code'=>$incode,"a.is_del"=>0])->field("a.id,a.wsm_in_code,a.num,a.error_code,b.result as error_msg,a.error_img,remark,a.is_del")->select(); $info['checkinfo']= $check; $return = Db::name("purchease_return")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find(); $return['return_contactor']=isset($return['return_contactor'])&&$return['return_contactor']!="" ?$return['return_contactor']:''; $return['return_addr']=isset($return['return_addr'])&&$return['return_addr']!="" ?$return['return_addr']:''; $return['return_mobile']=isset($return['return_mobile'])&&$return['return_mobile']!="" ?$return['return_mobile']:''; //以下字段临时补充到return中 $return['wsm_in_code'] = $incode; $return['wsm_num'] = isset($return['wsm_num']) ? $return['wsm_num'] : 0; $return['reissue_num'] = isset($return['reissue_num']) ? $return['reissue_num'] : 0; $return['return_num'] = isset($return['return_num']) ? $return['return_num'] : 0; $return['contactor'] = $return['return_contactor']; $return['addr'] = $return['return_addr']; $return['mobile'] = $return['return_mobile']; $return['error_remark'] = isset($return['error_remark']) ? $return['error_remark'] : ''; $return['remark'] = isset($return['remark']) ? $return['remark'] : ''; $info['return']= $return; $info['can']=$cat; if(isset($dom['companyNo'])&&$dom['companyNo']!=""){ $company = Db::name("business")->where(["companyNo"=>$dom['companyNo']])->find(); } $info['company'] = isset($company['company'])?$company['company']:""; $info['companyNo'] = isset($dom['companyNo'])?$dom['companyNo']:""; $info['order_type'] = isset($dom['order_type'])?$dom['order_type']:""; return app_show(0,'获取成功',$info); } public function check(){ $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :""; if($incode==""){ return error_show(1004,"参数wsm_in_code 不为空"); } $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find(); if(empty($info)){ return error_show(1004,"未找到数据"); } if($info['status']!=1){ return error_show(1004,"发货单状态有误"); } $order_num = isset($this->post['order_num']) && $this->post['order_num']!==''?intval($this->post['order_num']) :""; if($order_num===""){ return error_show(1004,"参数 order_num 不为空"); } $error_list = isset($this->post['error_list']) && $this->post['error_list']!==''?$this->post['error_list']:""; $result = GetUserInfo($this->post['token']); if((!empty($result) && $result['code']!=0) ||empty($result) ){ return error_show($result['code'],$result['message']); } $userinfo = $result['data']; Db::startTrans(); try{ $order = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"edit"]; $info['wsm_num'] = $order_num; $info['wait_num'] = $info['send_num']; $info['checkid'] = $userinfo['id']; $info['check_name'] = $userinfo['nickname']; $info['status'] =2; $info['error_num'] = $info['send_num']-$order_num; $info['updatetime'] = date("Y-m-d H:i:s"); $up =Db::name("purchease_in")->save($info); if($up){ ActionLog::logAdd($this->post['token'],$order,"RKD",2,$this->post); $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); $handle_user_list = implode(',', $uid); $process=["order_code"=>$info['wsm_in_code'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>'RKD',"before_status"=>$order['status'],'holder_id' => $info['apply_id'],"handle_user_list"=>$handle_user_list]; ProcessOrder::AddProcess($this->post['token'],$process); if(!empty($error_list)){ $data=[]; foreach ($error_list as $value ){ $temp=[]; $temp['wsm_in_code']=$info['wsm_in_code']; $temp['num']=$value['num']; $temp['error_code']=$value['error_code']; $temp['error_img']=$value['error_img']; $temp['remark']=$value['remark']; $temp['is_del']=0; $temp['addtime']=date("Y-m-d H:i:s"); $temp['updatetime']=date("Y-m-d H:i:s"); $data[]=$temp; } $derr= Db::name("purchease_info")->insertAll($data); if($derr){ Db::commit(); return app_show(0,"验货完成"); }else{ Db::rollback(); return error_show(1002,"验货数据保存失败"); } } Db::commit(); return app_show(0,"验货完成"); } Db::rollback(); return error_show(1002,"验货数据保存失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1002,$e->getMessage()); } } public function status(){ $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :""; if($incode==""){ return error_show(1004,"参数wsm_in_code 不为空"); } $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"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 不为空"); } if($info['status']==4){ return error_show(1004,"入库单已完成"); } $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']) :""; Db::startTrans(); try{ $order = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"status"]; $info['status'] = $status; $info['remark'] = $remark; $info['updatetime'] = date("Y-m-d H:i:s"); $update = Db::name("purchease_in")->save($info); if($update){ ActionLog::logAdd($this->post['token'],$order,"RKD",$status,$this->post); $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); $handle_user_list = implode(',', $uid); $process=["order_code"=>$info['wsm_in_code'],"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>'RKD',"before_status"=>$order['status'],'holder_id' => $info['apply_id'],"handle_user_list"=>$handle_user_list]; ProcessOrder::AddProcess($this->post['token'],$process); if($status==0){ $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find(); if(empty($cgdinfo)){ Db::rollback(); return error_show(1002,"未找到采购单数据"); } $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"]; $cgdinfo['wsend_num']+=$info['send_num']; $cgdinfo['send_num']-=$info['send_num']; $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['updatetime']=date("Y-m-d H:i:s"); $up=Db::name("purchease_order")->save($cgdinfo); if($up){ ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo); if($cgdinfo['status']==1 || $cgdinfo['status']==2){ // $orderinfo = Db::name('sale') // ->alias('a') // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid') // ->leftJoin('order_num b','b.orderCode=a.orderCode') // ->where([ // 'a.is_del'=>0, // 'b.cgdNo'=>$cgdinfo['cgdNo'], // ])->findOrEmpty(); $is_stock = Db::name('good_basic') ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']]) ->value('is_stock'); $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0); if($is_stock==1){ //库存品,推给31库管人员、41库管-张凯旋 $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作'); $handle_user_list=implode(",",$uid); // $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0),'handle_user_list'=>implode(',', $uid)]; }else{ //$holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0); if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作'); //$process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id,'handle_user_list'=>$holder_id]; }//非库存品和采返商品,推给供应商负责人 }//else $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0)]; $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id]; ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process); Db::commit(); return app_show(0,'取消成功'); }else{ Db::rollback(); return error_show(1002,"取消失败"); } } if($status==1){ $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find(); if(empty($cgdinfo)){ Db::rollback(); return error_show(1002,"未找到采购单数据"); } $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"]; $cgdinfo['wsend_num']-=$info['send_num']; $cgdinfo['send_num']+=$info['send_num']; $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['updatetime']=date("Y-m-d H:i:s"); $up=Db::name("purchease_order")->save($cgdinfo); if($up){ ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo); $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0); if($cgdinfo['status'] == 1 || $cgdinfo['status']==2){ // $orderinfo = Db::name('sale') // ->alias('a') // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid') // ->leftJoin('order_num b','b.orderCode=a.orderCode') // ->where([ // 'a.is_del'=>0, // 'b.cgdNo'=>$cgdinfo['cgdNo'], // ])->findOrEmpty(); $is_stock = Db::name('good_basic') ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']]) ->value('is_stock'); if($is_stock==1){ //库存品,推给31库管人员、41库管-张凯旋 $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作'); $handle_user_list = implode(",",$uid); // $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0),'handle_user_list'=>implode(',', $uid)]; }elseif ($is_stock==0 || $cgdinfo['order_type']==3) { if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作'); // $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id,'handle_user_list'=>$holder_id]; }//非库存品和采返商品,推给供应商负责人 }//else $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0)]; $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];ProcessOrder::AddProcess($this->post['token'],$process); Db::commit(); return app_show(0,'发货成功'); }else{ Db::rollback(); return error_show(1002,"发货失败"); } } if($status==4){ $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find(); if(empty($cgdinfo)){ Db::rollback(); return error_show(1002,"未找到采购单数据"); } $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"]; $cgdinfo['wsend_num']-=$info['send_num']; $cgdinfo['send_num']+=$info['send_num']; $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['updatetime']=date("Y-m-d H:i:s"); $up=Db::name("purchease_order")->save($cgdinfo); if($up==false){ Db::rollback(); return error_show(1002,"发货失败"); } ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo); $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0); if($cgdinfo['status']==1 || $cgdinfo['status']==2){ // $orderinfo = Db::name('sale') // ->alias('a') // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid') // ->leftJoin('order_num b','b.orderCode=a.orderCode') // ->where([ // 'a.is_del'=>0, // 'b.cgdNo'=>$cgdinfo['cgdNo'], // ])->findOrEmpty(); $is_stock = Db::name('good_basic') ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']]) ->value('is_stock'); if($is_stock==1){ //库存品,推给31库管人员、41库管-张凯旋 $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作'); // $holder_id = implode(",",$uid); // $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0),'handle_user_list'=>implode(',', $uid)]; }else{ if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作'); // $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id,'handle_user_list'=>$holder_id]; }//非库存品和采返商品,推给供应商负责人 }//else $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0)]; $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id]; ProcessOrder::AddProcess($this->post['token'],$process); $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->findOrEmpty(); if(empty($good)) { $good=[ "spuCode"=>$cgdinfo['spuCode'], "wsm_code"=>$info['wsm_code'], "usable_stock"=>0, "wait_out_stock"=>0, "wait_in_stock"=>$info['wsm_num'], "total_stock"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"create"]; }else{ $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"]; } $good['wait_in_stock']-=$info['wsm_num']; $good['usable_stock']+=$info['wsm_num']; $good['total_stock']=$good['usable_stock']+$good['wait_out_stock']; $good['updatetime'] = date("Y-m-d H:i:s"); $upd= Db::name("good_stock")->save($good); $stockid=$good['id']?? Db::name("good_stock")->getLastInsID(); //商品变动日志表,good_log_code字段存储入库单编号 $good_data=[]; $good_data[] = ['good_log_code' => $incode, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $info['wsm_num'], "stock_name" => "usable_stock"]; $good_data[] = ['good_log_code' => $incode, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $info['wsm_num'], "stock_name" => "wait_in_stock"]; if($upd){ $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$info['wsm_num'],$cgdinfo['good_price']); if($bnin==false){ Db::rollback(); return error_show(1004,"库存bn数据新增失败"); } GoodLog::LogAdd($this->post['token'],$good_data,'RKD'); Db::commit(); return app_show(0,'商品入库成功'); }else{ Db::rollback(); return error_show(1002,"商品入库失败"); } } Db::commit(); return app_show(0,"状态修改完成"); } Db::rollback(); return error_show(1002,"状态修改失败"); }catch (\Exception $e){ Db::rollback(); return error_show(1002,$e->getMessage()); } } /** * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function ReturnAdd(){ $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :""; if($incode==""){ return error_show(1004,"参数wsm_in_code 不为空"); } $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find(); if(empty($info)){ return error_show(1004,"未找到数据"); } //正常入库数量 $wsm_num = isset($this->post['wsm_num']) &&$this->post['wsm_num']!=="" ? intval($this->post['wsm_num']):"" ; if($wsm_num===""){ return error_show(1004,"参数wsm_num 不为空"); } //补货数量 $reissue_num = isset($this->post['reissue_num']) &&$this->post['reissue_num']!=="" ? intval($this->post['reissue_num']):"0" ; //退货数量 $return_num = isset($this->post['return_num']) &&$this->post['return_num']!=="" ? intval($this->post['return_num']):"0" ; $contactor = isset($this->post['contactor']) &&$this->post['contactor']!=="" ? trim($this->post['contactor']):"" ; if($contactor==""){ return error_show(1004,"参数contactor 不为空"); } $addr = isset($this->post['addr']) &&$this->post['addr']!=="" ? trim($this->post['addr']):"" ; if($addr==""){ return error_show(1004,"参数addr 不为空"); } $mobile = isset($this->post['mobile']) &&$this->post['mobile']!=="" ? trim($this->post['mobile']):"" ; if($mobile==""){ return error_show(1004,"参数mobile 不为空"); } $error_remark = isset($this->post['error_remark']) &&$this->post['error_remark']!=="" ? trim($this->post['error_remark']):"" ; $remark = isset($this->post['remark']) &&$this->post['remark']!=="" ? trim($this->post['remark']):"" ; $result = GetUserInfo($this->post['token']); if((!empty($result) && $result['code']!=0) ||empty($result) ){ return error_show($result['code'],$result['message']); } $userinfo = $result['data']; Db::startTrans(); try{ $sto = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"edit"]; // $info['status'] =$info['wsm_num']==$wsm_num ? 4 :5; $info['status'] = $info['send_num'] == $wsm_num ? 4 : 5;//艳蓉说,这里不能用入库数量比较,应该是用入库数量,即总数来比较 $info['is_th'] =$return_num>0 ?1:0; $info['wsm_num']= $wsm_num; $info['updatetime'] = date("Y-m-d H:i:s"); $up= Db::name("purchease_in")->save($info); if($up){ $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find(); if(empty($cgdinfo)){ Db::rollback(); return error_show(1002,"未找到采购单数据"); } //库存品,推给31库管人员、41库管-张凯旋 $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); $handle_user_list=implode(",",$uid); if($info['status']==4){ $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"]; $cgdinfo['wsend_num']-=$info['send_num']; $cgdinfo['send_num']+=$info['send_num']; $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['updatetime']=date("Y-m-d H:i:s"); $up=Db::name("purchease_order")->save($cgdinfo); if($up==false){ Db::rollback(); return error_show(1002,"发货失败"); } ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo); $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0); if($cgdinfo['status']==1 || $cgdinfo['status']==2){ $is_stock = Db::name('good_basic') ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']]) ->value('is_stock'); if($is_stock==1){ //库存品,推给31库管人员、41库管-张凯旋 if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作'); // $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0),'handle_user_list'=>implode(',', $uid)]; }else{ // $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0); if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作'); // $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id,'handle_user_list'=>$holder_id]; }//非库存品和采返商品,推给供应商负责人 }//else $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0)]; $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id]; ProcessOrder::AddProcess($this->post['token'],$process); $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find(); if(empty($good)) { $good=[ "spuCode"=>$cgdinfo['spuCode'], "wsm_code"=>$info['wsm_code'], "usable_stock"=>0, "wait_out_stock"=>0, "wait_in_stock"=>$wsm_num, "total_stock"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"create"]; }else{ $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"]; } $good['wait_in_stock']-=$wsm_num; // if($good['presale_stock']>0){ // if($good['presale_stock']>=$wsm_num){ // $good['presale_stock']-=$wsm_num; // $good['wait_out_stock']+=$wsm_num; // }else{ // $good['presale_stock']=0; // $good['usable_stock']+=$wsm_num-$good['presale_stock']; // $good['wait_out_stock']+=$good['presale_stock']; // } // }else{ $good['usable_stock']+=$wsm_num; // } // $good['usable_stock']+=$wsm_num; $good['total_stock']=$good['usable_stock']+$good['wait_out_stock']; $good['updatetime'] = date("Y-m-d H:i:s"); $upd= Db::name("good_stock")->save($good); $stockid=$good['id']?? Db::name("good_stock")->getLastInsID(); if($upd){ $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$info['wsm_num'],$cgdinfo['good_price']); if($bnin==false){ Db::rollback(); return error_show(1004,"库存bn数据新增失败"); } }else{ Db::rollback(); return error_show(1002,"商品入库失败"); } $good_data=[]; $good_data[]=['good_log_code' => $incode,"stock_id"=>isset($good['id'])? $good['id']:Db::name("good_stock")->getLastInsID(),"type"=>1, 'stock'=>$info['wsm_num'],"stock_name"=>"usable_stock"]; $good_data[]=['good_log_code' => $incode,"stock_id"=>isset($good['id'])? $good['id']:Db::name("good_stock")->getLastInsID(),"type"=>2, 'stock'=>$info['wsm_num'],"stock_name"=>"wait_in_stock"]; GoodLog::LogAdd($this->post['token'],$good_data,'RKD'); $bk =Db::name("order_bk")->where(['cgdNo'=>$cgdinfo['cgdNo']])->lock(true)->find(); if($bk==false){ $bk=[ "cgdNo"=>$cgdinfo['cgdNo'], "spuCode"=>$cgdinfo['spuCode'], "companyNo"=>$cgdinfo['companyNo'], "total_num"=>0, "merge_num"=>0, "balance_num"=>0, "status"=>1, "is_del"=>0, 'addtime'=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; } $bk['balance_num']+=$wsm_num; $bk['total_num']+=$wsm_num; $bk['updatetime']=date("Y-m-d H:i:s"); $upcgd =Db::name("order_bk")->save($bk); if($upcgd==false){ Db::rollback(); return error_show(1004,"采购单数量更新失败"); } } ActionLog::logAdd($this->post['token'],$sto,"RKD", $info['status'],$this->post); $process=["order_code"=>$info['wsm_in_code'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>'RKD',"before_status"=>$sto['status'],'holder_id' => $info['apply_id'],"handle_user_list"=>$handle_user_list??""]; ProcessOrder::AddProcess($this->post['token'],$process); //需要在详情页把相关退货记录原样取出来,所以这里把if去掉,所有记录都入purchease_return表 if($info['status']==5){ $returnCode=makeNo("CR"); $data=[ "returnCode"=>$returnCode, "wsm_in_code"=>$incode, "cgdNo"=>$cgdinfo['cgdNo'], "wsm_num"=>$wsm_num, "reissue_num"=>$reissue_num, "return_num"=>$return_num, "return_contactor"=>$contactor, "return_addr"=>$addr, "return_mobile"=>$mobile, "error_remark"=>$error_remark, "post_code"=>"", "post_company"=>"", "remark"=>$remark , "apply_id"=>$userinfo['id'], "apply_name"=>$userinfo['nickname'], "status"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $res = Db::name("purchease_return")->insert($data,true); if($res==false){ Db::rollback(); return error_show(1002,"验货审核失败"); } $sto = ["order_code"=>$incode,"status"=>0,"action_remark"=>'',"action_type"=>"create"]; ActionLog::logAdd($this->post['token'],$sto,"RKTHD",0,$data); //维护台账记录 // Db::name('standing_book') // ->where('wsm_in_code', $incode) // ->update([ // 'cgdReturnCode' => $returnCode, // 'updatetime' => date('Y-m-d H:i:s'), // ]); Db::execute("UPDATE `wsm_standing_book` SET `cgdReturnCode`=CONCAT(IFNULL(`cgdReturnCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$incode}',`wsm_in_code`)");//这里的wsm_in_code可能有多个 } Db::commit(); return app_show(0,"验货审核完成"); } }catch (\Exception $e){ Db::rollback(); return error_show(1002,$e->getMessage()); } } public function ReturnSend(){ $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :""; if($incode==""){ return error_show(1004,"参数wsm_in_code 不为空"); } $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find(); if(empty($info)){ return error_show(1004,"未找到发货单数据"); } $return = Db::name("purchease_return")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find(); if(empty($return)){ return error_show(1004,"未找到退货单数据"); } $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find(); if(empty($cgdinfo)){ Db::rollback(); return error_show(1002,"未找到采购单数据"); } $post_code = isset($this->post['post_code']) &&$this->post['post_code']!=="" ? trim($this->post['post_code']):"" ; if($post_code==""){ return error_show(1004,"参数 post_code 不能为空"); } $post_company = isset($this->post['post_company']) &&$this->post['post_company']!=="" ? trim($this->post['post_company']):"" ; if($post_company==""){ return error_show(1004,"参数 post_company 不能为空"); } $remark = isset($this->post['remark']) &&$this->post['remark']!=="" ? trim($this->post['remark']):"" ; $data=[ "post_code"=>$post_code, "post_company"=>$post_company, "remark"=>$remark, "status"=>1, "updatetime"=>date("Y-m-d H:i:s") ]; Db::startTrans(); try{ $up =Db::name("purchease_return")->where($return)->save($data); if($up){ $sto = ["order_code"=>$info['wsm_in_code'],"status"=>$return['status'],"action_remark"=>'', "action_type"=>"edit"]; ActionLog::logAdd($this->post['token'],$sto,"RKTHD",1,$this->post); $ark=$info['status']; $info['status']=6; $info['updatetime']=date("Y-m-d H:i:s"); $sa=Db::name("purchease_in")->save($info); if($sa){ $stn = ["order_code"=>$info['wsm_in_code'],"status"=>$ark,"action_remark"=>'',"action_type"=>"edit"]; ActionLog::logAdd($this->post['token'],$stn,"RKD",6,$info); $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); $handle_user_list = implode(',', $uid); $process=["order_code"=>$info['wsm_in_code'],"order_id"=>$info['id'],"order_status"=>6,"order_type"=>'RKD',"before_status"=>$stn['status'],'holder_id' => $info['apply_id'],"handle_user_list"=>$handle_user_list]; ProcessOrder::AddProcess($this->post['token'],$process); $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find(); if(empty($good)) { $good=[ "spuCode"=>$cgdinfo['spuCode'], "wsm_code"=>$info['wsm_code'], "usable_stock"=>0, "wait_out_stock"=>0, "wait_in_stock"=>$return['wsm_num'], "total_stock"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"]; }else{ $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"]; } $good['wait_in_stock']-=$return['wsm_num']+$return['return_num']-$return['reissue_num']; $good['usable_stock']+=$return['wsm_num']; $good['total_stock']=$good['usable_stock']+$good['wait_out_stock']; $good['updatetime'] = date("Y-m-d H:i:s"); $upd= Db::name("good_stock")->save($good); if($upd==false){ Db::rollback(); return error_show(1002,"库存更新失败"); } $stockid=$good['id']?? Db::name("good_stock")->getLastInsID(); $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$return['wsm_num'],$cgdinfo['good_price']); if($bnin==false){ Db::rollback(); return error_show(1004,"库存bn数据新增失败"); } $good_data=[]; $good_data[]=['good_log_code' => $incode,"stock_id"=>$stockid,"type"=>1, 'stock'=>$info['wsm_num'],"stock_name"=>"usable_stock"]; $good_data[]=['good_log_code' => $incode,"stock_id"=>$stockid,"type"=>2, 'stock'=>$good['wait_in_stock'],"stock_name"=>"wait_in_stock"]; GoodLog::LogAdd($this->post['token'],$good_data,'RKD'); $order = ["order_code"=>$cgdinfo['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"]; $cgdinfo['wsend_num']-=$return['wsm_num']+$return['return_num']-$return['reissue_num']; $cgdinfo['send_num']+=$return['wsm_num']+$return['return_num']-$return['reissue_num']; $cgdinfo['th_num']+=$return['return_num']-$return['reissue_num']; $cgdinfo['th_fee']=round(($return['return_num']-$return['reissue_num'])*$cgdinfo['good_price'],2); $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2); if($cgdinfo['th_num']==$cgdinfo['send_num']&& $cgdinfo['wsend_num']==0){ $cgdinfo['status']=4; } $cgdinfo['updatetime']=date("Y-m-d H:i:s"); $up=Db::name("purchease_order")->save($cgdinfo); if($up==false){ Db::rollback(); return error_show(1004,"采购单数量更新失败"); } $bk =Db::name("order_bk")->where(['cgdNo'=>$cgdinfo['cgdNo']])->lock(true)->find(); if($bk==false){ $bk=[ "cgdNo"=>$cgdinfo['cgdNo'], "spuCode"=>$cgdinfo['spuCode'], "companyNo"=>$cgdinfo['companyNo'], "total_num"=>0, "merge_num"=>0, "balance_num"=>0, "status"=>1, "is_del"=>0, 'addtime'=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; } $bk['balance_num']+=$return['wsm_num']; $bk['total_num']+=$return['wsm_num']; $bk['updatetime']=date("Y-m-d H:i:s"); $upcgd =Db::name("order_bk")->save($bk); if($upcgd==false){ Db::rollback(); return error_show(1004,"采购单数量更新失败"); } ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo); $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0); if($cgdinfo['status']==1 || $cgdinfo['status']==2){ // $orderinfo = Db::name('sale') // ->alias('a') // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid') // ->leftJoin('order_num b','b.orderCode=a.orderCode') // ->where([ // 'a.is_del'=>0, // 'b.cgdNo'=>$cgdinfo['cgdNo'], // ])->findOrEmpty(); $is_stock = Db::name('good_basic') ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']]) ->value('is_stock'); if($is_stock==1){ //库存品,推给31库管人员、41库管-张凯旋 $uid = Db::name('user_role') ->where([ ['is_del', '=', 0], ['roleid', 'in', [31, 41]], ['status', '=', 1] ])->column('uid'); if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作'); // $process=["order_code"=>$cgdinfo['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0),'handle_user_list'=>implode(',', $uid)]; // $handle_user_list = implode(",",$uid); }else{ // $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0); if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作'); // $process=["order_code"=>$cgdinfo['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id,'handle_user_list'=>$holder_id];//非库存品和采返商品,推给供应商负责人 } }//else $process=["order_code"=>$cgdinfo['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0)]; $process=["order_code"=>$cgdinfo['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];//非库存品和采返商品,推给供应商负责人 ProcessOrder::AddProcess($this->post['token'],$process); Db::commit(); return app_show(0,"退货发货成功"); }else{ Db::rollback(); return error_show(1004,"退货发货失败"); } }else{ return error_show(1004,"退货发货失败"); } }catch (\Exception $e){ Db::rollback(); return error_show(1004,$e->getMessage()); } } public function returnList(){ $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 = [['a.is_del',"=",0],['b.is_del',"=",0]]; $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):""; if($cgdNo!=""){ $where[]=['a.cgdNo',"like", "%$cgdNo%"]; } $returnCode= isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):""; if($returnCode!=""){ $where[]=['a.returnCode',"like", "%$returnCode%"]; } $good_type_code=isset($this->post['good_type_code']) && $this->post['good_type_code']!==""? trim($this->post['good_type_code']):""; if($good_type_code !=""){ $where[]= ["b.spuCode",'like',"%$good_type_code%"]; } $good_name=isset($this->post['good_name']) && $this->post['good_name']!==""? trim($this->post['good_name']):""; if($good_name !=""){ $where[]= ["b.good_name",'like',"%$good_name%"]; } $status=isset($this->post['status']) && $this->post['status']!==""? trim($this->post['status']):""; if($status!=""){ $where[]= ["a.status",'=',$status]; } // $role = $this->checkDataShare(); // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ["a.apply_id", "in", $role[DataGroupModel::$type_全部]]; $role = $this->checkDataShare(); $hand = resign_hand_user($this->uid,0); if (!empty($role[DataGroupModel::$type_全部])) { $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部])); $where[] = ['a.apply_id', 'in',$arr]; } $count = Db::name('purchease_return') ->alias('a') ->join("purchease_order b","b.cgdNo=a.cgdNo","left") ->where($where) ->count(); $total = ceil($count / $size); $page = $page >= $total ? $total : $page; $list = Db::name('purchease_return') ->alias('a') ->join("purchease_order b","b.cgdNo=a.cgdNo","left") ->where($where) ->page($page,$size) ->field("a.*,b.spuCode,b.good_price,b.order_type,b.good_price,b.total_fee,b.good_name") ->order("a.addtime desc") ->select() ->toArray(); $data=[]; foreach ($list as $value){ if($value['order_type']==3){ $goon = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find(); }else { $goon =Db::name('good_basic')->where(['spuCode'=>$value['spuCode']])->find(); } $value['can']= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[]; $value['return_fee']= round($value['good_price']*$value['return_num'],2); //是否具有编辑权限 $value['is_allow_update'] = 0; if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1; $data[]=$value; } return app_show(0,"获取成功",['list'=>$data,'count'=>$count]); } }