Orderback.php 45 KB

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