Reorder.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ProcessOrder;
  4. use think\facade\Db;
  5. use think\App;
  6. use app\admin\model\ActionLog;
  7. class Reorder extends \app\BaseController
  8. {
  9. public $post=[];
  10. public function __construct(App $app)
  11. {
  12. parent::__construct($app);
  13. $this->post =$this->request->post();
  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. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  25. if($errorCode==''){
  26. return error_show(1004,"参数errorCode 不能为空");
  27. }
  28. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  29. if(empty($error)){
  30. return error_show(1005,"未找到退货原因数据");
  31. }
  32. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  33. $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :"";
  34. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  35. if($token==''){
  36. return error_show(105,"参数token不能为空");
  37. }
  38. $user =GetUserInfo($token);
  39. if(empty($user)||$user['code']!=0){
  40. return error_show(1002,"申请人数据不存在");
  41. }
  42. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  43. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  44. $returnadr =isset($this->post['returnAddr'])&&$this->post['returnAddr']!="" ? $this->post['returnAddr']:"";
  45. if($returnadr!=''){
  46. $thnum=array_sum(array_column($returnadr,"return_num"));
  47. }
  48. $returnCode=makeNo("RS");
  49. Db::startTrans();
  50. try{
  51. $in = [
  52. "returnCode"=>$returnCode,
  53. "orderCode"=>$ordeCode,
  54. "good_code"=>$order['good_code'],
  55. "good_name"=>$order['good_name'],
  56. "apply_id"=>$rm,
  57. "apply_name"=>$ri,
  58. "error_code"=>$errorCode,
  59. "num"=>$thnum,
  60. "remark"=>$remark,
  61. "order_type"=>$order['order_type'],
  62. "status"=>0,
  63. "is_del"=>0,
  64. "addtime"=>date("Y-m-d H:i:s"),
  65. "updatetime"=>date("Y-m-d H:i:s")
  66. ];
  67. $create = Db::name("sale_return")->insert($in,true);
  68. if($create>0){
  69. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  70. ActionLog::logAdd($this->post['token'],$stn,"QRTHD",0,$stn);
  71. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'QRTHD'];
  72. ProcessOrder::AddProcess($this->post['token'],$process);
  73. if($returnadr!=""){
  74. $inf=[];
  75. foreach ($returnadr as $val){
  76. $temp=[];
  77. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  78. if($addrinfo==false){
  79. Db::rollback();
  80. return error_show(1005,"地址信息未找到");
  81. }
  82. $send =Db::name('order_out')->where([["addrid","=",$val['id']],["status",">=",2]])->sum('send_num');
  83. if($send!=0 && $addrinfo['receipt_quantity']<$send+$val['return_num']){
  84. Db::rollback();
  85. return error_show(1005,"地址已发货");
  86. }
  87. $temp['returnCode']=$returnCode;
  88. $temp['orderCode']=$ordeCode;
  89. $temp['addrid']=$val['id'];
  90. $temp['send_num']=$addrinfo['receipt_quantity'];
  91. $temp['return_num']=$val['return_num'];
  92. $temp['is_del']=0;
  93. $temp['addtime']=date("Y-m-d H:i:s");
  94. $temp['updatetime']=date("Y-m-d H:i:s");
  95. $inf[]=$temp;
  96. }
  97. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  98. if($inadd==0){
  99. Db::rollback();
  100. return error_show(1005,"退货单新建失败");
  101. }
  102. }
  103. Db::commit();
  104. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  105. }
  106. Db::rollback();
  107. return error_show(1005,"退货单新建失败");
  108. }catch (\Exception $e){
  109. Db::rollback();
  110. return error_show(1005,$e->getMessage());
  111. }
  112. }
  113. public function list(){
  114. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  115. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  116. $where =[['is_del',"=",0]];
  117. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  118. if($bkcode!=""){
  119. $where[]=['returnCode',"like", "%{$bkcode}%"];
  120. }
  121. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  122. if($status!==""){
  123. $where[]=['status',"=", $status];
  124. }
  125. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  126. :"";
  127. if($orderCode!=""){
  128. $where[]=['orderCode',"like", "%{$orderCode}%"];
  129. }
  130. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  131. :"";
  132. if($apply_name!=""){
  133. $where[]=['apply_name',"like", "%{$apply_name}%"];
  134. }
  135. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  136. if($start!==""){
  137. $where[]=['addtime',">=", $start];
  138. }
  139. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  140. if($end!==""){
  141. $where[]=['addtime',"<=", $end];
  142. }
  143. $count=Db::name("sale_return")->where($where)->count();
  144. $total = ceil($count/$size);
  145. $page = $total>=$page ? $page :$total;
  146. $list = Db::name("sale_return")->where($where)->order("addtime desc")->page($page,$size)->select();
  147. // echo Db::name("sale_return")->getLastSql();
  148. $data=[];
  149. foreach ($list as $value){
  150. $value['error_msg']='';
  151. if($value['error_code']!=''){
  152. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  153. $value['error_msg']= isset($error['result'])?$error['result']:"";
  154. }
  155. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  156. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  157. $value['return_total'] =$value['sale_price']*$value['num'] ;
  158. $value['total_num'] =$order['good_num'] ;
  159. $data[]=$value ;
  160. }
  161. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  162. }
  163. public function info(){
  164. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  165. :"";
  166. if($code==""){
  167. return error_show(1004,"参数returnCode不能为空");
  168. }
  169. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  170. if(empty($info)){
  171. return error_show(1004,"未找到退货数据");
  172. }
  173. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  174. if($orderinfo['order_type']==3|| $orderinfo['order_type']==4){
  175. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  176. }else {
  177. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  178. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  179. }
  180. if ($goon==false) {
  181. return error_show(1003, "未找到商品数据");
  182. }
  183. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  184. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  185. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  186. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  187. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  188. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  189. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  190. $info['return_total'] = $info['sale_price']*$info['num'] ;
  191. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  192. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  193. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  194. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  195. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  196. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  197. $info['customer_name']='';
  198. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  199. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  200. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  201. }
  202. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  203. $info['supplier_name']='';
  204. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  205. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  206. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  207. }
  208. $info['platform_name']='';
  209. $info['platform_id']=$orderinfo['platform_id'];
  210. if($orderinfo['platform_id']!=0){
  211. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  212. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  213. }
  214. $info['cgd_wsend']="";
  215. $info['cgd_send']="";
  216. $info['cgd_total']="";
  217. if($orderinfo['order_type']==2){
  218. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  219. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  220. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  221. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  222. }
  223. $info['error_msg']='';
  224. if($info['error_code']!=''){
  225. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  226. $info['error_msg']= isset($error['result'])?$error['result']:"";
  227. }
  228. $info['wsm_name']='';
  229. if($info['return_wsm']!=""){
  230. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  231. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  232. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  233. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  234. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  235. }
  236. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  237. $wsm=[];
  238. if(!empty($wsm_return)){
  239. foreach ($wsm_return as $value){
  240. $value['wsm_name']="";
  241. $value['wsm_supplier']="";
  242. $value['wsm_supplierNo']="";
  243. if($value['wsm_code']!=""){
  244. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  245. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  246. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  247. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  248. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  249. }
  250. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  251. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  252. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  253. $value['wsm_send'] = $send ?? 0;
  254. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  255. $wsm[]=$value;
  256. }
  257. }
  258. $info['wsminfo']=$wsm;
  259. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  260. $addrinfo=[];
  261. if(!empty($addr)){
  262. foreach ( $addr as $value){
  263. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  264. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  265. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  266. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  267. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  268. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  269. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  270. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  271. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  272. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode']])->sum("send_num");
  273. $value['addr_send'] = $send ?? 0;
  274. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  275. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  276. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  277. $addrinfo[]=$value;
  278. }
  279. }
  280. $info['addrinfo']=$addrinfo;
  281. $info['can']=$int;
  282. return app_show(0,"获取成功",$info);
  283. }
  284. /**
  285. * @return \think\response\Json|void
  286. * @throws \think\db\exception\DataNotFoundException
  287. * @throws \think\db\exception\DbException
  288. * @throws \think\db\exception\ModelNotFoundException
  289. */
  290. public function delete(){
  291. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  292. :"";
  293. if($code==""){
  294. return error_show(1004,"参数returnCode不能为空");
  295. }
  296. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  297. if(empty($info)){
  298. return error_show(1004,"未找到退货数据");
  299. }
  300. $info["is_del"]=1;
  301. $info["updatetime"]=date("Y-m-d H:i:s");
  302. $del = Db::name("sale_return")->save($info);
  303. if($del){
  304. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  305. ActionLog::logAdd($this->post['token'],$ste,$info['order_type'] == 2?"ZXTHD":"QRTHD",0,$ste);
  306. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>$info['order_type'] == 2?"ZXTHD":"QRTHD"];
  307. ProcessOrder::workdel($process);
  308. return app_show(0,"删除成功");
  309. }else{
  310. return error_show(1004,"删除失败");
  311. }
  312. }
  313. /**审核
  314. * @return \think\response\Json|void
  315. * @throws \think\db\exception\DataNotFoundException
  316. * @throws \think\db\exception\DbException
  317. * @throws \think\db\exception\ModelNotFoundException
  318. */
  319. public function exam(){
  320. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  321. :"";
  322. if($code==""){
  323. return error_show(1004,"参数returnCode不能为空");
  324. }
  325. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  326. if(empty($info)){
  327. return error_show(1004,"未找到退货数据");
  328. }
  329. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  330. :"";
  331. if($status===""){
  332. return error_show(1004,"参数status不能为空");
  333. }
  334. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  335. $var = $info['status'];
  336. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  337. $apply_id =GetUserInfo($token);
  338. if(empty($apply_id)||$apply_id['code']!=0){
  339. return error_show(1002,"申请人数据不存在");
  340. }
  341. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  342. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  343. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  344. if($orderinfo==false){
  345. return error_show(1004,"未找到订单数据");
  346. }
  347. if($status==4){
  348. $cgd=Db::name("order_num")->where(['orderCode'=>$info["orderCode"]])->order("send_num asc")->column('cgdNo');
  349. if(empty($cgd)){
  350. return error_show(1004,"未找到商品库存数据");
  351. }
  352. $wsm =Db::name("purchease_order")->where(["cgdNo"=>$cgd])->column("wsm_code");
  353. if(empty($wsm)){
  354. return error_show(1004,"未找到商品仓库库存数据");
  355. }
  356. $good_stock = Db::name("good_stock")->where(["spuCode"=>$orderinfo['good_code'],"wsm_code"=>$wsm,"is_del"=>0])
  357. ->select()->toArray();
  358. if(empty($good_stock)){
  359. return error_show(1004,"未找到商品库存数据");
  360. }
  361. }
  362. if($status==3){
  363. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  364. if($is_th===""){
  365. return error_show(1004,"参数is_th不能为空");
  366. }
  367. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  368. if($return_wsm===""){
  369. return error_show(1004,"参数return_wsm 不能为空");
  370. }
  371. $wsmcode = Db::name("warehouse_info")->where(["wsm_code"=>$return_wsm])->find();
  372. if($wsmcode==false){
  373. return error_show(1004,"未找到对应得仓库");
  374. }
  375. $info['return_wsm']=$return_wsm;
  376. $info['is_th']=$is_th;
  377. }
  378. Db::startTrans();
  379. try{
  380. $temp= $info['status'];
  381. $info['status'] =$status;
  382. $remark!=""? $info['remark'] =$remark:"";
  383. $info["updatetime"]=date("Y-m-d H:i:s");
  384. $up = Db::name("sale_return")->save($info);
  385. if($up){
  386. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>$info['order_type'] == 2?"ZXTHD":"QRTHD"];
  387. ProcessOrder::AddProcess($this->post['token'],$process);
  388. $ste = ["order_code"=>$code,"status"=>$temp,"action_remark"=>'',"action_type"=>"status"];
  389. ActionLog::logAdd($this->post['token'],$ste,$info['order_type'] == 2?"ZXTHD":"QRTHD",$status,$info);
  390. if($status==4){
  391. $orderinfo['wsend_num']-=$info['num'];
  392. $orderinfo['th_num']+=$info['num'];
  393. $orderinfo['send_status'] = $orderinfo['send_num'] == 0 ? 1 : $orderinfo['wsend_num'] == 0 ? 3 : 2;
  394. $orderinfo['status'] = $orderinfo['send_num'] == 0 ? 3 : $orderinfo['wsend_num'] == 0 ? 5 : 4;
  395. $orderinfo['updatetime']=date("Y-m-d H:i:s");
  396. $return = Db::name("sale")->save($orderinfo);
  397. if($return==false){
  398. Db::rollback();
  399. return error_show(1004,"更新失败");
  400. }
  401. foreach ($good_stock as $value){
  402. if($value['wait_out_stock']>=$info['num']){
  403. $value['usable_stock']+=$info['num'];
  404. $value['wait_out_stock']-=$info['num'];
  405. $value['updatetime']=date("Y-m-d H:i:s");
  406. $info["num"]=0;
  407. }else{
  408. $value['usable_stock']+=$value['wait_out_stock'];
  409. $value['wait_out_stock']=0;
  410. $value['updatetime']=date("Y-m-d H:i:s");
  411. $info["num"]-=$value['wait_out_stock'];
  412. }
  413. $stock = Db::name("good_stock")->save($value);
  414. if($stock==false){
  415. Db::rollback();
  416. return error_show(1004,"更新失败");
  417. }
  418. if($info['num']==0){
  419. break;
  420. }
  421. }
  422. }
  423. Db::commit();
  424. return app_show(0,"更新成功");
  425. }
  426. Db::rollback();
  427. return error_show(1004,"更新失败");
  428. }catch (\Exception $e){
  429. Db::rollback();
  430. return error_show(1004,"更新失败");
  431. }
  432. }
  433. public function zxcreate(){
  434. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  435. if($ordeCode==''){
  436. return error_show(1004,"参数orderCode 不能为空");
  437. }
  438. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  439. if(empty($order)){
  440. return error_show(1005,"未找到订单数据");
  441. }
  442. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  443. if($errorCode==''){
  444. return error_show(1004,"参数errorCode 不能为空");
  445. }
  446. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  447. if(empty($error)){
  448. return error_show(1005,"未找到退货原因数据");
  449. }
  450. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  451. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  452. if($token==''){
  453. return error_show(105,"参数token不能为空");
  454. }
  455. $user =GetUserInfo($token);
  456. if(empty($user)||$user['code']!=0){
  457. return error_show(1002,"申请人数据不存在");
  458. }
  459. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  460. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  461. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  462. if($num==''){
  463. return error_show(1005,"参数num不能为空");
  464. }
  465. if($order['wsend_num']<$num){
  466. return error_show(1002,"仓库未发货数量不足退货");
  467. }
  468. $returnCode=makeNo("RS");
  469. Db::startTrans();
  470. try{
  471. $in = [
  472. "returnCode"=>$returnCode,
  473. "orderCode"=>$ordeCode,
  474. "good_code"=>$order['good_code'],
  475. "good_name"=>$order['good_name'],
  476. "apply_id"=>$rm,
  477. "apply_name"=>$ri,
  478. "error_code"=>$errorCode,
  479. "num"=>$num,
  480. "remark"=>$remark,
  481. "order_type"=>2,
  482. "status"=>0,
  483. "is_del"=>0,
  484. "addtime"=>date("Y-m-d H:i:s"),
  485. "updatetime"=>date("Y-m-d H:i:s")
  486. ];
  487. $create = Db::name("sale_return")->insert($in,true);
  488. if($create>0) {
  489. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'ZXTHD'];
  490. ProcessOrder::AddProcess($this->post['token'],$process);
  491. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  492. "action_type"=>"create"];
  493. ActionLog::logAdd($this->post['token'],$ste,"ZXTHD",3,$in);
  494. if ($order['send_type'] == 1) {
  495. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"
  496. =>2])->select();
  497. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  498. if(!empty($wsend)){
  499. foreach ($wsend as $value){
  500. $tt = $value['status'];
  501. $value['status']=0;
  502. $value['updatetime']=date("Y-m-d H:i:s");
  503. $up =Db::name("order_out")->save($value);
  504. if($up){
  505. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD'];
  506. ProcessOrder::AddProcess($this->post['token'],$process);
  507. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  508. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  509. }else{
  510. Db::rollback();
  511. return error_show(1005,"退货单新建失败");
  512. }
  513. }
  514. }
  515. }
  516. Db::commit();
  517. return error_show(0,"退货单新建成功");
  518. }
  519. Db::rollback();
  520. return error_show(1005,"退货单新建失败");
  521. }catch (\Exception $e){
  522. Db::rollback();
  523. return error_show(1005,$e->getMessage());
  524. }
  525. }
  526. }