Reorder.php 105 KB

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