Reorder.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  5. use think\facade\Db;
  6. use think\App;
  7. use app\admin\model\ActionLog;
  8. //销售单退货
  9. class Reorder extends Base
  10. {
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. }
  15. public function create(){
  16. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  17. if($ordeCode==''){
  18. return error_show(1004,"参数orderCode 不能为空");
  19. }
  20. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  21. if(empty($order)){
  22. return error_show(1005,"未找到订单数据");
  23. }
  24. $retrun =Db::name("sale_return")->where([["orderCode","=",$ordeCode],["is_del","=",0],["status","<",4]])
  25. ->count();
  26. if($retrun>0){
  27. return error_show(1005,"存在未完成退货订单数据");
  28. }
  29. if($order['order_type']==3){
  30. $goon = Db::name("good_zixun")->where(["spuCode"=>$order['good_code'],"is_del"=>0])->find();
  31. }else {
  32. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  33. ->where(['a.skuCode' => $order['skuCode']])->field("b.creater,b.createrid,b.supplierNo")->find();
  34. }
  35. if($goon==false){
  36. return error_show(1005,"未找到商品数据");
  37. }
  38. $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  39. if($supplier==false){
  40. return error_show(1005,"未找到商品供应商数据");
  41. }
  42. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  43. if($errorCode==''){
  44. return error_show(1004,"参数errorCode 不能为空");
  45. }
  46. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  47. if(empty($error)){
  48. return error_show(1005,"未找到退货原因数据");
  49. }
  50. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  51. $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :"";
  52. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  53. if($token==''){
  54. return error_show(105,"参数token不能为空");
  55. }
  56. $user =GetUserInfo($token);
  57. if(empty($user)||$user['code']!=0){
  58. return error_show(1002,"申请人数据不存在");
  59. }
  60. $is_addr=0;
  61. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  62. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  63. $returnadr =isset($this->post['returnAddr'])&& !empty($this->post['returnAddr']) ? $this->post['returnAddr']:"";
  64. if($returnadr!=''){
  65. $thnum=array_sum(array_column($returnadr,"return_num"));
  66. $is_addr=1;
  67. }
  68. $returnCode=makeNo("RN");
  69. Db::startTrans();
  70. try{
  71. $in = [
  72. "returnCode"=>$returnCode,
  73. "orderCode"=>$ordeCode,
  74. "good_code"=>$order['good_code'],
  75. "good_name"=>$order['good_name'],
  76. "apply_id"=>$rm,
  77. "apply_name"=>$ri,
  78. "cgderid"=>$goon['createrid'],
  79. "cgder"=>$goon['creater'],
  80. "person"=>$supplier['person']??'',
  81. "person_id"=>$supplier['personid']??0,
  82. "error_code"=>$errorCode,
  83. "num"=>$thnum,
  84. "total_fee"=>round($order['sale_price']*$thnum,2),
  85. "good_price"=>$order['sale_price'],
  86. "platform_id"=>$order['platform_id'],
  87. "remark"=>$remark,
  88. "order_type"=>$order['order_type'],
  89. "is_addr"=>$is_addr,
  90. "status"=>$order['is_stock']==1?4:1,
  91. "is_del"=>0,
  92. "addtime"=>date("Y-m-d H:i:s"),
  93. "updatetime"=>date("Y-m-d H:i:s")
  94. ];
  95. $create = Db::name("sale_return")->insert($in,true);
  96. if($create>0){
  97. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  98. ActionLog::logAdd($this->post['token'],$stn,"XSTHD",$in['status'],$in);
  99. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  100. ProcessOrder::AddProcess($this->post['token'],$process);
  101. //维护台账信息
  102. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  103. if($returnadr!=""){
  104. $inf=[];
  105. foreach ($returnadr as $val){
  106. if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
  107. $temp=[];
  108. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  109. if($addrinfo==false){
  110. Db::rollback();
  111. return error_show(1005,"地址信息未找到");
  112. }
  113. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  114. if($send['status']>=2){
  115. Db::rollback();
  116. return error_show(1005,"地址已发货");
  117. }
  118. if($order['is_stock']==1){
  119. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  120. Db::rollback();
  121. return error_show(1004, "地址发货数量不足");
  122. }
  123. $addrinfo['receipt_quantity'] -= $val['return_num'];
  124. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  125. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  126. $addrup = Db::name("order_addr")->save($addrinfo);
  127. if ($addrup == false) {
  128. Db::rollback();
  129. return error_show(1004, "地址发货数量更新失败");
  130. }
  131. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  132. if ($out == false) {
  133. Db::rollback();
  134. return error_show(1004, "地址发货单数据未找到");
  135. }
  136. // if ($send!=false) {
  137. if ($out['status'] >= 2) {
  138. Db::rollback();
  139. return error_show(1004, "地址发货单已发货");
  140. }
  141. if ($out['send_num'] < $val['return_num']) {
  142. Db::rollback();
  143. return error_show(1004, "地址发货单发货数量不足");
  144. }
  145. $out['send_num'] -= $val['return_num'];
  146. $out['is_del'] = $out['send_num']==0?1:0;
  147. $out['updatetime'] = date("Y-m-d H:i:s");
  148. $outup = Db::name("order_out")->save($out);
  149. if ($outup == false) {
  150. Db::rollback();
  151. return error_show(1004, "地址发货单更新失败");
  152. }
  153. // $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  154. // if ($ordersend['send_num'] < $val['return_num']) {
  155. // Db::rollback();
  156. // return error_show(1004, "发货单发货数量不足");
  157. // }
  158. // $ordersend['send_num'] -= $val['return_num'];
  159. // $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  160. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  161. // $sendip = Db::name("order_send")->save($ordersend);
  162. // if ($sendip == false) {
  163. // Db::rollback();
  164. // return error_show(1004, "发货单更新失败");
  165. // }
  166. // }
  167. }
  168. $temp['returnCode']=$returnCode;
  169. $temp['orderCode']=$ordeCode;
  170. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  171. $temp['addrid']=$val['id'];
  172. $temp['send_num']=$addrinfo['receipt_quantity'];
  173. $temp['return_num']=$val['return_num'];
  174. $temp['is_del']=0;
  175. $temp['addtime']=date("Y-m-d H:i:s");
  176. $temp['updatetime']=date("Y-m-d H:i:s");
  177. $inf[]=$temp;
  178. }
  179. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  180. if($inadd==0){
  181. Db::rollback();
  182. return error_show(1005,"退货单新建失败");
  183. }
  184. }
  185. if($order['is_stock']==1){
  186. if ($order['wsend_num'] < $thnum) {
  187. Db::rollback();
  188. return error_show(1004, "销售单未发货数量不足退货");
  189. }
  190. $lor=$order['status'] ;
  191. $order['wsend_num'] -= $thnum;
  192. $order['send_num'] += $thnum;
  193. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  194. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  195. $order['th_num'] += $thnum;
  196. if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){
  197. $order['status']=3;
  198. }
  199. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  200. $order['updatetime'] = date("Y-m-d H:i:s");
  201. $uap = Db::name("sale")->save($order);
  202. if ($uap == false) {
  203. Db::rollback();
  204. return error_show(1005, '销售单订单更新失败');
  205. }
  206. ActionLog::logAdd($this->post['token'], [
  207. "order_code" => $order["orderCode"],//出库单号
  208. "status" => $lor,//这里的status是之前的值
  209. "action_remark" => '',//备注
  210. "action_type" => "status"//新建create,编辑edit,更改状态status
  211. ], "XSQRD", $order['status'], $order);
  212. ProcessOrder::AddProcess($this->post['token'], [
  213. "order_type" => 'XSQRD',
  214. "order_code" =>$order["orderCode"],//出库单号
  215. "order_id" => $order["id"],
  216. "order_status" =>$order['status'],"before_status"=>$lor
  217. ]);
  218. // $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  219. // if ($ordernum == false) {
  220. // Db::rollback();
  221. // return error_show(1005, '未找到关联采购单');
  222. // }
  223. // $ordernum['send_num'] -= $thnum;
  224. // $orderup = Db::name("order_num")->save($ordernum);
  225. // if ($orderup == false) {
  226. // Db::rollback();
  227. // return error_show(1005, '关联数据更新失败');
  228. // }
  229. // $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  230. // if ($cgd == false) {
  231. // Db::rollback();
  232. // return error_show(1005, '未找到采购单数据');
  233. // }
  234. // $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  235. // $cgd['th_num'] += $thnum;
  236. // $cgd['updatetime'] = date("Y-m-d H:i:s");
  237. // $cgdup = Db::name("purchease_order")->save($cgd);
  238. // if ($cgdup == false) {
  239. // Db::rollback();
  240. // return error_show(1005, '采购单数据更新失败');
  241. // }
  242. // if ($cgd['bkcode'] != "") {
  243. // $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "order_source" =>0, "is_del" => 0])
  244. // ->find();
  245. // if ($bk == false) {
  246. // Db::rollback();
  247. // return error_show(1005, '未找到备库单数据');
  248. // }
  249. // $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  250. // if ($orderbk == false) {
  251. // Db::rollback();
  252. // return error_show(1005, '备库单未完全入库');
  253. // }
  254. // $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" =>
  255. // 1, "is_del" => 0])->where("order_source","<>",0)->field("sum(send_num)-sum(th_num) as num")
  256. // ->find();
  257. //
  258. // $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  259. // $orderbk['merge_num'] = $merge_num['num'];
  260. // $orderbk['updatetime'] = date("Y-m-d H:i:s");
  261. // $orderbkup = Db::name("order_bk")->save($orderbk);
  262. // if ($orderbkup == false) {
  263. // Db::rollback();
  264. // return error_show(1005, '备库单库存数据释放失败');
  265. // }
  266. // }
  267. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$ordeCode],["num",">",0]])->select()
  268. ->toArray();
  269. if(empty($saleinfo)) {
  270. Db::rollback();
  271. return error_show(1005, '商品批次数据未找到');
  272. }
  273. $tempnum =$thnum;
  274. foreach ( $saleinfo as $va){
  275. if($tempnum ==0) break;
  276. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])
  277. ->find();
  278. if($stock==false) {
  279. Db::rollback();
  280. return error_show(1005, '商品库存数据未找到');
  281. }
  282. if($va['num']>=$tempnum) {
  283. $tnm = $tempnum;
  284. $va['num']-= $tempnum;
  285. $tempnum=0;
  286. }else{
  287. $tnm = $va['num'];
  288. $tempnum -=$va['num'];
  289. $va['num'] =0;
  290. }
  291. $stock['usable_stock']+=$tnm;
  292. $stock['wait_out_stock'] -=$tnm;
  293. $stock['updatetime'] = date("Y-m-d H:i:s");
  294. $st_up = Db::name("good_stock")->save($stock);
  295. if ($st_up == false) {
  296. return error_show(1005, '可售商品入库失败');
  297. }
  298. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  299. if ($ps == false) {
  300. return error_show(1005, '商品批次退货入库失败');
  301. }
  302. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  303. if ($ret == false) {
  304. return error_show(1005, '商品批次退货入库失败');
  305. }
  306. $va['updatetime']=date("Y-m-d H:i:s");
  307. $sal= Db::name("sale_info")->save($va);
  308. if ($sal == false) {
  309. return error_show(1005, '商品批次退货入库失败');
  310. }
  311. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  312. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  313. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  314. }
  315. $data=[
  316. "orderCode"=>$ordeCode,
  317. "th_type"=>1,
  318. "th_num"=>$thnum,
  319. "th_fee"=>round($order['sale_price']*$thnum,2),
  320. "thCode"=>$returnCode,
  321. "spuCode"=>$order['good_code'],
  322. "good_name"=>$order['good_name'],
  323. "cat_id"=>$order['cat_id'],
  324. "apply_id"=>$rm,
  325. "apply_name"=>$ri,
  326. "addtime"=>date("Y-m-d H:i:s"),
  327. "status"=>1,
  328. "is_del"=>0
  329. ];
  330. $inse=Db::name("th_data")->insert($data);
  331. if($inse==false){
  332. Db::rollback();
  333. return error_show(1004,"退货单更新失败");
  334. }
  335. }
  336. Db::commit();
  337. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  338. }
  339. Db::rollback();
  340. return error_show(1005,"退货单新建失败");
  341. }catch (\Exception $e){
  342. Db::rollback();
  343. return error_show(1005,$e->getMessage());
  344. }
  345. }
  346. public function list(){
  347. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  348. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  349. $where =[['sr.is_del',"=",0]];
  350. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  351. if($bkcode!=""){
  352. $where[]=['sr.returnCode',"like", "%{$bkcode}%"];
  353. }
  354. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  355. if($status!==""){
  356. $where[]=['sr.status',"=", $status];
  357. }
  358. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  359. :"";
  360. if($orderCode!=""){
  361. $where[]=['sr.orderCode',"like", "%{$orderCode}%"];
  362. }
  363. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  364. :"";
  365. if($apply_name!=""){
  366. $where[]=['sr.apply_name',"like", "%{$apply_name}%"];
  367. }
  368. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  369. if($start!==""){
  370. $where[]=['sr.addtime',">=", $start.' 00:00:00'];
  371. }
  372. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  373. if($end!==""){
  374. $where[]=['sr.addtime',"<=", $end.' 23:59:59'];
  375. }
  376. $role=$this->checkRole();
  377. $condition='';
  378. if(!empty($role['write']) && $this->uid!=""){
  379. // $where[]=["sr.apply_id","in",$role['write']];
  380. $condition .="sr.cgderid = {$this->uid} or sr.apply_id in (".implode(',',$role['write']).") or sr.person_id={$this->uid}";
  381. }
  382. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  383. if ($company_name !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($company_name)];
  384. $count=Db::name("sale_return")->alias('sr')->where($where)->where($condition)->count();
  385. $total = ceil($count/$size);
  386. $page = $total>=$page ? $page :$total;
  387. $list = Db::name("sale_return")
  388. ->alias('sr')
  389. ->field('sr.*,u.itemid')
  390. ->leftJoin("depart_user u", "u.uid=sr.apply_id AND u.is_del=0")
  391. ->where($where)
  392. ->where($condition)
  393. ->order("addtime desc")
  394. ->page($page,$size)
  395. ->cursor();
  396. // echo Db::name("sale_return")->getLastSql();
  397. $data=[];
  398. foreach ($list as $value){
  399. $value['error_msg']='';
  400. if($value['error_code']!=''){
  401. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  402. $value['error_msg']= isset($error['result'])?$error['result']:"";
  403. }
  404. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  405. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  406. $value['return_total'] =$value['sale_price']*$value['num'] ;
  407. $value['total_num'] =$order['good_num'] ;
  408. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  409. $data[]=$value ;
  410. }
  411. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  412. }
  413. public function info(){
  414. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  415. :"";
  416. if($code==""){
  417. return error_show(1004,"参数returnCode不能为空");
  418. }
  419. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  420. if(empty($info)){
  421. return error_show(1004,"未找到退货数据");
  422. }
  423. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  424. if($orderinfo['order_type']==3){
  425. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  426. }else {
  427. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  428. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  429. }
  430. if ($goon==false) {
  431. return error_show(1003, "未找到商品数据");
  432. }
  433. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  434. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  435. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  436. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  437. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  438. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  439. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  440. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  441. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  442. $info['return_total'] = $info['sale_price']*$info['num'] ;
  443. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  444. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  445. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  446. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  447. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  448. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  449. $info['customer_name']='';
  450. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  451. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  452. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  453. }
  454. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  455. $info['supplier_name']='';
  456. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  457. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  458. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  459. }
  460. $info['platform_name']='';
  461. $info['platform_id']=$orderinfo['platform_id'];
  462. if($orderinfo['platform_id']!=0){
  463. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  464. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  465. }
  466. $info['cgd_wsend']="";
  467. $info['cgd_send']="";
  468. $info['cgd_total']="";
  469. if($orderinfo['order_type']==2){
  470. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  471. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  472. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  473. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  474. }
  475. $info['error_msg']='';
  476. if($info['error_code']!=''){
  477. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  478. $info['error_msg']= isset($error['result'])?$error['result']:"";
  479. }
  480. if($info['return_wsm']!=""){
  481. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  482. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  483. }
  484. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  485. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  486. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  487. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  488. $wsm=[];
  489. if(!empty($wsm_return)){
  490. foreach ($wsm_return as $value){
  491. $value['wsm_name']="";
  492. $value['wsm_supplier']="";
  493. $value['wsm_supplierNo']="";
  494. if($value['wsm_code']!=""){
  495. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  496. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  497. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  498. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  499. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  500. }
  501. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  502. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  503. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  504. $value['wsm_send'] = $send ?? 0;
  505. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  506. $wsm[]=$value;
  507. }
  508. }
  509. $info['wsminfo']=$wsm;
  510. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  511. $addrinfo=[];
  512. if(!empty($addr)){
  513. foreach ( $addr as $value){
  514. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  515. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  516. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  517. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  518. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  519. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  520. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  521. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  522. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  523. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode'],"is_del"=>0])->where("status",">=",2)->sum("send_num");
  524. $value['addr_send'] = $send ?? 0;
  525. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  526. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  527. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  528. $addrinfo[]=$value;
  529. }
  530. }
  531. $info['addrinfo']=$addrinfo;
  532. $info['can']=$int;
  533. return app_show(0,"获取成功",$info);
  534. }
  535. /**
  536. * @return \think\response\Json|void
  537. * @throws \think\db\exception\DataNotFoundException
  538. * @throws \think\db\exception\DbException
  539. * @throws \think\db\exception\ModelNotFoundException
  540. */
  541. public function delete(){
  542. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  543. :"";
  544. if($code==""){
  545. return error_show(1004,"参数returnCode不能为空");
  546. }
  547. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  548. if(empty($info)){
  549. return error_show(1004,"未找到退货数据");
  550. }
  551. $info["is_del"]=1;
  552. $info["updatetime"]=date("Y-m-d H:i:s");
  553. $del = Db::name("sale_return")->save($info);
  554. if($del){
  555. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  556. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",0,$ste);
  557. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  558. ProcessOrder::workdel($process);
  559. return app_show(0,"删除成功");
  560. }else{
  561. return error_show(1004,"删除失败");
  562. }
  563. }
  564. /**审核
  565. * @return \think\response\Json|void
  566. * @throws \think\db\exception\DataNotFoundException
  567. * @throws \think\db\exception\DbException
  568. * @throws \think\db\exception\ModelNotFoundException
  569. */
  570. public function exam(){
  571. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']):"";
  572. if($code==""){
  573. return error_show(1004,"参数returnCode不能为空");
  574. }
  575. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  576. if(empty($info)){
  577. return error_show(1004,"未找到退货数据");
  578. }
  579. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  580. if($status===""){
  581. return error_show(1004,"参数status不能为空");
  582. }
  583. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  584. $var = $info['status'];
  585. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  586. if($orderinfo==false){
  587. return error_show(1004,"未找到订单数据");
  588. }
  589. if($status==4){
  590. if($info['is_addr']==1){
  591. $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select()
  592. ->toArray();
  593. if(empty($addr)){
  594. return error_show(1004,"未找到发货单地址数据");
  595. }
  596. }
  597. }
  598. if($status==3){
  599. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  600. if($is_th===""){
  601. return error_show(1004,"参数is_th不能为空");
  602. }
  603. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  604. if($is_th==0){
  605. if($return_wsm===""){
  606. return error_show(1004,"参数return_wsm 不能为空");
  607. }
  608. $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  609. if($wsmcode==false){
  610. return error_show(1004,"为找到仓库数据");
  611. }
  612. }
  613. $info['return_wsm'] =$return_wsm ;
  614. $info['is_th'] =$is_th ;
  615. }
  616. Db::startTrans();
  617. try{
  618. $temp= $info['status'];
  619. $info['status'] =$status;
  620. $remark!=""? $info['remark'] =$remark:"";
  621. $info["updatetime"]=date("Y-m-d H:i:s");
  622. $up = Db::name("sale_return")->save($info);
  623. if($up) {
  624. //如果是节点2(待专员审核),要将待办数据推给供应商负责人
  625. 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']];
  626. else $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type" => "XSTHD", 'before_status' => $temp, 'holder_id' => $info['apply_id']];
  627. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  628. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  629. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $ste, "XSTHD", $status, $info);
  630. if ($status == 4) {
  631. if ($info['is_addr'] == 1) {
  632. if (isset($addr) && !empty($addr)) {
  633. foreach ($addr as $value) {
  634. $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find();
  635. if ($addrinfo == false) {
  636. Db::rollback();
  637. return error_show(1004, "地址数据未找到");
  638. }
  639. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  640. Db::rollback();
  641. return error_show(1004, "地址发货数量不足");
  642. }
  643. $addrinfo['receipt_quantity'] -= $value['return_num'];
  644. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  645. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  646. $addrup = Db::name("order_addr")->save($addrinfo);
  647. if ($addrup == false) {
  648. Db::rollback();
  649. return error_show(1004, "地址发货数量更新失败");
  650. }
  651. if ($value['outCode'] != "") {
  652. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->find();
  653. if ($out == false) {
  654. Db::rollback();
  655. return error_show(1004, "地址发货单数据未找到");
  656. }
  657. if ($out['status'] >= 2) {
  658. Db::rollback();
  659. return error_show(1004, "地址发货单已发货");
  660. }
  661. if ($out['send_num'] < $value['return_num']) {
  662. Db::rollback();
  663. return error_show(1004, "地址发货单发货数量不足");
  664. }
  665. $out['send_num'] -= $value['return_num'];
  666. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  667. $out['updatetime'] = date("Y-m-d H:i:s");
  668. $outup = Db::name("order_out")->save($out);
  669. if ($outup == false) {
  670. Db::rollback();
  671. return error_show(1004, "地址发货单更新失败");
  672. }
  673. }
  674. }
  675. }
  676. }
  677. if ($orderinfo['wsend_num'] < $info['num']) {
  678. Db::rollback();
  679. return error_show(1004, "销售单未发货数量不足退货");
  680. }
  681. $lor=$orderinfo['status'];
  682. $orderinfo['wsend_num'] -= $info['num'];
  683. $orderinfo['send_num'] += $info['num'];
  684. $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1);
  685. $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2);
  686. if ($orderinfo['is_stock'] == 1) {
  687. $orderinfo['th_num'] += $info['num'];
  688. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  689. $orderinfo['status']=3;
  690. }
  691. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  692. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  693. $uap = Db::name("sale")->save($orderinfo);
  694. if ($uap == false) {
  695. Db::rollback();
  696. return error_show(1005, '销售单订单更新失败');
  697. }
  698. }else{
  699. if ($info['is_th'] == 1){
  700. $orderinfo['th_num'] += $info['num'];
  701. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  702. $orderinfo['status']=3;
  703. }
  704. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  705. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  706. $uap = Db::name("sale")->save($orderinfo);
  707. if ($uap == false) {
  708. Db::rollback();
  709. return error_show(1005, '销售单订单更新失败');
  710. }
  711. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  712. if ($ordernum == false) {
  713. Db::rollback();
  714. return error_show(1005, '未找到关联采购单');
  715. }
  716. $ordernum['send_num'] -= $info['num'];
  717. $orderup = Db::name("order_num")->save($ordernum);
  718. if ($orderup == false) {
  719. Db::rollback();
  720. return error_show(1005, '关联数据更新失败');
  721. }
  722. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  723. if ($cgd == false) {
  724. Db::rollback();
  725. return error_show(1005, '未找到采购单数据');
  726. }
  727. if( $info['is_all']==1 && $cgd['send_status']!=1){
  728. Db::rollback();
  729. return error_show(1005, '采购单已入库无法全部退货');
  730. }
  731. $lor=$cgd['status'];
  732. if($cgd['wsend_num']<$info['num']){
  733. $cgd['send_num'] += $cgd['wsend_num']==0?0 :($info['num']-$cgd['wsend_num']);
  734. $cgd['wsend_num']=0;
  735. }else{
  736. $cgd['wsend_num'] -= $info['num'];
  737. $cgd['send_num'] += $info['num'];
  738. }
  739. $cgd['status'] = $cgd['wsend_num']==0?2:($cgd['send_num']==0?0:1);
  740. $cgd['send_status'] =$cgd['wsend_num']==0?3:($cgd['send_num']==0?1:2);
  741. $cgd['th_num'] += $info['num'];
  742. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  743. $cgd['status']=4;
  744. }
  745. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  746. $cgd['updatetime'] = date("Y-m-d H:i:s");
  747. $cgdup = Db::name("purchease_order")->save($cgd);
  748. if ($cgdup == false) {
  749. Db::rollback();
  750. return error_show(1005, '采购单数据更新失败');
  751. }
  752. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  753. if ($stock==false) {
  754. Db::rollback();
  755. return error_show(1005, '商品仓库未找到');
  756. }
  757. if($stock['wait_in_stock']<$info['num']){
  758. $stock['wait_in_stock']=0;
  759. if( $stock['usable_stock']>$info['num']-$stock['wait_in_stock']){
  760. $stock['usable_stock'] -= $info['num']-$stock['wait_in_stock'];
  761. }else{
  762. $stock['usable_stock'] = 0;
  763. $stock['wait_out_stock']-= $info['num']-$stock['wait_in_stock']- $stock['usable_stock'] ;
  764. }
  765. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  766. }else{
  767. $stock['wait_in_stock']-=$info['num'];
  768. }
  769. $stock['updatetime'] = date("Y-m-d H:i:s");
  770. $st_up = Db::name("good_stock")->save($stock);
  771. if ($st_up == false) {
  772. Db::rollback();
  773. return error_show(1005, '可售商品入库失败');
  774. }
  775. } else {
  776. $orderinfo['th_num'] += $info['num'];
  777. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  778. $orderinfo['status']=3;
  779. }
  780. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  781. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  782. $uap = Db::name("sale")->save($orderinfo);
  783. if ($uap == false) {
  784. Db::rollback();
  785. return error_show(1005, '销售单订单更新失败');
  786. }
  787. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  788. if ($ordernum == false) {
  789. Db::rollback();
  790. return error_show(1005, '未找到关联采购单');
  791. }
  792. $ordernum['send_num'] -= $info['num'];
  793. $ordernum['wsend_num'] = $info['num'];
  794. $orderup = Db::name("order_num")->save($ordernum);
  795. if ($orderup == false) {
  796. Db::rollback();
  797. return error_show(1005, '关联数据更新失败');
  798. }
  799. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  800. if ($cgd == false) {
  801. Db::rollback();
  802. return error_show(1005, '未找到采购单数据');
  803. }
  804. $stock = Db::name("good_stock")->where(["spuCode" => $orderinfo['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  805. if ($stock==false) {
  806. Db::rollback();
  807. return error_show(1005, '商品仓库未找到');
  808. }
  809. if($info['is_all']==1){
  810. if($stock['wait_in_stock']<$info['num']){
  811. Db::rollback();
  812. return error_show(1005, '商品可用库存不足退回数量');
  813. }else{
  814. $stock['wait_in_stock']-= $info['num'];
  815. }
  816. }else{
  817. if($stock['usable_stock']+$stock['wait_out_stock']<$info['num']){
  818. Db::rollback();
  819. return error_show(1005, '商品可用库存不足退回数量');
  820. }else{
  821. if($stock['usable_stock']>$info['num']){
  822. $stock['usable_stock'] -= $info['num'];
  823. }else{
  824. $stock['usable_stock'] = 0;
  825. $stock['wait_out_stock']-= $info['num']- $stock['usable_stock'] ;
  826. }
  827. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  828. }
  829. }
  830. $stock['updatetime'] = date("Y-m-d H:i:s");
  831. $st_up = Db::name("good_stock")->save($stock);
  832. if ($st_up == false) {
  833. Db::rollback();
  834. return error_show(1005, '可售商品入库失败');
  835. }
  836. }
  837. }
  838. ActionLog::logAdd($this->post['token'], [
  839. "order_code" => $orderinfo["orderCode"],//出库单号
  840. "status" => $lor,//这里的status是之前的值
  841. "action_remark" => '',//备注
  842. "action_type" => "status"//新建create,编辑edit,更改状态status
  843. ], "XSQRD", $orderinfo['status'], $orderinfo);
  844. ProcessOrder::AddProcess($this->post['token'], [
  845. "order_type" => 'XSQRD',
  846. "order_code" =>$orderinfo["orderCode"],//出库单号
  847. "order_id" => $orderinfo["id"],
  848. "order_status" =>$orderinfo['status'],"before_status"=>$lor
  849. ]);
  850. //对应采购单也要处理
  851. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  852. "order_type" => 'CGD',
  853. "order_code" => $cgd['cgdNo'],
  854. "order_id" => $cgd["id"],
  855. "order_status" => $cgd['status'],
  856. "before_status" => $lor,
  857. 'holder_id' => Db::name('supplier')->where(['code' => $cgd['supplierNo'], 'is_del' => 0])->value('personid', 0)
  858. ]);
  859. if($orderinfo['is_stock'] == 1 || $info['is_th'] == 0){
  860. $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm'];
  861. if ($wsmcode == "") {
  862. Db::rollback();
  863. return error_show(1005, '未找到退货仓库');
  864. }
  865. //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存
  866. $spuCode= $info['good_code'];
  867. if($orderinfo['order_type']!==1){
  868. if($orderinfo['order_type']==3){
  869. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  870. $isZx=1;
  871. }else {
  872. $goon = Db::name('good_basic')->where(['spuCode' => $orderinfo['good_code']])->find();
  873. $isZx=2;
  874. }
  875. $spuCode = $this->CheckGood($goon,$isZx,$code);
  876. }
  877. $stock = Db::name("good_stock")->where(["spuCode" => $spuCode, 'wsm_code' => $wsmcode])->find();
  878. if (empty($stock)) {
  879. $stock = [
  880. "spuCode" => $spuCode,
  881. "wsm_code" => $wsmcode,
  882. "usable_stock" => 0,
  883. "wait_out_stock" => 0,
  884. "wait_in_stock" => 0,
  885. "total_stock" => 0,
  886. "addtime" => date("Y-m-d H:i:s"),
  887. "updatetime" => date("Y-m-d H:i:s"),
  888. ];
  889. }
  890. $stock['usable_stock'] += $info['num'];
  891. $stock['updatetime'] = date("Y-m-d H:i:s");
  892. $st_up = Db::name("good_stock")->save($stock);
  893. if ($st_up == false) {
  894. Db::rollback();
  895. return error_show(1005, '可售商品入库失败');
  896. }
  897. $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  898. $sabebn =Db::name("sale_info")->where(["orderCode"=>$orderinfo["orderCode"]])->select()->toArray();
  899. if(!empty($sabebn)){
  900. $total_num =$info['num'];
  901. foreach ($sabebn as $ve){
  902. $tempnum=0;
  903. if ($total_num==0) break;
  904. if($total_num>=$ve['num']){
  905. $tempnum = $ve['num'];
  906. $total_num-=$ve['num'];
  907. $ve['num']=0;
  908. }else{
  909. $tempnum = $total_num;
  910. $total_num=0;
  911. $ve['num']-=$total_num;
  912. }
  913. $bnin=GoodStockInfo::AddBn($stockid,$ve['bnCode'],$tempnum,$ve['origin_price']);
  914. if($bnin==false){
  915. Db::rollback();
  916. return error_show(1005, '可售商品Bn库存数入库失败');
  917. }
  918. $ve['updatetime']=date("Y-m-d H:i:s");
  919. $up=Db::name("sale_info")->save($ve);
  920. if($up==false){
  921. Db::rollback();
  922. return error_show(1005, '可售商品Bn库存数入库失败');
  923. }
  924. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  925. if($bnin==false){
  926. Db::rollback();
  927. return error_show(1005, '可售商品Bn库存数入库失败');
  928. }
  929. }
  930. }else{
  931. if($orderinfo['order_type']!=1){
  932. $bn=makeNo("BN");
  933. $bnin=GoodStockInfo::AddBn($stockid,$bn,$info['num'],$cgd['good_price']??0);
  934. if($bnin==false){
  935. Db::rollback();
  936. return error_show(1005, '可售商品Bn库存数入库失败');
  937. }
  938. }
  939. }
  940. $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => $stockid, "type" => 1, 'stock'=> $info['num'], "stock_name" => "usable_stock"];
  941. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  942. }
  943. $data=[
  944. "orderCode"=>$info['orderCode'],
  945. "th_type"=>1,
  946. "th_num"=>$info['num'],
  947. "th_fee"=>round($info['num']*$orderinfo['sale_price'],2),
  948. "thCode"=>$info['returnCode'],
  949. "spuCode"=>$orderinfo['good_code'],
  950. "good_name"=>$orderinfo['good_name'],
  951. "cat_id"=>$orderinfo['cat_id'],
  952. "apply_id"=>$info['apply_id'],
  953. "apply_name"=>$info['apply_name'],
  954. "addtime"=>date("Y-m-d H:i:s"),
  955. "status"=>1,
  956. "is_del"=>0
  957. ];
  958. $inse=Db::name("th_data")->insert($data);
  959. if($inse==false){
  960. Db::rollback();
  961. return error_show(1004,"退货单更新失败");
  962. }
  963. }
  964. }
  965. Db::commit();
  966. return app_show(0,"更新成功");
  967. }catch (\Exception $e){
  968. Db::rollback();
  969. return error_show(1004,$e->getMessage()."|".$e->getLine());
  970. }
  971. }
  972. public function zxcreate(){
  973. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  974. if($ordeCode==''){
  975. return error_show(1004,"参数orderCode 不能为空");
  976. }
  977. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  978. if(empty($order)){
  979. return error_show(1005,"未找到订单数据");
  980. }
  981. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  982. if($errorCode==''){
  983. return error_show(1004,"参数errorCode 不能为空");
  984. }
  985. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  986. if(empty($error)){
  987. return error_show(1005,"未找到退货原因数据");
  988. }
  989. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  990. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  991. if($token==''){
  992. return error_show(105,"参数token不能为空");
  993. }
  994. $user =GetUserInfo($token);
  995. if(empty($user)||$user['code']!=0){
  996. return error_show(1002,"申请人数据不存在");
  997. }
  998. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  999. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  1000. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  1001. if($num==''){
  1002. return error_show(1005,"参数num不能为空");
  1003. }
  1004. if($order['wsend_num']<$num){
  1005. return error_show(1002,"仓库未发货数量不足退货");
  1006. }
  1007. $returnCode=makeNo("RS");
  1008. Db::startTrans();
  1009. try{
  1010. $in = [
  1011. "returnCode"=>$returnCode,
  1012. "orderCode"=>$ordeCode,
  1013. "good_code"=>$order['good_code'],
  1014. "good_name"=>$order['good_name'],
  1015. "apply_id"=>$rm,
  1016. "apply_name"=>$ri,
  1017. "error_code"=>$errorCode,
  1018. "num"=>$num,
  1019. "remark"=>$remark,
  1020. "order_type"=>2,
  1021. "status"=>0,
  1022. "is_del"=>0,
  1023. "addtime"=>date("Y-m-d H:i:s"),
  1024. "updatetime"=>date("Y-m-d H:i:s")
  1025. ];
  1026. $create = Db::name("sale_return")->insert($in,true);
  1027. if($create>0) {
  1028. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1029. ProcessOrder::AddProcess($this->post['token'],$process);
  1030. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1031. "action_type"=>"create"];
  1032. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in);
  1033. //维护台账记录
  1034. // Db::name('standing_book')
  1035. // ->where('ordeCode', $ordeCode)
  1036. // ->update([
  1037. // 'returnGoodCode' => $returnCode,
  1038. // 'updatetime' => date("Y-m-d H:i:s")
  1039. // ]);
  1040. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  1041. if ($order['send_type'] == 1) {
  1042. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1043. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1044. if(!empty($wsend)){
  1045. foreach ($wsend as $value){
  1046. $tt = $value['status'];
  1047. $value['status']=0;
  1048. $value['updatetime']=date("Y-m-d H:i:s");
  1049. $up =Db::name("order_out")->save($value);
  1050. if($up){
  1051. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt,'holder_id' => $value['apply_id']];
  1052. ProcessOrder::AddProcess($this->post['token'],$process);
  1053. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1054. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  1055. }else{
  1056. Db::rollback();
  1057. return error_show(1005,"退货单新建失败");
  1058. }
  1059. }
  1060. }
  1061. }
  1062. Db::commit();
  1063. return error_show(0,"退货单新建成功");
  1064. }
  1065. Db::rollback();
  1066. return error_show(1005,"退货单新建失败");
  1067. }catch (\Exception $e){
  1068. Db::rollback();
  1069. return error_show(1005,$e->getMessage());
  1070. }
  1071. }
  1072. public function allReturn(){
  1073. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''? trim($this->post['orderCode']):"";
  1074. if($orderCode==''){
  1075. return error_show(1004,"参数orderCode 不能为空");
  1076. }
  1077. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  1078. if(empty($order)){
  1079. return error_show(1005,"未找到订单数据");
  1080. }
  1081. if($order['wsend_num']!=$order['good_num']){
  1082. return error_show(1005,"订单未发货数量与总数不等,无法全退");
  1083. }
  1084. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","<",4]])->count();
  1085. if($retrun>0){
  1086. return error_show(1005,"存在退货订单数据");
  1087. }
  1088. if($order['order_type']==3){
  1089. $goon = Db::name("good_zixun")->where(["spuCode"=>$order['good_code'],"is_del"=>0])->find();
  1090. }else {
  1091. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1092. ->where(['a.skuCode' => $order['skuCode']])->field("b.creater,b.createrid,b.supplierNo")->find();
  1093. }
  1094. if($goon==false){
  1095. return error_show(1005,"未找到商品数据");
  1096. }
  1097. $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1098. if($supplier==false){
  1099. return error_show(1005,"未找到商品供应商数据");
  1100. }
  1101. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1102. if($errorCode==''){
  1103. return error_show(1004,"参数errorCode 不能为空");
  1104. }
  1105. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1106. if(empty($error)){
  1107. return error_show(1005,"未找到退货原因数据");
  1108. }
  1109. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1110. $addr =Db::name("order_addr")->where([["orderCode","=",$orderCode],["is_del","=",0]])->select()->toArray();
  1111. if($order['is_stock']==0){
  1112. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1113. if ($ordernum == false) {
  1114. return error_show(1005, '未找到关联采购单');
  1115. }
  1116. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1117. if ($cgd == false) {
  1118. return error_show(1005, '未找到采购单数据');
  1119. }
  1120. }
  1121. if($order['is_stock']==0&&$cgd['send_status']!=1){
  1122. return error_show(1005, '采购单已发起入库');
  1123. }
  1124. $returnCode=makeNo("RN");
  1125. Db::startTrans();
  1126. try{
  1127. $in = [
  1128. "returnCode"=>$returnCode,
  1129. "orderCode"=>$orderCode,
  1130. "good_code"=>$order['good_code'],
  1131. "good_name"=>$order['good_name'],
  1132. "apply_id"=>$this->uid,
  1133. "apply_name"=>$this->uname,
  1134. "cgderid"=>$goon['createrid'],
  1135. "cgder"=>$goon['creater'],
  1136. "person"=>$supplier['person']??'',
  1137. "person_id"=>$supplier['personid']??0,
  1138. "error_code"=>$errorCode,
  1139. "num"=>$order['wsend_num'],
  1140. "total_fee"=>round($order['sale_price']*$order['wsend_num'],2),
  1141. "good_price"=>$order['sale_price'],
  1142. "platform_id"=>$order['platform_id'],
  1143. "remark"=>$remark,
  1144. "order_type"=>$order['order_type'],
  1145. "is_addr"=>count($addr)>0 ?1:0,
  1146. "status"=>$order['is_stock']==1?4:1,
  1147. "is_del"=>0,
  1148. "is_all"=>1,
  1149. "addtime"=>date("Y-m-d H:i:s"),
  1150. "updatetime"=>date("Y-m-d H:i:s")
  1151. ];
  1152. $create = Db::name("sale_return")->insert($in,true);
  1153. if($create>0){
  1154. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1155. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  1156. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1157. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname],$process);
  1158. //维护台账信息
  1159. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  1160. if(count($addr)!=0){
  1161. $inf=[];
  1162. foreach ($addr as $val){
  1163. if ($val['receipt_quantity'] == 0) continue;//当退货数量为0时,跳过
  1164. $temp=[];
  1165. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  1166. if ($send == false) {
  1167. Db::rollback();
  1168. return error_show(1004, "地址发货单未找到");
  1169. }
  1170. if($send['status']>=2){
  1171. Db::rollback();
  1172. return error_show(1005,"地址已发货");
  1173. }
  1174. if($order['is_stock']==1){
  1175. $val['is_del'] = 1;
  1176. $val['updatetime'] = date("Y-m-d H:i:s");
  1177. $addrup = Db::name("order_addr")->save($val);
  1178. if ($addrup == false) {
  1179. Db::rollback();
  1180. return error_show(1004, "地址更新失败");
  1181. }
  1182. $send['is_del'] = 1;
  1183. $send['updatetime'] = date("Y-m-d H:i:s");
  1184. $outup = Db::name("order_out")->save($send);
  1185. if ($outup == false) {
  1186. Db::rollback();
  1187. return error_show(1004, "地址发货单更新失败");
  1188. }
  1189. //处理发货申请单流程
  1190. ProcessOrder::AddProcess(["id" => $this->uid, "nickname" => $this->uname], [
  1191. "order_type" => 'CKD',
  1192. "order_code" => $send["outCode"],//出库单号
  1193. "order_id" => $send["id"],
  1194. "order_status" => 4,//全部退货
  1195. "before_status" => $send['status'],
  1196. 'holder_id=' => $send['apply_id']
  1197. ]);
  1198. // $ordersend = Db::name("order_send")->where(["outCode" => $send['outCode']])->find();
  1199. // if($ordersend==false){
  1200. // Db::rollback();
  1201. // return error_show(1004, "发货单关联数据未找到");
  1202. // }
  1203. // $ordersend['status'] = 0;
  1204. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  1205. // $sendip = Db::name("order_send")->save($ordersend);
  1206. // if ($sendip == false) {
  1207. // Db::rollback();
  1208. // return error_show(1004, "发货单更新失败");
  1209. // }
  1210. }
  1211. $temp['returnCode']=$returnCode;
  1212. $temp['orderCode']=$orderCode;
  1213. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  1214. $temp['addrid']=$val['id'];
  1215. $temp['send_num']=$val['receipt_quantity'];
  1216. $temp['return_num']=$val['receipt_quantity'];
  1217. $temp['is_del']=0;
  1218. $temp['addtime']=date("Y-m-d H:i:s");
  1219. $temp['updatetime']=date("Y-m-d H:i:s");
  1220. $inf[]=$temp;
  1221. }
  1222. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  1223. if($inadd==0){
  1224. Db::rollback();
  1225. return error_show(1005,"退货单新建失败");
  1226. }
  1227. }
  1228. if($order['is_stock']==1){
  1229. $lor=$order['status'];
  1230. $thnum = $order['wsend_num'];
  1231. $order['send_num'] += $thnum;
  1232. $order['th_num'] += $thnum;
  1233. $order['wsend_num'] =0;
  1234. $order['status'] = 3;
  1235. $order['send_status']=3;
  1236. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  1237. $order['updatetime'] = date("Y-m-d H:i:s");
  1238. $uap = Db::name("sale")->save($order);
  1239. if ($uap == false) {
  1240. Db::rollback();
  1241. return error_show(1005, '销售单订单更新失败');
  1242. }
  1243. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname], [
  1244. "order_code" => $order["orderCode"],//出库单号
  1245. "status" => $lor,//这里的status是之前的值
  1246. "action_remark" => '',//备注
  1247. "action_type" => "status"//新建create,编辑edit,更改状态status
  1248. ], "XSQRD", $order['status'], $order);
  1249. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname], [
  1250. "order_type" => 'XSQRD',
  1251. "order_code" =>$order["orderCode"],//出库单号
  1252. "order_id" => $order["id"],
  1253. "order_status" =>$order['status'],
  1254. "before_status"=>$lor,
  1255. 'holder_id=' => $order['apply_id']
  1256. ]);
  1257. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$orderCode],["num",">",0]])->select()->toArray();
  1258. if(empty($saleinfo)) {
  1259. Db::rollback();
  1260. return error_show(1005, '商品批次数据未找到');
  1261. }
  1262. $tempnum =$thnum;
  1263. foreach ( $saleinfo as $va){
  1264. if($tempnum ==0) break;
  1265. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])->find();
  1266. if($stock==false) {
  1267. Db::rollback();
  1268. return error_show(1005, '商品库存数据未找到');
  1269. }
  1270. if($va['num']>=$tempnum) {
  1271. $tnm = $tempnum;
  1272. $va['num']-= $tempnum;
  1273. $tempnum=0;
  1274. }else{
  1275. $tnm = $va['num'];
  1276. $tempnum -=$va['num'];
  1277. $va['num'] =0;
  1278. }
  1279. $stock['usable_stock']+=$tnm;
  1280. $stock['wait_out_stock'] -=$tnm;
  1281. $stock['updatetime'] = date("Y-m-d H:i:s");
  1282. $st_up = Db::name("good_stock")->save($stock);
  1283. if ($st_up == false) {
  1284. return error_show(1005, '可售商品入库失败');
  1285. }
  1286. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  1287. if ($ps == false) {
  1288. return error_show(1005, '商品批次退货入库失败');
  1289. }
  1290. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  1291. if ($ret == false) {
  1292. return error_show(1005, '商品批次退货入库失败');
  1293. }
  1294. $va['updatetime']=date("Y-m-d H:i:s");
  1295. $sal= Db::name("sale_info")->save($va);
  1296. if ($sal == false) {
  1297. return error_show(1005, '商品批次退货入库失败');
  1298. }
  1299. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  1300. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  1301. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  1302. }
  1303. $data=[
  1304. "orderCode"=>$orderCode,
  1305. "th_type"=>1,
  1306. "th_num"=>$thnum,
  1307. "th_fee"=>round($order['sale_price']*$thnum,2),
  1308. "thCode"=>$returnCode,
  1309. "spuCode"=>$order['good_code'],
  1310. "good_name"=>$order['good_name'],
  1311. "cat_id"=>$order['cat_id'],
  1312. "apply_id"=>$this->uid,
  1313. "apply_name"=>$this->uname,
  1314. "addtime"=>date("Y-m-d H:i:s"),
  1315. "status"=>1,
  1316. "is_del"=>0
  1317. ];
  1318. $inse=Db::name("th_data")->insert($data);
  1319. if($inse==false){
  1320. Db::rollback();
  1321. return error_show(1004,"退货单更新失败");
  1322. }
  1323. }
  1324. Db::commit();
  1325. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  1326. }
  1327. Db::rollback();
  1328. return error_show(1005,"退货单新建失败");
  1329. }catch (\Exception $e){
  1330. Db::rollback();
  1331. return error_show(1005, $e->getMessage());
  1332. }
  1333. }
  1334. }