Orderback.php 20 KB

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