After.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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,'is_del'=>0])->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'],"is_del"=>0])->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'],"is_del"=>0])->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. "spuCode"=>$sale['good_code'],
  367. "good_name"=>$sale['good_name'],
  368. "cat_id"=>$sale['cat_id'],
  369. "apply_id"=>$info['apply_id'],
  370. "apply_name"=>$info['apply_name'],
  371. "addtime"=>date("Y-m-d H:i:s"),
  372. "status"=>1,
  373. "is_del"=>0
  374. ];
  375. $inse=Db::name("th_data")->insert($data);
  376. if($inse==false){
  377. Db::rollback();
  378. return error_show(1004,"退回单更新失败");
  379. }
  380. }
  381. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  382. ActionLog::logAdd($this->post['token'],$order,"SHD", $info['status'],$this->post);
  383. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>"SHD","before_status"=>$var];
  384. ProcessOrder::AddProcess($this->post['token'],$process);
  385. Db::commit();
  386. return app_show(0,"更新成功");
  387. }else{
  388. Db::rollback();
  389. return error_show(1004,"更新失败");
  390. }
  391. }catch (\Exception $e){
  392. Db::rollback();
  393. return error_show(1004,$e->getMessage());
  394. }
  395. }
  396. public function setWsm(){
  397. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  398. if($bkcode==""){
  399. return error_show(1005,"参数returnCode 不能为空");
  400. }
  401. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  402. if(empty($info)){
  403. return error_show(1005,"未找到售后数据");
  404. }
  405. if($info['status']!=3){
  406. return error_show(1005,"采购主管未审核无法设置退回仓库");
  407. }
  408. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']) :"";
  409. if($info['return_tag']==2) {
  410. if($wsm_code==""){
  411. return error_show(1005,"参数 wsm_code 不能为空");
  412. }
  413. $warehouse =Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code])->find();
  414. if($warehouse==false){
  415. return error_show(1005,"仓库数据未找到");
  416. }
  417. $wareaddr =Db::name("warehouse_addr")->where(["wsm_code"=>$wsm_code,"is_del"=>0,"status"=>1])->find();
  418. }
  419. $addrcode=isset($this->post['addr_code'])&&$this->post['addr_code']!=""?$this->post['addr_code']:"";
  420. if($addrcode==""){
  421. return error_show(1005,"参数 addr_code 不能为空");
  422. }
  423. $addr=isset($this->post['addr'])&&$this->post['addr']!=""?trim($this->post['addr']):"";
  424. if($addr==""){
  425. return error_show(1005,"参数 addr 不能为空");
  426. }
  427. $mobile=isset($this->post['mobile'])&&$this->post['mobile']!=""?trim($this->post['mobile']):"";
  428. if($mobile==""){
  429. return error_show(1005,"参数 mobile 不能为空");
  430. }
  431. $contactor=isset($this->post['contactor'])&&$this->post['contactor']!=""?trim($this->post['contactor']):"";
  432. if($contactor==""){
  433. return error_show(1005,"参数 contactor 不能为空");
  434. }
  435. $returninfo=[
  436. "returnCode"=>$bkcode,
  437. "return_wsm"=>$wsm_code,
  438. "contactor"=>$contactor,
  439. "mobile"=>$mobile ,
  440. "addr"=>$addr,
  441. "addr_code"=>$addrcode,
  442. "addtime"=>date("Y-m-d H:i:s")
  443. ];
  444. Db::startTrans();
  445. try {
  446. $in=Db::name("order_returninfo")->insert($returninfo);
  447. if($in){
  448. if($info['return_tag']==2 &&$wareaddr==false) {
  449. $addrs =[
  450. "wsm_code"=>$wsm_code,
  451. "wsm_type"=>1,
  452. "wsm_name"=>$contactor,
  453. "wsm_addr"=>$addr,
  454. "addr_code"=>$addrcode,
  455. "wsm_mobile"=>$mobile,
  456. "status"=>'1',
  457. "is_del"=>'0',
  458. "addtime"=>date("Y-m-d H:i:s"),
  459. "updatetime"=>date("Y-m-d H:i:s")
  460. ];
  461. $wareaddr =Db::name("warehouse_addr")->insert($addrs);
  462. if($wareaddr==false){
  463. Db::rollback();
  464. return app_show(1004,"退货仓库地址录入失败");
  465. }
  466. }
  467. $old_info_status = $info['status'];
  468. $info['status']=4;
  469. $info["updatetime"]=date("Y-m-d H:i:s");
  470. $up =Db::name("order_return")->save($info);
  471. if($up){
  472. //修改状态,添加待办
  473. ActionLog::logAdd($this->post['token'], [
  474. "order_code" => $info['returnCode'],//销售单code
  475. "status" => $old_info_status,//这里的status是之前的值
  476. "action_remark" => '',//备注
  477. "action_type" => "status"//新建create,编辑edit,更改状态status
  478. ], "SHD", $info['status'], $info);
  479. ProcessOrder::AddProcess($this->post['token'], [
  480. "order_type" => 'SHD',
  481. "order_code" => $info['returnCode'],//销售单code
  482. "order_id" => $info['id'],
  483. "order_status" => $info['status'],"before_status"=>$old_info_status
  484. ]);
  485. Db::commit();
  486. return app_show(0,"退货仓库设置成功");
  487. }
  488. }
  489. Db::rollback();
  490. return error_show(1004,"设置失败");
  491. }catch (\Exception $e){
  492. Db::rollback();
  493. return error_show(1004,$e->getMessage());
  494. }
  495. }
  496. /**
  497. * @return \think\response\Json|void
  498. * @throws \think\db\exception\DataNotFoundException
  499. * @throws \think\db\exception\DbException
  500. * @throws \think\db\exception\ModelNotFoundException
  501. */
  502. public function postfeed(){
  503. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  504. if($bkcode==""){
  505. return error_show(1005,"参数returnCode 不能为空");
  506. }
  507. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  508. if(empty($info)){
  509. return error_show(1005,"未找到售后数据");
  510. }
  511. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor']) :"";
  512. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  513. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  514. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ?
  515. $this->post['addr_code']:"";
  516. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  517. if($wsm_code==""){
  518. return error_show(1005,"参数 wsm_code不能为空");
  519. }
  520. if($info['is_th']==0&&$info['order_type']==1) {
  521. $wsm = Db::name("warehouse_info")->where(["wsm_code" => $wsm_code])->find();
  522. if (empty($wsm)) {
  523. return error_show(1005, "未找到仓库数据");
  524. }
  525. $sm = Db::name("warehouse_addr")->where(["wsm_code" => $wsm_code,"is_del"=>0])->find();
  526. $contactor==""?(isset($sm['wsm_name']) ? $contactor=$sm['wsm_name']:""):"";
  527. $sm['wsm_name'] =isset($sm['wsm_name']) ? $sm['wsm_name']:"";
  528. $mobile==""?(isset($sm['wsm_mobile']) ? $mobile=$sm['wsm_mobile']:""):"";
  529. $sm['wsm_mobile'] =isset($sm['wsm_mobile']) ? $sm['wsm_mobile']:"";
  530. $addr==""?(isset($sm['wsm_addr']) ? $addr=$sm['wsm_addr']:""):"";
  531. $sm['wsm_addr'] =isset($sm['wsm_addr']) ? $sm['wsm_addr']:"";
  532. $addr_code==""?(isset($sm['addr_code']) ? $addr=$sm['addr_code']:""):"";
  533. $sm['addr_code'] =isset($sm['addr_code']) ? $sm['addr_code']:"";
  534. }
  535. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  536. if($info['is_th']==1 && $info['order_type']==1){
  537. if($contactor==''){
  538. return error_show(1005,"参数 contactor 不能为空");
  539. }
  540. if($mobile==''){
  541. return error_show(1005,"参数 mobile 不能为空");
  542. }
  543. if($addr==''){
  544. return error_show(1005,"参数 addr 不能为空");
  545. }
  546. if($addr_code==''){
  547. return error_show(1005,"参数 addr_code 不能为空");
  548. }
  549. if($addr_code!==''&&is_array($addr_code)){
  550. $addrs=[];
  551. $addrs['provice_code'] = $addr_code[0];
  552. $addrs['city_code'] = $addr_code[1];
  553. $addrs['area_code'] = $addr_code[2];
  554. $addr_code = json_encode($addrs);
  555. }
  556. }
  557. Db::startTrans();
  558. try{
  559. $var=$info['status'];
  560. $info['status']=$info['order_type']==1?3:4;
  561. $info['remark']=$remark;
  562. $info['updatetime']=date("Y-m-d H:i:s");
  563. $up =Db::name("order_return")->save($info);
  564. if($up){
  565. // var_dump($contactor);
  566. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  567. if(empty($returninfo)){
  568. $returninfo=[
  569. "returnCode"=>$bkcode,
  570. "return_wsm"=>$wsm_code,
  571. "contactor"=>$contactor,
  572. "mobile"=>$mobile ,
  573. "addr"=>$addr,
  574. "addr_code"=>$addr_code,
  575. "addtime"=>date("Y-m-d H:i:s")
  576. ];
  577. }else{
  578. $returninfo['return_wsm'] = $wsm_code;
  579. //if($info['is_th']==1 && $info['order_type']==1) {
  580. $contactor !==""? $returninfo['contactor'] = $contactor:"";
  581. $mobile!==""?$returninfo['mobile'] = $mobile:"";
  582. $addr!==""? $returninfo['addr'] = $addr:"";
  583. $addr_code!==""?$returninfo['addr_code'] = $addr_code:"";
  584. // }
  585. }
  586. // var_dump($contactor);
  587. // var_dump($returninfo);
  588. $in=Db::name("order_returninfo")->save($returninfo);
  589. if($in){
  590. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  591. ActionLog::logAdd($this->post['token'],$order,"SHD",$info['status'],$this->post);
  592. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$var];
  593. ProcessOrder::AddProcess($this->post['token'],$process);
  594. Db::commit();
  595. return app_show(0,"更新成功");
  596. }
  597. }
  598. Db::rollback();
  599. return error_show(1004,"更新失败");
  600. }catch (\Exception $e){
  601. Db::rollback();
  602. return error_show(1004,$e->getMessage());
  603. }
  604. }
  605. /**
  606. * @return \think\response\Json|void
  607. * @throws \think\db\exception\DataNotFoundException
  608. * @throws \think\db\exception\DbException
  609. * @throws \think\db\exception\ModelNotFoundException
  610. */
  611. public function addpost(){
  612. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  613. if($bkcode==""){
  614. return error_show(1005,"参数returnCode 不能为空");
  615. }
  616. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  617. if(empty($info)){
  618. return error_show(1005,"未找到售后数据");
  619. }
  620. $return_info = Db::name("order_returninfo")->where(['returnCode'=>$bkcode])->find();
  621. if(empty($return_info)){
  622. $return_info=[
  623. "returnCode"=>$bkcode,
  624. "return_wsm"=>'',
  625. "contactor"=>'',
  626. "mobile"=>'',
  627. "addr"=>'',
  628. "addr_code"=>'',
  629. "addtime"=>date("Y-m-d H:i:s"),
  630. "post_company"=>'',
  631. "post_code"=>'',
  632. "post_fee"=>'',
  633. ];
  634. }
  635. $post_own = isset($this->post['own']) &&$this->post['own']!='' ? trim($this->post['own']):"";
  636. if($post_own==''){
  637. return error_show(1005,"参数own不能为空");
  638. }
  639. $info['post_own'] = $post_own;
  640. $post_company = isset($this->post['company'])&&$this->post['company']!='' ? trim($this->post['company']):"";
  641. if($post_company==''){
  642. return error_show(1005,"参数company不能为空");
  643. }
  644. $return_info['post_company'] = $post_company;
  645. $post_code = isset($this->post['post_code'])&&$this->post['post_code']!='' ? trim($this->post['post_code']):"";
  646. if($post_code==''){
  647. return error_show(1005,"参数post_code不能为空");
  648. }
  649. $return_info['post_code'] = $post_code;
  650. $fee= isset($this->post['post_fee'])&&$this->post['post_fee']!=='' ? round($this->post['post_fee'],2):"";
  651. if($fee===''){
  652. return error_show(1005,"参数post_fee不能为空");
  653. }
  654. $return_info['post_fee'] = $fee;
  655. Db::startTrans();
  656. try{
  657. $str=$info['status'];
  658. $info['status']=$info['order_type']==1?4:5;
  659. $info['updatetime']=date("Y-m-d H:i:s");
  660. $up =Db::name("order_return")->save($info);
  661. if($up){
  662. $order = ["order_code"=>$info['orderCode'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  663. ActionLog::logAdd($this->post['token'],$order,"SHD",$info['status'],$this->post);
  664. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$str];
  665. ProcessOrder::AddProcess($this->post['token'],$process);
  666. $in=Db::name("order_returninfo")->save($return_info);
  667. if($in){
  668. $data=[
  669. 'thNo'=>makeNo("ST"),
  670. "orderCode"=>$info['orderCode'],
  671. "order_type"=>$info['order_type'],
  672. "outCode"=>$info['outCode'],
  673. "returnCode"=>$info['returnCode'],
  674. "good_code"=>$info['good_code'],
  675. "good_name"=>$info['good_name'],
  676. "return_wsm"=>$return_info['return_wsm'],
  677. "return_num"=>$info['error_num'],
  678. "contactor"=>$return_info['contactor'],
  679. "mobile"=>$return_info['mobile'],
  680. "addr"=>$return_info['addr'],
  681. "addr_code"=>$return_info['addr_code'],
  682. "return_code"=>$info['error_code'],
  683. "return_msg"=>$info['error_msg'],
  684. "post_fee"=>$return_info['post_fee'],
  685. "post_code"=>$return_info['post_code'],
  686. "post_company"=>$return_info['post_company'],
  687. "customer_code"=>$info['customer_code'],
  688. "addtime"=>date("Y-m-d H:i:s"),
  689. "updatetime"=>date("Y-m-d H:i:s")
  690. ];
  691. $back =Db::name("order_back")->insert($data,true);
  692. if($back>0){
  693. $order = ["order_code"=>$data['thNo'],"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  694. ActionLog::logAdd($this->post['token'],$order,"CKTHD", 0,$data);
  695. $process=["order_code"=>$data['thNo'],"order_id"=>$back,"order_status"=>0,"order_type"=>"CKTHD","before_status"=>0];
  696. ProcessOrder::AddProcess($this->post['token'],$process);
  697. Db::commit();
  698. return app_show(0,"更新成功");
  699. }
  700. }
  701. }
  702. Db::rollback();
  703. return error_show(1004,"更新失败");
  704. }catch (\Exception $e){
  705. Db::rollback();
  706. return error_show(1004,$e->getMessage());
  707. }
  708. }
  709. /**
  710. * @return \think\response\Json|void
  711. * @throws \think\db\exception\DataNotFoundException
  712. * @throws \think\db\exception\DbException
  713. * @throws \think\db\exception\ModelNotFoundException
  714. */
  715. public function GysFeed(){
  716. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  717. if($bkcode==""){
  718. return error_show(1005,"参数returnCode 不能为空");
  719. }
  720. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  721. if(empty($info)){
  722. return error_show(1005,"未找到售后数据");
  723. }
  724. $isth = isset($this->post['is_th']) && $this->post['is_th']!="" ? intval($this->post['is_th']):"0";
  725. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor'])
  726. :"";
  727. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  728. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  729. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ? $this->post['addr_code']:"";
  730. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  731. if($isth==1){
  732. if($contactor==''){
  733. return error_show(1005,"参数 contactor 不能为空");
  734. }
  735. if($mobile==''){
  736. return error_show(1005,"参数 mobile 不能为空");
  737. }
  738. if($addr==''){
  739. return error_show(1005,"参数 addr 不能为空");
  740. }
  741. // var_dump(isset($this->post['addr_code']) , $this->post['addr_code']!="",!empty($addr_code));
  742. if($addr_code==''){
  743. return error_show(1005,"参数 addr_code 不能为空");
  744. }
  745. if(is_array($addr_code)){
  746. $addrs=[];
  747. $addrs['provice_code'] = $addr_code[0];
  748. $addrs['city_code'] = $addr_code[1];
  749. $addrs['area_code'] = $addr_code[2];
  750. $addr_code = json_encode($addrs);
  751. }
  752. }
  753. $orde= Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  754. if(empty($orde)){
  755. return error_show(1005,"未找到出库单数据");
  756. }
  757. Db::startTrans();
  758. try{
  759. $stat=$info['status'];
  760. $info['status']=$info['order_status']==1?5:3;
  761. $info['is_th']=$isth;
  762. $info['updatetime']=date("Y-m-d H:i:s");
  763. $up =Db::name("order_return")->save($info);
  764. if($up){
  765. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  766. if(empty($returninfo)){
  767. $returninfo=[
  768. "returnCode"=>$bkcode,
  769. "return_wsm"=>'',
  770. "contactor"=>$contactor,
  771. "mobile"=>$mobile ,
  772. "addr"=>$addr,
  773. "addr_code"=>$addr_code,
  774. "gys_remark"=>$remark,
  775. "addtime"=>date("Y-m-d H:i:s")
  776. ];
  777. }else{
  778. $returninfo['return_wsm'] = '';
  779. $returninfo['contactor'] = $contactor;
  780. $returninfo['mobile'] = $mobile;
  781. $returninfo['addr'] = $addr;
  782. $returninfo['gys_remark'] = $remark;
  783. $returninfo['addr_code'] = $addr_code;
  784. }
  785. $in=Db::name("order_returninfo")->save($returninfo);
  786. if($in){
  787. $order = ["order_code"=>$info['returnCode'],"status"=>$stat,"action_remark"=>'',"action_type"=>"edit"];
  788. ActionLog::logAdd($this->post['token'],$order,"SHD", $info['status'],$this->post);
  789. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"SHD","before_status"=>$stat];
  790. ProcessOrder::AddProcess($this->post['token'],$process);
  791. Db::commit();
  792. return app_show(0,"更新成功");
  793. }
  794. }
  795. Db::rollback();
  796. return error_show(1004,"更新失败");
  797. }catch (\Exception $e){
  798. Db::rollback();
  799. return error_show(1004,$e->getMessage());
  800. }
  801. }
  802. public function GetWsm(){
  803. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  804. if($bkcode==""){
  805. return error_show(1005,"参数returnCode 不能为空");
  806. }
  807. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  808. if($info==false){
  809. return error_show(1005,"未找到售后数据");
  810. }
  811. $list=[];
  812. if($info['is_th']==1){
  813. if($info["order_type"]==1){
  814. $out =Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  815. if(empty($out)){
  816. return error_show(1005,"未找到出库单数据");
  817. }
  818. $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")
  819. ->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();
  820. if(empty($ware)){
  821. return error_show(1005,"未找到仓库数据");
  822. }
  823. $temp['wsm_code']=isset($ware['wsm_code'])?$ware['wsm_code']:"";
  824. $temp['wsm_name']=isset($ware['name'])?$ware['name']:"";
  825. $temp['contactor'] = isset($ware['wsm_name'])?$ware['wsm_name']:"";
  826. $temp['mobile'] =isset($ware['wsm_mobile'])?$ware['wsm_mobile']:"";
  827. $temp['addr'] =isset($ware['wsm_addr'])?$ware['wsm_addr']:"";
  828. $temp['addr_code'] = isset($ware['addr_code'])?$ware['addr_code']:"";
  829. $temp['addr_cn'] =GetAddr( $ware['addr_code']);
  830. $list[]=$temp;
  831. }else {
  832. $returninfo = Db::name("order_returninfo")->where(["returnCode" => $bkcode])->find();
  833. if (empty($returninfo)) {
  834. return error_show(1005, "未找到供应商仓库数据");
  835. }
  836. $data = [];
  837. $data['wsm_code'] = isset($returninfo['return_wsm']) && $returninfo['return_wsm'] != "" ? $returninfo['return_wsm'] : "-";
  838. $data['wsm_name'] = '供应商仓';
  839. $data['supplier_name'] = '供应商';
  840. $data['contactor'] = $returninfo['contactor'];
  841. $data['mobile'] = $returninfo['mobile'];
  842. $data['addr'] = $returninfo['addr'];
  843. $data['addr_cn'] =GetAddr( $returninfo['addr_code']);
  844. $data['addr_code'] = $returninfo['addr_code'];
  845. $list[] = $data;
  846. }
  847. }
  848. $supplier = Db::name("supplier")->where(["is_platform"=>1,"status"=>1,"is_del"=>0])->column("code,name");
  849. if(empty($supplier)){
  850. return error_show(1005,"未找到供应商仓库数据");
  851. }
  852. foreach ($supplier as $value){
  853. $temp=[];
  854. $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")
  855. ->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();
  856. if(empty($ware)){
  857. continue;
  858. }
  859. $temp['wsm_code']=isset($ware['wsm_code'])?$ware['wsm_code']:"";
  860. $temp['wsm_name']=isset($ware['name'])?$ware['name']:"";
  861. $temp['supplier_name']=$value['name'];
  862. $temp['contactor'] = isset($ware['wsm_name'])?$ware['wsm_name']:"";
  863. $temp['mobile'] =isset($ware['wsm_mobile'])?$ware['wsm_mobile']:"";
  864. $temp['addr'] =isset($ware['wsm_addr'])?$ware['wsm_addr']:"";
  865. $temp['addr_cn'] =GetAddr($ware['addr_code']);
  866. $temp['addr_code'] = isset($ware['addr_code'])?$ware['addr_code']:"";
  867. $list[]=$temp;
  868. }
  869. return app_show(0,"获取成功",$list);
  870. }
  871. public function setdelivery(){
  872. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  873. if($bkcode==""){
  874. return error_show(1005,"参数returnCode 不能为空");
  875. }
  876. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  877. if(empty($info)){
  878. return error_show(1005,"未找到售后数据");
  879. }
  880. if(($info['is_receive']==0&&$info['status']!=3)||($info['is_receive']==1&&$info['status']!=4) ){
  881. return error_show(1005,"售后单流程状态有误");
  882. }
  883. $out=Db::name("order_out")->where(["outCode"=>$info["outCode"],"is_del"=>0])->find();
  884. if($out==false){
  885. return error_show(1005,"未找到发货单数据");
  886. }
  887. $is_th = isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  888. if($is_th===""){
  889. return error_show(1005,"参数is_th不能为空");
  890. }
  891. $company=isset($this->post['company'])&&$this->post['company']!=""?trim($this->post['company']):"";
  892. $post_code=isset($this->post['post_code'])&&$this->post['post_code']!=""?trim($this->post['post_code']):"";
  893. $post_fee=isset($this->post['post_fee'])&&$this->post['post_fee']!==""?floatval($this->post['post_fee']):"";
  894. if($is_th==1){
  895. if($company==""){
  896. return error_show(1005,"参数company不能为空");
  897. }
  898. if($post_code==""){
  899. return error_show(1005,"参数post_code不能为空");
  900. }
  901. if($post_fee===""){
  902. return error_show(1005,"参数post_fee不能为空");
  903. }
  904. }
  905. $sale= Db::name('sale')->where(['orderCode'=>$info['orderCode'],"is_del"=>0])->find();
  906. if($sale==false){
  907. Db::rollback();
  908. return error_show(1004,"未找到销售单数据");
  909. }
  910. Db::startTrans();
  911. try {
  912. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  913. if($returninfo==false){
  914. $returninfo=[
  915. "returnCode"=>$bkcode,
  916. "return_wsm"=>"",
  917. "contactor"=>"",
  918. "mobile"=>"",
  919. "addr"=>"",
  920. "addr_code"=>"",
  921. "post_code"=>$post_code,
  922. "post_company"=>$company,
  923. "post_fee"=>$post_fee,
  924. "gys_remark"=>"",
  925. "addtime"=>date("Y-m-d H:i:s")
  926. ];
  927. }else{
  928. $returninfo['post_fee']=$post_fee;
  929. $returninfo['post_company']=$company;
  930. $returninfo['post_code']=$post_code;
  931. }
  932. $up =Db::name("order_returninfo")->save($returninfo);
  933. if($up){
  934. $old_info_status = $info['status'];
  935. $info['status']=5;
  936. $info['updatetime']=date("y-m-d H:i:s");
  937. $ro =Db::name("order_return")->save($info);
  938. if($ro){
  939. //修改状态,添加待办
  940. ActionLog::logAdd($this->post['token'], [
  941. "order_code" => $info['returnCode'],//销售单code
  942. "status" => $old_info_status,//这里的status是之前的值
  943. "action_remark" => '',//备注
  944. "action_type" => "status"//新建create,编辑edit,更改状态status
  945. ], "SHD", $info['status'], $this->post);
  946. ProcessOrder::AddProcess($this->post['token'], [
  947. "order_type" => 'SHD',
  948. "order_code" => $info['returnCode'],//销售单code
  949. "order_id" => $info['id'],
  950. "order_status" => $info['status'],"before_status"=>$old_info_status
  951. ]);
  952. $thNo =makeNo("ST");
  953. $thdata=[
  954. "thNo"=>$thNo,
  955. "orderCode"=>$out["orderCode"],
  956. "outCode"=>$out["outCode"],
  957. "order_type"=>$info["order_type"],
  958. "returnCode"=>$info["returnCode"],
  959. "good_code"=>$info["good_code"],
  960. "good_name"=>$info["good_name"],
  961. "return_wsm"=>$returninfo["return_wsm"],
  962. "total_fee"=>round($sale['sale_price']*$info["error_num"],2),
  963. "good_price"=>$sale['sale_price'],
  964. "platform_id"=>$sale['platform_id'],
  965. "return_msg"=>$info["error_msg"],
  966. "return_num"=>$info["error_num"],
  967. "normal_num"=>0,
  968. "received_num"=>0,
  969. "apply_id"=>$info['apply_id'],
  970. "apply_name"=>$info['apply_name'],
  971. "cgder"=>$info['cgder'],
  972. "cgderid"=>$info['cgderid'],
  973. "contactor"=>$returninfo["contactor"],
  974. "mobile"=>$returninfo["mobile"],
  975. "addr"=>$returninfo["addr"],
  976. "addr_code"=>$returninfo["addr_code"],
  977. "return_code"=>$info["error_code"],
  978. "post_fee"=>$returninfo["post_fee"],
  979. "post_code"=>$returninfo["post_code"],
  980. "post_company"=>$returninfo["post_company"],
  981. "customer_code"=>$info["customer_code"],
  982. "status"=>$info['return_tag']==1?4:1,
  983. "addtime"=>date("Y-m-d H:i:s"),
  984. "updatetime"=>date("Y-m-d H:i:s")
  985. ];
  986. $sav= Db::name("order_back")->insert($thdata,true);
  987. if($sav){
  988. //修改状态,添加待办
  989. ActionLog::logAdd($this->post['token'], [
  990. "order_code" => $thdata['thNo'],//销售单code
  991. "status" => $thdata['status'],//这里的status是之前的值
  992. "action_remark" => '',//备注
  993. "action_type" => "create"//新建create,编辑edit,更改状态status
  994. ], "CKTHD", $thdata['status'], $thdata);
  995. ProcessOrder::AddProcess($this->post['token'], [
  996. "order_type" => 'CKTHD',
  997. "order_code" => $thdata['thNo'],//销售单code
  998. "order_id" => $sav,
  999. "order_status" => $thdata['status'],"before_status"=>1
  1000. ]);
  1001. if($info['return_tag']==1){
  1002. $sale['th_num']+= $info['error_num'];
  1003. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  1004. $sale['status']=3;
  1005. }
  1006. $sale['th_fee']+= round($info['error_num']*$sale['sale_price'],2);
  1007. $sale['updatetime']= date("Y-m-d H:i:s");
  1008. $uap=Db::name("sale")->save($sale);
  1009. if($uap==false){
  1010. Db::rollback();
  1011. return error_show(1005,'销售单订单更新失败');
  1012. }
  1013. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  1014. if($ordernum==false){
  1015. Db::rollback();
  1016. return error_show(1005,'未找到关联采购单');
  1017. }
  1018. $ordernum['send_num']-=$info['error_num'];
  1019. $orderup =Db::name("order_num")->save($ordernum);
  1020. if($orderup==false){
  1021. Db::rollback();
  1022. return error_show(1005,'关联数据更新失败');
  1023. }
  1024. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  1025. if($cgd==false){
  1026. Db::rollback();
  1027. return error_show(1005,'未找到采购单数据');
  1028. }
  1029. $cgd['th_num']+= $info['error_num'];
  1030. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  1031. $cgd['status']=4;
  1032. }
  1033. $cgd['th_fee']+= round($info['error_num']*$cgd['good_price'],2);
  1034. $cgd['updatetime']= date("Y-m-d H:i:s");
  1035. $cgdup =Db::name("purchease_order")->save($cgd);
  1036. if($cgdup==false){
  1037. Db::rollback();
  1038. return error_show(1005,'采购单数据更新失败');
  1039. }
  1040. if($info['error_num']>=$out['send_num']){
  1041. $out['status']=4;
  1042. $out['updatetime']=date("Y-m-d H:i:s");
  1043. $upout =Db::name("order_out")->save($out);
  1044. if($upout==false){
  1045. Db::rollback();
  1046. return error_show(1005,'出库单数据更新失败');
  1047. }
  1048. }
  1049. $data=[
  1050. "orderCode"=>$info['orderCode'],
  1051. "th_type"=>3,
  1052. "th_num"=>$info['error_num'],
  1053. "th_fee"=>round($sale['sale_price']*$info["error_num"],2),
  1054. "thCode"=>$thNo,
  1055. "spuCode"=>$sale['good_code'],
  1056. "good_name"=>$sale['good_name'],
  1057. "cat_id"=>$sale['cat_id'],
  1058. "apply_id"=>$info['apply_id'],
  1059. "apply_name"=>$info['apply_name'],
  1060. "addtime"=>date("Y-m-d H:i:s"),
  1061. "status"=>1,
  1062. "is_del"=>0
  1063. ];
  1064. $inse=Db::name("th_data")->insert($data);
  1065. if($inse==false){
  1066. Db::rollback();
  1067. return error_show(1004,"退货单更新失败");
  1068. }
  1069. }
  1070. Db::commit();
  1071. return app_show(0,"退货单新建成功",["thNo"=>$thNo]);
  1072. }
  1073. }
  1074. }
  1075. Db::rollback();
  1076. return error_show(1004,'退货失败');
  1077. }catch (\Exception $e){
  1078. Db::rollback();
  1079. return error_show(1004,$e->getMessage());
  1080. }
  1081. }
  1082. //取消售后
  1083. public function Cancel()
  1084. {
  1085. //接收参数
  1086. $data = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1087. $val = Validate::rule(['returnCode|售后单编号' => 'require']);
  1088. if (!$val->check($data)) return error_show(1005, $val->getError());
  1089. Db::startTrans();
  1090. try {
  1091. $rs = Db::name('order_return')->field('id,status')->where([['returnCode', '=', $data['returnCode']], ['is_del', '=', 0], ['status', '<>', 8]])->find();
  1092. if (!$rs) throw new Exception('该售后单记录不存在');
  1093. //修改记录
  1094. $res = Db::name('order_return')->where(['id' => $rs['id']])->update(['status' => 8, 'updatetime' => date('Y-m-d H:i:s')]);
  1095. if (!$res) throw new Exception('该售后单记录修改失败');
  1096. //记录日志
  1097. ActionLog::logAdd($data['token'], ["order_code" => $data['returnCode'], "status" => $rs['status'], "action_remark" => '', "action_type" => "delete"], 'SHD', 8, $this->post);
  1098. ProcessOrder::AddProcess($this->post['token'], ["order_code" => $data['returnCode'], "order_id" => $rs['id'], "order_status" => 8, "order_type" => 'SHD',"before_status"=>$rs['status']]);
  1099. //提交
  1100. Db::commit();
  1101. return app_show(0, "取消成功");
  1102. } catch (\Exception $e) {
  1103. Db::rollback();
  1104. return error_show(1004, $e->getMessage());
  1105. }
  1106. }
  1107. }