Orderback.php 38 KB

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