After.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\App;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //售后
  10. class After extends Base
  11. {
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. }
  16. public function create(){
  17. $outCode = isset($this->post['outCode']) &&$this->post['outCode']!=''?trim($this->post['outCode']) :"";
  18. if($outCode==''){
  19. return error_show(1004,"参数outCode 不能为空");
  20. }
  21. $orderout= Db::name("order_out")->where(["outCode"=>$outCode])->find();
  22. if($orderout==false){
  23. return error_show(1005,"未找到订单数据");
  24. }
  25. $after_num =Db::name("order_return")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode])
  26. ->where("status","not in",[5,8])->sum("error_num");
  27. $th_num =Db::name("order_back")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode,"is_del"=>0])
  28. ->where("status","<>",4)->sum("return_num");
  29. if($th_num!=0 || $after_num!=0){
  30. return error_show(1005,"存在售后或售后退货未结束流程");
  31. }
  32. $wsm =Db::name("warehouse_info")->where(["wsm_code"=>$orderout['wsm_code']])->find();
  33. if($wsm==false){
  34. return error_show(1005,"未找到仓库数据");
  35. }
  36. $oder= Db::name("sale")->where(["orderCode"=>$orderout['orderCode'],"is_del"=>0])->find();
  37. if(empty($oder)){
  38. return error_show(1005,"未找到订单数据");
  39. }
  40. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  41. if($outCode==''){
  42. return error_show(1004,"参数errorCode 不能为空");
  43. }
  44. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  45. if(empty($error)){
  46. return error_show(1005,"未找到退货原因数据");
  47. }
  48. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  49. $is_receive =isset($this->post['is_receive']) &&$this->post['is_receive']!==''?intval($this->post['is_receive'])
  50. :"";
  51. if($is_receive===""){
  52. return error_show(1004,"参数 is_receive 不能为空");
  53. }
  54. if($is_receive==0){
  55. $num=$orderout['send_num'];
  56. }else{
  57. $num =isset($this->post['num']) &&$this->post['num']!=''?intval($this->post['num']) :"";
  58. if($num==""){
  59. return error_show(1004,"参数 num 不能为空或零");
  60. }
  61. }
  62. $thnum =Db::name("order_back")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode,"is_del"=>0])
  63. ->where("status","=",4)->sum("return_num");
  64. if($num>$orderout['send_num']-$thnum){
  65. return error_show(1004,"可用售后数量不足");
  66. }
  67. $except_code =isset($this->post['except_code']) &&$this->post['except_code']!=''?trim($this->post['except_code']) :"";
  68. $img = isset($this->post['img']) &&$this->post['img']!=''?trim($this->post['img']) :"";
  69. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  70. if($token==''){
  71. return error_show(105,"参数token不能为空");
  72. }
  73. $user =GetUserInfo($token);
  74. if(empty($user)||$user['code']!=0){
  75. return error_show(102,"申请人数据不存在");
  76. }
  77. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  78. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  79. $returnCode=makeNo("RS");
  80. Db::startTrans();
  81. try{
  82. $in = [
  83. "returnCode"=>$returnCode,
  84. "outCode"=>$outCode,
  85. "orderCode"=> $oder['orderCode'],
  86. "good_code"=>$oder['good_code'],
  87. "good_name"=>$oder['good_name'],
  88. "customer_code"=>$oder['customer_code'],
  89. "apply_id"=>$rm,
  90. "apply_name"=>$ri,
  91. "cgder"=>$wsm['contactor_name'],
  92. "cgderid"=>$wsm['contactor'],
  93. 'total_num'=>$orderout['send_num'],
  94. "error_code"=>$errorCode,
  95. "error_num"=>$num,
  96. "error_img"=>$img,
  97. "error_msg"=>$error['result'],
  98. "error_remark"=>$remark,
  99. "is_receive"=>$is_receive,
  100. "remark"=>'',
  101. "except_code"=>$except_code,
  102. "order_type"=>$orderout['order_type'],
  103. "status"=>1,
  104. "is_del"=>0,
  105. "addtime"=>date("Y-m-d H:i:s"),
  106. "updatetime"=>date("Y-m-d H:i:s")
  107. ];
  108. $create = Db::name("order_return")->insert($in,true);
  109. $orde = ["order_code"=>$returnCode,"status"=>$in['status'],"action_remark"=>'',"action_type"=>"create"];
  110. ActionLog::logAdd($this->post['token'],$orde,'SHD',1,$orde);
  111. if($create>0){
  112. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'SHD',"before_status"=>1];
  113. ProcessOrder::AddProcess($token,$process);
  114. Db::commit();
  115. return app_show(0,"售后申请单新建成功",["returnCode"=>$returnCode]);
  116. }
  117. Db::rollback();
  118. return error_show(1005,"售后申请单新建失败");
  119. }catch (\Exception $e){
  120. Db::rollback();
  121. return error_show(1005,$e->getMessage());
  122. }
  123. }
  124. public function list(){
  125. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  126. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  127. $where =[['a.is_del',"=",0]];
  128. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  129. if($bkcode!=""){
  130. $where[]=['a.returnCode',"like", "%{$bkcode}%"];
  131. }
  132. $out = isset($this->post['outCode']) && $this->post['outCode']!="" ? trim($this->post['outCode']):"";
  133. if($out!=""){
  134. $where[]=['a.outCode',"like", "%{$out}%"];
  135. }
  136. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  137. if($status!==""){
  138. $where[]=['a.status',"=", $status];
  139. }
  140. $order_type = isset($this->post['order_type']) && $this->post['order_type']!=="" ? intval($this->post['order_type']):"";
  141. if($order_type!==""){
  142. $where[]=['a.order_type',"=", $order_type];
  143. }
  144. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  145. :"";
  146. if($orderCode!=""){
  147. $where[]=['a.orderCode',"like", "%{$orderCode}%"];
  148. }
  149. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code'])
  150. :"";
  151. if($good_code!=""){
  152. $where[]=['a.good_code',"like", "%{$good_code}%"];
  153. }
  154. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name'])
  155. :"";
  156. if($good_name!=""){
  157. $where[]=['a.good_name',"like", "%{$good_name}%"];
  158. }
  159. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code']!="" ? trim($this->post['customer_code'])
  160. :"";
  161. if($customer_code!=""){
  162. $where[]=['a.customer_code',"like", "%{$customer_code}%"];
  163. }
  164. $except_code = isset($this->post['except_code']) && $this->post['except_code']!="" ? trim($this->post['except_code']):"";
  165. if($except_code!=""){
  166. $where[]=['a.except_code',"=", $except_code];
  167. }
  168. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  169. :"";
  170. if($apply_name!=""){
  171. $where[]=['a.apply_name',"like", "%{$apply_name}%"];
  172. }
  173. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  174. if($start!==""){
  175. $where[]=['a.addtime',">=", $start];
  176. }
  177. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  178. if($end!==""){
  179. $where[]=['a.addtime',"<=", $end];
  180. }
  181. $role=$this->checkRole();
  182. $condition='';
  183. if(!empty($role['write']) && $this->uid!=""){
  184. // $where[]=["a.apply_id","in",$role['write']];
  185. $condition .="a.cgderid = {$this->uid} or a.apply_id in (".implode(',',$role['write']).")";
  186. }
  187. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  188. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  189. $count=Db::name("order_return")->alias("a")->leftJoin("order_returninfo b","a.returnCode=b.returnCode")->where
  190. ($where)->where($condition)->count();
  191. $total = ceil($count/$size);
  192. $page = $total>=$page ? $page :$total;
  193. $list =Db::name("order_return")
  194. ->alias("a")
  195. ->leftJoin("order_returninfo b","a.returnCode=b.returnCode")
  196. ->where($where)
  197. ->where($condition)
  198. ->field("a.*,b.return_wsm,b.contactor,b.mobile,b.addr,b.addr_code,b.post_code,b.post_company,b.post_fee,b.gys_remark,u.itemid")
  199. ->page($page,$size)
  200. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  201. ->order("addtime desc")
  202. ->cursor();
  203. $data=[];
  204. foreach ($list as $value){
  205. $customer = Db::name("customer_info")->where(["companyNo"=>$value['customer_code']])->find();
  206. $value['customer_name'] = isset($customer['companyName']) ?trim($customer['companyName']):'';
  207. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  208. $data[]=$value;
  209. }
  210. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  211. }
  212. /**
  213. * @return \think\response\Json|void
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\DbException
  216. * @throws \think\db\exception\ModelNotFoundException
  217. */
  218. public function info()
  219. {
  220. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode'] != "" ? trim($this->post['returnCode']) : "";
  221. if ($bkcode == "") {
  222. return error_show(1005, "参数returnCode 不能为空");
  223. }
  224. $info = Db::name("order_return")->alias("a")->leftJoin("order_returninfo b", "a.returnCode=b.returnCode")
  225. ->field("a.*,b.return_wsm,b.contactor,b.mobile,b.addr,b.addr_code,b.post_code,b.post_company,b.post_fee,b.gys_remark")
  226. ->where(['a.returnCode' => $bkcode])->find();
  227. if (empty($info)) {
  228. return error_show(1005, "未找到售后数据");
  229. }
  230. $orderinfo = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  231. $info['origin_price'] = $orderinfo['origin_price'];
  232. $info['order_type'] = $orderinfo['order_type'];
  233. $info['sale_price'] = $orderinfo['sale_price'];
  234. $info['total_price'] = sprintf("%1\$.2f", $orderinfo['sale_price'] * $info['error_num']);
  235. if ($orderinfo['order_type'] == 3 || $orderinfo['order_type'] == 4) {
  236. $goon = Db::name("good_zixun")->where(["spuCode" => $orderinfo['good_code'], "is_del" => 0])->find();
  237. } else {
  238. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  239. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  240. }
  241. if (empty($goon)) {
  242. return error_show(1003, "未找到商品数据");
  243. }
  244. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  245. $addcode = '';
  246. if ($info['addr_code'] != "") {
  247. list($a, $b, $c) = explode(",", $info['addr_code']);
  248. $addcode = json_encode(["provice_code" => $a, "city_code" => $b, "area_code" => $c]);
  249. }
  250. $info["addr_cn"]=GetAddr($addcode);
  251. // $info["addr_code"]=json_decode($addcode,true);
  252. $out =Db::name("order_out")->where(["outCode"=>$info['outCode']])->find();
  253. $info['out_num'] = isset($out['send_num'])?$out['send_num']:'';
  254. // $customer = Db::name("customer_info")->where(["companyNo"=>$info['customer_code']])->find();
  255. $wsmcode=[];
  256. if($info['return_wsm']!=''){
  257. $wsmcode = Db::name("warehouse_info")
  258. ->alias("a")
  259. ->leftJoin("supplier b","a.supplierNo=b.code")
  260. ->where(["a.wsm_code"=>$info['return_wsm']])
  261. ->field("a.name as wsm_name,b.name,b.code,a.wsm_code,a.contactor_name,a.position,a.mobile,a.addrs_code,a.addr,a.companyNo")
  262. ->find();
  263. }
  264. $info['wsm_code'] =isset($wsmcode['wsm_code']) ? $wsmcode['wsm_code']:"";
  265. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  266. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  267. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  268. $customer=[];
  269. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  270. $customer = Db::name("business")->field('id,company,companyNo')->where(["companyNo"=>$orderinfo['supplierNo']])->find();
  271. }
  272. $info['customer_name'] = isset($customer['company']) ?trim($customer['company']):'';
  273. $info['customer_code'] = isset($customer['companyNo']) ?trim($customer['companyNo']):'';
  274. $info['can']=$int;
  275. //仓库的联系人、职位、电话、省市区和地址
  276. isset($wsmcode['contactor_name'])? $info['contactor'] =$wsmcode['contactor_name']:'';//联系人
  277. isset($wsmcode['mobile'])? $info['mobile'] =$wsmcode['mobile']:'';//联系人
  278. isset($wsmcode['addrs_code'])? $info['addr_code'] =$wsmcode['addrs_code']:'';//联系人
  279. isset($wsmcode['addr'])? $info['addr'] =$wsmcode['addr']:'';//联系人
  280. isset($wsmcode['contactor_name'])? $info['contactor'] =$wsmcode['contactor_name']:'';//联系人
  281. // $info['position'] = $wsmcode['position'];//职位
  282. // $info['mobile'] = $wsmcode['mobile'];//电话
  283. // $info['addrs_code'] = json_decode($wsmcode['addrs_code'], true);//省市区编码
  284. // $info['addr'] = $wsmcode['addr'];//地址
  285. $info['spuCode'] = $orderinfo['good_code'];//地址
  286. $info['skuCode'] = $orderinfo['skuCode'];//地址
  287. $info['return_tag'] = $info['return_tag']==0?"":$info['return_tag'];//地址
  288. $info['is_th'] = $info['is_th']==0?"":$info['is_th'];//地址
  289. return app_show(0,"获取成功",$info);
  290. }
  291. public function status(){
  292. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  293. if($bkcode==""){
  294. return error_show(1005,"参数returnCode 不能为空");
  295. }
  296. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  297. if(empty($info)){
  298. return error_show(1005,"未找到售后数据");
  299. }
  300. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status'])
  301. :"";
  302. if($status===""){
  303. return error_show(1005,"参数status 不能为空");
  304. }
  305. $orde= Db::name("order_out")->where(["outCode"=>$info['outCode']])->find();
  306. if($orde==false){
  307. return error_show(1005,"未找到出库单数据");
  308. }
  309. $sale = Db::name("sale")->where(["orderCode"=> $info['orderCode']])->find();
  310. if($sale==false){
  311. return error_show(1005,"未找到销售单数据");
  312. }
  313. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  314. if($remark!=''){
  315. $info['remark'] =$remark;
  316. }
  317. if($status==3 && $info['is_receive']==1){
  318. $post =isset($this->post['is_post'])&&$this->post['is_post']!==""? intval($this->post['is_post']):"";
  319. if($post===""){
  320. return error_show(1005,"参数is_post不能为空");
  321. }
  322. $info['is_post']=$post;
  323. $return_tag =isset($this->post['return_tag'])&&$this->post['return_tag']!==""? intval($this->post['return_tag']):"";
  324. if($return_tag===""){
  325. return error_show(1005,"参数return_tag不能为空");
  326. }
  327. $info['return_tag']=$return_tag;
  328. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!==""? intval($this->post['is_th']):"0";
  329. $info['is_th']=$is_th;
  330. }
  331. // $status = $info['order_status']==1&&$info['order_type']==1 && $status==1?4:$status;
  332. Db::startTrans();
  333. try{
  334. $var=$info['status'];
  335. $info['status']=$status;
  336. $info['updatetime']=date("Y-m-d H:i:s");
  337. $up =Db::name("order_return")->save($info);
  338. if($up){
  339. if($status==5 && $info['is_receive']==0){
  340. $od_status = $orde['status'];
  341. $orde['status']=4;
  342. $orde['updatetime']=date("Y-m-d H:i:s");
  343. $out = Db::name("order_out")->save($orde);
  344. if($out==false){
  345. Db::rollback();
  346. return error_show(1004,"发货单更新失败");
  347. }
  348. $order = ["order_code"=>$orde['outCode'],"status"=>$od_status,"action_remark"=>'',"action_type"=>"edit"];
  349. ActionLog::logAdd($this->post['token'],$order,"CKD", $orde['status'],$this->post);
  350. $process=["order_code"=>$orde['outCode'],"order_id"=>$orde['id'],"order_status"=>$orde['status'],"order_type"=>"CKD","before_status"=>$od_status];
  351. ProcessOrder::AddProcess($this->post['token'],$process);
  352. $sale['th_num'] += $info['error_num'];
  353. $sale['th_fee'] += round($info['error_num']*$sale['sale_price'],2);
  354. $sale['updatetime']=date("Y-m-d H:i:s");
  355. $sup =Db::name("sale")->save($sale);
  356. if($sup==false){
  357. Db::rollback();
  358. return error_show(1004,"销售单更新失败");
  359. }
  360. $data=[
  361. "orderCode"=>$info['orderCode'],
  362. "th_type"=>2,
  363. "th_num"=>$info['error_num'],
  364. "th_fee"=>round($info['error_num']*$sale['sale_price'],2),
  365. "thCode"=>$info['returnCode'],
  366. "apply_id"=>$info['apply_id'],
  367. "apply_name"=>$info['apply_name'],
  368. "addtime"=>date("Y-m-d H:i:s"),
  369. "status"=>1,
  370. "is_del"=>0
  371. ];
  372. $inse=Db::name("th_data")->insert($data);
  373. if($inse==false){
  374. Db::rollback();
  375. return error_show(1004,"退回单更新失败");
  376. }
  377. }
  378. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  379. ActionLog::logAdd($this->post['token'],$order,"SHD", $info['status'],$this->post);
  380. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>"SHD","before_status"=>$var];
  381. ProcessOrder::AddProcess($this->post['token'],$process);
  382. Db::commit();
  383. return app_show(0,"更新成功");
  384. }else{
  385. Db::rollback();
  386. return error_show(1004,"更新失败");
  387. }
  388. }catch (\Exception $e){
  389. Db::rollback();
  390. return error_show(1004,$e->getMessage());
  391. }
  392. }
  393. public function setWsm(){
  394. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  395. if($bkcode==""){
  396. return error_show(1005,"参数returnCode 不能为空");
  397. }
  398. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  399. if(empty($info)){
  400. return error_show(1005,"未找到售后数据");
  401. }
  402. if($info['status']!=3){
  403. return error_show(1005,"采购主管未审核无法设置退回仓库");
  404. }
  405. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']) :"";
  406. if($info['return_tag']==2) {
  407. if($wsm_code==""){
  408. return error_show(1005,"参数 wsm_code 不能为空");
  409. }
  410. $warehouse =Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code])->find();
  411. if($warehouse==false){
  412. return error_show(1005,"仓库数据未找到");
  413. }
  414. $wareaddr =Db::name("warehouse_addr")->where(["wsm_code"=>$wsm_code,"is_del"=>0,"status"=>1])->find();
  415. }
  416. $addrcode=isset($this->post['addr_code'])&&$this->post['addr_code']!=""?$this->post['addr_code']:"";
  417. if($addrcode==""){
  418. return error_show(1005,"参数 addr_code 不能为空");
  419. }
  420. $addr=isset($this->post['addr'])&&$this->post['addr']!=""?trim($this->post['addr']):"";
  421. if($addr==""){
  422. return error_show(1005,"参数 addr 不能为空");
  423. }
  424. $mobile=isset($this->post['mobile'])&&$this->post['mobile']!=""?trim($this->post['mobile']):"";
  425. if($mobile==""){
  426. return error_show(1005,"参数 mobile 不能为空");
  427. }
  428. $contactor=isset($this->post['contactor'])&&$this->post['contactor']!=""?trim($this->post['contactor']):"";
  429. if($contactor==""){
  430. return error_show(1005,"参数 contactor 不能为空");
  431. }
  432. $returninfo=[
  433. "returnCode"=>$bkcode,
  434. "return_wsm"=>$wsm_code,
  435. "contactor"=>$contactor,
  436. "mobile"=>$mobile ,
  437. "addr"=>$addr,
  438. "addr_code"=>$addrcode,
  439. "addtime"=>date("Y-m-d H:i:s")
  440. ];
  441. Db::startTrans();
  442. try {
  443. $in=Db::name("order_returninfo")->insert($returninfo);
  444. if($in){
  445. if($info['return_tag']==2 &&$wareaddr==false) {
  446. $addrs =[
  447. "wsm_code"=>$wsm_code,
  448. "wsm_type"=>1,
  449. "wsm_name"=>$contactor,
  450. "wsm_addr"=>$addr,
  451. "addr_code"=>$addrcode,
  452. "wsm_mobile"=>$mobile,
  453. "status"=>'1',
  454. "is_del"=>'0',
  455. "addtime"=>date("Y-m-d H:i:s"),
  456. "updatetime"=>date("Y-m-d H:i:s")
  457. ];
  458. $wareaddr =Db::name("warehouse_addr")->insert($addrs);
  459. if($wareaddr==false){
  460. Db::rollback();
  461. return app_show(1004,"退货仓库地址录入失败");
  462. }
  463. }
  464. $old_info_status = $info['status'];
  465. $info['status']=4;
  466. $info["updatetime"]=date("Y-m-d H:i:s");
  467. $up =Db::name("order_return")->save($info);
  468. if($up){
  469. //修改状态,添加待办
  470. ActionLog::logAdd($this->post['token'], [
  471. "order_code" => $info['returnCode'],//销售单code
  472. "status" => $old_info_status,//这里的status是之前的值
  473. "action_remark" => '',//备注
  474. "action_type" => "status"//新建create,编辑edit,更改状态status
  475. ], "SHD", $info['status'], $info);
  476. ProcessOrder::AddProcess($this->post['token'], [
  477. "order_type" => 'SHD',
  478. "order_code" => $info['returnCode'],//销售单code
  479. "order_id" => $info['id'],
  480. "order_status" => $info['status'],"before_status"=>$old_info_status
  481. ]);
  482. Db::commit();
  483. return app_show(0,"退货仓库设置成功");
  484. }
  485. }
  486. Db::rollback();
  487. return error_show(1004,"设置失败");
  488. }catch (\Exception $e){
  489. Db::rollback();
  490. return error_show(1004,$e->getMessage());
  491. }
  492. }
  493. /**
  494. * @return \think\response\Json|void
  495. * @throws \think\db\exception\DataNotFoundException
  496. * @throws \think\db\exception\DbException
  497. * @throws \think\db\exception\ModelNotFoundException
  498. */
  499. public function postfeed(){
  500. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  501. if($bkcode==""){
  502. return error_show(1005,"参数returnCode 不能为空");
  503. }
  504. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  505. if(empty($info)){
  506. return error_show(1005,"未找到售后数据");
  507. }
  508. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor']) :"";
  509. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  510. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  511. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ?
  512. $this->post['addr_code']:"";
  513. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  514. if($wsm_code==""){
  515. return error_show(1005,"参数 wsm_code不能为空");
  516. }
  517. if($info['is_th']==0&&$info['order_type']==1) {
  518. $wsm = Db::name("warehouse_info")->where(["wsm_code" => $wsm_code])->find();
  519. if (empty($wsm)) {
  520. return error_show(1005, "未找到仓库数据");
  521. }
  522. $sm = Db::name("warehouse_addr")->where(["wsm_code" => $wsm_code,"is_del"=>0])->find();
  523. $contactor==""?(isset($sm['wsm_name']) ? $contactor=$sm['wsm_name']:""):"";
  524. $sm['wsm_name'] =isset($sm['wsm_name']) ? $sm['wsm_name']:"";
  525. $mobile==""?(isset($sm['wsm_mobile']) ? $mobile=$sm['wsm_mobile']:""):"";
  526. $sm['wsm_mobile'] =isset($sm['wsm_mobile']) ? $sm['wsm_mobile']:"";
  527. $addr==""?(isset($sm['wsm_addr']) ? $addr=$sm['wsm_addr']:""):"";
  528. $sm['wsm_addr'] =isset($sm['wsm_addr']) ? $sm['wsm_addr']:"";
  529. $addr_code==""?(isset($sm['addr_code']) ? $addr=$sm['addr_code']:""):"";
  530. $sm['addr_code'] =isset($sm['addr_code']) ? $sm['addr_code']:"";
  531. }
  532. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  533. if($info['is_th']==1 && $info['order_type']==1){
  534. if($contactor==''){
  535. return error_show(1005,"参数 contactor 不能为空");
  536. }
  537. if($mobile==''){
  538. return error_show(1005,"参数 mobile 不能为空");
  539. }
  540. if($addr==''){
  541. return error_show(1005,"参数 addr 不能为空");
  542. }
  543. if($addr_code==''){
  544. return error_show(1005,"参数 addr_code 不能为空");
  545. }
  546. if($addr_code!==''&&is_array($addr_code)){
  547. $addrs=[];
  548. $addrs['provice_code'] = $addr_code[0];
  549. $addrs['city_code'] = $addr_code[1];
  550. $addrs['area_code'] = $addr_code[2];
  551. $addr_code = json_encode($addrs);
  552. }
  553. }
  554. Db::startTrans();
  555. try{
  556. $var=$info['status'];
  557. $info['status']=$info['order_type']==1?3:4;
  558. $info['remark']=$remark;
  559. $info['updatetime']=date("Y-m-d H:i:s");
  560. $up =Db::name("order_return")->save($info);
  561. if($up){
  562. // var_dump($contactor);
  563. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  564. if(empty($returninfo)){
  565. $returninfo=[
  566. "returnCode"=>$bkcode,
  567. "return_wsm"=>$wsm_code,
  568. "contactor"=>$contactor,
  569. "mobile"=>$mobile ,
  570. "addr"=>$addr,
  571. "addr_code"=>$addr_code,
  572. "addtime"=>date("Y-m-d H:i:s")
  573. ];
  574. }else{
  575. $returninfo['return_wsm'] = $wsm_code;
  576. //if($info['is_th']==1 && $info['order_type']==1) {
  577. $contactor !==""? $returninfo['contactor'] = $contactor:"";
  578. $mobile!==""?$returninfo['mobile'] = $mobile:"";
  579. $addr!==""? $returninfo['addr'] = $addr:"";
  580. $addr_code!==""?$returninfo['addr_code'] = $addr_code:"";
  581. // }
  582. }
  583. // var_dump($contactor);
  584. // var_dump($returninfo);
  585. $in=Db::name("order_returninfo")->save($returninfo);
  586. if($in){
  587. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  588. ActionLog::logAdd($this->post['token'],$order,"SHD",$info['status'],$this->post);
  589. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$var];
  590. ProcessOrder::AddProcess($this->post['token'],$process);
  591. Db::commit();
  592. return app_show(0,"更新成功");
  593. }
  594. }
  595. Db::rollback();
  596. return error_show(1004,"更新失败");
  597. }catch (\Exception $e){
  598. Db::rollback();
  599. return error_show(1004,$e->getMessage());
  600. }
  601. }
  602. /**
  603. * @return \think\response\Json|void
  604. * @throws \think\db\exception\DataNotFoundException
  605. * @throws \think\db\exception\DbException
  606. * @throws \think\db\exception\ModelNotFoundException
  607. */
  608. public function addpost(){
  609. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  610. if($bkcode==""){
  611. return error_show(1005,"参数returnCode 不能为空");
  612. }
  613. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  614. if(empty($info)){
  615. return error_show(1005,"未找到售后数据");
  616. }
  617. $return_info = Db::name("order_returninfo")->where(['returnCode'=>$bkcode])->find();
  618. if(empty($return_info)){
  619. $return_info=[
  620. "returnCode"=>$bkcode,
  621. "return_wsm"=>'',
  622. "contactor"=>'',
  623. "mobile"=>'',
  624. "addr"=>'',
  625. "addr_code"=>'',
  626. "addtime"=>date("Y-m-d H:i:s"),
  627. "post_company"=>'',
  628. "post_code"=>'',
  629. "post_fee"=>'',
  630. ];
  631. }
  632. $post_own = isset($this->post['own']) &&$this->post['own']!='' ? trim($this->post['own']):"";
  633. if($post_own==''){
  634. return error_show(1005,"参数own不能为空");
  635. }
  636. $info['post_own'] = $post_own;
  637. $post_company = isset($this->post['company'])&&$this->post['company']!='' ? trim($this->post['company']):"";
  638. if($post_company==''){
  639. return error_show(1005,"参数company不能为空");
  640. }
  641. $return_info['post_company'] = $post_company;
  642. $post_code = isset($this->post['post_code'])&&$this->post['post_code']!='' ? trim($this->post['post_code']):"";
  643. if($post_code==''){
  644. return error_show(1005,"参数post_code不能为空");
  645. }
  646. $return_info['post_code'] = $post_code;
  647. $fee= isset($this->post['post_fee'])&&$this->post['post_fee']!=='' ? round($this->post['post_fee'],2):"";
  648. if($fee===''){
  649. return error_show(1005,"参数post_fee不能为空");
  650. }
  651. $return_info['post_fee'] = $fee;
  652. Db::startTrans();
  653. try{
  654. $str=$info['status'];
  655. $info['status']=$info['order_type']==1?4:5;
  656. $info['updatetime']=date("Y-m-d H:i:s");
  657. $up =Db::name("order_return")->save($info);
  658. if($up){
  659. $order = ["order_code"=>$info['orderCode'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  660. ActionLog::logAdd($this->post['token'],$order,"SHD",$info['status'],$this->post);
  661. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$str];
  662. ProcessOrder::AddProcess($this->post['token'],$process);
  663. $in=Db::name("order_returninfo")->save($return_info);
  664. if($in){
  665. $data=[
  666. 'thNo'=>makeNo("ST"),
  667. "orderCode"=>$info['orderCode'],
  668. "order_type"=>$info['order_type'],
  669. "outCode"=>$info['outCode'],
  670. "returnCode"=>$info['returnCode'],
  671. "good_code"=>$info['good_code'],
  672. "good_name"=>$info['good_name'],
  673. "return_wsm"=>$return_info['return_wsm'],
  674. "return_num"=>$info['error_num'],
  675. "contactor"=>$return_info['contactor'],
  676. "mobile"=>$return_info['mobile'],
  677. "addr"=>$return_info['addr'],
  678. "addr_code"=>$return_info['addr_code'],
  679. "return_code"=>$info['error_code'],
  680. "return_msg"=>$info['error_msg'],
  681. "post_fee"=>$return_info['post_fee'],
  682. "post_code"=>$return_info['post_code'],
  683. "post_company"=>$return_info['post_company'],
  684. "customer_code"=>$info['customer_code'],
  685. "addtime"=>date("Y-m-d H:i:s"),
  686. "updatetime"=>date("Y-m-d H:i:s")
  687. ];
  688. $back =Db::name("order_back")->insert($data,true);
  689. if($back>0){
  690. $order = ["order_code"=>$data['thNo'],"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  691. ActionLog::logAdd($this->post['token'],$order,"CKTHD", 0,$data);
  692. $process=["order_code"=>$data['thNo'],"order_id"=>$back,"order_status"=>0,"order_type"=>"CKTHD","before_status"=>0];
  693. ProcessOrder::AddProcess($this->post['token'],$process);
  694. Db::commit();
  695. return app_show(0,"更新成功");
  696. }
  697. }
  698. }
  699. Db::rollback();
  700. return error_show(1004,"更新失败");
  701. }catch (\Exception $e){
  702. Db::rollback();
  703. return error_show(1004,$e->getMessage());
  704. }
  705. }
  706. /**
  707. * @return \think\response\Json|void
  708. * @throws \think\db\exception\DataNotFoundException
  709. * @throws \think\db\exception\DbException
  710. * @throws \think\db\exception\ModelNotFoundException
  711. */
  712. public function GysFeed(){
  713. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  714. if($bkcode==""){
  715. return error_show(1005,"参数returnCode 不能为空");
  716. }
  717. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  718. if(empty($info)){
  719. return error_show(1005,"未找到售后数据");
  720. }
  721. $isth = isset($this->post['is_th']) && $this->post['is_th']!="" ? intval($this->post['is_th']):"0";
  722. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor'])
  723. :"";
  724. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  725. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  726. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ? $this->post['addr_code']:"";
  727. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  728. if($isth==1){
  729. if($contactor==''){
  730. return error_show(1005,"参数 contactor 不能为空");
  731. }
  732. if($mobile==''){
  733. return error_show(1005,"参数 mobile 不能为空");
  734. }
  735. if($addr==''){
  736. return error_show(1005,"参数 addr 不能为空");
  737. }
  738. // var_dump(isset($this->post['addr_code']) , $this->post['addr_code']!="",!empty($addr_code));
  739. if($addr_code==''){
  740. return error_show(1005,"参数 addr_code 不能为空");
  741. }
  742. if(is_array($addr_code)){
  743. $addrs=[];
  744. $addrs['provice_code'] = $addr_code[0];
  745. $addrs['city_code'] = $addr_code[1];
  746. $addrs['area_code'] = $addr_code[2];
  747. $addr_code = json_encode($addrs);
  748. }
  749. }
  750. $orde= Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  751. if(empty($orde)){
  752. return error_show(1005,"未找到出库单数据");
  753. }
  754. Db::startTrans();
  755. try{
  756. $stat=$info['status'];
  757. $info['status']=$info['order_status']==1?5:3;
  758. $info['is_th']=$isth;
  759. $info['updatetime']=date("Y-m-d H:i:s");
  760. $up =Db::name("order_return")->save($info);
  761. if($up){
  762. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  763. if(empty($returninfo)){
  764. $returninfo=[
  765. "returnCode"=>$bkcode,
  766. "return_wsm"=>'',
  767. "contactor"=>$contactor,
  768. "mobile"=>$mobile ,
  769. "addr"=>$addr,
  770. "addr_code"=>$addr_code,
  771. "gys_remark"=>$remark,
  772. "addtime"=>date("Y-m-d H:i:s")
  773. ];
  774. }else{
  775. $returninfo['return_wsm'] = '';
  776. $returninfo['contactor'] = $contactor;
  777. $returninfo['mobile'] = $mobile;
  778. $returninfo['addr'] = $addr;
  779. $returninfo['gys_remark'] = $remark;
  780. $returninfo['addr_code'] = $addr_code;
  781. }
  782. $in=Db::name("order_returninfo")->save($returninfo);
  783. if($in){
  784. $order = ["order_code"=>$info['returnCode'],"status"=>$stat,"action_remark"=>'',"action_type"=>"edit"];
  785. ActionLog::logAdd($this->post['token'],$order,"SHD", $info['status'],$this->post);
  786. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"SHD","before_status"=>$stat];
  787. ProcessOrder::AddProcess($this->post['token'],$process);
  788. Db::commit();
  789. return app_show(0,"更新成功");
  790. }
  791. }
  792. Db::rollback();
  793. return error_show(1004,"更新失败");
  794. }catch (\Exception $e){
  795. Db::rollback();
  796. return error_show(1004,$e->getMessage());
  797. }
  798. }
  799. public function GetWsm(){
  800. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  801. if($bkcode==""){
  802. return error_show(1005,"参数returnCode 不能为空");
  803. }
  804. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  805. if($info==false){
  806. return error_show(1005,"未找到售后数据");
  807. }
  808. $list=[];
  809. if($info['is_th']==1){
  810. if($info["order_type"]==1){
  811. $out =Db::name("order_out")->where(["outCode"=>$info['outCode']])->find();
  812. if(empty($out)){
  813. return error_show(1005,"未找到出库单数据");
  814. }
  815. $ware = Db::name("warehouse_info")->alias("a")->leftJoin("warehouse_addr b","a.wsm_code=b.wsm_code and b.status=1 and b.is_del=0")
  816. ->where(["a.status"=>1,"wsm_type"=>1,"a.is_del"=>0,"a.supplierNo"=>$out['wsm_code']])->field("a.wsm_code,a.name,b.wsm_name,b.wsm_mobile,b.wsm_addr,b.addr_code")->find();
  817. if(empty($ware)){
  818. return error_show(1005,"未找到仓库数据");
  819. }
  820. $temp['wsm_code']=isset($ware['wsm_code'])?$ware['wsm_code']:"";
  821. $temp['wsm_name']=isset($ware['name'])?$ware['name']:"";
  822. $temp['contactor'] = isset($ware['wsm_name'])?$ware['wsm_name']:"";
  823. $temp['mobile'] =isset($ware['wsm_mobile'])?$ware['wsm_mobile']:"";
  824. $temp['addr'] =isset($ware['wsm_addr'])?$ware['wsm_addr']:"";
  825. $temp['addr_code'] = isset($ware['addr_code'])?$ware['addr_code']:"";
  826. $temp['addr_cn'] =GetAddr( $ware['addr_code']);
  827. $list[]=$temp;
  828. }else {
  829. $returninfo = Db::name("order_returninfo")->where(["returnCode" => $bkcode])->find();
  830. if (empty($returninfo)) {
  831. return error_show(1005, "未找到供应商仓库数据");
  832. }
  833. $data = [];
  834. $data['wsm_code'] = isset($returninfo['return_wsm']) && $returninfo['return_wsm'] != "" ? $returninfo['return_wsm'] : "-";
  835. $data['wsm_name'] = '供应商仓';
  836. $data['supplier_name'] = '供应商';
  837. $data['contactor'] = $returninfo['contactor'];
  838. $data['mobile'] = $returninfo['mobile'];
  839. $data['addr'] = $returninfo['addr'];
  840. $data['addr_cn'] =GetAddr( $returninfo['addr_code']);
  841. $data['addr_code'] = $returninfo['addr_code'];
  842. $list[] = $data;
  843. }
  844. }
  845. $supplier = Db::name("supplier")->where(["is_platform"=>1,"status"=>1,"is_del"=>0])->column("code,name");
  846. if(empty($supplier)){
  847. return error_show(1005,"未找到供应商仓库数据");
  848. }
  849. foreach ($supplier as $value){
  850. $temp=[];
  851. $ware = Db::name("warehouse_info")->alias("a")->leftJoin("warehouse_addr b","a.wsm_code=b.wsm_code and b.status=1 and b.is_del=0")
  852. ->where(["a.wsm_type"=>1,"a.status"=>1,"a.is_del"=>0,"a.supplierNo"=>$value['code']])->field("a.wsm_code,a.name,b.wsm_name,b.wsm_mobile,b.wsm_addr,b.addr_code")->find();
  853. if(empty($ware)){
  854. continue;
  855. }
  856. $temp['wsm_code']=isset($ware['wsm_code'])?$ware['wsm_code']:"";
  857. $temp['wsm_name']=isset($ware['name'])?$ware['name']:"";
  858. $temp['supplier_name']=$value['name'];
  859. $temp['contactor'] = isset($ware['wsm_name'])?$ware['wsm_name']:"";
  860. $temp['mobile'] =isset($ware['wsm_mobile'])?$ware['wsm_mobile']:"";
  861. $temp['addr'] =isset($ware['wsm_addr'])?$ware['wsm_addr']:"";
  862. $temp['addr_cn'] =GetAddr($ware['addr_code']);
  863. $temp['addr_code'] = isset($ware['addr_code'])?$ware['addr_code']:"";
  864. $list[]=$temp;
  865. }
  866. return app_show(0,"获取成功",$list);
  867. }
  868. public function setdelivery(){
  869. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  870. if($bkcode==""){
  871. return error_show(1005,"参数returnCode 不能为空");
  872. }
  873. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  874. if(empty($info)){
  875. return error_show(1005,"未找到售后数据");
  876. }
  877. if(($info['is_receive']==0&&$info['status']!=3)||($info['is_receive']==1&&$info['status']!=4) ){
  878. return error_show(1005,"售后单流程状态有误");
  879. }
  880. $out=Db::name("order_out")->where(["outCode"=>$info["outCode"]])->find();
  881. if($out==false){
  882. return error_show(1005,"未找到发货单数据");
  883. }
  884. $is_th = isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  885. if($is_th===""){
  886. return error_show(1005,"参数is_th不能为空");
  887. }
  888. $company=isset($this->post['company'])&&$this->post['company']!=""?trim($this->post['company']):"";
  889. $post_code=isset($this->post['post_code'])&&$this->post['post_code']!=""?trim($this->post['post_code']):"";
  890. $post_fee=isset($this->post['post_fee'])&&$this->post['post_fee']!==""?floatval($this->post['post_fee']):"";
  891. if($is_th==1){
  892. if($company==""){
  893. return error_show(1005,"参数company不能为空");
  894. }
  895. if($post_code==""){
  896. return error_show(1005,"参数post_code不能为空");
  897. }
  898. if($post_fee===""){
  899. return error_show(1005,"参数post_fee不能为空");
  900. }
  901. }
  902. $sale= Db::name('sale')->where(['orderCode'=>$info['orderCode'],"is_del"=>0])->find();
  903. if($sale==false){
  904. Db::rollback();
  905. return error_show(1004,"未找到销售单数据");
  906. }
  907. Db::startTrans();
  908. try {
  909. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  910. if($returninfo==false){
  911. $returninfo=[
  912. "returnCode"=>$bkcode,
  913. "return_wsm"=>"",
  914. "contactor"=>"",
  915. "mobile"=>"",
  916. "addr"=>"",
  917. "addr_code"=>"",
  918. "post_code"=>$post_code,
  919. "post_company"=>$company,
  920. "post_fee"=>$post_fee,
  921. "gys_remark"=>"",
  922. "addtime"=>date("Y-m-d H:i:s")
  923. ];
  924. }else{
  925. $returninfo['post_fee']=$post_fee;
  926. $returninfo['post_company']=$company;
  927. $returninfo['post_code']=$post_code;
  928. }
  929. $up =Db::name("order_returninfo")->save($returninfo);
  930. if($up){
  931. $old_info_status = $info['status'];
  932. $info['status']=5;
  933. $info['updatetime']=date("y-m-d H:i:s");
  934. $ro =Db::name("order_return")->save($info);
  935. if($ro){
  936. //修改状态,添加待办
  937. ActionLog::logAdd($this->post['token'], [
  938. "order_code" => $info['returnCode'],//销售单code
  939. "status" => $old_info_status,//这里的status是之前的值
  940. "action_remark" => '',//备注
  941. "action_type" => "status"//新建create,编辑edit,更改状态status
  942. ], "SHD", $info['status'], $this->post);
  943. ProcessOrder::AddProcess($this->post['token'], [
  944. "order_type" => 'SHD',
  945. "order_code" => $info['returnCode'],//销售单code
  946. "order_id" => $info['id'],
  947. "order_status" => $info['status'],"before_status"=>$old_info_status
  948. ]);
  949. $thNo =makeNo("ST");
  950. $thdata=[
  951. "thNo"=>$thNo,
  952. "orderCode"=>$out["orderCode"],
  953. "outCode"=>$out["outCode"],
  954. "order_type"=>$info["order_type"],
  955. "returnCode"=>$info["returnCode"],
  956. "good_code"=>$info["good_code"],
  957. "good_name"=>$info["good_name"],
  958. "return_wsm"=>$returninfo["return_wsm"],
  959. "total_fee"=>round($sale['sale_price']*$info["error_num"],2),
  960. "good_price"=>$sale['sale_price'],
  961. "platform_id"=>$sale['platform_id'],
  962. "return_msg"=>$info["error_msg"],
  963. "return_num"=>$info["error_num"],
  964. "normal_num"=>0,
  965. "received_num"=>0,
  966. "apply_id"=>$info['apply_id'],
  967. "apply_name"=>$info['apply_name'],
  968. "cgder"=>$info['cgder'],
  969. "cgderid"=>$info['cgderid'],
  970. "contactor"=>$returninfo["contactor"],
  971. "mobile"=>$returninfo["mobile"],
  972. "addr"=>$returninfo["addr"],
  973. "addr_code"=>$returninfo["addr_code"],
  974. "return_code"=>$info["error_code"],
  975. "post_fee"=>$returninfo["post_fee"],
  976. "post_code"=>$returninfo["post_code"],
  977. "post_company"=>$returninfo["post_company"],
  978. "customer_code"=>$info["customer_code"],
  979. "status"=>$info['return_tag']==1?4:1,
  980. "addtime"=>date("Y-m-d H:i:s"),
  981. "updatetime"=>date("Y-m-d H:i:s")
  982. ];
  983. $sav= Db::name("order_back")->insert($thdata,true);
  984. if($sav){
  985. //修改状态,添加待办
  986. ActionLog::logAdd($this->post['token'], [
  987. "order_code" => $thdata['thNo'],//销售单code
  988. "status" => $thdata['status'],//这里的status是之前的值
  989. "action_remark" => '',//备注
  990. "action_type" => "create"//新建create,编辑edit,更改状态status
  991. ], "CKTHD", $thdata['status'], $thdata);
  992. ProcessOrder::AddProcess($this->post['token'], [
  993. "order_type" => 'CKTHD',
  994. "order_code" => $thdata['thNo'],//销售单code
  995. "order_id" => $sav,
  996. "order_status" => $thdata['status'],"before_status"=>1
  997. ]);
  998. if($info['return_tag']==1){
  999. $sale['th_num']+= $info['error_num'];
  1000. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  1001. $sale['status']=3;
  1002. }
  1003. $sale['th_fee']+= round($info['error_num']*$sale['sale_price'],2);
  1004. $sale['updatetime']= date("Y-m-d H:i:s");
  1005. $uap=Db::name("sale")->save($sale);
  1006. if($uap==false){
  1007. Db::rollback();
  1008. return error_show(1005,'销售单订单更新失败');
  1009. }
  1010. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  1011. if($ordernum==false){
  1012. Db::rollback();
  1013. return error_show(1005,'未找到关联采购单');
  1014. }
  1015. $ordernum['send_num']-=$info['error_num'];
  1016. $orderup =Db::name("order_num")->save($ordernum);
  1017. if($orderup==false){
  1018. Db::rollback();
  1019. return error_show(1005,'关联数据更新失败');
  1020. }
  1021. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  1022. if($cgd==false){
  1023. Db::rollback();
  1024. return error_show(1005,'未找到采购单数据');
  1025. }
  1026. $cgd['th_num']+= $info['error_num'];
  1027. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  1028. $cgd['status']=4;
  1029. }
  1030. $cgd['th_fee']+= round($info['error_num']*$cgd['good_price'],2);
  1031. $cgd['updatetime']= date("Y-m-d H:i:s");
  1032. $cgdup =Db::name("purchease_order")->save($cgd);
  1033. if($cgdup==false){
  1034. Db::rollback();
  1035. return error_show(1005,'采购单数据更新失败');
  1036. }
  1037. }
  1038. Db::commit();
  1039. return app_show(0,"退货单新建成功",["thNo"=>$thNo]);
  1040. }
  1041. }
  1042. }
  1043. Db::rollback();
  1044. return error_show(1004,'退货失败');
  1045. }catch (\Exception $e){
  1046. Db::rollback();
  1047. return error_show(1004,$e->getMessage());
  1048. }
  1049. }
  1050. //取消售后
  1051. public function Cancel()
  1052. {
  1053. //接收参数
  1054. $data = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1055. $val = Validate::rule(['returnCode|售后单编号' => 'require']);
  1056. if (!$val->check($data)) return error_show(1005, $val->getError());
  1057. Db::startTrans();
  1058. try {
  1059. $rs = Db::name('order_return')->field('id,status')->where([['returnCode', '=', $data['returnCode']], ['is_del', '=', 0], ['status', '<>', 8]])->find();
  1060. if (!$rs) throw new Exception('该售后单记录不存在');
  1061. //修改记录
  1062. $res = Db::name('order_return')->where(['id' => $rs['id']])->update(['status' => 8, 'updatetime' => date('Y-m-d H:i:s')]);
  1063. if (!$res) throw new Exception('该售后单记录修改失败');
  1064. //记录日志
  1065. ActionLog::logAdd($data['token'], ["order_code" => $data['returnCode'], "status" => $rs['status'], "action_remark" => '', "action_type" => "delete"], 'SHD', 8, $this->post);
  1066. ProcessOrder::AddProcess($this->post['token'], ["order_code" => $data['returnCode'], "order_id" => $rs['id'], "order_status" => 8, "order_type" => 'SHD',"before_status"=>$rs['status']]);
  1067. //提交
  1068. Db::commit();
  1069. return app_show(0, "取消成功");
  1070. } catch (\Exception $e) {
  1071. Db::rollback();
  1072. return error_show(1004, $e->getMessage());
  1073. }
  1074. }
  1075. }