Orderback.php 33 KB

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