Orderback.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\DataGroup as DataGroupModel;
  5. use app\admin\model\GoodLog;
  6. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  7. use think\App;
  8. use think\Exception;
  9. use think\facade\Db;
  10. use think\facade\Validate;
  11. //退货单
  12. class Orderback extends Base
  13. {
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. }
  18. public function list(){
  19. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  20. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  21. $where =[['is_del',"=",0]];
  22. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  23. if($thNo!=""){
  24. $where[]=['thNo',"like", "%{$thNo}%"];
  25. }
  26. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  27. if($status!==""){
  28. // $where['status'] = $status;
  29. $where[]=['status',"=", $status];
  30. }
  31. $post_code= isset($this->post['post_code']) && $this->post['post_code']!="" ? trim($this->post['post_code']):"";
  32. if($post_code!=""){
  33. $where[]=['post_code',"like", "%{$post_code}%"];
  34. }
  35. $post_compay = isset($this->post['post_compay']) && $this->post['post_compay']!="" ? trim($this->post['post_compay']):"";
  36. if($post_compay!=""){
  37. $where[]=['post_company',"=", $post_compay];
  38. }
  39. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code']!="" ? trim($this->post['customer_code']):"";
  40. if($customer_code!=""){
  41. $where[]=['customer_code',"like", "%{$customer_code}%"];
  42. }
  43. $order_code = isset($this->post['order_code']) && $this->post['order_code']!="" ? trim($this->post['order_code']):"";
  44. if($order_code!=""){
  45. $where[]=['orderCode',"like", "%{$order_code}%"];
  46. }
  47. $out_code = isset($this->post['out_code']) && $this->post['out_code']!="" ? trim($this->post['out_code']):"";
  48. if($out_code!=""){
  49. $where[]=['outCode',"like", "%{$out_code}%"];
  50. }
  51. $return_code = isset($this->post['return_code']) && $this->post['return_code']!="" ? trim($this->post['return_code']):"";
  52. if($return_code!=""){
  53. $where[]=['returnCode',"like", "%{$return_code}%"];
  54. }
  55. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  56. if($start!==""){
  57. $where[]=['addtime',">=", $start];
  58. }
  59. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  60. if($end!==""){
  61. $where[]=['addtime',"<=", $end];
  62. }
  63. $condition='';
  64. // $role=$this->checkRole();
  65. // if(!empty($role['write']) && $this->uid!=""){
  66. // // $where[]=["a.apply_id","in",$role['write']];
  67. // $condition .="cgderid = {$this->uid} or apply_id in (".implode(',',$role['write']).")";
  68. // }
  69. $role = $this->checkDataShare();
  70. if (!empty($role[DataGroupModel::$type_全部])) $condition .= "cgderid = {$this->uid} or apply_id in (" . implode(',', $role[DataGroupModel::$type_全部]) . ")";
  71. // if(!empty($role['platform']) ){
  72. // $where[]=["platform_id","in",$role['platform']];
  73. // }
  74. $count=Db::name("order_back")->where($where)->where($condition)->count();
  75. $total = ceil($count/$size);
  76. $page = $page >= $total ? $total : $page;
  77. $list = Db::name("order_back")->where($where)->where($condition)->page($page,$size)->order("addtime desc")->select()->toArray();
  78. $data=[];
  79. foreach ($list as $value){
  80. $value['wsm_name']="";
  81. $value['wsm_supplier']='';
  82. $value['wsm_supplierNo']='';
  83. if($value['return_wsm']!=""){
  84. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  85. ->where(["a.wsm_code"=>$value['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  86. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  87. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  88. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  89. }
  90. $value['customer_name']='';
  91. if($value['customer_code']!=''){
  92. $customer = Db::name("customer_info")->where(['companyNo'=>$value['customer_code']])->find();
  93. $value['customer_name']=isset($customer['companyName'])?$customer['companyName']:'';
  94. }
  95. $inorder= Db::name("order_backinfo")->where(['thNo'=>$value['thNo'],"is_del"=>0])->select();
  96. $value['child']=empty($inorder)? [] : $inorder;
  97. //是否具有编辑权限
  98. $value['is_allow_update'] = 0;
  99. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  100. $data[]=$value;
  101. }
  102. return app_show(0,"获取成功",["list"=>$data ,"count"=>$count]);
  103. }
  104. /**
  105. * @return \think\response\Json|void
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function info(){
  111. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  112. if($thNo==""){
  113. return error_show(1004,"参数thNo不能为空");
  114. }
  115. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  116. if(empty($info)){
  117. return error_show(1004,"未找到数据");
  118. }
  119. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  120. $info['origin_price']=$orderinfo['origin_price'];
  121. $info['sale_price']=$orderinfo['sale_price'];
  122. $info['order_type']=$orderinfo['order_type'];
  123. $info['order_source']=$orderinfo['order_source'];
  124. $info['total_price']=$orderinfo['total_price'];
  125. $info['companyNo']=$orderinfo['supplierNo'];
  126. $info['company'] = Db::name('business')->where(['companyNo' => $orderinfo['supplierNo'], 'is_del' => 0])->value('company','');
  127. $info['addr_cn']=GetAddr($info['addr_code']);
  128. if($orderinfo['order_type']==3){
  129. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  130. $is_stock=0;
  131. }else {
  132. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  133. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  134. $is_stock=$goon['is_stock'];
  135. }
  136. if (empty($goon)) {
  137. return error_show(1003, "未找到商品数据");
  138. }
  139. $info['out_wsm_name']="";
  140. $info['out_wsm_supplier']='';
  141. $info['out_wsm_supplierNo']='';
  142. if($is_stock==1){
  143. $out =Db::name("order_out")->where(['outCode'=>$info['outCode']])->find();
  144. if($out==false){
  145. return error_show(1003, "未找到出库单数据");
  146. }
  147. if($out['wsm_code']!=''){
  148. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  149. ->where(["a.wsm_code"=>$out['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  150. $info['out_wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  151. $info['out_wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  152. $info['out_wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  153. }
  154. }
  155. $retutninfo = Db::name("order_return")->where(['returnCode'=>$info['returnCode']])->find();
  156. $info['apply_id'] = $retutninfo['apply_id'];
  157. $info['apply_name'] = $retutninfo['apply_name'];
  158. $info['return_tag'] = $retutninfo['return_tag'];
  159. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  160. $info['wsm_name']="";
  161. $info['wsm_supplier']='';
  162. $info['wsm_supplierNo']='';
  163. if($info['return_wsm']!=""){
  164. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  165. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  166. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  167. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  168. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  169. }
  170. $inorder= Db::name("order_backinfo")->alias("a")->leftJoin("result_info b","a.error_code=b.result_code")
  171. ->where(['a.thNo'=>$info['thNo'],"a.is_del"=>0])->field("a.*,b.result as error_msg")->select();
  172. $info['child']=empty($inorder)? [] : $inorder;
  173. $info['can']=$int;
  174. //补充仓库名称
  175. $warehouse = Db::name('warehouse_info')
  176. ->alias('wi')
  177. ->where(['wi.is_del' => 0, 'wi.status' => 1])
  178. ->whereIn('wi.wsm_code', [$info['can_sell_wsm'], $info['defective_wsm']])
  179. ->leftJoin('supplier s', 's.code=wi.supplierNo AND s.is_del=0')
  180. ->column('wi.name,wi.supplierNo,s.name supplier', 'wi.wsm_code');
  181. $info['can_sell_wsm_name'] = isset($warehouse[$info['can_sell_wsm']]['name']) ? $warehouse[$info['can_sell_wsm']]['name'] : '';
  182. $info['can_sell_wsm_supplierNo'] = isset($warehouse[$info['can_sell_wsm']]['supplierNo']) ? $warehouse[$info['can_sell_wsm']]['supplierNo'] : '';
  183. $info['can_sell_wsm_supplier'] = isset($warehouse[$info['can_sell_wsm']]['supplier']) ? $warehouse[$info['can_sell_wsm']]['supplier'] : '';
  184. $info['defective_wsm_name'] = isset($warehouse[$info['defective_wsm']]['name']) ? $warehouse[$info['defective_wsm']]['name'] : '';
  185. $info['defective_wsm_supplierNo'] = isset($warehouse[$info['defective_wsm']]['supplierNo']) ? $warehouse[$info['defective_wsm']]['supplierNo'] : '';
  186. $info['defective_wsm_supplier'] = isset($warehouse[$info['defective_wsm']]['supplier']) ? $warehouse[$info['defective_wsm']]['supplier'] : '';
  187. return app_show(0, "获取成功", $info);
  188. }
  189. /**
  190. * @return \think\response\Json|void
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\DbException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. */
  195. public function check(){
  196. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  197. if($thNo==""){
  198. return error_show(1004,"参数thNo不能为空");
  199. }
  200. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  201. if(empty($info)){
  202. return error_show(1004,"未找到数据");
  203. }
  204. $normal = isset($this->post['normal']) && $this->post['normal']!=="" ? intval($this->post['normal']):"";
  205. if($normal===""){
  206. return error_show(1004,"参数normal不能为空");
  207. }
  208. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  209. if($info['status']== 2){
  210. return error_show(1002,"退货单已验货");
  211. }
  212. $receive=$normal+array_sum(array_column($errorlist,'error_num'));
  213. $remark = isset($this->post['remark']) && $this->post['remark']!=="" ? trim($this->post['remark']):"";
  214. $info['received_num'] = $receive;
  215. $info['normal_num'] = $normal;
  216. $info['remark'] = $remark;
  217. $str = $info['status'];
  218. $info['status'] =empty($errorlist)?3: 2;
  219. $info['updatetime'] = date("Y-m-d H:i:s");
  220. Db::startTrans();
  221. try{
  222. $up=Db::name("order_back")->save($info);
  223. if($up){
  224. $stn = ["order_code"=>$info['thNo'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  225. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname],$stn,"CKTHD",$info['status'],$info);
  226. if($info['status'] == 2) $roleid = [10, 31, 41];//特殊处理,写入到10采购负责人、31库管人员、41库管-张凯旋这三个角色下的所有人
  227. else $roleid = [31,41];//特殊处理,写入到31库管人员、41库管-张凯旋这三个角色下的所有人
  228. $handle_user_list = Db::name('user_role')
  229. ->where('is_del', 0)
  230. ->whereIn('roleid', $roleid)
  231. ->column('uid');
  232. $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)];
  233. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  234. if(empty($errorlist)){
  235. Db::commit();
  236. return app_show(0,'更新成功');
  237. }
  238. foreach($errorlist as $value){
  239. $data=[];
  240. isset($value['id'])&&$value['id']!=''?$data['id']=$value['id']:"";
  241. $data['thNo']=$thNo;
  242. $data['error_num']=$value['error_num'];
  243. $data['error_code']=$value['error_code'];
  244. $data['error_remark']=$value['error_remark'];
  245. $data['is_del']=isset($value['is_del'])?$value['is_del']:0;
  246. isset($value['id'])&&$value['id']!=''? "" : $data['addtime']=date("Y-m-d H:i:s");
  247. $data['updatetime']=date("Y-m-d H:i:s");
  248. $in = Db::name("order_backinfo")->save($data);
  249. if(!$in){
  250. Db::rollback();
  251. return error_show(1005,'更新失败');
  252. }
  253. }
  254. Db::commit();
  255. return app_show(0,'更新成功');
  256. }
  257. Db::rollback();
  258. return error_show(1005,'更新失败');
  259. }catch (\Exception $e){
  260. Db::rollback();
  261. return error_show(1004,$e->getMessage());
  262. }
  263. }
  264. public function CheckExam(){
  265. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  266. if($thNo==""){
  267. return error_show(1004,"参数thNo不能为空");
  268. }
  269. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  270. if(empty($info)){
  271. return error_show(1004,"未找到数据");
  272. }
  273. $errorlist = isset($this->post['errorlist']) && $this->post['errorlist']!=="" ? $this->post['errorlist']:[];
  274. Db::startTrans();
  275. try{
  276. $temp=$info['status'];
  277. $info['status'] =3;
  278. $info['updatetime'] = date("Y-m-d H:i:s");
  279. $up=Db::name("order_back")->save($info);
  280. if($up){
  281. $stn = ["order_code"=>$thNo,"status"=>$temp,"action_remark"=>'',"action_type"=>"edit"];
  282. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $stn, "CKTHD", $info['status'], $stn);
  283. //特殊处理,写入到31库管人员、41库管-张凯旋这三个角色下的所有人
  284. $handle_user_list = Db::name('user_role')
  285. ->where('is_del', 0)
  286. ->whereIn('roleid', [31, 41])
  287. ->column('uid');
  288. $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)];
  289. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  290. if(empty($errorlist)){
  291. $up =Db::name("order_backinfo")->where(["thNo"=>$thNo,"is_del"=>0])->save(["status"=>1,
  292. "updatetime"=>date("Y-m-d H:i:s")]);
  293. if($up){
  294. Db::commit();
  295. return app_show(0,'更新成功');
  296. }else{
  297. Db::rollback();
  298. return error_show(1005,'异常记录数据更新失败');
  299. }
  300. }else{
  301. foreach($errorlist as $value){
  302. $temp=Db::name("order_backinfo")->where(['id'=>$value["id"],"is_del"=>0,"thNo"=>$thNo])->find();
  303. if(empty($temp)){
  304. Db::rollback();
  305. return error_show(1005,'异常记录数据未找到');
  306. }
  307. $temp['status']=$value['status'];
  308. $temp['exam_remark']=$value['remark'];
  309. $temp['updatetime']=date("Y-m-d H:i:s");
  310. $com = Db::name("order_backinfo")->save($temp);
  311. if($com==false){
  312. Db::rollback();
  313. return error_show(1005,'异常记录数据更新失败');
  314. }
  315. }
  316. Db::commit();
  317. return app_show(0,'更新成功');
  318. }
  319. }
  320. Db::rollback();
  321. return error_show(1005,'更新失败');
  322. }catch (\Exception $e){
  323. Db::rollback();
  324. return error_show(1004,$e->getMessage());
  325. }
  326. }
  327. public function Exam(){
  328. $thNo = isset($this->post['thNo']) && $this->post['thNo']!="" ? trim($this->post['thNo']):"";
  329. if($thNo==""){
  330. return error_show(1004,"参数thNo不能为空");
  331. }
  332. $info =Db::name("order_back")->where(['thNo'=>$thNo])->find();
  333. if(empty($info)){
  334. return error_show(1004,"未找到数据");
  335. }
  336. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  337. if($status===""){
  338. return error_show(1004,"参数status不能为空");
  339. }
  340. $return_wsm=isset($this->post["return_wsm"])&&$this->post["return_wsm"]!=""?trim($this->post["return_wsm"]):"";
  341. $normal_num=isset($this->post["normal_num"])&&$this->post["normal_num"]!=""?intval($this->post["normal_num"])
  342. :"";
  343. // $defective_wsm=isset($this->post["defective_wsm"])&&$this->post["defective_wsm"]!=""?trim($this->post["defective_wsm"]):"";
  344. // $defective_num=isset($this->post["defective_num"])&&$this->post["defective_num"]!==""?intval($this->post["defective_num"]):"";
  345. // $loss_num=isset($this->post["loss_num"])&&$this->post["loss_num"]!=""?intval($this->post["loss_num"]):"";
  346. if($status==4){
  347. // if($defective_num===""){
  348. // return error_show(1004,"参数defective_num不能为空");
  349. // }
  350. // if($loss_num===""){
  351. // return error_show(1004,"参数loss_num不能为空");
  352. // }
  353. if($normal_num===""){
  354. return error_show(1004,"参数normal_num不能为空");
  355. }
  356. // if($defective_wsm===""){
  357. // return error_show(1004,"参数defective_wsm不能为空");
  358. // }
  359. if($return_wsm===""){
  360. return error_show(1004,"参数return_wsm不能为空");
  361. }
  362. $info['return_wsm']=$return_wsm;
  363. $info['normal_num']=$normal_num;
  364. // $info['loss_num']=$loss_num;//order_back表里没有这三个字段,不知道是干嘛的
  365. // $info['defective_wsm']=$defective_wsm;
  366. // $info['defective_num']=$defective_num;
  367. }
  368. $remark= isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  369. Db::startTrans();
  370. try{
  371. $var = $info['status'];
  372. $info['status'] =$status;
  373. $info['remark'] =$remark;
  374. $info['updatetime'] = date("Y-m-d H:i:s");
  375. $up=Db::name("order_back")->save($info);
  376. if($up){
  377. $stn = ["order_code"=>$thNo,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  378. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$stn,"CKTHD",$info['status'],$this->post);
  379. if (in_array($status, [2, 3])) {
  380. if ($status == 2) $roleid = [10, 31, 41];//特殊处理,写入到10采购负责人、31库管人员、41库管-张凯旋这三个角色下的所有人
  381. else $roleid = [31, 41];//特殊处理,写入到、31库管人员、41库管-张凯旋这三个角色下的所有人
  382. $handle_user_list = Db::name('user_role')
  383. ->where('is_del', 0)
  384. ->whereIn('roleid', $roleid)
  385. ->column('uid');
  386. $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)];
  387. } else $process = ["order_code" => $info['thNo'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "CKTHD", "before_status" => $var];
  388. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  389. Db::commit();
  390. return app_show(0,'更新成功');
  391. }
  392. Db::rollback();
  393. return error_show(1005,'更新失败');
  394. }catch (\Exception $e){
  395. Db::rollback();
  396. return error_show(1004,$e->getMessage());
  397. }
  398. }
  399. //新写一个退货接口
  400. public function ExamNew()
  401. {
  402. $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');
  403. $val = Validate::rule(['thNo|退货编码' => 'require', 'status|状态' => 'require|number|in:4,2,1', 'status_remark|备注' => 'checkRemark:']);
  404. $val->extend('checkRemark', function ($val, $rule, $data) {
  405. return $data['status'] == 4 ? true : (empty($val) ? '请填写备注' : true);
  406. });
  407. if (!$val->check($param)) return error_show(1004, $val->getError());
  408. Db::startTrans();
  409. try {
  410. $info = Db::name("order_back")
  411. ->field('id,order_type,status,outCode,good_code,return_num,orderCode,returnCode,apply_id,apply_name')
  412. ->where(['thNo' => $param['thNo'], 'is_del' => 0])
  413. ->find();
  414. if (empty($info)) throw new Exception('未找到数据');
  415. $sale= Db::name('sale')->where(['orderCode'=>$info['orderCode'],"is_del"=>0])->find();
  416. if($sale==false){
  417. Db::rollback();
  418. return error_show(1004,"未找到销售单数据");
  419. }
  420. $spuCode= $info['good_code'];
  421. if($sale['order_type']!=1){
  422. if($sale['order_type']==3){
  423. $goon = Db::name("good_zixun")->where(["spuCode"=>$sale['good_code'],"is_del"=>0])->find();
  424. $isZx=1;
  425. }else {
  426. $goon = Db::name('good_basic')->where(['spuCode' => $sale['good_code']])->find();
  427. $isZx=2;
  428. }
  429. $spuCode = $this->CheckGoodZx($goon,$isZx,$param['thNo']);
  430. }
  431. $out =Db::name("order_out")->where(['outCode'=>$info['outCode']])->find();
  432. if($out==false){
  433. Db::rollback();
  434. return error_show(1004,"未找到发货单数据");
  435. }
  436. $returninfo =Db::name("order_return")->where(['returnCode'=>$info['returnCode']])->find();
  437. if($returninfo==false){
  438. Db::rollback();
  439. return error_show(1004,"未找到售后单数据");
  440. }
  441. $sabebn =Db::name("sale_info")->where([["orderCode",'=',$info['orderCode']],["num",">",0]])->select()->toArray();
  442. if($param['can_sell_num']!=0&& $param['can_sell_wsm']!=''){
  443. // if($sale['is_stock']==1){ //库存品退回原仓库 备库单释放数量 采购单
  444. // $param['can_sell_wsm'] = $out['wsm_code'];
  445. // }
  446. $stock = Db::name("good_stock")->where(["spuCode"=>$spuCode,'wsm_code'=>$param['can_sell_wsm']])
  447. ->find();
  448. if(empty($stock)) {
  449. $stock=[
  450. "spuCode"=>$spuCode,
  451. "wsm_code"=>$param['can_sell_wsm'],
  452. "usable_stock"=>0,
  453. "wait_out_stock"=>0,
  454. "wait_in_stock"=>0,
  455. "total_stock"=>0,
  456. "addtime"=>date("Y-m-d H:i:s"),
  457. "updatetime"=>date("Y-m-d H:i:s"),
  458. ];
  459. }
  460. $stock['usable_stock']+=$param['can_sell_num'];
  461. $stock['updatetime']=date("Y-m-d H:i:s");
  462. $st_up = Db::name("good_stock")->save($stock);
  463. if($st_up==false){
  464. return error_show(1005,'可售商品入库失败');
  465. }
  466. $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  467. if(!empty($sabebn)){
  468. $total_num =$param['can_sell_num'];
  469. foreach ($sabebn as $ve){
  470. $tempnum=0;
  471. if ($total_num==0) break;
  472. if($total_num>=$ve['num']){
  473. $tempnum = $ve['num'];
  474. $total_num-=$ve['num'];
  475. $ve['th_num']+=$ve['num'];
  476. $ve['num']=0;
  477. }else{
  478. $tempnum = $total_num;
  479. $ve['num']-=$total_num;
  480. $ve['th_num']+=$total_num;
  481. $total_num=0;
  482. }
  483. $bnin=GoodStockInfo::ReturnAdd($stockid,$ve['bnCode'],$tempnum,$ve['stockid']);
  484. if($bnin==false){
  485. Db::rollback();
  486. return error_show(1005, '可售商品Bn库存数入库失败');
  487. }
  488. $up=Db::name("sale_info")->save($ve);
  489. if($up==false){
  490. Db::rollback();
  491. return error_show(1005, '可售商品Bn库存数入库失败');
  492. }
  493. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  494. if($bnin==false){
  495. Db::rollback();
  496. return error_show(1005, '可售商品Bn库存数入库失败');
  497. }
  498. }
  499. }else{
  500. if($sale['order_type']==1){
  501. $ordernum = Db::name("order_num")->where(['orderCode' =>$info['orderCode']])->find();
  502. if ($ordernum == false) {
  503. Db::rollback();
  504. return error_show(1005, '未找到关联采购单');
  505. }
  506. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  507. if ($cgd == false) {
  508. Db::rollback();
  509. return error_show(1005, '未找到采购单数据');
  510. }
  511. $bn =makeNo("BN");
  512. $yp=GoodStockInfo::AddBn($stockid,$bn,$param['can_sell_num'],$cgd['good_price']);
  513. if($yp==false){
  514. Db::rollback();
  515. return error_show(1005, '商品批次退货入库失败');
  516. }
  517. }
  518. }
  519. $good_data=['good_log_code'=>$param['thNo'],"stock_id"=> $stockid,"type"=>1,'stock'=>$param['can_sell_num'],"stock_name"=>"usable_stock"];
  520. GoodLog::LogAdd($this->post['token'],$good_data,'RKTHD');
  521. }
  522. if($param['defective_num']!=0&& $param['defective_wsm']!=''){
  523. $defective = Db::name("good_stock")->where(["spuCode"=>$spuCode,'wsm_code'=>$param['defective_wsm']])->find();
  524. if(empty($defective)) {
  525. $defective=[
  526. "spuCode"=>$spuCode,
  527. "wsm_code"=>$param['defective_wsm'],
  528. "usable_stock"=>0,
  529. "wait_out_stock"=>0,
  530. "wait_in_stock"=>0,
  531. "total_stock"=>0,
  532. "addtime"=>date("Y-m-d H:i:s"),
  533. "updatetime"=>date("Y-m-d H:i:s"),
  534. ];
  535. $order = ["order_code"=>$spuCode,"status"=>1,"action_remark"=>'', "action_type"=>"create"];
  536. }else{
  537. $order = ["order_code"=>$spuCode,"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  538. }
  539. $defective['usable_stock']+=$param['defective_num'];
  540. $defective['updatetime'] = date("Y-m-d H:i:s");
  541. $upd= Db::name("good_stock")->save($defective);
  542. if($upd==false){
  543. Db::rollback();
  544. return error_show(1005,'次品商品入库失败');
  545. }
  546. $stockid = isset($defective['id']) ? $defective['id'] : Db::name("good_stock")->getLastInsID();
  547. $sabebn =Db::name("sale_info")->where(["orderCode"=>$info['orderCode']])->select()->toArray();
  548. if(!empty($sabebn)){
  549. $total_num =$param['defective_num'];
  550. foreach ($sabebn as $ve){
  551. $tempnum=0;
  552. if ($total_num==0) break;
  553. if($total_num>=$ve['num']){
  554. $tempnum = $ve['num'];
  555. $total_num-=$ve['num'];
  556. $ve['th_num']+=$ve['num'];
  557. $ve['num']=0;
  558. }else{
  559. $tempnum = $total_num;
  560. $ve['num']-=$total_num;
  561. $ve['th_num']+=$total_num;
  562. $total_num=0;
  563. }
  564. $bnin=GoodStockInfo::ReturnAdd($stockid,$ve['bnCode'],$tempnum,$ve['stockid']);
  565. if($bnin==false){
  566. Db::rollback();
  567. return error_show(1005, '可售商品Bn库存数入库失败');
  568. }
  569. $up=Db::name("sale_info")->save($ve);
  570. if($up==false){
  571. Db::rollback();
  572. return error_show(1005, '可售商品Bn库存数入库失败');
  573. }
  574. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  575. if($bnin==false){
  576. Db::rollback();
  577. return error_show(1005, '可售商品Bn库存数入库失败');
  578. }
  579. }
  580. }else{
  581. if($sale['order_type']==1){
  582. $ordernum = Db::name("order_num")->where(['orderCode' =>$info['orderCode']])->find();
  583. if ($ordernum == 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. $bn =makeNo("BN");
  593. $yp=GoodStockInfo::AddBn($stockid,$bn,$param['defective_num'],$cgd['good_price']);
  594. if($yp==false){
  595. Db::rollback();
  596. return error_show(1005, '商品批次退货入库失败');
  597. }
  598. }
  599. }
  600. $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"];
  601. GoodLog::LogAdd($this->post['token'],$good_data,'RKTHD');
  602. }
  603. $update_data = ['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')];
  604. //status==4通过,其他值表示驳回
  605. if ($param['status'] != 4) $update_data['status_remark'] = $param['status_remark'];
  606. else {
  607. $update_data['can_sell_wsm'] = $param['can_sell_wsm'];
  608. $update_data['can_sell_num'] = $param['can_sell_num'];
  609. $update_data['defective_wsm'] = $param['defective_wsm'];
  610. $update_data['defective_num'] = $param['defective_num'];
  611. $update_data['loss_num'] = $param['loss_num'];
  612. if($sale['is_stock']==1){
  613. $sale['th_num']+= $info['return_num'];
  614. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  615. $sale['status']=3;
  616. }
  617. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  618. $sale['updatetime']= date("Y-m-d H:i:s");
  619. $uap=Db::name("sale")->save($sale);
  620. if($uap==false){
  621. Db::rollback();
  622. return error_show(1005,'销售单订单更新失败');
  623. }
  624. // $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  625. // if($ordernum==false){
  626. // Db::rollback();
  627. // return error_show(1005,'未找到关联采购单');
  628. // }
  629. // $ordernum['send_num']-=$info['return_num'];
  630. // $orderup =Db::name("order_num")->save($ordernum);
  631. // if($orderup==false){
  632. // Db::rollback();
  633. // return error_show(1005,'关联数据更新失败');
  634. // }
  635. // $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  636. // if($cgd==false){
  637. // Db::rollback();
  638. // return error_show(1005,'未找到采购单数据');
  639. // }
  640. // $cgd['th_fee']+= round($cgd['good_price']*$param['can_sell_num'],2);
  641. // $cgd['th_num']+= $param['can_sell_num'];
  642. // if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  643. // $cgd['status']=4;
  644. // }
  645. // $cgd['updatetime']= date("Y-m-d H:i:s");
  646. // $cgdup =Db::name("purchease_order")->save($cgd);
  647. // if($cgdup==false){
  648. // Db::rollback();
  649. // return error_show(1005,'采购单数据更新失败');
  650. // }
  651. // if($cgd['bkcode']!=""){
  652. // $bk=Db::name("purchease_order")->where(["bkcode"=>$cgd['bkcode'],"order_type"=>1,"order_source"=>0,"is_del"=>0])->find();
  653. // if($bk==false){
  654. // Db::rollback();
  655. // return error_show(1005,'未找到备库单数据');
  656. // }
  657. // $orderbk = Db::name("order_bk")->where(['cgdNo'=>$bk['cgdNo'],"is_del"=>0])->find();
  658. // if($orderbk==false){
  659. // Db::rollback();
  660. // return error_show(1005,'备库单未完全入库');
  661. // }
  662. // $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();
  663. //
  664. // $orderbk['balance_num']=$orderbk['total_num']-$merge_num['num'];
  665. // $orderbk['merge_num']=$merge_num['num'];
  666. // $orderbk['updatetime']=date("Y-m-d H:i:s");
  667. // $orderbkup=Db::name("order_bk")->save($orderbk);
  668. // if($orderbkup==false){
  669. // Db::rollback();
  670. // return error_show(1005,'备库单库存数据释放失败');
  671. // }
  672. // }
  673. }else{
  674. if($returninfo['return_tag']==1){
  675. $sale['th_num']+= $info['return_num'];
  676. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  677. $sale['status']=3;
  678. }
  679. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  680. $sale['updatetime']= date("Y-m-d H:i:s");
  681. $uap=Db::name("sale")->save($sale);
  682. if($uap==false){
  683. Db::rollback();
  684. return error_show(1005,'销售单订单更新失败');
  685. }
  686. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  687. if($ordernum==false){
  688. Db::rollback();
  689. return error_show(1005,'未找到关联采购单');
  690. }
  691. $ordernum['send_num']-=$info['return_num'];
  692. $orderup =Db::name("order_num")->save($ordernum);
  693. if($orderup==false){
  694. Db::rollback();
  695. return error_show(1005,'关联数据更新失败');
  696. }
  697. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  698. if($cgd==false){
  699. Db::rollback();
  700. return error_show(1005,'未找到采购单数据');
  701. }
  702. $cgd['th_num']+= $info['return_num'];
  703. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  704. $cgd['status']=4;
  705. }
  706. $cgd['th_fee']+= round($info['return_num']*$cgd['good_price'],2);
  707. $cgd['updatetime']= date("Y-m-d H:i:s");
  708. $cgdup =Db::name("purchease_order")->save($cgd);
  709. if($cgdup==false){
  710. Db::rollback();
  711. return error_show(1005,'采购单数据更新失败');
  712. }
  713. }else{
  714. $sale['th_num']+= $info['return_num'];
  715. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  716. $sale['status']=3;
  717. }
  718. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  719. $sale['updatetime']= date("Y-m-d H:i:s");
  720. $uap=Db::name("sale")->save($sale);
  721. if($uap==false){
  722. Db::rollback();
  723. return error_show(1005,'销售单订单更新失败');
  724. }
  725. // $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  726. // if($ordernum==false){
  727. // Db::rollback();
  728. // return error_show(1005,'未找到关联采购单');
  729. // }
  730. // $ordernum['send_num']-=$info['return_num'];
  731. // $orderup =Db::name("order_num")->save($ordernum);
  732. // if($orderup==false){
  733. // Db::rollback();
  734. // return error_show(1005,'关联数据更新失败');
  735. // }
  736. // $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  737. // if($cgd==false){
  738. // Db::rollback();
  739. // return error_show(1005,'未找到采购单数据');
  740. // }
  741. // $cgd['th_fee']+= round($cgd['good_price']*$param['can_sell_num'],2);
  742. // $cgd['th_num']+= $param['can_sell_num'];
  743. // if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  744. // $cgd['status']=4;
  745. // }
  746. // $cgd['updatetime']= date("Y-m-d H:i:s");
  747. // $cgdup =Db::name("purchease_order")->save($cgd);
  748. // if($cgdup==false){
  749. // Db::rollback();
  750. // return error_show(1005,'采购单数据更新失败');
  751. // }
  752. }
  753. }
  754. }
  755. $res = Db::name("order_back")
  756. ->where(['id' => $info['id']])
  757. ->update($update_data);
  758. if ($res) {
  759. $data=[
  760. "orderCode"=>$info['orderCode'],
  761. "th_type"=>3,
  762. "th_num"=>$info['return_num'],
  763. "th_fee"=>round($info['return_num']*$sale['sale_price'],2),
  764. "thCode"=>$info['returnCode'],
  765. "apply_id"=>$info['apply_id'],
  766. "apply_name"=>$info['apply_name'],
  767. "spuCode"=>$sale['good_code'],
  768. "good_name"=>$sale['good_name'],
  769. "cat_id"=>$sale['cat_id'],
  770. "addtime"=>date("Y-m-d H:i:s"),
  771. "status"=>1,
  772. "is_del"=>0
  773. ];
  774. $inse=Db::name("th_data")->insert($data);
  775. if($inse==false){
  776. Db::rollback();
  777. return error_show(1004,"退货单更新失败");
  778. }
  779. if($info['return_num']>=$out['send_num']){
  780. $out['status']=4;
  781. $out['updatetime']=date("Y-m-d H:i:s");
  782. $upout =Db::name("order_out")->save($out);
  783. if($upout==false){
  784. Db::rollback();
  785. return error_show(1005,'出库单数据更新失败');
  786. }
  787. }
  788. $stn = ["order_code" => $param['thNo'], "status" => $info['status'], "action_remark" => '', "action_type" => "edit"];
  789. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stn, "CKTHD", $param['status'], $stn);
  790. if (in_array($param['status'], [1,2, 3])) {
  791. if ($param['status'] == 2) $roleid = [10, 31, 41];//特殊处理,写入到10采购负责人、31库管人员、41库管-张凯旋这三个角色下的所有人
  792. else $roleid = [31, 41];//特殊处理,写入到、31库管人员、41库管-张凯旋这三个角色下的所有人
  793. $handle_user_list = Db::name('user_role')
  794. ->where('is_del', 0)
  795. ->whereIn('roleid', $roleid)
  796. ->column('uid');
  797. //如果是退货商品验收(节点1),把供应商负责人也加进去
  798. if ($param['status'] == 1) $handle_user_list[] = $sale['cgderid'];
  799. $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)];
  800. } else $process = ["order_code" => $param['thNo'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "CKTHD", "before_status" => $info['status']];
  801. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  802. Db::commit();
  803. return app_show(0, '操作成功');
  804. } else throw new Exception('操作失败');
  805. } catch (Exception $exception) {
  806. Db::rollback();
  807. return error_show(1005, $exception->getMessage());
  808. }
  809. }
  810. }