Orderback.php 46 KB

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