Orderback.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\ProcessOrder;
  6. use think\App;
  7. use think\Exception;
  8. use think\facade\Db;
  9. use think\facade\Validate;
  10. //退货单
  11. class Orderback extends Base
  12. {
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. }
  17. public function list(){
  18. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  19. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  20. $where =[['is_del',"=",0]];
  21. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  22. if($thNo!=""){
  23. $where[]=['thNo',"like", "%{$thNo}%"];
  24. }
  25. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  26. if($status!==""){
  27. // $where['status'] = $status;
  28. $where[]=['status',"=", $status];
  29. }
  30. $post_code= isset($this->post['post_code']) && $this->post['post_code']!="" ? trim($this->post['post_code']):"";
  31. if($post_code!=""){
  32. $where[]=['post_code',"like", "%{$post_code}%"];
  33. }
  34. $post_compay = isset($this->post['post_compay']) && $this->post['post_compay']!="" ? trim($this->post['post_compay']):"";
  35. if($post_compay!=""){
  36. $where[]=['post_company',"=", $post_compay];
  37. }
  38. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code']!="" ? trim($this->post['customer_code']):"";
  39. if($customer_code!=""){
  40. $where[]=['customer_code',"like", "%{$customer_code}%"];
  41. }
  42. $order_code = isset($this->post['order_code']) && $this->post['order_code']!="" ? trim($this->post['order_code']):"";
  43. if($order_code!=""){
  44. $where[]=['orderCode',"like", "%{$order_code}%"];
  45. }
  46. $out_code = isset($this->post['out_code']) && $this->post['out_code']!="" ? trim($this->post['out_code']):"";
  47. if($out_code!=""){
  48. $where[]=['outCode',"like", "%{$out_code}%"];
  49. }
  50. $return_code = isset($this->post['return_code']) && $this->post['return_code']!="" ? trim($this->post['return_code']):"";
  51. if($return_code!=""){
  52. $where[]=['returnCode',"like", "%{$return_code}%"];
  53. }
  54. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  55. if($start!==""){
  56. $where[]=['addtime',">=", $start];
  57. }
  58. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  59. if($end!==""){
  60. $where[]=['addtime',"<=", $end];
  61. }
  62. $role=$this->checkRole();
  63. if(!empty($role['write'])){
  64. $where[]=["apply_id","in",$role['write']];
  65. }
  66. $count=Db::name("order_back")->where($where)->count();
  67. $total = ceil($count/$size);
  68. $page = $page >= $total ? $total : $page;
  69. $list = Db::name("order_back")->where($where)->page($page,$size)->order("addtime desc")->select();
  70. $data=[];
  71. foreach ($list as $value){
  72. $value['wsm_name']="";
  73. $value['wsm_supplier']='';
  74. $value['wsm_supplierNo']='';
  75. if($value['return_wsm']!=""){
  76. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  77. ->where(["a.wsm_code"=>$value['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  78. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  79. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  80. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  81. }
  82. $value['customer_name']='';
  83. if($value['customer_code']!=''){
  84. $customer = Db::name("customer_info")->where(['companyNo'=>$value['customer_code']])->find();
  85. $value['customer_name']=isset($customer['companyName'])?$customer['companyName']:'';
  86. }
  87. $inorder= Db::name("order_backinfo")->where(['thNo'=>$value['thNo'],"is_del"=>0])->select();
  88. $value['child']=empty($inorder)? [] : $inorder;
  89. $data[]=$value;
  90. }
  91. return app_show(0,"获取成功",["list"=>$data ,"count"=>$count]);
  92. }
  93. /**
  94. * @return \think\response\Json|void
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function info(){
  100. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  101. if($thNo==""){
  102. return error_show(1004,"参数thNo不能为空");
  103. }
  104. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  105. if(empty($info)){
  106. return error_show(1004,"未找到数据");
  107. }
  108. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  109. $info['origin_price']=$orderinfo['origin_price'];
  110. $info['sale_price']=$orderinfo['sale_price'];
  111. $info['order_type']=$orderinfo['order_type'];
  112. $info['total_price']=$orderinfo['total_price'];
  113. $info['companyNo']=$orderinfo['supplierNo'];
  114. $info['company'] = Db::name('business')->where(['companyNo' => $orderinfo['supplierNo'], 'is_del' => 0])->value('company','');
  115. $info['addr_cn']=GetAddr($info['addr_code']);
  116. if($orderinfo['order_type']==3|| $orderinfo['order_type']==4){
  117. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  118. $is_stock=0;
  119. }else {
  120. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  121. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  122. $is_stock=$goon['is_stock'];
  123. }
  124. if (empty($goon)) {
  125. return error_show(1003, "未找到商品数据");
  126. }
  127. $info['out_wsm_name']="";
  128. $info['out_wsm_supplier']='';
  129. $info['out_wsm_supplierNo']='';
  130. if($is_stock==1){
  131. $out =Db::name("order_out")->where(['outCode'=>$info['outCode']])->find();
  132. if($out==false){
  133. return error_show(1003, "未找到出库单数据");
  134. }
  135. if($out['wsm_code']!=''){
  136. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  137. ->where(["a.wsm_code"=>$out['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  138. $info['out_wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  139. $info['out_wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  140. $info['out_wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  141. }
  142. }
  143. $retutninfo = Db::name("order_return")->where(['returnCode'=>$info['returnCode']])->find();
  144. $info['apply_id'] = $retutninfo['apply_id'];
  145. $info['apply_name'] = $retutninfo['apply_name'];
  146. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  147. $info['wsm_name']="";
  148. $info['wsm_supplier']='';
  149. $info['wsm_supplierNo']='';
  150. if($info['return_wsm']!=""){
  151. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  152. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  153. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  154. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  155. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  156. }
  157. $inorder= Db::name("order_backinfo")->alias("a")->leftJoin("result_info b","a.error_code=b.result_code")
  158. ->where(['a.thNo'=>$info['thNo'],"a.is_del"=>0])->field("a.*,b.result as error_msg")->select();
  159. $info['child']=empty($inorder)? [] : $inorder;
  160. $info['can']=$int;
  161. //补充仓库名称
  162. $warehouse = Db::name('warehouse_info')
  163. ->alias('wi')
  164. ->where(['wi.is_del' => 0, 'wi.status' => 1])
  165. ->whereIn('wi.wsm_code', [$info['can_sell_wsm'], $info['defective_wsm']])
  166. ->leftJoin('supplier s', 's.code=wi.supplierNo AND s.is_del=0')
  167. ->column('wi.name,wi.supplierNo,s.name supplier', 'wi.wsm_code');
  168. $info['can_sell_wsm_name'] = isset($warehouse[$info['can_sell_wsm']]['name']) ? $warehouse[$info['can_sell_wsm']]['name'] : '';
  169. $info['can_sell_wsm_supplierNo'] = isset($warehouse[$info['can_sell_wsm']]['supplierNo']) ? $warehouse[$info['can_sell_wsm']]['supplierNo'] : '';
  170. $info['can_sell_wsm_supplier'] = isset($warehouse[$info['can_sell_wsm']]['supplier']) ? $warehouse[$info['can_sell_wsm']]['supplier'] : '';
  171. $info['defective_wsm_name'] = isset($warehouse[$info['defective_wsm']]['name']) ? $warehouse[$info['defective_wsm']]['name'] : '';
  172. $info['defective_wsm_supplierNo'] = isset($warehouse[$info['defective_wsm']]['supplierNo']) ? $warehouse[$info['defective_wsm']]['supplierNo'] : '';
  173. $info['defective_wsm_supplier'] = isset($warehouse[$info['defective_wsm']]['supplier']) ? $warehouse[$info['defective_wsm']]['supplier'] : '';
  174. return app_show(0, "获取成功", $info);
  175. }
  176. /**
  177. * @return \think\response\Json|void
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. public function check(){
  183. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  184. if($thNo==""){
  185. return error_show(1004,"参数thNo不能为空");
  186. }
  187. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  188. if(empty($info)){
  189. return error_show(1004,"未找到数据");
  190. }
  191. $normal = isset($this->post['normal']) && $this->post['normal']!=="" ? intval($this->post['normal']):"";
  192. if($normal===""){
  193. return error_show(1004,"参数normal不能为空");
  194. }
  195. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  196. if($info['status']== 2){
  197. return error_show(1002,"退货单已验货");
  198. }
  199. $receive=$normal+array_sum(array_column($errorlist,'error_num'));
  200. $remark = isset($this->post['remark']) && $this->post['remark']!=="" ? trim($this->post['remark']):"";
  201. $info['received_num'] = $receive;
  202. $info['normal_num'] = $normal;
  203. $info['remark'] = $remark;
  204. $str = $info['status'];
  205. $info['status'] =empty($errorlist)?3: 2;
  206. $info['updatetime'] = date("Y-m-d H:i:s");
  207. Db::startTrans();
  208. try{
  209. $up=Db::name("order_back")->save($info);
  210. if($up){
  211. $stn = ["order_code"=>$info['thNo'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  212. ActionLog::logAdd($this->post['token'],$stn,"CKTHD",$info['status'],$info);
  213. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>$info['status'],"CKTHD"];
  214. ProcessOrder::AddProcess($this->post['token'],$process);
  215. if(empty($errorlist)){
  216. Db::commit();
  217. return app_show(0,'更新成功');
  218. }
  219. foreach($errorlist as $value){
  220. $data=[];
  221. isset($value['id'])&&$value['id']!=''?$data['id']=$value['id']:"";
  222. $data['thNo']=$thNo;
  223. $data['error_num']=$value['error_num'];
  224. $data['error_code']=$value['error_code'];
  225. $data['error_remark']=$value['error_remark'];
  226. $data['is_del']=isset($value['is_del'])?$value['is_del']:0;
  227. isset($value['id'])&&$value['id']!=''? "" : $data['addtime']=date("Y-m-d H:i:s");
  228. $data['updatetime']=date("Y-m-d H:i:s");
  229. $in = Db::name("order_backinfo")->save($data);
  230. if(!$in){
  231. Db::rollback();
  232. return error_show(1005,'更新失败');
  233. }
  234. }
  235. Db::commit();
  236. return app_show(0,'更新成功');
  237. }
  238. Db::rollback();
  239. return error_show(1005,'更新失败');
  240. }catch (\Exception $e){
  241. Db::rollback();
  242. return error_show(1004,$e->getMessage());
  243. }
  244. }
  245. public function CheckExam(){
  246. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  247. if($thNo==""){
  248. return error_show(1004,"参数thNo不能为空");
  249. }
  250. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  251. if(empty($info)){
  252. return error_show(1004,"未找到数据");
  253. }
  254. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  255. Db::startTrans();
  256. try{
  257. $temp=$info['status'];
  258. $info['status'] =3;
  259. $info['updatetime'] = date("Y-m-d H:i:s");
  260. $up=Db::name("order_back")->save($info);
  261. if($up){
  262. $stn = ["order_code"=>$thNo,"status"=>$temp,"action_remark"=>'',"action_type"=>"edit"];
  263. ActionLog::logAdd($this->post['token'],$stn,"CKTHD",$info['status'],$stn);
  264. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>3,"order_type"=>"CKTHD"];
  265. ProcessOrder::AddProcess($this->post['token'],$process);
  266. if(empty($errorlist)){
  267. $up =Db::name("order_backinfo")->where(["thNo"=>$thNo,"is_del"=>0])->save(["status"=>1,
  268. "updatetime"=>date("Y-m-d H:i:s")]);
  269. if($up){
  270. Db::commit();
  271. return app_show(0,'更新成功');
  272. }else{
  273. Db::rollback();
  274. return error_show(1005,'异常记录数据更新失败');
  275. }
  276. }else{
  277. foreach($errorlist as $value){
  278. $temp=Db::name("order_backinfo")->where(['id'=>$value["id"],"is_del"=>0,"thNo"=>$thNo])->find();
  279. if(empty($temp)){
  280. Db::rollback();
  281. return error_show(1005,'异常记录数据未找到');
  282. }
  283. $temp['status']=$value['status'];
  284. $temp['exam_remark']=$value['remark'];
  285. $temp['updatetime']=date("Y-m-d H:i:s");
  286. $com = Db::name("order_backinfo")->save($temp);
  287. if($com==false){
  288. Db::rollback();
  289. return error_show(1005,'异常记录数据更新失败');
  290. }
  291. }
  292. Db::commit();
  293. return app_show(0,'更新成功');
  294. }
  295. }
  296. Db::rollback();
  297. return error_show(1005,'更新失败');
  298. }catch (\Exception $e){
  299. Db::rollback();
  300. return error_show(1004,$e->getMessage());
  301. }
  302. }
  303. public function Exam(){
  304. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  305. if($thNo==""){
  306. return error_show(1004,"参数thNo不能为空");
  307. }
  308. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  309. if(empty($info)){
  310. return error_show(1004,"未找到数据");
  311. }
  312. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  313. if($status===""){
  314. return error_show(1004,"参数status不能为空");
  315. }
  316. $return_wsm=isset($this->post["return_wsm"])&&$this->post["return_wsm"]!=""?trim($this->post["return_wsm"]):"";
  317. $normal_num=isset($this->post["normal_num"])&&$this->post["normal_num"]!=""?intval($this->post["normal_num"])
  318. :"";
  319. // $defective_wsm=isset($this->post["defective_wsm"])&&$this->post["defective_wsm"]!=""?trim($this->post["defective_wsm"]):"";
  320. // $defective_num=isset($this->post["defective_num"])&&$this->post["defective_num"]!==""?intval($this->post["defective_num"]):"";
  321. // $loss_num=isset($this->post["loss_num"])&&$this->post["loss_num"]!=""?intval($this->post["loss_num"]):"";
  322. if($status==4){
  323. // if($defective_num===""){
  324. // return error_show(1004,"参数defective_num不能为空");
  325. // }
  326. // if($loss_num===""){
  327. // return error_show(1004,"参数loss_num不能为空");
  328. // }
  329. if($normal_num===""){
  330. return error_show(1004,"参数normal_num不能为空");
  331. }
  332. // if($defective_wsm===""){
  333. // return error_show(1004,"参数defective_wsm不能为空");
  334. // }
  335. if($return_wsm===""){
  336. return error_show(1004,"参数return_wsm不能为空");
  337. }
  338. $info['return_wsm']=$return_wsm;
  339. $info['normal_num']=$normal_num;
  340. // $info['loss_num']=$loss_num;//order_back表里没有这三个字段,不知道是干嘛的
  341. // $info['defective_wsm']=$defective_wsm;
  342. // $info['defective_num']=$defective_num;
  343. }
  344. $remark= isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  345. Db::startTrans();
  346. try{
  347. $var = $info['status'];
  348. $info['status'] =$status;
  349. $info['remark'] =$remark;
  350. $info['updatetime'] = date("Y-m-d H:i:s");
  351. $up=Db::name("order_back")->save($info);
  352. if($up){
  353. $stn = ["order_code"=>$thNo,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  354. ActionLog::logAdd($this->post['token'],$stn,"CKTHD",$info['status'],$this->post);
  355. $process=["order_code"=>$info['thNo'],"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>"CKTHD"];
  356. ProcessOrder::AddProcess($this->post['token'],$process);
  357. Db::commit();
  358. return app_show(0,'更新成功');
  359. }
  360. Db::rollback();
  361. return error_show(1005,'更新失败');
  362. }catch (\Exception $e){
  363. Db::rollback();
  364. return error_show(1004,$e->getMessage());
  365. }
  366. }
  367. //新写一个退货接口
  368. public function ExamNew()
  369. {
  370. $param = $this->request->only(['thNo', 'status', 'status_remark', 'can_sell_wsm' => '', 'can_sell_num' => 0, 'defective_wsm' => '', 'defective_num' => 0, 'loss_num' => 0], 'post', 'trim');
  371. $val = Validate::rule(['thNo|退货编码' => 'require', 'status|状态' => 'require|number|in:4,2,1', 'status_remark|备注' => 'checkRemark:']);
  372. $val->extend('checkRemark', function ($val, $rule, $data) {
  373. return $data['status'] == 4 ? true : (empty($val) ? '请填写备注' : true);
  374. });
  375. if (!$val->check($param)) return error_show(1004, $val->getError());
  376. // $good =Db::name("good")
  377. Db::startTrans();
  378. try {
  379. $info = Db::name("order_back")
  380. ->field('id,order_type,status,outCode,good_code,return_num,orderCode,returnCode')
  381. ->where(['thNo' => $param['thNo'], 'is_del' => 0])
  382. ->find();
  383. if (empty($info)) throw new Exception('未找到数据');
  384. $sale= Db::name('sale')->where(['orderCode'=>$info['orderCode'],"is_del"=>0])->find();
  385. if($sale==false){
  386. Db::rollback();
  387. return error_show(1004,"未找到销售单数据");
  388. }
  389. $out =Db::name("order_out")->where(['outCode'=>$info['outCode']])->find();
  390. if($out==false){
  391. Db::rollback();
  392. return error_show(1004,"未找到发货单数据");
  393. }
  394. $returninfo =Db::name("order_return")->where(['returnCode'=>$info['returnCode']])->find();
  395. if($returninfo==false){
  396. Db::rollback();
  397. return error_show(1004,"未找到售后单数据");
  398. }
  399. if($param['can_sell_num']!=0&& $param['can_sell_wsm']!=''){
  400. if($sale['is_stock']==1){ //库存品退回原仓库 备库单释放数量 采购单
  401. $param['can_sell_wsm'] = $out['wsm_code'];
  402. }
  403. $stock = Db::name("good_stock")->where(["spuCode"=>$info['good_code'],'wsm_code'=>$param['can_sell_wsm']])->find();
  404. if(empty($stock)) {
  405. $stock=[
  406. "spuCode"=>$info['good_code'],
  407. "wsm_code"=>$param['can_sell_wsm'],
  408. "usable_stock"=>0,
  409. "wait_out_stock"=>0,
  410. "wait_in_stock"=>0,
  411. "total_stock"=>0,
  412. "addtime"=>date("Y-m-d H:i:s"),
  413. "updatetime"=>date("Y-m-d H:i:s"),
  414. ];
  415. }
  416. $stock['usable_stock']+=$param['can_sell_num'];
  417. $stock['updatetime']=date("Y-m-d H:i:s");
  418. $st_up = Db::name("good_stock")->save($stock);
  419. if($st_up==false){
  420. return error_show(1005,'可售商品入库失败');
  421. }
  422. $good_data=['good_log_code'=>$param['thNo'],"stock_id"=>isset($stock['id'])? $stock['id']:Db::name("good_stock")->getLastInsID(),"type"=>1,'stock'=>$param['can_sell_num'],"stock_name"=>"usable_stock"];
  423. GoodLog::LogAdd($this->post['token'],$good_data,'RKTHD');
  424. }
  425. if($param['defective_num']!=0&& $param['defective_wsm']!=''){
  426. $defective = Db::name("good_stock")->where(["spuCode"=>$info['good_code'],'wsm_code'=>$param['defective_wsm']])->find();
  427. if(empty($defective)) {
  428. $defective=[
  429. "spuCode"=>$info['good_code'],
  430. "wsm_code"=>$param['defective_wsm'],
  431. "usable_stock"=>0,
  432. "wait_out_stock"=>0,
  433. "wait_in_stock"=>0,
  434. "total_stock"=>0,
  435. "addtime"=>date("Y-m-d H:i:s"),
  436. "updatetime"=>date("Y-m-d H:i:s"),
  437. ];
  438. $order = ["order_code"=>$info['good_code'],"status"=>1,"action_remark"=>'', "action_type"=>"create"];
  439. }else{
  440. $order = ["order_code"=>$info['good_code'],"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  441. }
  442. $defective['usable_stock']+=$param['defective_num'];
  443. $defective['updatetime'] = date("Y-m-d H:i:s");
  444. $upd= Db::name("good_stock")->save($defective);
  445. if($upd==false){
  446. Db::rollback();
  447. return error_show(1005,'次品商品入库失败');
  448. }
  449. $good_data=['good_log_code'=>$param['thNo'],"stock_id"=>isset($defective['id'])? $defective['id']:Db::name("good_stock")->getLastInsID(),"type"=>1,'stock'=>$param['defective_num'],"stock_name"=>"usable_stock"];
  450. GoodLog::LogAdd($this->post['token'],$good_data,'RKTHD');
  451. }
  452. $update_data = ['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')];
  453. //status==4通过,其他值表示驳回
  454. if ($param['status'] != 4) $update_data['status_remark'] = $param['status_remark'];
  455. else {
  456. $update_data['can_sell_wsm'] = $param['can_sell_wsm'];
  457. $update_data['can_sell_num'] = $param['can_sell_num'];
  458. $update_data['defective_wsm'] = $param['defective_wsm'];
  459. $update_data['defective_num'] = $param['defective_num'];
  460. $update_data['loss_num'] = $param['loss_num'];
  461. if($sale['is_stock']==1){
  462. $sale['th_num']+= $info['return_num'];
  463. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  464. $sale['updatetime']= date("Y-m-d H:i:s");
  465. $uap=Db::name("sale")->save($sale);
  466. if($uap==false){
  467. Db::rollback();
  468. return error_show(1005,'销售单订单更新失败');
  469. }
  470. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  471. if($ordernum==false){
  472. Db::rollback();
  473. return error_show(1005,'未找到关联采购单');
  474. }
  475. $ordernum['send_num']-=$info['return_num'];
  476. $orderup =Db::name("order_num")->save($ordernum);
  477. if($orderup==false){
  478. Db::rollback();
  479. return error_show(1005,'关联数据更新失败');
  480. }
  481. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  482. if($cgd==false){
  483. Db::rollback();
  484. return error_show(1005,'未找到采购单数据');
  485. }
  486. $cgd['th_fee']+= round($cgd['good_price']*$param['can_sell_num'],2);
  487. $cgd['th_num']+= $param['can_sell_num'];
  488. $cgd['updatetime']= date("Y-m-d H:i:s");
  489. $cgdup =Db::name("purchease_order")->save($cgd);
  490. if($cgdup==false){
  491. Db::rollback();
  492. return error_show(1005,'采购单数据更新失败');
  493. }
  494. if($cgd['bkcode']!=""){
  495. $bk=Db::name("purchease_order")->where(["bkcode"=>$cgd['bkcode'],"order_type"=>1,"is_del"=>0])
  496. ->find();
  497. if($bk==false){
  498. Db::rollback();
  499. return error_show(1005,'未找到备库单数据');
  500. }
  501. $orderbk = Db::name("order_bk")->where(['cgdNo'=>$bk['cgdNo'],"is_del"=>0])->find();
  502. if($orderbk==false){
  503. Db::rollback();
  504. return error_show(1005,'备库单未完全入库');
  505. }
  506. $merge_num = Db::name("purchease_order")->where(["bkcode"=>$bk['bkcode'],"order_type"=>5,"is_del"=>0])
  507. ->field("sum(send_num)-sum(th_num) as num")->find();
  508. $orderbk['balance_num']=$orderbk['total_num']-$merge_num['num'];
  509. $orderbk['merge_num']=$merge_num['num'];
  510. $orderbk['updatetime']=date("Y-m-d H:i:s");
  511. $orderbkup=Db::name("order_bk")->save($orderbk);
  512. if($orderbkup==false){
  513. Db::rollback();
  514. return error_show(1005,'备库单库存数据释放失败');
  515. }
  516. }
  517. }else{
  518. if($returninfo['return_tag']==1){
  519. $sale['th_num']+= $info['return_num'];
  520. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  521. $sale['updatetime']= date("Y-m-d H:i:s");
  522. $uap=Db::name("sale")->save($sale);
  523. if($uap==false){
  524. Db::rollback();
  525. return error_show(1005,'销售单订单更新失败');
  526. }
  527. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  528. if($ordernum==false){
  529. Db::rollback();
  530. return error_show(1005,'未找到关联采购单');
  531. }
  532. $ordernum['send_num']-=$info['return_num'];
  533. $orderup =Db::name("order_num")->save($ordernum);
  534. if($orderup==false){
  535. Db::rollback();
  536. return error_show(1005,'关联数据更新失败');
  537. }
  538. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  539. if($cgd==false){
  540. Db::rollback();
  541. return error_show(1005,'未找到采购单数据');
  542. }
  543. $cgd['th_num']+= $info['return_num'];
  544. $cgd['th_fee']+= round($info['return_num']*$cgd['good_price'],2);
  545. $cgd['updatetime']= date("Y-m-d H:i:s");
  546. $cgdup =Db::name("purchease_order")->save($cgd);
  547. if($cgdup==false){
  548. Db::rollback();
  549. return error_show(1005,'采购单数据更新失败');
  550. }
  551. }else{
  552. $sale['th_num']+= $info['return_num'];
  553. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  554. $sale['updatetime']= date("Y-m-d H:i:s");
  555. $uap=Db::name("sale")->save($sale);
  556. if($uap==false){
  557. Db::rollback();
  558. return error_show(1005,'销售单订单更新失败');
  559. }
  560. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  561. if($ordernum==false){
  562. Db::rollback();
  563. return error_show(1005,'未找到关联采购单');
  564. }
  565. $ordernum['send_num']-=$info['return_num'];
  566. $orderup =Db::name("order_num")->save($ordernum);
  567. if($orderup==false){
  568. Db::rollback();
  569. return error_show(1005,'关联数据更新失败');
  570. }
  571. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  572. if($cgd==false){
  573. Db::rollback();
  574. return error_show(1005,'未找到采购单数据');
  575. }
  576. $cgd['th_fee']+= round($cgd['good_price']*$param['can_sell_num'],2);
  577. $cgd['th_num']+= $param['can_sell_num'];
  578. $cgd['updatetime']= date("Y-m-d H:i:s");
  579. $cgdup =Db::name("purchease_order")->save($cgd);
  580. if($cgdup==false){
  581. Db::rollback();
  582. return error_show(1005,'采购单数据更新失败');
  583. }
  584. }
  585. }
  586. }
  587. $res = Db::name("order_back")
  588. ->where(['id' => $info['id']])
  589. ->update($update_data);
  590. if ($res) {
  591. $stn = ["order_code" => $param['thNo'], "status" => $info['status'], "action_remark" => '', "action_type" => "edit"];
  592. ActionLog::logAdd($this->post['token'], $stn, "CKTHD", $param['status'], $stn);
  593. $process = ["order_code" => $param['thNo'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" =>"CKTHD"];
  594. ProcessOrder::AddProcess($this->post['token'], $process);
  595. Db::commit();
  596. return app_show(0, '操作成功');
  597. } else throw new Exception('操作失败');
  598. } catch (Exception $exception) {
  599. Db::rollback();
  600. return error_show(1005, $exception->getMessage());
  601. }
  602. }
  603. }