Reorder.php 94 KB

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