Purchin.php 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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. //采购单入库
  14. class Purchin extends Base
  15. {
  16. public function __construct(App $app)
  17. {
  18. parent::__construct($app);
  19. // $this->post=$this->request->post();
  20. }
  21. public function list(){
  22. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  23. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  24. $where =[['a.is_del',"=",0],['b.is_del',"=",0]];
  25. $wsm_in_code=isset($this->post['wsm_in_code']) && $this->post['wsm_in_code']!==""? trim($this->post['wsm_in_code']) :"";
  26. if($wsm_in_code!==""){
  27. $where[]=["a.wsm_in_code",'like',"%$wsm_in_code%"];
  28. }
  29. $cgdNo= isset($this->post['cgdNo']) && $this->post['cgdNo'] !==""? trim($this->post['cgdNo']) :"";
  30. if($cgdNo!==""){
  31. $where[]=['a.cgdNo','like',"%$cgdNo%"];
  32. }
  33. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? trim($this->post['apply_name']):"";
  34. if($apply_name!==""){
  35. $where[]=['a.apply_name','like',"%$apply_name%"];
  36. }
  37. $apply_id= isset($this->post['apply_id']) && $this->post['apply_id'] !==""? trim($this->post['apply_id']):"";
  38. if($apply_id!==""){
  39. $where[]=['a.apply_id','=',$apply_id];
  40. }
  41. $status= isset($this->post['status']) && $this->post['status'] !==""? trim($this->post['status']):"";
  42. if($status!==""){
  43. $where[]=['a.status','=',$status];
  44. }
  45. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  46. if ($start !=="") {
  47. $where[]= ["a.sendtime",'>=',$start];
  48. }
  49. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  50. if($end !==""){
  51. $where[]= ["a.sendtime",'<=',$end];
  52. }
  53. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : "";
  54. if ($order_type !== "") {
  55. $where[] = ["b.order_type", '=', $order_type];
  56. }
  57. $order_source = isset($this->post['order_source']) && $this->post['order_source'] !== "" ? intval($this->post['order_source']) : "";
  58. if ($order_source !== "") {
  59. $where[] = ["b.order_source", '=', $order_source];
  60. }
  61. $good_type_code=isset($this->post['good_type_code']) && $this->post['good_type_code']!==""? trim($this->post['good_type_code']):"";
  62. if($good_type_code !==""){
  63. $where[]= ["b.spuCode",'like',"%$good_type_code%"];
  64. }
  65. $good_name=isset($this->post['good_name']) && $this->post['good_name']!==""? trim($this->post['good_name']):"";
  66. if($good_name !==""){
  67. $where[]= ["b.good_name",'like',"%$good_name%"];
  68. }
  69. $supplie=isset($this->post['supplier']) && $this->post['supplier']!==""? trim($this->post['supplier']):"";
  70. if($supplie !==""){
  71. $where[]= ["b.supplierNo",'like',"%$supplie%"];
  72. }
  73. $wsm_code=isset($this->post['wsm_code']) && $this->post['wsm_code']!==""? trim($this->post['wsm_code']):"";
  74. if($wsm_code !==""){
  75. $where[]= ["a.wsm_code",'=',$wsm_code];
  76. }
  77. $supplierNo =isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']):"";
  78. if($supplierNo!=""){
  79. $supplier = Db::name("supplier")->where(["code"=>$supplierNo])->find();
  80. if(empty($supplier)){
  81. return error_show(1004,"未找到供应商信息");
  82. }
  83. $wsmcode = Db::name("warehouse_info")->where([
  84. "is_del"=>0,"supplierNo"=>$supplierNo])->column("wsm_code");
  85. $where[]=['a.wsm_code',"in",$wsmcode];
  86. }
  87. $condition=[];
  88. $role = $this->checkDataShare();
  89. $hand = resign_hand_user($this->uid,0);
  90. if (!empty($role[DataGroupModel::$type_全部])) {
  91. $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  92. $condition[]= ["sip.personid","in",$arr];
  93. $condition[]= ["a.apply_id","in",$hand];
  94. $condition[]= ["c.contactor","in",$hand];
  95. // $condition.="sip.personid in (".implode(",",
  96. // $role[DataGroupModel::$type_全部]).") or a.apply_id in {$hand} or c.contactor in {$hand}";
  97. }
  98. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  99. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  100. //$count = Db::name('purchease_in')->alias('a')->join("purchease_order b","b.cgdNo=a.cgdNo","left")
  101. $count = Db::name('purchease_in')
  102. ->alias('a')
  103. ->join("purchease_order b","b.cgdNo=a.cgdNo","left")
  104. ->leftJoin("warehouse_info c","a.wsm_code=c.wsm_code")
  105. ->leftJoin('supplier sip', 'sip.code=b.supplierNo')
  106. ->where($where)
  107. // ->whereOr($condition)
  108. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  109. ->count();
  110. $total = ceil($count/$size);
  111. $page = $page >= $total ? $total : $page;
  112. $list = Db::name('purchease_in')
  113. ->alias('a')
  114. ->join("purchease_order b","b.cgdNo=a.cgdNo","left")
  115. ->leftJoin('supplier sip', 'sip.code=b.supplierNo')
  116. ->leftJoin("warehouse_info c","a.wsm_code=c.wsm_code")
  117. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  118. ->where($where)
  119. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  120. ->page($page,$size)
  121. ->field("a.apply_name,a.sendtime,b.order_type,b.order_source,a.status,a.cgdNo,
  122. a.wsm_in_code,a.send_num,a.wsm_code,b.good_name,b.spuCode,a.post_company,a.post_code,
  123. a.post_fee,a.apply_id,a.wsm_num,a.error_num,a.wait_num,a.status,a.check_name,a.checkid,
  124. b.bkcode,b.supplierNo,u.itemid,sip.personid as cgder_id,sip.person as cgder,b.good_createrid,b.good_creater")
  125. ->order("a.addtime desc")
  126. ->cursor();
  127. $data=[];
  128. foreach ($list as $value){
  129. //获取规格、品牌信息
  130. if ($value['order_type'] == 3) {
  131. $temp = Db::name('good_zixun')
  132. ->alias('gz')
  133. ->field('gz.specinfo,b.brand_name')
  134. ->join('brand b', 'b.id=gz.brand_id')
  135. ->where(['gz.spuCode' => $value['spuCode'], 'gz.is_del' => 0])
  136. ->find();
  137. $value["brand_name"] = $temp['brand_name'];
  138. $value["speclist"] = json_decode($temp['specinfo'], true);
  139. } else {
  140. $value['brand_name'] = Db::name('good_basic')
  141. ->alias('gb')
  142. ->join('brand b', 'b.id=gb.brand_id')
  143. ->where(['gb.spuCode' => $value['spuCode']])
  144. ->value('b.brand_name');
  145. $spec = Db::name("good_spec")
  146. ->where(["spuCode" => $value['spuCode'], "is_del" => 0])
  147. ->select()
  148. ->toArray();
  149. $speclist = [];
  150. if (!empty($spec)) {
  151. foreach ($spec as $val) {
  152. $temp = [];
  153. $temp['id'] = $val['id'];
  154. $temp['spuCode'] = $val['spuCode'];
  155. $temp['spec_id'] = $val['spec_id'];
  156. $temp['spec_value_id'] = $val['spec_value_id'];
  157. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  158. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  159. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  160. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  161. $speclist[] = $temp;
  162. }
  163. }
  164. $value["speclist"] = empty($speclist) ? [] : $speclist;
  165. }
  166. $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c","k.supplierNo=c.code")->where(["k.wsm_code"=>$value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  167. $value['wsm_name'] = $wsmcode['wsm_name'];
  168. $value['wsm_supplier'] = $wsmcode['name'];
  169. $value['wsm_supplierNo'] = $wsmcode['code'];
  170. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  171. //是否具有编辑权限
  172. $value['is_allow_update'] = 0;
  173. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  174. $data[]=$value;
  175. }
  176. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  177. }
  178. public function add(){
  179. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!=""? trim($this->post['cgdNo']):"";
  180. if($cgdNo==""){
  181. return error_show(1005,"参数cgdNo 不能为空");
  182. }
  183. $cgdinfo = Db::name("purchease_order")->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
  184. if(empty($cgdinfo)){
  185. return error_show(1005,"未找到采购单数据");
  186. }
  187. if($cgdinfo['status']==0){
  188. return error_show(1005,"采购单未确认");
  189. }
  190. $isreturn = cgd_sale_return($cgdNo);
  191. if($isreturn){
  192. return error_show(1005, "关联销售单存在退货单正在进行");
  193. }
  194. $wsm_in_code = makeNo("CF");
  195. $bn_code = makeNo("BN");
  196. $wsminfo = Db::name("warehouse_info")->where(["wsm_code"=>$cgdinfo['wsm_code'],"is_del"=>0])->find();
  197. if(empty($wsminfo)){
  198. return error_show(1004,"未找到仓库信息");
  199. }
  200. $recep = Db::name('warehouse_addr')->where(["wsm_code"=>$cgdinfo['wsm_code'],"is_del"=>0])->find();
  201. $sendtype = isset($this->post['sendtype'])&&$this->post['sendtype']!="" ? intval($this->post['sendtype']):"";
  202. if($sendtype==""){
  203. return error_show(1004,"参数 sendtype 不能为空");
  204. }
  205. // $data = GetUserInfo($this->post['token']);
  206. // if((!empty($data) && $data['code']!=0) ||empty($data) ){
  207. // return error_show($data['code'],$data['message']);
  208. // }
  209. // $userinfo = $data['data'];
  210. $post_company = isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  211. $post_code = isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  212. $post_fee = isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? $this->post['post_fee']:"0";
  213. $post_time = isset($this->post['post_time'])&&$this->post['post_time']!="" ? $this->post['post_time']:date("Y-m-d H:i:s");
  214. $send_num = isset($this->post['send_num'])&&$this->post['send_num']!="" ? intval($this->post['send_num']):"";
  215. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"1 ";
  216. if($send_num==''){
  217. return error_show(1004,"参数 send_num 不能为空或零");
  218. }
  219. $wsmin_num=Db::name("purchease_in")->where([["cgdNo","=",$cgdNo],["is_del","=",0],["status","not in",[4,6]]])
  220. ->sum("send_num");
  221. if($send_num>$cgdinfo['wsend_num']-$wsmin_num){
  222. return error_show(1009,"采购单剩余未发货数量不足");
  223. }
  224. if($sendtype==2 || $cgdinfo['order_type']!=1){
  225. $status=4;
  226. }
  227. $orin = [
  228. "wsm_in_code"=>$wsm_in_code,
  229. "cgdNo"=>$cgdNo,
  230. "bnCode"=>$bn_code,
  231. "wsm_code"=>$cgdinfo['wsm_code'],
  232. "wsm_reaper"=>isset($recep)&& $recep['wsm_name']!=''? $recep['wsm_name']:"",
  233. "send_num"=>$send_num,
  234. "post_company"=>$post_company,
  235. "post_code"=>$post_code,
  236. "post_fee"=>$post_fee,
  237. "sendtime"=>$post_time,
  238. "apply_id"=>$this->uid,
  239. "apply_name"=>$this->uname,
  240. "wait_num"=>0,
  241. "wsm_num"=>0,
  242. "sendtype"=>$sendtype,
  243. "status"=>$status,
  244. "addtime"=>date("Y-m-d H:i:s"),
  245. "updatetime"=>date("Y-m-d H:i:s")
  246. ];
  247. Db::startTrans();
  248. try{
  249. $win = Db::name("purchease_in")->insert($orin,true);
  250. if($win){
  251. $sto = ["order_code"=>$wsm_in_code,"status"=>$status,"action_remark"=>'',"action_type"=>"edit"];
  252. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$sto,"RKD",$status,$this->post);
  253. if($status==1){
  254. $uid = Db::name('user_role')
  255. ->where([
  256. ['is_del', '=', 0],
  257. ['roleid', 'in', [31, 41]],
  258. ['status', '=', 1]
  259. ])->column('uid');
  260. $handle_user_list = implode(',', $uid);
  261. }
  262. $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??""];
  263. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  264. $order = ["order_code"=>$cgdNo,"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  265. //维护台账信息
  266. 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}'");
  267. $is_stock = Db::name('good_basic')
  268. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  269. ->value('is_stock');
  270. // if($is_stock == 1){
  271. // //库存品,只能由库管操作
  272. // $own_roleid = Db::name('user_role')->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])->value('roleid');
  273. // if (!in_array($own_roleid, config('app.wsm_cgder_role'))) throw new Exception('只能由库管人员操作');
  274. // }else{
  275. //非库存品,只能由供应商负责人操作(开通账号的不能操作)
  276. //判断该供应商是否开通账号
  277. //好像status==4里面的判断没用了,但是没时间修改
  278. if($is_stock==0){
  279. //全都只能由供应商负责人操作(开通账号的不能操作)
  280. $res = SupplierRelationUserModel::field('id')
  281. ->where(['is_del' => SupplierUserModel::$is_del_normal, 'supplierNo' => $cgdinfo['supplierNo'], 'status' => SupplierUserModel::$status_normal])
  282. ->findOrEmpty()
  283. ->isEmpty();
  284. if (!$res) throw new Exception('供应商已开通账号,只能由供应商端操作');
  285. }
  286. $personid = Db::name('supplier')
  287. ->where('code', $cgdinfo['supplierNo'])
  288. ->value('personid');
  289. if ($this->uid != $personid) throw new Exception('只能由供应商负责人操作');
  290. // }
  291. if($status==1){
  292. Db::commit();
  293. return app_show(0,'新建成功',["wsm_in_code"=>$wsm_in_code]);
  294. }
  295. if($status==4){
  296. $cgdinfo['wsend_num']-=$send_num;
  297. $cgdinfo['send_num']+=$send_num;
  298. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  299. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  300. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  301. $up=Db::name("purchease_order")->save($cgdinfo);
  302. if($up==false){
  303. Db::rollback();
  304. return error_show(1004,"新建失败");
  305. }
  306. $good = Db::name("good_stock")->where(["wsm_code"=>$cgdinfo['wsm_code'],"spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->lock(true)->find();
  307. if(empty($good)) {
  308. $good=[
  309. "spuCode"=>$cgdinfo['spuCode'],
  310. "wsm_code"=>$cgdinfo['wsm_code'],
  311. "usable_stock"=>0,
  312. "wait_out_stock"=>0,
  313. "wait_in_stock"=>$send_num,
  314. "total_stock"=>0,
  315. "addtime"=>date("Y-m-d H:i:s"),
  316. "updatetime"=>date("Y-m-d H:i:s"),
  317. ];
  318. }
  319. $good['wait_in_stock']-=$send_num;
  320. $good['usable_stock']+=$send_num;
  321. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  322. $good['updatetime'] = date("Y-m-d H:i:s");
  323. $upd= Db::name("good_stock")->save($good);
  324. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  325. //商品变动日志表,good_log_code字段存储采购单单号
  326. $good_data=[];
  327. $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"usable_stock"];
  328. $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>2,'stock'=>$send_num,"stock_name"=>"wait_in_stock"];
  329. if($upd){
  330. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname],$good_data,'RKD');
  331. //todo:: 新增bn编号库存数据修改
  332. if($cgdinfo['order_type']==1){
  333. $bnin=GoodStockInfo::AddBn($stockid,$bn_code,$send_num,$cgdinfo['good_price']);
  334. if($bnin==false){
  335. Db::rollback();
  336. return error_show(1004,"库存bn数据新增失败");
  337. }
  338. $bk =Db::name("order_bk")->where(['cgdNo'=>$cgdinfo['cgdNo']])->find();
  339. if($bk==false){
  340. $bk=[
  341. "cgdNo"=>$cgdinfo['cgdNo'],
  342. "spuCode"=>$cgdinfo['spuCode'],
  343. "companyNo"=>$cgdinfo['companyNo'],
  344. "total_num"=>$send_num,
  345. "merge_num"=>0,
  346. "balance_num"=>$send_num,
  347. "status"=>1,
  348. "is_del"=>0,
  349. 'addtime'=>date("Y-m-d H:i:s"),
  350. "updatetime"=>date("Y-m-d H:i:s")
  351. ];
  352. $upcgd =Db::name("order_bk")->save($bk);
  353. }else{
  354. $where=['id'=>$bk['id'],'total_num'=>$bk['total_num'],'balance_num'=>$bk['balance_num']];
  355. $bk['balance_num']+=$send_num;
  356. $bk['total_num']+=$send_num;
  357. $bk['updatetime']=date("Y-m-d H:i:s");
  358. $upcgd =Db::name("order_bk")->where($where)->update($bk);
  359. }
  360. if($upcgd==false){
  361. Db::rollback();
  362. return error_show(1004,"采购单数量更新失败");
  363. }
  364. }
  365. if($cgdinfo['send_status']==3 && $cgdinfo['order_type']!=1){
  366. $send=Db::name("order_send")->where(["cgdNo"=>$cgdNo,"status"=>1])->column("outCode");
  367. if(!empty($send)){
  368. $out =Db::name("order_out")->where(["outCode"=>$send,"status"=>0,"is_del"=>0])->select()
  369. ->toArray();
  370. if(!empty($out)){
  371. $up =Db::name("order_out")->where(["outCode"=>$send,"status"=>0,"is_del"=>0])->update(["status"=>1,'wsm_code'=>$cgdinfo['wsm_code'],"updatetime"=>date('Y-m-d H:i:s')]);
  372. if($up==false){
  373. Db::rollback();
  374. return error_show(1004,"出库单出库失败");
  375. }else{
  376. //查询是否有非库存品订单
  377. // $is_stock = Db::name('sale')
  378. // ->where([
  379. // ['is_del','=',0],
  380. // ['orderCode','in',array_column($out,'orderCode')]
  381. // ])->column('is_stock','orderCode');
  382. $supplier = Db::name('supplier')
  383. ->field('person,personid')
  384. ->where(['code'=>$cgdinfo['supplierNo'],'is_del'=>0])
  385. ->findOrEmpty();
  386. foreach ($out as $k => $v_outCode) {
  387. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  388. "order_code" => $v_outCode['outCode'],//出库单号
  389. "status" => 0,//这里的status是之前的值
  390. "action_remark" => '',//备注
  391. "action_type" => "status"//新建create,编辑edit,更改状态status
  392. ], "CKD", 1, $this->post);
  393. if ($is_stock == 1) {
  394. //库存品,推给库管和库管-张凯旋
  395. $roleid = config('app.wsm_cgder_role');
  396. $uids = Db::name('user_role')
  397. ->where('is_del', 0)
  398. ->whereIn('roleid', $roleid)
  399. ->column('uid');
  400. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  401. "order_type" => 'CKD',
  402. "order_code" => $v_outCode['outCode'],//出库单号
  403. "order_id" => $v_outCode['id'],
  404. "order_status" => 1,
  405. "before_status" => 0,
  406. // 'wait_id' => $supplier['personid'],
  407. // 'wait_name' => $supplier['person'],
  408. // 'holder_id' => $supplier['personid'],
  409. 'handle_user_list' => implode(',', $uids)
  410. ]);
  411. } else {
  412. //非库存品和采返商品,推给供应商负责人
  413. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  414. "order_type" => 'CKD',
  415. "order_code" => $v_outCode['outCode'],//出库单号
  416. "order_id" => $v_outCode['id'],
  417. "order_status" => 1,
  418. "before_status" => 0,
  419. 'wait_id' => $supplier['personid'],
  420. 'wait_name' => $supplier['person'],
  421. 'holder_id' => $supplier['personid'],
  422. ]);
  423. }
  424. }
  425. }
  426. $good1 = Db::name("good_stock")->where(["wsm_code"=>$cgdinfo['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find();
  427. if($good1==false){
  428. Db::rollback();
  429. return error_show(1004,"未找到对应商品库存");
  430. }
  431. $out_num = intval(array_sum(array_column($out,"send_num")));
  432. if($out_num>$good1['usable_stock']){
  433. Db::rollback();
  434. return error_show(1004,"库存数量不足发货");
  435. }
  436. $good1['usable_stock']-=$out_num;
  437. $good1['wait_out_stock']+=$out_num;
  438. $good1['total_stock']=$good1['usable_stock']+$good1['wait_out_stock'];
  439. $good1['updatetime'] = date("Y-m-d H:i:s");
  440. $upd= Db::name("good_stock")->save($good1);
  441. if($upd==false){
  442. Db::rollback();
  443. return error_show(1004,"库存数量更新失败");
  444. }
  445. $stockid= isset($good['id'])?$good['id']:Db::name("good_stock")->getLastInsID();
  446. $good_data=[];
  447. $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"wait_out_stock"];
  448. $good_data[]=['good_log_code'=>$wsm_in_code,"stock_id"=>$stockid,"type"=>1,'stock'=>$send_num,"stock_name"=>"usable_stock"];
  449. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname],$good_data,'RKD');
  450. $sendp=Db::name("order_send")->where(["cgdNo"=>$cgdNo,"outCode"=>$send,"status"=>1])->save (["status"=>2]);
  451. if($sendp==false){
  452. Db::rollback();
  453. return error_show(1004,"库存发货失败");
  454. }
  455. }
  456. }
  457. }
  458. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  459. $personid = Db::name('supplier')
  460. ->where('is_del',0)
  461. ->where('code',$cgdinfo['supplierNo'])
  462. ->value('personid');
  463. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  464. // $is_stock = Db::name('good_basic')
  465. // ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  466. // ->value('is_stock');
  467. // if($is_stock==1){
  468. // //库存品,推给31库管人员、41库管-张凯旋
  469. // $uid = Db::name('user_role')
  470. // ->where([
  471. // ['is_del', '=', 0],
  472. // ['roleid', 'in', [31, 41]],
  473. // ['status', '=', 1]
  474. // ])->column('uid');
  475. // if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  476. //// $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)];
  477. //
  478. // }else{
  479. if($this->uid != $personid)throw new Exception('只能由供应商负责人操作');
  480. // }
  481. }//else $process = ["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $personid];
  482. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],["order_code" => $cgdNo, "order_id" => $cgdinfo['id'], "order_status" => $cgdinfo['status'], "order_type" => 'CGD', "before_status" => $order['status'], 'holder_id' => $personid]);
  483. Db::commit();
  484. return app_show(0,'商品入库成功',["wsm_in_code"=>$wsm_in_code]);
  485. }else{
  486. Db::rollback();
  487. return error_show(1002,"商品入库失败");
  488. }
  489. }
  490. Db::commit();
  491. return app_show(0,'新建成功',["wsm_in_code"=>$wsm_in_code]);
  492. }
  493. Db::rollback();
  494. return error_show(1004,"新建失败");
  495. }catch (\Exception $e){
  496. Db::rollback();
  497. return error_show(1004, $e->getMessage());
  498. }
  499. }
  500. public function info(){
  501. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  502. if($incode==""){
  503. return error_show(1004,"参数wsm_in_code 不为空");
  504. }
  505. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  506. if(empty($info)){
  507. return error_show(1004,"未找到数据");
  508. }
  509. $dom = Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],'is_del'=>0])->find();
  510. if($dom['order_type']==3){
  511. $goon = Db::name("good_zixun")->where(["spuCode"=>$dom['spuCode'],"is_del"=>0])->find();
  512. }else {
  513. $goon =Db::name('good_basic')->where(['spuCode'=>$dom['spuCode']])->find();
  514. }
  515. if(empty($goon)){
  516. return error_show(1002,"未找到商品数据");
  517. }
  518. $cat= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  519. $info['cgder']=Db::name("supplier")->where(["code"=>$dom['supplierNo']])->value("person",'');
  520. $info['spuCode']=$dom['spuCode'];
  521. $info['good_name']=$dom['good_name'];
  522. $info['skuCode']=$dom['skuCode'];
  523. $info['good_num']=$dom['good_num'];
  524. $info['good_price']=$dom['good_price'];
  525. $info['total_fee']=$dom['total_fee'];
  526. $info['diff_fee']=$dom['diff_fee'];
  527. $info['diff_weight']=$dom['diff_weight'];
  528. $info['gold_price']=$dom['gold_price'];
  529. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  530. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  531. ->where(["a.wsm_code"=>$info['wsm_code']])->field("a.name as wsm_name,b.name,b.code,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code")->find();
  532. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  533. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  534. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  535. $info['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  536. $info['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  537. $info['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  538. $info['wsm_addr'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  539. $info['wsm_addr'].=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  540. $check = Db::name("purchease_info")->alias("a")->leftJoin("result_info b","a.error_code=b.result_code")->where
  541. (['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();
  542. $info['checkinfo']= $check;
  543. $return = Db::name("purchease_return")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  544. $return['return_contactor']=isset($return['return_contactor'])&&$return['return_contactor']!="" ?$return['return_contactor']:'';
  545. $return['return_addr']=isset($return['return_addr'])&&$return['return_addr']!="" ?$return['return_addr']:'';
  546. $return['return_mobile']=isset($return['return_mobile'])&&$return['return_mobile']!="" ?$return['return_mobile']:'';
  547. //以下字段临时补充到return中
  548. $return['wsm_in_code'] = $incode;
  549. $return['wsm_num'] = isset($return['wsm_num']) ? $return['wsm_num'] : 0;
  550. $return['reissue_num'] = isset($return['reissue_num']) ? $return['reissue_num'] : 0;
  551. $return['return_num'] = isset($return['return_num']) ? $return['return_num'] : 0;
  552. $return['contactor'] = $return['return_contactor'];
  553. $return['addr'] = $return['return_addr'];
  554. $return['mobile'] = $return['return_mobile'];
  555. $return['error_remark'] = isset($return['error_remark']) ? $return['error_remark'] : '';
  556. $return['remark'] = isset($return['remark']) ? $return['remark'] : '';
  557. $info['return']= $return;
  558. $info['can']=$cat;
  559. if(isset($dom['companyNo'])&&$dom['companyNo']!=""){
  560. $company = Db::name("business")->where(["companyNo"=>$dom['companyNo']])->find();
  561. }
  562. $info['company'] = isset($company['company'])?$company['company']:"";
  563. $info['companyNo'] = isset($dom['companyNo'])?$dom['companyNo']:"";
  564. $info['order_type'] = isset($dom['order_type'])?$dom['order_type']:"";
  565. return app_show(0,'获取成功',$info);
  566. }
  567. public function check(){
  568. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  569. if($incode==""){
  570. return error_show(1004,"参数wsm_in_code 不为空");
  571. }
  572. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  573. if(empty($info)){
  574. return error_show(1004,"未找到数据");
  575. }
  576. if($info['status']!=1){
  577. return error_show(1004,"发货单状态有误");
  578. }
  579. $order_num = isset($this->post['order_num']) && $this->post['order_num']!==''?intval($this->post['order_num'])
  580. :"";
  581. if($order_num===""){
  582. return error_show(1004,"参数 order_num 不为空");
  583. }
  584. $error_list = isset($this->post['error_list']) && $this->post['error_list']!==''?$this->post['error_list']:"";
  585. $result = GetUserInfo($this->post['token']);
  586. if((!empty($result) && $result['code']!=0) ||empty($result) ){
  587. return error_show($result['code'],$result['message']);
  588. }
  589. $userinfo = $result['data'];
  590. Db::startTrans();
  591. try{
  592. $order = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"edit"];
  593. $info['wsm_num'] = $order_num;
  594. $info['wait_num'] = $info['send_num'];
  595. $info['checkid'] = $userinfo['id'];
  596. $info['check_name'] = $userinfo['nickname'];
  597. $info['status'] =2;
  598. $info['error_num'] = $info['send_num']-$order_num;
  599. $info['updatetime'] = date("Y-m-d H:i:s");
  600. $up =Db::name("purchease_in")->save($info);
  601. if($up){
  602. ActionLog::logAdd($this->post['token'],$order,"RKD",2,$this->post);
  603. $uid = Db::name('user_role')
  604. ->where([
  605. ['is_del', '=', 0],
  606. ['roleid', 'in', [31, 41]],
  607. ['status', '=', 1]
  608. ])->column('uid');
  609. $handle_user_list = implode(',', $uid);
  610. $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];
  611. ProcessOrder::AddProcess($this->post['token'],$process);
  612. if(!empty($error_list)){
  613. $data=[];
  614. foreach ($error_list as $value ){
  615. $temp=[];
  616. $temp['wsm_in_code']=$info['wsm_in_code'];
  617. $temp['num']=$value['num'];
  618. $temp['error_code']=$value['error_code'];
  619. $temp['error_img']=$value['error_img'];
  620. $temp['remark']=$value['remark'];
  621. $temp['is_del']=0;
  622. $temp['addtime']=date("Y-m-d H:i:s");
  623. $temp['updatetime']=date("Y-m-d H:i:s");
  624. $data[]=$temp;
  625. }
  626. $derr= Db::name("purchease_info")->insertAll($data);
  627. if($derr){
  628. Db::commit();
  629. return app_show(0,"验货完成");
  630. }else{
  631. Db::rollback();
  632. return error_show(1002,"验货数据保存失败");
  633. }
  634. }
  635. Db::commit();
  636. return app_show(0,"验货完成");
  637. }
  638. Db::rollback();
  639. return error_show(1002,"验货数据保存失败");
  640. }catch (\Exception $e){
  641. Db::rollback();
  642. return error_show(1002,$e->getMessage());
  643. }
  644. }
  645. public function status(){
  646. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  647. if($incode==""){
  648. return error_show(1004,"参数wsm_in_code 不为空");
  649. }
  650. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  651. if(empty($info)){
  652. return error_show(1004,"未找到数据");
  653. }
  654. $status = isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  655. if($status===""){
  656. return error_show(1004,"参数status 不为空");
  657. }
  658. if($info['status']==4){
  659. return error_show(1004,"入库单已完成");
  660. }
  661. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']) :"";
  662. Db::startTrans();
  663. try{
  664. $order = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"status"];
  665. $info['status'] = $status;
  666. $info['remark'] = $remark;
  667. $info['updatetime'] = date("Y-m-d H:i:s");
  668. $update = Db::name("purchease_in")->save($info);
  669. if($update){
  670. ActionLog::logAdd($this->post['token'],$order,"RKD",$status,$this->post);
  671. $uid = Db::name('user_role')
  672. ->where([
  673. ['is_del', '=', 0],
  674. ['roleid', 'in', [31, 41]],
  675. ['status', '=', 1]
  676. ])->column('uid');
  677. $handle_user_list = implode(',', $uid);
  678. $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];
  679. ProcessOrder::AddProcess($this->post['token'],$process);
  680. if($status==0){
  681. $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find();
  682. if(empty($cgdinfo)){
  683. Db::rollback();
  684. return error_show(1002,"未找到采购单数据");
  685. }
  686. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  687. $cgdinfo['wsend_num']+=$info['send_num'];
  688. $cgdinfo['send_num']-=$info['send_num'];
  689. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  690. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  691. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  692. $up=Db::name("purchease_order")->save($cgdinfo);
  693. if($up){
  694. ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  695. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  696. // $orderinfo = Db::name('sale')
  697. // ->alias('a')
  698. // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
  699. // ->leftJoin('order_num b','b.orderCode=a.orderCode')
  700. // ->where([
  701. // 'a.is_del'=>0,
  702. // 'b.cgdNo'=>$cgdinfo['cgdNo'],
  703. // ])->findOrEmpty();
  704. $is_stock = Db::name('good_basic')
  705. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  706. ->value('is_stock');
  707. $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  708. if($is_stock==1){
  709. //库存品,推给31库管人员、41库管-张凯旋
  710. $uid = Db::name('user_role')
  711. ->where([
  712. ['is_del', '=', 0],
  713. ['roleid', 'in', [31, 41]],
  714. ['status', '=', 1]
  715. ])->column('uid');
  716. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  717. $handle_user_list=implode(",",$uid);
  718. // $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)];
  719. }else{
  720. //$holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  721. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  722. //$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];
  723. }//非库存品和采返商品,推给供应商负责人
  724. }//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)];
  725. $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];
  726. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  727. Db::commit();
  728. return app_show(0,'取消成功');
  729. }else{
  730. Db::rollback();
  731. return error_show(1002,"取消失败");
  732. }
  733. }
  734. if($status==1){
  735. $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find();
  736. if(empty($cgdinfo)){
  737. Db::rollback();
  738. return error_show(1002,"未找到采购单数据");
  739. }
  740. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  741. $cgdinfo['wsend_num']-=$info['send_num'];
  742. $cgdinfo['send_num']+=$info['send_num'];
  743. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  744. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  745. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  746. $up=Db::name("purchease_order")->save($cgdinfo);
  747. if($up){
  748. ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  749. $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  750. if($cgdinfo['status'] == 1 || $cgdinfo['status']==2){
  751. // $orderinfo = Db::name('sale')
  752. // ->alias('a')
  753. // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
  754. // ->leftJoin('order_num b','b.orderCode=a.orderCode')
  755. // ->where([
  756. // 'a.is_del'=>0,
  757. // 'b.cgdNo'=>$cgdinfo['cgdNo'],
  758. // ])->findOrEmpty();
  759. $is_stock = Db::name('good_basic')
  760. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  761. ->value('is_stock');
  762. if($is_stock==1){
  763. //库存品,推给31库管人员、41库管-张凯旋
  764. $uid = Db::name('user_role')
  765. ->where([
  766. ['is_del', '=', 0],
  767. ['roleid', 'in', [31, 41]],
  768. ['status', '=', 1]
  769. ])->column('uid');
  770. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  771. $handle_user_list = implode(",",$uid);
  772. // $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)];
  773. }elseif ($is_stock==0 || $cgdinfo['order_type']==3) {
  774. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  775. // $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];
  776. }//非库存品和采返商品,推给供应商负责人
  777. }//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)];
  778. $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];ProcessOrder::AddProcess($this->post['token'],$process);
  779. Db::commit();
  780. return app_show(0,'发货成功');
  781. }else{
  782. Db::rollback();
  783. return error_show(1002,"发货失败");
  784. }
  785. }
  786. if($status==4){
  787. $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find();
  788. if(empty($cgdinfo)){
  789. Db::rollback();
  790. return error_show(1002,"未找到采购单数据");
  791. }
  792. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  793. $cgdinfo['wsend_num']-=$info['send_num'];
  794. $cgdinfo['send_num']+=$info['send_num'];
  795. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  796. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  797. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  798. $up=Db::name("purchease_order")->save($cgdinfo);
  799. if($up==false){
  800. Db::rollback();
  801. return error_show(1002,"发货失败");
  802. }
  803. ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  804. $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  805. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  806. // $orderinfo = Db::name('sale')
  807. // ->alias('a')
  808. // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
  809. // ->leftJoin('order_num b','b.orderCode=a.orderCode')
  810. // ->where([
  811. // 'a.is_del'=>0,
  812. // 'b.cgdNo'=>$cgdinfo['cgdNo'],
  813. // ])->findOrEmpty();
  814. $is_stock = Db::name('good_basic')
  815. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  816. ->value('is_stock');
  817. if($is_stock==1){
  818. //库存品,推给31库管人员、41库管-张凯旋
  819. $uid = Db::name('user_role')
  820. ->where([
  821. ['is_del', '=', 0],
  822. ['roleid', 'in', [31, 41]],
  823. ['status', '=', 1]
  824. ])->column('uid');
  825. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  826. // $holder_id = implode(",",$uid);
  827. // $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)];
  828. }else{
  829. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  830. // $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];
  831. }//非库存品和采返商品,推给供应商负责人
  832. }//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)];
  833. $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];
  834. ProcessOrder::AddProcess($this->post['token'],$process);
  835. $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->findOrEmpty();
  836. if(empty($good)) {
  837. $good=[
  838. "spuCode"=>$cgdinfo['spuCode'],
  839. "wsm_code"=>$info['wsm_code'],
  840. "usable_stock"=>0,
  841. "wait_out_stock"=>0,
  842. "wait_in_stock"=>$info['wsm_num'],
  843. "total_stock"=>0,
  844. "addtime"=>date("Y-m-d H:i:s"),
  845. "updatetime"=>date("Y-m-d H:i:s"),
  846. ];
  847. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  848. "action_type"=>"create"];
  849. }else{
  850. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  851. }
  852. $good['wait_in_stock']-=$info['wsm_num'];
  853. $good['usable_stock']+=$info['wsm_num'];
  854. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  855. $good['updatetime'] = date("Y-m-d H:i:s");
  856. $upd= Db::name("good_stock")->save($good);
  857. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  858. //商品变动日志表,good_log_code字段存储入库单编号
  859. $good_data=[];
  860. $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"];
  861. $good_data[] = ['good_log_code' => $incode, "stock_id" => isset($good['id']) ? $good['id'] :
  862. Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $info['wsm_num'], "stock_name" => "wait_in_stock"];
  863. if($upd){
  864. $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$info['wsm_num'],$cgdinfo['good_price']);
  865. if($bnin==false){
  866. Db::rollback();
  867. return error_show(1004,"库存bn数据新增失败");
  868. }
  869. GoodLog::LogAdd($this->post['token'],$good_data,'RKD');
  870. Db::commit();
  871. return app_show(0,'商品入库成功');
  872. }else{
  873. Db::rollback();
  874. return error_show(1002,"商品入库失败");
  875. }
  876. }
  877. Db::commit();
  878. return app_show(0,"状态修改完成");
  879. }
  880. Db::rollback();
  881. return error_show(1002,"状态修改失败");
  882. }catch (\Exception $e){
  883. Db::rollback();
  884. return error_show(1002,$e->getMessage());
  885. }
  886. }
  887. /**
  888. * @return \think\response\Json|void
  889. * @throws \think\db\exception\DataNotFoundException
  890. * @throws \think\db\exception\DbException
  891. * @throws \think\db\exception\ModelNotFoundException
  892. */
  893. public function ReturnAdd(){
  894. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  895. if($incode==""){
  896. return error_show(1004,"参数wsm_in_code 不为空");
  897. }
  898. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  899. if(empty($info)){
  900. return error_show(1004,"未找到数据");
  901. }
  902. //正常入库数量
  903. $wsm_num = isset($this->post['wsm_num']) &&$this->post['wsm_num']!=="" ? intval($this->post['wsm_num']):"" ;
  904. if($wsm_num===""){
  905. return error_show(1004,"参数wsm_num 不为空");
  906. }
  907. //补货数量
  908. $reissue_num = isset($this->post['reissue_num']) &&$this->post['reissue_num']!=="" ? intval($this->post['reissue_num']):"0" ;
  909. //退货数量
  910. $return_num = isset($this->post['return_num']) &&$this->post['return_num']!=="" ? intval($this->post['return_num']):"0" ;
  911. $contactor = isset($this->post['contactor']) &&$this->post['contactor']!=="" ? trim($this->post['contactor']):"" ;
  912. if($contactor==""){
  913. return error_show(1004,"参数contactor 不为空");
  914. }
  915. $addr = isset($this->post['addr']) &&$this->post['addr']!=="" ? trim($this->post['addr']):"" ;
  916. if($addr==""){
  917. return error_show(1004,"参数addr 不为空");
  918. }
  919. $mobile = isset($this->post['mobile']) &&$this->post['mobile']!=="" ? trim($this->post['mobile']):"" ;
  920. if($mobile==""){
  921. return error_show(1004,"参数mobile 不为空");
  922. }
  923. $error_remark = isset($this->post['error_remark']) &&$this->post['error_remark']!=="" ? trim($this->post['error_remark']):"" ;
  924. $remark = isset($this->post['remark']) &&$this->post['remark']!=="" ? trim($this->post['remark']):"" ;
  925. $result = GetUserInfo($this->post['token']);
  926. if((!empty($result) && $result['code']!=0) ||empty($result) ){
  927. return error_show($result['code'],$result['message']);
  928. }
  929. $userinfo = $result['data'];
  930. Db::startTrans();
  931. try{
  932. $sto = ["order_code"=>$incode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"edit"];
  933. // $info['status'] =$info['wsm_num']==$wsm_num ? 4 :5;
  934. $info['status'] = $info['send_num'] == $wsm_num ? 4 : 5;//艳蓉说,这里不能用入库数量比较,应该是用入库数量,即总数来比较
  935. $info['is_th'] =$return_num>0 ?1:0;
  936. $info['wsm_num']= $wsm_num;
  937. $info['updatetime'] = date("Y-m-d H:i:s");
  938. $up= Db::name("purchease_in")->save($info);
  939. if($up){
  940. $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find();
  941. if(empty($cgdinfo)){
  942. Db::rollback();
  943. return error_show(1002,"未找到采购单数据");
  944. }
  945. //库存品,推给31库管人员、41库管-张凯旋
  946. $uid = Db::name('user_role')
  947. ->where([
  948. ['is_del', '=', 0],
  949. ['roleid', 'in', [31, 41]],
  950. ['status', '=', 1]
  951. ])->column('uid');
  952. $handle_user_list=implode(",",$uid);
  953. if($info['status']==4){
  954. $order = ["order_code"=>$info['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  955. $cgdinfo['wsend_num']-=$info['send_num'];
  956. $cgdinfo['send_num']+=$info['send_num'];
  957. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  958. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  959. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  960. $up=Db::name("purchease_order")->save($cgdinfo);
  961. if($up==false){
  962. Db::rollback();
  963. return error_show(1002,"发货失败");
  964. }
  965. ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  966. $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  967. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  968. $is_stock = Db::name('good_basic')
  969. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  970. ->value('is_stock');
  971. if($is_stock==1){
  972. //库存品,推给31库管人员、41库管-张凯旋
  973. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  974. // $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)];
  975. }else{
  976. // $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  977. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  978. // $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];
  979. }//非库存品和采返商品,推给供应商负责人
  980. }//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)];
  981. $process=["order_code"=>$info['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];
  982. ProcessOrder::AddProcess($this->post['token'],$process);
  983. $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find();
  984. if(empty($good)) {
  985. $good=[
  986. "spuCode"=>$cgdinfo['spuCode'],
  987. "wsm_code"=>$info['wsm_code'],
  988. "usable_stock"=>0,
  989. "wait_out_stock"=>0,
  990. "wait_in_stock"=>$wsm_num,
  991. "total_stock"=>0,
  992. "addtime"=>date("Y-m-d H:i:s"),
  993. "updatetime"=>date("Y-m-d H:i:s"),
  994. ];
  995. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',
  996. "action_type"=>"create"];
  997. }else{
  998. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  999. }
  1000. $good['wait_in_stock']-=$wsm_num;
  1001. // if($good['presale_stock']>0){
  1002. // if($good['presale_stock']>=$wsm_num){
  1003. // $good['presale_stock']-=$wsm_num;
  1004. // $good['wait_out_stock']+=$wsm_num;
  1005. // }else{
  1006. // $good['presale_stock']=0;
  1007. // $good['usable_stock']+=$wsm_num-$good['presale_stock'];
  1008. // $good['wait_out_stock']+=$good['presale_stock'];
  1009. // }
  1010. // }else{
  1011. $good['usable_stock']+=$wsm_num;
  1012. // }
  1013. // $good['usable_stock']+=$wsm_num;
  1014. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  1015. $good['updatetime'] = date("Y-m-d H:i:s");
  1016. $upd= Db::name("good_stock")->save($good);
  1017. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  1018. if($upd){
  1019. $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$info['wsm_num'],$cgdinfo['good_price']);
  1020. if($bnin==false){
  1021. Db::rollback();
  1022. return error_show(1004,"库存bn数据新增失败");
  1023. }
  1024. }else{
  1025. Db::rollback();
  1026. return error_show(1002,"商品入库失败");
  1027. }
  1028. $good_data=[];
  1029. $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"];
  1030. $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"];
  1031. GoodLog::LogAdd($this->post['token'],$good_data,'RKD');
  1032. $bk =Db::name("order_bk")->where(['cgdNo'=>$cgdinfo['cgdNo']])->lock(true)->find();
  1033. if($bk==false){
  1034. $bk=[
  1035. "cgdNo"=>$cgdinfo['cgdNo'],
  1036. "spuCode"=>$cgdinfo['spuCode'],
  1037. "companyNo"=>$cgdinfo['companyNo'],
  1038. "total_num"=>0,
  1039. "merge_num"=>0,
  1040. "balance_num"=>0,
  1041. "status"=>1,
  1042. "is_del"=>0,
  1043. 'addtime'=>date("Y-m-d H:i:s"),
  1044. "updatetime"=>date("Y-m-d H:i:s")
  1045. ];
  1046. }
  1047. $bk['balance_num']+=$wsm_num;
  1048. $bk['total_num']+=$wsm_num;
  1049. $bk['updatetime']=date("Y-m-d H:i:s");
  1050. $upcgd =Db::name("order_bk")->save($bk);
  1051. if($upcgd==false){
  1052. Db::rollback();
  1053. return error_show(1004,"采购单数量更新失败");
  1054. }
  1055. }
  1056. ActionLog::logAdd($this->post['token'],$sto,"RKD", $info['status'],$this->post);
  1057. $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??""];
  1058. ProcessOrder::AddProcess($this->post['token'],$process);
  1059. //需要在详情页把相关退货记录原样取出来,所以这里把if去掉,所有记录都入purchease_return表
  1060. if($info['status']==5){
  1061. $returnCode=makeNo("CR");
  1062. $data=[
  1063. "returnCode"=>$returnCode,
  1064. "wsm_in_code"=>$incode,
  1065. "cgdNo"=>$cgdinfo['cgdNo'],
  1066. "wsm_num"=>$wsm_num,
  1067. "reissue_num"=>$reissue_num,
  1068. "return_num"=>$return_num,
  1069. "return_contactor"=>$contactor,
  1070. "return_addr"=>$addr,
  1071. "return_mobile"=>$mobile,
  1072. "error_remark"=>$error_remark,
  1073. "post_code"=>"",
  1074. "post_company"=>"",
  1075. "remark"=>$remark ,
  1076. "apply_id"=>$userinfo['id'],
  1077. "apply_name"=>$userinfo['nickname'],
  1078. "status"=>0,
  1079. "addtime"=>date("Y-m-d H:i:s"),
  1080. "updatetime"=>date("Y-m-d H:i:s")
  1081. ];
  1082. $res = Db::name("purchease_return")->insert($data,true);
  1083. if($res==false){
  1084. Db::rollback();
  1085. return error_show(1002,"验货审核失败");
  1086. }
  1087. $sto = ["order_code"=>$incode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1088. ActionLog::logAdd($this->post['token'],$sto,"RKTHD",0,$data);
  1089. //维护台账记录
  1090. // Db::name('standing_book')
  1091. // ->where('wsm_in_code', $incode)
  1092. // ->update([
  1093. // 'cgdReturnCode' => $returnCode,
  1094. // 'updatetime' => date('Y-m-d H:i:s'),
  1095. // ]);
  1096. 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可能有多个
  1097. }
  1098. Db::commit();
  1099. return app_show(0,"验货审核完成");
  1100. }
  1101. }catch (\Exception $e){
  1102. Db::rollback();
  1103. return error_show(1002,$e->getMessage());
  1104. }
  1105. }
  1106. public function ReturnSend(){
  1107. $incode = isset($this->post['wsm_in_code'])&&$this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']) :"";
  1108. if($incode==""){
  1109. return error_show(1004,"参数wsm_in_code 不为空");
  1110. }
  1111. $info = Db::name("purchease_in")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  1112. if(empty($info)){
  1113. return error_show(1004,"未找到发货单数据");
  1114. }
  1115. $return = Db::name("purchease_return")->where(['wsm_in_code'=>$incode,"is_del"=>0])->find();
  1116. if(empty($return)){
  1117. return error_show(1004,"未找到退货单数据");
  1118. }
  1119. $cgdinfo=Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],"is_del"=>0])->find();
  1120. if(empty($cgdinfo)){
  1121. Db::rollback();
  1122. return error_show(1002,"未找到采购单数据");
  1123. }
  1124. $post_code = isset($this->post['post_code']) &&$this->post['post_code']!=="" ? trim($this->post['post_code']):"" ;
  1125. if($post_code==""){
  1126. return error_show(1004,"参数 post_code 不能为空");
  1127. }
  1128. $post_company = isset($this->post['post_company']) &&$this->post['post_company']!=="" ? trim($this->post['post_company']):"" ;
  1129. if($post_company==""){
  1130. return error_show(1004,"参数 post_company 不能为空");
  1131. }
  1132. $remark = isset($this->post['remark']) &&$this->post['remark']!=="" ? trim($this->post['remark']):"" ;
  1133. $data=[
  1134. "post_code"=>$post_code,
  1135. "post_company"=>$post_company,
  1136. "remark"=>$remark,
  1137. "status"=>1,
  1138. "updatetime"=>date("Y-m-d H:i:s")
  1139. ];
  1140. Db::startTrans();
  1141. try{
  1142. $up =Db::name("purchease_return")->where($return)->save($data);
  1143. if($up){
  1144. $sto = ["order_code"=>$info['wsm_in_code'],"status"=>$return['status'],"action_remark"=>'', "action_type"=>"edit"];
  1145. ActionLog::logAdd($this->post['token'],$sto,"RKTHD",1,$this->post);
  1146. $ark=$info['status'];
  1147. $info['status']=6;
  1148. $info['updatetime']=date("Y-m-d H:i:s");
  1149. $sa=Db::name("purchease_in")->save($info);
  1150. if($sa){
  1151. $stn = ["order_code"=>$info['wsm_in_code'],"status"=>$ark,"action_remark"=>'',"action_type"=>"edit"];
  1152. ActionLog::logAdd($this->post['token'],$stn,"RKD",6,$info);
  1153. $uid = Db::name('user_role')
  1154. ->where([
  1155. ['is_del', '=', 0],
  1156. ['roleid', 'in', [31, 41]],
  1157. ['status', '=', 1]
  1158. ])->column('uid');
  1159. $handle_user_list = implode(',', $uid);
  1160. $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];
  1161. ProcessOrder::AddProcess($this->post['token'],$process);
  1162. $good = Db::name("good_stock")->where(["wsm_code"=>$info['wsm_code'], "spuCode"=>$cgdinfo['spuCode'],"is_del"=>0])->find();
  1163. if(empty($good)) {
  1164. $good=[
  1165. "spuCode"=>$cgdinfo['spuCode'],
  1166. "wsm_code"=>$info['wsm_code'],
  1167. "usable_stock"=>0,
  1168. "wait_out_stock"=>0,
  1169. "wait_in_stock"=>$return['wsm_num'],
  1170. "total_stock"=>0,
  1171. "addtime"=>date("Y-m-d H:i:s"),
  1172. "updatetime"=>date("Y-m-d H:i:s"),
  1173. ];
  1174. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  1175. }else{
  1176. $order = ["order_code"=>$good['spuCode'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  1177. }
  1178. $good['wait_in_stock']-=$return['wsm_num']+$return['return_num']-$return['reissue_num'];
  1179. $good['usable_stock']+=$return['wsm_num'];
  1180. $good['total_stock']=$good['usable_stock']+$good['wait_out_stock'];
  1181. $good['updatetime'] = date("Y-m-d H:i:s");
  1182. $upd= Db::name("good_stock")->save($good);
  1183. if($upd==false){
  1184. Db::rollback();
  1185. return error_show(1002,"库存更新失败");
  1186. }
  1187. $stockid=$good['id']?? Db::name("good_stock")->getLastInsID();
  1188. $bnin=GoodStockInfo::AddBn($stockid,$info['bnCode'],$return['wsm_num'],$cgdinfo['good_price']);
  1189. if($bnin==false){
  1190. Db::rollback();
  1191. return error_show(1004,"库存bn数据新增失败");
  1192. }
  1193. $good_data=[];
  1194. $good_data[]=['good_log_code' => $incode,"stock_id"=>$stockid,"type"=>1, 'stock'=>$info['wsm_num'],"stock_name"=>"usable_stock"];
  1195. $good_data[]=['good_log_code' => $incode,"stock_id"=>$stockid,"type"=>2, 'stock'=>$good['wait_in_stock'],"stock_name"=>"wait_in_stock"];
  1196. GoodLog::LogAdd($this->post['token'],$good_data,'RKD');
  1197. $order = ["order_code"=>$cgdinfo['cgdNo'],"status"=>$cgdinfo['status'],"action_remark"=>'',"action_type"=>"edit"];
  1198. $cgdinfo['wsend_num']-=$return['wsm_num']+$return['return_num']-$return['reissue_num'];
  1199. $cgdinfo['send_num']+=$return['wsm_num']+$return['return_num']-$return['reissue_num'];
  1200. $cgdinfo['th_num']+=$return['return_num']-$return['reissue_num'];
  1201. $cgdinfo['th_fee']=round(($return['return_num']-$return['reissue_num'])*$cgdinfo['good_price'],2);
  1202. $cgdinfo['send_status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1203. $cgdinfo['status']=$cgdinfo['wsend_num']==0 ? 3:($cgdinfo['send_num']==0 ?1:2);
  1204. if($cgdinfo['th_num']==$cgdinfo['send_num']&& $cgdinfo['wsend_num']==0){
  1205. $cgdinfo['status']=4;
  1206. }
  1207. $cgdinfo['updatetime']=date("Y-m-d H:i:s");
  1208. $up=Db::name("purchease_order")->save($cgdinfo);
  1209. if($up==false){
  1210. Db::rollback();
  1211. return error_show(1004,"采购单数量更新失败");
  1212. }
  1213. $bk =Db::name("order_bk")->where(['cgdNo'=>$cgdinfo['cgdNo']])->lock(true)->find();
  1214. if($bk==false){
  1215. $bk=[
  1216. "cgdNo"=>$cgdinfo['cgdNo'],
  1217. "spuCode"=>$cgdinfo['spuCode'],
  1218. "companyNo"=>$cgdinfo['companyNo'],
  1219. "total_num"=>0,
  1220. "merge_num"=>0,
  1221. "balance_num"=>0,
  1222. "status"=>1,
  1223. "is_del"=>0,
  1224. 'addtime'=>date("Y-m-d H:i:s"),
  1225. "updatetime"=>date("Y-m-d H:i:s")
  1226. ];
  1227. }
  1228. $bk['balance_num']+=$return['wsm_num'];
  1229. $bk['total_num']+=$return['wsm_num'];
  1230. $bk['updatetime']=date("Y-m-d H:i:s");
  1231. $upcgd =Db::name("order_bk")->save($bk);
  1232. if($upcgd==false){
  1233. Db::rollback();
  1234. return error_show(1004,"采购单数量更新失败");
  1235. }
  1236. ActionLog::logAdd($this->post['token'],$order,"CGD", $cgdinfo['status'],$cgdinfo);
  1237. $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  1238. if($cgdinfo['status']==1 || $cgdinfo['status']==2){
  1239. // $orderinfo = Db::name('sale')
  1240. // ->alias('a')
  1241. // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
  1242. // ->leftJoin('order_num b','b.orderCode=a.orderCode')
  1243. // ->where([
  1244. // 'a.is_del'=>0,
  1245. // 'b.cgdNo'=>$cgdinfo['cgdNo'],
  1246. // ])->findOrEmpty();
  1247. $is_stock = Db::name('good_basic')
  1248. ->where(['is_del'=>0,'spuCode'=>$cgdinfo['spuCode']])
  1249. ->value('is_stock');
  1250. if($is_stock==1){
  1251. //库存品,推给31库管人员、41库管-张凯旋
  1252. $uid = Db::name('user_role')
  1253. ->where([
  1254. ['is_del', '=', 0],
  1255. ['roleid', 'in', [31, 41]],
  1256. ['status', '=', 1]
  1257. ])->column('uid');
  1258. if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  1259. // $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)];
  1260. // $handle_user_list = implode(",",$uid);
  1261. }else{
  1262. // $holder_id = Db::name('supplier')->where(['code' => $cgdinfo['supplierNo'], 'is_del' => 0])->value('personid', 0);
  1263. if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  1264. // $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];//非库存品和采返商品,推给供应商负责人
  1265. }
  1266. }//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)];
  1267. $process=["order_code"=>$cgdinfo['cgdNo'],"order_id"=>$cgdinfo['id'],"order_status"=>$cgdinfo['status'],"order_type"=>'CGD',"before_status"=>$order['status'],'holder_id'=>$holder_id];//非库存品和采返商品,推给供应商负责人
  1268. ProcessOrder::AddProcess($this->post['token'],$process);
  1269. Db::commit();
  1270. return app_show(0,"退货发货成功");
  1271. }else{
  1272. Db::rollback();
  1273. return error_show(1004,"退货发货失败");
  1274. }
  1275. }else{
  1276. return error_show(1004,"退货发货失败");
  1277. }
  1278. }catch (\Exception $e){
  1279. Db::rollback();
  1280. return error_show(1004,$e->getMessage());
  1281. }
  1282. }
  1283. public function returnList(){
  1284. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  1285. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  1286. $where = [['a.is_del',"=",0],['b.is_del',"=",0]];
  1287. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
  1288. if($cgdNo!=""){
  1289. $where[]=['a.cgdNo',"like", "%$cgdNo%"];
  1290. }
  1291. $returnCode= isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  1292. if($returnCode!=""){
  1293. $where[]=['a.returnCode',"like", "%$returnCode%"];
  1294. }
  1295. $good_type_code=isset($this->post['good_type_code']) && $this->post['good_type_code']!==""? trim($this->post['good_type_code']):"";
  1296. if($good_type_code !=""){
  1297. $where[]= ["b.spuCode",'like',"%$good_type_code%"];
  1298. }
  1299. $good_name=isset($this->post['good_name']) && $this->post['good_name']!==""? trim($this->post['good_name']):"";
  1300. if($good_name !=""){
  1301. $where[]= ["b.good_name",'like',"%$good_name%"];
  1302. }
  1303. $status=isset($this->post['status']) && $this->post['status']!==""? trim($this->post['status']):"";
  1304. if($status!=""){
  1305. $where[]= ["a.status",'=',$status];
  1306. }
  1307. // $role = $this->checkDataShare();
  1308. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ["a.apply_id", "in", $role[DataGroupModel::$type_全部]];
  1309. $role = $this->checkDataShare();
  1310. $hand = resign_hand_user($this->uid,0);
  1311. if (!empty($role[DataGroupModel::$type_全部])) {
  1312. $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  1313. $where[] = ['a.apply_id', 'in',$arr];
  1314. }
  1315. $count = Db::name('purchease_return')
  1316. ->alias('a')
  1317. ->join("purchease_order b","b.cgdNo=a.cgdNo","left")
  1318. ->where($where)
  1319. ->count();
  1320. $total = ceil($count / $size);
  1321. $page = $page >= $total ? $total : $page;
  1322. $list = Db::name('purchease_return')
  1323. ->alias('a')
  1324. ->join("purchease_order b","b.cgdNo=a.cgdNo","left")
  1325. ->where($where)
  1326. ->page($page,$size)
  1327. ->field("a.*,b.spuCode,b.good_price,b.order_type,b.good_price,b.total_fee,b.good_name")
  1328. ->order("a.addtime desc")
  1329. ->select()
  1330. ->toArray();
  1331. $data=[];
  1332. foreach ($list as $value){
  1333. if($value['order_type']==3){
  1334. $goon = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  1335. }else {
  1336. $goon =Db::name('good_basic')->where(['spuCode'=>$value['spuCode']])->find();
  1337. }
  1338. $value['can']= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  1339. $value['return_fee']= round($value['good_price']*$value['return_num'],2);
  1340. //是否具有编辑权限
  1341. $value['is_allow_update'] = 0;
  1342. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  1343. $data[]=$value;
  1344. }
  1345. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  1346. }
  1347. }