Orderback.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. //补充仓库名称
  141. $warehouse = Db::name('warehouse_info')
  142. ->where(['is_del' => 0, 'status' => 1])
  143. ->whereIn('wsm_code', [$info['can_sell_wsm'], $info['defective_wsm']])
  144. ->column('name', 'wsm_code');
  145. $info['can_sell_wsm_name'] = isset($warehouse[$info['can_sell_wsm']]) ? $warehouse[$info['can_sell_wsm']] : '';
  146. $info['defective_wsm_name'] = isset($warehouse[$info['defective_wsm']]) ? $warehouse[$info['defective_wsm']] : '';
  147. return app_show(0,"获取成功",$info);
  148. }
  149. /**
  150. * @return \think\response\Json|void
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. public function check(){
  156. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  157. if($thNo==""){
  158. return error_show(1004,"参数thNo不能为空");
  159. }
  160. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  161. if(empty($info)){
  162. return error_show(1004,"未找到数据");
  163. }
  164. $normal = isset($this->post['normal']) && $this->post['normal']!=="" ? intval($this->post['normal']):"";
  165. if($normal===""){
  166. return error_show(1004,"参数normal不能为空");
  167. }
  168. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  169. if($info['status']== 2){
  170. return error_show(1002,"退货单已验货");
  171. }
  172. $receive=$normal+array_sum(array_column($errorlist,'error_num'));
  173. $remark = isset($this->post['remark']) && $this->post['remark']!=="" ? trim($this->post['remark']):"";
  174. $info['received_num'] = $receive;
  175. $info['normal_num'] = $normal;
  176. $info['remark'] = $remark;
  177. $str = $info['status'];
  178. $info['status'] =empty($errorlist)?3: 2;
  179. $info['updatetime'] = date("Y-m-d H:i:s");
  180. Db::startTrans();
  181. try{
  182. $up=Db::name("order_back")->save($info);
  183. if($up){
  184. $stn = ["order_code"=>$info['thNo'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  185. ActionLog::logAdd($this->post['token'],$stn,$info['order_type'] == 2?"ZXCKTHD":"CKTHD",2,$info);
  186. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>2,"order_type"=>$info['order_type'] == 2?"ZXCKTHD":"CKTHD"];
  187. ProcessOrder::AddProcess($this->post['token'],$process);
  188. if(empty($errorlist)){
  189. Db::commit();
  190. return app_show(0,'更新成功');
  191. }
  192. foreach($errorlist as $value){
  193. $data=[];
  194. isset($value['id'])&&$value['id']!=''?$data['id']=$value['id']:"";
  195. $data['thNo']=$thNo;
  196. $data['error_num']=$value['error_num'];
  197. $data['error_code']=$value['error_code'];
  198. $data['error_remark']=$value['error_remark'];
  199. $data['is_del']=isset($value['is_del'])?$value['is_del']:0;
  200. isset($value['id'])&&$value['id']!=''? "" : $data['addtime']=date("Y-m-d H:i:s");
  201. $data['updatetime']=date("Y-m-d H:i:s");
  202. $in = Db::name("order_backinfo")->save($data);
  203. if(!$in){
  204. Db::rollback();
  205. return error_show(1005,'更新失败');
  206. }
  207. }
  208. Db::commit();
  209. return app_show(0,'更新成功');
  210. }
  211. Db::rollback();
  212. return error_show(1005,'更新失败');
  213. }catch (\Exception $e){
  214. Db::rollback();
  215. return error_show(1004,$e->getMessage());
  216. }
  217. }
  218. public function CheckExam(){
  219. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  220. if($thNo==""){
  221. return error_show(1004,"参数thNo不能为空");
  222. }
  223. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  224. if(empty($info)){
  225. return error_show(1004,"未找到数据");
  226. }
  227. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  228. Db::startTrans();
  229. try{
  230. $temp=$info['status'];
  231. $info['status'] =3;
  232. $info['updatetime'] = date("Y-m-d H:i:s");
  233. $up=Db::name("order_back")->save($info);
  234. if($up){
  235. $stn = ["order_code"=>$thNo,"status"=>$temp,"action_remark"=>'',"action_type"=>"edit"];
  236. ActionLog::logAdd($this->post['token'],$stn,$info['order_type'] == 2?"ZXCKTHD":"CKTHD",$info['status'],$stn);
  237. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>3,"order_type"=>$info['order_type'] == 2?"ZXCKTHD":"CKTHD"];
  238. ProcessOrder::AddProcess($this->post['token'],$process);
  239. if(empty($errorlist)){
  240. $up =Db::name("order_backinfo")->where(["thNo"=>$thNo,"is_del"=>0])->save(["status"=>1,
  241. "updatetime"=>date("Y-m-d H:i:s")]);
  242. if($up){
  243. Db::commit();
  244. return app_show(0,'更新成功');
  245. }else{
  246. Db::rollback();
  247. return error_show(1005,'异常记录数据更新失败');
  248. }
  249. }else{
  250. foreach($errorlist as $value){
  251. $temp=Db::name("order_backinfo")->where(['id'=>$value["id"],"is_del"=>0,"thNo"=>$thNo])->find();
  252. if(empty($temp)){
  253. Db::rollback();
  254. return error_show(1005,'异常记录数据未找到');
  255. }
  256. $temp['status']=$value['status'];
  257. $temp['exam_remark']=$value['remark'];
  258. $temp['updatetime']=date("Y-m-d H:i:s");
  259. $com = Db::name("order_backinfo")->save($temp);
  260. if($com==false){
  261. Db::rollback();
  262. return error_show(1005,'异常记录数据更新失败');
  263. }
  264. }
  265. Db::commit();
  266. return app_show(0,'更新成功');
  267. }
  268. }
  269. Db::rollback();
  270. return error_show(1005,'更新失败');
  271. }catch (\Exception $e){
  272. Db::rollback();
  273. return error_show(1004,$e->getMessage());
  274. }
  275. }
  276. public function Exam(){
  277. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  278. if($thNo==""){
  279. return error_show(1004,"参数thNo不能为空");
  280. }
  281. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  282. if(empty($info)){
  283. return error_show(1004,"未找到数据");
  284. }
  285. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  286. if($status===""){
  287. return error_show(1004,"参数status不能为空");
  288. }
  289. $return_wsm=isset($this->post["return_wsm"])&&$this->post["return_wsm"]!=""?trim($this->post["return_wsm"]):"";
  290. $normal_num=isset($this->post["normal_num"])&&$this->post["normal_num"]!=""?intval($this->post["normal_num"])
  291. :"";
  292. // $defective_wsm=isset($this->post["defective_wsm"])&&$this->post["defective_wsm"]!=""?trim($this->post["defective_wsm"]):"";
  293. // $defective_num=isset($this->post["defective_num"])&&$this->post["defective_num"]!==""?intval($this->post["defective_num"]):"";
  294. // $loss_num=isset($this->post["loss_num"])&&$this->post["loss_num"]!=""?intval($this->post["loss_num"]):"";
  295. if($status==4){
  296. // if($defective_num===""){
  297. // return error_show(1004,"参数defective_num不能为空");
  298. // }
  299. // if($loss_num===""){
  300. // return error_show(1004,"参数loss_num不能为空");
  301. // }
  302. if($normal_num===""){
  303. return error_show(1004,"参数normal_num不能为空");
  304. }
  305. // if($defective_wsm===""){
  306. // return error_show(1004,"参数defective_wsm不能为空");
  307. // }
  308. if($return_wsm===""){
  309. return error_show(1004,"参数return_wsm不能为空");
  310. }
  311. $info['return_wsm']=$return_wsm;
  312. $info['normal_num']=$normal_num;
  313. // $info['loss_num']=$loss_num;//order_back表里没有这三个字段,不知道是干嘛的
  314. // $info['defective_wsm']=$defective_wsm;
  315. // $info['defective_num']=$defective_num;
  316. }
  317. $remark= isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  318. Db::startTrans();
  319. try{
  320. $var = $info['status'];
  321. $info['status'] =$status;
  322. $info['remark'] =$remark;
  323. $info['updatetime'] = date("Y-m-d H:i:s");
  324. $up=Db::name("order_back")->save($info);
  325. if($up){
  326. $stn = ["order_code"=>$thNo,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  327. ActionLog::logAdd($this->post['token'],$stn,$info['order_type'] == 2?"ZXCKTHD":"CKTHD",$info['status'],$stn);
  328. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>$info['order_type'] == 2?"ZXCKTHD":"CKTHD"];
  329. ProcessOrder::AddProcess($this->post['token'],$process);
  330. Db::commit();
  331. return app_show(0,'更新成功');
  332. }
  333. Db::rollback();
  334. return error_show(1005,'更新失败');
  335. }catch (\Exception $e){
  336. Db::rollback();
  337. return error_show(1004,$e->getMessage());
  338. }
  339. }
  340. //新写一个退货接口
  341. public function ExamNew()
  342. {
  343. $param = $this->request->only(['thNo', 'status', 'remark', 'can_sell_wsm' => '', 'can_sell_num' => 0, 'defective_wsm' => '', 'defective_num' => 0, 'loss_num' => 0], 'post', 'trim');
  344. $val = Validate::rule(['thNo|退货编码' => 'require', 'status|状态' => 'require|number|in:4,2,1', 'remark|备注' => 'checkRemark:']);
  345. $val->extend('checkRemark', function ($val, $rule, $data) {
  346. return $data['status'] == 4 ? true : (empty($val) ? '请填写备注' : true);
  347. });
  348. if (!$val->check($param)) return error_show(1004, $val->getError());
  349. Db::startTrans();
  350. try {
  351. $info = Db::name("order_back")
  352. ->field('id,order_type,status')
  353. ->where(['thNo' => $param['thNo'], 'is_del' => 0])
  354. ->find();
  355. if (empty($info)) throw new Exception('未找到数据');
  356. $update_data = ['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')];
  357. //status==4通过,其他值表示驳回
  358. if ($param['status'] != 4) $update_data['remark'] = $param['remark'];
  359. else {
  360. $update_data['can_sell_wsm'] = $param['can_sell_wsm'];
  361. $update_data['can_sell_num'] = $param['can_sell_num'];
  362. $update_data['defective_wsm'] = $param['defective_wsm'];
  363. $update_data['defective_num'] = $param['defective_num'];
  364. $update_data['loss_num'] = $param['loss_num'];
  365. }
  366. $res = Db::name("order_back")
  367. ->where(['id' => $info['id']])
  368. ->update($update_data);
  369. if ($res) {
  370. $stn = ["order_code" => $param['thNo'], "status" => $param['status'], "action_remark" => '', "action_type" => "edit"];
  371. ActionLog::logAdd($this->post['token'], $stn, $info['order_type'] == 2 ? "ZXCKTHD" : "CKTHD", $info['status'], $stn);
  372. $process = ["order_code" => $param['thNo'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => $info['order_type'] == 2 ? "ZXCKTHD" : "CKTHD"];
  373. ProcessOrder::AddProcess($this->post['token'], $process);
  374. Db::commit();
  375. return app_show(0, '操作成功');
  376. } else throw new Exception('操作失败');
  377. } catch (Exception $exception) {
  378. Db::rollback();
  379. return error_show(1005, $exception->getMessage());
  380. }
  381. }
  382. }