Reorder.php 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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.customer_code|b.supplierNo', '=', $relaComNo];
  429. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
  430. if ($supplierNo != "") $where[] = ['b.supplierNo', '=', $supplierNo];
  431. $condition=[];
  432. // $role=$this->checkRole();
  433. // if(!empty($role['write']) && $this->uid!=""){
  434. // // $where[]=["sr.apply_id","in",$role['write']];
  435. // $condition .="sr.cgderid = {$this->uid} or sr.apply_id in (".implode(',',$role['write']).")";
  436. // }
  437. $role = $this->checkDataShare();
  438. $hand = resign_hand_user($this->uid,0);
  439. if (!empty($role[DataGroupModel::$type_全部])) {
  440. $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  441. // $uidim =implode(",",$hand);
  442. // $condition .= "sr.cgderid in ($uidim) or sr.person_id in ($uidim) or sr.apply_id in (" . implode(',',$arr) .
  443. // ")";
  444. $condition[]= ["sr.apply_id","in",$arr];
  445. $condition[]= ["sr.cgderid","in",$hand];
  446. $condition[]= ["sr.person_id","in",$hand];
  447. }
  448. // $role = $this->checkDataShare();
  449. // 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_全部]) . ")";
  450. if(in_array($this->roleid,config('app.wsm_cgder_role'))){
  451. $where[]=["b.order_type","=",1];
  452. }
  453. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  454. if ($company_name !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($company_name)];
  455. $count=Db::name("sale_return")
  456. ->alias('sr')
  457. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  458. ->where($where)
  459. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  460. ->count('sr.id');
  461. $total = ceil($count/$size);
  462. $page = $total>=$page ? $page :$total;
  463. $list = Db::name("sale_return")
  464. ->alias('sr')
  465. ->field('sr.*,b.skuCode,b.sale_price,b.good_num total_num ')
  466. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  467. ->where($where)
  468. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  469. ->order("addtime desc")
  470. ->page($page,$size)
  471. ->select()
  472. ->toArray();
  473. // echo Db::name("sale_return")->getLastSql();
  474. $all_createrid = array_column($list,'apply_id');
  475. $item = get_company_name_by_uid($all_createrid);
  476. $data=[];
  477. foreach ($list as $value){
  478. $value['error_msg']='';
  479. if($value['error_code']!=''){
  480. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  481. $value['error_msg']= isset($error['result'])?$error['result']:"";
  482. }
  483. // $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  484. // $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  485. $value['return_total'] =$value['sale_price']*$value['num'] ;
  486. // $value['total_num'] =$order['good_num'] ;
  487. $value['company_name'] = $item[$value['apply_id']]??'';
  488. //是否具有编辑权限
  489. $value['is_allow_update'] = 0;
  490. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  491. $data[]=$value ;
  492. }
  493. return app_show(0,"获取成功",["count"=>$count,'list'=>$data]);
  494. }
  495. public function info(){
  496. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  497. :"";
  498. if($code==""){
  499. return error_show(1004,"参数returnCode不能为空");
  500. }
  501. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  502. if(empty($info)){
  503. return error_show(1004,"未找到退货数据");
  504. }
  505. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  506. if($orderinfo['order_type']==3){
  507. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  508. }else {
  509. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  510. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  511. }
  512. if ($goon==false) {
  513. return error_show(1003, "未找到商品数据");
  514. }
  515. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  516. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  517. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  518. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  519. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  520. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  521. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  522. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  523. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  524. $info['return_total'] = $info['sale_price']*$info['num'] ;
  525. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  526. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  527. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  528. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  529. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  530. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  531. $info['customer_name']='';
  532. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  533. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  534. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  535. }
  536. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  537. $info['supplier_name']='';
  538. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  539. // $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  540. // $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  541. $userCommon = new \app\admin\common\User();
  542. $tmp = $userCommon->handle('bInfo', ['companyNo' => $orderinfo['supplierNo']]);
  543. $info['supplier_name'] = $tmp['data']['company'] ?? '';
  544. }
  545. $info['platform_name']='';
  546. $info['platform_id']=$orderinfo['platform_id'];
  547. if($orderinfo['platform_id']!=0){
  548. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  549. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  550. }
  551. $info['cgd_wsend']="";
  552. $info['cgd_send']="";
  553. $info['cgd_total']="";
  554. if($orderinfo['order_type']==2){
  555. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  556. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  557. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  558. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  559. }
  560. $info['error_msg']='';
  561. if($info['error_code']!=''){
  562. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  563. $info['error_msg']= isset($error['result'])?$error['result']:"";
  564. }
  565. if($info['return_wsm']!=""){
  566. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  567. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  568. }
  569. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  570. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  571. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  572. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  573. $wsm=[];
  574. if(!empty($wsm_return)){
  575. foreach ($wsm_return as $value){
  576. $value['wsm_name']="";
  577. $value['wsm_supplier']="";
  578. $value['wsm_supplierNo']="";
  579. if($value['wsm_code']!=""){
  580. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  581. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  582. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  583. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  584. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  585. }
  586. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  587. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  588. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  589. $value['wsm_send'] = $send ?? 0;
  590. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  591. $wsm[]=$value;
  592. }
  593. }
  594. $info['wsminfo']=$wsm;
  595. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  596. $addrinfo=[];
  597. if(!empty($addr)){
  598. foreach ( $addr as $value){
  599. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  600. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  601. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  602. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  603. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  604. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  605. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  606. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  607. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  608. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode'],"is_del"=>0])->where("status",">=",2)->sum("send_num");
  609. $value['addr_send'] = $send ?? 0;
  610. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  611. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  612. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  613. $addrinfo[]=$value;
  614. }
  615. }
  616. $info['addrinfo']=$addrinfo;
  617. $info['can']=$int;
  618. return app_show(0,"获取成功",$info);
  619. }
  620. /**
  621. * @return \think\response\Json|void
  622. * @throws \think\db\exception\DataNotFoundException
  623. * @throws \think\db\exception\DbException
  624. * @throws \think\db\exception\ModelNotFoundException
  625. */
  626. public function delete(){
  627. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  628. :"";
  629. if($code==""){
  630. return error_show(1004,"参数returnCode不能为空");
  631. }
  632. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  633. if(empty($info)){
  634. return error_show(1004,"未找到退货数据");
  635. }
  636. $info["is_del"]=1;
  637. $info["updatetime"]=date("Y-m-d H:i:s");
  638. $del = Db::name("sale_return")->save($info);
  639. if($del){
  640. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  641. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"XSTHD",0,$ste);
  642. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  643. ProcessOrder::workdel($process);
  644. return app_show(0,"删除成功");
  645. }else{
  646. return error_show(1004,"删除失败");
  647. }
  648. }
  649. /**审核
  650. * 1待业务审批
  651. * 2待专员审批(该节点废除)
  652. * 3待主管审批(该节点废除)
  653. * 4退货完成
  654. * 5业务驳回
  655. * 6采购驳回(该节点废除)
  656. * 7专员审批不通过(该节点废除)
  657. * 9待供应商审核
  658. * 8供应商已驳回待采购审核
  659. * 10业务公司修改待供应商确认
  660. */
  661. public function exam()
  662. {
  663. $param = $this->request->only(['returnCode', 'status', 'remark' => '', 'return_wsm' => ''], 'post', 'trim');
  664. $val = Validate::rule([
  665. 'returnCode|退货单编号' => 'require',
  666. 'status|状态' => 'require|in:5,9,8,10,4',
  667. 'remark|备注' => 'requireIf:status,5|requireIf:status,8|requireIf:status,10',
  668. ]);
  669. if ($val->check($param) == false) return json_show(1004, $val->getError());
  670. $code = $param['returnCode'];
  671. $status = $param['status'];
  672. $remark = $param['remark'];
  673. // $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']):"";
  674. // if($code==""){
  675. // return error_show(1004,"参数returnCode不能为空");
  676. // }
  677. $info = Db::name("sale_return")->where(["returnCode" => $code, "is_del" => 0])->findOrEmpty();
  678. if (empty($info)) return error_show(1004, "未找到退货数据");
  679. switch ($info['status']) {
  680. case 1:
  681. if (in_array($param['status'], [5, 9]) == false) return json_show(1004, '选项错误');
  682. break;
  683. case 9:
  684. if (in_array($param['status'], [4, 8]) == false) return json_show(1004, '选项错误');
  685. break;
  686. case 8:
  687. if (in_array($param['status'], [4, 10]) == false) return json_show(1004, '选项错误');
  688. if ($param['status'] == 4) {
  689. if ($param['return_wsm'] == '') return json_show(1004, '仓库编码不能为空');
  690. $tmp = Db::name("warehouse_info")
  691. ->field('id')
  692. ->where(['wsm_code' => $param['return_wsm']])
  693. ->findOrEmpty();
  694. if (empty($tmp)) return json_show(1004, '未找到仓库数据');
  695. else $info['return_wsm'] = $param['return_wsm'];
  696. }
  697. break;
  698. case 10:
  699. if (in_array($param['status'], [4, 8]) == false) return json_show(1004, '选项错误');
  700. if ($param['status'] == 8) $info['loop_total'] += 1;
  701. break;
  702. }
  703. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  704. // if($status===""){
  705. // return error_show(1004,"参数status不能为空");
  706. // }
  707. // $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  708. $var = $info['status'];
  709. $orderinfo = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->findOrEmpty();
  710. if (empty($orderinfo)) return error_show(1004, "未找到订单数据");
  711. if ($status == 4) {
  712. if ($info['is_addr'] == 1) {
  713. $addr = Db::name("sale_returnaddr")
  714. ->where(['returnCode' => $info['returnCode'], "is_del" => 0])
  715. ->select()
  716. ->toArray();
  717. if (empty($addr)) return error_show(1004, "未找到发货单地址数据");
  718. }
  719. }
  720. // if($status==3){
  721. // $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  722. // if($is_th===""){
  723. // return error_show(1004,"参数is_th不能为空");
  724. // }
  725. // $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  726. // if($is_th==0){
  727. // if($return_wsm===""){
  728. // return error_show(1004,"参数return_wsm 不能为空");
  729. // }
  730. // $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  731. // if($wsmcode==false){
  732. // return error_show(1004,"为找到仓库数据");
  733. // }
  734. // }
  735. // $info['return_wsm'] =$return_wsm ;
  736. // $info['is_th'] =$is_th ;
  737. // }
  738. if ($info['status'] == 9 && $param['status'] == 4) $info['is_th'] = 1;
  739. Db::startTrans();
  740. try {
  741. $date = date("Y-m-d H:i:s");
  742. $userCommon = new \app\admin\common\User();
  743. $temp = $info['status'];
  744. $info['status'] = $status;
  745. $remark != "" ? $info['remark'] = $remark : "";
  746. $info["updatetime"] = $date;
  747. $up = Db::name("sale_return")->save($info);
  748. if ($up) {
  749. //如果是节点2(待专员审核),要将待办数据推给供应商负责人
  750. 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']];
  751. else $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type" => "XSTHD", 'before_status' => $temp, 'holder_id' => $info['apply_id']];
  752. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  753. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  754. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $ste, "XSTHD", $status, $info);
  755. if ($status == 4) {
  756. if ($info['is_addr'] == 1) {
  757. if (isset($addr) && !empty($addr)) {
  758. foreach ($addr as $value) {
  759. $addrinfo = Db::name("order_addr")
  760. ->where(['id' => $value['addrid'], "is_del" => 0])
  761. ->find();
  762. if ($addrinfo == false) {
  763. Db::rollback();
  764. return error_show(1004, "地址数据未找到");
  765. }
  766. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  767. Db::rollback();
  768. return error_show(1004, "地址发货数量不足");
  769. }
  770. $addrinfo['receipt_quantity'] -= $value['return_num'];
  771. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  772. $addrinfo['updatetime'] = $date;
  773. $addrup = Db::name("order_addr")->save($addrinfo);
  774. if ($addrup == false) throw new Exception('地址发货数量更新失败');
  775. if ($value['outCode'] != "") {
  776. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->findOrEmpty();
  777. if (empty($out)) throw new Exception('地址发货单数据未找到');
  778. if ($out['status'] >= 2) throw new Exception('地址发货单已发货');
  779. if ($out['send_num'] < $value['return_num']) throw new Exception('地址发货单发货数量不足');
  780. $out['send_num'] -= $value['return_num'];
  781. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  782. $out['updatetime'] = $date;
  783. $outup = Db::name("order_out")->save($out);
  784. if ($outup == false) throw new Exception('地址发货单更新失败');
  785. }
  786. }
  787. }
  788. }
  789. if ($orderinfo['wsend_num'] < $info['num']) throw new Exception('销售单未发货数量不足退货');
  790. $lor = $orderinfo['status'];
  791. $orderinfo['wsend_num'] -= $info['num'];
  792. $orderinfo['send_num'] += $info['num'];
  793. $orderinfo['status'] = $orderinfo['wsend_num'] == 0 ? 2 : ($orderinfo['send_num'] == 0 ? 0 : 1);
  794. $orderinfo['send_status'] = $orderinfo['wsend_num'] == 0 ? 3 : ($orderinfo['send_num'] == 0 ? 1 : 2);
  795. if ($orderinfo['is_stock'] == 1) {
  796. $orderinfo['th_num'] += $info['num'];
  797. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  798. $orderinfo['status'] = 3;
  799. }
  800. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  801. $orderinfo['updatetime'] = $date;
  802. $uap = Db::name("sale")->save($orderinfo);
  803. if ($uap == false) throw new Exception('销售单订单更新失败');
  804. } else {
  805. if ($info['is_th'] == 1) {
  806. $orderinfo['th_num'] += $info['num'];
  807. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  808. $orderinfo['status'] = 3;
  809. }
  810. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  811. $orderinfo['updatetime'] = $date;
  812. $uap = Db::name("sale")->save($orderinfo);
  813. if ($uap == false) throw new Exception('销售单订单更新失败');
  814. $ordernum = Db::name("order_num")
  815. ->where(['orderCode' => $orderinfo['orderCode']])
  816. ->findOrEmpty();
  817. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  818. $ordernum['send_num'] -= $info['num'];
  819. $orderup = Db::name("order_num")->save($ordernum);
  820. if ($orderup == false) throw new Exception('关联数据更新失败');
  821. $cgd = Db::name("purchease_order")
  822. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  823. ->findOrEmpty();
  824. if ($cgd == false) throw new Exception('未找到采购单数据');
  825. if ($info['is_all'] == 1 && $cgd['send_status'] != 1) throw new Exception('采购单已入库无法全部退货');
  826. $lor = $cgd['status'];
  827. if ($cgd['wsend_num'] < $info['num']) {
  828. $cgd['send_num'] += $cgd['wsend_num'] == 0 ? 0 : ($info['num'] - $cgd['wsend_num']);
  829. $cgd['wsend_num'] = 0;
  830. } else {
  831. $cgd['wsend_num'] -= $info['num'];
  832. $cgd['send_num'] += $info['num'];
  833. }
  834. $cgd['status'] = $cgd['wsend_num'] == 0 ? 2 : ($cgd['send_num'] == 0 ? 0 : 1);
  835. $cgd['send_status'] = $cgd['wsend_num'] == 0 ? 3 : ($cgd['send_num'] == 0 ? 1 : 2);
  836. $cgd['th_num'] += $info['num'];
  837. if ($cgd['th_num'] == $cgd['send_num'] && $cgd['wsend_num'] == 0) {
  838. $cgd['status'] = 4;
  839. }
  840. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  841. $cgd['updatetime'] = $date();
  842. $cgdup = Db::name("purchease_order")->save($cgd);
  843. if ($cgdup == false) throw new Exception('采购单数据更新失败');
  844. $stock = Db::name("good_stock")
  845. ->where(['is_del' => 0, "spuCode" => $info['good_code'], 'wsm_code' => $cgd['wsm_code']])
  846. ->findOrEmpty();
  847. if (empty($stock)) throw new Exception('商品仓库未找到');
  848. if ($stock['wait_in_stock'] < $info['num']) {
  849. $stock['wait_in_stock'] = 0;
  850. if ($stock['usable_stock'] > $info['num'] - $stock['wait_in_stock']) {
  851. $stock['usable_stock'] -= $info['num'] - $stock['wait_in_stock'];
  852. } else {
  853. $stock['usable_stock'] = 0;
  854. $stock['wait_out_stock'] -= $info['num'] - $stock['wait_in_stock'] - $stock['usable_stock'];
  855. }
  856. $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
  857. } else {
  858. $stock['wait_in_stock'] -= $info['num'];
  859. }
  860. $stock['updatetime'] = $date;
  861. $st_up = Db::name("good_stock")->save($stock);
  862. if ($st_up == false) throw new Exception('可售商品入库失败');
  863. } else {
  864. $orderinfo['th_num'] += $info['num'];
  865. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  866. $orderinfo['status'] = 3;
  867. }
  868. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  869. $orderinfo['updatetime'] = $date;
  870. $uap = Db::name("sale")->save($orderinfo);
  871. if ($uap == false) throw new Exception('销售单订单更新失败');
  872. $ordernum = Db::name("order_num")
  873. ->where(['orderCode' => $orderinfo['orderCode']])
  874. ->findOrEmpty();
  875. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  876. $ordernum['send_num'] -= $info['num'];
  877. $ordernum['wsend_num'] = $info['num'];
  878. $orderup = Db::name("order_num")->save($ordernum);
  879. if ($orderup == false) throw new Exception('关联数据更新失败');
  880. $cgd = Db::name("purchease_order")
  881. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  882. ->findOrEmpty();
  883. if (empty($cgd)) throw new Exception('未找到采购单数据');
  884. $stock = Db::name("good_stock")
  885. ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code']])
  886. ->findOrEmpty();
  887. if (empty($stock)) throw new Exception('商品仓库未找到');
  888. if ($info['is_all'] == 1) {
  889. if ($stock['wait_in_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
  890. else $stock['wait_in_stock'] -= $info['num'];
  891. } else {
  892. if ($stock['usable_stock'] + $stock['wait_out_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
  893. else {
  894. if ($stock['usable_stock'] > $info['num']) {
  895. $stock['usable_stock'] -= $info['num'];
  896. } else {
  897. $stock['usable_stock'] = 0;
  898. $stock['wait_out_stock'] -= $info['num'] - $stock['usable_stock'];
  899. }
  900. $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
  901. }
  902. }
  903. $stock['updatetime'] = $date;
  904. $st_up = Db::name("good_stock")->save($stock);
  905. if ($st_up == false) throw new Exception('可售商品入库失败');
  906. }
  907. }
  908. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  909. "order_code" => $orderinfo["orderCode"],//出库单号
  910. "status" => $lor,//这里的status是之前的值
  911. "action_remark" => '',//备注
  912. "action_type" => "status"//新建create,编辑edit,更改状态status
  913. ], "XSQRD", $orderinfo['status'], $orderinfo);
  914. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  915. "order_type" => 'XSQRD',
  916. "order_code" => $orderinfo["orderCode"],//出库单号
  917. "order_id" => $orderinfo["id"],
  918. "order_status" => $orderinfo['status'], "before_status" => $lor
  919. ]);
  920. //对应采购单也要处理
  921. // $holder_id = Db::name('supplier')->where(['code' => $cgd['supplierNo'], 'is_del' => 0])->value('personid', 0);
  922. $holder = $userCommon->handle('sInfo', ['code' => $cgd['supplierNo']]);
  923. if (!isset($holder['code']) || $holder['code'] != 0) throw new Exception($holder['message']);
  924. $holder_id = $holder['data']['personid'];
  925. if (in_array($cgd['status'], [4])) {
  926. if ($orderinfo['is_stock'] == 1) {
  927. //库存品,推给31库管人员、41库管-张凯旋
  928. $uid = Db::name('user_role')
  929. ->where([
  930. ['is_del', '=', 0],
  931. ['roleid', 'in', [31, 41]],
  932. ['status', '=', 1]
  933. ])->column('uid');
  934. // if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  935. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  936. "order_type" => 'CGD',
  937. "order_code" => $cgd['cgdNo'],
  938. "order_id" => $cgd["id"],
  939. "order_status" => $cgd['status'],
  940. "before_status" => $lor,
  941. 'holder_id' => $holder_id,
  942. 'handle_user_list' => implode(',', $uid),
  943. ]);
  944. } else {
  945. // if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  946. //非库存品和采返商品,推给供应商负责人
  947. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  948. "order_type" => 'CGD',
  949. "order_code" => $cgd['cgdNo'],
  950. "order_id" => $cgd["id"],
  951. "order_status" => $cgd['status'],
  952. "before_status" => $lor,
  953. 'holder_id' => $holder_id,
  954. 'handle_user_list' => $orderinfo['cgderid'],
  955. ]);
  956. }
  957. }
  958. // ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  959. // "order_type" => 'CGD',
  960. // "order_code" => $cgd['cgdNo'],
  961. // "order_id" => $cgd["id"],
  962. // "order_status" => $cgd['status'],
  963. // "before_status" => $lor,
  964. // 'holder_id' => $holder_id
  965. // ]);
  966. if ($orderinfo['is_stock'] == 1 || $info['is_th'] == 0) {
  967. if ($orderinfo['order_type'] != 1) {
  968. if ($orderinfo['order_type'] == 3) {
  969. $goon = Db::name("good_zixun")
  970. ->where(["spuCode" => $orderinfo['good_code'], "is_del" => 0])
  971. ->findOrEmpty();
  972. $isZx = 1;
  973. } else {
  974. $goon = Db::name('good_basic')
  975. ->where(['spuCode' => $orderinfo['good_code']])
  976. ->findOrEmpty();
  977. $isZx = 2;
  978. }
  979. $spuCode = $this->CheckGoodZx($goon, $isZx, $code);
  980. $wsmcode = $info['return_wsm'];
  981. if ($wsmcode == "") throw new Exception('未找到退货仓库');
  982. $stock = Db::name("good_stock")
  983. ->where(['is_del' => 0, "spuCode" => $spuCode, 'wsm_code' => $wsmcode])
  984. ->findOrEmpty();
  985. if (empty($stock)) {
  986. $stock = [
  987. "spuCode" => $spuCode,
  988. "wsm_code" => $wsmcode,
  989. "usable_stock" => 0,
  990. "wait_out_stock" => 0,
  991. "wait_in_stock" => 0,
  992. "total_stock" => 0,
  993. "addtime" => $date,
  994. "updatetime" => $date,
  995. ];
  996. }
  997. $stock['usable_stock'] += $info['num'];
  998. $stock['updatetime'] = $date;
  999. $st_up = Db::name("good_stock")->save($stock);
  1000. if ($st_up == false) throw new Exception('可售商品入库失败');
  1001. $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  1002. $sabebn = Db::name("sale_info")
  1003. ->where(["orderCode" => $orderinfo["orderCode"]])
  1004. ->select()
  1005. ->toArray();
  1006. if (!empty($sabebn)) {
  1007. $total_num = $info['num'];
  1008. foreach ($sabebn as $ve) {
  1009. $tempnum = 0;
  1010. if ($total_num == 0) break;
  1011. if ($total_num >= $ve['num']) {
  1012. $tempnum = $ve['num'];
  1013. $total_num -= $ve['num'];
  1014. $ve['th_num'] += $ve['num'];
  1015. $ve['num'] = 0;
  1016. } else {
  1017. $tempnum = $total_num;
  1018. $ve['num'] -= $total_num;
  1019. $ve['th_num'] += $total_num;
  1020. $total_num = 0;
  1021. }
  1022. $bnin = GoodStockInfo::AddBn($stockid, $ve['bnCode'], $tempnum, $ve['origin_price']);
  1023. if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1024. $ve['updatetime'] = $date;
  1025. $up = Db::name("sale_info")->save($ve);
  1026. if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
  1027. $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
  1028. if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1029. }
  1030. } else {
  1031. $bn = makeNo("BN");
  1032. $bnin = GoodStockInfo::AddBn($stockid, $bn, $info['num'], $cgd['good_price'] ?? 0);
  1033. if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1034. }
  1035. $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => $stockid, "type" => 1, 'stock' => $info['num'], "stock_name" => "usable_stock"];
  1036. GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname], $good_data, 'XSTHD');
  1037. } else {
  1038. $sabebn = Db::name("sale_info")
  1039. ->where(["orderCode" => $orderinfo["orderCode"]])
  1040. ->select()
  1041. ->toArray();
  1042. if (!empty($sabebn)) {
  1043. $total_num = $info['num'];
  1044. foreach ($sabebn as $ve) {
  1045. $stock = Db::name("good_stock")
  1046. ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'id' => $ve['stockid']])
  1047. ->findOrEmpty();
  1048. if ($stock == false) throw new Exception('商品库存数据未找到');
  1049. $tempnum = 0;
  1050. if ($total_num == 0) break;
  1051. if ($total_num >= $ve['num']) {
  1052. $tempnum = $ve['num'];
  1053. $total_num -= $ve['num'];
  1054. $ve['th_num'] += $ve['num'];
  1055. $ve['num'] = 0;
  1056. } else {
  1057. $tempnum = $total_num;
  1058. $ve['num'] -= $total_num;
  1059. $ve['th_num'] += $total_num;
  1060. $total_num = 0;
  1061. }
  1062. $stock['usable_stock'] += $tempnum;
  1063. $stock['wait_out_stock'] -= $tempnum;
  1064. $stock['updatetime'] = $date;
  1065. $st_up = Db::name("good_stock")->save($stock);
  1066. if ($st_up == false) throw new Exception('可售商品入库失败');
  1067. $bnin = GoodStockInfo::AddBn($ve['stock_id'], $ve['bnCode'], $tempnum, $ve['origin_price']);
  1068. if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1069. $ve['updatetime'] = $date;
  1070. $up = Db::name("sale_info")->save($ve);
  1071. if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
  1072. $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
  1073. if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1074. }
  1075. } else {
  1076. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo["orderCode"]])->findOrEmpty();
  1077. if ($ordernum == false) throw new Exception('未找到关联采购单');
  1078. $cgd = Db::name("purchease_order")
  1079. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  1080. ->findOrEmpty();
  1081. if ($cgd == false) throw new Exception('未找到采购单数据');
  1082. $bn = makeNo("BN");
  1083. $stock = Db::name("good_stock")
  1084. ->where(["spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code'], "is_del" => 0, "status" => 1])
  1085. ->findOrEmpty();
  1086. if ($stock == false) throw new Exception('商品库存数据未找到');
  1087. $stock['usable_stock'] += $info['num'];
  1088. $stock['wait_out_stock'] -= $info['num'];
  1089. $stock['updatetime'] = $date;
  1090. $st_up = Db::name("good_stock")->save($stock);
  1091. if ($st_up == false) throw new Exception('可售商品入库失败');
  1092. $yp = GoodStockInfo::AddBn($stock['id'], $bn, $info['num'], $cgd['good_price']);
  1093. if ($yp == false) throw new Exception('商品批次退货入库失败');
  1094. }
  1095. }
  1096. }
  1097. $data = [
  1098. "orderCode" => $info['orderCode'],
  1099. "th_type" => 1,
  1100. "th_num" => $info['num'],
  1101. "th_fee" => round($info['num'] * $orderinfo['sale_price'], 2),
  1102. "thCode" => $info['returnCode'],
  1103. "spuCode" => $orderinfo['good_code'],
  1104. "good_name" => $orderinfo['good_name'],
  1105. "cat_id" => $orderinfo['cat_id'],
  1106. "apply_id" => $info['apply_id'],
  1107. "apply_name" => $info['apply_name'],
  1108. "addtime" => $date,
  1109. "status" => 1,
  1110. "is_del" => 0
  1111. ];
  1112. $inse = Db::name("th_data")->insert($data);
  1113. if ($inse == false) throw new Exception('退货单更新失败');
  1114. }
  1115. }
  1116. Db::commit();
  1117. return app_show(0, "更新成功");
  1118. } catch (Exception $e) {
  1119. Db::rollback();
  1120. return error_show(1004, $e->getMessage() . '|' . $e->getFile() . ':' . $e->getLine());
  1121. }
  1122. }
  1123. public function zxcreate(){
  1124. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  1125. if($ordeCode==''){
  1126. return error_show(1004,"参数orderCode 不能为空");
  1127. }
  1128. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  1129. if(empty($order)){
  1130. return error_show(1005,"未找到订单数据");
  1131. }
  1132. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1133. if($errorCode==''){
  1134. return error_show(1004,"参数errorCode 不能为空");
  1135. }
  1136. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1137. if(empty($error)){
  1138. return error_show(1005,"未找到退货原因数据");
  1139. }
  1140. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1141. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  1142. // if($token==''){
  1143. // return error_show(105,"参数token不能为空");
  1144. // }
  1145. // $user =GetUserInfo($token);
  1146. // if(empty($user)||$user['code']!=0){
  1147. // return error_show(1002,"申请人数据不存在");
  1148. // }
  1149. $rm= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  1150. $ri= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  1151. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  1152. if($num==''){
  1153. return error_show(1005,"参数num不能为空");
  1154. }
  1155. if($order['wsend_num']<$num){
  1156. return error_show(1002,"仓库未发货数量不足退货");
  1157. }
  1158. $returnCode=makeNo("RS");
  1159. Db::startTrans();
  1160. try{
  1161. $in = [
  1162. "returnCode"=>$returnCode,
  1163. "orderCode"=>$ordeCode,
  1164. "good_code"=>$order['good_code'],
  1165. "good_name"=>$order['good_name'],
  1166. "apply_id"=>$rm,
  1167. "apply_name"=>$ri,
  1168. "error_code"=>$errorCode,
  1169. "num"=>$num,
  1170. "remark"=>$remark,
  1171. "order_type"=>2,
  1172. "status"=>0,
  1173. "is_del"=>0,
  1174. "addtime"=>date("Y-m-d H:i:s"),
  1175. "updatetime"=>date("Y-m-d H:i:s")
  1176. ];
  1177. $create = Db::name("sale_return")->insert($in,true);
  1178. if($create>0) {
  1179. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1180. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1181. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1182. "action_type"=>"create"];
  1183. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"XSTHD",3,$in);
  1184. //维护台账记录
  1185. // Db::name('standing_book')
  1186. // ->where('ordeCode', $ordeCode)
  1187. // ->update([
  1188. // 'returnGoodCode' => $returnCode,
  1189. // 'updatetime' => date("Y-m-d H:i:s")
  1190. // ]);
  1191. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  1192. if ($order['send_type'] == 1) {
  1193. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1194. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1195. if(!empty($wsend)){
  1196. foreach ($wsend as $value){
  1197. $tt = $value['status'];
  1198. $value['status']=0;
  1199. $value['updatetime']=date("Y-m-d H:i:s");
  1200. $up =Db::name("order_out")->save($value);
  1201. if($up){
  1202. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt,'holder_id' => $value['apply_id']];
  1203. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1204. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1205. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"CKD",0,$value);
  1206. }else{
  1207. Db::rollback();
  1208. return error_show(1005,"退货单新建失败");
  1209. }
  1210. }
  1211. }
  1212. }
  1213. Db::commit();
  1214. return error_show(0,"退货单新建成功");
  1215. }
  1216. Db::rollback();
  1217. return error_show(1005,"退货单新建失败");
  1218. }catch (\Exception $e){
  1219. Db::rollback();
  1220. return error_show(1005,$e->getMessage());
  1221. }
  1222. }
  1223. //全部退货
  1224. public function allReturn(){
  1225. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''? trim($this->post['orderCode']):"";
  1226. if($orderCode==''){
  1227. return error_show(1004,"参数orderCode 不能为空");
  1228. }
  1229. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  1230. if(empty($order)){
  1231. return error_show(1005,"未找到订单数据");
  1232. }
  1233. if($order['wsend_num']!=$order['good_num']){
  1234. return error_show(1005,"订单未发货数量与总数不等,无法全退");
  1235. }
  1236. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","<",4]])->count();
  1237. if($retrun>0){
  1238. return error_show(1005,"存在退货订单数据");
  1239. }
  1240. if($order['order_type']==3){
  1241. $goon = Db::name("good_zixun")
  1242. ->where(["spuCode"=>$order['good_code'],"is_del"=>0])
  1243. ->findOrEmpty();
  1244. }else {
  1245. $goon = Db::name('good_platform')
  1246. ->alias('a')
  1247. ->join('good b', 'b.spuCode=a.spuCode', 'left')
  1248. ->where(['a.skuCode' => $order['skuCode']])
  1249. ->field("b.creater,b.createrid,b.supplierNo")
  1250. ->findOrEmpty();
  1251. }
  1252. if(empty($goon)){
  1253. return error_show(1005,"未找到商品数据");
  1254. }
  1255. // $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1256. // if($supplier==false){
  1257. // return error_show(1005,"未找到商品供应商数据");
  1258. // }
  1259. $userCommon = new \app\admin\common\User();
  1260. $tmp = $userCommon->handle('sInfo', ['code' => $goon['supplierNo']]);
  1261. if (!isset($tmp['code']) || $tmp['code'] != 0) return json_show($tmp['code'], $tmp['message'], $tmp['data']);
  1262. $supplier = $tmp['data'];
  1263. if (empty($supplier)) return json_show(1005, "未找到商品供应商数据");
  1264. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1265. if($errorCode==''){
  1266. return error_show(1004,"参数errorCode 不能为空");
  1267. }
  1268. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1269. if(empty($error)){
  1270. return error_show(1005,"未找到退货原因数据");
  1271. }
  1272. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1273. $addr =Db::name("order_addr")->where([["orderCode","=",$orderCode],["is_del","=",0]])->select()->toArray();
  1274. if($order['is_stock']==0){
  1275. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1276. if ($ordernum == false) {
  1277. return error_show(1005, '未找到关联采购单');
  1278. }
  1279. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1280. if ($cgd == false) {
  1281. return error_show(1005, '未找到采购单数据');
  1282. }
  1283. }
  1284. if($order['is_stock']==0&&$cgd['send_status']!=1){
  1285. return error_show(1005, '采购单已发起入库');
  1286. }
  1287. $returnCode=makeNo("RN");
  1288. Db::startTrans();
  1289. try{
  1290. $in = [
  1291. "returnCode"=>$returnCode,
  1292. "orderCode"=>$orderCode,
  1293. "good_code"=>$order['good_code'],
  1294. "good_name"=>$order['good_name'],
  1295. "apply_id"=>$this->uid,
  1296. "apply_name"=>$this->uname,
  1297. "cgderid"=>$goon['createrid'],
  1298. "cgder"=>$goon['creater'],
  1299. "person"=>$supplier['person']??'',
  1300. "person_id"=>$supplier['personid']??0,
  1301. "error_code"=>$errorCode,
  1302. "num"=>$order['wsend_num'],
  1303. "total_fee"=>round($order['sale_price']*$order['wsend_num'],2),
  1304. "good_price"=>$order['sale_price'],
  1305. "platform_id"=>$order['platform_id'],
  1306. "remark"=>$remark,
  1307. "order_type"=>$order['order_type'],
  1308. "is_addr"=>count($addr)>0 ?1:0,
  1309. "status"=>$order['is_stock']==1?4:1,
  1310. "is_del"=>0,
  1311. "is_all"=>1,
  1312. "addtime"=>date("Y-m-d H:i:s"),
  1313. "updatetime"=>date("Y-m-d H:i:s")
  1314. ];
  1315. $create = Db::name("sale_return")->insert($in,true);
  1316. if($create>0){
  1317. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1318. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  1319. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1320. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname],$process);
  1321. //维护台账信息
  1322. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  1323. if(count($addr)!=0){
  1324. $inf=[];
  1325. foreach ($addr as $val){
  1326. if ($val['receipt_quantity'] == 0) continue;//当退货数量为0时,跳过
  1327. $temp=[];
  1328. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  1329. if ($send == false) {
  1330. Db::rollback();
  1331. return error_show(1004, "地址发货单未找到");
  1332. }
  1333. if($send['status']>=2){
  1334. Db::rollback();
  1335. return error_show(1005,"地址已发货");
  1336. }
  1337. if($order['is_stock']==1){
  1338. $val['is_del'] = 1;
  1339. $val['updatetime'] = date("Y-m-d H:i:s");
  1340. $addrup = Db::name("order_addr")->save($val);
  1341. if ($addrup == false) {
  1342. Db::rollback();
  1343. return error_show(1004, "地址更新失败");
  1344. }
  1345. $send['is_del'] = 1;
  1346. $send['remark'] ='全退';
  1347. $send['updatetime'] = date("Y-m-d H:i:s");
  1348. $outup = Db::name("order_out")->save($send);
  1349. if ($outup == false) {
  1350. Db::rollback();
  1351. return error_show(1004, "地址发货单更新失败");
  1352. }
  1353. //处理发货申请单流程
  1354. ProcessOrder::AddProcess(["id" => $this->uid, "nickname" => $this->uname], [
  1355. "order_type" => 'CKD',
  1356. "order_code" => $send["outCode"],//出库单号
  1357. "order_id" => $send["id"],
  1358. "order_status" => 4,//全部退货
  1359. "before_status" => $send['status'],
  1360. 'holder_id=' => $send['apply_id']
  1361. ]);
  1362. // $ordersend = Db::name("order_send")->where(["outCode" => $send['outCode']])->find();
  1363. // if($ordersend==false){
  1364. // Db::rollback();
  1365. // return error_show(1004, "发货单关联数据未找到");
  1366. // }
  1367. // $ordersend['status'] = 0;
  1368. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  1369. // $sendip = Db::name("order_send")->save($ordersend);
  1370. // if ($sendip == false) {
  1371. // Db::rollback();
  1372. // return error_show(1004, "发货单更新失败");
  1373. // }
  1374. }
  1375. $temp['returnCode']=$returnCode;
  1376. $temp['orderCode']=$orderCode;
  1377. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  1378. $temp['addrid']=$val['id'];
  1379. $temp['send_num']=$val['receipt_quantity'];
  1380. $temp['return_num']=$val['receipt_quantity'];
  1381. $temp['is_del']=0;
  1382. $temp['addtime']=date("Y-m-d H:i:s");
  1383. $temp['updatetime']=date("Y-m-d H:i:s");
  1384. $inf[]=$temp;
  1385. }
  1386. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  1387. if($inadd==0){
  1388. Db::rollback();
  1389. return error_show(1005,"退货单新建失败");
  1390. }
  1391. }
  1392. if($order['is_stock']==1){
  1393. $lor=$order['status'];
  1394. $thnum = $order['wsend_num'];
  1395. $order['send_num'] += $thnum;
  1396. $order['th_num'] += $thnum;
  1397. $order['wsend_num'] =0;
  1398. $order['status'] = 3;
  1399. $order['send_status']=3;
  1400. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  1401. $order['updatetime'] = date("Y-m-d H:i:s");
  1402. $uap = Db::name("sale")->save($order);
  1403. if ($uap == false) {
  1404. Db::rollback();
  1405. return error_show(1005, '销售单订单更新失败');
  1406. }
  1407. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname], [
  1408. "order_code" => $order["orderCode"],//出库单号
  1409. "status" => $lor,//这里的status是之前的值
  1410. "action_remark" => '',//备注
  1411. "action_type" => "status"//新建create,编辑edit,更改状态status
  1412. ], "XSQRD", $order['status'], $order);
  1413. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname], [
  1414. "order_type" => 'XSQRD',
  1415. "order_code" =>$order["orderCode"],//出库单号
  1416. "order_id" => $order["id"],
  1417. "order_status" =>$order['status'],
  1418. "before_status"=>$lor,
  1419. 'holder_id=' => $order['apply_id']
  1420. ]);
  1421. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$orderCode],["num",">",0]])->select()->toArray();
  1422. if(empty($saleinfo)) {
  1423. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1424. if ($ordernum == false) {
  1425. Db::rollback();
  1426. return error_show(1005, '未找到关联采购单');
  1427. }
  1428. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1429. if ($cgd == false) {
  1430. Db::rollback();
  1431. return error_show(1005, '未找到采购单数据');
  1432. }
  1433. $bn =makeNo("BN");
  1434. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code'=>$cgd['wsm_code'],"is_del"=>0,"status"=>1])->find();
  1435. if($stock==false){
  1436. Db::rollback();
  1437. return error_show(1005, '商品库存数据未找到');
  1438. }
  1439. $stock['usable_stock']+=$thnum;
  1440. $stock['wait_out_stock'] -=$thnum;
  1441. $stock['updatetime'] = date("Y-m-d H:i:s");
  1442. $st_up = Db::name("good_stock")->save($stock);
  1443. if ($st_up == false) {
  1444. return error_show(1005, '可售商品入库失败');
  1445. }
  1446. $yp=GoodStockInfo::AddBn($stock['id'],$bn,$thnum,$cgd['good_price']);
  1447. if($yp==false){
  1448. Db::rollback();
  1449. return error_show(1005, '商品批次退货入库失败');
  1450. }
  1451. }else{
  1452. $tempnum =$thnum;
  1453. foreach ($saleinfo as $va){
  1454. if($tempnum ==0) break;
  1455. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])->find();
  1456. if($stock==false) {
  1457. Db::rollback();
  1458. return error_show(1005, '商品库存数据未找到');
  1459. }
  1460. if($va['num']>=$tempnum) {
  1461. $tnm = $tempnum;
  1462. $va['num']-= $tempnum;
  1463. $va['th_num']+= $tempnum;
  1464. $tempnum=0;
  1465. }else{
  1466. $tnm = $va['num'];
  1467. $tempnum -=$va['num'];
  1468. $va['th_num']+= $va['num'];
  1469. $va['num'] =0;
  1470. }
  1471. $stock['usable_stock']+=$tnm;
  1472. $stock['wait_out_stock'] -=$tnm;
  1473. $stock['updatetime'] = date("Y-m-d H:i:s");
  1474. $st_up = Db::name("good_stock")->save($stock);
  1475. if ($st_up == false) {
  1476. return error_show(1005, '可售商品入库失败');
  1477. }
  1478. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  1479. if ($ps == false) {
  1480. return error_show(1005, '商品批次退货入库失败');
  1481. }
  1482. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  1483. if ($ret == false) {
  1484. return error_show(1005, '商品批次退货入库失败');
  1485. }
  1486. $va['updatetime']=date("Y-m-d H:i:s");
  1487. $sal= Db::name("sale_info")->save($va);
  1488. if ($sal == false) {
  1489. return error_show(1005, '商品批次退货入库失败');
  1490. }
  1491. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $tnm, "stock_name" => "usable_stock"];
  1492. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $tnm, "stock_name" => "wait_out_stock"];
  1493. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, 'XSTHD');
  1494. }
  1495. }
  1496. $data=[
  1497. "orderCode"=>$orderCode,
  1498. "th_type"=>1,
  1499. "th_num"=>$thnum,
  1500. "th_fee"=>round($order['sale_price']*$thnum,2),
  1501. "thCode"=>$returnCode,
  1502. "spuCode"=>$order['good_code'],
  1503. "good_name"=>$order['good_name'],
  1504. "cat_id"=>$order['cat_id'],
  1505. "apply_id"=>$this->uid,
  1506. "apply_name"=>$this->uname,
  1507. "addtime"=>date("Y-m-d H:i:s"),
  1508. "status"=>1,
  1509. "is_del"=>0
  1510. ];
  1511. $inse=Db::name("th_data")->insert($data);
  1512. if($inse==false){
  1513. Db::rollback();
  1514. return error_show(1004,"退货单更新失败");
  1515. }
  1516. }
  1517. Db::commit();
  1518. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  1519. }
  1520. Db::rollback();
  1521. return error_show(1005,"退货单新建失败");
  1522. }catch (\Exception $e){
  1523. Db::rollback();
  1524. return error_show(1005, $e->getMessage());
  1525. }
  1526. }
  1527. }