Purchin.php 119 KB

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