Orderback.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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=['good_log_code'=>$param['thNo'],"stock_id"=> $stockid,"type"=>1,'stock'=>$param['can_sell_num'],"stock_name"=>"usable_stock"];
  530. GoodLog::LogAdd($this->post['token'],$good_data,'RKTHD');
  531. }
  532. if($param['defective_num']!=0&& $param['defective_wsm']!=''){
  533. $defective = Db::name("good_stock")->where(["spuCode"=>$spuCode,'wsm_code'=>$param['defective_wsm']])->find();
  534. if(empty($defective)) {
  535. $defective=[
  536. "spuCode"=>$spuCode,
  537. "wsm_code"=>$param['defective_wsm'],
  538. "usable_stock"=>0,
  539. "wait_out_stock"=>0,
  540. "wait_in_stock"=>0,
  541. "total_stock"=>0,
  542. "addtime"=>date("Y-m-d H:i:s"),
  543. "updatetime"=>date("Y-m-d H:i:s"),
  544. ];
  545. $order = ["order_code"=>$spuCode,"status"=>1,"action_remark"=>'', "action_type"=>"create"];
  546. }else{
  547. $order = ["order_code"=>$spuCode,"status"=>1,"action_remark"=>'', "action_type"=>"edit"];
  548. }
  549. $defective['usable_stock']+=$param['defective_num'];
  550. $defective['updatetime'] = date("Y-m-d H:i:s");
  551. $upd= Db::name("good_stock")->save($defective);
  552. if($upd==false){
  553. Db::rollback();
  554. return error_show(1005,'次品商品入库失败');
  555. }
  556. $stockid = isset($defective['id']) ? $defective['id'] : Db::name("good_stock")->getLastInsID();
  557. $sabebn =Db::name("sale_info")->where(["orderCode"=>$info['orderCode']])->select()->toArray();
  558. if(!empty($sabebn)){
  559. $total_num =$param['defective_num'];
  560. foreach ($sabebn as $ve){
  561. $tempnum=0;
  562. if ($total_num==0) break;
  563. if($total_num>=$ve['num']){
  564. $tempnum = $ve['num'];
  565. $total_num-=$ve['num'];
  566. $ve['th_num']+=$ve['num'];
  567. $ve['num']=0;
  568. }else{
  569. $tempnum = $total_num;
  570. $ve['num']-=$total_num;
  571. $ve['th_num']+=$total_num;
  572. $total_num=0;
  573. }
  574. $bnin=GoodStockInfo::ReturnAdd($stockid,$ve['bnCode'],$tempnum,$ve['stockid']);
  575. if($bnin==false){
  576. Db::rollback();
  577. return error_show(1005, '可售商品Bn库存数入库失败');
  578. }
  579. $up=Db::name("sale_info")->save($ve);
  580. if($up==false){
  581. Db::rollback();
  582. return error_show(1005, '可售商品Bn库存数入库失败');
  583. }
  584. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  585. if($bnin==false){
  586. Db::rollback();
  587. return error_show(1005, '可售商品Bn库存数入库失败');
  588. }
  589. }
  590. }else{
  591. if($sale['order_type']==1){
  592. $ordernum = Db::name("order_num")->where(['orderCode' =>$info['orderCode']])->find();
  593. if ($ordernum == false) {
  594. Db::rollback();
  595. return error_show(1005, '未找到关联采购单');
  596. }
  597. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  598. if ($cgd == false) {
  599. Db::rollback();
  600. return error_show(1005, '未找到采购单数据');
  601. }
  602. $bn =makeNo("BN");
  603. $yp=GoodStockInfo::AddBn($stockid,$bn,$param['defective_num'],$cgd['good_price']);
  604. if($yp==false){
  605. Db::rollback();
  606. return error_show(1005, '商品批次退货入库失败');
  607. }
  608. }
  609. }
  610. $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"];
  611. GoodLog::LogAdd($this->post['token'],$good_data,'RKTHD');
  612. }
  613. $update_data = ['status' => $param['status'], 'updatetime' => date('Y-m-d H:i:s')];
  614. //status==4通过,其他值表示驳回
  615. if ($param['status'] != 4) $update_data['status_remark'] = $param['status_remark'];
  616. else {
  617. $update_data['can_sell_wsm'] = $param['can_sell_wsm'];
  618. $update_data['can_sell_num'] = $param['can_sell_num'];
  619. $update_data['defective_wsm'] = $param['defective_wsm'];
  620. $update_data['defective_num'] = $param['defective_num'];
  621. $update_data['loss_num'] = $param['loss_num'];
  622. if($sale['is_stock']==1){
  623. $sale['th_num']+= $info['return_num'];
  624. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  625. $sale['status']=3;
  626. }
  627. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  628. $sale['updatetime']= date("Y-m-d H:i:s");
  629. $uap=Db::name("sale")->save($sale);
  630. if($uap==false){
  631. Db::rollback();
  632. return error_show(1005,'销售单订单更新失败');
  633. }
  634. // $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  635. // if($ordernum==false){
  636. // Db::rollback();
  637. // return error_show(1005,'未找到关联采购单');
  638. // }
  639. // $ordernum['send_num']-=$info['return_num'];
  640. // $orderup =Db::name("order_num")->save($ordernum);
  641. // if($orderup==false){
  642. // Db::rollback();
  643. // return error_show(1005,'关联数据更新失败');
  644. // }
  645. // $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  646. // if($cgd==false){
  647. // Db::rollback();
  648. // return error_show(1005,'未找到采购单数据');
  649. // }
  650. // $cgd['th_fee']+= round($cgd['good_price']*$param['can_sell_num'],2);
  651. // $cgd['th_num']+= $param['can_sell_num'];
  652. // if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  653. // $cgd['status']=4;
  654. // }
  655. // $cgd['updatetime']= date("Y-m-d H:i:s");
  656. // $cgdup =Db::name("purchease_order")->save($cgd);
  657. // if($cgdup==false){
  658. // Db::rollback();
  659. // return error_show(1005,'采购单数据更新失败');
  660. // }
  661. // if($cgd['bkcode']!=""){
  662. // $bk=Db::name("purchease_order")->where(["bkcode"=>$cgd['bkcode'],"order_type"=>1,"order_source"=>0,"is_del"=>0])->find();
  663. // if($bk==false){
  664. // Db::rollback();
  665. // return error_show(1005,'未找到备库单数据');
  666. // }
  667. // $orderbk = Db::name("order_bk")->where(['cgdNo'=>$bk['cgdNo'],"is_del"=>0])->find();
  668. // if($orderbk==false){
  669. // Db::rollback();
  670. // return error_show(1005,'备库单未完全入库');
  671. // }
  672. // $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();
  673. //
  674. // $orderbk['balance_num']=$orderbk['total_num']-$merge_num['num'];
  675. // $orderbk['merge_num']=$merge_num['num'];
  676. // $orderbk['updatetime']=date("Y-m-d H:i:s");
  677. // $orderbkup=Db::name("order_bk")->save($orderbk);
  678. // if($orderbkup==false){
  679. // Db::rollback();
  680. // return error_show(1005,'备库单库存数据释放失败');
  681. // }
  682. // }
  683. }else{
  684. if($returninfo['return_tag']==1){
  685. $sale['th_num']+= $info['return_num'];
  686. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  687. $sale['status']=3;
  688. }
  689. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  690. $sale['updatetime']= date("Y-m-d H:i:s");
  691. $uap=Db::name("sale")->save($sale);
  692. if($uap==false){
  693. Db::rollback();
  694. return error_show(1005,'销售单订单更新失败');
  695. }
  696. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  697. if($ordernum==false){
  698. Db::rollback();
  699. return error_show(1005,'未找到关联采购单');
  700. }
  701. $ordernum['send_num']-=$info['return_num'];
  702. $orderup =Db::name("order_num")->save($ordernum);
  703. if($orderup==false){
  704. Db::rollback();
  705. return error_show(1005,'关联数据更新失败');
  706. }
  707. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  708. if($cgd==false){
  709. Db::rollback();
  710. return error_show(1005,'未找到采购单数据');
  711. }
  712. $cgd['th_num']+= $info['return_num'];
  713. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  714. $cgd['status']=4;
  715. }
  716. $cgd['th_fee']+= round($info['return_num']*$cgd['good_price'],2);
  717. $cgd['updatetime']= date("Y-m-d H:i:s");
  718. $cgdup =Db::name("purchease_order")->save($cgd);
  719. if($cgdup==false){
  720. Db::rollback();
  721. return error_show(1005,'采购单数据更新失败');
  722. }
  723. }else{
  724. $sale['th_num']+= $info['return_num'];
  725. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  726. $sale['status']=3;
  727. }
  728. $sale['th_fee']+= round($info['return_num']*$sale['sale_price'],2);
  729. $sale['updatetime']= date("Y-m-d H:i:s");
  730. $uap=Db::name("sale")->save($sale);
  731. if($uap==false){
  732. Db::rollback();
  733. return error_show(1005,'销售单订单更新失败');
  734. }
  735. // $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  736. // if($ordernum==false){
  737. // Db::rollback();
  738. // return error_show(1005,'未找到关联采购单');
  739. // }
  740. // $ordernum['send_num']-=$info['return_num'];
  741. // $orderup =Db::name("order_num")->save($ordernum);
  742. // if($orderup==false){
  743. // Db::rollback();
  744. // return error_show(1005,'关联数据更新失败');
  745. // }
  746. // $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  747. // if($cgd==false){
  748. // Db::rollback();
  749. // return error_show(1005,'未找到采购单数据');
  750. // }
  751. // $cgd['th_fee']+= round($cgd['good_price']*$param['can_sell_num'],2);
  752. // $cgd['th_num']+= $param['can_sell_num'];
  753. // if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  754. // $cgd['status']=4;
  755. // }
  756. // $cgd['updatetime']= date("Y-m-d H:i:s");
  757. // $cgdup =Db::name("purchease_order")->save($cgd);
  758. // if($cgdup==false){
  759. // Db::rollback();
  760. // return error_show(1005,'采购单数据更新失败');
  761. // }
  762. }
  763. }
  764. }
  765. $res = Db::name("order_back")
  766. ->where(['id' => $info['id']])
  767. ->update($update_data);
  768. if ($res) {
  769. $data=[
  770. "orderCode"=>$info['orderCode'],
  771. "th_type"=>3,
  772. "th_num"=>$info['return_num'],
  773. "th_fee"=>round($info['return_num']*$sale['sale_price'],2),
  774. "thCode"=>$info['returnCode'],
  775. "apply_id"=>$info['apply_id'],
  776. "apply_name"=>$info['apply_name'],
  777. "spuCode"=>$sale['good_code'],
  778. "good_name"=>$sale['good_name'],
  779. "cat_id"=>$sale['cat_id'],
  780. "addtime"=>date("Y-m-d H:i:s"),
  781. "status"=>1,
  782. "is_del"=>0
  783. ];
  784. $inse=Db::name("th_data")->insert($data);
  785. if($inse==false){
  786. Db::rollback();
  787. return error_show(1004,"退货单更新失败");
  788. }
  789. if($info['return_num']>=$out['send_num']){
  790. $out['status']=4;
  791. $out['updatetime']=date("Y-m-d H:i:s");
  792. $upout =Db::name("order_out")->save($out);
  793. if($upout==false){
  794. Db::rollback();
  795. return error_show(1005,'出库单数据更新失败');
  796. }
  797. }
  798. $stn = ["order_code" => $param['thNo'], "status" => $info['status'], "action_remark" => '', "action_type" => "edit"];
  799. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stn, "CKTHD", $param['status'], $stn);
  800. if (in_array($param['status'], [1,2, 3])) {
  801. if ($param['status'] == 2) $roleid = [10, 31, 41];//特殊处理,写入到10采购负责人、31库管人员、41库管-张凯旋这三个角色下的所有人
  802. else $roleid = [31, 41];//特殊处理,写入到、31库管人员、41库管-张凯旋这三个角色下的所有人
  803. $handle_user_list = Db::name('user_role')
  804. ->where('is_del', 0)
  805. ->whereIn('roleid', $roleid)
  806. ->column('uid');
  807. //如果是退货商品验收(节点1),把供应商负责人也加进去
  808. if ($param['status'] == 1) $handle_user_list[] = $sale['cgderid'];
  809. $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)];
  810. } else $process = ["order_code" => $param['thNo'], "order_id" => $info['id'], "order_status" => $param['status'], "order_type" => "CKTHD", "before_status" => $info['status']];
  811. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  812. Db::commit();
  813. return app_show(0, '操作成功');
  814. } else throw new Exception('操作失败');
  815. } catch (Exception $exception) {
  816. Db::rollback();
  817. return error_show(1005, $exception->getMessage());
  818. }
  819. }
  820. }