Reorder.php 100 KB

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