Reorder.php 106 KB

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