Reorder.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\App;
  5. use app\admin\model\ActionLog;
  6. class Reorder extends \app\BaseController
  7. {
  8. public $post=[];
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->post =$this->request->post();
  13. }
  14. public function create(){
  15. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  16. if($ordeCode==''){
  17. return error_show(1004,"参数orderCode 不能为空");
  18. }
  19. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  20. if(empty($order)){
  21. return error_show(1005,"未找到订单数据");
  22. }
  23. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  24. if($errorCode==''){
  25. return error_show(1004,"参数errorCode 不能为空");
  26. }
  27. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  28. if(empty($error)){
  29. return error_show(1005,"未找到退货原因数据");
  30. }
  31. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  32. $data = isset($this->post['list'])&&$this->post['list']!='' ? $this->post['list'] :'';
  33. if($data=='' || empty($data)){
  34. return error_show(1005,"参数list不能为空");
  35. }
  36. $addr = isset($this->post['addr'])&&$this->post['addr']!='' ? $this->post['addr'] :'';
  37. if($addr=='' || empty($addr)){
  38. return error_show(1005,"参数addr不能为空");
  39. }
  40. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  41. if($token==''){
  42. return error_show(1005,"参数token不能为空");
  43. }
  44. $user =GetUserInfo($token);
  45. if(empty($user)||$user['code']!=0){
  46. return error_show(1002,"申请人数据不存在");
  47. }
  48. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  49. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  50. $total = array_sum(array_column($data,"return_num"));
  51. $addrnum = array_sum(array_column($addr,"return_num"));
  52. if($total!=$addrnum){
  53. return error_show(1002,"仓库退货数据与地址退货数据不一致");
  54. }
  55. // var_dump($order['good_code']);
  56. $returnCode=makeNo("RXS");
  57. Db::startTrans();
  58. try{
  59. $in = [
  60. "returnCode"=>$returnCode,
  61. "orderCode"=>$ordeCode,
  62. "good_code"=>$order['good_code'],
  63. "good_name"=>$order['good_name'],
  64. "apply_id"=>$rm,
  65. "apply_name"=>$ri,
  66. "error_code"=>$errorCode,
  67. "num"=>$total,
  68. "remark"=>$remark,
  69. "order_type"=>1,
  70. "status"=>0,
  71. "is_del"=>0,
  72. "addtime"=>date("Y-m-d H:i:s"),
  73. "updatetime"=>date("Y-m-d H:i:s")
  74. ];
  75. $create = Db::name("sale_return")->insert($in);
  76. if($create){
  77. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  78. ActionLog::logAdd($this->post['token'],$stn,"xsd",0,$stn);
  79. $addrinfo=[];
  80. foreach ($addr as $value){
  81. $addrorder= Db::name("order_addr")->where(['orderCode'=>$ordeCode,"id"=>$value['addrid']])
  82. ->find();
  83. if(empty($addrorder)){
  84. Db::rollback();
  85. return error_show(1002,"订单地址数据不存在");
  86. }
  87. $total = Db::name("sale_returnaddr")->where(['orderCode'=>$ordeCode,"id"=>$value['addrid'],"is_del"=>0])
  88. ->sum("return_num");
  89. if($addrorder['receipt_quantity']-$total<$value['return_num']){
  90. Db::rollback();
  91. return error_show(1002,"订单地址退货数量超出发货数量");
  92. }
  93. $temp=[];
  94. $temp['returnCode']=$returnCode;
  95. $temp['orderCode']=$ordeCode;
  96. $temp['addrid']=$value['addrid'];
  97. $temp['return_num']=$value['return_num'];
  98. $temp['send_num']= $addrorder['receipt_quantity'];
  99. $temp['addtime']= date("Y-m-d H:i:s");
  100. $temp['updatetime']= date("Y-m-d H:i:s");
  101. $addrinfo[]=$temp;
  102. }
  103. $return = Db::name("sale_returnaddr")->insertAll($addrinfo);
  104. if($return<=0){
  105. $str = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  106. ActionLog::logAdd($this->post['token'],$str,"xsd",0,$str);
  107. Db::rollback();
  108. return error_show(1002,"订单地址退货数据更新失败");
  109. }
  110. $info=[];
  111. foreach ($data as $value){
  112. $wsmorder= Db::name("sale_info")->where(['orderCode'=>$ordeCode,"wsm_code"=>$value['wsm_code']])
  113. ->find();
  114. if(empty($wsmorder)){
  115. Db::rollback();
  116. return error_show(1002,"订单仓库数据不存在");
  117. }
  118. $total_wsm = Db::name("sale_returninfo")->where(['orderCode'=>$ordeCode,"wsm_code"=>$value['wsm_code'],"is_del"=>0])
  119. ->sum("return_num");
  120. if($wsmorder['num']-$total_wsm<$value['return_num']){
  121. Db::rollback();
  122. return error_show(1002,"仓库退货数量超出发货数量");
  123. }
  124. $temp=[];
  125. $temp['returnCode']=$returnCode;
  126. $temp['orderCode']=$ordeCode;
  127. $temp['wsm_code']=$value['wsm_code'];
  128. $temp['return_num']=$value['return_num'];
  129. $temp['total_num']= $wsmorder['num'];
  130. $temp['addtime']= date("Y-m-d H:i:s");
  131. $temp['updatetime']= date("Y-m-d H:i:s");
  132. $info[]=$temp;
  133. }
  134. $return = Db::name("sale_returninfo")->insertAll($info);
  135. if($return){
  136. $stm = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  137. ActionLog::logAdd($this->post['token'],$stm,"xsd",0,$stm);
  138. Db::commit();
  139. return app_show(0,"退货单新建成功");
  140. }
  141. }
  142. Db::rollback();
  143. return error_show(1005,"退货单新建失败");
  144. }catch (\Exception $e){
  145. Db::rollback();
  146. return error_show(1005,$e->getMessage());
  147. }
  148. }
  149. public function list(){
  150. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  151. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  152. $where =[['is_del',"=",0]];
  153. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  154. if($bkcode!=""){
  155. $where[]=['returnCode',"like", "%{$bkcode}%"];
  156. }
  157. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  158. if($status!==""){
  159. $where[]=['status',"=", $status];
  160. }
  161. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  162. :"";
  163. if($orderCode!=""){
  164. $where[]=['orderCode',"like", "%{$orderCode}%"];
  165. }
  166. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  167. :"";
  168. if($apply_name!=""){
  169. $where[]=['apply_name',"like", "%{$apply_name}%"];
  170. }
  171. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  172. if($start!==""){
  173. $where[]=['addtime',">=", $start];
  174. }
  175. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  176. if($end!==""){
  177. $where[]=['addtime',"<=", $end];
  178. }
  179. $count=Db::name("sale_return")->where($where)->count();
  180. $total = ceil($count/$size);
  181. $page = $total>=$page ? $page :$total;
  182. $list = Db::name("sale_return")->where($where)->order("addtime desc")->page($page,$size)->select();
  183. // echo Db::name("sale_return")->getLastSql();
  184. $data=[];
  185. foreach ($list as $value){
  186. $value['error_msg']='';
  187. if($value['error_code']!=''){
  188. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  189. $value['error_msg']= isset($error['result'])?$error['result']:"";
  190. }
  191. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  192. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  193. $value['return_total'] =$value['sale_price']*$value['num'] ;
  194. $value['total_num'] =$order['good_num'] ;
  195. $data[]=$value ;
  196. }
  197. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  198. }
  199. public function info(){
  200. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  201. :"";
  202. if($code==""){
  203. return error_show(1004,"参数returnCode不能为空");
  204. }
  205. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  206. if(empty($info)){
  207. return error_show(1004,"未找到退货数据");
  208. }
  209. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  210. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  211. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  212. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  213. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  214. $info['return_total'] = $info['sale_price']*$info['num'] ;
  215. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  216. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  217. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  218. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  219. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  220. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  221. $info['customer_name']='';
  222. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  223. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  224. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  225. }
  226. $info['error_msg']='';
  227. if($info['error_code']!=''){
  228. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  229. $info['error_msg']= isset($error['result'])?$error['result']:"";
  230. }
  231. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  232. $wsm=[];
  233. if(!empty($wsm_return)){
  234. foreach ($wsm_return as $value){
  235. $value['wsm_name']="";
  236. $value['wsm_supplier']="";
  237. $value['wsm_supplierNo']="";
  238. if($value['wsm_code']!=""){
  239. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  240. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  241. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  242. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  243. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  244. }
  245. $wsm[]=$value;
  246. }
  247. }
  248. $info['wsminfo']=$wsm;
  249. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  250. $addrinfo=[];
  251. if(!empty($addr)){
  252. foreach ( $addr as $value){
  253. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  254. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  255. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  256. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  257. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  258. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  259. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  260. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  261. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  262. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  263. $addrinfo[]=$value;
  264. }
  265. }
  266. $info['addrinfo']=$addrinfo;
  267. return app_show(0,"获取成功",$info);
  268. }
  269. /**
  270. * @return \think\response\Json|void
  271. * @throws \think\db\exception\DataNotFoundException
  272. * @throws \think\db\exception\DbException
  273. * @throws \think\db\exception\ModelNotFoundException
  274. */
  275. public function delete(){
  276. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  277. :"";
  278. if($code==""){
  279. return error_show(1004,"参数returnCode不能为空");
  280. }
  281. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  282. if(empty($info)){
  283. return error_show(1004,"未找到退货数据");
  284. }
  285. $info["is_del"]=1;
  286. $info["updatetime"]=date("Y-m-d H:i:s");
  287. $del = Db::name("sale_return")->save($info);
  288. if($del){
  289. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  290. ActionLog::logAdd($this->post['token'],$ste,"xsd",0,$ste);
  291. return app_show(0,"删除成功");
  292. }else{
  293. return error_show(1004,"删除失败");
  294. }
  295. }
  296. /**审核
  297. * @return \think\response\Json|void
  298. * @throws \think\db\exception\DataNotFoundException
  299. * @throws \think\db\exception\DbException
  300. * @throws \think\db\exception\ModelNotFoundException
  301. */
  302. public function exam(){
  303. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  304. :"";
  305. if($code==""){
  306. return error_show(1004,"参数returnCode不能为空");
  307. }
  308. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  309. if(empty($info)){
  310. return error_show(1004,"未找到退货数据");
  311. }
  312. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  313. :"";
  314. if($status===""){
  315. return error_show(1004,"参数status不能为空");
  316. }
  317. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  318. $var = $info['status'];
  319. if($info['order_type']==2){
  320. $wsend_th = isset($this->post['wsend_th']) && $this->post['wsend_th'] !=="" ? intval($this->post['wsend_th']) :"";
  321. if($status==3){
  322. if($wsend_th===''){
  323. return error_show(1004,"参数wsend_th不能为空");
  324. }
  325. $info['wsend_th'] =$wsend_th;
  326. }
  327. $return_wsm = isset($this->post['return_wsm']) && $this->post['return_wsm'] !="" ? trim($this->post['return_wsm']) :"";
  328. if($status==4){
  329. if($return_wsm===''){
  330. return error_show(1004,"参数return_wsm不能为空");
  331. }
  332. $info['return_wsm'] =$return_wsm;
  333. }
  334. }
  335. $info['status'] =$status;
  336. $info['remark'] =$remark;
  337. $info["updatetime"]=date("Y-m-d H:i:s");
  338. $up = Db::name("sale_return")->save($info);
  339. if($up){
  340. $ste = ["order_code"=>$code,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  341. ActionLog::logAdd($this->post['token'],$ste,"xsd",$info['status'],$ste);
  342. return app_show(0,"更新成功");
  343. }else{
  344. return error_show(1004,"更新失败");
  345. }
  346. }
  347. public function zxcreate(){
  348. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  349. if($ordeCode==''){
  350. return error_show(1004,"参数orderCode 不能为空");
  351. }
  352. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  353. if(empty($order)){
  354. return error_show(1005,"未找到订单数据");
  355. }
  356. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  357. if($errorCode==''){
  358. return error_show(1004,"参数errorCode 不能为空");
  359. }
  360. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  361. if(empty($error)){
  362. return error_show(1005,"未找到退货原因数据");
  363. }
  364. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  365. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  366. if($token==''){
  367. return error_show(1005,"参数token不能为空");
  368. }
  369. $user =GetUserInfo($token);
  370. if(empty($user)||$user['code']!=0){
  371. return error_show(1002,"申请人数据不存在");
  372. }
  373. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  374. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  375. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  376. if($num==''){
  377. return error_show(1005,"参数num不能为空");
  378. }
  379. if($order['wsend_num']<$num){
  380. return error_show(1002,"仓库未发货数量不足退货");
  381. }
  382. $returnCode=makeNo("RXS");
  383. Db::startTrans();
  384. try{
  385. $in = [
  386. "returnCode"=>$returnCode,
  387. "orderCode"=>$ordeCode,
  388. "good_code"=>$order['good_code'],
  389. "good_name"=>$order['good_name'],
  390. "apply_id"=>$rm,
  391. "apply_name"=>$ri,
  392. "error_code"=>$errorCode,
  393. "num"=>$num,
  394. "remark"=>$remark,
  395. "order_type"=>2,
  396. "status"=>0,
  397. "is_del"=>0,
  398. "addtime"=>date("Y-m-d H:i:s"),
  399. "updatetime"=>date("Y-m-d H:i:s")
  400. ];
  401. $create = Db::name("sale_return")->insert($in);
  402. if($create){
  403. $addrorder= Db::name("order_addr")->where(['orderCode'=>$ordeCode])->select();
  404. $addrinfo=[];
  405. foreach ($addrorder as $value){
  406. $addrorder= Db::name("order_addr")->where(['orderCode'=>$ordeCode,"id"=>$value['addrid']])
  407. ->find();
  408. if(empty($addrorder)){
  409. Db::rollback();
  410. return error_show(1002,"订单地址数据不存在");
  411. }
  412. $total = Db::name("sale_returnaddr")->where(['orderCode'=>$ordeCode,"id"=>$value['addrid'],"is_del"=>0])
  413. ->sum("return_num");
  414. if($addrorder['receipt_quantity']-$total<$value['return_num']){
  415. Db::rollback();
  416. return error_show(1002,"订单地址退货数量超出发货数量");
  417. }
  418. $temp=[];
  419. $temp['returnCode']=$returnCode;
  420. $temp['orderCode']=$ordeCode;
  421. $temp['addrid']=$value['addrid'];
  422. $temp['return_num']=$value['return_num'];
  423. $temp['send_num']= $addrorder['receipt_quantity'];
  424. $temp['addtime']= date("Y-m-d H:i:s");
  425. $temp['updatetime']= date("Y-m-d H:i:s");
  426. $addrinfo[]=$temp;
  427. }
  428. $return = Db::name("sale_returnaddr")->insertAll($addrinfo);
  429. if($return<=0){
  430. Db::rollback();
  431. return error_show(1002,"订单地址退货数据更新失败");
  432. }
  433. $info=[];
  434. $wsmorder= Db::name("sale_info")->where(['orderCode'=>$ordeCode])->find();
  435. Db::commit();
  436. return app_show(0,"退货单新建成功");
  437. }
  438. Db::rollback();
  439. return error_show(1005,"退货单新建失败");
  440. }catch (\Exception $e){
  441. Db::rollback();
  442. return error_show(1005,$e->getMessage());
  443. }
  444. }
  445. }