Reorder.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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(1005,"参数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. // var_dump($order['good_code']);
  45. $returnCode=makeNo("RXS");
  46. Db::startTrans();
  47. try{
  48. $in = [
  49. "returnCode"=>$returnCode,
  50. "orderCode"=>$ordeCode,
  51. "good_code"=>$order['good_code'],
  52. "good_name"=>$order['good_name'],
  53. "apply_id"=>$rm,
  54. "apply_name"=>$ri,
  55. "error_code"=>$errorCode,
  56. "num"=>$thnum,
  57. "remark"=>$remark,
  58. "order_type"=>$order['order_type'],
  59. "status"=>0,
  60. "is_del"=>0,
  61. "addtime"=>date("Y-m-d H:i:s"),
  62. "updatetime"=>date("Y-m-d H:i:s")
  63. ];
  64. $create = Db::name("sale_return")->insert($in,true);
  65. if($create>0){
  66. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  67. ActionLog::logAdd($this->post['token'],$stn,"QRTHD",0,$stn);
  68. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'QRTHD'];
  69. ProcessOrder::AddProcess($this->post['token'],$process);
  70. $order['wsend_num']-=$thnum;
  71. $order['good_num']-=$thnum;
  72. $order['send_status'] = $order['send_num'] == 0 ? 1 : $order['wsend_num'] == 0 ? 3 : 2;
  73. $order['status'] = $order['send_num'] == 0 ? 3 : $order['wsend_num'] == 0 ? 5 : 4;
  74. $order['updatetime']=date("Y-m-d H:i:s");
  75. $return = Db::name("sale")->save($order);
  76. if($return){
  77. Db::commit();
  78. return app_show(0,"退货单新建成功");
  79. }
  80. }
  81. Db::rollback();
  82. return error_show(1005,"退货单新建失败");
  83. }catch (\Exception $e){
  84. Db::rollback();
  85. return error_show(1005,$e->getMessage());
  86. }
  87. }
  88. public function list(){
  89. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  90. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  91. $where =[['is_del',"=",0]];
  92. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  93. if($bkcode!=""){
  94. $where[]=['returnCode',"like", "%{$bkcode}%"];
  95. }
  96. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  97. if($status!==""){
  98. $where[]=['status',"=", $status];
  99. }
  100. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  101. :"";
  102. if($orderCode!=""){
  103. $where[]=['orderCode',"like", "%{$orderCode}%"];
  104. }
  105. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  106. :"";
  107. if($apply_name!=""){
  108. $where[]=['apply_name',"like", "%{$apply_name}%"];
  109. }
  110. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  111. if($start!==""){
  112. $where[]=['addtime',">=", $start];
  113. }
  114. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  115. if($end!==""){
  116. $where[]=['addtime',"<=", $end];
  117. }
  118. $count=Db::name("sale_return")->where($where)->count();
  119. $total = ceil($count/$size);
  120. $page = $total>=$page ? $page :$total;
  121. $list = Db::name("sale_return")->where($where)->order("addtime desc")->page($page,$size)->select();
  122. // echo Db::name("sale_return")->getLastSql();
  123. $data=[];
  124. foreach ($list as $value){
  125. $value['error_msg']='';
  126. if($value['error_code']!=''){
  127. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  128. $value['error_msg']= isset($error['result'])?$error['result']:"";
  129. }
  130. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  131. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  132. $value['return_total'] =$value['sale_price']*$value['num'] ;
  133. $value['total_num'] =$order['good_num'] ;
  134. $data[]=$value ;
  135. }
  136. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  137. }
  138. public function info(){
  139. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  140. :"";
  141. if($code==""){
  142. return error_show(1004,"参数returnCode不能为空");
  143. }
  144. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  145. if(empty($info)){
  146. return error_show(1004,"未找到退货数据");
  147. }
  148. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  149. $goon = Db::name('good')->alias('b')->join("good_type a", "a.good_code=b.good_code", "left")
  150. ->where(['a.type_code' => $info['good_code']])->find();
  151. if (empty($goon)) {
  152. return error_show(1003, "未找到商品数据");
  153. }
  154. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  155. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  156. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  157. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  158. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  159. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  160. $info['return_total'] = $info['sale_price']*$info['num'] ;
  161. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  162. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  163. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  164. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  165. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  166. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  167. $info['customer_name']='';
  168. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  169. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  170. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  171. }
  172. $info['cgd_wsend']="";
  173. $info['cgd_send']="";
  174. $info['cgd_total']="";
  175. if($orderinfo['order_type']==2){
  176. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  177. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  178. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  179. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  180. }
  181. $info['error_msg']='';
  182. if($info['error_code']!=''){
  183. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  184. $info['error_msg']= isset($error['result'])?$error['result']:"";
  185. }
  186. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  187. $wsm=[];
  188. if(!empty($wsm_return)){
  189. foreach ($wsm_return as $value){
  190. $value['wsm_name']="";
  191. $value['wsm_supplier']="";
  192. $value['wsm_supplierNo']="";
  193. if($value['wsm_code']!=""){
  194. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  195. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  196. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  197. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  198. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  199. }
  200. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  201. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  202. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  203. $value['wsm_send'] = $send ?? 0;
  204. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  205. $wsm[]=$value;
  206. }
  207. }
  208. $info['wsminfo']=$wsm;
  209. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  210. $addrinfo=[];
  211. if(!empty($addr)){
  212. foreach ( $addr as $value){
  213. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  214. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  215. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  216. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  217. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  218. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  219. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  220. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  221. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  222. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode']])->sum("send_num");
  223. $value['addr_send'] = $send ?? 0;
  224. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  225. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  226. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  227. $addrinfo[]=$value;
  228. }
  229. }
  230. $info['addrinfo']=$addrinfo;
  231. $info['can']=$int;
  232. return app_show(0,"获取成功",$info);
  233. }
  234. /**
  235. * @return \think\response\Json|void
  236. * @throws \think\db\exception\DataNotFoundException
  237. * @throws \think\db\exception\DbException
  238. * @throws \think\db\exception\ModelNotFoundException
  239. */
  240. public function delete(){
  241. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  242. :"";
  243. if($code==""){
  244. return error_show(1004,"参数returnCode不能为空");
  245. }
  246. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  247. if(empty($info)){
  248. return error_show(1004,"未找到退货数据");
  249. }
  250. $info["is_del"]=1;
  251. $info["updatetime"]=date("Y-m-d H:i:s");
  252. $del = Db::name("sale_return")->save($info);
  253. if($del){
  254. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  255. ActionLog::logAdd($this->post['token'],$ste,$info['order_type'] == 2?"ZXTHD":"QRTHD",0,$ste);
  256. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>$info['order_type'] == 2?"ZXTHD":"QRTHD"];
  257. ProcessOrder::workdel($process);
  258. return app_show(0,"删除成功");
  259. }else{
  260. return error_show(1004,"删除失败");
  261. }
  262. }
  263. /**审核
  264. * @return \think\response\Json|void
  265. * @throws \think\db\exception\DataNotFoundException
  266. * @throws \think\db\exception\DbException
  267. * @throws \think\db\exception\ModelNotFoundException
  268. */
  269. public function exam(){
  270. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  271. :"";
  272. if($code==""){
  273. return error_show(1004,"参数returnCode不能为空");
  274. }
  275. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  276. if(empty($info)){
  277. return error_show(1004,"未找到退货数据");
  278. }
  279. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  280. :"";
  281. if($status===""){
  282. return error_show(1004,"参数status不能为空");
  283. }
  284. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  285. $var = $info['status'];
  286. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  287. $apply_id =GetUserInfo($token);
  288. if(empty($apply_id)||$apply_id['code']!=0){
  289. return error_show(1002,"申请人数据不存在");
  290. }
  291. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  292. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  293. Db::startTrans();
  294. try{
  295. $temp= $info['status'];
  296. $info['status'] =$status;
  297. $remark!=""? $info['remark'] =$remark:"";
  298. $info["updatetime"]=date("Y-m-d H:i:s");
  299. $up = Db::name("sale_return")->save($info);
  300. if($up){
  301. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>$info['order_type'] == 2?"ZXTHD":"QRTHD"];
  302. ProcessOrder::AddProcess($this->post['token'],$process);
  303. $ste = ["order_code"=>$code,"status"=>$temp,"action_remark"=>'',"action_type"=>"status"];
  304. ActionLog::logAdd($this->post['token'],$ste,$info['order_type'] == 2?"ZXTHD":"QRTHD",$status,$info);
  305. Db::commit();
  306. return app_show(0,"更新成功");
  307. }else{
  308. Db::rollback();
  309. return error_show(1004,"更新失败");
  310. }
  311. }catch (\Exception $e){
  312. Db::rollback();
  313. return error_show(1004,"更新失败");
  314. }
  315. }
  316. public function zxcreate(){
  317. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  318. if($ordeCode==''){
  319. return error_show(1004,"参数orderCode 不能为空");
  320. }
  321. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  322. if(empty($order)){
  323. return error_show(1005,"未找到订单数据");
  324. }
  325. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  326. if($errorCode==''){
  327. return error_show(1004,"参数errorCode 不能为空");
  328. }
  329. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  330. if(empty($error)){
  331. return error_show(1005,"未找到退货原因数据");
  332. }
  333. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  334. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  335. if($token==''){
  336. return error_show(1005,"参数token不能为空");
  337. }
  338. $user =GetUserInfo($token);
  339. if(empty($user)||$user['code']!=0){
  340. return error_show(1002,"申请人数据不存在");
  341. }
  342. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  343. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  344. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  345. if($num==''){
  346. return error_show(1005,"参数num不能为空");
  347. }
  348. if($order['wsend_num']<$num){
  349. return error_show(1002,"仓库未发货数量不足退货");
  350. }
  351. $returnCode=makeNo("RXS");
  352. Db::startTrans();
  353. try{
  354. $in = [
  355. "returnCode"=>$returnCode,
  356. "orderCode"=>$ordeCode,
  357. "good_code"=>$order['good_code'],
  358. "good_name"=>$order['good_name'],
  359. "apply_id"=>$rm,
  360. "apply_name"=>$ri,
  361. "error_code"=>$errorCode,
  362. "num"=>$num,
  363. "remark"=>$remark,
  364. "order_type"=>2,
  365. "status"=>0,
  366. "is_del"=>0,
  367. "addtime"=>date("Y-m-d H:i:s"),
  368. "updatetime"=>date("Y-m-d H:i:s")
  369. ];
  370. $create = Db::name("sale_return")->insert($in,true);
  371. if($create>0) {
  372. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'ZXTHD'];
  373. ProcessOrder::AddProcess($this->post['token'],$process);
  374. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  375. "action_type"=>"create"];
  376. ActionLog::logAdd($this->post['token'],$ste,"ZXTHD",3,$in);
  377. if ($order['send_type'] == 1) {
  378. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"
  379. =>2])->select();
  380. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  381. if(!empty($wsend)){
  382. foreach ($wsend as $value){
  383. $tt = $value['status'];
  384. $value['status']=0;
  385. $value['updatetime']=date("Y-m-d H:i:s");
  386. $up =Db::name("order_out")->save($value);
  387. if($up){
  388. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD'];
  389. ProcessOrder::AddProcess($this->post['token'],$process);
  390. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  391. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  392. }else{
  393. Db::rollback();
  394. return error_show(1005,"退货单新建失败");
  395. }
  396. }
  397. }
  398. }
  399. Db::commit();
  400. return error_show(0,"退货单新建成功");
  401. }
  402. Db::rollback();
  403. return error_show(1005,"退货单新建失败");
  404. }catch (\Exception $e){
  405. Db::rollback();
  406. return error_show(1005,$e->getMessage());
  407. }
  408. }
  409. }