Reorder.php 109 KB

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