Orderback.php 44 KB

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