Orderback.php 47 KB

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