Reorder.php 84 KB

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