Orderback.php 45 KB

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