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