Reorder.php 109 KB

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