Reorder.php 27 KB

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