Purchin.php 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. <?php
  2. namespace app\admin\controller;
  3. use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
  4. use app\abutment\model\SupplierUser as SupplierUserModel;
  5. use app\admin\model\ActionLog;
  6. use app\admin\model\DataGroup as DataGroupModel;
  7. use app\admin\model\GoodLog;
  8. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  9. use app\BaseController;
  10. use think\Exception;
  11. use think\facade\Db;
  12. use think\App;
  13. use think\facade\Validate;
  14. //采购单入库
  15. class Purchin extends Base
  16. {
  17. public function __construct(App $app)
  18. {
  19. parent::__construct($app);
  20. // $this->post=$this->request->post();
  21. }
  22. public function list(){
  23. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  24. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  25. $where =[['a.is_del',"=",0],['b.is_del',"=",0]];
  26. $wsm_in_code=isset($this->post['wsm_in_code']) && $this->post['wsm_in_code']!==""? trim($this->post['wsm_in_code']) :"";
  27. if($wsm_in_code!==""){
  28. $where[]=["a.wsm_in_code",'like',"%$wsm_in_code%"];
  29. }
  30. $cgdNo= isset($this->post['cgdNo']) && $this->post['cgdNo'] !==""? trim($this->post['cgdNo']) :"";
  31. if($cgdNo!==""){
  32. $where[]=['a.cgdNo','like',"%$cgdNo%"];
  33. }
  34. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? trim($this->post['apply_name']):"";
  35. if($apply_name!==""){
  36. $where[]=['a.apply_name','like',"%$apply_name%"];
  37. }
  38. $apply_id= isset($this->post['apply_id']) && $this->post['apply_id'] !==""? trim($this->post['apply_id']):"";
  39. if($apply_id!==""){
  40. $where[]=['a.apply_id','=',$apply_id];
  41. }
  42. $status= isset($this->post['status']) && $this->post['status'] !==""? trim($this->post['status']):"";
  43. if($status!==""){
  44. $where[]=['a.status','=',$status];
  45. }
  46. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  47. if ($start !=="") {
  48. $where[]= ["a.sendtime",'>=',$start];
  49. }
  50. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  51. if($end !==""){
  52. $where[]= ["a.sendtime",'<=',$end];
  53. }
  54. $order_type=isset($this->post['order_type']) && $this->post['order_type']!==""? intval($this->post['order_type']):"";
  55. if($order_type !==""){
  56. $where[]= ["b.order_type",'=',$order_type];
  57. }
  58. $order_source=isset($this->post['order_source']) && $this->post['order_source']!==""? intval($this->post['order_source']):"";
  59. if($order_source !=="") $where[]= ["b.order_source",'=',$order_source];
  60. $good_type_code=isset($this->post['good_type_code']) && $this->post['good_type_code']!==""? trim($this->post['good_type_code']):"";
  61. if($good_type_code !==""){
  62. $where[]= ["b.spuCode",'like',"%$good_type_code%"];
  63. }
  64. $good_name=isset($this->post['good_name']) && $this->post['good_name']!==""? trim($this->post['good_name']):"";
  65. if($good_name !==""){
  66. $where[]= ["b.good_name",'like',"%$good_name%"];
  67. }
  68. $supplie=isset($this->post['supplier']) && $this->post['supplier']!==""? trim($this->post['supplier']):"";
  69. if($supplie !==""){
  70. $where[]= ["b.supplier_name",'like',"%$supplie%"];
  71. }
  72. $wsm_code=isset($this->post['wsm_code']) && $this->post['wsm_code']!==""? trim($this->post['wsm_code']):"";
  73. if($wsm_code !==""){
  74. $where[]= ["a.wsm_code",'=',$wsm_code];
  75. }
  76. $supplierNo =isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']):"";
  77. if($supplierNo!==""){
  78. // $supplier = Db::name("supplier")->where(["code"=>$supplierNo])->find();
  79. // if(empty($supplier)){
  80. // return error_show(1004,"未找到供应商信息");
  81. // }
  82. //不再根据供应商筛选仓库,要筛选仓库只能是业务公司
  83. // $wsmcode = Db::name("warehouse_info")
  84. // ->where(["is_del"=>0,"supplierNo"=>$supplierNo])
  85. // ->column("wsm_code");
  86. // $where[]=['a.wsm_code',"in",$wsmcode];
  87. $where[]= ["b.supplierNo",'=',$supplierNo];
  88. }
  89. $condition=[];
  90. //只有level2的账号过滤数据权限
  91. // if ($this->level == 2) {
  92. // $role = $this->checkDataShare();
  93. // $hand = resign_hand_user($this->uid, 0);
  94. // if (!empty($role[DataGroupModel::$type_全部])) {
  95. // $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  96. //
  97. // if($arr){
  98. // $sys_supplier = Db::connect('mysql_sys')
  99. // ->name('supplier')
  100. // ->where(['is_del'=>0,'personid'=>$arr])
  101. // ->column('code');
  102. // $condition[] = ["b.supplierNo", "in", $sys_supplier];
  103. // }
  104. //
  105. // $condition[] = ["a.apply_id", "in", $hand];
  106. // $condition[] = ["c.contactor", "in", $hand];
  107. //// $condition.="sip.personid in (".implode(",",
  108. //// $role[DataGroupModel::$type_全部]).") or a.apply_id in {$hand} or c.contactor in {$hand}";
  109. // }
  110. // }
  111. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  112. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  113. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo']!="" ? trim($this->post['relaComNo']):"";
  114. if($relaComNo!=='') $where[]=['b.companyNo','=', $relaComNo];
  115. //$count = Db::name('purchease_in')->alias('a')->join("purchease_order b","b.cgdNo=a.cgdNo","left")
  116. $count = Db::name('purchease_in')
  117. ->alias('a')
  118. ->join("purchease_order b","b.cgdNo=a.cgdNo","left")
  119. ->leftJoin("warehouse_info c","a.wsm_code=c.wsm_code")
  120. // ->leftJoin('supplier sip', 'sip.code=b.supplierNo')
  121. ->where($where)
  122. // ->whereOr($condition)
  123. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  124. ->count();
  125. $total = ceil($count/$size);
  126. $page = $page >= $total ? $total : $page;
  127. $list = Db::name('purchease_in')
  128. ->alias('a')
  129. ->join("purchease_order b","b.cgdNo=a.cgdNo","left")
  130. // ->leftJoin('supplier sip', 'sip.code=b.supplierNo')
  131. ->leftJoin("warehouse_info c","a.wsm_code=c.wsm_code")
  132. ->where($where)
  133. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  134. ->page($page,$size)
  135. ->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,'' cgder_id,'' cgder,b.good_createrid,b.good_creater,c.name wsm_name,b.supplierNo,b.supplier_name,c.supplierNo wsm_supplierNo,c.supplierName wsm_supplier,b.companyNo,b.companyName")
  136. ->order("a.addtime desc")
  137. ->select()
  138. ->toArray();
  139. $all_createrid = array_column($list,'apply_id');
  140. $item = get_company_name_by_uid($all_createrid);
  141. //获取采购单供应商和仓库供应商的名称
  142. $userCommon = \app\admin\common\User::getIns();
  143. // $names = $userCommon->handle('getCodeAndName',['code'=>array_unique(array_column($list,'wsm_supplierNo'))]);
  144. $all_supplierNos = array_unique(array_column($list,'supplierNo'));
  145. $all_supplier = $userCommon->handle('sGetList',['more_code'=>$all_supplierNos,'page'=>1,'size'=>$size]);
  146. $all_supplier_list = array_column($all_supplier['data']['list'],null,'code');
  147. //校验是否开通了供应商账号
  148. $supp_account = checkHasAccountBySupplierNos($all_supplierNos);
  149. $data=[];
  150. foreach ($list as $value){
  151. $value['cgder_id'] = $all_supplier_list[$value['supplierNo']]['personid']??'';
  152. $value['cgder'] = $all_supplier_list[$value['supplierNo']]['person']??'';
  153. //获取规格、品牌信息
  154. if ($value['order_type'] == 3 || $value['order_type'] == 4 ) {
  155. $temp = Db::name('good_zixun')
  156. ->alias('gz')
  157. ->field('gz.specinfo,b.brand_name')
  158. ->join('brand b', 'b.id=gz.brand_id')
  159. ->where(['gz.spuCode' => $value['spuCode'], 'gz.is_del' => 0])
  160. ->find();
  161. $value["brand_name"] = $temp['brand_name'];
  162. $value["speclist"] = json_decode($temp['specinfo'], true);
  163. } else {
  164. $value['brand_name'] = Db::name('good_basic')
  165. ->alias('gb')
  166. ->join('brand b', 'b.id=gb.brand_id')
  167. ->where(['gb.spuCode' => $value['spuCode']])
  168. ->value('b.brand_name');
  169. $spec = Db::name("good_spec")
  170. ->where(["spuCode" => $value['spuCode'], "is_del" => 0])
  171. ->select()
  172. ->toArray();
  173. $speclist = [];
  174. if (!empty($spec)) {
  175. foreach ($spec as $val) {
  176. $temp = [];
  177. $temp['id'] = $val['id'];
  178. $temp['spuCode'] = $val['spuCode'];
  179. $temp['spec_id'] = $val['spec_id'];
  180. $temp['spec_value_id'] = $val['spec_value_id'];
  181. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  182. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  183. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  184. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  185. $speclist[] = $temp;
  186. }
  187. }
  188. $value["speclist"] = empty($speclist) ? [] : $speclist;
  189. }
  190. // $wsmcode = Db::name("warehouse_info")
  191. // ->alias("k")
  192. // ->leftJoin("supplier c","k.supplierNo=c.code")
  193. // ->where(["k.wsm_code"=>$value['wsm_code']])
  194. // ->field("k.name as wsm_name,c.name,c.code")
  195. // ->find();
  196. // $value['wsm_name'] = $wsmcode['wsm_name'];
  197. // $value['wsm_supplier'] = $names['data'][$value['wsm_supplierNo']]??'';
  198. // $value['companyName'] = $names['data'][$value['companyNo']]??'';
  199. // $value['wsm_supplierNo'] = $wsmcode['code'];
  200. $value['company_name'] = $item[$value['apply_id']]??'';
  201. //是否具有编辑权限
  202. // $value['is_allow_update'] = 0;
  203. // if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  204. $value['has_account'] = (int)isset($supp_account[$value['supplierNo']]);
  205. $data[]=$value;
  206. }
  207. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  208. }
  209. public function add(){
  210. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!=""? trim($this->post['cgdNo']):"";
  211. if($cgdNo==""){
  212. return error_show(1005,"参数cgdNo 不能为空");
  213. }
  214. $cgdinfo = Db::name("purchease_order")->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
  215. if(empty($cgdinfo)){
  216. return error_show(1005,"未找到采购单数据");
  217. }
  218. if($cgdinfo['status']==0){
  219. return error_show(1005,"采购单未确认");
  220. }
  221. $isreturn = cgd_sale_return($cgdNo);
  222. if($isreturn){
  223. return error_show(1005, "关联销售单存在退货单正在进行");
  224. }
  225. $wsm_in_code = makeNo("CF");
  226. $bn_code = makeNo("BN");
  227. $wsminfo = Db::name("warehouse_info")->where(["wsm_code"=>$cgdinfo['wsm_code'],"is_del"=>0])->find();
  228. if(empty($wsminfo)){
  229. return error_show(1004,"未找到仓库信息");
  230. }
  231. $recep = Db::name('warehouse_addr')->where(["wsm_code"=>$cgdinfo['wsm_code'],"is_del"=>0])->find();
  232. $sendtype = isset($this->post['sendtype'])&&$this->post['sendtype']!="" ? intval($this->post['sendtype']):"";
  233. if($sendtype==""){
  234. return error_show(1004,"参数 sendtype 不能为空");
  235. }
  236. $post_company = isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  237. $post_code = isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  238. $post_fee = isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? $this->post['post_fee']:"0";
  239. $post_time = isset($this->post['post_time'])&&$this->post['post_time']!="" ? $this->post['post_time']:date("Y-m-d H:i:s");
  240. $send_num = isset($this->post['send_num'])&&$this->post['send_num']!="" ? intval($this->post['send_num']):"";
  241. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"1 ";
  242. if($send_num==''){
  243. return error_show(1004,"参数 send_num 不能为空或零");
  244. }
  245. $wsmin_num=Db::name("purchease_in")->where([["cgdNo","=",$cgdNo],["is_del","=",0],["status","=",1]])
  246. ->sum("send_num");
  247. if($send_num>$cgdinfo['wsend_num']-$wsmin_num){
  248. return error_show(1009,"采购单剩余未发货数量不足");
  249. }
  250. if($cgdinfo['order_type']!=1){
  251. $status=2;
  252. }
  253. $orin = [
  254. "wsm_in_code"=>$wsm_in_code,
  255. "cgdNo"=>$cgdNo,
  256. "bnCode"=>$bn_code,
  257. "wsm_code"=>$cgdinfo['wsm_code'],
  258. "wsm_reaper"=>isset($recep)&& $recep['wsm_name']!=''? $recep['wsm_name']:"",
  259. "send_num"=>$send_num,
  260. "post_company"=>$post_company,
  261. "post_code"=>$post_code,
  262. "post_fee"=>$post_fee,
  263. "sendtime"=>$post_time,
  264. "apply_id"=>$this->uid,
  265. "apply_name"=>$this->uname,
  266. "wait_num"=>0,
  267. "wsm_num"=>0,
  268. "sendtype"=>$sendtype,
  269. "status"=>$status,
  270. "addtime"=>date("Y-m-d H:i:s"),
  271. "updatetime"=>date("Y-m-d H:i:s")
  272. ];
  273. Db::startTrans();
  274. try{
  275. //
  276. if ($this->level == 2) {
  277. //如果开通了供应商账号,则只允许level3账号操作,level2账号不允许操作
  278. $all_has_supplier_account = checkHasAccountBySupplierNos([$cgdinfo['supplierNo']]);
  279. if (isset($all_has_supplier_account[$cgdinfo['supplierNo']])) throw new Exception('已开通供应商账号,当前账号不能操作');
  280. }
  281. $win = Db::name("purchease_in")->insert($orin,true);
  282. if($win){
  283. $sto = ["order_code"=>$wsm_in_code,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"];
  284. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$sto,"RKD",$status,$this->post);
  285. if($status==1){
  286. // $uid = Db::name('user_role')
  287. // ->where([
  288. // ['is_del', '=', 0],
  289. // ['roleid', 'in', [31, 41]],
  290. // ['status', '=', 1]
  291. // ])->column('uid');
  292. // $handle_user_list = implode(',', $uid);
  293. }
  294. $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??""];
  295. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  296. $order = ["order_code"=>$cgdNo,"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  297. //维护台账信息
  298. 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}'");
  299. $is_stock = Db::name('good_basic')
  300. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  301. ->value('is_stock');
  302. $userCommon = \app\admin\common\User::getIns();
  303. $supplier_temp = $userCommon->handle('sInfo',['code'=>$cgdinfo['supplierNo']]);
  304. $person = $supplier_temp['data'];
  305. // if ($this->level == 2 && $this->uid != $person['personid']) throw new Exception('只能由供应商负责人操作');
  306. if($status==1){
  307. Db::commit();
  308. return app_show(0,'新建成功',["wsm_in_code"=>$wsm_in_code]);
  309. }
  310. if($status==2){
  311. $cgdinfo['wsend_num']-=$send_num;
  312. $cgdinfo['send_num']+=$send_num;
  313. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  314. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  315. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  316. $up=Db::name("purchease_order")->save($cgdinfo);
  317. if($up==false){
  318. Db::rollback();
  319. return error_show(1004,"新建失败");
  320. }
  321. $good = Db::name("good_stock")->where(["wsm_code"=>$cgdinfo['wsm_code'],"spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->lock(true)->find();
  322. if(empty($good)) {
  323. $good=[
  324. "spuCode"=>$cgdinfo['spuCode'],
  325. "wsm_code"=>$cgdinfo['wsm_code'],
  326. "usable_stock"=>0,
  327. "wait_out_stock"=>0,
  328. "wait_in_stock"=>$send_num,
  329. "total_stock"=>0,
  330. "addtime"=>date("Y-m-d H:i:s"),
  331. "updatetime"=>date("Y-m-d H:i:s"),
  332. ];
  333. }
  334. $good['wait_in_stock']-=$send_num;
  335. $good['usable_stock']+=$send_num;
  336. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  337. $good['updatetime'] = date("Y-m-d H:i:s");
  338. $upd= Db::name("good_stock")->save($good);
  339. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  340. //商品变动日志表,good_log_code字段存储采购单单号
  341. $good_data=[];
  342. $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"usable_stock"];
  343. $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>2,'stock'=>$send_num,"stock_name"=>"wait_in_stock"];
  344. if($upd){
  345. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname],$good_data,'RKD');
  346. $bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo['good_price']);
  347. if($bnin==false){
  348. Db::rollback();
  349. return error_show(1004,"库存bn数据新增失败");
  350. }
  351. //todo:: 新增bn编号库存数据修改
  352. if($cgdinfo['order_type']==1){
  353. //商品表里的'可用总库存数'增加
  354. Db::name('good')
  355. ->data(['updatetime' => date('Y-m-d H:i:s')])
  356. ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
  357. ->inc('usable_stock', $send_num)
  358. ->update();
  359. }
  360. if($cgdinfo['send_status']==3 && $cgdinfo['order_type']!=1){
  361. $send=Db::name("order_send")->where(["cgdNo"=>$cgdNo,"status"=>1])->column("outCode");
  362. if(!empty($send)){
  363. $out =Db::name("order_out")->where(["outCode"=>$send,"status"=>0,"is_del"=>0])->select()
  364. ->toArray();
  365. if(!empty($out)){
  366. $up =Db::name("order_out")->where(["outCode"=>$send,"status"=>0,"is_del"=>0])->update(["status"=>1,'send_status'=>1,'wsm_code'=>$cgdinfo['wsm_code'],"updatetime"=>date('Y-m-d H:i:s')]);
  367. //2023-03-14 17:34修改,增加'send_status'=>1 by 武
  368. if($up==false){
  369. Db::rollback();
  370. return error_show(1004,"出库单出库失败");
  371. }else{
  372. foreach ($out as $k => $v_outCode) {
  373. \app\admin\model\OrderOutChild::makeChild($v_outCode['outCode']);
  374. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  375. "order_code" => $v_outCode['outCode'],//出库单号
  376. "status" => 0,//这里的status是之前的值
  377. "action_remark" => '',//备注
  378. "action_type" => "status"//新建create,编辑edit,更改状态status
  379. ], "CKD", 1, $this->post);
  380. if ($is_stock == 1) {
  381. //库存品,推给库管和库管-张凯旋
  382. $roleid = config('app.wsm_cgder_role');
  383. $uids = Db::name('user_role')
  384. ->where('is_del', 0)
  385. ->whereIn('roleid', $roleid)
  386. ->column('uid');
  387. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  388. "order_type" => 'CKD',
  389. "order_code" => $v_outCode['outCode'],//出库单号
  390. "order_id" => $v_outCode['id'],
  391. "order_status" => 1,
  392. "before_status" => 0,
  393. // 'wait_id' => $supplier['personid'],
  394. // 'wait_name' => $supplier['person'],
  395. // 'holder_id' => $supplier['personid'],
  396. 'handle_user_list' => implode(',', $uids)
  397. ]);
  398. } else {
  399. //非库存品和采返商品,推给供应商负责人
  400. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  401. "order_type" => 'CKD',
  402. "order_code" => $v_outCode['outCode'],//出库单号
  403. "order_id" => $v_outCode['id'],
  404. "order_status" => 1,
  405. "before_status" => 0,
  406. 'wait_id' => $person['personid']??0,
  407. 'wait_name' => $person['person']??'',
  408. 'holder_id' =>$person['personid']??0,
  409. ]);
  410. }
  411. }
  412. }
  413. // $good1 = Db::name("good_stock")->where(["wsm_code"=>$cgdinfo['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find();
  414. // if($good1==false){
  415. // Db::rollback();
  416. // return error_show(1004,"未找到对应商品库存");
  417. // }
  418. // $out_num = intval(array_sum(array_column($out,"send_num")));
  419. // if($out_num>$good1['usable_stock']){
  420. // Db::rollback();
  421. // return error_show(1004,"库存数量不足发货");
  422. // }
  423. // $good1['usable_stock']-=$out_num;
  424. // $good1['wait_out_stock']+=$out_num;
  425. // $good1['total_stock']=$good1['usable_stock']+$good1['wait_out_stock'];
  426. // $good1['updatetime'] = date("Y-m-d H:i:s");
  427. // $upd= Db::name("good_stock")->save($good1);
  428. // if($upd==false){
  429. // Db::rollback();
  430. // return error_show(1004,"库存数量更新失败");
  431. // }
  432. // $stockid= isset($good['id'])?$good['id']:Db::name("good_stock")->getLastInsID();
  433. //
  434. // $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"wait_out_stock"];
  435. // $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"usable_stock"];
  436. // GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname],$good_data,'RKD');
  437. $sendp=Db::name("order_send")->where(["cgdNo"=>$cgdNo,"outCode"=>$send,"status"=>1])->save (["status"=>2]);
  438. if($sendp==false){
  439. Db::rollback();
  440. return error_show(1004,"库存发货失败");
  441. }
  442. }
  443. }
  444. if ($cgdinfo['order_type'] == 2) Db::name('good')
  445. ->data(['updatetime' => date('Y-m-d H:i:s')])
  446. ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
  447. ->inc('usable_stock', $send_num)
  448. ->update();
  449. else Db::name('good_zixun')
  450. ->data(['updatetime' => date('Y-m-d H:i:s')])
  451. ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
  452. ->inc('usable_stock', $send_num)
  453. ->update();
  454. }
  455. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  456. // $personid = Db::name('supplier')
  457. // ->where('is_del',0)
  458. // ->where('code',$cgdinfo['supplierNo'])
  459. // ->value('personid');
  460. // if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  461. // $is_stock = Db::name('good_basic')
  462. // ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  463. // ->value('is_stock');
  464. // if($is_stock==1){
  465. // //库存品,推给31库管人员、41库管-张凯旋
  466. // $uid = Db::name('user_role')
  467. // ->where([
  468. // ['is_del', '=', 0],
  469. // ['roleid', 'in', [31, 41]],
  470. // ['status', '=', 1]
  471. // ])->column('uid');
  472. // if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  473. //// $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)];
  474. //
  475. // }else{
  476. // if (($this->level == 2) && ($this->uid != $person['personid'])) throw new Exception('只能由供应商负责人操作');
  477. // }
  478. // }//else $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $personid];
  479. 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' => $person['personid']??0]);
  480. Db::commit();
  481. return app_show(0,'商品入库成功',["wsm_in_code"=>$wsm_in_code]);
  482. }else{
  483. Db::rollback();
  484. return error_show(1002,"商品入库失败");
  485. }
  486. }
  487. Db::commit();
  488. return app_show(0,'新建成功',["wsm_in_code"=>$wsm_in_code]);
  489. }
  490. Db::rollback();
  491. return error_show(1004,"新建失败");
  492. }catch (\Exception $e){
  493. Db::rollback();
  494. return error_show(1004, $e->getMessage());
  495. }
  496. }
  497. //批量添加入库单
  498. public function addBatch()
  499. {
  500. $param = $this->request->filter('trim')->only(['cgdNo'], 'post');
  501. $val = Validate::rule(['cgdNo|订单编号' => 'require|array|max:100']);
  502. //入库方式 供应商包邮,
  503. //运费 0
  504. if (!$val->check($param)) return json_show(1004, $val->getError());
  505. $cgdinfo = Db::name("purchease_order")
  506. ->whereIn('cgdNo', $param['cgdNo'])
  507. ->where(['is_del' => 0])
  508. ->where('status', 'in', [1, 2])//1待入库,2部分入库
  509. ->column('id,wsm_code,good_num,good_price,spuCode,order_type,send_status,status,supplierNo', 'cgdNo');
  510. if (empty($cgdinfo)) return json_show(1005, "未找到采购单数据或采购单未发货");
  511. $all_wsm_code = array_column($cgdinfo, 'wsm_code');
  512. $recep = Db::name('warehouse_addr')
  513. ->where('is_del', 0)
  514. ->whereIn('wsm_code', $all_wsm_code)
  515. ->column('wsm_name', 'wsm_code');
  516. $uid = $this->uid;
  517. $nickname = $this->uname;
  518. Db::startTrans();
  519. try {
  520. $date = date('Y-m-d H:i:s');
  521. // $supplier_cgderid = Db::name('supplier')
  522. // ->where('is_del',0)
  523. // ->whereIn('code',array_column($cgdinfo,'supplierNo'))
  524. // ->column('personid,person','code');
  525. $codes = array_unique(array_column($cgdinfo, 'supplierNo'));
  526. $temp = \app\admin\common\User::getIns()->handle('sGetList', ['more_code' => $codes, 'size' => count($codes)]);
  527. $supplier_cgderid = [];
  528. foreach ($temp['data']['list'] as $da) {
  529. if (!isset($supplier_cgderid[$da['code']])) $supplier_cgderid[$da['code']] = [
  530. 'personid' => $da['personid'],
  531. 'person' => $da['person'],
  532. ];
  533. }
  534. $i = 0;
  535. $status = 2;//4入库完成
  536. foreach ($param['cgdNo'] as $cgdNo) {
  537. $isreturn = cgd_sale_return($cgdNo);
  538. if ($isreturn) {
  539. throw new Exception("{$cgdNo}关联销售单存在退货单正在进行");
  540. }
  541. if (empty($cgdinfo[$cgdNo])) throw new Exception('未找到采购单数据或采购单未发货');
  542. $wsm_in_code = makeNo("CF");
  543. $bn_code = makeNo("BN");
  544. $send_num = $cgdinfo[$cgdNo]['good_num'];
  545. //改变编码规则,将原编码后两位换成序列号
  546. //str_pad字符串填充
  547. $wsm_in_code = substr($wsm_in_code, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  548. $i++;
  549. //入库单
  550. $win = Db::name("purchease_in")->insertGetId([
  551. "wsm_in_code" => $wsm_in_code,
  552. "cgdNo" => $cgdNo,
  553. "bnCode" => $bn_code,
  554. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  555. "wsm_reaper" => isset($recep[$cgdinfo[$cgdNo]['wsm_code']]) ? $recep[$cgdinfo[$cgdNo]['wsm_code']] : '',
  556. "send_num" => $cgdinfo[$cgdNo]['good_num'],
  557. "post_company" => '',
  558. "post_code" => '',
  559. "post_fee" => 0,//运费固定值:0
  560. "sendtime" => $date,
  561. "apply_id" => $uid,
  562. "apply_name" => $nickname,
  563. "wait_num" => 0,
  564. "wsm_num" => 0,
  565. "sendtype" => 2,//发货方式,固定值,供应商包邮2
  566. "status" => $status,
  567. "addtime" => $date,
  568. "updatetime" => $date
  569. ]);
  570. if ($win) {
  571. $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  572. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $sto, "RKD", $status, $this->request->filter('trim')->post());
  573. $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status, 'holder_id' => $uid];
  574. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  575. $order = ["order_code" => $cgdNo, "status" => $cgdinfo[$cgdNo]['status'], "action_remark" => '', "action_type" => "edit"];
  576. //维护台账信息
  577. Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . $date . "' WHERE `cgdNo`='{$cgdNo}'");
  578. //////////////////
  579. $good = Db::name("good_stock")
  580. ->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])
  581. ->lock(true)
  582. ->find();
  583. if (empty($good)) {
  584. $good = [
  585. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  586. "wsm_code" => $cgdinfo[$cgdNo]['wsm_code'],
  587. "usable_stock" => 0,
  588. "wait_out_stock" => 0,
  589. "wait_in_stock" => $send_num,
  590. "total_stock" => 0,
  591. "addtime" => date("Y-m-d H:i:s"),
  592. "updatetime" => date("Y-m-d H:i:s"),
  593. ];
  594. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "create"];
  595. } else {
  596. // $order = ["order_code" => $good['spuCode'], "status" => 1, "action_remark" => '', "action_type" => "edit"];
  597. }
  598. $good['wait_in_stock'] -= $send_num;
  599. // if ($good['presale_stock'] > 0) {
  600. // if ($good['presale_stock'] >= $send_num) {
  601. // $good['presale_stock'] -= $send_num;
  602. // $good['wait_out_stock'] += $send_num;
  603. // } else {
  604. // $good['presale_stock'] = 0;
  605. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  606. // $good['wait_out_stock'] += $good['presale_stock'];
  607. // }
  608. // } else {
  609. $good['usable_stock'] += $send_num;
  610. // }
  611. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  612. $good['updatetime'] = date("Y-m-d H:i:s");
  613. $upd = Db::name("good_stock")->save($good);
  614. $stockid = $good['id'] ?? Db::name("good_stock")->getLastInsID();
  615. //商品变动日志表,good_log_code字段存储采购单单号
  616. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => $stockid, "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  617. $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => $stockid, "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
  618. if ($upd) {
  619. GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  620. $bnin = GoodStockInfo::AddBn($stockid, $bn_code, $send_num, $cgdinfo[$cgdNo]['good_price']);
  621. if ($bnin == false) {
  622. Db::rollback();
  623. return error_show(1004, "库存bn数据新增失败");
  624. }
  625. if ($cgdinfo[$cgdNo]['order_type'] == 1) {
  626. $bk = Db::name("order_bk")->where(['cgdNo' => $cgdNo])->find();
  627. if ($bk == false) {
  628. $bk = [
  629. "cgdNo" => $cgdNo,
  630. "spuCode" => $cgdinfo[$cgdNo]['spuCode'],
  631. "companyNo" => $cgdinfo[$cgdNo]['companyNo'],
  632. "total_num" => $send_num,
  633. "merge_num" => 0,
  634. "balance_num" => $send_num,
  635. "status" => 1,
  636. "is_del" => 0,
  637. 'addtime' => date("Y-m-d H:i:s"),
  638. "updatetime" => date("Y-m-d H:i:s")
  639. ];
  640. $upcgd = Db::name("order_bk")->save($bk);
  641. } else {
  642. $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  643. $bk['balance_num'] += $send_num;
  644. $bk['total_num'] += $send_num;
  645. $bk['updatetime'] = date("Y-m-d H:i:s");
  646. $upcgd = Db::name("order_bk")->where($where)->update($bk);
  647. }
  648. if ($upcgd == false) throw new Exception('采购单数量更新失败');
  649. }
  650. if (in_array($cgdinfo[$cgdNo]['send_status'], [1, 2]) && $cgdinfo[$cgdNo]['order_type'] != 1) {
  651. $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  652. if (!empty($send)) {
  653. $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0,'is_del'=>0])->select()->toArray();
  654. if (!empty($out)) {
  655. $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0,'is_del'=>0])->update(["status" => 1,'send_status'=>1, 'wsm_code' => $cgdinfo[$cgdNo]['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
  656. //2023-03-14 17:34修改,增加'send_status'=>1 by 武
  657. if ($up == false) throw new Exception('出库单出库失败');
  658. else {
  659. //查询是否有非库存品订单
  660. $is_stock = Db::name('sale')
  661. ->where([
  662. ['is_del', '=', 0],
  663. ['orderCode', 'in', array_column($out, 'orderCode')]
  664. ])->column('is_stock', 'orderCode');
  665. foreach ($out as $k => $v_outCode) {
  666. \app\admin\model\OrderOutChild::makeChild($v_outCode['outCode']);
  667. //修改状态,添加待办
  668. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], [
  669. "order_code" => $v_outCode['outCode'],//出库单号
  670. "status" => 0,//这里的status是之前的值
  671. "action_remark" => '',//备注
  672. "action_type" => "status"//新建create,编辑edit,更改状态status
  673. ], "CKD", 1, $param);
  674. //库存品,推给库管和库管-张凯旋
  675. if ($is_stock[$v_outCode['orderCode']] == 1) {
  676. $roleid = config('app.wsm_cgder_role');
  677. $uids = Db::name('user_role')
  678. ->where('is_del', 0)
  679. ->whereIn('roleid', $roleid)
  680. ->column('uid');
  681. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  682. "order_type" => 'CKD',
  683. "order_code" => $v_outCode['outCode'],//出库单号
  684. "order_id" => $v_outCode['id'],
  685. "order_status" => 1,
  686. "before_status" => 0,
  687. // 'wait_id' => $wait['personid'],
  688. // 'wait_name' => $wait['person'],
  689. // 'holder_id' => $wait['personid'],
  690. 'handle_user_list' => implode(',', $uids)
  691. ]);
  692. } else {
  693. //推给供应商负责人
  694. $wait = $supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']];
  695. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], [
  696. "order_type" => 'CKD',
  697. "order_code" => $v_outCode['outCode'],//出库单号
  698. "order_id" => $v_outCode['id'],
  699. "order_status" => 1,
  700. "before_status" => 0,
  701. 'wait_id' => $wait['personid'],
  702. 'wait_name' => $wait['person'],
  703. 'holder_id' => $wait['personid'],
  704. ]);
  705. }
  706. }
  707. }
  708. // $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo[$cgdNo]['wsm_code'], "spuCode" => $cgdinfo[$cgdNo]['spuCode'], "is_del" => 0])->find();
  709. // if ($good1 == false) throw new Exception('未找到对应商品库存');
  710. // $out_num = intval(array_sum(array_column($out, "send_num")));
  711. // if ($out_num > $good1['usable_stock']) throw new Exception('库存数量不足发货');
  712. //
  713. // $good1['usable_stock'] -= $out_num;
  714. // $good1['wait_out_stock'] += $out_num;
  715. // $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  716. // $good1['updatetime'] = date("Y-m-d H:i:s");
  717. // $upd = Db::name("good_stock")->save($good1);
  718. // if ($upd == false) throw new Exception('库存数量更新失败');
  719. //
  720. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  721. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  722. // GoodLog::LogAdd(['id' => $uid, 'nickname' => $nickname], $good_data, 'RKD');
  723. $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
  724. if ($sendp == false) throw new Exception('库存发货失败');
  725. }
  726. }
  727. }
  728. ActionLog::logAdd(['id' => $uid, 'nickname' => $nickname], $order, "CGD", $cgdinfo[$cgdNo]['status'], $cgdinfo[$cgdNo]);
  729. $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo[$cgdNo]['id'], "order_status" => $cgdinfo[$cgdNo]['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => isset($supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']]['personid']) ? $supplier_cgderid[$cgdinfo[$cgdNo]['supplierNo']]['personid'] : 0];
  730. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $nickname], $process);
  731. // Db::commit();
  732. // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
  733. } else throw new Exception('商品入库失败');
  734. //////////////////
  735. } else throw new Exception('新建入库单失败');
  736. }
  737. //维护原有采购单信息
  738. Db::name("purchease_order")
  739. ->whereIn('id', array_column($cgdinfo, 'id'))
  740. ->update([
  741. 'wsend_num' => 0,
  742. 'send_num' => Db::raw('good_num'),
  743. 'send_status' => 3,
  744. 'status' => 3,
  745. 'updatetime' => date("Y-m-d H:i:s"),
  746. ]);
  747. Db::commit();
  748. return json_show(0, '新建入库单成功');
  749. // $win = Db::name("purchease_in")->insert($orin, true);
  750. // if ($win) {
  751. // $sto = ["order_code" => $wsm_in_code, "status" => $status, "action_remark" => '', "action_type" => "edit"];
  752. // ActionLog::logAdd($this->post['token'], $sto, "RKD", $status, $this->post);
  753. // $process = ["order_code" => $wsm_in_code, "order_id" => $win, "order_status" => $status, "order_type" => 'RKD', "before_status" => $status];
  754. // ProcessOrder::AddProcess($this->post['token'], $process);
  755. // $order = ["order_code" => $cgdNo, "status" => $cgdinfo['status'], "action_remark" => '', "action_type" => "edit"];
  756. //维护台账信息
  757. // 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}'");
  758. // if ($status == 1) {
  759. // Db::commit();
  760. // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
  761. // }
  762. // if ($status == 4) {
  763. // $cgdinfo['wsend_num'] -= $send_num;
  764. // $cgdinfo['send_num'] += $send_num;
  765. // $cgdinfo['send_status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
  766. // $cgdinfo['status'] = $cgdinfo['wsend_num'] == 0 ? 3 : ($cgdinfo['send_num'] == 0 ? 1 : 2);
  767. // $cgdinfo['updatetime'] = date("Y-m-d H:i:s");
  768. // $up = Db::name("purchease_order")->save($cgdinfo);
  769. // if ($up == false) {
  770. // Db::rollback();
  771. // return json_show(1004, "新建失败");
  772. // }
  773. // $good = Db::name("good_stock")
  774. // ->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])
  775. // ->lock(true)
  776. // ->find();
  777. // if (empty($good)) {
  778. // $good = [
  779. // "spuCode" => $cgdinfo['spuCode'],
  780. // "wsm_code" => $cgdinfo['wsm_code'],
  781. // "usable_stock" => 0,
  782. // "wait_out_stock" => 0,
  783. // "wait_in_stock" => $send_num,
  784. // "total_stock" => 0,
  785. // "addtime" => date("Y-m-d H:i:s"),
  786. // "updatetime" => date("Y-m-d H:i:s"),
  787. // ];
  788. // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  789. // // "action_type"=>"create"];
  790. // } else {
  791. // // $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  792. // // "action_type"=>"edit"];
  793. // }
  794. // $good['wait_in_stock'] -= $send_num;
  795. // if ($good['presale_stock'] > 0) {
  796. // if ($good['presale_stock'] >= $send_num) {
  797. // $good['presale_stock'] -= $send_num;
  798. // $good['wait_out_stock'] += $send_num;
  799. // } else {
  800. // $good['presale_stock'] = 0;
  801. // $good['usable_stock'] += $send_num - $good['presale_stock'];
  802. // $good['wait_out_stock'] += $good['presale_stock'];
  803. // }
  804. // } else {
  805. // $good['usable_stock'] += $send_num;
  806. // }
  807. //
  808. // $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  809. // $good['updatetime'] = date("Y-m-d H:i:s");
  810. // $upd = Db::name("good_stock")->save($good);
  811. // //商品变动日志表,good_log_code字段存储采购单单号
  812. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  813. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $send_num, "stock_name" => "wait_in_stock"];
  814. // if ($upd) {
  815. // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
  816. // if ($cgdinfo['order_type'] == 1) {
  817. // $bk = Db::name("order_bk")->where(['cgdNo' => $cgdinfo['cgdNo']])->find();
  818. // if ($bk == false) {
  819. // $bk = [
  820. // "cgdNo" => $cgdinfo['cgdNo'],
  821. // "spuCode" => $cgdinfo['spuCode'],
  822. // "companyNo" => $cgdinfo['companyNo'],
  823. // "total_num" => $send_num,
  824. // "merge_num" => 0,
  825. // "balance_num" => $send_num,
  826. // "status" => 1,
  827. // "is_del" => 0,
  828. // 'addtime' => date("Y-m-d H:i:s"),
  829. // "updatetime" => date("Y-m-d H:i:s")
  830. // ];
  831. // $upcgd = Db::name("order_bk")->save($bk);
  832. // } else {
  833. // $where = ['id' => $bk['id'], 'total_num' => $bk['total_num'], 'balance_num' => $bk['balance_num']];
  834. // $bk['balance_num'] += $send_num;
  835. // $bk['total_num'] += $send_num;
  836. // $bk['updatetime'] = date("Y-m-d H:i:s");
  837. // $upcgd = Db::name("order_bk")->where($where)->update($bk);
  838. // }
  839. //
  840. // if ($upcgd == false) {
  841. // Db::rollback();
  842. // return json_show(1004, "采购单数量更新失败");
  843. // }
  844. // }
  845. // if ($cgdinfo['send_status'] == 3 && $cgdinfo['order_type'] != 1) {
  846. // $send = Db::name("order_send")->where(["cgdNo" => $cgdNo, "status" => 1])->column("outCode");
  847. // if (!empty($send)) {
  848. // $out = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->select()
  849. // ->toArray();
  850. // if (!empty($out)) {
  851. // $up = Db::name("order_out")->where(["outCode" => $send, "status" => 0])->update
  852. // (["status" => 1, 'wsm_code' => $cgdinfo['wsm_code'], "updatetime" => date('Y-m-d H:i:s')]);
  853. // if ($up == false) {
  854. // Db::rollback();
  855. // return json_show(1004, "出库单出库失败");
  856. // } else {
  857. // foreach ($out as $k => $v_outCode) {
  858. // //修改状态,添加待办
  859. // ActionLog::logAdd($this->post['token'], [
  860. // "order_code" => $v_outCode['outCode'],//出库单号
  861. // "status" => 0,//这里的status是之前的值
  862. // "action_remark" => '',//备注
  863. // "action_type" => "status"//新建create,编辑edit,更改状态status
  864. // ], "CKD", 1, $this->post);
  865. //
  866. // ProcessOrder::AddProcess($this->post['token'], [
  867. // "order_type" => 'CKD',
  868. // "order_code" => $v_outCode['outCode'],//出库单号
  869. // "order_id" => $v_outCode['id'],
  870. // "order_status" => 1, "before_status" => 0
  871. // ]);
  872. // }
  873. // }
  874. // $good1 = Db::name("good_stock")->where(["wsm_code" => $cgdinfo['wsm_code'], "spuCode" => $cgdinfo['spuCode'], "is_del" => 0])->find();
  875. // if ($good1 == false) {
  876. // Db::rollback();
  877. // return json_show(1004, "未找到对应商品库存");
  878. // }
  879. // $out_num = intval(array_sum(array_column($out, "send_num")));
  880. // if ($out_num > $good1['usable_stock']) {
  881. // Db::rollback();
  882. // return json_show(1004, "库存数量不足发货");
  883. // }
  884. // $good1['usable_stock'] -= $out_num;
  885. // $good1['wait_out_stock'] += $out_num;
  886. // $good1['total_stock'] = $good1['usable_stock'] + $good1['wait_out_stock'];
  887. // $good1['updatetime'] = date("Y-m-d H:i:s");
  888. // $upd = Db::name("good_stock")->save($good1);
  889. // if ($upd == false) {
  890. // Db::rollback();
  891. // return json_show(1004, "库存数量更新失败");
  892. // }
  893. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  894. // $good_data[] = ['good_log_code' => $wsm_in_code, "stock_id" => isset($good['id']) ? $good['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $send_num, "stock_name" => "usable_stock"];
  895. // GoodLog::LogAdd($this->post['token'], $good_data, 'RKD');
  896. // $sendp = Db::name("order_send")->where(["cgdNo" => $cgdNo, "outCode" => $send, "status" => 1])->save(["status" => 2]);
  897. // if ($sendp == false) {
  898. // Db::rollback();
  899. // return json_show(1004, "库存发货失败");
  900. // }
  901. // }
  902. // }
  903. // }
  904. // ActionLog::logAdd($this->post['token'], $order, "CGD", $cgdinfo['status'], $cgdinfo);
  905. // $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status']];
  906. // ProcessOrder::AddProcess($this->post['token'], $process);
  907. // Db::commit();
  908. // return json_show(0, '商品入库成功', ["wsm_in_code" => $wsm_in_code]);
  909. // } else {
  910. // Db::rollback();
  911. // return json_show(1002, "商品入库失败");
  912. // }
  913. // }
  914. // Db::commit();
  915. // return json_show(0, '新建成功', ["wsm_in_code" => $wsm_in_code]);
  916. // }
  917. // Db::rollback();
  918. // return json_show(1004, "新建失败");
  919. } catch (Exception $e) {
  920. Db::rollback();
  921. return json_show(1004, $e->getMessage().'|'.$e->getFile().'|'.$e->getLine());
  922. }
  923. }
  924. public function info(){
  925. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  926. if($incode==""){
  927. return error_show(1004,"参数wsm_in_code 不为空");
  928. }
  929. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  930. if(empty($info)){
  931. return error_show(1004,"未找到数据");
  932. }
  933. $dom = Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],'is_del'=>0])->find();
  934. if($dom['order_type']==3|| $dom['order_type']==4){
  935. $goon = Db::name("good_zixun")->where(["spuCode"=>$dom['spuCode'],"is_del"=>0])->find();
  936. }else {
  937. $goon =Db::name('good_basic')->where(['spuCode'=>$dom['spuCode']])->find();
  938. }
  939. if(empty($goon)){
  940. return error_show(1002,"未找到商品数据");
  941. }
  942. $cat= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  943. $info['cgder']=$dom['cgder'];
  944. $info['spuCode']=$dom['spuCode'];
  945. $info['good_name']=$dom['good_name'];
  946. $info['skuCode']=$dom['skuCode'];
  947. $info['good_num']=$dom['good_num'];
  948. $info['good_price']=$dom['good_price'];
  949. $info['total_fee']=$dom['total_fee'];
  950. $info['diff_fee']=$dom['diff_fee'];
  951. $info['diff_weight']=$dom['diff_weight'];
  952. $info['gold_price']=$dom['gold_price'];
  953. $wsmcode = Db::name("warehouse_info")
  954. ->alias("a")
  955. // ->leftJoin("supplier b","a.supplierNo=b.code")
  956. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  957. ->where(["a.wsm_code"=>$info['wsm_code']])
  958. ->field("a.name as wsm_name,a.contactor,a.contactor_name,a.supplierNo,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code")
  959. ->find();
  960. $names = \app\admin\common\User::getIns()->handle('getCodeAndName',['code'=>$wsmcode['supplierNo']]);
  961. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  962. $info['wsm_supplier'] =$names['data'][$wsmcode['supplierNo']]??'';
  963. $info['wsm_supplierNo'] =$wsmcode['supplierNo']??"";
  964. $info['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  965. $info['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  966. $info['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  967. $info['wsm_addr'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  968. $info['wsm_addr'].=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  969. $info['contactor'] =$wsmcode['contactor']??"";
  970. $info['contactor_name'] =$wsmcode['contactor_name']??"";
  971. $check = Db::name("purchease_info")->alias("a")->leftJoin("result_info b","a.error_code=b.result_code")->where
  972. (['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();
  973. $info['checkinfo']= $check;
  974. $return = Db::name("purchease_return")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  975. $return['return_contactor']=isset($return['return_contactor'])&&$return['return_contactor']!="" ?$return['return_contactor']:'';
  976. $return['return_addr']=isset($return['return_addr'])&&$return['return_addr']!="" ?$return['return_addr']:'';
  977. $return['return_mobile']=isset($return['return_mobile'])&&$return['return_mobile']!="" ?$return['return_mobile']:'';
  978. //以下字段临时补充到return中
  979. $return['wsm_in_code'] = $incode;
  980. $return['wsm_num'] = isset($return['wsm_num']) ? $return['wsm_num'] : 0;
  981. $return['reissue_num'] = isset($return['reissue_num']) ? $return['reissue_num'] : 0;
  982. $return['return_num'] = isset($return['return_num']) ? $return['return_num'] : 0;
  983. $return['contactor'] = $return['return_contactor'];
  984. $return['addr'] = $return['return_addr'];
  985. $return['mobile'] = $return['return_mobile'];
  986. $return['error_remark'] = isset($return['error_remark']) ? $return['error_remark'] : '';
  987. $return['remark'] = isset($return['remark']) ? $return['remark'] : '';
  988. $info['return']= $return;
  989. $info['can']=$cat;
  990. $info['company'] = '';
  991. if(isset($dom['companyNo'])&&$dom['companyNo']!=""){
  992. // $company = Db::name("business")->where(["companyNo"=>$dom['companyNo']])->find();
  993. $userCommon = \app\admin\common\User::getIns();
  994. $tmp = $userCommon->handle('getCodeAndName', ['code' => [$dom['companyNo']]]);
  995. $info['company'] = $tmp['data'][$dom['companyNo']]??'';
  996. }
  997. // $info['company'] = isset($company['company'])?$company['company']:"";
  998. $info['companyNo'] = isset($dom['companyNo'])?$dom['companyNo']:"";
  999. $info['order_type'] = isset($dom['order_type'])?$dom['order_type']:"";
  1000. return app_show(0,'获取成功',$info);
  1001. }
  1002. public function check(){
  1003. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  1004. if($incode==""){
  1005. return error_show(1004,"参数wsm_in_code 不为空");
  1006. }
  1007. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  1008. if(empty($info)){
  1009. return error_show(1004,"未找到数据");
  1010. }
  1011. if($info['status']!=1){
  1012. return error_show(1004,"发货单状态有误");
  1013. }
  1014. $order_num = isset($this->post['order_num']) && $this->post['order_num']!==''?intval($this->post['order_num'])
  1015. :"";
  1016. if($order_num===""){
  1017. return error_show(1004,"参数 order_num 不为空");
  1018. }
  1019. $error_list = isset($this->post['error_list']) && $this->post['error_list']!==''?$this->post['error_list']:"";
  1020. Db::startTrans();
  1021. try{
  1022. $order = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"edit"];
  1023. $info['wsm_num'] = $order_num;
  1024. $info['wait_num'] = $info['send_num'];
  1025. $info['checkid'] = $this->uid;
  1026. $info['check_name'] = $this->uname;
  1027. $info['status'] =2;
  1028. $info['error_num'] = $info['send_num']-$order_num;
  1029. $info['updatetime'] = date("Y-m-d H:i:s");
  1030. $up =Db::name("purchease_in")->save($info);
  1031. if($up){
  1032. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"RKD",2,$this->post);
  1033. $uid = Db::name('user_role')
  1034. ->where([
  1035. ['is_del', '=', 0],
  1036. ['roleid', 'in', [31, 41]],
  1037. ['status', '=', 1]
  1038. ])->column('uid');
  1039. $handle_user_list = implode(',', $uid);
  1040. $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];
  1041. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1042. if(!empty($error_list)){
  1043. $data=[];
  1044. foreach ($error_list as $value ){
  1045. $temp=[];
  1046. $temp['wsm_in_code']=$info['wsm_in_code'];
  1047. $temp['num']=$value['num'];
  1048. $temp['error_code']=$value['error_code'];
  1049. $temp['error_img']=$value['error_img'];
  1050. $temp['remark']=$value['remark'];
  1051. $temp['is_del']=0;
  1052. $temp['addtime']=date("Y-m-d H:i:s");
  1053. $temp['updatetime']=date("Y-m-d H:i:s");
  1054. $data[]=$temp;
  1055. }
  1056. $derr= Db::name("purchease_info")->insertAll($data);
  1057. if($derr){
  1058. Db::commit();
  1059. return app_show(0,"验货完成");
  1060. }else{
  1061. Db::rollback();
  1062. return error_show(1002,"验货数据保存失败");
  1063. }
  1064. }
  1065. Db::commit();
  1066. return app_show(0,"验货完成");
  1067. }
  1068. Db::rollback();
  1069. return error_show(1002,"验货数据保存失败");
  1070. }catch (\Exception $e){
  1071. Db::rollback();
  1072. return error_show(1002,$e->getMessage());
  1073. }
  1074. }
  1075. public function status(){
  1076. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  1077. if($incode==""){
  1078. return error_show(1004,"参数wsm_in_code 不为空");
  1079. }
  1080. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  1081. if(empty($info)){
  1082. return error_show(1004,"未找到数据");
  1083. }
  1084. $status = isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1085. if($status===""){
  1086. return error_show(1004,"参数status 不为空");
  1087. }
  1088. if($info['status']==4){
  1089. return error_show(1004,"入库单已完成");
  1090. }
  1091. $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find();
  1092. if(empty($cgdinfo)){
  1093. return error_show(1002,"未找到采购单数据");
  1094. }
  1095. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']) :"";
  1096. $userCommon= \app\admin\common\User::getIns();
  1097. $supplier_temp =$userCommon->handle("sInfo",["code"=>$cgdinfo['supplierNo']]);
  1098. if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1002,"采购单供应商不存在");
  1099. $holder_id=$supplier_temp['data']['personid']??0;
  1100. Db::startTrans();
  1101. try{
  1102. $order = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"status"];
  1103. $info['status'] = $status;
  1104. $info['remark'] = $remark;
  1105. $info['updatetime'] = date("Y-m-d H:i:s");
  1106. $update = Db::name("purchease_in")->save($info);
  1107. if($update){
  1108. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"RKD",$status,$this->post);
  1109. $uid = Db::name('user_role')
  1110. ->where([
  1111. ['is_del', '=', 0],
  1112. ['roleid', 'in', [31, 41]],
  1113. ['status', '=', 1]
  1114. ])->column('uid');
  1115. $handle_user_list = implode(',', $uid);
  1116. $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];
  1117. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1118. if($status==0){
  1119. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  1120. $cgdinfo['wsend_num']+=$info['send_num'];
  1121. $cgdinfo['send_num']-=$info['send_num'];
  1122. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1123. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1124. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  1125. $up=Db::name("purchease_order")->save($cgdinfo);
  1126. if($up){
  1127. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  1128. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  1129. $is_stock = Db::name('good_basic')
  1130. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  1131. ->value('is_stock');
  1132. if($is_stock==1){
  1133. //库存品,推给31库管人员、41库管-张凯旋
  1134. $uid = Db::name('user_role')
  1135. ->where([
  1136. ['is_del', '=', 0],
  1137. ['roleid', 'in', [31, 41]],
  1138. ['status', '=', 1]
  1139. ])->column('uid');
  1140. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  1141. $handle_user_list=implode(",",$uid);
  1142. // $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)];
  1143. }else{
  1144. //$holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  1145. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  1146. //$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];
  1147. }//非库存品和采返商品,推给供应商负责人
  1148. }//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)];
  1149. $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];
  1150. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  1151. Db::commit();
  1152. return app_show(0,'取消成功');
  1153. }else{
  1154. Db::rollback();
  1155. return error_show(1002,"取消失败");
  1156. }
  1157. }
  1158. if($status==1){
  1159. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  1160. $cgdinfo['wsend_num']-=$info['send_num'];
  1161. $cgdinfo['send_num']+=$info['send_num'];
  1162. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1163. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1164. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  1165. $up=Db::name("purchease_order")->save($cgdinfo);
  1166. if($up){
  1167. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  1168. if($cgdinfo['status'] == 1 || $cgdinfo['status']==2){
  1169. $is_stock = Db::name('good_basic')
  1170. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  1171. ->value('is_stock');
  1172. if($is_stock==1){
  1173. //库存品,推给31库管人员、41库管-张凯旋
  1174. $uid = Db::name('user_role')
  1175. ->where([
  1176. ['is_del', '=', 0],
  1177. ['roleid', 'in', [31, 41]],
  1178. ['status', '=', 1]
  1179. ])->column('uid');
  1180. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  1181. $handle_user_list = implode(",",$uid);
  1182. // $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)];
  1183. }elseif ($is_stock==0 || $cgdinfo['order_type']==3) {
  1184. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  1185. // $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];
  1186. }//非库存品和采返商品,推给供应商负责人
  1187. }//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)];
  1188. $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);
  1189. Db::commit();
  1190. return app_show(0,'发货成功');
  1191. }else{
  1192. Db::rollback();
  1193. return error_show(1002,"发货失败");
  1194. }
  1195. }
  1196. if($status==4){
  1197. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  1198. $cgdinfo['wsend_num']-=$info['send_num'];
  1199. $cgdinfo['send_num']+=$info['send_num'];
  1200. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1201. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1202. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  1203. $up=Db::name("purchease_order")->save($cgdinfo);
  1204. if($up==false){
  1205. Db::rollback();
  1206. return error_show(1002,"发货失败");
  1207. }
  1208. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  1209. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  1210. $is_stock = Db::name('good_basic')
  1211. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  1212. ->value('is_stock');
  1213. if($is_stock==1){
  1214. //库存品,推给31库管人员、41库管-张凯旋
  1215. $uid = Db::name('user_role')
  1216. ->where([
  1217. ['is_del', '=', 0],
  1218. ['roleid', 'in', [31, 41]],
  1219. ['status', '=', 1]
  1220. ])->column('uid');
  1221. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  1222. }else{
  1223. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  1224. // $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];
  1225. }//非库存品和采返商品,推给供应商负责人
  1226. }//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)];
  1227. $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];
  1228. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1229. $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find();
  1230. if(empty($good)) {
  1231. $good=[
  1232. "spuCode"=>$cgdinfo['spuCode'],
  1233. "wsm_code"=>$info['wsm_code'],
  1234. "usable_stock"=>0,
  1235. "wait_out_stock"=>0,
  1236. "wait_in_stock"=>$info['wsm_num'],
  1237. "total_stock"=>0,
  1238. "addtime"=>date("Y-m-d H:i:s"),
  1239. "updatetime"=>date("Y-m-d H:i:s"),
  1240. ];
  1241. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  1242. "action_type"=>"create"];
  1243. }else{
  1244. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  1245. }
  1246. $good['wait_in_stock']-=$info['wsm_num'];
  1247. $good['usable_stock']+=$info['wsm_num'];
  1248. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  1249. $good['updatetime'] = date("Y-m-d H:i:s");
  1250. $upd= Db::name("good_stock")->save($good);
  1251. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  1252. //商品变动日志表,good_log_code字段存储入库单编号
  1253. $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"];
  1254. $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"];
  1255. if($upd){
  1256. $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$info['wsm_num'],$cgdinfo['good_price']);
  1257. if($bnin==false){
  1258. Db::rollback();
  1259. return error_show(1004,"库存bn数据新增失败");
  1260. }
  1261. if($cgdinfo['order_type']==1){
  1262. //商品表里的'可用总库存数'增加
  1263. Db::name('good')
  1264. ->data(['updatetime' => date('Y-m-d H:i:s')])
  1265. ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
  1266. ->inc('usable_stock', $info['wsm_num'])
  1267. ->update();
  1268. } //商品表里的'可用总库存数'增加
  1269. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname],$good_data,'RKD');
  1270. Db::commit();
  1271. return app_show(0,'商品入库成功');
  1272. }else{
  1273. Db::rollback();
  1274. return error_show(1002,"商品入库失败");
  1275. }
  1276. }
  1277. Db::commit();
  1278. return app_show(0,"状态修改完成");
  1279. }
  1280. Db::rollback();
  1281. return error_show(1002,"状态修改失败");
  1282. }catch (\Exception $e){
  1283. Db::rollback();
  1284. return error_show(1002,$e->getMessage());
  1285. }
  1286. }
  1287. /** 0代发货 1库管验货 2仓库主管审核 3 采购审核 4 入库完成 5填写物流 6 退货完成
  1288. /** 0代发货 1待库管验货 2入库完成
  1289. * @return \think\response\Json|void
  1290. * @throws \think\db\exception\DataNotFoundException
  1291. * @throws \think\db\exception\DbException
  1292. * @throws \think\db\exception\ModelNotFoundException
  1293. */
  1294. public function ReturnAdd(){
  1295. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  1296. if($incode==""){
  1297. return error_show(1004,"参数wsm_in_code 不为空");
  1298. }
  1299. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  1300. if(empty($info)){
  1301. return error_show(1004,"未找到数据");
  1302. }
  1303. //正常入库数量
  1304. $wsm_num = isset($this->post['wsm_num']) &&$this->post['wsm_num']!=="" ? intval($this->post['wsm_num']):"" ;
  1305. if($wsm_num===""){
  1306. return error_show(1004,"参数wsm_num 不为空");
  1307. }
  1308. //补货数量
  1309. $reissue_num = isset($this->post['reissue_num']) &&$this->post['reissue_num']!=="" ? intval($this->post['reissue_num']):"0" ;
  1310. //退货数量
  1311. $return_num = isset($this->post['return_num']) &&$this->post['return_num']!=="" ? intval($this->post['return_num']):"0" ;
  1312. $contactor = isset($this->post['contactor']) &&$this->post['contactor']!=="" ? trim($this->post['contactor']):"" ;
  1313. if($contactor==""){
  1314. return error_show(1004,"参数contactor 不为空");
  1315. }
  1316. $addr = isset($this->post['addr']) &&$this->post['addr']!=="" ? trim($this->post['addr']):"" ;
  1317. if($addr==""){
  1318. return error_show(1004,"参数addr 不为空");
  1319. }
  1320. $mobile = isset($this->post['mobile']) &&$this->post['mobile']!=="" ? trim($this->post['mobile']):"" ;
  1321. if($mobile==""){
  1322. return error_show(1004,"参数mobile 不为空");
  1323. }
  1324. $error_remark = isset($this->post['error_remark']) &&$this->post['error_remark']!=="" ? trim($this->post['error_remark']):"" ;
  1325. $remark = isset($this->post['remark']) &&$this->post['remark']!=="" ? trim($this->post['remark']):"" ;
  1326. $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find();
  1327. if(empty($cgdinfo)){
  1328. return error_show(1002,"未找到采购单数据");
  1329. }
  1330. $userCommon= \app\admin\common\User::getIns();
  1331. $supplier_temp =$userCommon->handle("sInfo",["code"=>$cgdinfo['supplierNo']]);
  1332. if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1002,"采购单供应商不存在");
  1333. $holder_id=$supplier_temp['data']['personid']??0;
  1334. Db::startTrans();
  1335. try{
  1336. $sto = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"edit"];
  1337. $info['status'] = $info['send_num'] == $wsm_num ? 4 : 5;//艳蓉说,这里不能用入库数量比较,应该是用入库数量,即总数来比较
  1338. $info['is_th'] =$return_num>0 ?1:0;
  1339. $info['wsm_num']= $wsm_num;
  1340. $info['updatetime'] = date("Y-m-d H:i:s");
  1341. $up= Db::name("purchease_in")->save($info);
  1342. if($up){
  1343. //库存品,推给仓库对应的管理员
  1344. $uid = Db::name('warehouse_info')
  1345. ->where(['wsm_code' => $info['wsm_code'], 'is_del' => 0])
  1346. ->column('contactor');
  1347. $handle_user_list=implode(",",$uid);
  1348. if($info['status']==4){
  1349. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  1350. $cgdinfo['wsend_num']-=$info['send_num'];
  1351. $cgdinfo['send_num']+=$info['send_num'];
  1352. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1353. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1354. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  1355. $up=Db::name("purchease_order")->save($cgdinfo);
  1356. if($up==false){
  1357. Db::rollback();
  1358. return error_show(1002,"发货失败");
  1359. }
  1360. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $order, "CGD", $cgdinfo['status'], $cgdinfo);
  1361. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  1362. $is_stock = Db::name('good_basic')
  1363. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  1364. ->value('is_stock');
  1365. if($is_stock==1){
  1366. //库存品,推给仓库对应的负责人
  1367. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  1368. }else{
  1369. // $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  1370. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  1371. // $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];
  1372. }//非库存品和采返商品,推给供应商负责人
  1373. }//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)];
  1374. $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];
  1375. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname],$process);
  1376. $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find();
  1377. if(empty($good)) {
  1378. $good=[
  1379. "spuCode"=>$cgdinfo['spuCode'],
  1380. "wsm_code"=>$info['wsm_code'],
  1381. "usable_stock"=>0,
  1382. "wait_out_stock"=>0,
  1383. "wait_in_stock"=>$wsm_num,
  1384. "total_stock"=>0,
  1385. "addtime"=>date("Y-m-d H:i:s"),
  1386. "updatetime"=>date("Y-m-d H:i:s"),
  1387. ];
  1388. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  1389. "action_type"=>"create"];
  1390. }else{
  1391. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  1392. }
  1393. $good['wait_in_stock']-=$wsm_num;
  1394. $good['usable_stock']+=$wsm_num;
  1395. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  1396. $good['updatetime'] = date("Y-m-d H:i:s");
  1397. $upd= Db::name("good_stock")->save($good);
  1398. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  1399. if($upd){
  1400. $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$info['wsm_num'],$cgdinfo['good_price']);
  1401. if($bnin==false){
  1402. Db::rollback();
  1403. return error_show(1004,"库存bn数据新增失败");
  1404. }
  1405. }else{
  1406. Db::rollback();
  1407. return error_show(1002,"商品入库失败");
  1408. }
  1409. $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"];
  1410. $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"];
  1411. GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname],$good_data,'RKD');
  1412. $bk =Db::name("order_bk")->where(['cgdNo'=>$cgdinfo['cgdNo']])->lock(true)->find();
  1413. if($bk==false){
  1414. $bk=[
  1415. "cgdNo"=>$cgdinfo['cgdNo'],
  1416. "spuCode"=>$cgdinfo['spuCode'],
  1417. "companyNo"=>$cgdinfo['companyNo'],
  1418. "total_num"=>0,
  1419. "merge_num"=>0,
  1420. "balance_num"=>0,
  1421. "status"=>1,
  1422. "is_del"=>0,
  1423. 'addtime'=>date("Y-m-d H:i:s"),
  1424. "updatetime"=>date("Y-m-d H:i:s")
  1425. ];
  1426. }
  1427. $bk['balance_num']+=$wsm_num;
  1428. $bk['total_num']+=$wsm_num;
  1429. $bk['updatetime']=date("Y-m-d H:i:s");
  1430. $upcgd =Db::name("order_bk")->save($bk);
  1431. if($upcgd==false){
  1432. Db::rollback();
  1433. return error_show(1004,"采购单数量更新失败");
  1434. }
  1435. }
  1436. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname],$sto,"RKD", $info['status'],$this->post);
  1437. $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??""];
  1438. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname],$process);
  1439. //需要在详情页把相关退货记录原样取出来,所以这里把if去掉,所有记录都入purchease_return表
  1440. if($info['status']==5){
  1441. $returnCode=makeNo("CR");
  1442. $data=[
  1443. "returnCode"=>$returnCode,
  1444. "wsm_in_code"=>$incode,
  1445. "cgdNo"=>$cgdinfo['cgdNo'],
  1446. "wsm_num"=>$wsm_num,
  1447. "reissue_num"=>$reissue_num,
  1448. "return_num"=>$return_num,
  1449. "return_contactor"=>$contactor,
  1450. "return_addr"=>$addr,
  1451. "return_mobile"=>$mobile,
  1452. "error_remark"=>$error_remark,
  1453. "post_code"=>"",
  1454. "post_company"=>"",
  1455. "remark"=>$remark ,
  1456. "apply_id"=>$this->uid,
  1457. "apply_name"=>$this->uname,
  1458. "status"=>0,
  1459. "addtime"=>date("Y-m-d H:i:s"),
  1460. "updatetime"=>date("Y-m-d H:i:s")
  1461. ];
  1462. $res = Db::name("purchease_return")->insert($data,true);
  1463. if($res==false){
  1464. Db::rollback();
  1465. return error_show(1002,"验货审核失败");
  1466. }
  1467. $sto = ["order_code"=>$incode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1468. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname],$sto,"RKTHD",0,$data);
  1469. //维护台账记录
  1470. // Db::name('standing_book')
  1471. // ->where('wsm_in_code', $incode)
  1472. // ->update([
  1473. // 'cgdReturnCode' => $returnCode,
  1474. // 'updatetime' => date('Y-m-d H:i:s'),
  1475. // ]);
  1476. 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可能有多个
  1477. }
  1478. Db::commit();
  1479. return app_show(0,"验货审核完成");
  1480. }
  1481. }catch (\Exception $e){
  1482. Db::rollback();
  1483. return error_show(1002,$e->getMessage());
  1484. }
  1485. }
  1486. public function ReturnSend(){
  1487. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  1488. if($incode==""){
  1489. return error_show(1004,"参数wsm_in_code 不为空");
  1490. }
  1491. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  1492. if(empty($info)){
  1493. return error_show(1004,"未找到发货单数据");
  1494. }
  1495. $return = Db::name("purchease_return")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  1496. if(empty($return)){
  1497. return error_show(1004,"未找到退货单数据");
  1498. }
  1499. $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find();
  1500. if(empty($cgdinfo)){
  1501. return error_show(1002,"未找到采购单数据");
  1502. }
  1503. $post_code = isset($this->post['post_code']) &&$this->post['post_code']!=="" ? trim($this->post['post_code']):"" ;
  1504. if($post_code==""){
  1505. return error_show(1004,"参数 post_code 不能为空");
  1506. }
  1507. $post_company = isset($this->post['post_company']) &&$this->post['post_company']!=="" ? trim($this->post['post_company']):"" ;
  1508. if($post_company==""){
  1509. return error_show(1004,"参数 post_company 不能为空");
  1510. }
  1511. $remark = isset($this->post['remark']) &&$this->post['remark']!=="" ? trim($this->post['remark']):"" ;
  1512. $data=[
  1513. "post_code"=>$post_code,
  1514. "post_company"=>$post_company,
  1515. "remark"=>$remark,
  1516. "status"=>1,
  1517. "updatetime"=>date("Y-m-d H:i:s")
  1518. ];
  1519. $userCommon= \app\admin\common\User::getIns();
  1520. $supplier_temp =$userCommon->handle("sInfo",["code"=>$cgdinfo['supplierNo']]);
  1521. if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1002,"采购单供应商不存在");
  1522. $holder_id=$supplier_temp['data']['personid']??0;
  1523. Db::startTrans();
  1524. try{
  1525. $up =Db::name("purchease_return")->where($return)->save($data);
  1526. if($up){
  1527. $sto = ["order_code"=>$info['wsm_in_code'],"status"=>$return['status'],"action_remark"=>'', "action_type"=>"edit"];
  1528. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$sto,"RKTHD",1,$this->post);
  1529. $ark=$info['status'];
  1530. $info['status']=6;
  1531. $info['updatetime']=date("Y-m-d H:i:s");
  1532. $sa=Db::name("purchease_in")->save($info);
  1533. if($sa){
  1534. $stn = ["order_code"=>$info['wsm_in_code'],"status"=>$ark,"action_remark"=>'',"action_type"=>"edit"];
  1535. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$stn,"RKD",6,$info);
  1536. // $uid = Db::name('user_role')
  1537. // ->where([
  1538. // ['is_del', '=', 0],
  1539. // ['roleid', 'in', [31, 41]],
  1540. // ['status', '=', 1]
  1541. // ])->column('uid');
  1542. //库管人员取仓库管理员
  1543. $uid = Db::name('warehouse_info')->where(['is_del'=>0,'wsm_code'=>$info['wsm_code']])->column('contactor');
  1544. $handle_user_list = implode(',', $uid);
  1545. $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];
  1546. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1547. $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find();
  1548. if(empty($good)) {
  1549. $good=[
  1550. "spuCode"=>$cgdinfo['spuCode'],
  1551. "wsm_code"=>$info['wsm_code'],
  1552. "usable_stock"=>0,
  1553. "wait_out_stock"=>0,
  1554. "wait_in_stock"=>$return['wsm_num'],
  1555. "total_stock"=>0,
  1556. "addtime"=>date("Y-m-d H:i:s"),
  1557. "updatetime"=>date("Y-m-d H:i:s"),
  1558. ];
  1559. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  1560. }else{
  1561. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  1562. }
  1563. $good['wait_in_stock']-=$return['wsm_num']+$return['return_num']-$return['reissue_num'];
  1564. $good['usable_stock']+=$return['wsm_num'];
  1565. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  1566. $good['updatetime'] = date("Y-m-d H:i:s");
  1567. $upd= Db::name("good_stock")->save($good);
  1568. if($upd==false){
  1569. Db::rollback();
  1570. return error_show(1002,"库存更新失败");
  1571. }
  1572. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  1573. $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$return['wsm_num'],$cgdinfo['good_price']);
  1574. if($bnin==false){
  1575. Db::rollback();
  1576. return error_show(1004,"库存bn数据新增失败");
  1577. }
  1578. $good_data[]=['good_log_code' => $incode,"stock_id"=>$stockid,"type"=>1, 'stock'=>$info['wsm_num'],"stock_name"=>"usable_stock"];
  1579. $good_data[]=['good_log_code' => $incode,"stock_id"=>$stockid,"type"=>2, 'stock'=>$good['wait_in_stock'],"stock_name"=>"wait_in_stock"];
  1580. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname],$good_data,'RKD');
  1581. $order = ["order_code"=>$cgdinfo['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  1582. $cgdinfo['wsend_num']-=$return['wsm_num']+$return['return_num']-$return['reissue_num'];
  1583. $cgdinfo['send_num']+=$return['wsm_num']+$return['return_num']-$return['reissue_num'];
  1584. $cgdinfo['th_num']+=$return['return_num']-$return['reissue_num'];
  1585. $cgdinfo['th_fee']=round(($return['return_num']-$return['reissue_num'])*$cgdinfo['good_price'],2);
  1586. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1587. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1588. if($cgdinfo['th_num']==$cgdinfo['send_num']&& $cgdinfo['wsend_num']==0){
  1589. $cgdinfo['status']=4;
  1590. }
  1591. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  1592. $up=Db::name("purchease_order")->save($cgdinfo);
  1593. if($up==false){
  1594. Db::rollback();
  1595. return error_show(1004,"采购单数量更新失败");
  1596. }
  1597. $bk =Db::name("order_bk")->where(['cgdNo'=>$cgdinfo['cgdNo']])->lock(true)->find();
  1598. if($bk==false){
  1599. $bk=[
  1600. "cgdNo"=>$cgdinfo['cgdNo'],
  1601. "spuCode"=>$cgdinfo['spuCode'],
  1602. "companyNo"=>$cgdinfo['companyNo'],
  1603. "total_num"=>0,
  1604. "merge_num"=>0,
  1605. "balance_num"=>0,
  1606. "status"=>1,
  1607. "is_del"=>0,
  1608. 'addtime'=>date("Y-m-d H:i:s"),
  1609. "updatetime"=>date("Y-m-d H:i:s")
  1610. ];
  1611. }
  1612. $bk['balance_num']+=$return['wsm_num'];
  1613. $bk['total_num']+=$return['wsm_num'];
  1614. $bk['updatetime']=date("Y-m-d H:i:s");
  1615. $upcgd =Db::name("order_bk")->save($bk);
  1616. if($upcgd==false){
  1617. Db::rollback();
  1618. return error_show(1004,"采购单数量更新失败");
  1619. }
  1620. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  1621. // $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  1622. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  1623. // $orderinfo = Db::name('sale')
  1624. // ->alias('a')
  1625. // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
  1626. // ->leftJoin('order_num b','b.orderCode=a.orderCode')
  1627. // ->where([
  1628. // 'a.is_del'=>0,
  1629. // 'b.cgdNo'=>$cgdinfo['cgdNo'],
  1630. // ])->findOrEmpty();
  1631. $is_stock = Db::name('good_basic')
  1632. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  1633. ->value('is_stock');
  1634. if($is_stock==1){
  1635. //库存品,推给31库管人员、41库管-张凯旋
  1636. // $uid = Db::name('user_role')
  1637. // ->where([
  1638. // ['is_del', '=', 0],
  1639. // ['roleid', 'in', [31, 41]],
  1640. // ['status', '=', 1]
  1641. // ])->column('uid');
  1642. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  1643. // $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)];
  1644. // $handle_user_list = implode(",",$uid);
  1645. }else{
  1646. // $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  1647. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  1648. // $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];//非库存品和采返商品,推给供应商负责人
  1649. }
  1650. }//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)];
  1651. $process=["order_code"=>$cgdinfo['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];//非库存品和采返商品,推给供应商负责人
  1652. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1653. Db::commit();
  1654. return app_show(0,"退货发货成功");
  1655. }else{
  1656. Db::rollback();
  1657. return error_show(1004,"退货发货失败");
  1658. }
  1659. }else{
  1660. return error_show(1004,"退货发货失败");
  1661. }
  1662. }catch (\Exception $e){
  1663. Db::rollback();
  1664. return error_show(1004,$e->getMessage());
  1665. }
  1666. }
  1667. public function returnList(){
  1668. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  1669. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  1670. $where = [['a.is_del',"=",0],['b.is_del',"=",0]];
  1671. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
  1672. if($cgdNo!=""){
  1673. $where[]=['a.cgdNo',"like", "%$cgdNo%"];
  1674. }
  1675. $returnCode= isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  1676. if($returnCode!=""){
  1677. $where[]=['a.returnCode',"like", "%$returnCode%"];
  1678. }
  1679. $good_type_code=isset($this->post['good_type_code']) && $this->post['good_type_code']!==""? trim($this->post['good_type_code']):"";
  1680. if($good_type_code !=""){
  1681. $where[]= ["b.spuCode",'like',"%$good_type_code%"];
  1682. }
  1683. $good_name=isset($this->post['good_name']) && $this->post['good_name']!==""? trim($this->post['good_name']):"";
  1684. if($good_name !=""){
  1685. $where[]= ["b.good_name",'like',"%$good_name%"];
  1686. }
  1687. $status=isset($this->post['status']) && $this->post['status']!==""? trim($this->post['status']):"";
  1688. if($status!=""){
  1689. $where[]= ["a.status",'=',$status];
  1690. }
  1691. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo']!="" ? trim($this->post['relaComNo']):"";
  1692. if($relaComNo!='') $where[]=['b.companyNo','=', $relaComNo];
  1693. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
  1694. if ($supplierNo != '') $where[] = ['b.supplierNo', '=', $supplierNo];
  1695. $order_source = isset($this->post['order_source']) && $this->post['order_source'] != "" ? intval($this->post['order_source']) : "";
  1696. if ($order_source != '') $where[] = ['b.order_source', '=', $order_source];
  1697. $order_type = isset($this->post['order_type']) && $this->post['order_type'] != "" ? intval($this->post['order_type']) : "";
  1698. if ($order_type !== '') $where[] = ['b.order_type', '=', $order_type];
  1699. // $role = $this->checkDataShare();
  1700. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ["a.apply_id", "in", $role[DataGroupModel::$type_全部]];
  1701. //只有level2的账号过滤数据权限
  1702. if ($this->level == 2) {
  1703. $role = $this->checkDataShare();
  1704. $hand = resign_hand_user($this->uid, 0);
  1705. if (!empty($role[DataGroupModel::$type_全部])) {
  1706. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  1707. $where[] = ['a.apply_id', 'in', $arr];
  1708. }
  1709. }
  1710. $count = Db::name('purchease_return')
  1711. ->alias('a')
  1712. ->leftJoin("purchease_order b","b.cgdNo=a.cgdNo")
  1713. ->leftJoin("good_basic c","c.spuCode=b.spuCode")
  1714. ->where($where)
  1715. ->count();
  1716. $total = ceil($count / $size);
  1717. $page = $page >= $total ? $total : $page;
  1718. $list = Db::name('purchease_return')
  1719. ->alias('a')
  1720. ->leftJoin("purchease_order b","b.cgdNo=a.cgdNo")
  1721. ->leftJoin("good_basic c","c.spuCode=b.spuCode")
  1722. ->where($where)
  1723. ->page($page,$size)
  1724. ->field("a.*,b.spuCode,b.good_price,b.order_type,b.good_price,b.total_fee,b.good_name,b.order_source,c.is_stock,b.supplierNo,b.supplier_name,b.companyNo,b.companyName")
  1725. ->order("a.addtime desc")
  1726. ->select()
  1727. ->toArray();
  1728. $data=[];
  1729. // $userCommon = \app\admin\common\User::getIns();
  1730. // $names = $userCommon->handle('getCodeAndName',['code'=>array_unique(array_column($list,'companyNo'))]);
  1731. //校验是否开通了供应商账号
  1732. $supp_account = checkHasAccountBySupplierNos(array_unique(array_column($list,'supplierNo')));
  1733. foreach ($list as $value){
  1734. if (in_array($value['order_type'], [3, 4])) {
  1735. $goon = Db::name("good_zixun")->field('cat_id')->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  1736. }else {
  1737. $goon =Db::name('good_basic')->field('cat_id')->where(['spuCode'=>$value['spuCode']])->find();
  1738. }
  1739. $value['can']= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  1740. $value['return_fee']= round($value['good_price']*$value['return_num'],2);
  1741. $value['has_account'] = (int)isset($supp_account[$value['supplierNo']]);
  1742. $data[]=$value;
  1743. }
  1744. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  1745. }
  1746. //库管验货 0待发货 1 待验货 2验货完成
  1747. public function checkOrder(){
  1748. $post =$this->request->only(["wsm_in_code"=>"","wsm_num"=>0],"post","trim");
  1749. $valide= Validate::rule([
  1750. "wsm_in_code|入库单号"=>"require|max:255",
  1751. "wsm_num|入库数量"=>"require|number"
  1752. ]);
  1753. if($valide->check($post)==false)return error_show(1004,$valide->getError());
  1754. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$post['wsm_in_code'],"is_del"=>0])->findOrEmpty();
  1755. if(empty($info)) return error_show(1004,"未找到入库单数据");
  1756. if($info['status']!=1) return error_show(1004,"入库单状态有误");
  1757. if($info['send_num'] <$post['wsm_num'])return error_show(1004,"入库数量不可大于发货单总数量");
  1758. $cgdinfo = Db::name("purchease_order")->where(["cgdNo"=>$info['cgdNo'],"is_del"=>0])->findOrEmpty();
  1759. if(empty($cgdinfo)) return error_show(1004,"未找到采购单信息");
  1760. if ($cgdinfo['order_type'] == 1) {
  1761. //库存品,只能由仓库管理员操作
  1762. $contactor = Db::name('warehouse_info')->where(['is_del' => 0, 'wsm_code' => $info['wsm_code']])->value('contactor', 0);
  1763. if ($this->uid != $contactor) return json_show(1004, '库存品只能由仓库管理员操作');
  1764. }
  1765. $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->findOrEmpty();
  1766. if(empty($good)) {
  1767. $good=[
  1768. "spuCode"=>$cgdinfo['spuCode'],
  1769. "wsm_code"=>$info['wsm_code'],
  1770. "usable_stock"=>0,
  1771. "wait_out_stock"=>0,
  1772. "wait_in_stock"=>$cgdinfo['wsend_num'],
  1773. "total_stock"=>0,
  1774. "addtime"=>date("Y-m-d H:i:s"),
  1775. "updatetime"=>date("Y-m-d H:i:s"),
  1776. ];
  1777. }
  1778. Db::startTrans();
  1779. try{
  1780. $upsend=[
  1781. "wsm_num"=>$post['wsm_num'],
  1782. "error_num"=>$info['send_num'] - $post['wsm_num'],
  1783. "checkid"=>$this->uid,
  1784. "check_name"=>$this->uname,
  1785. "status"=>2,
  1786. "updatetime"=>date("Y-m-d H:i:s")
  1787. ];
  1788. $up=Db::name("purchease_in")->where($info)->update($upsend);
  1789. if($up==false) throw new \Exception("入库失败");
  1790. $order = ["order_code"=>$post['wsm_in_code'],"status"=>2,"action_remark"=>'',"action_type"=>"status"];
  1791. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"RKD", $info['status'], $info);
  1792. $cgdstatus=$cgdinfo['status'];
  1793. $cgdinfo['wsend_num']-=$info['send_num'];
  1794. $cgdinfo['send_num']+=$info['send_num'];
  1795. $cgdinfo['th_num']+=$info['send_num'] - $post['wsm_num'];
  1796. $cgdinfo['th_fee']= round($cgdinfo['th_num']*$cgdinfo['good_price'],2);
  1797. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1798. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1799. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  1800. $upOrder=Db::name("purchease_order")->save($cgdinfo);
  1801. if($upOrder==false)throw new \Exception("入库失败");
  1802. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"status"];
  1803. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"RKD",$cgdstatus, $cgdinfo);
  1804. $good['wait_in_stock']-=$info['send_num'];
  1805. $good['usable_stock']+=$post['wsm_num'];
  1806. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  1807. $good['updatetime'] = date("Y-m-d H:i:s");
  1808. $upstock = Db::name("good_stock")->save($good);
  1809. if($upstock==false) throw new \Exception("库存更新失败");
  1810. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  1811. $buadd= GoodStockInfo::AddBn($stockid,$info['bnCode'],$post['wsm_num'],$cgdinfo['good_price']);
  1812. if($buadd==false) throw new \Exception("库存bn更新失败");
  1813. //商品表里的'可用总库存数'增加
  1814. $isGood= Db::name('good')->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])->findOrEmpty();
  1815. if(!empty($isGood)){
  1816. $upgood = Db::name('good')
  1817. ->data(['updatetime' => date('Y-m-d H:i:s')])
  1818. ->where(['is_del' => 0, 'spuCode' => $cgdinfo['spuCode']])
  1819. ->inc('usable_stock', $post['wsm_num'])
  1820. ->update();
  1821. if($upgood ==false) throw new \Exception("商品库存更新失败");
  1822. }
  1823. if($upsend['error_num']>0){
  1824. $returnCode=makeNo("CR");
  1825. $data=[
  1826. "returnCode"=>$returnCode,
  1827. "wsm_in_code"=>$post['wsm_in_code'],
  1828. "cgdNo"=>$cgdinfo['cgdNo'],
  1829. "wsm_num"=>$post['wsm_num'],
  1830. "reissue_num"=>0,
  1831. "return_num"=>$upsend['error_num'],
  1832. "return_contactor"=>'',
  1833. "return_addr"=>'',
  1834. "return_mobile"=>'',
  1835. "error_remark"=>'',
  1836. "post_code"=>"",
  1837. "post_company"=>"",
  1838. "remark"=>'' ,
  1839. "apply_id"=>$this->uid,
  1840. "apply_name"=>$this->uname,
  1841. "status"=>0,
  1842. "addtime"=>date("Y-m-d H:i:s"),
  1843. "updatetime"=>date("Y-m-d H:i:s")
  1844. ];
  1845. $res = Db::name("purchease_return")->insert($data);
  1846. if($res==false) throw new \Exception("退货单生成失败");
  1847. }
  1848. Db::commit();
  1849. return app_show(0,"验货入库完成");
  1850. }catch (\Exception $exception){
  1851. Db::rollback();
  1852. return error_show(1004,$exception->getMessage());
  1853. }
  1854. }
  1855. }