Reorder.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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. $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type"=>"XSTHD",'before_status'=>$temp,'holder_id'=>$info['apply_id']];
  625. ProcessOrder::AddProcess($this->post['token'], $process);
  626. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  627. ActionLog::logAdd($this->post['token'], $ste, "XSTHD", $status, $info);
  628. if ($status == 4) {
  629. if ($info['is_addr'] == 1) {
  630. if (isset($addr) && !empty($addr)) {
  631. foreach ($addr as $value) {
  632. $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find();
  633. if ($addrinfo == false) {
  634. Db::rollback();
  635. return error_show(1004, "地址数据未找到");
  636. }
  637. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  638. Db::rollback();
  639. return error_show(1004, "地址发货数量不足");
  640. }
  641. $addrinfo['receipt_quantity'] -= $value['return_num'];
  642. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  643. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  644. $addrup = Db::name("order_addr")->save($addrinfo);
  645. if ($addrup == false) {
  646. Db::rollback();
  647. return error_show(1004, "地址发货数量更新失败");
  648. }
  649. if ($value['outCode'] != "") {
  650. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->find();
  651. if ($out == false) {
  652. Db::rollback();
  653. return error_show(1004, "地址发货单数据未找到");
  654. }
  655. if ($out['status'] >= 2) {
  656. Db::rollback();
  657. return error_show(1004, "地址发货单已发货");
  658. }
  659. if ($out['send_num'] < $value['return_num']) {
  660. Db::rollback();
  661. return error_show(1004, "地址发货单发货数量不足");
  662. }
  663. $out['send_num'] -= $value['return_num'];
  664. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  665. $out['updatetime'] = date("Y-m-d H:i:s");
  666. $outup = Db::name("order_out")->save($out);
  667. if ($outup == false) {
  668. Db::rollback();
  669. return error_show(1004, "地址发货单更新失败");
  670. }
  671. }
  672. }
  673. }
  674. }
  675. if ($orderinfo['wsend_num'] < $info['num']) {
  676. Db::rollback();
  677. return error_show(1004, "销售单未发货数量不足退货");
  678. }
  679. $lor=$orderinfo['status'];
  680. $orderinfo['wsend_num'] -= $info['num'];
  681. $orderinfo['send_num'] += $info['num'];
  682. $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1);
  683. $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2);
  684. if ($orderinfo['is_stock'] == 1) {
  685. $orderinfo['th_num'] += $info['num'];
  686. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  687. $orderinfo['status']=3;
  688. }
  689. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  690. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  691. $uap = Db::name("sale")->save($orderinfo);
  692. if ($uap == false) {
  693. Db::rollback();
  694. return error_show(1005, '销售单订单更新失败');
  695. }
  696. }else{
  697. if ($info['is_th'] == 1){
  698. $orderinfo['th_num'] += $info['num'];
  699. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  700. $orderinfo['status']=3;
  701. }
  702. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  703. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  704. $uap = Db::name("sale")->save($orderinfo);
  705. if ($uap == false) {
  706. Db::rollback();
  707. return error_show(1005, '销售单订单更新失败');
  708. }
  709. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  710. if ($ordernum == false) {
  711. Db::rollback();
  712. return error_show(1005, '未找到关联采购单');
  713. }
  714. $ordernum['send_num'] -= $info['num'];
  715. $orderup = Db::name("order_num")->save($ordernum);
  716. if ($orderup == false) {
  717. Db::rollback();
  718. return error_show(1005, '关联数据更新失败');
  719. }
  720. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  721. if ($cgd == false) {
  722. Db::rollback();
  723. return error_show(1005, '未找到采购单数据');
  724. }
  725. if( $info['is_all']==1 && $cgd['send_status']!=1){
  726. Db::rollback();
  727. return error_show(1005, '采购单已入库无法全部退货');
  728. }
  729. $lor=$cgd['status'];
  730. if($cgd['wsend_num']<$info['num']){
  731. $cgd['send_num'] += $cgd['wsend_num']==0?0 :($info['num']-$cgd['wsend_num']);
  732. $cgd['wsend_num']=0;
  733. }else{
  734. $cgd['wsend_num'] -= $info['num'];
  735. $cgd['send_num'] += $info['num'];
  736. }
  737. $cgd['status'] = $cgd['wsend_num']==0?2:($cgd['send_num']==0?0:1);
  738. $cgd['send_status'] =$cgd['wsend_num']==0?3:($cgd['send_num']==0?1:2);
  739. $cgd['th_num'] += $info['num'];
  740. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  741. $cgd['status']=4;
  742. }
  743. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  744. $cgd['updatetime'] = date("Y-m-d H:i:s");
  745. $cgdup = Db::name("purchease_order")->save($cgd);
  746. if ($cgdup == false) {
  747. Db::rollback();
  748. return error_show(1005, '采购单数据更新失败');
  749. }
  750. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  751. if ($stock==false) {
  752. Db::rollback();
  753. return error_show(1005, '商品仓库未找到');
  754. }
  755. if($stock['wait_in_stock']<$info['num']){
  756. $stock['wait_in_stock']=0;
  757. if( $stock['usable_stock']>$info['num']-$stock['wait_in_stock']){
  758. $stock['usable_stock'] -= $info['num']-$stock['wait_in_stock'];
  759. }else{
  760. $stock['usable_stock'] = 0;
  761. $stock['wait_out_stock']-= $info['num']-$stock['wait_in_stock']- $stock['usable_stock'] ;
  762. }
  763. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  764. }else{
  765. $stock['wait_in_stock']-=$info['num'];
  766. }
  767. $stock['updatetime'] = date("Y-m-d H:i:s");
  768. $st_up = Db::name("good_stock")->save($stock);
  769. if ($st_up == false) {
  770. Db::rollback();
  771. return error_show(1005, '可售商品入库失败');
  772. }
  773. } else {
  774. $orderinfo['th_num'] += $info['num'];
  775. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  776. $orderinfo['status']=3;
  777. }
  778. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  779. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  780. $uap = Db::name("sale")->save($orderinfo);
  781. if ($uap == false) {
  782. Db::rollback();
  783. return error_show(1005, '销售单订单更新失败');
  784. }
  785. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  786. if ($ordernum == false) {
  787. Db::rollback();
  788. return error_show(1005, '未找到关联采购单');
  789. }
  790. $ordernum['send_num'] -= $info['num'];
  791. $ordernum['wsend_num'] = $info['num'];
  792. $orderup = Db::name("order_num")->save($ordernum);
  793. if ($orderup == false) {
  794. Db::rollback();
  795. return error_show(1005, '关联数据更新失败');
  796. }
  797. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  798. if ($cgd == false) {
  799. Db::rollback();
  800. return error_show(1005, '未找到采购单数据');
  801. }
  802. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  803. if ($stock==false) {
  804. Db::rollback();
  805. return error_show(1005, '商品仓库未找到');
  806. }
  807. if($stock['usable_stock']+$stock['wait_out_stock']<$info['num']){
  808. Db::rollback();
  809. return error_show(1005, '商品可用库存不足退回数量');
  810. }else{
  811. if($stock['usable_stock']>$info['num']){
  812. $stock['usable_stock'] -= $info['num'];
  813. }else{
  814. $stock['usable_stock'] = 0;
  815. $stock['wait_out_stock']-= $info['num']- $stock['usable_stock'] ;
  816. }
  817. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  818. }
  819. $stock['updatetime'] = date("Y-m-d H:i:s");
  820. $st_up = Db::name("good_stock")->save($stock);
  821. if ($st_up == false) {
  822. Db::rollback();
  823. return error_show(1005, '可售商品入库失败');
  824. }
  825. }
  826. }
  827. ActionLog::logAdd($this->post['token'], [
  828. "order_code" => $orderinfo["orderCode"],//出库单号
  829. "status" => $lor,//这里的status是之前的值
  830. "action_remark" => '',//备注
  831. "action_type" => "status"//新建create,编辑edit,更改状态status
  832. ], "XSQRD", $orderinfo['status'], $orderinfo);
  833. ProcessOrder::AddProcess($this->post['token'], [
  834. "order_type" => 'XSQRD',
  835. "order_code" =>$orderinfo["orderCode"],//出库单号
  836. "order_id" => $orderinfo["id"],
  837. "order_status" =>$orderinfo['status'],"before_status"=>$lor
  838. ]);
  839. //对应采购单也要处理
  840. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  841. "order_type" => 'CGD',
  842. "order_code" => $cgd['cgdNo'],
  843. "order_id" => $cgd["id"],
  844. "order_status" => $cgd['status'],
  845. "before_status" => $lor,
  846. 'holder_id' => Db::name('supplier')->where(['code' => $cgd['supplierNo'], 'is_del' => 0])->value('personid', 0)
  847. ]);
  848. if($orderinfo['is_stock'] == 1 || $info['is_th'] == 0){
  849. $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm'];
  850. if ($wsmcode == "") {
  851. Db::rollback();
  852. return error_show(1005, '未找到退货仓库');
  853. }
  854. //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存
  855. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' => $wsmcode])->find();
  856. if (empty($stock)) {
  857. $stock = [
  858. "spuCode" => $info['good_code'],
  859. "wsm_code" => $wsmcode,
  860. "usable_stock" => 0,
  861. "wait_out_stock" => 0,
  862. "wait_in_stock" => 0,
  863. "total_stock" => 0,
  864. "addtime" => date("Y-m-d H:i:s"),
  865. "updatetime" => date("Y-m-d H:i:s"),
  866. ];
  867. }
  868. $stock['usable_stock'] += $info['num'];
  869. $stock['updatetime'] = date("Y-m-d H:i:s");
  870. $st_up = Db::name("good_stock")->save($stock);
  871. if ($st_up == false) {
  872. Db::rollback();
  873. return error_show(1005, '可售商品入库失败');
  874. }
  875. $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  876. $sabebn =Db::name("sale_info")->where(["orderCode"=>$orderinfo["orderCode"]])->select()->toArray();
  877. if(!empty($sabebn)){
  878. $total_num =$info['num'];
  879. foreach ($sabebn as $ve){
  880. $tempnum=0;
  881. if ($total_num==0) break;
  882. if($total_num>=$ve['num']){
  883. $tempnum = $ve['num'];
  884. $total_num-=$ve['num'];
  885. $ve['num']=0;
  886. }else{
  887. $tempnum = $total_num;
  888. $total_num=0;
  889. $ve['num']-=$total_num;
  890. }
  891. $bnin=GoodStockInfo::AddBn($stockid,$ve['bnCode'],$tempnum,$ve['origin_price']);
  892. if($bnin==false){
  893. Db::rollback();
  894. return error_show(1005, '可售商品Bn库存数入库失败');
  895. }
  896. $ve['updatetime']=date("Y-m-d H:i:s");
  897. $up=Db::name("sale_info")->save($ve);
  898. if($up==false){
  899. Db::rollback();
  900. return error_show(1005, '可售商品Bn库存数入库失败');
  901. }
  902. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  903. if($bnin==false){
  904. Db::rollback();
  905. return error_show(1005, '可售商品Bn库存数入库失败');
  906. }
  907. }
  908. }
  909. $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => $stockid, "type" => 1, 'stock'=> $info['num'], "stock_name" => "usable_stock"];
  910. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  911. }
  912. $data=[
  913. "orderCode"=>$info['orderCode'],
  914. "th_type"=>1,
  915. "th_num"=>$info['num'],
  916. "th_fee"=>round($info['num']*$orderinfo['sale_price'],2),
  917. "thCode"=>$info['returnCode'],
  918. "spuCode"=>$orderinfo['good_code'],
  919. "good_name"=>$orderinfo['good_name'],
  920. "cat_id"=>$orderinfo['cat_id'],
  921. "apply_id"=>$info['apply_id'],
  922. "apply_name"=>$info['apply_name'],
  923. "addtime"=>date("Y-m-d H:i:s"),
  924. "status"=>1,
  925. "is_del"=>0
  926. ];
  927. $inse=Db::name("th_data")->insert($data);
  928. if($inse==false){
  929. Db::rollback();
  930. return error_show(1004,"退货单更新失败");
  931. }
  932. }
  933. }
  934. Db::commit();
  935. return app_show(0,"更新成功");
  936. }catch (\Exception $e){
  937. Db::rollback();
  938. return error_show(1004,$e->getMessage()."|".$e->getLine());
  939. }
  940. }
  941. public function zxcreate(){
  942. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  943. if($ordeCode==''){
  944. return error_show(1004,"参数orderCode 不能为空");
  945. }
  946. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  947. if(empty($order)){
  948. return error_show(1005,"未找到订单数据");
  949. }
  950. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  951. if($errorCode==''){
  952. return error_show(1004,"参数errorCode 不能为空");
  953. }
  954. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  955. if(empty($error)){
  956. return error_show(1005,"未找到退货原因数据");
  957. }
  958. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  959. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  960. if($token==''){
  961. return error_show(105,"参数token不能为空");
  962. }
  963. $user =GetUserInfo($token);
  964. if(empty($user)||$user['code']!=0){
  965. return error_show(1002,"申请人数据不存在");
  966. }
  967. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  968. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  969. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  970. if($num==''){
  971. return error_show(1005,"参数num不能为空");
  972. }
  973. if($order['wsend_num']<$num){
  974. return error_show(1002,"仓库未发货数量不足退货");
  975. }
  976. $returnCode=makeNo("RS");
  977. Db::startTrans();
  978. try{
  979. $in = [
  980. "returnCode"=>$returnCode,
  981. "orderCode"=>$ordeCode,
  982. "good_code"=>$order['good_code'],
  983. "good_name"=>$order['good_name'],
  984. "apply_id"=>$rm,
  985. "apply_name"=>$ri,
  986. "error_code"=>$errorCode,
  987. "num"=>$num,
  988. "remark"=>$remark,
  989. "order_type"=>2,
  990. "status"=>0,
  991. "is_del"=>0,
  992. "addtime"=>date("Y-m-d H:i:s"),
  993. "updatetime"=>date("Y-m-d H:i:s")
  994. ];
  995. $create = Db::name("sale_return")->insert($in,true);
  996. if($create>0) {
  997. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  998. ProcessOrder::AddProcess($this->post['token'],$process);
  999. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1000. "action_type"=>"create"];
  1001. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in);
  1002. //维护台账记录
  1003. // Db::name('standing_book')
  1004. // ->where('ordeCode', $ordeCode)
  1005. // ->update([
  1006. // 'returnGoodCode' => $returnCode,
  1007. // 'updatetime' => date("Y-m-d H:i:s")
  1008. // ]);
  1009. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  1010. if ($order['send_type'] == 1) {
  1011. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1012. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1013. if(!empty($wsend)){
  1014. foreach ($wsend as $value){
  1015. $tt = $value['status'];
  1016. $value['status']=0;
  1017. $value['updatetime']=date("Y-m-d H:i:s");
  1018. $up =Db::name("order_out")->save($value);
  1019. if($up){
  1020. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt,'holder_id' => $value['apply_id']];
  1021. ProcessOrder::AddProcess($this->post['token'],$process);
  1022. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1023. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  1024. }else{
  1025. Db::rollback();
  1026. return error_show(1005,"退货单新建失败");
  1027. }
  1028. }
  1029. }
  1030. }
  1031. Db::commit();
  1032. return error_show(0,"退货单新建成功");
  1033. }
  1034. Db::rollback();
  1035. return error_show(1005,"退货单新建失败");
  1036. }catch (\Exception $e){
  1037. Db::rollback();
  1038. return error_show(1005,$e->getMessage());
  1039. }
  1040. }
  1041. public function allReturn(){
  1042. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''? trim($this->post['orderCode']):"";
  1043. if($orderCode==''){
  1044. return error_show(1004,"参数orderCode 不能为空");
  1045. }
  1046. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  1047. if(empty($order)){
  1048. return error_show(1005,"未找到订单数据");
  1049. }
  1050. if($order['wsend_num']!=$order['good_num']){
  1051. return error_show(1005,"订单未发货数量与总数不等,无法全退");
  1052. }
  1053. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","<",4]])->count();
  1054. if($retrun>0){
  1055. return error_show(1005,"存在退货订单数据");
  1056. }
  1057. if($order['order_type']==3){
  1058. $goon = Db::name("good_zixun")->where(["spuCode"=>$order['good_code'],"is_del"=>0])->find();
  1059. }else {
  1060. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1061. ->where(['a.skuCode' => $order['skuCode']])->field("b.creater,b.createrid,b.supplierNo")->find();
  1062. }
  1063. if($goon==false){
  1064. return error_show(1005,"未找到商品数据");
  1065. }
  1066. $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1067. if($supplier==false){
  1068. return error_show(1005,"未找到商品供应商数据");
  1069. }
  1070. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1071. if($errorCode==''){
  1072. return error_show(1004,"参数errorCode 不能为空");
  1073. }
  1074. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1075. if(empty($error)){
  1076. return error_show(1005,"未找到退货原因数据");
  1077. }
  1078. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1079. $addr =Db::name("order_addr")->where([["orderCode","=",$orderCode],["is_del","=",0]])->select()->toArray();
  1080. if($order['is_stock']==0){
  1081. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1082. if ($ordernum == false) {
  1083. return error_show(1005, '未找到关联采购单');
  1084. }
  1085. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1086. if ($cgd == false) {
  1087. return error_show(1005, '未找到采购单数据');
  1088. }
  1089. }
  1090. if($order['is_stock']==0&&$cgd['send_status']!=1){
  1091. return error_show(1005, '采购单已发起入库');
  1092. }
  1093. $returnCode=makeNo("RN");
  1094. Db::startTrans();
  1095. try{
  1096. $in = [
  1097. "returnCode"=>$returnCode,
  1098. "orderCode"=>$orderCode,
  1099. "good_code"=>$order['good_code'],
  1100. "good_name"=>$order['good_name'],
  1101. "apply_id"=>$this->uid,
  1102. "apply_name"=>$this->uname,
  1103. "cgderid"=>$goon['createrid'],
  1104. "cgder"=>$goon['creater'],
  1105. "person"=>$supplier['person']??'',
  1106. "person_id"=>$supplier['personid']??0,
  1107. "error_code"=>$errorCode,
  1108. "num"=>$order['wsend_num'],
  1109. "total_fee"=>round($order['sale_price']*$order['wsend_num'],2),
  1110. "good_price"=>$order['sale_price'],
  1111. "platform_id"=>$order['platform_id'],
  1112. "remark"=>$remark,
  1113. "order_type"=>$order['order_type'],
  1114. "is_addr"=>count($addr)>0 ?1:0,
  1115. "status"=>$order['is_stock']==1?4:1,
  1116. "is_del"=>0,
  1117. "is_all"=>1,
  1118. "addtime"=>date("Y-m-d H:i:s"),
  1119. "updatetime"=>date("Y-m-d H:i:s")
  1120. ];
  1121. $create = Db::name("sale_return")->insert($in,true);
  1122. if($create>0){
  1123. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1124. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  1125. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1126. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname],$process);
  1127. //维护台账信息
  1128. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  1129. if(count($addr)!=0){
  1130. $inf=[];
  1131. foreach ($addr as $val){
  1132. if ($val['receipt_quantity'] == 0) continue;//当退货数量为0时,跳过
  1133. $temp=[];
  1134. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  1135. if ($send == false) {
  1136. Db::rollback();
  1137. return error_show(1004, "地址发货单未找到");
  1138. }
  1139. if($send['status']>=2){
  1140. Db::rollback();
  1141. return error_show(1005,"地址已发货");
  1142. }
  1143. if($order['is_stock']==1){
  1144. $val['is_del'] = 1;
  1145. $val['updatetime'] = date("Y-m-d H:i:s");
  1146. $addrup = Db::name("order_addr")->save($val);
  1147. if ($addrup == false) {
  1148. Db::rollback();
  1149. return error_show(1004, "地址更新失败");
  1150. }
  1151. $send['is_del'] = 1;
  1152. $send['updatetime'] = date("Y-m-d H:i:s");
  1153. $outup = Db::name("order_out")->save($send);
  1154. if ($outup == false) {
  1155. Db::rollback();
  1156. return error_show(1004, "地址发货单更新失败");
  1157. }
  1158. //处理发货申请单流程
  1159. ProcessOrder::AddProcess(["id" => $this->uid, "nickname" => $this->uname], [
  1160. "order_type" => 'CKD',
  1161. "order_code" => $send["outCode"],//出库单号
  1162. "order_id" => $send["id"],
  1163. "order_status" => 4,//全部退货
  1164. "before_status" => $send['status'],
  1165. 'holder_id=' => $send['apply_id']
  1166. ]);
  1167. // $ordersend = Db::name("order_send")->where(["outCode" => $send['outCode']])->find();
  1168. // if($ordersend==false){
  1169. // Db::rollback();
  1170. // return error_show(1004, "发货单关联数据未找到");
  1171. // }
  1172. // $ordersend['status'] = 0;
  1173. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  1174. // $sendip = Db::name("order_send")->save($ordersend);
  1175. // if ($sendip == false) {
  1176. // Db::rollback();
  1177. // return error_show(1004, "发货单更新失败");
  1178. // }
  1179. }
  1180. $temp['returnCode']=$returnCode;
  1181. $temp['orderCode']=$orderCode;
  1182. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  1183. $temp['addrid']=$val['id'];
  1184. $temp['send_num']=$val['receipt_quantity'];
  1185. $temp['return_num']=$val['receipt_quantity'];
  1186. $temp['is_del']=0;
  1187. $temp['addtime']=date("Y-m-d H:i:s");
  1188. $temp['updatetime']=date("Y-m-d H:i:s");
  1189. $inf[]=$temp;
  1190. }
  1191. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  1192. if($inadd==0){
  1193. Db::rollback();
  1194. return error_show(1005,"退货单新建失败");
  1195. }
  1196. }
  1197. if($order['is_stock']==1){
  1198. $lor=$order['status'];
  1199. $thnum = $order['wsend_num'];
  1200. $order['send_num'] += $thnum;
  1201. $order['th_num'] += $thnum;
  1202. $order['wsend_num'] =0;
  1203. $order['status'] = 3;
  1204. $order['send_status']=3;
  1205. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  1206. $order['updatetime'] = date("Y-m-d H:i:s");
  1207. $uap = Db::name("sale")->save($order);
  1208. if ($uap == false) {
  1209. Db::rollback();
  1210. return error_show(1005, '销售单订单更新失败');
  1211. }
  1212. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname], [
  1213. "order_code" => $order["orderCode"],//出库单号
  1214. "status" => $lor,//这里的status是之前的值
  1215. "action_remark" => '',//备注
  1216. "action_type" => "status"//新建create,编辑edit,更改状态status
  1217. ], "XSQRD", $order['status'], $order);
  1218. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname], [
  1219. "order_type" => 'XSQRD',
  1220. "order_code" =>$order["orderCode"],//出库单号
  1221. "order_id" => $order["id"],
  1222. "order_status" =>$order['status'],
  1223. "before_status"=>$lor,
  1224. 'holder_id=' => $order['apply_id']
  1225. ]);
  1226. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$orderCode],["num",">",0]])->select()->toArray();
  1227. if(empty($saleinfo)) {
  1228. Db::rollback();
  1229. return error_show(1005, '商品批次数据未找到');
  1230. }
  1231. $tempnum =$thnum;
  1232. foreach ( $saleinfo as $va){
  1233. if($tempnum ==0) break;
  1234. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])->find();
  1235. if($stock==false) {
  1236. Db::rollback();
  1237. return error_show(1005, '商品库存数据未找到');
  1238. }
  1239. if($va['num']>=$tempnum) {
  1240. $tnm = $tempnum;
  1241. $va['num']-= $tempnum;
  1242. $tempnum=0;
  1243. }else{
  1244. $tnm = $va['num'];
  1245. $tempnum -=$va['num'];
  1246. $va['num'] =0;
  1247. }
  1248. $stock['usable_stock']+=$tnm;
  1249. $stock['wait_out_stock'] -=$tnm;
  1250. $stock['updatetime'] = date("Y-m-d H:i:s");
  1251. $st_up = Db::name("good_stock")->save($stock);
  1252. if ($st_up == false) {
  1253. return error_show(1005, '可售商品入库失败');
  1254. }
  1255. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  1256. if ($ps == false) {
  1257. return error_show(1005, '商品批次退货入库失败');
  1258. }
  1259. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  1260. if ($ret == false) {
  1261. return error_show(1005, '商品批次退货入库失败');
  1262. }
  1263. $va['updatetime']=date("Y-m-d H:i:s");
  1264. $sal= Db::name("sale_info")->save($va);
  1265. if ($sal == false) {
  1266. return error_show(1005, '商品批次退货入库失败');
  1267. }
  1268. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  1269. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  1270. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  1271. }
  1272. $data=[
  1273. "orderCode"=>$orderCode,
  1274. "th_type"=>1,
  1275. "th_num"=>$thnum,
  1276. "th_fee"=>round($order['sale_price']*$thnum,2),
  1277. "thCode"=>$returnCode,
  1278. "spuCode"=>$order['good_code'],
  1279. "good_name"=>$order['good_name'],
  1280. "cat_id"=>$order['cat_id'],
  1281. "apply_id"=>$this->uid,
  1282. "apply_name"=>$this->uname,
  1283. "addtime"=>date("Y-m-d H:i:s"),
  1284. "status"=>1,
  1285. "is_del"=>0
  1286. ];
  1287. $inse=Db::name("th_data")->insert($data);
  1288. if($inse==false){
  1289. Db::rollback();
  1290. return error_show(1004,"退货单更新失败");
  1291. }
  1292. }
  1293. Db::commit();
  1294. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  1295. }
  1296. Db::rollback();
  1297. return error_show(1005,"退货单新建失败");
  1298. }catch (\Exception $e){
  1299. Db::rollback();
  1300. return error_show(1005, $e->getMessage());
  1301. }
  1302. }
  1303. }