Orderback.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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()->toArray();
  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(['id' => $this->uid, 'nickname' => $this->uname],$stn,"CKTHD",$info['status'],$info);
  220. if($info['status'] == 2) $roleid = [10, 31, 41];//特殊处理,写入到10采购负责人、31库管人员、41库管-张凯旋这三个角色下的所有人
  221. else $roleid = [31,41];//特殊处理,写入到31库管人员、41库管-张凯旋这三个角色下的所有人
  222. $handle_user_list = Db::name('user_role')
  223. ->where('is_del', 0)
  224. ->whereIn('roleid', $roleid)
  225. ->column('uid');
  226. $process = ["order_code" => $info['thNo'], "order_id" => $info['id'], "order_status" => $info['status'], 'order_type' => "CKTHD", "before_status" => $str, 'handle_user_list' => implode(',', $handle_user_list)];
  227. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  228. if(empty($errorlist)){
  229. Db::commit();
  230. return app_show(0,'更新成功');
  231. }
  232. foreach($errorlist as $value){
  233. $data=[];
  234. isset($value['id'])&&$value['id']!=''?$data['id']=$value['id']:"";
  235. $data['thNo']=$thNo;
  236. $data['error_num']=$value['error_num'];
  237. $data['error_code']=$value['error_code'];
  238. $data['error_remark']=$value['error_remark'];
  239. $data['is_del']=isset($value['is_del'])?$value['is_del']:0;
  240. isset($value['id'])&&$value['id']!=''? "" : $data['addtime']=date("Y-m-d H:i:s");
  241. $data['updatetime']=date("Y-m-d H:i:s");
  242. $in = Db::name("order_backinfo")->save($data);
  243. if(!$in){
  244. Db::rollback();
  245. return error_show(1005,'更新失败');
  246. }
  247. }
  248. Db::commit();
  249. return app_show(0,'更新成功');
  250. }
  251. Db::rollback();
  252. return error_show(1005,'更新失败');
  253. }catch (\Exception $e){
  254. Db::rollback();
  255. return error_show(1004,$e->getMessage());
  256. }
  257. }
  258. public function CheckExam(){
  259. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  260. if($thNo==""){
  261. return error_show(1004,"参数thNo不能为空");
  262. }
  263. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  264. if(empty($info)){
  265. return error_show(1004,"未找到数据");
  266. }
  267. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  268. Db::startTrans();
  269. try{
  270. $temp=$info['status'];
  271. $info['status'] =3;
  272. $info['updatetime'] = date("Y-m-d H:i:s");
  273. $up=Db::name("order_back")->save($info);
  274. if($up){
  275. $stn = ["order_code"=>$thNo,"status"=>$temp,"action_remark"=>'',"action_type"=>"edit"];
  276. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $stn, "CKTHD", $info['status'], $stn);
  277. //特殊处理,写入到31库管人员、41库管-张凯旋这三个角色下的所有人
  278. $handle_user_list = Db::name('user_role')
  279. ->where('is_del', 0)
  280. ->whereIn('roleid', [31, 41])
  281. ->column('uid');
  282. $process = ["order_code" => $info['thNo'], "order_id" => $info['id'], "order_status" => 3, "order_type" => "CKTHD", "before_status" => $temp, 'handle_user_list' => implode(',', $handle_user_list)];
  283. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  284. if(empty($errorlist)){
  285. $up =Db::name("order_backinfo")->where(["thNo"=>$thNo,"is_del"=>0])->save(["status"=>1,
  286. "updatetime"=>date("Y-m-d H:i:s")]);
  287. if($up){
  288. Db::commit();
  289. return app_show(0,'更新成功');
  290. }else{
  291. Db::rollback();
  292. return error_show(1005,'异常记录数据更新失败');
  293. }
  294. }else{
  295. foreach($errorlist as $value){
  296. $temp=Db::name("order_backinfo")->where(['id'=>$value["id"],"is_del"=>0,"thNo"=>$thNo])->find();
  297. if(empty($temp)){
  298. Db::rollback();
  299. return error_show(1005,'异常记录数据未找到');
  300. }
  301. $temp['status']=$value['status'];
  302. $temp['exam_remark']=$value['remark'];
  303. $temp['updatetime']=date("Y-m-d H:i:s");
  304. $com = Db::name("order_backinfo")->save($temp);
  305. if($com==false){
  306. Db::rollback();
  307. return error_show(1005,'异常记录数据更新失败');
  308. }
  309. }
  310. Db::commit();
  311. return app_show(0,'更新成功');
  312. }
  313. }
  314. Db::rollback();
  315. return error_show(1005,'更新失败');
  316. }catch (\Exception $e){
  317. Db::rollback();
  318. return error_show(1004,$e->getMessage());
  319. }
  320. }
  321. public function Exam(){
  322. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  323. if($thNo==""){
  324. return error_show(1004,"参数thNo不能为空");
  325. }
  326. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  327. if(empty($info)){
  328. return error_show(1004,"未找到数据");
  329. }
  330. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  331. if($status===""){
  332. return error_show(1004,"参数status不能为空");
  333. }
  334. $return_wsm=isset($this->post["return_wsm"])&&$this->post["return_wsm"]!=""?trim($this->post["return_wsm"]):"";
  335. $normal_num=isset($this->post["normal_num"])&&$this->post["normal_num"]!=""?intval($this->post["normal_num"])
  336. :"";
  337. // $defective_wsm=isset($this->post["defective_wsm"])&&$this->post["defective_wsm"]!=""?trim($this->post["defective_wsm"]):"";
  338. // $defective_num=isset($this->post["defective_num"])&&$this->post["defective_num"]!==""?intval($this->post["defective_num"]):"";
  339. // $loss_num=isset($this->post["loss_num"])&&$this->post["loss_num"]!=""?intval($this->post["loss_num"]):"";
  340. if($status==4){
  341. // if($defective_num===""){
  342. // return error_show(1004,"参数defective_num不能为空");
  343. // }
  344. // if($loss_num===""){
  345. // return error_show(1004,"参数loss_num不能为空");
  346. // }
  347. if($normal_num===""){
  348. return error_show(1004,"参数normal_num不能为空");
  349. }
  350. // if($defective_wsm===""){
  351. // return error_show(1004,"参数defective_wsm不能为空");
  352. // }
  353. if($return_wsm===""){
  354. return error_show(1004,"参数return_wsm不能为空");
  355. }
  356. $info['return_wsm']=$return_wsm;
  357. $info['normal_num']=$normal_num;
  358. // $info['loss_num']=$loss_num;//order_back表里没有这三个字段,不知道是干嘛的
  359. // $info['defective_wsm']=$defective_wsm;
  360. // $info['defective_num']=$defective_num;
  361. }
  362. $remark= isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  363. Db::startTrans();
  364. try{
  365. $var = $info['status'];
  366. $info['status'] =$status;
  367. $info['remark'] =$remark;
  368. $info['updatetime'] = date("Y-m-d H:i:s");
  369. $up=Db::name("order_back")->save($info);
  370. if($up){
  371. $stn = ["order_code"=>$thNo,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  372. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$stn,"CKTHD",$info['status'],$this->post);
  373. if (in_array($status, [2, 3])) {
  374. if ($status == 2) $roleid = [10, 31, 41];//特殊处理,写入到10采购负责人、31库管人员、41库管-张凯旋这三个角色下的所有人
  375. else $roleid = [31, 41];//特殊处理,写入到、31库管人员、41库管-张凯旋这三个角色下的所有人
  376. $handle_user_list = Db::name('user_role')
  377. ->where('is_del', 0)
  378. ->whereIn('roleid', $roleid)
  379. ->column('uid');
  380. $process = ["order_code" => $info['thNo'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "CKTHD", "before_status" => $var, 'handle_user_list' => implode(',', $handle_user_list)];
  381. } else $process = ["order_code" => $info['thNo'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "CKTHD", "before_status" => $var];
  382. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  383. Db::commit();
  384. return app_show(0,'更新成功');
  385. }
  386. Db::rollback();
  387. return error_show(1005,'更新失败');
  388. }catch (\Exception $e){
  389. Db::rollback();
  390. return error_show(1004,$e->getMessage());
  391. }
  392. }
  393. //新写一个退货接口
  394. public function ExamNew()
  395. {
  396. $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');
  397. $val = Validate::rule(['thNo|退货编码' => 'require', 'status|状态' => 'require|number|in:4,2,1', 'status_remark|备注' => 'checkRemark:']);
  398. $val->extend('checkRemark', function ($val, $rule, $data) {
  399. return $data['status'] == 4 ? true : (empty($val) ? '请填写备注' : true);
  400. });
  401. if (!$val->check($param)) return error_show(1004, $val->getError());
  402. Db::startTrans();
  403. try {
  404. $info = Db::name("order_back")
  405. ->field('id,order_type,status,outCode,good_code,return_num,orderCode,returnCode,apply_id,apply_name')
  406. ->where(['thNo' => $param['thNo'], 'is_del' => 0])
  407. ->find();
  408. if (empty($info)) throw new Exception('未找到数据');
  409. $sale= Db::name('sale')->where(['orderCode'=>$info['orderCode'],"is_del"=>0])->find();
  410. if($sale==false){
  411. Db::rollback();
  412. return error_show(1004,"未找到销售单数据");
  413. }
  414. $spuCode= $info['good_code'];
  415. if($sale['order_type']!==1){
  416. if($sale['order_type']==3){
  417. $goon = Db::name("good_zixun")->where(["spuCode"=>$sale['good_code'],"is_del"=>0])->find();
  418. $isZx=1;
  419. }else {
  420. $goon = Db::name('good_basic')->where(['spuCode' => $sale['good_code']])->find();
  421. $isZx=2;
  422. }
  423. $spuCode = $this->CheckGood($goon,$isZx,$param['thNo']);
  424. }
  425. $out =Db::name("order_out")->where(['outCode'=>$info['outCode']])->find();
  426. if($out==false){
  427. Db::rollback();
  428. return error_show(1004,"未找到发货单数据");
  429. }
  430. $returninfo =Db::name("order_return")->where(['returnCode'=>$info['returnCode']])->find();
  431. if($returninfo==false){
  432. Db::rollback();
  433. return error_show(1004,"未找到售后单数据");
  434. }
  435. $sabebn =Db::name("sale_info")->where([["orderCode",'=',$info['orderCode']],["num",">",0]])->select()->toArray();
  436. if($param['can_sell_num']!=0&& $param['can_sell_wsm']!=''){
  437. if($sale['is_stock']==1){ //库存品退回原仓库 备库单释放数量 采购单
  438. $param['can_sell_wsm'] = $out['wsm_code'];
  439. }
  440. $stock = Db::name("good_stock")->where(["spuCode"=>$spuCode,'wsm_code'=>$param['can_sell_wsm']])
  441. ->find();
  442. if(empty($stock)) {
  443. $stock=[
  444. "spuCode"=>$spuCode,
  445. "wsm_code"=>$param['can_sell_wsm'],
  446. "usable_stock"=>0,
  447. "wait_out_stock"=>0,
  448. "wait_in_stock"=>0,
  449. "total_stock"=>0,
  450. "addtime"=>date("Y-m-d H:i:s"),
  451. "updatetime"=>date("Y-m-d H:i:s"),
  452. ];
  453. }
  454. $stock['usable_stock']+=$param['can_sell_num'];
  455. $stock['updatetime']=date("Y-m-d H:i:s");
  456. $st_up = Db::name("good_stock")->save($stock);
  457. if($st_up==false){
  458. return error_show(1005,'可售商品入库失败');
  459. }
  460. $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  461. if(!empty($sabebn)){
  462. $total_num =$param['can_sell_num'];
  463. foreach ($sabebn as $ve){
  464. $tempnum=0;
  465. if ($total_num==0) break;
  466. if($total_num>=$ve['num']){
  467. $tempnum = $ve['num'];
  468. $total_num-=$ve['num'];
  469. $ve['num']=0;
  470. }else{
  471. $tempnum = $total_num;
  472. $ve['num']-=$total_num;
  473. $total_num=0;
  474. }
  475. $bnin=GoodStockInfo::ReturnAdd($stockid,$ve['bnCode'],$tempnum,$ve['stockid']);
  476. if($bnin==false){
  477. Db::rollback();
  478. return error_show(1005, '可售商品Bn库存数入库失败');
  479. }
  480. $up=Db::name("sale_info")->save($ve);
  481. if($up==false){
  482. Db::rollback();
  483. return error_show(1005, '可售商品Bn库存数入库失败');
  484. }
  485. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  486. if($bnin==false){
  487. Db::rollback();
  488. return error_show(1005, '可售商品Bn库存数入库失败');
  489. }
  490. }
  491. }
  492. $good_data=['good_log_code'=>$param['thNo'],"stock_id"=> $stockid,"type"=>1,'stock'=>$param['can_sell_num'],"stock_name"=>"usable_stock"];
  493. GoodLog::LogAdd($this->post['token'],$good_data,'RKTHD');
  494. }
  495. if($param['defective_num']!=0&& $param['defective_wsm']!=''){
  496. $defective = Db::name("good_stock")->where(["spuCode"=>$spuCode,'wsm_code'=>$param['defective_wsm']])->find();
  497. if(empty($defective)) {
  498. $defective=[
  499. "spuCode"=>$spuCode,
  500. "wsm_code"=>$param['defective_wsm'],
  501. "usable_stock"=>0,
  502. "wait_out_stock"=>0,
  503. "wait_in_stock"=>0,
  504. "total_stock"=>0,
  505. "addtime"=>date("Y-m-d H:i:s"),
  506. "updatetime"=>date("Y-m-d H:i:s"),
  507. ];
  508. $order = ["order_code"=>$spuCode,"status"=>1,"action_remark"=>'', "action_type"=>"create"];
  509. }else{
  510. $order = ["order_code"=>$spuCode,"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  511. }
  512. $defective['usable_stock']+=$param['defective_num'];
  513. $defective['updatetime'] = date("Y-m-d H:i:s");
  514. $upd= Db::name("good_stock")->save($defective);
  515. if($upd==false){
  516. Db::rollback();
  517. return error_show(1005,'次品商品入库失败');
  518. }
  519. $stockid = isset($defective['id']) ? $defective['id'] : Db::name("good_stock")->getLastInsID();
  520. $sabebn =Db::name("sale_info")->where(["orderCode"=>$info['orderCode']])->select()->toArray();
  521. if(!empty($sabebn)){
  522. $total_num =$param['defective_num'];
  523. foreach ($sabebn as $ve){
  524. $tempnum=0;
  525. if ($total_num==0) break;
  526. if($total_num>=$ve['num']){
  527. $tempnum = $ve['num'];
  528. $total_num-=$ve['num'];
  529. $ve['num']=0;
  530. }else{
  531. $tempnum = $total_num;
  532. $ve['num']-=$total_num;
  533. $total_num=0;
  534. }
  535. $bnin=GoodStockInfo::ReturnAdd($stockid,$ve['bnCode'],$tempnum,$ve['stockid']);
  536. if($bnin==false){
  537. Db::rollback();
  538. return error_show(1005, '可售商品Bn库存数入库失败');
  539. }
  540. $up=Db::name("sale_info")->save($ve);
  541. if($up==false){
  542. Db::rollback();
  543. return error_show(1005, '可售商品Bn库存数入库失败');
  544. }
  545. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  546. if($bnin==false){
  547. Db::rollback();
  548. return error_show(1005, '可售商品Bn库存数入库失败');
  549. }
  550. }
  551. }
  552. $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"];
  553. GoodLog::LogAdd($this->post['token'],$good_data,'RKTHD');
  554. }
  555. $update_data = ['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')];
  556. //status==4通过,其他值表示驳回
  557. if ($param['status'] != 4) $update_data['status_remark'] = $param['status_remark'];
  558. else {
  559. $update_data['can_sell_wsm'] = $param['can_sell_wsm'];
  560. $update_data['can_sell_num'] = $param['can_sell_num'];
  561. $update_data['defective_wsm'] = $param['defective_wsm'];
  562. $update_data['defective_num'] = $param['defective_num'];
  563. $update_data['loss_num'] = $param['loss_num'];
  564. if($sale['is_stock']==1){
  565. $sale['th_num']+= $info['return_num'];
  566. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  567. $sale['status']=3;
  568. }
  569. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  570. $sale['updatetime']= date("Y-m-d H:i:s");
  571. $uap=Db::name("sale")->save($sale);
  572. if($uap==false){
  573. Db::rollback();
  574. return error_show(1005,'销售单订单更新失败');
  575. }
  576. // $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  577. // if($ordernum==false){
  578. // Db::rollback();
  579. // return error_show(1005,'未找到关联采购单');
  580. // }
  581. // $ordernum['send_num']-=$info['return_num'];
  582. // $orderup =Db::name("order_num")->save($ordernum);
  583. // if($orderup==false){
  584. // Db::rollback();
  585. // return error_show(1005,'关联数据更新失败');
  586. // }
  587. // $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  588. // if($cgd==false){
  589. // Db::rollback();
  590. // return error_show(1005,'未找到采购单数据');
  591. // }
  592. // $cgd['th_fee']+= round($cgd['good_price']*$param['can_sell_num'],2);
  593. // $cgd['th_num']+= $param['can_sell_num'];
  594. // if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  595. // $cgd['status']=4;
  596. // }
  597. // $cgd['updatetime']= date("Y-m-d H:i:s");
  598. // $cgdup =Db::name("purchease_order")->save($cgd);
  599. // if($cgdup==false){
  600. // Db::rollback();
  601. // return error_show(1005,'采购单数据更新失败');
  602. // }
  603. // if($cgd['bkcode']!=""){
  604. // $bk=Db::name("purchease_order")->where(["bkcode"=>$cgd['bkcode'],"order_type"=>1,"order_source"=>0,"is_del"=>0])->find();
  605. // if($bk==false){
  606. // Db::rollback();
  607. // return error_show(1005,'未找到备库单数据');
  608. // }
  609. // $orderbk = Db::name("order_bk")->where(['cgdNo'=>$bk['cgdNo'],"is_del"=>0])->find();
  610. // if($orderbk==false){
  611. // Db::rollback();
  612. // return error_show(1005,'备库单未完全入库');
  613. // }
  614. // $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();
  615. //
  616. // $orderbk['balance_num']=$orderbk['total_num']-$merge_num['num'];
  617. // $orderbk['merge_num']=$merge_num['num'];
  618. // $orderbk['updatetime']=date("Y-m-d H:i:s");
  619. // $orderbkup=Db::name("order_bk")->save($orderbk);
  620. // if($orderbkup==false){
  621. // Db::rollback();
  622. // return error_show(1005,'备库单库存数据释放失败');
  623. // }
  624. // }
  625. }else{
  626. if($returninfo['return_tag']==1){
  627. $sale['th_num']+= $info['return_num'];
  628. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  629. $sale['status']=3;
  630. }
  631. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  632. $sale['updatetime']= date("Y-m-d H:i:s");
  633. $uap=Db::name("sale")->save($sale);
  634. if($uap==false){
  635. Db::rollback();
  636. return error_show(1005,'销售单订单更新失败');
  637. }
  638. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  639. if($ordernum==false){
  640. Db::rollback();
  641. return error_show(1005,'未找到关联采购单');
  642. }
  643. $ordernum['send_num']-=$info['return_num'];
  644. $orderup =Db::name("order_num")->save($ordernum);
  645. if($orderup==false){
  646. Db::rollback();
  647. return error_show(1005,'关联数据更新失败');
  648. }
  649. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  650. if($cgd==false){
  651. Db::rollback();
  652. return error_show(1005,'未找到采购单数据');
  653. }
  654. $cgd['th_num']+= $info['return_num'];
  655. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  656. $cgd['status']=4;
  657. }
  658. $cgd['th_fee']+= round($info['return_num']*$cgd['good_price'],2);
  659. $cgd['updatetime']= date("Y-m-d H:i:s");
  660. $cgdup =Db::name("purchease_order")->save($cgd);
  661. if($cgdup==false){
  662. Db::rollback();
  663. return error_show(1005,'采购单数据更新失败');
  664. }
  665. }else{
  666. $sale['th_num']+= $info['return_num'];
  667. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  668. $sale['status']=3;
  669. }
  670. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  671. $sale['updatetime']= date("Y-m-d H:i:s");
  672. $uap=Db::name("sale")->save($sale);
  673. if($uap==false){
  674. Db::rollback();
  675. return error_show(1005,'销售单订单更新失败');
  676. }
  677. // $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  678. // if($ordernum==false){
  679. // Db::rollback();
  680. // return error_show(1005,'未找到关联采购单');
  681. // }
  682. // $ordernum['send_num']-=$info['return_num'];
  683. // $orderup =Db::name("order_num")->save($ordernum);
  684. // if($orderup==false){
  685. // Db::rollback();
  686. // return error_show(1005,'关联数据更新失败');
  687. // }
  688. // $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  689. // if($cgd==false){
  690. // Db::rollback();
  691. // return error_show(1005,'未找到采购单数据');
  692. // }
  693. // $cgd['th_fee']+= round($cgd['good_price']*$param['can_sell_num'],2);
  694. // $cgd['th_num']+= $param['can_sell_num'];
  695. // if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  696. // $cgd['status']=4;
  697. // }
  698. // $cgd['updatetime']= date("Y-m-d H:i:s");
  699. // $cgdup =Db::name("purchease_order")->save($cgd);
  700. // if($cgdup==false){
  701. // Db::rollback();
  702. // return error_show(1005,'采购单数据更新失败');
  703. // }
  704. }
  705. }
  706. }
  707. $res = Db::name("order_back")
  708. ->where(['id' => $info['id']])
  709. ->update($update_data);
  710. if ($res) {
  711. $data=[
  712. "orderCode"=>$info['orderCode'],
  713. "th_type"=>3,
  714. "th_num"=>$info['return_num'],
  715. "th_fee"=>round($info['return_num']*$sale['sale_price'],2),
  716. "thCode"=>$info['returnCode'],
  717. "apply_id"=>$info['apply_id'],
  718. "apply_name"=>$info['apply_name'],
  719. "spuCode"=>$sale['good_code'],
  720. "good_name"=>$sale['good_name'],
  721. "cat_id"=>$sale['cat_id'],
  722. "addtime"=>date("Y-m-d H:i:s"),
  723. "status"=>1,
  724. "is_del"=>0
  725. ];
  726. $inse=Db::name("th_data")->insert($data);
  727. if($inse==false){
  728. Db::rollback();
  729. return error_show(1004,"退货单更新失败");
  730. }
  731. if($info['return_num']>=$out['send_num']){
  732. $out['status']=4;
  733. $out['updatetime']=date("Y-m-d H:i:s");
  734. $upout =Db::name("order_out")->save($out);
  735. if($upout==false){
  736. Db::rollback();
  737. return error_show(1005,'出库单数据更新失败');
  738. }
  739. }
  740. $stn = ["order_code" => $param['thNo'], "status" => $info['status'], "action_remark" => '', "action_type" => "edit"];
  741. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stn, "CKTHD", $param['status'], $stn);
  742. if (in_array($param['status'], [1,2, 3])) {
  743. if ($param['status'] == 2) $roleid = [10, 31, 41];//特殊处理,写入到10采购负责人、31库管人员、41库管-张凯旋这三个角色下的所有人
  744. else $roleid = [31, 41];//特殊处理,写入到、31库管人员、41库管-张凯旋这三个角色下的所有人
  745. $handle_user_list = Db::name('user_role')
  746. ->where('is_del', 0)
  747. ->whereIn('roleid', $roleid)
  748. ->column('uid');
  749. //如果是退货商品验收(节点1),把供应商负责人也加进去
  750. if ($param['status'] == 1) $handle_user_list[] = $sale['cgderid'];
  751. $process = ["order_code" => $param['thNo'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "CKTHD", "before_status" => $info['status'], 'handle_user_list' => implode(',', $handle_user_list)];
  752. } else $process = ["order_code" => $param['thNo'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "CKTHD", "before_status" => $info['status']];
  753. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  754. Db::commit();
  755. return app_show(0, '操作成功');
  756. } else throw new Exception('操作失败');
  757. } catch (Exception $exception) {
  758. Db::rollback();
  759. return error_show(1005, $exception->getMessage());
  760. }
  761. }
  762. /**
  763. * @param $good
  764. * @param $isZx
  765. * @param string $thNo
  766. * @return mixed|string
  767. * @throws \think\Exception
  768. */
  769. private function CheckGood($good,$isZx,$thNo=''){
  770. $spuCode= makeNo("SKU");
  771. //查询是否存在转库存品记录 存在则返回库存编号
  772. $newCode= Db::name("good_change_log")->where(["oldCode"=>$good['spuCode']])->value("newCode",'');
  773. if($newCode==''){
  774. $log=["oldCode"=>$good['spuCode'],
  775. "order_source"=>$isZx,
  776. "newCode"=>$spuCode,
  777. "thCode"=>$thNo,
  778. "createrid"=>$this->uid,
  779. "creater"=>$this->uname,
  780. "addtime"=>date("Y-m-d H:i:s")
  781. ];
  782. }else{
  783. return $newCode;
  784. }
  785. if($isZx==1){
  786. $supplier =Db::name("supplier")->where(["code"=>$good['supplierNo']])->findOrEmpty();
  787. if(empty($supplier)) throw new Exception('非库存商品转库存失败');
  788. $good = [
  789. 'spuCode' => $spuCode,
  790. 'good_code' => $good['good_code'],
  791. 'good_name' => $good['good_name'],
  792. 'cat_id' => $good['cat_id'],
  793. 'brand_id' => $good['brand_id'],
  794. 'companyNo' => $good['companyNo'],
  795. 'good_unit' => $good['good_unit'],
  796. 'good_type' => $good['good_type'],
  797. 'moq' => $good['moq'],
  798. 'customized' => $good['customized'],
  799. 'tax' => $good['tax'],
  800. 'supplierNo' => $good['supplierNo'],
  801. 'is_auth' => $good['is_auth'],
  802. 'auth_img' => $good['auth_img'],
  803. 'after_sales' => '',
  804. 'craft_desc' => $good['craft_desc'],
  805. 'good_remark' => $good['good_remark'],
  806. 'weight' => $good['weight'],
  807. 'packing_way' => '',
  808. 'packing_size' => '',
  809. 'packing_spec' => '',
  810. 'packing_list' => '',
  811. 'packing_weight' => '',
  812. 'good_bar' => '',
  813. 'supply_area' => $good['supply_area'],
  814. 'delivery_place' => '',
  815. 'origin_place' => '',
  816. 'delivery_day' => '',
  817. 'lead_time' => '',
  818. 'sample_day' => '',
  819. 'sample_fee' => '',
  820. 'good_img' => '',//$good['good_img'],
  821. 'good_thumb_img' => $good['good_thumb_img'],
  822. 'good_info_img' => $good['good_img'],//商品主图对应到商品详情
  823. 'cert_fee' => '',
  824. 'packing_fee' => '',
  825. 'cost_fee' => '',
  826. 'mark_fee' => '',
  827. 'demo_fee' => '',
  828. 'open_fee' => '',
  829. 'noble_metal' => $good['noble_metal'],
  830. 'noble_weight' => $good['good_weight'],
  831. 'is_gold_price' => $good['is_gold_price'],
  832. 'cgd_gold_price' => '',
  833. 'market_price' => '',
  834. 'nake_price' => '',
  835. 'is_step' => '',
  836. 'is_online' => '0',
  837. 'status' =>0,//咨询商品待编辑
  838. 'createrid' =>$good['createrid'],
  839. 'creater' => $good['creater'],
  840. 'field_change' => '',
  841. 'is_del' => 0,
  842. 'is_support_stock' => 0,
  843. 'addtime' => date('Y-m-d H:i:s'),
  844. 'updatetime' => date('Y-m-d H:i:s'),
  845. 'is_diff' => $good['is_diff'],
  846. 'config' => $good['config'],
  847. 'other_config' => $good['other_config'],
  848. 'chargerid'=>$supplier['personid']??$good['createrid'],
  849. 'charger'=>$supplier['person']??$good['creater'],//负责人跟创建人一致
  850. ];
  851. }else{
  852. $good['id']=null;
  853. $good['spuCode']=$spuCode;
  854. $good['is_stock']=1;
  855. $good['is_support_stock']=0;
  856. $good['status']=0;
  857. $good['addtime']=date("Y-m-d H:i:s");
  858. $good['updatetime']=date("Y-m-d H:i:s");
  859. }
  860. $insert =Db::name("good_basic")->insert($good);
  861. if($insert==false) throw new \Exception("非库存商品转库存失败",1005);
  862. $newAdd=Db::name("good_change_log")->insert($log);
  863. if($newAdd==false) throw new \Exception("非库存商品转库存失败",1005);
  864. return $spuCode;
  865. }
  866. }