Reorder.php 104 KB

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