Reorder.php 97 KB

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