Reorder.php 108 KB

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