Reorder.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. $data = isset($this->post['list'])&&$this->post['list']!='' ? $this->post['list'] :'';
  34. if($data=='' || empty($data)){
  35. return error_show(1005,"参数list不能为空");
  36. }
  37. $addr = isset($this->post['addr'])&&$this->post['addr']!='' ? $this->post['addr'] :'';
  38. if($addr=='' || empty($addr)){
  39. return error_show(1005,"参数addr不能为空");
  40. }
  41. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  42. if($token==''){
  43. return error_show(1005,"参数token不能为空");
  44. }
  45. $user =GetUserInfo($token);
  46. if(empty($user)||$user['code']!=0){
  47. return error_show(1002,"申请人数据不存在");
  48. }
  49. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  50. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  51. $total = array_sum(array_column($data,"return_num"));
  52. $addrnum = array_sum(array_column($addr,"return_num"));
  53. if($total!=$addrnum){
  54. return error_show(1002,"仓库退货数据与地址退货数据不一致");
  55. }
  56. // var_dump($order['good_code']);
  57. $returnCode=makeNo("RXS");
  58. Db::startTrans();
  59. try{
  60. $in = [
  61. "returnCode"=>$returnCode,
  62. "orderCode"=>$ordeCode,
  63. "good_code"=>$order['good_code'],
  64. "good_name"=>$order['good_name'],
  65. "apply_id"=>$rm,
  66. "apply_name"=>$ri,
  67. "error_code"=>$errorCode,
  68. "num"=>$total,
  69. "remark"=>$remark,
  70. "order_type"=>1,
  71. "status"=>0,
  72. "is_del"=>0,
  73. "addtime"=>date("Y-m-d H:i:s"),
  74. "updatetime"=>date("Y-m-d H:i:s")
  75. ];
  76. $create = Db::name("sale_return")->insert($in,true);
  77. if($create>0){
  78. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  79. ActionLog::logAdd($this->post['token'],$stn,"QRTHD",0,$stn);
  80. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'QRTHD'];
  81. ProcessOrder::AddProcess($this->post['token'],$process);
  82. $addrinfo=[];
  83. foreach ($addr as $value){
  84. $addrorder= Db::name("order_addr")->where(['orderCode'=>$ordeCode,"id"=>$value['addrid']])
  85. ->find();
  86. if(empty($addrorder)){
  87. Db::rollback();
  88. return error_show(1002,"订单地址数据不存在");
  89. }
  90. $total = Db::name("sale_returnaddr")->where(['orderCode'=>$ordeCode,"id"=>$value['addrid'],"is_del"=>0])
  91. ->sum("return_num");
  92. if($addrorder['receipt_quantity']-$total<$value['return_num']){
  93. Db::rollback();
  94. return error_show(1002,"订单地址退货数量超出发货数量");
  95. }
  96. $temp=[];
  97. $temp['returnCode']=$returnCode;
  98. $temp['orderCode']=$ordeCode;
  99. $temp['addrid']=$value['addrid'];
  100. $temp['return_num']=$value['return_num'];
  101. $temp['send_num']= $addrorder['receipt_quantity'];
  102. $temp['addtime']= date("Y-m-d H:i:s");
  103. $temp['updatetime']= date("Y-m-d H:i:s");
  104. $addrinfo[]=$temp;
  105. }
  106. $return = Db::name("sale_returnaddr")->insertAll($addrinfo);
  107. if($return<=0){
  108. Db::rollback();
  109. return error_show(1002,"订单地址退货数据更新失败");
  110. }
  111. $info=[];
  112. foreach ($data as $value){
  113. $wsmorder= Db::name("sale_info")->where(['orderCode'=>$ordeCode,"wsm_code"=>$value['wsm_code']])
  114. ->find();
  115. if(empty($wsmorder)){
  116. Db::rollback();
  117. return error_show(1002,"订单仓库数据不存在");
  118. }
  119. $total_wsm = Db::name("sale_returninfo")->where(['orderCode'=>$ordeCode,"wsm_code"=>$value['wsm_code'],"is_del"=>0])
  120. ->sum("return_num");
  121. if($wsmorder['num']-$total_wsm<$value['return_num']){
  122. Db::rollback();
  123. return error_show(1002,"仓库退货数量超出发货数量");
  124. }
  125. $temp=[];
  126. $temp['returnCode']=$returnCode;
  127. $temp['orderCode']=$ordeCode;
  128. $temp['wsm_code']=$value['wsm_code'];
  129. $temp['return_num']=$value['return_num'];
  130. $temp['total_num']= $wsmorder['num'];
  131. $temp['addtime']= date("Y-m-d H:i:s");
  132. $temp['updatetime']= date("Y-m-d H:i:s");
  133. $info[]=$temp;
  134. }
  135. $return = Db::name("sale_returninfo")->insertAll($info);
  136. if($return){
  137. Db::commit();
  138. return app_show(0,"退货单新建成功");
  139. }
  140. }
  141. Db::rollback();
  142. return error_show(1005,"退货单新建失败");
  143. }catch (\Exception $e){
  144. Db::rollback();
  145. return error_show(1005,$e->getMessage());
  146. }
  147. }
  148. public function list(){
  149. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  150. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  151. $where =[['is_del',"=",0]];
  152. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  153. if($bkcode!=""){
  154. $where[]=['returnCode',"like", "%{$bkcode}%"];
  155. }
  156. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  157. if($status!==""){
  158. $where[]=['status',"=", $status];
  159. }
  160. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  161. :"";
  162. if($orderCode!=""){
  163. $where[]=['orderCode',"like", "%{$orderCode}%"];
  164. }
  165. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  166. :"";
  167. if($apply_name!=""){
  168. $where[]=['apply_name',"like", "%{$apply_name}%"];
  169. }
  170. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  171. if($start!==""){
  172. $where[]=['addtime',">=", $start];
  173. }
  174. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  175. if($end!==""){
  176. $where[]=['addtime',"<=", $end];
  177. }
  178. $count=Db::name("sale_return")->where($where)->count();
  179. $total = ceil($count/$size);
  180. $page = $total>=$page ? $page :$total;
  181. $list = Db::name("sale_return")->where($where)->order("addtime desc")->page($page,$size)->select();
  182. // echo Db::name("sale_return")->getLastSql();
  183. $data=[];
  184. foreach ($list as $value){
  185. $value['error_msg']='';
  186. if($value['error_code']!=''){
  187. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  188. $value['error_msg']= isset($error['result'])?$error['result']:"";
  189. }
  190. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  191. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  192. $value['return_total'] =$value['sale_price']*$value['num'] ;
  193. $value['total_num'] =$order['good_num'] ;
  194. $data[]=$value ;
  195. }
  196. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  197. }
  198. public function info(){
  199. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  200. :"";
  201. if($code==""){
  202. return error_show(1004,"参数returnCode不能为空");
  203. }
  204. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  205. if(empty($info)){
  206. return error_show(1004,"未找到退货数据");
  207. }
  208. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  209. $goon = Db::name('good')->alias('b')->join("good_type a", "a.good_code=b.good_code", "left")
  210. ->where(['a.type_code' => $info['good_code']])->find();
  211. if (empty($goon)) {
  212. return error_show(1003, "未找到商品数据");
  213. }
  214. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  215. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  216. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  217. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  218. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  219. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  220. $info['return_total'] = $info['sale_price']*$info['num'] ;
  221. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  222. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  223. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  224. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  225. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  226. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  227. $info['customer_name']='';
  228. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  229. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  230. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  231. }
  232. $info['cgd_wsend']="";
  233. $info['cgd_send']="";
  234. $info['cgd_total']="";
  235. if($orderinfo['order_type']==2){
  236. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  237. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  238. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  239. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  240. }
  241. $info['error_msg']='';
  242. if($info['error_code']!=''){
  243. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  244. $info['error_msg']= isset($error['result'])?$error['result']:"";
  245. }
  246. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  247. $wsm=[];
  248. if(!empty($wsm_return)){
  249. foreach ($wsm_return as $value){
  250. $value['wsm_name']="";
  251. $value['wsm_supplier']="";
  252. $value['wsm_supplierNo']="";
  253. if($value['wsm_code']!=""){
  254. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  255. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  256. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  257. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  258. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  259. }
  260. $wsm[]=$value;
  261. }
  262. }
  263. $info['wsminfo']=$wsm;
  264. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  265. $addrinfo=[];
  266. if(!empty($addr)){
  267. foreach ( $addr as $value){
  268. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  269. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  270. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  271. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  272. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  273. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  274. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  275. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  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,"QRTHD",0,$ste);
  307. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>'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. if($info['order_type']==2){
  338. $wsend_th = isset($this->post['wsend_th']) && $this->post['wsend_th'] !=="" ? intval($this->post['wsend_th']) :"";
  339. if($status==3){
  340. if($wsend_th===''){
  341. return error_show(1004,"参数wsend_th不能为空");
  342. }
  343. $info['wsend_th'] =$wsend_th;
  344. }
  345. $return_wsm = isset($this->post['return_wsm']) && $this->post['return_wsm'] !="" ? trim($this->post['return_wsm']) :"";
  346. if($status==4){
  347. if($return_wsm===''){
  348. return error_show(1004,"参数return_wsm不能为空");
  349. }
  350. $info['return_wsm'] =$return_wsm;
  351. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info['orderCode'],"order_type"=>2])->find();
  352. if(empty($cgd)){
  353. return error_show(1002,"未找到采购单数据");
  354. }
  355. $gd= Db::name('good')->where(['good_code'=>$cgd['good_code']])->find();
  356. if($gd==""){
  357. return error_show(1002,"未找到商品数据");
  358. }
  359. }
  360. }
  361. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  362. $apply_id =GetUserInfo($token);
  363. if(empty($apply_id)||$apply_id['code']!=0){
  364. return error_show(1002,"申请人数据不存在");
  365. }
  366. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  367. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  368. Db::startTrans();
  369. try{
  370. $temp= $info['status'];
  371. $info['status'] =$status;
  372. $info['remark'] =$remark;
  373. $info["updatetime"]=date("Y-m-d H:i:s");
  374. $up = Db::name("sale_return")->save($info);
  375. if($up){
  376. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>'QRTHD'];
  377. ProcessOrder::AddProcess($this->post['token'],$process);
  378. $ste = ["order_code"=>$code,"status"=>$temp,"action_remark"=>'',"action_type"=>"status"];
  379. ActionLog::logAdd($this->post['token'],$ste,"QRTHD",$status,$info);
  380. if($info['order_type']==2&&$status==4){
  381. $returnCode=makeNo("TH");
  382. $data = [
  383. "returnCode"=>$returnCode,
  384. "cgdNo"=>$cgd['cgdNo'],
  385. "good_code"=>$cgd['good_type_code'],
  386. "return_num"=>$cgd['wsend_num'],
  387. "good_name"=>$cgd['good_name'],
  388. "good_price"=>$cgd['good_price'],
  389. "good_catid"=>$gd['cat_id'],
  390. "apply_name"=>$rname,
  391. "apply_id"=>$rid,
  392. "good_total"=>$cgd['good_num'],
  393. "post_company"=>"",
  394. "post_code"=>"",
  395. "post_fee"=>"",
  396. "status"=>1,
  397. "remark"=>'',
  398. "is_del"=>0,
  399. "addtime"=>date("Y-m-d H:i:s"),
  400. "updatetime"=>date("Y-m-d H:i:s")
  401. ];
  402. $pd = Db::name('purchease_back')->insert($data,true);
  403. if($pd>0){
  404. $process=["order_code"=>$returnCode,"order_id"=>$pd,"order_status"=>1,"order_type"=>'CGTHD'];
  405. ProcessOrder::AddProcess($this->post['token'],$process);
  406. $ste = ["order_code"=>$returnCode,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  407. ActionLog::logAdd($this->post['token'],$ste,"CGTHD",1,$data);
  408. $cg_status=$cgd['status'];
  409. $cgd['wsend_num']=0;
  410. $cgd['status']=3;
  411. $cgd['send_status']=3;
  412. $cgd['updatetime']=date("Y-m-d H:i:s");
  413. $up=Db::name('purchease_order')->save($cgd);
  414. if($up){
  415. $process=["order_code"=>$cgd['cgdNo'],"order_id"=>$cgd['id'],"order_status"=>1,"order_type"=>'CGD'];
  416. ProcessOrder::AddProcess($this->post['token'],$process);
  417. $ste = ["order_code"=>$cgd['cgdNo'],"status"=>$cg_status,"action_remark"=>'',
  418. "action_type"=>"edit"];
  419. ActionLog::logAdd($this->post['token'],$ste,"CGD",3,$data);
  420. Db::commit();
  421. return app_show(0,"更新成功");
  422. }else{
  423. Db::rollback();
  424. return error_show(1004,"更新失败");
  425. }
  426. }else{
  427. Db::rollback();
  428. return error_show(1004,"更新失败");
  429. }
  430. }
  431. Db::commit();
  432. return app_show(0,"更新成功");
  433. }else{
  434. Db::rollback();
  435. return error_show(1004,"更新失败");
  436. }
  437. }catch (\Exception $e){
  438. Db::rollback();
  439. return error_show(1004,"更新失败");
  440. }
  441. }
  442. public function zxcreate(){
  443. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  444. if($ordeCode==''){
  445. return error_show(1004,"参数orderCode 不能为空");
  446. }
  447. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  448. if(empty($order)){
  449. return error_show(1005,"未找到订单数据");
  450. }
  451. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  452. if($errorCode==''){
  453. return error_show(1004,"参数errorCode 不能为空");
  454. }
  455. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  456. if(empty($error)){
  457. return error_show(1005,"未找到退货原因数据");
  458. }
  459. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  460. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  461. if($token==''){
  462. return error_show(1005,"参数token不能为空");
  463. }
  464. $user =GetUserInfo($token);
  465. if(empty($user)||$user['code']!=0){
  466. return error_show(1002,"申请人数据不存在");
  467. }
  468. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  469. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  470. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  471. if($num==''){
  472. return error_show(1005,"参数num不能为空");
  473. }
  474. if($order['wsend_num']<$num){
  475. return error_show(1002,"仓库未发货数量不足退货");
  476. }
  477. $returnCode=makeNo("RXS");
  478. Db::startTrans();
  479. try{
  480. $in = [
  481. "returnCode"=>$returnCode,
  482. "orderCode"=>$ordeCode,
  483. "good_code"=>$order['good_code'],
  484. "good_name"=>$order['good_name'],
  485. "apply_id"=>$rm,
  486. "apply_name"=>$ri,
  487. "error_code"=>$errorCode,
  488. "num"=>$num,
  489. "remark"=>$remark,
  490. "order_type"=>2,
  491. "status"=>0,
  492. "is_del"=>0,
  493. "addtime"=>date("Y-m-d H:i:s"),
  494. "updatetime"=>date("Y-m-d H:i:s")
  495. ];
  496. $create = Db::name("sale_return")->insert($in,true);
  497. if($create>0) {
  498. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'QRTHD'];
  499. ProcessOrder::AddProcess($this->post['token'],$process);
  500. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  501. "action_type"=>"create"];
  502. ActionLog::logAdd($this->post['token'],$ste,"QRTHD",3,$in);
  503. if ($order['send_type'] == 1) {
  504. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"
  505. =>2])->select();
  506. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  507. if(!empty($wsend)){
  508. foreach ($wsend as $value){
  509. $tt = $value['status'];
  510. $value['status']=0;
  511. $value['updatetime']=date("Y-m-d H:i:s");
  512. $up =Db::name("order_out")->save($value);
  513. if($up){
  514. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD'];
  515. ProcessOrder::AddProcess($this->post['token'],$process);
  516. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  517. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  518. }else{
  519. Db::rollback();
  520. return error_show(1005,"退货单新建失败");
  521. }
  522. }
  523. }
  524. }
  525. Db::commit();
  526. return error_show(0,"退货单新建成功");
  527. }
  528. Db::rollback();
  529. return error_show(1005,"退货单新建失败");
  530. }catch (\Exception $e){
  531. Db::rollback();
  532. return error_show(1005,$e->getMessage());
  533. }
  534. }
  535. }