Reorder.php 100 KB

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