After.php 57 KB

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