Reorder.php 22 KB

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