Reorder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\App;
  5. class Reorder extends \app\BaseController
  6. {
  7. public $post=[];
  8. public function __construct(App $app)
  9. {
  10. parent::__construct($app);
  11. $this->post =$this->request->post();
  12. }
  13. public function create(){
  14. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  15. if($ordeCode==''){
  16. return error_show(1004,"参数orderCode 不能为空");
  17. }
  18. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  19. if(empty($order)){
  20. return error_show(1005,"未找到订单数据");
  21. }
  22. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  23. if($ordeCode==''){
  24. return error_show(1004,"参数orderCode 不能为空");
  25. }
  26. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  27. if(empty($error)){
  28. return error_show(1005,"未找到退货原因数据");
  29. }
  30. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  31. $data = isset($this->post['list'])&&$this->post['list']!='' ? $this->post['list'] :'';
  32. if($data=='' || empty($data)){
  33. return error_show(1005,"参数list不能为空");
  34. }
  35. $addr = isset($this->post['addr'])&&$this->post['addr']!='' ? $this->post['addr'] :'';
  36. if($addr=='' || empty($addr)){
  37. return error_show(1005,"参数addr不能为空");
  38. }
  39. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  40. if($token==''){
  41. return error_show(1005,"参数token不能为空");
  42. }
  43. $user =GetUserInfo($token);
  44. if(empty($user)||$user['code']!=0){
  45. return error_show(1002,"申请人数据不存在");
  46. }
  47. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  48. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  49. $total = array_sum(array_column($data,"return_num"));
  50. $addrnum = array_sum(array_column($addr,"return_num"));
  51. if($total!=$addrnum){
  52. return error_show(1002,"仓库退货数据与地址退货数据不一致");
  53. }
  54. // var_dump($order['good_code']);
  55. $returnCode=makeNo("RXS");
  56. Db::startTrans();
  57. try{
  58. $in = [
  59. "returnCode"=>$returnCode,
  60. "orderCode"=>$ordeCode,
  61. "good_code"=>$order['good_code'],
  62. "good_name"=>$order['good_name'],
  63. "apply_id"=>$rm,
  64. "apply_name"=>$ri,
  65. "error_code"=>$errorCode,
  66. "num"=>$total,
  67. "remark"=>$remark,
  68. "order_type"=>1,
  69. "status"=>0,
  70. "is_del"=>0,
  71. "addtime"=>date("Y-m-d H:i:s"),
  72. "updatetime"=>date("Y-m-d H:i:s")
  73. ];
  74. $create = Db::name("sale_return")->insert($in);
  75. if($create){
  76. $addrinfo=[];
  77. foreach ($addr as $value){
  78. $addrorder= Db::name("order_addr")->where(['orderCode'=>$ordeCode,"id"=>$value['addrid']])
  79. ->find();
  80. if(empty($addrorder)){
  81. Db::rollback();
  82. return error_show(1002,"订单地址数据不存在");
  83. }
  84. $total = Db::name("sale_returnaddr")->where(['orderCode'=>$ordeCode,"id"=>$value['addrid'],"is_del"=>0])
  85. ->sum("return_num");
  86. if($addrorder['receipt_quantity']-$total<$value['return_num']){
  87. Db::rollback();
  88. return error_show(1002,"订单地址退货数量超出发货数量");
  89. }
  90. $temp=[];
  91. $temp['returnCode']=$returnCode;
  92. $temp['orderCode']=$ordeCode;
  93. $temp['addrid']=$value['addrid'];
  94. $temp['return_num']=$value['return_num'];
  95. $temp['send_num']= $addrorder['receipt_quantity'];
  96. $temp['addtime']= date("Y-m-d H:i:s");
  97. $temp['updatetime']= date("Y-m-d H:i:s");
  98. $addrinfo[]=$temp;
  99. }
  100. $return = Db::name("sale_returnaddr")->insertAll($addrinfo);
  101. if($return<=0){
  102. Db::rollback();
  103. return error_show(1002,"订单地址退货数据更新失败");
  104. }
  105. $info=[];
  106. foreach ($data as $value){
  107. $wsmorder= Db::name("sale_info")->where(['orderCode'=>$ordeCode,"wsm_code"=>$value['wsm_code']])
  108. ->find();
  109. if(empty($wsmorder)){
  110. Db::rollback();
  111. return error_show(1002,"订单仓库数据不存在");
  112. }
  113. $total_wsm = Db::name("sale_returninfo")->where(['orderCode'=>$ordeCode,"wsm_code"=>$value['wsm_code'],"is_del"=>0])
  114. ->sum("return_num");
  115. if($wsmorder['num']-$total_wsm<$value['return_num']){
  116. Db::rollback();
  117. return error_show(1002,"仓库退货数量超出发货数量");
  118. }
  119. $temp=[];
  120. $temp['returnCode']=$returnCode;
  121. $temp['orderCode']=$ordeCode;
  122. $temp['wsm_code']=$value['wsm_code'];
  123. $temp['return_num']=$value['return_num'];
  124. $temp['total_num']= $wsmorder['num'];
  125. $temp['addtime']= date("Y-m-d H:i:s");
  126. $temp['updatetime']= date("Y-m-d H:i:s");
  127. $info[]=$temp;
  128. }
  129. $return = Db::name("sale_returninfo")->insertAll($info);
  130. if($return){
  131. Db::commit();
  132. return app_show(0,"退货单新建成功");
  133. }
  134. }
  135. Db::rollback();
  136. return error_show(1005,"退货单新建失败");
  137. }catch (\Exception $e){
  138. Db::rollback();
  139. return error_show(1005,$e->getMessage());
  140. }
  141. }
  142. public function list(){
  143. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  144. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  145. $where =[['is_del',"=",0]];
  146. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  147. if($bkcode!=""){
  148. $where[]=['returnCode',"like", "%{$bkcode}%"];
  149. }
  150. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  151. if($status!==""){
  152. $where[]=['status',"=", $status];
  153. }
  154. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  155. :"";
  156. if($orderCode!=""){
  157. $where[]=['orderCode',"like", "%{$orderCode}%"];
  158. }
  159. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  160. :"";
  161. if($apply_name!=""){
  162. $where[]=['apply_name',"like", "%{$apply_name}%"];
  163. }
  164. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  165. if($start!==""){
  166. $where[]=['addtime',">=", $start];
  167. }
  168. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  169. if($end!==""){
  170. $where[]=['addtime',"<=", $end];
  171. }
  172. $count=Db::name("sale_return")->where($where)->count();
  173. $total = ceil($count/$size);
  174. $page = $total>=$page ? $page :$total;
  175. $list = Db::name("sale_return")->where($where)->order("addtime desc")->page($page,$size)->select();
  176. // echo Db::name("sale_return")->getLastSql();
  177. $data=[];
  178. foreach ($list as $value){
  179. $value['error_msg']='';
  180. if($value['error_code']!=''){
  181. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  182. $value['error_msg']= isset($error['result'])?$error['result']:"";
  183. }
  184. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  185. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  186. $value['return_total'] =$value['sale_price']*$value['num'] ;
  187. $value['total_num'] =$order['good_num'] ;
  188. $data[]=$value ;
  189. }
  190. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  191. }
  192. public function info(){
  193. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  194. :"";
  195. if($code==""){
  196. return error_show(1004,"参数returnCode不能为空");
  197. }
  198. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  199. if(empty($info)){
  200. return error_show(1004,"未找到退货数据");
  201. }
  202. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  203. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  204. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  205. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  206. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  207. $info['return_total'] = $info['sale_price']*$info['num'] ;
  208. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  209. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  210. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  211. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  212. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  213. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  214. $info['customer_name']='';
  215. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  216. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  217. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  218. }
  219. $info['error_msg']='';
  220. if($info['error_code']!=''){
  221. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  222. $info['error_msg']= isset($error['result'])?$error['result']:"";
  223. }
  224. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  225. $wsm=[];
  226. if(!empty($wsm_return)){
  227. foreach ($wsm_return as $value){
  228. $value['wsm_name']="";
  229. $value['wsm_supplier']="";
  230. $value['wsm_supplierNo']="";
  231. if($value['wsm_code']!=""){
  232. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  233. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  234. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  235. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  236. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  237. }
  238. $wsm[]=$value;
  239. }
  240. }
  241. $info['wsminfo']=$wsm;
  242. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  243. $addrinfo=[];
  244. if(!empty($addr)){
  245. foreach ( $addr as $value){
  246. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  247. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  248. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  249. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  250. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  251. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  252. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  253. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  254. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  255. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  256. $addrinfo[]=$value;
  257. }
  258. }
  259. $info['addrinfo']=$addrinfo;
  260. return app_show(0,"获取成功",$info);
  261. }
  262. /**
  263. * @return \think\response\Json|void
  264. * @throws \think\db\exception\DataNotFoundException
  265. * @throws \think\db\exception\DbException
  266. * @throws \think\db\exception\ModelNotFoundException
  267. */
  268. public function delete(){
  269. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  270. :"";
  271. if($code==""){
  272. return error_show(1004,"参数returnCode不能为空");
  273. }
  274. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  275. if(empty($info)){
  276. return error_show(1004,"未找到退货数据");
  277. }
  278. $info["is_del"]=1;
  279. $info["updatetime"]=date("Y-m-d H:i:s");
  280. $del = Db::name("sale_return")->save($info);
  281. if($del){
  282. return app_show(0,"删除成功");
  283. }else{
  284. return error_show(1004,"删除失败");
  285. }
  286. }
  287. /**审核
  288. * @return \think\response\Json|void
  289. * @throws \think\db\exception\DataNotFoundException
  290. * @throws \think\db\exception\DbException
  291. * @throws \think\db\exception\ModelNotFoundException
  292. */
  293. public function exam(){
  294. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  295. :"";
  296. if($code==""){
  297. return error_show(1004,"参数returnCode不能为空");
  298. }
  299. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  300. if(empty($info)){
  301. return error_show(1004,"未找到退货数据");
  302. }
  303. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  304. :"";
  305. if($status===""){
  306. return error_show(1004,"参数status不能为空");
  307. }
  308. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  309. $info['status'] =$status;
  310. $info['remark'] =$remark;
  311. $info["updatetime"]=date("Y-m-d H:i:s");
  312. $up = Db::name("sale_return")->save($info);
  313. if($up){
  314. return app_show(0,"更新成功");
  315. }else{
  316. return error_show(1004,"更新失败");
  317. }
  318. }
  319. }