Reorder.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. 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("RS");
  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. "error_code"=>$errorCode,
  81. "num"=>$thnum,
  82. "total_fee"=>round($order['sale_price']*$thnum,2),
  83. "good_price"=>$order['sale_price'],
  84. "platform_id"=>$order['platform_id'],
  85. "remark"=>$remark,
  86. "order_type"=>$order['order_type'],
  87. "is_addr"=>$is_addr,
  88. "status"=>$order['is_stock']==1?4:1,
  89. "is_del"=>0,
  90. "addtime"=>date("Y-m-d H:i:s"),
  91. "updatetime"=>date("Y-m-d H:i:s")
  92. ];
  93. $create = Db::name("sale_return")->insert($in,true);
  94. if($create>0){
  95. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  96. ActionLog::logAdd($this->post['token'],$stn,"XSTHD",$in['status'],$in);
  97. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0];
  98. ProcessOrder::AddProcess($this->post['token'],$process);
  99. //维护台账信息
  100. Db::name('standing_book')
  101. ->where('orderCode', $ordeCode)
  102. ->update([
  103. "returnGoodCode" => $returnCode,
  104. "updatetime" => date("Y-m-d H:i:s")
  105. ]);
  106. if($returnadr!=""){
  107. $inf=[];
  108. foreach ($returnadr as $val){
  109. if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
  110. $temp=[];
  111. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  112. if($addrinfo==false){
  113. Db::rollback();
  114. return error_show(1005,"地址信息未找到");
  115. }
  116. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  117. if($send['status']>=2){
  118. Db::rollback();
  119. return error_show(1005,"地址已发货");
  120. }
  121. if($order['is_stock']==1){
  122. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  123. Db::rollback();
  124. return error_show(1004, "地址发货数量不足");
  125. }
  126. $addrinfo['receipt_quantity'] -= $val['return_num'];
  127. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  128. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  129. $addrup = Db::name("order_addr")->save($addrinfo);
  130. if ($addrup == false) {
  131. Db::rollback();
  132. return error_show(1004, "地址发货数量更新失败");
  133. }
  134. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  135. if ($out == false) {
  136. Db::rollback();
  137. return error_show(1004, "地址发货单数据未找到");
  138. }
  139. // if ($send!=false) {
  140. if ($out['status'] >= 2) {
  141. Db::rollback();
  142. return error_show(1004, "地址发货单已发货");
  143. }
  144. if ($out['send_num'] < $val['return_num']) {
  145. Db::rollback();
  146. return error_show(1004, "地址发货单发货数量不足");
  147. }
  148. $out['send_num'] -= $val['return_num'];
  149. $out['is_del'] = $out['send_num']==0?1:0;
  150. $out['updatetime'] = date("Y-m-d H:i:s");
  151. $outup = Db::name("order_out")->save($out);
  152. if ($outup == false) {
  153. Db::rollback();
  154. return error_show(1004, "地址发货单更新失败");
  155. }
  156. $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  157. if ($ordersend['send_num'] < $val['return_num']) {
  158. Db::rollback();
  159. return error_show(1004, "发货单发货数量不足");
  160. }
  161. $ordersend['send_num'] -= $val['return_num'];
  162. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  163. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  164. $sendip = Db::name("order_send")->save($ordersend);
  165. if ($sendip == false) {
  166. Db::rollback();
  167. return error_show(1004, "发货单更新失败");
  168. }
  169. // }
  170. }
  171. $temp['returnCode']=$returnCode;
  172. $temp['orderCode']=$ordeCode;
  173. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  174. $temp['addrid']=$val['id'];
  175. $temp['send_num']=$addrinfo['receipt_quantity'];
  176. $temp['return_num']=$val['return_num'];
  177. $temp['is_del']=0;
  178. $temp['addtime']=date("Y-m-d H:i:s");
  179. $temp['updatetime']=date("Y-m-d H:i:s");
  180. $inf[]=$temp;
  181. }
  182. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  183. if($inadd==0){
  184. Db::rollback();
  185. return error_show(1005,"退货单新建失败");
  186. }
  187. }
  188. if($order['is_stock']==1){
  189. if ($order['wsend_num'] < $thnum) {
  190. Db::rollback();
  191. return error_show(1004, "销售单未发货数量不足退货");
  192. }
  193. $lor=$order['status'] ;
  194. $order['wsend_num'] -= $thnum;
  195. $order['send_num'] += $thnum;
  196. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  197. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  198. $order['th_num'] += $thnum;
  199. if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){
  200. $order['status']=3;
  201. }
  202. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  203. $order['updatetime'] = date("Y-m-d H:i:s");
  204. $uap = Db::name("sale")->save($order);
  205. if ($uap == false) {
  206. Db::rollback();
  207. return error_show(1005, '销售单订单更新失败');
  208. }
  209. ActionLog::logAdd($this->post['token'], [
  210. "order_code" => $order["orderCode"],//出库单号
  211. "status" => $lor,//这里的status是之前的值
  212. "action_remark" => '',//备注
  213. "action_type" => "status"//新建create,编辑edit,更改状态status
  214. ], "XSQRD", $order['status'], $order);
  215. ProcessOrder::AddProcess($this->post['token'], [
  216. "order_type" => 'XSQRD',
  217. "order_code" =>$order["orderCode"],//出库单号
  218. "order_id" => $order["id"],
  219. "order_status" =>$order['status'],"before_status"=>$lor
  220. ]);
  221. $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  222. if ($ordernum == false) {
  223. Db::rollback();
  224. return error_show(1005, '未找到关联采购单');
  225. }
  226. $ordernum['send_num'] -= $thnum;
  227. $orderup = Db::name("order_num")->save($ordernum);
  228. if ($orderup == false) {
  229. Db::rollback();
  230. return error_show(1005, '关联数据更新失败');
  231. }
  232. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  233. if ($cgd == false) {
  234. Db::rollback();
  235. return error_show(1005, '未找到采购单数据');
  236. }
  237. $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  238. $cgd['th_num'] += $thnum;
  239. $cgd['updatetime'] = date("Y-m-d H:i:s");
  240. $cgdup = Db::name("purchease_order")->save($cgd);
  241. if ($cgdup == false) {
  242. Db::rollback();
  243. return error_show(1005, '采购单数据更新失败');
  244. }
  245. if ($cgd['bkcode'] != "") {
  246. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "order_source" =>0, "is_del" => 0])
  247. ->find();
  248. if ($bk == false) {
  249. Db::rollback();
  250. return error_show(1005, '未找到备库单数据');
  251. }
  252. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  253. if ($orderbk == false) {
  254. Db::rollback();
  255. return error_show(1005, '备库单未完全入库');
  256. }
  257. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" =>
  258. 1, "is_del" => 0])->where("order_source","<>",0)->field("sum(send_num)-sum(th_num) as num")
  259. ->find();
  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. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code']])
  270. ->find();
  271. if (empty($stock)) {
  272. $stock = [
  273. "spuCode" => $order['good_code'],
  274. "wsm_code" => $cgd['wsm_code'],
  275. "usable_stock" => 0,
  276. "wait_out_stock" => 0,
  277. "wait_in_stock" => 0,
  278. "total_stock" => 0,
  279. "addtime" => date("Y-m-d H:i:s"),
  280. "updatetime" => date("Y-m-d H:i:s"),
  281. ];
  282. }
  283. // if($stock['presale_stock']>0){
  284. // if($stock['presale_stock']>=$thnum){
  285. // $stock['presale_stock']-=$thnum;
  286. // }else{
  287. // $stock['presale_stock']=0;
  288. // $stock['usable_stock']+=$thnum;
  289. // $stock['wait_out_stock'] -= ($thnum -$stock['presale_stock']);
  290. // }
  291. // }else{
  292. $stock['usable_stock']+=$thnum;
  293. $stock['wait_out_stock'] -= $thnum;
  294. // }
  295. $stock['updatetime'] = date("Y-m-d H:i:s");
  296. $st_up = Db::name("good_stock")->save($stock);
  297. if ($st_up == false) {
  298. return error_show(1005, '可售商品入库失败');
  299. }
  300. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  301. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  302. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  303. $data=[
  304. "orderCode"=>$ordeCode,
  305. "th_type"=>1,
  306. "th_num"=>$thnum,
  307. "th_fee"=>round($order['sale_price']*$thnum,2),
  308. "thCode"=>$returnCode,
  309. "spuCode"=>$order['good_code'],
  310. "good_name"=>$order['good_name'],
  311. "cat_id"=>$order['cat_id'],
  312. "apply_id"=>$rm,
  313. "apply_name"=>$ri,
  314. "addtime"=>date("Y-m-d H:i:s"),
  315. "status"=>1,
  316. "is_del"=>0
  317. ];
  318. $inse=Db::name("th_data")->insert($data);
  319. if($inse==false){
  320. Db::rollback();
  321. return error_show(1004,"退货单更新失败");
  322. }
  323. }
  324. Db::commit();
  325. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  326. }
  327. Db::rollback();
  328. return error_show(1005,"退货单新建失败");
  329. }catch (\Exception $e){
  330. Db::rollback();
  331. return error_show(1005,$e->getMessage());
  332. }
  333. }
  334. public function list(){
  335. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  336. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  337. $where =[['sr.is_del',"=",0]];
  338. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  339. if($bkcode!=""){
  340. $where[]=['sr.returnCode',"like", "%{$bkcode}%"];
  341. }
  342. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  343. if($status!==""){
  344. $where[]=['sr.status',"=", $status];
  345. }
  346. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  347. :"";
  348. if($orderCode!=""){
  349. $where[]=['sr.orderCode',"like", "%{$orderCode}%"];
  350. }
  351. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  352. :"";
  353. if($apply_name!=""){
  354. $where[]=['sr.apply_name',"like", "%{$apply_name}%"];
  355. }
  356. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  357. if($start!==""){
  358. $where[]=['sr.addtime',">=", $start.' 00:00:00'];
  359. }
  360. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  361. if($end!==""){
  362. $where[]=['sr.addtime',"<=", $end.' 23:59:59'];
  363. }
  364. $role=$this->checkRole();
  365. $condition='';
  366. if(!empty($role['write']) && $this->uid!=""){
  367. // $where[]=["sr.apply_id","in",$role['write']];
  368. $condition .="sr.cgderid = {$this->uid} or sr.apply_id in (".implode(',',$role['write']).")";
  369. }
  370. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  371. if ($company_name !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($company_name)];
  372. $count=Db::name("sale_return")->alias('sr')->where($where)->where($condition)->count();
  373. $total = ceil($count/$size);
  374. $page = $total>=$page ? $page :$total;
  375. $list = Db::name("sale_return")
  376. ->alias('sr')
  377. ->field('sr.*,u.itemid')
  378. ->leftJoin("depart_user u", "u.uid=sr.apply_id AND u.is_del=0")
  379. ->where($where)
  380. ->where($condition)
  381. ->order("addtime desc")
  382. ->page($page,$size)
  383. ->cursor();
  384. // echo Db::name("sale_return")->getLastSql();
  385. $data=[];
  386. foreach ($list as $value){
  387. $value['error_msg']='';
  388. if($value['error_code']!=''){
  389. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  390. $value['error_msg']= isset($error['result'])?$error['result']:"";
  391. }
  392. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  393. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  394. $value['return_total'] =$value['sale_price']*$value['num'] ;
  395. $value['total_num'] =$order['good_num'] ;
  396. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  397. $data[]=$value ;
  398. }
  399. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  400. }
  401. public function info(){
  402. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  403. :"";
  404. if($code==""){
  405. return error_show(1004,"参数returnCode不能为空");
  406. }
  407. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  408. if(empty($info)){
  409. return error_show(1004,"未找到退货数据");
  410. }
  411. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  412. if($orderinfo['order_type']==3){
  413. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  414. }else {
  415. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  416. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  417. }
  418. if ($goon==false) {
  419. return error_show(1003, "未找到商品数据");
  420. }
  421. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  422. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  423. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  424. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  425. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  426. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  427. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  428. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  429. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  430. $info['return_total'] = $info['sale_price']*$info['num'] ;
  431. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  432. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  433. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  434. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  435. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  436. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  437. $info['customer_name']='';
  438. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  439. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  440. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  441. }
  442. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  443. $info['supplier_name']='';
  444. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  445. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  446. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  447. }
  448. $info['platform_name']='';
  449. $info['platform_id']=$orderinfo['platform_id'];
  450. if($orderinfo['platform_id']!=0){
  451. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  452. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  453. }
  454. $info['cgd_wsend']="";
  455. $info['cgd_send']="";
  456. $info['cgd_total']="";
  457. if($orderinfo['order_type']==2){
  458. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  459. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  460. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  461. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  462. }
  463. $info['error_msg']='';
  464. if($info['error_code']!=''){
  465. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  466. $info['error_msg']= isset($error['result'])?$error['result']:"";
  467. }
  468. if($info['return_wsm']!=""){
  469. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  470. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  471. }
  472. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  473. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  474. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  475. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  476. $wsm=[];
  477. if(!empty($wsm_return)){
  478. foreach ($wsm_return as $value){
  479. $value['wsm_name']="";
  480. $value['wsm_supplier']="";
  481. $value['wsm_supplierNo']="";
  482. if($value['wsm_code']!=""){
  483. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  484. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  485. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  486. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  487. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  488. }
  489. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  490. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  491. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  492. $value['wsm_send'] = $send ?? 0;
  493. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  494. $wsm[]=$value;
  495. }
  496. }
  497. $info['wsminfo']=$wsm;
  498. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  499. $addrinfo=[];
  500. if(!empty($addr)){
  501. foreach ( $addr as $value){
  502. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  503. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  504. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  505. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  506. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  507. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  508. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  509. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  510. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  511. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode'],"is_del"=>0])->where("status",">=",2)->sum("send_num");
  512. $value['addr_send'] = $send ?? 0;
  513. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  514. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  515. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  516. $addrinfo[]=$value;
  517. }
  518. }
  519. $info['addrinfo']=$addrinfo;
  520. $info['can']=$int;
  521. return app_show(0,"获取成功",$info);
  522. }
  523. /**
  524. * @return \think\response\Json|void
  525. * @throws \think\db\exception\DataNotFoundException
  526. * @throws \think\db\exception\DbException
  527. * @throws \think\db\exception\ModelNotFoundException
  528. */
  529. public function delete(){
  530. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  531. :"";
  532. if($code==""){
  533. return error_show(1004,"参数returnCode不能为空");
  534. }
  535. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  536. if(empty($info)){
  537. return error_show(1004,"未找到退货数据");
  538. }
  539. $info["is_del"]=1;
  540. $info["updatetime"]=date("Y-m-d H:i:s");
  541. $del = Db::name("sale_return")->save($info);
  542. if($del){
  543. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  544. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",0,$ste);
  545. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  546. ProcessOrder::workdel($process);
  547. return app_show(0,"删除成功");
  548. }else{
  549. return error_show(1004,"删除失败");
  550. }
  551. }
  552. /**审核
  553. * @return \think\response\Json|void
  554. * @throws \think\db\exception\DataNotFoundException
  555. * @throws \think\db\exception\DbException
  556. * @throws \think\db\exception\ModelNotFoundException
  557. */
  558. public function exam(){
  559. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  560. :"";
  561. if($code==""){
  562. return error_show(1004,"参数returnCode不能为空");
  563. }
  564. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  565. if(empty($info)){
  566. return error_show(1004,"未找到退货数据");
  567. }
  568. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  569. :"";
  570. if($status===""){
  571. return error_show(1004,"参数status不能为空");
  572. }
  573. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  574. $var = $info['status'];
  575. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  576. $apply_id =GetUserInfo($token);
  577. if(empty($apply_id)||$apply_id['code']!=0){
  578. return error_show(1002,"申请人数据不存在");
  579. }
  580. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  581. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  582. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  583. if($orderinfo==false){
  584. return error_show(1004,"未找到订单数据");
  585. }
  586. if($status==4){
  587. if($info['is_addr']==1){
  588. $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select()
  589. ->toArray();
  590. if(empty($addr)){
  591. return error_show(1004,"未找到发货单地址数据");
  592. }
  593. }
  594. }
  595. if($status==3){
  596. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  597. if($is_th===""){
  598. return error_show(1004,"参数is_th不能为空");
  599. }
  600. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  601. if($is_th==0){
  602. if($return_wsm===""){
  603. return error_show(1004,"参数return_wsm 不能为空");
  604. }
  605. $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  606. if($wsmcode==false){
  607. return error_show(1004,"为找到仓库数据");
  608. }
  609. }
  610. $info['return_wsm'] =$return_wsm ;
  611. $info['is_th'] =$is_th ;
  612. }
  613. Db::startTrans();
  614. try{
  615. $temp= $info['status'];
  616. $info['status'] =$status;
  617. $remark!=""? $info['remark'] =$remark:"";
  618. $info["updatetime"]=date("Y-m-d H:i:s");
  619. $up = Db::name("sale_return")->save($info);
  620. if($up) {
  621. $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type"=>"XSTHD",'before_status'=>$temp];
  622. ProcessOrder::AddProcess($this->post['token'], $process);
  623. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  624. ActionLog::logAdd($this->post['token'], $ste, "XSTHD", $status, $info);
  625. if ($status == 4) {
  626. if ($info['is_addr'] == 1) {
  627. if (isset($addr) && !empty($addr)) {
  628. foreach ($addr as $value) {
  629. $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find();
  630. if ($addrinfo == false) {
  631. Db::rollback();
  632. return error_show(1004, "地址数据未找到");
  633. }
  634. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  635. Db::rollback();
  636. return error_show(1004, "地址发货数量不足");
  637. }
  638. $addrinfo['receipt_quantity'] -= $value['return_num'];
  639. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  640. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  641. $addrup = Db::name("order_addr")->save($addrinfo);
  642. if ($addrup == false) {
  643. Db::rollback();
  644. return error_show(1004, "地址发货数量更新失败");
  645. }
  646. if ($value['outCode'] != "") {
  647. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->find();
  648. if ($out == false) {
  649. Db::rollback();
  650. return error_show(1004, "地址发货单数据未找到");
  651. }
  652. if ($out['status'] >= 2) {
  653. Db::rollback();
  654. return error_show(1004, "地址发货单已发货");
  655. }
  656. if ($out['send_num'] < $value['return_num']) {
  657. Db::rollback();
  658. return error_show(1004, "地址发货单发货数量不足");
  659. }
  660. $out['send_num'] -= $value['return_num'];
  661. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  662. $out['updatetime'] = date("Y-m-d H:i:s");
  663. $outup = Db::name("order_out")->save($out);
  664. if ($outup == false) {
  665. Db::rollback();
  666. return error_show(1004, "地址发货单更新失败");
  667. }
  668. $ordersend = Db::name("order_send")->where(["outCode" => $value['outCode']])->find();
  669. if ($ordersend['send_num'] < $value['return_num']) {
  670. Db::rollback();
  671. return error_show(1004, "发货单发货数量不足");
  672. }
  673. $ordersend['send_num'] -= $value['return_num'];
  674. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  675. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  676. $sendip = Db::name("order_send")->save($ordersend);
  677. if ($sendip == false) {
  678. Db::rollback();
  679. return error_show(1004, "发货单更新失败");
  680. }
  681. }
  682. }
  683. }
  684. }
  685. if ($orderinfo['wsend_num'] < $info['num']) {
  686. Db::rollback();
  687. return error_show(1004, "销售单未发货数量不足退货");
  688. }
  689. $lor=$orderinfo['status'];
  690. $orderinfo['wsend_num'] -= $info['num'];
  691. $orderinfo['send_num'] += $info['num'];
  692. $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1);
  693. $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2);
  694. if ($orderinfo['is_stock'] == 1) {
  695. $orderinfo['th_num'] += $info['num'];
  696. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  697. $orderinfo['status']=3;
  698. }
  699. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  700. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  701. $uap = Db::name("sale")->save($orderinfo);
  702. if ($uap == false) {
  703. Db::rollback();
  704. return error_show(1005, '销售单订单更新失败');
  705. }
  706. $ordernum = Db::name("order_num")->where(['orderCode' => $info['orderCode']])->find();
  707. if ($ordernum == false) {
  708. Db::rollback();
  709. return error_show(1005, '未找到关联采购单');
  710. }
  711. $ordernum['send_num'] -= $info['num'];
  712. $orderup = Db::name("order_num")->save($ordernum);
  713. if ($orderup == false) {
  714. Db::rollback();
  715. return error_show(1005, '关联数据更新失败');
  716. }
  717. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  718. if ($cgd == false) {
  719. Db::rollback();
  720. return error_show(1005, '未找到采购单数据');
  721. }
  722. $cgd['th_fee'] += round($cgd['good_price'] * $info['num'], 2);
  723. $cgd['th_num'] += $info['num'];
  724. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  725. $cgd['status']=4;
  726. }
  727. $cgd['updatetime'] = date("Y-m-d H:i:s");
  728. $cgdup = Db::name("purchease_order")->save($cgd);
  729. if ($cgdup == false) {
  730. Db::rollback();
  731. return error_show(1005, '采购单数据更新失败');
  732. }
  733. if ($cgd['bkcode'] != "") {
  734. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1,"order_source"=>0, "is_del" => 0])
  735. ->find();
  736. if ($bk == false) {
  737. Db::rollback();
  738. return error_show(1005, '未找到备库单数据');
  739. }
  740. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  741. if ($orderbk == false) {
  742. Db::rollback();
  743. return error_show(1005, '备库单未完全入库');
  744. }
  745. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type"
  746. => 1, "is_del" => 0])->where("order_source","<>",0)
  747. ->field("sum(send_num)-sum(th_num) as num")->find();
  748. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  749. $orderbk['merge_num'] = $merge_num['num'];
  750. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  751. $orderbkup = Db::name("order_bk")->save($orderbk);
  752. if ($orderbkup == false) {
  753. Db::rollback();
  754. return error_show(1005, '备库单库存数据释放失败');
  755. }
  756. }
  757. } else {
  758. if ($info['is_th'] == 1) {
  759. $orderinfo['th_num'] += $info['num'];
  760. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  761. $orderinfo['status']=3;
  762. }
  763. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  764. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  765. $uap = Db::name("sale")->save($orderinfo);
  766. if ($uap == false) {
  767. Db::rollback();
  768. return error_show(1005, '销售单订单更新失败');
  769. }
  770. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  771. if ($ordernum == false) {
  772. Db::rollback();
  773. return error_show(1005, '未找到关联采购单');
  774. }
  775. $ordernum['send_num'] -= $info['num'];
  776. $orderup = Db::name("order_num")->save($ordernum);
  777. if ($orderup == false) {
  778. Db::rollback();
  779. return error_show(1005, '关联数据更新失败');
  780. }
  781. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  782. if ($cgd == false) {
  783. Db::rollback();
  784. return error_show(1005, '未找到采购单数据');
  785. }
  786. $lor=$cgd['status'];
  787. if($cgd['wsend_num']<$info['num']){
  788. $cgd['wsend_num']=0;
  789. $cgd['send_num'] += $info['num']-$cgd['wsend_num'];
  790. }else{
  791. $cgd['wsend_num'] -= $info['num'];
  792. $cgd['send_num'] += $info['num'];
  793. }
  794. $cgd['status'] = $cgd['wsend_num']==0?2:($cgd['send_num']==0?0:1);
  795. $cgd['send_status'] =$cgd['wsend_num']==0?3:($cgd['send_num']==0?1:2);
  796. $cgd['th_num'] += $info['num'];
  797. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  798. $cgd['status']=4;
  799. }
  800. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  801. $cgd['updatetime'] = date("Y-m-d H:i:s");
  802. $cgdup = Db::name("purchease_order")->save($cgd);
  803. if ($cgdup == false) {
  804. Db::rollback();
  805. return error_show(1005, '采购单数据更新失败');
  806. }
  807. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  808. if ($stock==false) {
  809. Db::rollback();
  810. return error_show(1005, '商品仓库未找到');
  811. }
  812. if($stock['wait_in_stock']<$info['num']){
  813. $stock['wait_in_stock']=0;
  814. if( $stock['usable_stock']>$info['num']-$stock['wait_in_stock']){
  815. $stock['usable_stock'] -= $info['num']-$stock['wait_in_stock'];
  816. }else{
  817. $stock['usable_stock'] = 0;
  818. $stock['wait_out_stock']-= $info['num']-$stock['wait_in_stock']- $stock['usable_stock'] ;
  819. }
  820. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  821. }else{
  822. $stock['wait_in_stock']-=$info['num'];
  823. }
  824. $stock['updatetime'] = date("Y-m-d H:i:s");
  825. $st_up = Db::name("good_stock")->save($stock);
  826. if ($st_up == false) {
  827. Db::rollback();
  828. return error_show(1005, '可售商品入库失败');
  829. }
  830. } else {
  831. $orderinfo['th_num'] += $info['num'];
  832. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  833. $orderinfo['status']=3;
  834. }
  835. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  836. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  837. $uap = Db::name("sale")->save($orderinfo);
  838. if ($uap == false) {
  839. Db::rollback();
  840. return error_show(1005, '销售单订单更新失败');
  841. }
  842. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  843. if ($ordernum == false) {
  844. Db::rollback();
  845. return error_show(1005, '未找到关联采购单');
  846. }
  847. $ordernum['send_num'] -= $info['num'];
  848. $ordernum['wsend_num'] = $info['num'];
  849. $orderup = Db::name("order_num")->save($ordernum);
  850. if ($orderup == false) {
  851. Db::rollback();
  852. return error_show(1005, '关联数据更新失败');
  853. }
  854. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  855. if ($cgd == false) {
  856. Db::rollback();
  857. return error_show(1005, '未找到采购单数据');
  858. }
  859. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  860. if ($stock==false) {
  861. Db::rollback();
  862. return error_show(1005, '商品仓库未找到');
  863. }
  864. if($stock['usable_stock']+$stock['wait_out_stock']<$info['num']){
  865. Db::rollback();
  866. return error_show(1005, '商品可用库存不足退回数量');
  867. }else{
  868. if($stock['usable_stock']>$info['num']){
  869. $stock['usable_stock'] -= $info['num'];
  870. }else{
  871. $stock['usable_stock'] = 0;
  872. $stock['wait_out_stock']-= $info['num']- $stock['usable_stock'] ;
  873. }
  874. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  875. }
  876. $stock['updatetime'] = date("Y-m-d H:i:s");
  877. $st_up = Db::name("good_stock")->save($stock);
  878. if ($st_up == false) {
  879. Db::rollback();
  880. return error_show(1005, '可售商品入库失败');
  881. }
  882. }
  883. }
  884. ActionLog::logAdd($this->post['token'], [
  885. "order_code" => $orderinfo["orderCode"],//出库单号
  886. "status" => $lor,//这里的status是之前的值
  887. "action_remark" => '',//备注
  888. "action_type" => "status"//新建create,编辑edit,更改状态status
  889. ], "XSQRD", $orderinfo['status'], $orderinfo);
  890. ProcessOrder::AddProcess($this->post['token'], [
  891. "order_type" => 'XSQRD',
  892. "order_code" =>$orderinfo["orderCode"],//出库单号
  893. "order_id" => $orderinfo["id"],
  894. "order_status" =>$orderinfo['status'],"before_status"=>$lor
  895. ]);
  896. if($orderinfo['is_stock'] == 1 || $info['is_th'] == 0){
  897. $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm'];
  898. if ($wsmcode == "") {
  899. Db::rollback();
  900. return error_show(1005, '未找到退货仓库');
  901. }
  902. //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存
  903. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' => $wsmcode])->find();
  904. if (empty($stock)) {
  905. $stock = [
  906. "spuCode" => $info['good_code'],
  907. "wsm_code" => $wsmcode,
  908. "usable_stock" => 0,
  909. "wait_out_stock" => 0,
  910. "wait_in_stock" => 0,
  911. "total_stock" => 0,
  912. "addtime" => date("Y-m-d H:i:s"),
  913. "updatetime" => date("Y-m-d H:i:s"),
  914. ];
  915. }
  916. $stock['usable_stock'] += $info['num'];
  917. $stock['updatetime'] = date("Y-m-d H:i:s");
  918. $st_up = Db::name("good_stock")->save($stock);
  919. if ($st_up == false) {
  920. Db::rollback();
  921. return error_show(1005, '可售商品入库失败');
  922. }
  923. $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $info['num'], "stock_name" => "usable_stock"];
  924. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  925. }
  926. $data=[
  927. "orderCode"=>$info['orderCode'],
  928. "th_type"=>1,
  929. "th_num"=>$info['num'],
  930. "th_fee"=>round($info['num']*$orderinfo['sale_price'],2),
  931. "thCode"=>$info['returnCode'],
  932. "spuCode"=>$orderinfo['good_code'],
  933. "good_name"=>$orderinfo['good_name'],
  934. "cat_id"=>$orderinfo['cat_id'],
  935. "apply_id"=>$info['apply_id'],
  936. "apply_name"=>$info['apply_name'],
  937. "addtime"=>date("Y-m-d H:i:s"),
  938. "status"=>1,
  939. "is_del"=>0
  940. ];
  941. $inse=Db::name("th_data")->insert($data);
  942. if($inse==false){
  943. Db::rollback();
  944. return error_show(1004,"退货单更新失败");
  945. }
  946. }
  947. }
  948. Db::commit();
  949. return app_show(0,"更新成功");
  950. }catch (\Exception $e){
  951. Db::rollback();
  952. return error_show(1004,$e->getMessage()."|".$e->getLine());
  953. }
  954. }
  955. public function zxcreate(){
  956. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  957. if($ordeCode==''){
  958. return error_show(1004,"参数orderCode 不能为空");
  959. }
  960. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  961. if(empty($order)){
  962. return error_show(1005,"未找到订单数据");
  963. }
  964. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  965. if($errorCode==''){
  966. return error_show(1004,"参数errorCode 不能为空");
  967. }
  968. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  969. if(empty($error)){
  970. return error_show(1005,"未找到退货原因数据");
  971. }
  972. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  973. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  974. if($token==''){
  975. return error_show(105,"参数token不能为空");
  976. }
  977. $user =GetUserInfo($token);
  978. if(empty($user)||$user['code']!=0){
  979. return error_show(1002,"申请人数据不存在");
  980. }
  981. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  982. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  983. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  984. if($num==''){
  985. return error_show(1005,"参数num不能为空");
  986. }
  987. if($order['wsend_num']<$num){
  988. return error_show(1002,"仓库未发货数量不足退货");
  989. }
  990. $returnCode=makeNo("RS");
  991. Db::startTrans();
  992. try{
  993. $in = [
  994. "returnCode"=>$returnCode,
  995. "orderCode"=>$ordeCode,
  996. "good_code"=>$order['good_code'],
  997. "good_name"=>$order['good_name'],
  998. "apply_id"=>$rm,
  999. "apply_name"=>$ri,
  1000. "error_code"=>$errorCode,
  1001. "num"=>$num,
  1002. "remark"=>$remark,
  1003. "order_type"=>2,
  1004. "status"=>0,
  1005. "is_del"=>0,
  1006. "addtime"=>date("Y-m-d H:i:s"),
  1007. "updatetime"=>date("Y-m-d H:i:s")
  1008. ];
  1009. $create = Db::name("sale_return")->insert($in,true);
  1010. if($create>0) {
  1011. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0];
  1012. ProcessOrder::AddProcess($this->post['token'],$process);
  1013. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1014. "action_type"=>"create"];
  1015. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in);
  1016. //维护台账记录
  1017. Db::name('standing_book')
  1018. ->where('ordeCode', $ordeCode)
  1019. ->update([
  1020. 'returnGoodCode' => $returnCode,
  1021. 'updatetime' => date("Y-m-d H:i:s")
  1022. ]);
  1023. if ($order['send_type'] == 1) {
  1024. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1025. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1026. if(!empty($wsend)){
  1027. foreach ($wsend as $value){
  1028. $tt = $value['status'];
  1029. $value['status']=0;
  1030. $value['updatetime']=date("Y-m-d H:i:s");
  1031. $up =Db::name("order_out")->save($value);
  1032. if($up){
  1033. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt];
  1034. ProcessOrder::AddProcess($this->post['token'],$process);
  1035. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1036. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  1037. }else{
  1038. Db::rollback();
  1039. return error_show(1005,"退货单新建失败");
  1040. }
  1041. }
  1042. }
  1043. }
  1044. Db::commit();
  1045. return error_show(0,"退货单新建成功");
  1046. }
  1047. Db::rollback();
  1048. return error_show(1005,"退货单新建失败");
  1049. }catch (\Exception $e){
  1050. Db::rollback();
  1051. return error_show(1005,$e->getMessage());
  1052. }
  1053. }
  1054. }