After.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. if($orderCode!=""){
  153. $where[]=['a.orderCode',"like", "%{$orderCode}%"];
  154. }
  155. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code'])
  156. :"";
  157. if($good_code!=""){
  158. $where[]=['a.good_code',"like", "%{$good_code}%"];
  159. }
  160. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name'])
  161. :"";
  162. if($good_name!=""){
  163. $where[]=['a.good_name',"like", "%{$good_name}%"];
  164. }
  165. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code']!="" ? trim($this->post['customer_code'])
  166. :"";
  167. if($customer_code!=""){
  168. $where[]=['a.customer_code',"like", "%{$customer_code}%"];
  169. }
  170. $except_code = isset($this->post['except_code']) && $this->post['except_code']!="" ? trim($this->post['except_code']):"";
  171. if($except_code!=""){
  172. $where[]=['a.except_code',"=", $except_code];
  173. }
  174. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  175. :"";
  176. if($apply_name!=""){
  177. $where[]=['a.apply_name',"like", "%{$apply_name}%"];
  178. }
  179. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  180. if($start!==""){
  181. $where[]=['a.addtime',">=", $start];
  182. }
  183. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  184. if($end!==""){
  185. $where[]=['a.addtime',"<=", $end];
  186. }
  187. $role=$this->checkRole();
  188. $condition='';
  189. if(!empty($role['write']) && $this->uid!=""){
  190. // $where[]=["a.apply_id","in",$role['write']];
  191. $condition .="a.cgderid = {$this->uid} or a.apply_id in (".implode(',',$role['write']).")";
  192. }
  193. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  194. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  195. $count=Db::name("order_return")->alias("a")->leftJoin("order_returninfo b","a.returnCode=b.returnCode")->where
  196. ($where)->where($condition)->count();
  197. $total = ceil($count/$size);
  198. $page = $total>=$page ? $page :$total;
  199. $list =Db::name("order_return")
  200. ->alias("a")
  201. ->leftJoin("order_returninfo b","a.returnCode=b.returnCode")
  202. ->where($where)
  203. ->where($condition)
  204. ->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,a.orderCode")
  205. ->page($page,$size)
  206. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  207. ->order("addtime desc")
  208. ->cursor();
  209. $data=[];
  210. foreach ($list as $value){
  211. $customer = Db::name("customer_info")->where(["companyNo"=>$value['customer_code']])->find();
  212. $value['customer_name'] = isset($customer['companyName']) ?trim($customer['companyName']):'';
  213. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  214. $data[]=$value;
  215. }
  216. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  217. }
  218. /**
  219. * @return \think\response\Json|void
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\DbException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. */
  224. public function info()
  225. {
  226. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode'] != "" ? trim($this->post['returnCode']) : "";
  227. if ($bkcode == "") {
  228. return error_show(1005, "参数returnCode 不能为空");
  229. }
  230. $info = Db::name("order_return")->alias("a")->leftJoin("order_returninfo b", "a.returnCode=b.returnCode")
  231. ->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")
  232. ->where(['a.returnCode' => $bkcode])->find();
  233. if (empty($info)) {
  234. return error_show(1005, "未找到售后数据");
  235. }
  236. $orderinfo = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  237. $info['origin_price'] = $orderinfo['origin_price'];
  238. $info['order_type'] = $orderinfo['order_type'];
  239. $info['sale_price'] = $orderinfo['sale_price'];
  240. $info['total_price'] = sprintf("%1\$.2f", $orderinfo['sale_price'] * $info['error_num']);
  241. if ($orderinfo['order_type'] == 3) {
  242. $goon = Db::name("good_zixun")->where(["spuCode" => $orderinfo['good_code'], "is_del" => 0])->find();
  243. } else {
  244. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  245. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  246. }
  247. if (empty($goon)) {
  248. return error_show(1003, "未找到商品数据");
  249. }
  250. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  251. $addcode = '';
  252. if ($info['addr_code'] != "") {
  253. list($a, $b, $c) = explode(",", $info['addr_code']);
  254. $addcode = json_encode(["provice_code" => $a, "city_code" => $b, "area_code" => $c]);
  255. }
  256. $info["addr_cn"]=GetAddr($addcode);
  257. // $info["addr_code"]=json_decode($addcode,true);
  258. $out =Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  259. $info['out_num'] = isset($out['send_num'])?$out['send_num']:'';
  260. // $customer = Db::name("customer_info")->where(["companyNo"=>$info['customer_code']])->find();
  261. $wsmcode=[];
  262. if($info['return_wsm']!=''){
  263. $wsmcode = Db::name("warehouse_info")
  264. ->alias("a")
  265. ->leftJoin("supplier b","a.supplierNo=b.code")
  266. ->where(["a.wsm_code"=>$info['return_wsm']])
  267. ->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")
  268. ->find();
  269. }
  270. $info['wsm_code'] =isset($wsmcode['wsm_code']) ? $wsmcode['wsm_code']:"";
  271. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  272. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  273. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  274. $customer=[];
  275. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  276. $customer = Db::name("business")->field('id,company,companyNo')->where(["companyNo"=>$orderinfo['supplierNo']])->find();
  277. }
  278. $info['customer_name'] = isset($customer['company']) ?trim($customer['company']):'';
  279. $info['customer_code'] = isset($customer['companyNo']) ?trim($customer['companyNo']):'';
  280. $info['can']=$int;
  281. //仓库的联系人、职位、电话、省市区和地址
  282. isset($wsmcode['contactor_name'])? $info['contactor'] =$wsmcode['contactor_name']:'';//联系人
  283. isset($wsmcode['mobile'])? $info['mobile'] =$wsmcode['mobile']:'';//联系人
  284. isset($wsmcode['addrs_code'])? $info['addr_code'] =$wsmcode['addrs_code']:'';//联系人
  285. isset($wsmcode['addr'])? $info['addr'] =$wsmcode['addr']:'';//联系人
  286. isset($wsmcode['contactor_name'])? $info['contactor'] =$wsmcode['contactor_name']:'';//联系人
  287. // $info['position'] = $wsmcode['position'];//职位
  288. // $info['mobile'] = $wsmcode['mobile'];//电话
  289. // $info['addrs_code'] = json_decode($wsmcode['addrs_code'], true);//省市区编码
  290. // $info['addr'] = $wsmcode['addr'];//地址
  291. $info['spuCode'] = $orderinfo['good_code'];//地址
  292. $info['skuCode'] = $orderinfo['skuCode'];//地址
  293. $info['return_tag'] = $info['return_tag']==0?"":$info['return_tag'];//地址
  294. $info['is_th'] = $info['is_th']==0?"":$info['is_th'];//地址
  295. return app_show(0,"获取成功",$info);
  296. }
  297. public function status(){
  298. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  299. if($bkcode==""){
  300. return error_show(1005,"参数returnCode 不能为空");
  301. }
  302. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  303. if(empty($info)){
  304. return error_show(1005,"未找到售后数据");
  305. }
  306. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status'])
  307. :"";
  308. if($status===""){
  309. return error_show(1005,"参数status 不能为空");
  310. }
  311. $orde= Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  312. if($orde==false){
  313. return error_show(1005,"未找到出库单数据");
  314. }
  315. $sale = Db::name("sale")->where(["orderCode"=> $info['orderCode']])->find();
  316. if($sale==false){
  317. return error_show(1005,"未找到销售单数据");
  318. }
  319. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  320. if($remark!=''){
  321. $info['remark'] =$remark;
  322. }
  323. if($status==3 && $info['is_receive']==1){
  324. $post =isset($this->post['is_post'])&&$this->post['is_post']!==""? intval($this->post['is_post']):"";
  325. if($post===""){
  326. return error_show(1005,"参数is_post不能为空");
  327. }
  328. $info['is_post']=$post;
  329. $return_tag =isset($this->post['return_tag'])&&$this->post['return_tag']!==""? intval($this->post['return_tag']):"";
  330. if($return_tag===""){
  331. return error_show(1005,"参数return_tag不能为空");
  332. }
  333. $info['return_tag']=$return_tag;
  334. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!==""? intval($this->post['is_th']):"0";
  335. $info['is_th']=$is_th;
  336. }
  337. // $status = $info['order_status']==1&&$info['order_type']==1 && $status==1?4:$status;
  338. Db::startTrans();
  339. try{
  340. $var=$info['status'];
  341. $info['status']=$status;
  342. $info['updatetime']=date("Y-m-d H:i:s");
  343. $up =Db::name("order_return")->save($info);
  344. if($up){
  345. if($status==5 && $info['is_receive']==0){
  346. $od_status = $orde['status'];
  347. $orde['status']=4;
  348. $orde['updatetime']=date("Y-m-d H:i:s");
  349. $out = Db::name("order_out")->save($orde);
  350. if($out==false){
  351. Db::rollback();
  352. return error_show(1004,"发货单更新失败");
  353. }
  354. $order = ["order_code"=>$orde['outCode'],"status"=>$od_status,"action_remark"=>'',"action_type"=>"edit"];
  355. ActionLog::logAdd($this->post['token'],$order,"CKD", $orde['status'],$this->post);
  356. $process=["order_code"=>$orde['outCode'],"order_id"=>$orde['id'],"order_status"=>$orde['status'],"order_type"=>"CKD","before_status"=>$od_status];
  357. ProcessOrder::AddProcess($this->post['token'],$process);
  358. $sale['th_num'] += $info['error_num'];
  359. $sale['th_fee'] += round($info['error_num']*$sale['sale_price'],2);
  360. $sale['updatetime']=date("Y-m-d H:i:s");
  361. $sup =Db::name("sale")->save($sale);
  362. if($sup==false){
  363. Db::rollback();
  364. return error_show(1004,"销售单更新失败");
  365. }
  366. $data=[
  367. "orderCode"=>$info['orderCode'],
  368. "th_type"=>2,
  369. "th_num"=>$info['error_num'],
  370. "th_fee"=>round($info['error_num']*$sale['sale_price'],2),
  371. "thCode"=>$info['returnCode'],
  372. "spuCode"=>$sale['good_code'],
  373. "good_name"=>$sale['good_name'],
  374. "cat_id"=>$sale['cat_id'],
  375. "apply_id"=>$info['apply_id'],
  376. "apply_name"=>$info['apply_name'],
  377. "addtime"=>date("Y-m-d H:i:s"),
  378. "status"=>1,
  379. "is_del"=>0
  380. ];
  381. $inse=Db::name("th_data")->insert($data);
  382. if($inse==false){
  383. Db::rollback();
  384. return error_show(1004,"退回单更新失败");
  385. }
  386. }
  387. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  388. ActionLog::logAdd($this->post['token'],$order,"SHD", $info['status'],$this->post);
  389. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>"SHD","before_status"=>$var];
  390. ProcessOrder::AddProcess($this->post['token'],$process);
  391. Db::commit();
  392. return app_show(0,"更新成功");
  393. }else{
  394. Db::rollback();
  395. return error_show(1004,"更新失败");
  396. }
  397. }catch (\Exception $e){
  398. Db::rollback();
  399. return error_show(1004,$e->getMessage());
  400. }
  401. }
  402. public function setWsm(){
  403. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  404. if($bkcode==""){
  405. return error_show(1005,"参数returnCode 不能为空");
  406. }
  407. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  408. if(empty($info)){
  409. return error_show(1005,"未找到售后数据");
  410. }
  411. if($info['status']!=3){
  412. return error_show(1005,"采购主管未审核无法设置退回仓库");
  413. }
  414. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']) :"";
  415. if($info['return_tag']==2) {
  416. if($wsm_code==""){
  417. return error_show(1005,"参数 wsm_code 不能为空");
  418. }
  419. $warehouse =Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code])->find();
  420. if($warehouse==false){
  421. return error_show(1005,"仓库数据未找到");
  422. }
  423. $wareaddr =Db::name("warehouse_addr")->where(["wsm_code"=>$wsm_code,"is_del"=>0,"status"=>1])->find();
  424. }
  425. $addrcode=isset($this->post['addr_code'])&&$this->post['addr_code']!=""?$this->post['addr_code']:"";
  426. if($addrcode==""){
  427. return error_show(1005,"参数 addr_code 不能为空");
  428. }
  429. $addr=isset($this->post['addr'])&&$this->post['addr']!=""?trim($this->post['addr']):"";
  430. if($addr==""){
  431. return error_show(1005,"参数 addr 不能为空");
  432. }
  433. $mobile=isset($this->post['mobile'])&&$this->post['mobile']!=""?trim($this->post['mobile']):"";
  434. if($mobile==""){
  435. return error_show(1005,"参数 mobile 不能为空");
  436. }
  437. $contactor=isset($this->post['contactor'])&&$this->post['contactor']!=""?trim($this->post['contactor']):"";
  438. if($contactor==""){
  439. return error_show(1005,"参数 contactor 不能为空");
  440. }
  441. $returninfo=[
  442. "returnCode"=>$bkcode,
  443. "return_wsm"=>$wsm_code,
  444. "contactor"=>$contactor,
  445. "mobile"=>$mobile ,
  446. "addr"=>$addr,
  447. "addr_code"=>$addrcode,
  448. "addtime"=>date("Y-m-d H:i:s")
  449. ];
  450. Db::startTrans();
  451. try {
  452. $in=Db::name("order_returninfo")->insert($returninfo);
  453. if($in){
  454. if($info['return_tag']==2 &&$wareaddr==false) {
  455. $addrs =[
  456. "wsm_code"=>$wsm_code,
  457. "wsm_type"=>1,
  458. "wsm_name"=>$contactor,
  459. "wsm_addr"=>$addr,
  460. "addr_code"=>$addrcode,
  461. "wsm_mobile"=>$mobile,
  462. "status"=>'1',
  463. "is_del"=>'0',
  464. "addtime"=>date("Y-m-d H:i:s"),
  465. "updatetime"=>date("Y-m-d H:i:s")
  466. ];
  467. $wareaddr =Db::name("warehouse_addr")->insert($addrs);
  468. if($wareaddr==false){
  469. Db::rollback();
  470. return app_show(1004,"退货仓库地址录入失败");
  471. }
  472. }
  473. $old_info_status = $info['status'];
  474. $info['status']=4;
  475. $info["updatetime"]=date("Y-m-d H:i:s");
  476. $up =Db::name("order_return")->save($info);
  477. if($up){
  478. //修改状态,添加待办
  479. ActionLog::logAdd($this->post['token'], [
  480. "order_code" => $info['returnCode'],//销售单code
  481. "status" => $old_info_status,//这里的status是之前的值
  482. "action_remark" => '',//备注
  483. "action_type" => "status"//新建create,编辑edit,更改状态status
  484. ], "SHD", $info['status'], $info);
  485. ProcessOrder::AddProcess($this->post['token'], [
  486. "order_type" => 'SHD',
  487. "order_code" => $info['returnCode'],//销售单code
  488. "order_id" => $info['id'],
  489. "order_status" => $info['status'],"before_status"=>$old_info_status
  490. ]);
  491. Db::commit();
  492. return app_show(0,"退货仓库设置成功");
  493. }
  494. }
  495. Db::rollback();
  496. return error_show(1004,"设置失败");
  497. }catch (\Exception $e){
  498. Db::rollback();
  499. return error_show(1004,$e->getMessage());
  500. }
  501. }
  502. /**
  503. * @return \think\response\Json|void
  504. * @throws \think\db\exception\DataNotFoundException
  505. * @throws \think\db\exception\DbException
  506. * @throws \think\db\exception\ModelNotFoundException
  507. */
  508. public function postfeed(){
  509. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  510. if($bkcode==""){
  511. return error_show(1005,"参数returnCode 不能为空");
  512. }
  513. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  514. if(empty($info)){
  515. return error_show(1005,"未找到售后数据");
  516. }
  517. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor']) :"";
  518. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  519. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  520. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ?
  521. $this->post['addr_code']:"";
  522. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  523. if($wsm_code==""){
  524. return error_show(1005,"参数 wsm_code不能为空");
  525. }
  526. if($info['is_th']==0&&$info['order_type']==1) {
  527. $wsm = Db::name("warehouse_info")->where(["wsm_code" => $wsm_code])->find();
  528. if (empty($wsm)) {
  529. return error_show(1005, "未找到仓库数据");
  530. }
  531. $sm = Db::name("warehouse_addr")->where(["wsm_code" => $wsm_code,"is_del"=>0])->find();
  532. $contactor==""?(isset($sm['wsm_name']) ? $contactor=$sm['wsm_name']:""):"";
  533. $sm['wsm_name'] =isset($sm['wsm_name']) ? $sm['wsm_name']:"";
  534. $mobile==""?(isset($sm['wsm_mobile']) ? $mobile=$sm['wsm_mobile']:""):"";
  535. $sm['wsm_mobile'] =isset($sm['wsm_mobile']) ? $sm['wsm_mobile']:"";
  536. $addr==""?(isset($sm['wsm_addr']) ? $addr=$sm['wsm_addr']:""):"";
  537. $sm['wsm_addr'] =isset($sm['wsm_addr']) ? $sm['wsm_addr']:"";
  538. $addr_code==""?(isset($sm['addr_code']) ? $addr=$sm['addr_code']:""):"";
  539. $sm['addr_code'] =isset($sm['addr_code']) ? $sm['addr_code']:"";
  540. }
  541. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  542. if($info['is_th']==1 && $info['order_type']==1){
  543. if($contactor==''){
  544. return error_show(1005,"参数 contactor 不能为空");
  545. }
  546. if($mobile==''){
  547. return error_show(1005,"参数 mobile 不能为空");
  548. }
  549. if($addr==''){
  550. return error_show(1005,"参数 addr 不能为空");
  551. }
  552. if($addr_code==''){
  553. return error_show(1005,"参数 addr_code 不能为空");
  554. }
  555. if($addr_code!==''&&is_array($addr_code)){
  556. $addrs=[];
  557. $addrs['provice_code'] = $addr_code[0];
  558. $addrs['city_code'] = $addr_code[1];
  559. $addrs['area_code'] = $addr_code[2];
  560. $addr_code = json_encode($addrs);
  561. }
  562. }
  563. Db::startTrans();
  564. try{
  565. $var=$info['status'];
  566. $info['status']=$info['order_type']==1?3:4;
  567. $info['remark']=$remark;
  568. $info['updatetime']=date("Y-m-d H:i:s");
  569. $up =Db::name("order_return")->save($info);
  570. if($up){
  571. // var_dump($contactor);
  572. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  573. if(empty($returninfo)){
  574. $returninfo=[
  575. "returnCode"=>$bkcode,
  576. "return_wsm"=>$wsm_code,
  577. "contactor"=>$contactor,
  578. "mobile"=>$mobile ,
  579. "addr"=>$addr,
  580. "addr_code"=>$addr_code,
  581. "addtime"=>date("Y-m-d H:i:s")
  582. ];
  583. }else{
  584. $returninfo['return_wsm'] = $wsm_code;
  585. //if($info['is_th']==1 && $info['order_type']==1) {
  586. $contactor !==""? $returninfo['contactor'] = $contactor:"";
  587. $mobile!==""?$returninfo['mobile'] = $mobile:"";
  588. $addr!==""? $returninfo['addr'] = $addr:"";
  589. $addr_code!==""?$returninfo['addr_code'] = $addr_code:"";
  590. // }
  591. }
  592. // var_dump($contactor);
  593. // var_dump($returninfo);
  594. $in=Db::name("order_returninfo")->save($returninfo);
  595. if($in){
  596. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  597. ActionLog::logAdd($this->post['token'],$order,"SHD",$info['status'],$this->post);
  598. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$var];
  599. ProcessOrder::AddProcess($this->post['token'],$process);
  600. Db::commit();
  601. return app_show(0,"更新成功");
  602. }
  603. }
  604. Db::rollback();
  605. return error_show(1004,"更新失败");
  606. }catch (\Exception $e){
  607. Db::rollback();
  608. return error_show(1004,$e->getMessage());
  609. }
  610. }
  611. /**
  612. * @return \think\response\Json|void
  613. * @throws \think\db\exception\DataNotFoundException
  614. * @throws \think\db\exception\DbException
  615. * @throws \think\db\exception\ModelNotFoundException
  616. */
  617. public function addpost(){
  618. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  619. if($bkcode==""){
  620. return error_show(1005,"参数returnCode 不能为空");
  621. }
  622. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  623. if(empty($info)){
  624. return error_show(1005,"未找到售后数据");
  625. }
  626. $return_info = Db::name("order_returninfo")->where(['returnCode'=>$bkcode])->find();
  627. if(empty($return_info)){
  628. $return_info=[
  629. "returnCode"=>$bkcode,
  630. "return_wsm"=>'',
  631. "contactor"=>'',
  632. "mobile"=>'',
  633. "addr"=>'',
  634. "addr_code"=>'',
  635. "addtime"=>date("Y-m-d H:i:s"),
  636. "post_company"=>'',
  637. "post_code"=>'',
  638. "post_fee"=>'',
  639. ];
  640. }
  641. $post_own = isset($this->post['own']) &&$this->post['own']!='' ? trim($this->post['own']):"";
  642. if($post_own==''){
  643. return error_show(1005,"参数own不能为空");
  644. }
  645. $info['post_own'] = $post_own;
  646. $post_company = isset($this->post['company'])&&$this->post['company']!='' ? trim($this->post['company']):"";
  647. if($post_company==''){
  648. return error_show(1005,"参数company不能为空");
  649. }
  650. $return_info['post_company'] = $post_company;
  651. $post_code = isset($this->post['post_code'])&&$this->post['post_code']!='' ? trim($this->post['post_code']):"";
  652. if($post_code==''){
  653. return error_show(1005,"参数post_code不能为空");
  654. }
  655. $return_info['post_code'] = $post_code;
  656. $fee= isset($this->post['post_fee'])&&$this->post['post_fee']!=='' ? round($this->post['post_fee'],2):"";
  657. if($fee===''){
  658. return error_show(1005,"参数post_fee不能为空");
  659. }
  660. $return_info['post_fee'] = $fee;
  661. Db::startTrans();
  662. try{
  663. $str=$info['status'];
  664. $info['status']=$info['order_type']==1?4:5;
  665. $info['updatetime']=date("Y-m-d H:i:s");
  666. $up =Db::name("order_return")->save($info);
  667. if($up){
  668. $order = ["order_code"=>$info['orderCode'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  669. ActionLog::logAdd($this->post['token'],$order,"SHD",$info['status'],$this->post);
  670. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$str];
  671. ProcessOrder::AddProcess($this->post['token'],$process);
  672. $in=Db::name("order_returninfo")->save($return_info);
  673. if($in){
  674. $data=[
  675. 'thNo'=>makeNo("ST"),
  676. "orderCode"=>$info['orderCode'],
  677. "order_type"=>$info['order_type'],
  678. "outCode"=>$info['outCode'],
  679. "returnCode"=>$info['returnCode'],
  680. "good_code"=>$info['good_code'],
  681. "good_name"=>$info['good_name'],
  682. "return_wsm"=>$return_info['return_wsm'],
  683. "return_num"=>$info['error_num'],
  684. "contactor"=>$return_info['contactor'],
  685. "mobile"=>$return_info['mobile'],
  686. "addr"=>$return_info['addr'],
  687. "addr_code"=>$return_info['addr_code'],
  688. "return_code"=>$info['error_code'],
  689. "return_msg"=>$info['error_msg'],
  690. "post_fee"=>$return_info['post_fee'],
  691. "post_code"=>$return_info['post_code'],
  692. "post_company"=>$return_info['post_company'],
  693. "customer_code"=>$info['customer_code'],
  694. "addtime"=>date("Y-m-d H:i:s"),
  695. "updatetime"=>date("Y-m-d H:i:s")
  696. ];
  697. $back =Db::name("order_back")->insert($data,true);
  698. if($back>0){
  699. //维护台账
  700. // Db::name('standing_book')
  701. // ->where('returnCode', $bkcode)
  702. // ->update([
  703. // 'thNo' => $data['thNo'],
  704. // 'updatetime' => date("Y-m-d H:i:s")
  705. // ]);
  706. Db::execute("UPDATE `wsm_standing_book` SET `thNo`=CONCAT(`thNo`,',{$data['thNo']}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET(`returnCode`,'{$bkcode}')");//这里的returnCode可能有多个
  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. Db::execute("UPDATE `wsm_standing_book` SET `thNo`=CONCAT(`thNo`,',{$thNo}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET(`returnCode`,'{$bkcode}')");//这里的returnCode可能有多个
  1023. if($info['return_tag']==1){
  1024. $sale['th_num']+= $info['error_num'];
  1025. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  1026. $sale['status']=3;
  1027. }
  1028. $sale['th_fee']+= round($info['error_num']*$sale['sale_price'],2);
  1029. $sale['updatetime']= date("Y-m-d H:i:s");
  1030. $uap=Db::name("sale")->save($sale);
  1031. if($uap==false){
  1032. Db::rollback();
  1033. return error_show(1005,'销售单订单更新失败');
  1034. }
  1035. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  1036. if($ordernum==false){
  1037. Db::rollback();
  1038. return error_show(1005,'未找到关联采购单');
  1039. }
  1040. $ordernum['send_num']-=$info['error_num'];
  1041. $orderup =Db::name("order_num")->save($ordernum);
  1042. if($orderup==false){
  1043. Db::rollback();
  1044. return error_show(1005,'关联数据更新失败');
  1045. }
  1046. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  1047. if($cgd==false){
  1048. Db::rollback();
  1049. return error_show(1005,'未找到采购单数据');
  1050. }
  1051. $cgd['th_num']+= $info['error_num'];
  1052. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  1053. $cgd['status']=4;
  1054. }
  1055. $cgd['th_fee']+= round($info['error_num']*$cgd['good_price'],2);
  1056. $cgd['updatetime']= date("Y-m-d H:i:s");
  1057. $cgdup =Db::name("purchease_order")->save($cgd);
  1058. if($cgdup==false){
  1059. Db::rollback();
  1060. return error_show(1005,'采购单数据更新失败');
  1061. }
  1062. if($info['error_num']>=$out['send_num']){
  1063. $out['status']=4;
  1064. $out['updatetime']=date("Y-m-d H:i:s");
  1065. $upout =Db::name("order_out")->save($out);
  1066. if($upout==false){
  1067. Db::rollback();
  1068. return error_show(1005,'出库单数据更新失败');
  1069. }
  1070. }
  1071. $data=[
  1072. "orderCode"=>$info['orderCode'],
  1073. "th_type"=>3,
  1074. "th_num"=>$info['error_num'],
  1075. "th_fee"=>round($sale['sale_price']*$info["error_num"],2),
  1076. "thCode"=>$thNo,
  1077. "spuCode"=>$sale['good_code'],
  1078. "good_name"=>$sale['good_name'],
  1079. "cat_id"=>$sale['cat_id'],
  1080. "apply_id"=>$info['apply_id'],
  1081. "apply_name"=>$info['apply_name'],
  1082. "addtime"=>date("Y-m-d H:i:s"),
  1083. "status"=>1,
  1084. "is_del"=>0
  1085. ];
  1086. $inse=Db::name("th_data")->insert($data);
  1087. if($inse==false){
  1088. Db::rollback();
  1089. return error_show(1004,"退货单更新失败");
  1090. }
  1091. }
  1092. Db::commit();
  1093. return app_show(0,"退货单新建成功",["thNo"=>$thNo]);
  1094. }
  1095. }
  1096. }
  1097. Db::rollback();
  1098. return error_show(1004,'退货失败');
  1099. }catch (\Exception $e){
  1100. Db::rollback();
  1101. return error_show(1004,$e->getMessage());
  1102. }
  1103. }
  1104. //取消售后
  1105. public function Cancel()
  1106. {
  1107. //接收参数
  1108. $data = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1109. $val = Validate::rule(['returnCode|售后单编号' => 'require']);
  1110. if (!$val->check($data)) return error_show(1005, $val->getError());
  1111. Db::startTrans();
  1112. try {
  1113. $rs = Db::name('order_return')->field('id,status')->where([['returnCode', '=', $data['returnCode']], ['is_del', '=', 0], ['status', '<>', 8]])->find();
  1114. if (!$rs) throw new Exception('该售后单记录不存在');
  1115. //修改记录
  1116. $res = Db::name('order_return')->where(['id' => $rs['id']])->update(['status' => 8, 'updatetime' => date('Y-m-d H:i:s')]);
  1117. if (!$res) throw new Exception('该售后单记录修改失败');
  1118. //记录日志
  1119. ActionLog::logAdd($data['token'], ["order_code" => $data['returnCode'], "status" => $rs['status'], "action_remark" => '', "action_type" => "delete"], 'SHD', 8, $this->post);
  1120. ProcessOrder::AddProcess($this->post['token'], ["order_code" => $data['returnCode'], "order_id" => $rs['id'], "order_status" => 8, "order_type" => 'SHD',"before_status"=>$rs['status']]);
  1121. //提交
  1122. Db::commit();
  1123. return app_show(0, "取消成功");
  1124. } catch (\Exception $e) {
  1125. Db::rollback();
  1126. return error_show(1004, $e->getMessage());
  1127. }
  1128. }
  1129. /**
  1130. * @todo 售后功能新写的接口,暂存
  1131. * //根据审核完成的售后申请单,创建新的销售单和采购单
  1132. * public function newCreateAfter(){
  1133. *
  1134. * $param = $this->request->only(['token','returnCode'],'post','trim');
  1135. *
  1136. * $val = Validate::rule(['token'=>'require','returnCode|售后申请单编号'=>'require']);
  1137. *
  1138. * if(!$val->check($param)) return error_show(1005,$val->getError());
  1139. *
  1140. * //查询售后申请单详情
  1141. * $info = Db::name('order_return')
  1142. * ->where('returnCode',$param['returnCode'])
  1143. * ->where('is_del',0)
  1144. * ->find();
  1145. *
  1146. * if(empty($info)) return error_show(1005,'该售后申请单不存在');
  1147. *
  1148. * if($info['status']!=5) return error_show(1005,'该售后申请单尚未审核完成');
  1149. *
  1150. * }
  1151. *
  1152. **/
  1153. }