Orderback.php 34 KB

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