Orderback.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. namespace app\Admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\App;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //退货单
  10. class Orderback extends \app\BaseController
  11. {
  12. public $post=[];
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->post =$this->request->post();
  17. }
  18. public function list(){
  19. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  20. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  21. $where =[['is_del',"=",0]];
  22. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  23. if($thNo!=""){
  24. $where[]=['thNo',"like", "%{$thNo}%"];
  25. }
  26. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  27. if($status!==""){
  28. // $where['status'] = $status;
  29. $where[]=['status',"=", $status];
  30. }
  31. $post_code= isset($this->post['post_code']) && $this->post['post_code']!="" ? trim($this->post['post_code']):"";
  32. if($post_code!=""){
  33. $where[]=['post_code',"like", "%{$post_code}%"];
  34. }
  35. $post_compay = isset($this->post['post_compay']) && $this->post['post_compay']!="" ? trim($this->post['post_compay']):"";
  36. if($post_compay!=""){
  37. $where[]=['post_company',"=", $post_compay];
  38. }
  39. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code']!="" ? trim($this->post['customer_code']):"";
  40. if($customer_code!=""){
  41. $where[]=['customer_code',"like", "%{$customer_code}%"];
  42. }
  43. $order_code = isset($this->post['order_code']) && $this->post['order_code']!="" ? trim($this->post['order_code']):"";
  44. if($order_code!=""){
  45. $where[]=['orderCode',"like", "%{$order_code}%"];
  46. }
  47. $out_code = isset($this->post['out_code']) && $this->post['out_code']!="" ? trim($this->post['out_code']):"";
  48. if($out_code!=""){
  49. $where[]=['outCode',"like", "%{$out_code}%"];
  50. }
  51. $return_code = isset($this->post['return_code']) && $this->post['return_code']!="" ? trim($this->post['return_code']):"";
  52. if($return_code!=""){
  53. $where[]=['returnCode',"like", "%{$return_code}%"];
  54. }
  55. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  56. if($start!==""){
  57. $where[]=['addtime',">=", $start];
  58. }
  59. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  60. if($end!==""){
  61. $where[]=['addtime',"<=", $end];
  62. }
  63. $count=Db::name("order_back")->where($where)->count();
  64. $total = ceil($count/$size);
  65. $page = $page >= $total ? $total : $page;
  66. $list = Db::name("order_back")->where($where)->page($page,$size)->order("addtime desc")->select();
  67. $data=[];
  68. foreach ($list as $value){
  69. $value['wsm_name']="";
  70. $value['wsm_supplier']='';
  71. $value['wsm_supplierNo']='';
  72. if($value['return_wsm']!=""){
  73. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  74. ->where(["a.wsm_code"=>$value['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  75. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  76. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  77. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  78. }
  79. $value['customer_name']='';
  80. if($value['customer_code']!=''){
  81. $customer = Db::name("customer_info")->where(['companyNo'=>$value['customer_code']])->find();
  82. $value['customer_name']=isset($customer['companyName'])?$customer['companyName']:'';
  83. }
  84. $inorder= Db::name("order_backinfo")->where(['thNo'=>$value['thNo'],"is_del"=>0])->select();
  85. $value['child']=empty($inorder)? [] : $inorder;
  86. $data[]=$value;
  87. }
  88. return app_show(0,"获取成功",["list"=>$data ,"count"=>$count]);
  89. }
  90. /**
  91. * @return \think\response\Json|void
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function info(){
  97. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  98. if($thNo==""){
  99. return error_show(1004,"参数thNo不能为空");
  100. }
  101. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  102. if(empty($info)){
  103. return error_show(1004,"未找到数据");
  104. }
  105. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  106. $info['origin_price']=$orderinfo['origin_price'];
  107. $info['sale_price']=$orderinfo['sale_price'];
  108. $info['order_type']=$orderinfo['order_type'];
  109. $info['total_price']=$orderinfo['total_price'];
  110. $info['companyNo']=$orderinfo['supplierNo'];
  111. $info['company'] = Db::name('business')->where(['companyNo' => $orderinfo['supplierNo'], 'is_del' => 0])->value('company','');
  112. $info['addr_cn']=GetAddr($info['addr_code']);
  113. if($orderinfo['order_type']==3|| $orderinfo['order_type']==4){
  114. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  115. }else {
  116. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  117. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  118. }
  119. if (empty($goon)) {
  120. return error_show(1003, "未找到商品数据");
  121. }
  122. $retutninfo = Db::name("order_return")->where(['returnCode'=>$info['returnCode']])->find();
  123. $info['apply_id'] = $retutninfo['apply_id'];
  124. $info['apply_name'] = $retutninfo['apply_name'];
  125. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  126. $info['wsm_name']="";
  127. $info['wsm_supplier']='';
  128. $info['wsm_supplierNo']='';
  129. if($info['return_wsm']!=""){
  130. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  131. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  132. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  133. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  134. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  135. }
  136. $inorder= Db::name("order_backinfo")->alias("a")->leftJoin("result_info b","a.error_code=b.result_code")
  137. ->where(['a.thNo'=>$info['thNo'],"a.is_del"=>0])->field("a.*,b.result as error_msg")->select();
  138. $info['child']=empty($inorder)? [] : $inorder;
  139. $info['can']=$int;
  140. return app_show(0,"获取成功",$info);
  141. }
  142. /**
  143. * @return \think\response\Json|void
  144. * @throws \think\db\exception\DataNotFoundException
  145. * @throws \think\db\exception\DbException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. */
  148. public function check(){
  149. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  150. if($thNo==""){
  151. return error_show(1004,"参数thNo不能为空");
  152. }
  153. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  154. if(empty($info)){
  155. return error_show(1004,"未找到数据");
  156. }
  157. $normal = isset($this->post['normal']) && $this->post['normal']!=="" ? intval($this->post['normal']):"";
  158. if($normal===""){
  159. return error_show(1004,"参数normal不能为空");
  160. }
  161. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  162. if($info['status']== 2){
  163. return error_show(1002,"退货单已验货");
  164. }
  165. $receive=$normal+array_sum(array_column($errorlist,'error_num'));
  166. $remark = isset($this->post['remark']) && $this->post['remark']!=="" ? trim($this->post['remark']):"";
  167. $info['received_num'] = $receive;
  168. $info['normal_num'] = $normal;
  169. $info['remark'] = $remark;
  170. $str = $info['status'];
  171. $info['status'] =empty($errorlist)?3: 2;
  172. $info['updatetime'] = date("Y-m-d H:i:s");
  173. Db::startTrans();
  174. try{
  175. $up=Db::name("order_back")->save($info);
  176. if($up){
  177. $stn = ["order_code"=>$info['thNo'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  178. ActionLog::logAdd($this->post['token'],$stn,$info['order_type'] == 2?"ZXCKTHD":"CKTHD",2,$info);
  179. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>2,"order_type"=>$info['order_type'] == 2?"ZXCKTHD":"CKTHD"];
  180. ProcessOrder::AddProcess($this->post['token'],$process);
  181. if(empty($errorlist)){
  182. Db::commit();
  183. return app_show(0,'更新成功');
  184. }
  185. foreach($errorlist as $value){
  186. $data=[];
  187. isset($value['id'])&&$value['id']!=''?$data['id']=$value['id']:"";
  188. $data['thNo']=$thNo;
  189. $data['error_num']=$value['error_num'];
  190. $data['error_code']=$value['error_code'];
  191. $data['error_remark']=$value['error_remark'];
  192. $data['is_del']=isset($value['is_del'])?$value['is_del']:0;
  193. isset($value['id'])&&$value['id']!=''? "" : $data['addtime']=date("Y-m-d H:i:s");
  194. $data['updatetime']=date("Y-m-d H:i:s");
  195. $in = Db::name("order_backinfo")->save($data);
  196. if(!$in){
  197. Db::rollback();
  198. return error_show(1005,'更新失败');
  199. }
  200. }
  201. Db::commit();
  202. return app_show(0,'更新成功');
  203. }
  204. Db::rollback();
  205. return error_show(1005,'更新失败');
  206. }catch (\Exception $e){
  207. Db::rollback();
  208. return error_show(1004,$e->getMessage());
  209. }
  210. }
  211. public function CheckExam(){
  212. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  213. if($thNo==""){
  214. return error_show(1004,"参数thNo不能为空");
  215. }
  216. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  217. if(empty($info)){
  218. return error_show(1004,"未找到数据");
  219. }
  220. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  221. Db::startTrans();
  222. try{
  223. $temp=$info['status'];
  224. $info['status'] =3;
  225. $info['updatetime'] = date("Y-m-d H:i:s");
  226. $up=Db::name("order_back")->save($info);
  227. if($up){
  228. $stn = ["order_code"=>$thNo,"status"=>$temp,"action_remark"=>'',"action_type"=>"edit"];
  229. ActionLog::logAdd($this->post['token'],$stn,$info['order_type'] == 2?"ZXCKTHD":"CKTHD",$info['status'],$stn);
  230. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>3,"order_type"=>$info['order_type'] == 2?"ZXCKTHD":"CKTHD"];
  231. ProcessOrder::AddProcess($this->post['token'],$process);
  232. if(empty($errorlist)){
  233. $up =Db::name("order_backinfo")->where(["thNo"=>$thNo,"is_del"=>0])->save(["status"=>1,
  234. "updatetime"=>date("Y-m-d H:i:s")]);
  235. if($up){
  236. Db::commit();
  237. return app_show(0,'更新成功');
  238. }else{
  239. Db::rollback();
  240. return error_show(1005,'异常记录数据更新失败');
  241. }
  242. }else{
  243. foreach($errorlist as $value){
  244. $temp=Db::name("order_backinfo")->where(['id'=>$value["id"],"is_del"=>0,"thNo"=>$thNo])->find();
  245. if(empty($temp)){
  246. Db::rollback();
  247. return error_show(1005,'异常记录数据未找到');
  248. }
  249. $temp['status']=$value['status'];
  250. $temp['exam_remark']=$value['remark'];
  251. $temp['updatetime']=date("Y-m-d H:i:s");
  252. $com = Db::name("order_backinfo")->save($temp);
  253. if($com==false){
  254. Db::rollback();
  255. return error_show(1005,'异常记录数据更新失败');
  256. }
  257. }
  258. Db::commit();
  259. return app_show(0,'更新成功');
  260. }
  261. }
  262. Db::rollback();
  263. return error_show(1005,'更新失败');
  264. }catch (\Exception $e){
  265. Db::rollback();
  266. return error_show(1004,$e->getMessage());
  267. }
  268. }
  269. public function Exam(){
  270. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  271. if($thNo==""){
  272. return error_show(1004,"参数thNo不能为空");
  273. }
  274. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  275. if(empty($info)){
  276. return error_show(1004,"未找到数据");
  277. }
  278. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  279. if($status===""){
  280. return error_show(1004,"参数status不能为空");
  281. }
  282. $return_wsm=isset($this->post["return_wsm"])&&$this->post["return_wsm"]!=""?trim($this->post["return_wsm"]):"";
  283. $normal_num=isset($this->post["normal_num"])&&$this->post["normal_num"]!=""?intval($this->post["normal_num"])
  284. :"";
  285. // $defective_wsm=isset($this->post["defective_wsm"])&&$this->post["defective_wsm"]!=""?trim($this->post["defective_wsm"]):"";
  286. // $defective_num=isset($this->post["defective_num"])&&$this->post["defective_num"]!==""?intval($this->post["defective_num"]):"";
  287. // $loss_num=isset($this->post["loss_num"])&&$this->post["loss_num"]!=""?intval($this->post["loss_num"]):"";
  288. if($status==4){
  289. // if($defective_num===""){
  290. // return error_show(1004,"参数defective_num不能为空");
  291. // }
  292. // if($loss_num===""){
  293. // return error_show(1004,"参数loss_num不能为空");
  294. // }
  295. if($normal_num===""){
  296. return error_show(1004,"参数normal_num不能为空");
  297. }
  298. // if($defective_wsm===""){
  299. // return error_show(1004,"参数defective_wsm不能为空");
  300. // }
  301. if($return_wsm===""){
  302. return error_show(1004,"参数return_wsm不能为空");
  303. }
  304. $info['return_wsm']=$return_wsm;
  305. $info['normal_num']=$normal_num;
  306. // $info['loss_num']=$loss_num;//order_back表里没有这三个字段,不知道是干嘛的
  307. // $info['defective_wsm']=$defective_wsm;
  308. // $info['defective_num']=$defective_num;
  309. }
  310. $remark= isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  311. Db::startTrans();
  312. try{
  313. $var = $info['status'];
  314. $info['status'] =$status;
  315. $info['remark'] =$remark;
  316. $info['updatetime'] = date("Y-m-d H:i:s");
  317. $up=Db::name("order_back")->save($info);
  318. if($up){
  319. $stn = ["order_code"=>$thNo,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  320. ActionLog::logAdd($this->post['token'],$stn,$info['order_type'] == 2?"ZXCKTHD":"CKTHD",$info['status'],$stn);
  321. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>$info['order_type'] == 2?"ZXCKTHD":"CKTHD"];
  322. ProcessOrder::AddProcess($this->post['token'],$process);
  323. Db::commit();
  324. return app_show(0,'更新成功');
  325. }
  326. Db::rollback();
  327. return error_show(1005,'更新失败');
  328. }catch (\Exception $e){
  329. Db::rollback();
  330. return error_show(1004,$e->getMessage());
  331. }
  332. }
  333. //新写一个退货接口
  334. public function ExamNew()
  335. {
  336. $param = $this->request->only(['thNo', 'status', 'remark', 'can_sell_wsm' => '', 'can_sell_num' => 0, 'defective_wsm' => '', 'defective_num' => 0, 'loss_num' => 0], 'post', 'trim');
  337. $val = Validate::rule(['thNo|退货编码' => 'require', 'status|状态' => 'require|number|in:4,2,1', 'remark|备注' => 'checkRemark:']);
  338. $val->extend('checkRemark', function ($val, $rule, $data) {
  339. return $data['status'] == 4 ? true : (empty($val) ? '请填写备注' : true);
  340. });
  341. if (!$val->check($param)) return error_show(1004, $val->getError());
  342. Db::startTrans();
  343. try {
  344. $info = Db::name("order_back")
  345. ->field('id,order_type,status')
  346. ->where(['thNo' => $param['thNo'], 'is_del' => 0])
  347. ->find();
  348. if (empty($info)) throw new Exception('未找到数据');
  349. $update_data = ['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')];
  350. //status==4通过,其他值表示驳回
  351. if ($param['status'] != 4) $update_data['remark'] = $param['remark'];
  352. else {
  353. $update_data['can_sell_wsm'] = $param['can_sell_wsm'];
  354. $update_data['can_sell_num'] = $param['can_sell_num'];
  355. $update_data['defective_wsm'] = $param['defective_wsm'];
  356. $update_data['defective_num'] = $param['defective_num'];
  357. $update_data['loss_num'] = $param['loss_num'];
  358. }
  359. $res = Db::name("order_back")
  360. ->where(['id' => $info['id']])
  361. ->update($update_data);
  362. if ($res) {
  363. $stn = ["order_code" => $param['thNo'], "status" => $param['status'], "action_remark" => '', "action_type" => "edit"];
  364. ActionLog::logAdd($this->post['token'], $stn, $info['order_type'] == 2 ? "ZXCKTHD" : "CKTHD", $info['status'], $stn);
  365. $process = ["order_code" => $param['thNo'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => $info['order_type'] == 2 ? "ZXCKTHD" : "CKTHD"];
  366. ProcessOrder::AddProcess($this->post['token'], $process);
  367. Db::commit();
  368. return app_show(0, '操作成功');
  369. } else throw new Exception('操作失败');
  370. } catch (Exception $exception) {
  371. Db::rollback();
  372. return error_show(1005, $exception->getMessage());
  373. }
  374. }
  375. }