After.php 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\ProcessOrder;
  6. use think\App;
  7. use think\Exception;
  8. use think\facade\Db;
  9. use think\facade\Validate;
  10. //售后
  11. class After extends Base
  12. {
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. }
  17. public function create(){
  18. $outCode = isset($this->post['outCode']) &&$this->post['outCode']!=''?trim($this->post['outCode']) :"";
  19. if($outCode==''){
  20. return error_show(1004,"参数outCode 不能为空");
  21. }
  22. $orderout= Db::name("order_out")->where(["outCode"=>$outCode,'is_del'=>0])->find();
  23. if($orderout==false){
  24. return error_show(1005,"未找到订单数据");
  25. }
  26. $after_num =Db::name("order_return")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode])
  27. ->where("status","not in",[5,8])->sum("error_num");
  28. $th_num =Db::name("order_back")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode,"is_del"=>0])
  29. ->where("status","<",4)->sum("return_num");
  30. if($th_num!=0 || $after_num!=0){
  31. return error_show(1005,"存在售后或售后退货未结束流程");
  32. }
  33. $wsm =Db::name("warehouse_info")->where(["wsm_code"=>$orderout['wsm_code']])->find();
  34. if($wsm==false){
  35. return error_show(1005,"未找到仓库数据");
  36. }
  37. $oder= Db::name("sale")->where(["orderCode"=>$orderout['orderCode'],"is_del"=>0])->find();
  38. if(empty($oder)){
  39. return error_show(1005,"未找到订单数据");
  40. }
  41. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  42. if($outCode==''){
  43. return error_show(1004,"参数errorCode 不能为空");
  44. }
  45. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  46. if(empty($error)){
  47. return error_show(1005,"未找到退货原因数据");
  48. }
  49. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  50. $is_receive =isset($this->post['is_receive']) &&$this->post['is_receive']!==''?intval($this->post['is_receive'])
  51. :"";
  52. if($is_receive===""){
  53. return error_show(1004,"参数 is_receive 不能为空");
  54. }
  55. if($is_receive==0){
  56. $num=$orderout['send_num'];
  57. }else{
  58. $num =isset($this->post['num']) &&$this->post['num']!=''?intval($this->post['num']) :"";
  59. if($num==""){
  60. return error_show(1004,"参数 num 不能为空或零");
  61. }
  62. }
  63. $thnum =Db::name("order_back")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode,"is_del"=>0])
  64. ->where("status","=",4)->sum("return_num");
  65. if($num>$orderout['send_num']-$thnum){
  66. return error_show(1004,"可用售后数量不足");
  67. }
  68. $except_code =isset($this->post['except_code']) &&$this->post['except_code']!=''?trim($this->post['except_code']) :"";
  69. $img = isset($this->post['img']) &&$this->post['img']!=''?trim($this->post['img']) :"";
  70. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  71. if($token==''){
  72. return error_show(105,"参数token不能为空");
  73. }
  74. $user =GetUserInfo($token);
  75. if(empty($user)||$user['code']!=0){
  76. return error_show(102,"申请人数据不存在");
  77. }
  78. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  79. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  80. $returnCode=makeNo("RS");
  81. Db::startTrans();
  82. try{
  83. $in = [
  84. "returnCode"=>$returnCode,
  85. "outCode"=>$outCode,
  86. "orderCode"=> $oder['orderCode'],
  87. "good_code"=>$oder['good_code'],
  88. "good_name"=>$oder['good_name'],
  89. "customer_code"=>$oder['customer_code'],
  90. "apply_id"=>$rm,
  91. "apply_name"=>$ri,
  92. "cgder"=>$wsm['contactor_name'],
  93. "cgderid"=>$wsm['contactor'],
  94. 'total_num'=>$orderout['send_num'],
  95. "error_code"=>$errorCode,
  96. "error_num"=>$num,
  97. "error_img"=>$img,
  98. "error_msg"=>$error['result'],
  99. "error_remark"=>$remark,
  100. "is_receive"=>$is_receive,
  101. "remark"=>'',
  102. "except_code"=>$except_code,
  103. "order_type"=>$orderout['order_type'],
  104. "status"=>1,
  105. "is_del"=>0,
  106. "addtime"=>date("Y-m-d H:i:s"),
  107. "updatetime"=>date("Y-m-d H:i:s")
  108. ];
  109. $create = Db::name("order_return")->insert($in,true);
  110. $orde = ["order_code"=>$returnCode,"status"=>$in['status'],"action_remark"=>'',"action_type"=>"create"];
  111. ActionLog::logAdd($this->post['token'],$orde,'SHD',1,$orde);
  112. if($create>0){
  113. //维护台账
  114. // Db::name('standing_book')
  115. // ->whereFindInSet('outCode', $outCode)
  116. // ->update([
  117. // 'returnCode' => $returnCode,
  118. // 'updatetime' => date("Y-m-d H:i:s")
  119. // ]);
  120. Db::execute("UPDATE `wsm_standing_book` SET `returnCode`=CONCAT(`returnCode`,',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$outCode}',`outCode`)");
  121. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'SHD',"before_status"=>1];
  122. ProcessOrder::AddProcess($token,$process);
  123. Db::commit();
  124. return app_show(0,"售后申请单新建成功",["returnCode"=>$returnCode]);
  125. }
  126. Db::rollback();
  127. return error_show(1005,"售后申请单新建失败");
  128. }catch (\Exception $e){
  129. Db::rollback();
  130. return error_show(1005,$e->getMessage());
  131. }
  132. }
  133. public function list(){
  134. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  135. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  136. $where =[['a.is_del',"=",0]];
  137. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  138. if($bkcode!=""){
  139. $where[]=['a.returnCode',"like", "%{$bkcode}%"];
  140. }
  141. $out = isset($this->post['outCode']) && $this->post['outCode']!="" ? trim($this->post['outCode']):"";
  142. if($out!=""){
  143. $where[]=['a.outCode',"like", "%{$out}%"];
  144. }
  145. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  146. if($status!==""){
  147. $where[]=['a.status',"=", $status];
  148. }
  149. $order_type = isset($this->post['order_type']) && $this->post['order_type']!=="" ? intval($this->post['order_type']):"";
  150. if($order_type!==""){
  151. $where[]=['a.order_type',"=", $order_type];
  152. }
  153. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode']) :"";
  154. if($orderCode!=""){
  155. $where[]=['a.orderCode',"like", "%{$orderCode}%"];
  156. }
  157. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code'])
  158. :"";
  159. if($good_code!=""){
  160. $where[]=['a.good_code',"like", "%{$good_code}%"];
  161. }
  162. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name'])
  163. :"";
  164. if($good_name!=""){
  165. $where[]=['a.good_name',"like", "%{$good_name}%"];
  166. }
  167. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code']!="" ? trim($this->post['customer_code'])
  168. :"";
  169. if($customer_code!=""){
  170. $where[]=['a.customer_code',"like", "%{$customer_code}%"];
  171. }
  172. $except_code = isset($this->post['except_code']) && $this->post['except_code']!="" ? trim($this->post['except_code']):"";
  173. if($except_code!=""){
  174. $where[]=['a.except_code',"=", $except_code];
  175. }
  176. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  177. :"";
  178. if($apply_name!=""){
  179. $where[]=['a.apply_name',"like", "%{$apply_name}%"];
  180. }
  181. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  182. if($start!==""){
  183. $where[]=['a.addtime',">=", $start];
  184. }
  185. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  186. if($end!==""){
  187. $where[]=['a.addtime',"<=", $end];
  188. }
  189. $role=$this->checkRole();
  190. $condition='';
  191. if(!empty($role['write']) && $this->uid!=""){
  192. // $where[]=["a.apply_id","in",$role['write']];
  193. $condition .="a.cgderid = {$this->uid} or a.apply_id in (".implode(',',$role['write']).")";
  194. }
  195. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  196. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  197. $count=Db::name("order_return")->alias("a")->leftJoin("order_returninfo b","a.returnCode=b.returnCode")->where
  198. ($where)->where($condition)->count();
  199. $total = ceil($count/$size);
  200. $page = $total>=$page ? $page :$total;
  201. $list =Db::name("order_return")
  202. ->alias("a")
  203. ->leftJoin("order_returninfo b","a.returnCode=b.returnCode")
  204. ->where($where)
  205. ->where($condition)
  206. ->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")
  207. ->page($page,$size)
  208. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  209. ->order("addtime desc")
  210. ->cursor();
  211. $data=[];
  212. foreach ($list as $value){
  213. $customer = Db::name("customer_info")->where(["companyNo"=>$value['customer_code']])->find();
  214. $value['customer_name'] = isset($customer['companyName']) ?trim($customer['companyName']):'';
  215. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  216. $data[]=$value;
  217. }
  218. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  219. }
  220. /**
  221. * @return \think\response\Json|void
  222. * @throws \think\db\exception\DataNotFoundException
  223. * @throws \think\db\exception\DbException
  224. * @throws \think\db\exception\ModelNotFoundException
  225. */
  226. public function info()
  227. {
  228. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode'] != "" ? trim($this->post['returnCode']) : "";
  229. if ($bkcode == "") {
  230. return error_show(1005, "参数returnCode 不能为空");
  231. }
  232. $info = Db::name("order_return")->alias("a")->leftJoin("order_returninfo b", "a.returnCode=b.returnCode")
  233. ->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")
  234. ->where(['a.returnCode' => $bkcode])->find();
  235. if (empty($info)) {
  236. return error_show(1005, "未找到售后数据");
  237. }
  238. $orderinfo = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  239. $info['origin_price'] = $orderinfo['origin_price'];
  240. $info['order_type'] = $orderinfo['order_type'];
  241. $info['sale_price'] = $orderinfo['sale_price'];
  242. $info['total_price'] = sprintf("%1\$.2f", $orderinfo['sale_price'] * $info['error_num']);
  243. if ($orderinfo['order_type'] == 3) {
  244. $goon = Db::name("good_zixun")->where(["spuCode" => $orderinfo['good_code'], "is_del" => 0])->find();
  245. } else {
  246. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  247. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  248. }
  249. if (empty($goon)) {
  250. return error_show(1003, "未找到商品数据");
  251. }
  252. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  253. $addcode = '';
  254. if ($info['addr_code'] != "") {
  255. list($a, $b, $c) = explode(",", $info['addr_code']);
  256. $addcode = json_encode(["provice_code" => $a, "city_code" => $b, "area_code" => $c]);
  257. }
  258. $info["addr_cn"]=GetAddr($addcode);
  259. // $info["addr_code"]=json_decode($addcode,true);
  260. $out =Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  261. $info['out_num'] = isset($out['send_num'])?$out['send_num']:'';
  262. // $customer = Db::name("customer_info")->where(["companyNo"=>$info['customer_code']])->find();
  263. $wsmcode=[];
  264. if($info['return_wsm']!=''){
  265. $wsmcode = Db::name("warehouse_info")
  266. ->alias("a")
  267. ->leftJoin("supplier b","a.supplierNo=b.code")
  268. ->where(["a.wsm_code"=>$info['return_wsm']])
  269. ->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")
  270. ->find();
  271. }
  272. $info['wsm_code'] =isset($wsmcode['wsm_code']) ? $wsmcode['wsm_code']:"";
  273. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  274. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  275. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  276. $customer=[];
  277. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  278. $customer = Db::name("business")->field('id,company,companyNo')->where(["companyNo"=>$orderinfo['supplierNo']])->find();
  279. }
  280. $info['customer_name'] = isset($customer['company']) ?trim($customer['company']):'';
  281. $info['customer_code'] = isset($customer['companyNo']) ?trim($customer['companyNo']):'';
  282. $info['can']=$int;
  283. //仓库的联系人、职位、电话、省市区和地址
  284. isset($wsmcode['contactor_name'])? $info['contactor'] =$wsmcode['contactor_name']:'';//联系人
  285. isset($wsmcode['mobile'])? $info['mobile'] =$wsmcode['mobile']:'';//联系人
  286. isset($wsmcode['addrs_code'])? $info['addr_code'] =$wsmcode['addrs_code']:'';//联系人
  287. isset($wsmcode['addr'])? $info['addr'] =$wsmcode['addr']:'';//联系人
  288. isset($wsmcode['contactor_name'])? $info['contactor'] =$wsmcode['contactor_name']:'';//联系人
  289. // $info['position'] = $wsmcode['position'];//职位
  290. // $info['mobile'] = $wsmcode['mobile'];//电话
  291. // $info['addrs_code'] = json_decode($wsmcode['addrs_code'], true);//省市区编码
  292. // $info['addr'] = $wsmcode['addr'];//地址
  293. $info['spuCode'] = $orderinfo['good_code'];//地址
  294. $info['skuCode'] = $orderinfo['skuCode'];//地址
  295. $info['return_tag'] = $info['return_tag']==0?"":$info['return_tag'];//地址
  296. $info['is_th'] = $info['is_th']==0?"":$info['is_th'];//地址
  297. return app_show(0,"获取成功",$info);
  298. }
  299. public function status(){
  300. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  301. if($bkcode==""){
  302. return error_show(1005,"参数returnCode 不能为空");
  303. }
  304. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  305. if(empty($info)){
  306. return error_show(1005,"未找到售后数据");
  307. }
  308. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status'])
  309. :"";
  310. if($status===""){
  311. return error_show(1005,"参数status 不能为空");
  312. }
  313. $orde= Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  314. if($orde==false){
  315. return error_show(1005,"未找到出库单数据");
  316. }
  317. $sale = Db::name("sale")->where(["orderCode"=> $info['orderCode']])->find();
  318. if($sale==false){
  319. return error_show(1005,"未找到销售单数据");
  320. }
  321. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  322. if($remark!=''){
  323. $info['remark'] =$remark;
  324. }
  325. if($status==3 && $info['is_receive']==1){
  326. $post =isset($this->post['is_post'])&&$this->post['is_post']!==""? intval($this->post['is_post']):"";
  327. if($post===""){
  328. return error_show(1005,"参数is_post不能为空");
  329. }
  330. $info['is_post']=$post;
  331. $return_tag =isset($this->post['return_tag'])&&$this->post['return_tag']!==""? intval($this->post['return_tag']):"";
  332. if($return_tag===""){
  333. return error_show(1005,"参数return_tag不能为空");
  334. }
  335. $info['return_tag']=$return_tag;
  336. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!==""? intval($this->post['is_th']):"0";
  337. $info['is_th']=$is_th;
  338. }
  339. // $status = $info['order_status']==1&&$info['order_type']==1 && $status==1?4:$status;
  340. Db::startTrans();
  341. try{
  342. $var=$info['status'];
  343. $info['status']=$status;
  344. $info['updatetime']=date("Y-m-d H:i:s");
  345. $up =Db::name("order_return")->save($info);
  346. if($up){
  347. if($status==5 && $info['is_receive']==0){
  348. $od_status = $orde['status'];
  349. $orde['status']=4;
  350. $orde['updatetime']=date("Y-m-d H:i:s");
  351. $out = Db::name("order_out")->save($orde);
  352. if($out==false){
  353. Db::rollback();
  354. return error_show(1004,"发货单更新失败");
  355. }
  356. $order = ["order_code"=>$orde['outCode'],"status"=>$od_status,"action_remark"=>'',"action_type"=>"edit"];
  357. ActionLog::logAdd($this->post['token'],$order,"CKD", $orde['status'],$this->post);
  358. $process=["order_code"=>$orde['outCode'],"order_id"=>$orde['id'],"order_status"=>$orde['status'],"order_type"=>"CKD","before_status"=>$od_status];
  359. ProcessOrder::AddProcess($this->post['token'],$process);
  360. $sale['th_num'] += $info['error_num'];
  361. $sale['th_fee'] += round($info['error_num']*$sale['sale_price'],2);
  362. $sale['updatetime']=date("Y-m-d H:i:s");
  363. $sup =Db::name("sale")->save($sale);
  364. if($sup==false){
  365. Db::rollback();
  366. return error_show(1004,"销售单更新失败");
  367. }
  368. $data=[
  369. "orderCode"=>$info['orderCode'],
  370. "th_type"=>2,
  371. "th_num"=>$info['error_num'],
  372. "th_fee"=>round($info['error_num']*$sale['sale_price'],2),
  373. "thCode"=>$info['returnCode'],
  374. "spuCode"=>$sale['good_code'],
  375. "good_name"=>$sale['good_name'],
  376. "cat_id"=>$sale['cat_id'],
  377. "apply_id"=>$info['apply_id'],
  378. "apply_name"=>$info['apply_name'],
  379. "addtime"=>date("Y-m-d H:i:s"),
  380. "status"=>1,
  381. "is_del"=>0
  382. ];
  383. $inse=Db::name("th_data")->insert($data);
  384. if($inse==false){
  385. Db::rollback();
  386. return error_show(1004,"退回单更新失败");
  387. }
  388. }
  389. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  390. ActionLog::logAdd($this->post['token'],$order,"SHD", $info['status'],$this->post);
  391. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$status,"order_type"=>"SHD","before_status"=>$var];
  392. ProcessOrder::AddProcess($this->post['token'],$process);
  393. Db::commit();
  394. return app_show(0,"更新成功");
  395. }else{
  396. Db::rollback();
  397. return error_show(1004,"更新失败");
  398. }
  399. }catch (\Exception $e){
  400. Db::rollback();
  401. return error_show(1004,$e->getMessage());
  402. }
  403. }
  404. public function setWsm(){
  405. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  406. if($bkcode==""){
  407. return error_show(1005,"参数returnCode 不能为空");
  408. }
  409. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  410. if(empty($info)){
  411. return error_show(1005,"未找到售后数据");
  412. }
  413. if($info['status']!=3){
  414. return error_show(1005,"采购主管未审核无法设置退回仓库");
  415. }
  416. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']) :"";
  417. if($info['return_tag']==2) {
  418. if($wsm_code==""){
  419. return error_show(1005,"参数 wsm_code 不能为空");
  420. }
  421. $warehouse =Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code])->find();
  422. if($warehouse==false){
  423. return error_show(1005,"仓库数据未找到");
  424. }
  425. $wareaddr =Db::name("warehouse_addr")->where(["wsm_code"=>$wsm_code,"is_del"=>0,"status"=>1])->find();
  426. }
  427. $addrcode=isset($this->post['addr_code'])&&$this->post['addr_code']!=""?$this->post['addr_code']:"";
  428. if($addrcode==""){
  429. return error_show(1005,"参数 addr_code 不能为空");
  430. }
  431. $addr=isset($this->post['addr'])&&$this->post['addr']!=""?trim($this->post['addr']):"";
  432. if($addr==""){
  433. return error_show(1005,"参数 addr 不能为空");
  434. }
  435. $mobile=isset($this->post['mobile'])&&$this->post['mobile']!=""?trim($this->post['mobile']):"";
  436. if($mobile==""){
  437. return error_show(1005,"参数 mobile 不能为空");
  438. }
  439. $contactor=isset($this->post['contactor'])&&$this->post['contactor']!=""?trim($this->post['contactor']):"";
  440. if($contactor==""){
  441. return error_show(1005,"参数 contactor 不能为空");
  442. }
  443. $returninfo=[
  444. "returnCode"=>$bkcode,
  445. "return_wsm"=>$wsm_code,
  446. "contactor"=>$contactor,
  447. "mobile"=>$mobile ,
  448. "addr"=>$addr,
  449. "addr_code"=>$addrcode,
  450. "addtime"=>date("Y-m-d H:i:s")
  451. ];
  452. Db::startTrans();
  453. try {
  454. $in=Db::name("order_returninfo")->insert($returninfo);
  455. if($in){
  456. if($info['return_tag']==2 &&$wareaddr==false) {
  457. $addrs =[
  458. "wsm_code"=>$wsm_code,
  459. "wsm_type"=>1,
  460. "wsm_name"=>$contactor,
  461. "wsm_addr"=>$addr,
  462. "addr_code"=>$addrcode,
  463. "wsm_mobile"=>$mobile,
  464. "status"=>'1',
  465. "is_del"=>'0',
  466. "addtime"=>date("Y-m-d H:i:s"),
  467. "updatetime"=>date("Y-m-d H:i:s")
  468. ];
  469. $wareaddr =Db::name("warehouse_addr")->insert($addrs);
  470. if($wareaddr==false){
  471. Db::rollback();
  472. return app_show(1004,"退货仓库地址录入失败");
  473. }
  474. }
  475. $old_info_status = $info['status'];
  476. $info['status']=4;
  477. $info["updatetime"]=date("Y-m-d H:i:s");
  478. $up =Db::name("order_return")->save($info);
  479. if($up){
  480. //修改状态,添加待办
  481. ActionLog::logAdd($this->post['token'], [
  482. "order_code" => $info['returnCode'],//销售单code
  483. "status" => $old_info_status,//这里的status是之前的值
  484. "action_remark" => '',//备注
  485. "action_type" => "status"//新建create,编辑edit,更改状态status
  486. ], "SHD", $info['status'], $info);
  487. ProcessOrder::AddProcess($this->post['token'], [
  488. "order_type" => 'SHD',
  489. "order_code" => $info['returnCode'],//销售单code
  490. "order_id" => $info['id'],
  491. "order_status" => $info['status'],"before_status"=>$old_info_status
  492. ]);
  493. Db::commit();
  494. return app_show(0,"退货仓库设置成功");
  495. }
  496. }
  497. Db::rollback();
  498. return error_show(1004,"设置失败");
  499. }catch (\Exception $e){
  500. Db::rollback();
  501. return error_show(1004,$e->getMessage());
  502. }
  503. }
  504. /**
  505. * @return \think\response\Json|void
  506. * @throws \think\db\exception\DataNotFoundException
  507. * @throws \think\db\exception\DbException
  508. * @throws \think\db\exception\ModelNotFoundException
  509. */
  510. public function postfeed(){
  511. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  512. if($bkcode==""){
  513. return error_show(1005,"参数returnCode 不能为空");
  514. }
  515. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  516. if(empty($info)){
  517. return error_show(1005,"未找到售后数据");
  518. }
  519. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor']) :"";
  520. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  521. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  522. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ?
  523. $this->post['addr_code']:"";
  524. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  525. if($wsm_code==""){
  526. return error_show(1005,"参数 wsm_code不能为空");
  527. }
  528. if($info['is_th']==0&&$info['order_type']==1) {
  529. $wsm = Db::name("warehouse_info")->where(["wsm_code" => $wsm_code])->find();
  530. if (empty($wsm)) {
  531. return error_show(1005, "未找到仓库数据");
  532. }
  533. $sm = Db::name("warehouse_addr")->where(["wsm_code" => $wsm_code,"is_del"=>0])->find();
  534. $contactor==""?(isset($sm['wsm_name']) ? $contactor=$sm['wsm_name']:""):"";
  535. $sm['wsm_name'] =isset($sm['wsm_name']) ? $sm['wsm_name']:"";
  536. $mobile==""?(isset($sm['wsm_mobile']) ? $mobile=$sm['wsm_mobile']:""):"";
  537. $sm['wsm_mobile'] =isset($sm['wsm_mobile']) ? $sm['wsm_mobile']:"";
  538. $addr==""?(isset($sm['wsm_addr']) ? $addr=$sm['wsm_addr']:""):"";
  539. $sm['wsm_addr'] =isset($sm['wsm_addr']) ? $sm['wsm_addr']:"";
  540. $addr_code==""?(isset($sm['addr_code']) ? $addr=$sm['addr_code']:""):"";
  541. $sm['addr_code'] =isset($sm['addr_code']) ? $sm['addr_code']:"";
  542. }
  543. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  544. if($info['is_th']==1 && $info['order_type']==1){
  545. if($contactor==''){
  546. return error_show(1005,"参数 contactor 不能为空");
  547. }
  548. if($mobile==''){
  549. return error_show(1005,"参数 mobile 不能为空");
  550. }
  551. if($addr==''){
  552. return error_show(1005,"参数 addr 不能为空");
  553. }
  554. if($addr_code==''){
  555. return error_show(1005,"参数 addr_code 不能为空");
  556. }
  557. if($addr_code!==''&&is_array($addr_code)){
  558. $addrs=[];
  559. $addrs['provice_code'] = $addr_code[0];
  560. $addrs['city_code'] = $addr_code[1];
  561. $addrs['area_code'] = $addr_code[2];
  562. $addr_code = json_encode($addrs);
  563. }
  564. }
  565. Db::startTrans();
  566. try{
  567. $var=$info['status'];
  568. $info['status']=$info['order_type']==1?3:4;
  569. $info['remark']=$remark;
  570. $info['updatetime']=date("Y-m-d H:i:s");
  571. $up =Db::name("order_return")->save($info);
  572. if($up){
  573. // var_dump($contactor);
  574. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  575. if(empty($returninfo)){
  576. $returninfo=[
  577. "returnCode"=>$bkcode,
  578. "return_wsm"=>$wsm_code,
  579. "contactor"=>$contactor,
  580. "mobile"=>$mobile ,
  581. "addr"=>$addr,
  582. "addr_code"=>$addr_code,
  583. "addtime"=>date("Y-m-d H:i:s")
  584. ];
  585. }else{
  586. $returninfo['return_wsm'] = $wsm_code;
  587. //if($info['is_th']==1 && $info['order_type']==1) {
  588. $contactor !==""? $returninfo['contactor'] = $contactor:"";
  589. $mobile!==""?$returninfo['mobile'] = $mobile:"";
  590. $addr!==""? $returninfo['addr'] = $addr:"";
  591. $addr_code!==""?$returninfo['addr_code'] = $addr_code:"";
  592. // }
  593. }
  594. // var_dump($contactor);
  595. // var_dump($returninfo);
  596. $in=Db::name("order_returninfo")->save($returninfo);
  597. if($in){
  598. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  599. ActionLog::logAdd($this->post['token'],$order,"SHD",$info['status'],$this->post);
  600. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$var];
  601. ProcessOrder::AddProcess($this->post['token'],$process);
  602. Db::commit();
  603. return app_show(0,"更新成功");
  604. }
  605. }
  606. Db::rollback();
  607. return error_show(1004,"更新失败");
  608. }catch (\Exception $e){
  609. Db::rollback();
  610. return error_show(1004,$e->getMessage());
  611. }
  612. }
  613. /**
  614. * @return \think\response\Json|void
  615. * @throws \think\db\exception\DataNotFoundException
  616. * @throws \think\db\exception\DbException
  617. * @throws \think\db\exception\ModelNotFoundException
  618. */
  619. public function addpost(){
  620. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  621. if($bkcode==""){
  622. return error_show(1005,"参数returnCode 不能为空");
  623. }
  624. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  625. if(empty($info)){
  626. return error_show(1005,"未找到售后数据");
  627. }
  628. $return_info = Db::name("order_returninfo")->where(['returnCode'=>$bkcode])->find();
  629. if(empty($return_info)){
  630. $return_info=[
  631. "returnCode"=>$bkcode,
  632. "return_wsm"=>'',
  633. "contactor"=>'',
  634. "mobile"=>'',
  635. "addr"=>'',
  636. "addr_code"=>'',
  637. "addtime"=>date("Y-m-d H:i:s"),
  638. "post_company"=>'',
  639. "post_code"=>'',
  640. "post_fee"=>'',
  641. ];
  642. }
  643. $post_own = isset($this->post['own']) &&$this->post['own']!='' ? trim($this->post['own']):"";
  644. if($post_own==''){
  645. return error_show(1005,"参数own不能为空");
  646. }
  647. $info['post_own'] = $post_own;
  648. $post_company = isset($this->post['company'])&&$this->post['company']!='' ? trim($this->post['company']):"";
  649. if($post_company==''){
  650. return error_show(1005,"参数company不能为空");
  651. }
  652. $return_info['post_company'] = $post_company;
  653. $post_code = isset($this->post['post_code'])&&$this->post['post_code']!='' ? trim($this->post['post_code']):"";
  654. if($post_code==''){
  655. return error_show(1005,"参数post_code不能为空");
  656. }
  657. $return_info['post_code'] = $post_code;
  658. $fee= isset($this->post['post_fee'])&&$this->post['post_fee']!=='' ? round($this->post['post_fee'],2):"";
  659. if($fee===''){
  660. return error_show(1005,"参数post_fee不能为空");
  661. }
  662. $return_info['post_fee'] = $fee;
  663. Db::startTrans();
  664. try{
  665. $str=$info['status'];
  666. $info['status']=$info['order_type']==1?4:5;
  667. $info['updatetime']=date("Y-m-d H:i:s");
  668. $up =Db::name("order_return")->save($info);
  669. if($up){
  670. $order = ["order_code"=>$info['orderCode'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  671. ActionLog::logAdd($this->post['token'],$order,"SHD",$info['status'],$this->post);
  672. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$str];
  673. ProcessOrder::AddProcess($this->post['token'],$process);
  674. $in=Db::name("order_returninfo")->save($return_info);
  675. if($in){
  676. $data=[
  677. 'thNo'=>makeNo("ST"),
  678. "orderCode"=>$info['orderCode'],
  679. "order_type"=>$info['order_type'],
  680. "outCode"=>$info['outCode'],
  681. "returnCode"=>$info['returnCode'],
  682. "good_code"=>$info['good_code'],
  683. "good_name"=>$info['good_name'],
  684. "return_wsm"=>$return_info['return_wsm'],
  685. "return_num"=>$info['error_num'],
  686. "contactor"=>$return_info['contactor'],
  687. "mobile"=>$return_info['mobile'],
  688. "addr"=>$return_info['addr'],
  689. "addr_code"=>$return_info['addr_code'],
  690. "return_code"=>$info['error_code'],
  691. "return_msg"=>$info['error_msg'],
  692. "post_fee"=>$return_info['post_fee'],
  693. "post_code"=>$return_info['post_code'],
  694. "post_company"=>$return_info['post_company'],
  695. "customer_code"=>$info['customer_code'],
  696. "addtime"=>date("Y-m-d H:i:s"),
  697. "updatetime"=>date("Y-m-d H:i:s")
  698. ];
  699. $back =Db::name("order_back")->insert($data,true);
  700. if($back>0){
  701. //维护台账
  702. // Db::name('standing_book')
  703. // ->where('returnCode', $bkcode)
  704. // ->update([
  705. // 'thNo' => $data['thNo'],
  706. // 'updatetime' => date("Y-m-d H:i:s")
  707. // ]);
  708. Db::execute("UPDATE `wsm_standing_book` SET `thNo`=CONCAT(`thNo`,',{$data['thNo']}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$bkcode}',`returnCode`)");//这里的returnCode可能有多个
  709. $order = ["order_code"=>$data['thNo'],"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  710. ActionLog::logAdd($this->post['token'],$order,"CKTHD", 0,$data);
  711. $process=["order_code"=>$data['thNo'],"order_id"=>$back,"order_status"=>0,"order_type"=>"CKTHD","before_status"=>0];
  712. ProcessOrder::AddProcess($this->post['token'],$process);
  713. Db::commit();
  714. return app_show(0,"更新成功");
  715. }
  716. }
  717. }
  718. Db::rollback();
  719. return error_show(1004,"更新失败");
  720. }catch (\Exception $e){
  721. Db::rollback();
  722. return error_show(1004,$e->getMessage());
  723. }
  724. }
  725. /**
  726. * @return \think\response\Json|void
  727. * @throws \think\db\exception\DataNotFoundException
  728. * @throws \think\db\exception\DbException
  729. * @throws \think\db\exception\ModelNotFoundException
  730. */
  731. public function GysFeed(){
  732. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  733. if($bkcode==""){
  734. return error_show(1005,"参数returnCode 不能为空");
  735. }
  736. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  737. if(empty($info)){
  738. return error_show(1005,"未找到售后数据");
  739. }
  740. $isth = isset($this->post['is_th']) && $this->post['is_th']!="" ? intval($this->post['is_th']):"0";
  741. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor'])
  742. :"";
  743. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  744. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  745. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ? $this->post['addr_code']:"";
  746. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  747. if($isth==1){
  748. if($contactor==''){
  749. return error_show(1005,"参数 contactor 不能为空");
  750. }
  751. if($mobile==''){
  752. return error_show(1005,"参数 mobile 不能为空");
  753. }
  754. if($addr==''){
  755. return error_show(1005,"参数 addr 不能为空");
  756. }
  757. // var_dump(isset($this->post['addr_code']) , $this->post['addr_code']!="",!empty($addr_code));
  758. if($addr_code==''){
  759. return error_show(1005,"参数 addr_code 不能为空");
  760. }
  761. if(is_array($addr_code)){
  762. $addrs=[];
  763. $addrs['provice_code'] = $addr_code[0];
  764. $addrs['city_code'] = $addr_code[1];
  765. $addrs['area_code'] = $addr_code[2];
  766. $addr_code = json_encode($addrs);
  767. }
  768. }
  769. $orde= Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  770. if(empty($orde)){
  771. return error_show(1005,"未找到出库单数据");
  772. }
  773. Db::startTrans();
  774. try{
  775. $stat=$info['status'];
  776. $info['status']=$info['order_status']==1?5:3;
  777. $info['is_th']=$isth;
  778. $info['updatetime']=date("Y-m-d H:i:s");
  779. $up =Db::name("order_return")->save($info);
  780. if($up){
  781. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  782. if(empty($returninfo)){
  783. $returninfo=[
  784. "returnCode"=>$bkcode,
  785. "return_wsm"=>'',
  786. "contactor"=>$contactor,
  787. "mobile"=>$mobile ,
  788. "addr"=>$addr,
  789. "addr_code"=>$addr_code,
  790. "gys_remark"=>$remark,
  791. "addtime"=>date("Y-m-d H:i:s")
  792. ];
  793. }else{
  794. $returninfo['return_wsm'] = '';
  795. $returninfo['contactor'] = $contactor;
  796. $returninfo['mobile'] = $mobile;
  797. $returninfo['addr'] = $addr;
  798. $returninfo['gys_remark'] = $remark;
  799. $returninfo['addr_code'] = $addr_code;
  800. }
  801. $in=Db::name("order_returninfo")->save($returninfo);
  802. if($in){
  803. $order = ["order_code"=>$info['returnCode'],"status"=>$stat,"action_remark"=>'',"action_type"=>"edit"];
  804. ActionLog::logAdd($this->post['token'],$order,"SHD", $info['status'],$this->post);
  805. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"SHD","before_status"=>$stat];
  806. ProcessOrder::AddProcess($this->post['token'],$process);
  807. Db::commit();
  808. return app_show(0,"更新成功");
  809. }
  810. }
  811. Db::rollback();
  812. return error_show(1004,"更新失败");
  813. }catch (\Exception $e){
  814. Db::rollback();
  815. return error_show(1004,$e->getMessage());
  816. }
  817. }
  818. public function GetWsm(){
  819. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  820. if($bkcode==""){
  821. return error_show(1005,"参数returnCode 不能为空");
  822. }
  823. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  824. if($info==false){
  825. return error_show(1005,"未找到售后数据");
  826. }
  827. $list=[];
  828. if($info['is_th']==1){
  829. if($info["order_type"]==1){
  830. $out =Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  831. if(empty($out)){
  832. return error_show(1005,"未找到出库单数据");
  833. }
  834. $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")
  835. ->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();
  836. if(empty($ware)){
  837. return error_show(1005,"未找到仓库数据");
  838. }
  839. $temp['wsm_code']=isset($ware['wsm_code'])?$ware['wsm_code']:"";
  840. $temp['wsm_name']=isset($ware['name'])?$ware['name']:"";
  841. $temp['contactor'] = isset($ware['wsm_name'])?$ware['wsm_name']:"";
  842. $temp['mobile'] =isset($ware['wsm_mobile'])?$ware['wsm_mobile']:"";
  843. $temp['addr'] =isset($ware['wsm_addr'])?$ware['wsm_addr']:"";
  844. $temp['addr_code'] = isset($ware['addr_code'])?$ware['addr_code']:"";
  845. $temp['addr_cn'] =GetAddr( $ware['addr_code']);
  846. $list[]=$temp;
  847. }else {
  848. $returninfo = Db::name("order_returninfo")->where(["returnCode" => $bkcode])->find();
  849. if (empty($returninfo)) {
  850. return error_show(1005, "未找到供应商仓库数据");
  851. }
  852. $data = [];
  853. $data['wsm_code'] = isset($returninfo['return_wsm']) && $returninfo['return_wsm'] != "" ? $returninfo['return_wsm'] : "-";
  854. $data['wsm_name'] = '供应商仓';
  855. $data['supplier_name'] = '供应商';
  856. $data['contactor'] = $returninfo['contactor'];
  857. $data['mobile'] = $returninfo['mobile'];
  858. $data['addr'] = $returninfo['addr'];
  859. $data['addr_cn'] =GetAddr( $returninfo['addr_code']);
  860. $data['addr_code'] = $returninfo['addr_code'];
  861. $list[] = $data;
  862. }
  863. }
  864. $supplier = Db::name("supplier")->where(["is_platform"=>1,"status"=>1,"is_del"=>0])->column("code,name");
  865. if(empty($supplier)){
  866. return error_show(1005,"未找到供应商仓库数据");
  867. }
  868. foreach ($supplier as $value){
  869. $temp=[];
  870. $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")
  871. ->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();
  872. if(empty($ware)){
  873. continue;
  874. }
  875. $temp['wsm_code']=isset($ware['wsm_code'])?$ware['wsm_code']:"";
  876. $temp['wsm_name']=isset($ware['name'])?$ware['name']:"";
  877. $temp['supplier_name']=$value['name'];
  878. $temp['contactor'] = isset($ware['wsm_name'])?$ware['wsm_name']:"";
  879. $temp['mobile'] =isset($ware['wsm_mobile'])?$ware['wsm_mobile']:"";
  880. $temp['addr'] =isset($ware['wsm_addr'])?$ware['wsm_addr']:"";
  881. $temp['addr_cn'] =GetAddr($ware['addr_code']);
  882. $temp['addr_code'] = isset($ware['addr_code'])?$ware['addr_code']:"";
  883. $list[]=$temp;
  884. }
  885. return app_show(0,"获取成功",$list);
  886. }
  887. public function setdelivery(){
  888. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  889. if($bkcode==""){
  890. return error_show(1005,"参数returnCode 不能为空");
  891. }
  892. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  893. if(empty($info)){
  894. return error_show(1005,"未找到售后数据");
  895. }
  896. if(($info['is_receive']==0&&$info['status']!=3)||($info['is_receive']==1&&$info['status']!=4) ){
  897. return error_show(1005,"售后单流程状态有误");
  898. }
  899. $out=Db::name("order_out")->where(["outCode"=>$info["outCode"],"is_del"=>0])->find();
  900. if($out==false){
  901. return error_show(1005,"未找到发货单数据");
  902. }
  903. $is_th = isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  904. if($is_th===""){
  905. return error_show(1005,"参数is_th不能为空");
  906. }
  907. $company=isset($this->post['company'])&&$this->post['company']!=""?trim($this->post['company']):"";
  908. $post_code=isset($this->post['post_code'])&&$this->post['post_code']!=""?trim($this->post['post_code']):"";
  909. $post_fee=isset($this->post['post_fee'])&&$this->post['post_fee']!==""?floatval($this->post['post_fee']):"";
  910. if($is_th==1){
  911. if($company==""){
  912. return error_show(1005,"参数company不能为空");
  913. }
  914. if($post_code==""){
  915. return error_show(1005,"参数post_code不能为空");
  916. }
  917. if($post_fee===""){
  918. return error_show(1005,"参数post_fee不能为空");
  919. }
  920. }
  921. $sale= Db::name('sale')->where(['orderCode'=>$info['orderCode'],"is_del"=>0])->find();
  922. if($sale==false){
  923. Db::rollback();
  924. return error_show(1004,"未找到销售单数据");
  925. }
  926. Db::startTrans();
  927. try {
  928. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  929. if($returninfo==false){
  930. $returninfo=[
  931. "returnCode"=>$bkcode,
  932. "return_wsm"=>"",
  933. "contactor"=>"",
  934. "mobile"=>"",
  935. "addr"=>"",
  936. "addr_code"=>"",
  937. "post_code"=>$post_code,
  938. "post_company"=>$company,
  939. "post_fee"=>$post_fee,
  940. "gys_remark"=>"",
  941. "addtime"=>date("Y-m-d H:i:s")
  942. ];
  943. }else{
  944. $returninfo['post_fee']=$post_fee;
  945. $returninfo['post_company']=$company;
  946. $returninfo['post_code']=$post_code;
  947. }
  948. $up =Db::name("order_returninfo")->save($returninfo);
  949. if($up){
  950. $old_info_status = $info['status'];
  951. $info['status']=5;
  952. $info['updatetime']=date("y-m-d H:i:s");
  953. $ro =Db::name("order_return")->save($info);
  954. if($ro){
  955. //修改状态,添加待办
  956. ActionLog::logAdd($this->post['token'], [
  957. "order_code" => $info['returnCode'],//销售单code
  958. "status" => $old_info_status,//这里的status是之前的值
  959. "action_remark" => '',//备注
  960. "action_type" => "status"//新建create,编辑edit,更改状态status
  961. ], "SHD", $info['status'], $this->post);
  962. ProcessOrder::AddProcess($this->post['token'], [
  963. "order_type" => 'SHD',
  964. "order_code" => $info['returnCode'],//销售单code
  965. "order_id" => $info['id'],
  966. "order_status" => $info['status'],"before_status"=>$old_info_status
  967. ]);
  968. $thNo =makeNo("ST");
  969. $thdata=[
  970. "thNo"=>$thNo,
  971. "orderCode"=>$out["orderCode"],
  972. "outCode"=>$out["outCode"],
  973. "order_type"=>$info["order_type"],
  974. "returnCode"=>$info["returnCode"],
  975. "good_code"=>$info["good_code"],
  976. "good_name"=>$info["good_name"],
  977. "return_wsm"=>$returninfo["return_wsm"],
  978. "total_fee"=>round($sale['sale_price']*$info["error_num"],2),
  979. "good_price"=>$sale['sale_price'],
  980. "platform_id"=>$sale['platform_id'],
  981. "return_msg"=>$info["error_msg"],
  982. "return_num"=>$info["error_num"],
  983. "normal_num"=>0,
  984. "received_num"=>0,
  985. "apply_id"=>$info['apply_id'],
  986. "apply_name"=>$info['apply_name'],
  987. "cgder"=>$info['cgder'],
  988. "cgderid"=>$info['cgderid'],
  989. "contactor"=>$returninfo["contactor"],
  990. "mobile"=>$returninfo["mobile"],
  991. "addr"=>$returninfo["addr"],
  992. "addr_code"=>$returninfo["addr_code"],
  993. "return_code"=>$info["error_code"],
  994. "post_fee"=>$returninfo["post_fee"],
  995. "post_code"=>$returninfo["post_code"],
  996. "post_company"=>$returninfo["post_company"],
  997. "customer_code"=>$info["customer_code"],
  998. "status"=>$info['return_tag']==1?4:1,
  999. "addtime"=>date("Y-m-d H:i:s"),
  1000. "updatetime"=>date("Y-m-d H:i:s")
  1001. ];
  1002. $sav= Db::name("order_back")->insert($thdata,true);
  1003. if($sav){
  1004. //修改状态,添加待办
  1005. ActionLog::logAdd($this->post['token'], [
  1006. "order_code" => $thdata['thNo'],//销售单code
  1007. "status" => $thdata['status'],//这里的status是之前的值
  1008. "action_remark" => '',//备注
  1009. "action_type" => "create"//新建create,编辑edit,更改状态status
  1010. ], "CKTHD", $thdata['status'], $thdata);
  1011. ProcessOrder::AddProcess($this->post['token'], [
  1012. "order_type" => 'CKTHD',
  1013. "order_code" => $thdata['thNo'],//销售单code
  1014. "order_id" => $sav,
  1015. "order_status" => $thdata['status'],"before_status"=>1
  1016. ]);
  1017. //维护台账记录
  1018. // Db::name('standing_book')
  1019. // ->where(['returnCode' => $bkcode])
  1020. // ->update([
  1021. // 'thNo' => $thNo,
  1022. // 'updatetime' => date("Y-m-d H:i:s")
  1023. // ]);
  1024. Db::execute("UPDATE `wsm_standing_book` SET `thNo`=CONCAT(`thNo`,',{$thNo}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$bkcode}',`returnCode`)");//这里的returnCode可能有多个
  1025. if($info['return_tag']==1){
  1026. $sale['th_num']+= $info['error_num'];
  1027. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  1028. $sale['status']=3;
  1029. }
  1030. $sale['th_fee']+= round($info['error_num']*$sale['sale_price'],2);
  1031. $sale['updatetime']= date("Y-m-d H:i:s");
  1032. $uap=Db::name("sale")->save($sale);
  1033. if($uap==false){
  1034. Db::rollback();
  1035. return error_show(1005,'销售单订单更新失败');
  1036. }
  1037. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  1038. if($ordernum==false){
  1039. Db::rollback();
  1040. return error_show(1005,'未找到关联采购单');
  1041. }
  1042. $ordernum['send_num']-=$info['error_num'];
  1043. $orderup =Db::name("order_num")->save($ordernum);
  1044. if($orderup==false){
  1045. Db::rollback();
  1046. return error_show(1005,'关联数据更新失败');
  1047. }
  1048. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  1049. if($cgd==false){
  1050. Db::rollback();
  1051. return error_show(1005,'未找到采购单数据');
  1052. }
  1053. $cgd['th_num']+= $info['error_num'];
  1054. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  1055. $cgd['status']=4;
  1056. }
  1057. $cgd['th_fee']+= round($info['error_num']*$cgd['good_price'],2);
  1058. $cgd['updatetime']= date("Y-m-d H:i:s");
  1059. $cgdup =Db::name("purchease_order")->save($cgd);
  1060. if($cgdup==false){
  1061. Db::rollback();
  1062. return error_show(1005,'采购单数据更新失败');
  1063. }
  1064. if($info['error_num']>=$out['send_num']){
  1065. $out['status']=4;
  1066. $out['updatetime']=date("Y-m-d H:i:s");
  1067. $upout =Db::name("order_out")->save($out);
  1068. if($upout==false){
  1069. Db::rollback();
  1070. return error_show(1005,'出库单数据更新失败');
  1071. }
  1072. }
  1073. $data=[
  1074. "orderCode"=>$info['orderCode'],
  1075. "th_type"=>3,
  1076. "th_num"=>$info['error_num'],
  1077. "th_fee"=>round($sale['sale_price']*$info["error_num"],2),
  1078. "thCode"=>$thNo,
  1079. "spuCode"=>$sale['good_code'],
  1080. "good_name"=>$sale['good_name'],
  1081. "cat_id"=>$sale['cat_id'],
  1082. "apply_id"=>$info['apply_id'],
  1083. "apply_name"=>$info['apply_name'],
  1084. "addtime"=>date("Y-m-d H:i:s"),
  1085. "status"=>1,
  1086. "is_del"=>0
  1087. ];
  1088. $inse=Db::name("th_data")->insert($data);
  1089. if($inse==false){
  1090. Db::rollback();
  1091. return error_show(1004,"退货单更新失败");
  1092. }
  1093. }
  1094. Db::commit();
  1095. return app_show(0,"退货单新建成功",["thNo"=>$thNo]);
  1096. }
  1097. }
  1098. }
  1099. Db::rollback();
  1100. return error_show(1004,'退货失败');
  1101. }catch (\Exception $e){
  1102. Db::rollback();
  1103. return error_show(1004,$e->getMessage());
  1104. }
  1105. }
  1106. //取消售后
  1107. public function Cancel()
  1108. {
  1109. //接收参数
  1110. $data = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1111. $val = Validate::rule(['returnCode|售后单编号' => 'require']);
  1112. if (!$val->check($data)) return error_show(1005, $val->getError());
  1113. Db::startTrans();
  1114. try {
  1115. $rs = Db::name('order_return')->field('id,status')->where([['returnCode', '=', $data['returnCode']], ['is_del', '=', 0], ['status', '<>', 8]])->find();
  1116. if (!$rs) throw new Exception('该售后单记录不存在');
  1117. //修改记录
  1118. $res = Db::name('order_return')->where(['id' => $rs['id']])->update(['status' => 8, 'updatetime' => date('Y-m-d H:i:s')]);
  1119. if (!$res) throw new Exception('该售后单记录修改失败');
  1120. //记录日志
  1121. ActionLog::logAdd($data['token'], ["order_code" => $data['returnCode'], "status" => $rs['status'], "action_remark" => '', "action_type" => "delete"], 'SHD', 8, $this->post);
  1122. ProcessOrder::AddProcess($this->post['token'], ["order_code" => $data['returnCode'], "order_id" => $rs['id'], "order_status" => 8, "order_type" => 'SHD',"before_status"=>$rs['status']]);
  1123. //提交
  1124. Db::commit();
  1125. return app_show(0, "取消成功");
  1126. } catch (\Exception $e) {
  1127. Db::rollback();
  1128. return error_show(1004, $e->getMessage());
  1129. }
  1130. }
  1131. //根据审核完成的售后申请单,创建新的销售单和采购单
  1132. public function createSaleAndCgdByAfter()
  1133. {
  1134. $param = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1135. $val = Validate::rule(['token' => 'require', 'returnCode|售后申请单编号' => 'require']);
  1136. if (!$val->check($param)) return error_show(1005, $val->getError());
  1137. //查询售后申请单详情
  1138. $info = Db::name('order_return')
  1139. ->where('returnCode', $param['returnCode'])
  1140. ->where('is_del', 0)
  1141. ->find();
  1142. if (empty($info)) return error_show(1005, '该售后申请单不存在');
  1143. if ($info['status'] != 5) return error_show(1005, '该售后申请单尚未审核完成');
  1144. if ($info['is_reissue'] == 1) return error_show(1005, '该售后申请单已完成补发,无法重复进行');
  1145. $apply_id = GetUserInfo($param['token']);
  1146. if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "申请人数据不存在");
  1147. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1148. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1149. //原本销售单信息
  1150. $old_sale = Db::name('sale')->where(['is_del' => 0, 'orderCode' => $info['orderCode']])->find();
  1151. if (empty($old_sale)) return error_show(1005, '没有对应的销售单信息');
  1152. //发货单信息
  1153. $old_out = Db::name('order_out')->where(['is_del' => 0, 'orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])->find();
  1154. if (empty($old_out)) return error_show(1005, '没有对应的发货单信息');
  1155. //根据是否咨询类商品,分别处理
  1156. if ($info['order_type'] == 3) return $this->createSaleZixun($info['id'], $info['good_code'], $info, $old_sale, $rm, $ri, $old_out['addrid'], $param['token']);
  1157. else return $this->createSaleNotZixun($info['id'], $info['good_code'], $info, $old_sale, $rm, $ri, $old_out, $param['token']);
  1158. }
  1159. /**
  1160. * 非咨询类商品处理
  1161. * @param int $orid 售后记录的id
  1162. * @param string $spuCode 商品的spuCode
  1163. * @param array $info 售后申请单的详情
  1164. * @param array $old_sale 原始销售订单信息
  1165. * @param int $rm 操作人id
  1166. * @param string $ri 操作人名称
  1167. * @param array $old_out 原始发货单信息
  1168. * @param string $token 当前用户的token
  1169. * @throws \think\db\exception\DataNotFoundException
  1170. * @throws \think\db\exception\DbException
  1171. * @throws \think\db\exception\ModelNotFoundException
  1172. */
  1173. private function createSaleNotZixun(int $orid = 0, string $spuCode = '', array $info = [], array $old_sale = [], int $rm = 0, string $ri = '', array $old_out = [], string $token = '')
  1174. {
  1175. $goodinfo = Db::name('good')
  1176. ->alias('g')
  1177. ->field('g.*,gp.skuCode')
  1178. ->join('good_platform gp', 'gp.spuCode=g.spuCode AND gp.exam_status=6')
  1179. ->where('g.spuCode', $spuCode)
  1180. ->where('gp.skuCode', $old_sale['skuCode'])
  1181. ->find();
  1182. if (empty($goodinfo)) return error_show(1005, '查不到这个商品,或该商品已下线无法售后');
  1183. $is_activity = $old_sale['is_activity'];
  1184. $actcode = $old_sale['activity_code'];
  1185. $good_num = $info['error_num'];//数量,取售后申请单中的异常数量
  1186. $supplierNo = $old_sale['supplierNo'];
  1187. $sale_price = 0;
  1188. $origin_price = $old_sale['origin_price'];
  1189. $goodtype = $old_sale['good_type'];
  1190. $is_stock = $goodinfo['is_stock'];
  1191. $skuCode = $goodinfo['skuCode'];
  1192. $orderCode = makeNo("QR");
  1193. if ($is_stock == 1) {
  1194. $stock = Db::name("good_stock")
  1195. ->alias("a")
  1196. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  1197. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" => $supplierNo])
  1198. ->field("a.id,a.wait_in_stock,a.usable_stock,a.wait_out_stock")
  1199. ->find();
  1200. if ($stock == false || $stock['usable_stock'] < $good_num) return error_show(1003, "库存数量不足");
  1201. }
  1202. $goodinfo['cgd_gold_price'] = 0;
  1203. if ($goodtype == 1) {
  1204. if ($is_activity == 1) {
  1205. $act = Db::name("activity_info")
  1206. ->alias("a")
  1207. ->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  1208. ->where(["a.skuCode" => $skuCode, "a.activity_code" => $actcode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
  1209. ->find();
  1210. if ($act == false) return error_show(1003, "未找到相关活动价");
  1211. if ($act['moq_num'] > $good_num) return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  1212. if ($act['activity_stock'] < $good_num) return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  1213. }
  1214. // else {
  1215. // $good = Db::name("good_ladder")
  1216. // ->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])
  1217. // ->where([["min_num", "<=", $good_num]])
  1218. // ->order("min_num desc")
  1219. // ->find();
  1220. //
  1221. // if ($good == false) return error_show(1003, "未找到相关阶梯价格");
  1222. //
  1223. // if ($goodinfo['is_gold_price'] == 1 && $is_stock != 1) {
  1224. // $gold = Db::name("gold_price1")
  1225. // ->field('id,price')
  1226. // ->where(["type" => $goodinfo['noble_metal'], "is_del" => 0, "status" => 1])
  1227. // ->order("addtime desc")
  1228. // ->find();
  1229. //
  1230. // $goodinfo['cgd_gold_price'] = $gold["price"];
  1231. // }
  1232. // }
  1233. }
  1234. //原有采购单信息
  1235. $old_cgd = Db::name('order_num')
  1236. ->alias('on')
  1237. ->field('on.id onid,po.*')
  1238. ->leftJoin('purchease_order po', 'po.cgdNo=on.cgdNo')
  1239. ->where('on.orderCode', $old_sale['orderCode'])
  1240. ->find();
  1241. $cgd = array_merge($old_cgd, [
  1242. 'id' => null,
  1243. // "supplierNo" => $ct['supplierNo'],
  1244. // "companyNo" => $supplierNo,
  1245. "orderCode" => $orderCode,
  1246. "spuCode" => $spuCode,
  1247. "skuCode" => $skuCode,
  1248. // "good_name" => $ct['good_name'],
  1249. "sale_price" => $old_cgd['good_price'],//采购单价格不为0,取原采购单的价格
  1250. "total_fee" => $old_cgd['good_price'] * $good_num,
  1251. // "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  1252. // "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  1253. // "open_fee" => $ct['open_fee'],
  1254. // "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  1255. 'cost_fee' => $old_cgd['teach_fee'],
  1256. // "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  1257. // "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  1258. // "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  1259. // "demo_fee" => $ct['demo_fee'],
  1260. "good_num" => $good_num,
  1261. // "good_type" => $goodtype,
  1262. // "weight" => $ct['noble_weight'],
  1263. // "gold_price" => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  1264. // "order_type" => $is_stock == 1 ? 1 : 2,
  1265. "order_source" => 6,//6售后补换货
  1266. "createrid" => $rm,
  1267. "creater" => $ri,
  1268. // 'send_way' => 2,
  1269. 'token' => $token,
  1270. 'addtime' => date('Y-m-d H:i:s'),
  1271. 'updatetime' => date('Y-m-d H:i:s'),
  1272. ]);
  1273. // $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  1274. $send_num = 0;
  1275. // if($sendtype==1){
  1276. // if($addrlist=="" || empty($addrlist) ||!is_array($addrlist)){
  1277. // return error_show(1004,"参数addrlist不能为空");
  1278. // }
  1279. // $send_num = array_sum(array_column($addrlist,"receipt_quantity"));
  1280. // }
  1281. // if($send_num>$good_num){
  1282. // return error_show(1004,"发货数量超出订单总数量");
  1283. // }
  1284. // $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  1285. // $proof_id =isset($this->post['proof_id']) && $this->post['proof_id'] !=="" ?intval($this->post['proof_id']):0;
  1286. // $apply_id = GetUserInfo($param['token']);
  1287. // if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "申请人数据不存在");
  1288. // $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1289. // $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1290. Db::startTrans();
  1291. try {
  1292. $data = array_merge($old_sale, [
  1293. 'id' => null,
  1294. "orderCode" => $orderCode,
  1295. "good_code" => $spuCode,
  1296. "skuCode" => $skuCode,
  1297. // "customer_code" => $info['customer_code'],
  1298. // "good_name" => (isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '') ? $goodinfo['good_name'] : '',
  1299. "good_num" => $good_num,
  1300. // "cat_id" => $goodinfo['cat_id'],
  1301. "apply_id" => $rm,
  1302. "apply_name" => $ri,
  1303. "origin_price" => $origin_price,
  1304. // "sale_price" => $old_sale['sale_price'],//取原价格
  1305. // "total_price" => $sale_price,//取原总价
  1306. // "post_fee" => 0,
  1307. // "status" => 0,
  1308. "th_num" => 0,
  1309. "th_fee" => 0,//退货金额为0
  1310. "send_num" => 0,
  1311. "wsend_num" => $good_num,
  1312. // "send_status" => 0,
  1313. // "good_type" => $goodtype,
  1314. // "send_type" => $old_sale['send_type'],
  1315. // "supplierNo" => $supplierNo,
  1316. // "is_del" => 0,
  1317. // "zxNo" => "",
  1318. // "platform_order" => $old_sale['platform_order'],
  1319. // "platform_id" => $old_sale['platform_id'],
  1320. "remark" => $old_sale['orderCode'],
  1321. // "is_stock" => $is_stock,
  1322. "is_activity" => $is_activity,
  1323. "activity_code" => $actcode,
  1324. // "order_type" => $is_stock == 1 ? 1 : 2,
  1325. "order_source" => 6,//6售后补换货
  1326. // "poNo"=>$poNo,
  1327. // 'good_weight' => $ct['noble_weight'],
  1328. // 'gold_price' => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  1329. // 'cost_price' => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  1330. // 'diff_weight' => 0,
  1331. // 'diff_fee' => 0,
  1332. // "workNo" => '',
  1333. 'status' => 0,
  1334. "addtime" => date("Y-m-d H:i:s"),
  1335. "updatetime" => date("Y-m-d H:i:s"),
  1336. // 'total_price' => round($sale_price * $good_num, 2),
  1337. // 'proof_id' => $old_sale['proof_id'],
  1338. // 'paytime' => $old_sale['paytime'],
  1339. 'returnCode' => $info['returnCode'],
  1340. ]);
  1341. $datainfo = Db::name('sale')->insertGetId($data);
  1342. if ($datainfo > 0) {
  1343. //修改状态,添加待办
  1344. ActionLog::logAdd($token, [
  1345. "order_code" => $orderCode,//销售单code
  1346. "status" => 0,//这里的status是之前的值
  1347. "action_remark" => '',//备注
  1348. "action_type" => "create"//新建create,编辑edit,更改状态status
  1349. ], "XSQRD", 0, $this->post);
  1350. ProcessOrder::AddProcess($token, [
  1351. "order_type" => 'XSQRD',
  1352. "order_code" => $orderCode,//销售单code
  1353. "order_id" => $datainfo,
  1354. "order_status" => 0, "before_status" => 0
  1355. ]);
  1356. if ($is_activity == 1) {
  1357. $actup = [
  1358. "activity_stock" => $act['activity_stock'] - $good_num,
  1359. "updatetime" => date("Y-m-d H:i:s")
  1360. ];
  1361. $actupp = Db::name("activity_info")
  1362. ->where(["skuCode" => $skuCode, "activity_code" => $actcode, "is_del" => 0, "status" => 1])
  1363. ->save($actup);
  1364. if ($actupp == false) throw new Exception('活动库存修改失败');
  1365. }
  1366. $outstatus = 0;
  1367. $standing_book_da = ['sale_id' => $datainfo, 'customer_code' => $old_sale['customer_code'], 'skuCode' => $skuCode, 'updatetime' => date('Y-m-d H:i:s')];
  1368. if ($is_stock == 0) $this->createCgd($cgd, $standing_book_da);
  1369. else {
  1370. $outstatus = 1;
  1371. $bol = $this->RelaCgd([
  1372. 'orderCode' => $orderCode,
  1373. "good_num" => $good_num,
  1374. "spuCode" => $spuCode,
  1375. "companyNo" => $supplierNo,
  1376. "cost_fee" => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  1377. 'order_type' => $data['order_type'],
  1378. 'order_source' => $data['order_source'],
  1379. ], $standing_book_da);
  1380. if (isset($stock)) {
  1381. $stck = [
  1382. "usable_stock" => $stock['usable_stock'] - $good_num,
  1383. "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
  1384. "updatetime" => date("Y-m-d H:i:s")
  1385. ];
  1386. $upad = Db::name("good_stock")->where($stock)->update($stck);
  1387. if ($upad == false) throw new Exception('库存商品更新库存失败');
  1388. //商品变动日志表,good_log_code字段存储采购单号
  1389. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  1390. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  1391. GoodLog::LogAdd($token, $good_data, "XSQRD");
  1392. }
  1393. }
  1394. // $old_out = Db::name('order_out')
  1395. // ->field('id,addrid,wsm_code')
  1396. // ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])
  1397. // ->find();
  1398. $old_out_addr_info = Db::name('order_out')
  1399. ->alias('oo')
  1400. ->field('oo.id,oa.addr,oa.addr_code,oa.contactor,oa.mobile,oa.customer_code,oa.post_fee,oa.arrive_time')
  1401. ->leftJoin('order_addr oa', 'oa.id=oo.addrid')
  1402. ->where(['oo.outCode' => $info['outCode'], 'oo.orderCode' => $info['orderCode']])
  1403. ->find();
  1404. if (empty($old_out_addr_info)) throw new Exception('发货单对应的地址信息查不到');
  1405. $temp = [
  1406. 'orderCode' => $orderCode,
  1407. 'addr' => $old_out_addr_info['addr'],
  1408. 'addr_code' => $old_out_addr_info['addr_code'],
  1409. 'contactor' => $old_out_addr_info['contactor'],
  1410. 'mobile' => $old_out_addr_info['mobile'],
  1411. 'customer_code' => $old_out_addr_info['customer_code'],
  1412. 'receipt_quantity' => $good_num,
  1413. 'arrive_time' => $old_out_addr_info['arrive_time'],
  1414. 'addtime' => date("Y-m-d H:i:s"),
  1415. 'updatetime' => date("Y-m-d H:i:s"),
  1416. // ''=>'',
  1417. ];
  1418. $addrid = Db::name('order_addr')->insertGetId($temp);
  1419. $outCode = makeNo("DF");
  1420. $out = array_merge($old_out, [
  1421. 'id' => null,
  1422. "orderCode" => $orderCode,
  1423. "outCode" => $outCode,
  1424. "apply_id" => $rm,
  1425. "apply_name" => $ri,
  1426. "addrid" => $addrid,
  1427. "post_name" => "",
  1428. "post_code" => "",
  1429. "post_fee" => 0,
  1430. "sendtime" => date("Y-m-d H:i:s"),
  1431. "send_num" => $good_num,
  1432. "check_num" => 0,
  1433. "error_num" => 0,
  1434. "wsm_code" => isset($old_out['wsm_code']) ? $old_out['wsm_code'] : '',
  1435. "order_type" => $is_stock == 1 ? 1 : 2,
  1436. "status" => $outstatus,
  1437. "addtime" => date("Y-m-d H:i:s"),
  1438. "updatetime" => date("Y-m-d H:i:s")
  1439. ]);
  1440. Db::name("order_out")->insert($out);
  1441. $standing_book_da['outCode'] = $outCode;
  1442. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])->find();
  1443. if (empty($order)) throw new Exception('采购单关联数据未找到');
  1444. $order['send_num'] += $good_num;
  1445. $order['wsend_num'] -= $good_num;
  1446. if ($order['wsend_num'] < 0) throw new Exception('发货数量已超出总数');
  1447. $ups = Db::name("order_num")->save($order);
  1448. if ($ups) {
  1449. $sen = Db::name("order_send")->save([
  1450. "cgdNo" => $order['cgdNo'],
  1451. "outCode" => $outCode,
  1452. "send_num" => $good_num,
  1453. "status" => 1,
  1454. "addtime" => date("Y-m-d H:i:s"),
  1455. "updatetime" => date("Y-m-d H:i:s")
  1456. ]);
  1457. if ($sen == false) throw new Exception('发货地址添加创建失败');
  1458. }
  1459. if (!isset($standing_book_da['returnCode'])) $standing_book_da['returnCode'] = '';
  1460. if (!isset($standing_book_da['thNo'])) $standing_book_da['thNo'] = '';
  1461. if (!isset($standing_book_da['returnGoodCode'])) $standing_book_da['returnGoodCode'] = '';
  1462. if (!isset($standing_book_da['wsm_in_code'])) $standing_book_da['wsm_in_code'] = '';
  1463. if (!isset($standing_book_da['cgdReturnCode'])) $standing_book_da['cgdReturnCode'] = '';
  1464. //处理台账
  1465. Db::name('standing_book')->insert(array_merge($standing_book_da, ['addtime' => date('Y-m-d H:i:s'), 'updatetime' => date('Y-m-d H:i:s'), 'standBookNo' => makeNo('IO')]));
  1466. //维护售后申请单
  1467. Db::name('order_return')
  1468. ->where(['id' => $orid, 'is_reissue' => 0])
  1469. ->update([
  1470. 'is_reissue' => 1,
  1471. 'newOrderCode' => $orderCode,
  1472. 'updatetime' => date('Y-m-d H:i:s')
  1473. ]);
  1474. Db::commit();
  1475. return error_show(0, "操作成功");
  1476. } else throw new Exception('咨询订单创建失败');
  1477. } catch (\Exception $e) {
  1478. Db::rollback();
  1479. return error_show(1005, $e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine());
  1480. }
  1481. }
  1482. /**
  1483. * 咨询类商品处理
  1484. * @param int $orid 对应售后记录的id
  1485. * @param string $spuCode 商品的spuCode
  1486. * @param array $info 售后申请单的详情
  1487. * @param array $old_sale 原始销售订单
  1488. * @param int $rm 操作人id
  1489. * @param string $ri 操作人名称
  1490. * @param int $addrid 原始发货单的地址id
  1491. * @param string $token 当前用户的token
  1492. * @throws \think\db\exception\DataNotFoundException
  1493. * @throws \think\db\exception\DbException
  1494. * @throws \think\db\exception\ModelNotFoundException
  1495. */
  1496. private function createSaleZixun(int $orid = 0, string $spuCode = '', array $info = [], array $old_sale = [], int $rm = 0, string $ri = '', int $addrid = 0, string $token = '')
  1497. {
  1498. // $bidNo =isset($this->post['bidNo'])&&$this->post['bidNo']!="" ? trim($this->post['bidNo']):"";
  1499. // if($bidNo==""){
  1500. // return error_show(1003,"参数bidNo不能为空");
  1501. // }
  1502. $zxinfo = Db::name("consult_bids")->where(["bidNo" => $old_sale['zxNo'], "is_del" => 0])->find();
  1503. if ($zxinfo == false) return error_show(1005, '未找到咨询单商品信息');
  1504. // if($zxinfo['status']!=6){
  1505. // return error_show(1003,"咨询单状态有误无法转单");
  1506. // }
  1507. // $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->find();
  1508. // if($zx==false){
  1509. // return error_show(1003,"未找到咨询单信息");
  1510. // }
  1511. // if($zx['status']!=4){
  1512. // return error_show(1003,"咨询单状态有误无法转单");
  1513. // }
  1514. // $zxorder =Db::name("consult_order")->where(["zxNo"=>$zx["zxNo"],"is_del"=>0])->find();
  1515. // if($zxorder==false){
  1516. // return error_show(1003,"未找到咨询单信息");
  1517. // }
  1518. // $good_num =isset($this->post['good_num'])&&$this->post['good_num']!="" ? intval($this->post['good_num']):"";
  1519. // if($good_num===""){
  1520. // return error_show(1003,"参数 good_num 不能为空");
  1521. // }
  1522. $good_num = $info['error_num'];
  1523. $sale_price = 0;
  1524. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] != "" ? floatval($this->post['sale_price']) : "";
  1525. // if ($sale_price === "") {
  1526. // return error_show(1003, "参数 sale_price 销售单价 不能为空");
  1527. // }
  1528. // if ($sale_price < $zxinfo['sale_price']) {
  1529. // return error_show(1003, "修改的销售单价不能低于原来的销售单价");
  1530. // }
  1531. // $sendtype = isset($this->post['sendtype'])&&$this->post['sendtype']!="" ? intval($this->post['sendtype']):"";
  1532. // if($sendtype==""){
  1533. // return error_show(1003,"参数sendtype不能为空");
  1534. // }
  1535. // $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  1536. $orderCode = makeNo("QR");
  1537. // $spuCode = $zxinfo['spuCode'];
  1538. $skuCode = "";
  1539. // $is_stock=0;
  1540. //原有采购单信息
  1541. $old_cgd = Db::name('order_num')
  1542. ->alias('on')
  1543. ->field('on.id onid,po.*')
  1544. ->leftJoin('purchease_order po', 'po.cgdNo=on.cgdNo')
  1545. ->where('on.orderCode', $old_sale['orderCode'])
  1546. ->find();
  1547. $cgd = array_merge($old_cgd, [
  1548. 'id' => null,
  1549. // "supplierNo"=>$zxinfo['supplierNo'],
  1550. // "companyNo"=>$zxorder['companyNo'],
  1551. "spuCode" => $spuCode,
  1552. "skuCode" => $skuCode,
  1553. "orderCode" => $orderCode,
  1554. // "good_name"=>$zxinfo['good_name'],
  1555. "sale_price" => $old_cgd['good_price'],//采购单价格不为0,取原采购单的价格
  1556. "total_fee" => round($old_cgd['good_price'] * $good_num, 2),
  1557. // "pakge_fee"=>$zxinfo['pakge_fee'],
  1558. // "cert_fee"=>$zxinfo['cert_fee'],
  1559. // "open_fee"=>$zxinfo['open_fee'],
  1560. 'cost_fee' => $old_cgd['teach_fee'],
  1561. // $old_sale['cost_fee']=$old_sale['teach_fee'];
  1562. // "mark_fee"=>$zxinfo['mark_fee'],
  1563. // "demo_fee"=>$zxinfo['demo_fee'],
  1564. // "nake_fee"=>$zxinfo['nake_fee'],
  1565. // "delivery_fee"=>$zxinfo['delivery_fee'],
  1566. "good_num" => $good_num,
  1567. "createrid" => $rm,
  1568. "creater" => $ri,
  1569. "good_type" => 1,
  1570. // "weight"=>$zxinfo['good_weight'],
  1571. // "gold_price"=>$zxinfo['gold_price'],
  1572. // "is_diff"=>$zxinfo['is_diff'],
  1573. "order_type" => 3,//咨询商品
  1574. "order_source" => 6,//6售后补换货
  1575. // 'send_way'=>$zxinfo['send_way']
  1576. 'token' => $token
  1577. ]);
  1578. // $token=isset($this->post['token'])&&$this->post['token']!=""? trim($this->post['token']):"";
  1579. // if($token==""){
  1580. // return error_show(102, "参数token不能为空");
  1581. // }
  1582. // $apply_id = GetUserInfo($token);
  1583. // if (empty($apply_id) || $apply_id['code'] != 0) {
  1584. // return error_show(102, "申请人数据不存在");
  1585. // }
  1586. // $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1587. // $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1588. // $va = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  1589. // $send_num=0;
  1590. // if($sendtype==1){
  1591. // if ($va == "") {
  1592. // return error_show(1002, "参数order_addr不能为空");
  1593. // }
  1594. // $send_num = array_sum(array_column($va,"receipt_quantity"));
  1595. // }
  1596. // if($good_num<$send_num){
  1597. // return error_show(1002, "发货数量不能超过购买数量");
  1598. // }
  1599. // $arrtime=isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
  1600. // if($arrtime==""){
  1601. // return error_show(1002, "参数arrtime不能为空");
  1602. // }
  1603. //新加字段
  1604. // $platform_order=isset($this->post['platform_order'])&&$this->post['platform_order']!="" ?$this->post['platform_order']:"";
  1605. // $workNo=isset($this->post['workNo'])&&$this->post['workNo']!="" ?$this->post['workNo']:"";
  1606. // $proof_type = isset($this->post['proof_type']) && $this->post['proof_type'] != "" ? intval($this->post['proof_type']) : 0;
  1607. // $proof_url = isset($this->post['proof_url']) && $this->post['proof_url'] != "" ? trim($this->post['proof_url']) : '';
  1608. Db::startTrans();
  1609. try {
  1610. // $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->lock(true)->find();
  1611. // if($zx==false){
  1612. // Db::rollback();
  1613. // return error_show(1003,"未找到咨询单信息");
  1614. // }
  1615. $data = array_merge($old_sale, [
  1616. 'id' => null,
  1617. "orderCode" => $orderCode,
  1618. "good_code" => $spuCode,
  1619. // "skuCode"=>$skuCode,
  1620. // "customer_code"=>$zxorder['khNo'],
  1621. // "good_name"=>isset($zxinfo['good_name'])&&$zxinfo['good_name']!==''?$zxinfo['good_name']:'',
  1622. "good_num" => $good_num,
  1623. // "cat_id"=>$zxinfo['cat_id'],
  1624. "apply_id" => $rm,
  1625. "apply_name" => $ri,
  1626. // "origin_price"=>$zxinfo['total_fee'],
  1627. // "sale_price" => $old_sale['sale_price'],//取原价格
  1628. // "post_fee"=>0,
  1629. "status" => 0,
  1630. "send_status" => 0,
  1631. "th_num" => 0,
  1632. "th_fee" => 0,//退货金额为0
  1633. "send_num" => 0,
  1634. "wsend_num" => $good_num,
  1635. // "send_status"=>1,
  1636. "good_type" => 1,
  1637. // "send_type"=>$sendtype,
  1638. // "supplierNo"=>$zxorder['companyNo'],
  1639. "is_del" => 0,
  1640. // "zxNo"=>$bidNo,
  1641. // "platform_order"=>$platform_order,
  1642. // "platform_id"=> $zxorder['platform_code'],
  1643. "remark" => $old_sale['orderCode'],
  1644. // "arrive_time"=>$arrtime,
  1645. // "is_stock"=>0,
  1646. // "is_activity"=>0,
  1647. // "proof_id"=>0,
  1648. "order_type" => 3,
  1649. "order_source" => 6,//6售后补换货
  1650. // 'good_weight'=>$zxinfo['good_weight'],
  1651. // 'gold_price'=>$zxinfo['gold_price'],
  1652. // 'cost_price'=>$zxinfo['sale_cost_fee'],
  1653. // 'diff_weight'=>0,
  1654. // 'diff_fee'=>0,
  1655. "addtime" => date("Y-m-d H:i:s"),
  1656. "updatetime" => date("Y-m-d H:i:s"),
  1657. 'returnCode' => $info['returnCode'],
  1658. // 'total_price' => round($sale_price * $good_num, 2),
  1659. // 'workNo'=>$workNo,
  1660. ]);
  1661. $datainfo = Db::name('sale')->insertGetId($data);
  1662. if ($datainfo > 0) {
  1663. $standing_book_data = [
  1664. 'sale_id' => $datainfo,
  1665. 'infoNo' => $zxinfo['infoNo'],
  1666. 'updatetime' => date('Y-m-d H:i:s')
  1667. ];
  1668. //修改状态,添加待办
  1669. ActionLog::logAdd($this->post['token'], [
  1670. "order_code" => $orderCode,//销售单code
  1671. "status" => 0,//这里的status是之前的值
  1672. "action_remark" => '',//备注
  1673. "action_type" => "create"//新建create,编辑edit,更改状态status
  1674. ], "XSQRD", $data['status'], $data);
  1675. ProcessOrder::AddProcess($this->post['token'], [
  1676. "order_type" => 'XSQRD',
  1677. "order_code" => $orderCode,//销售单code
  1678. "order_id" => $datainfo,
  1679. "order_status" => $data['status'], "before_status" => 0
  1680. ]);
  1681. // $old_zx_status = $zx['status'];
  1682. // $zx['status']=5;
  1683. // $zx['updatetime']=date("Y-m-d H:i:s");
  1684. // $sa=Db::name("consult_info")->save($zx);
  1685. // if($sa==false){
  1686. // Db::rollback();
  1687. // return error_show(1002,"咨询单修改失败");
  1688. // }else{
  1689. // //修改状态,添加待办
  1690. // ActionLog::logAdd($this->post['token'], [
  1691. // "order_code" => $zx['infoNo'],//咨询单详情编号
  1692. // "status" => $old_zx_status,//这里的status是之前的值
  1693. // "action_remark" => '',//备注
  1694. // "action_type" => "status"//新建create,编辑edit,更改状态status
  1695. // ], "ZXD", 5, $zx);
  1696. //
  1697. // }
  1698. $this->createCgd($cgd, $standing_book_data);
  1699. // if($bol==false){
  1700. // Db::rollback();
  1701. // return error_show(1002,"咨询订单创建失败");
  1702. // }
  1703. // $limt=[
  1704. // "spuCode"=>$zxinfo['spuCode'],
  1705. // "good_name"=>$zxinfo['good_name'],
  1706. // "brand_id"=>$zxinfo['brand_id'],
  1707. // "good_unit"=>$zxinfo['unit_id'],
  1708. // "cat_id"=>$zxinfo['cat_id'],
  1709. // "good_type"=>0,
  1710. // "moq"=>1,
  1711. // "customized"=>$zxinfo['work_day'],
  1712. // "tax"=>$zxinfo['tax'],
  1713. // "supplierNo"=>$zxinfo["supplierNo"],
  1714. // "is_auth"=>0,
  1715. // "craft_desc"=>$zxinfo['good_name'],
  1716. // "good_remark"=>"",
  1717. // "platform_id"=>$zxorder['platform_id'],
  1718. // "good_img"=>$zxinfo['good_img'],
  1719. // "good_thumb_img"=>"",
  1720. // "good_info_img"=>"",
  1721. // "specinfo"=>$zxinfo['specinfo'],
  1722. // "work_day"=>$zxinfo['work_day'],
  1723. // "noble_metal"=>$zxinfo['metal_id'],
  1724. // "is_gold_price"=>$zxinfo['is_gold_price'],
  1725. // "config"=>$zxinfo['config'],
  1726. // "other_config"=>$zxinfo['other_config'],
  1727. // "weight"=>$zxinfo['weight'],
  1728. // "good_weight"=>$zxinfo['good_weight'],
  1729. // "is_diff"=>$zxinfo['is_diff'],
  1730. // "supply_area"=>$zxinfo['supply_area'],
  1731. // "pay_way"=>$zxinfo['pay_way'],
  1732. // "send_way"=>$zxinfo['send_way'],
  1733. // "companyNo"=>$zxorder['companyNo'],
  1734. // "status"=>1,
  1735. // "is_del"=>0,
  1736. // "createrid"=>$zxinfo['createrid'],
  1737. // "creater"=>$zxinfo['creater'],
  1738. // "addtime"=>date("Y-m-d H:i:s"),
  1739. // "updatetime"=>date("Y-m-d H:i:s"),
  1740. // 'proof_type' => $proof_type,//凭证类型
  1741. // 'proof_url' => $proof_url,//凭证文件
  1742. // ];
  1743. // $good = Db::name("good_zixun")->insert($limt);
  1744. // if(!$good){
  1745. // Db::rollback();
  1746. // return error_show(1006,"咨询商品录入失败");
  1747. // }
  1748. // if(!empty($va)){
  1749. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])->find();
  1750. if (empty($order)) throw new Exception('采购单关联数据未找到');
  1751. // foreach ($va as $value){
  1752. // $temp=[];
  1753. // $addrs=[];
  1754. // if($value['addr_code']!==''&&is_array($value['addr_code'])&&!empty($value['addr_code'])){
  1755. // $addrs['provice_code'] = $value['addr_code'][0];
  1756. // $addrs['city_code'] = $value['addr_code'][1];
  1757. // $addrs['area_code'] = $value['addr_code'][2];
  1758. // $addr = json_encode($addrs);
  1759. // }else{
  1760. // $addr = isset($value['addr_code'])?$value['addr_code']:'';
  1761. // }
  1762. // $temp['orderCode']=$orderCode;
  1763. // $temp['contactor']=$value['contactor'];
  1764. // $temp['mobile'] = $value['mobile'];
  1765. // $temp['addr'] = $value['addr'];
  1766. // $temp['addr_code']=$addr;
  1767. // $temp['customer_code'] =$zxorder['khNo'];
  1768. // $temp['receipt_quantity']=$value['receipt_quantity'];
  1769. // $temp['post_fee'] =0;
  1770. // $temp['is_del'] =0;
  1771. // $temp['addtime'] =date("Y-m-d H:i:s");
  1772. // $temp['updatetime'] =date("Y-m-d H:i:s");
  1773. // $temp['arrive_time']=$arrtime;
  1774. $old_out_addr_info = Db::name('order_out')
  1775. ->alias('oo')
  1776. ->field('oo.id,oa.addr,oa.addr_code,oa.contactor,oa.mobile,oa.customer_code,oa.post_fee,oa.arrive_time')
  1777. ->leftJoin('order_addr oa', 'oa.id=oo.addrid')
  1778. ->where(['oo.outCode' => $info['outCode'], 'oo.orderCode' => $info['orderCode']])
  1779. ->find();
  1780. if (empty($old_out_addr_info)) throw new Exception('发货单对应的地址信息查不到');
  1781. $temp = [
  1782. 'orderCode' => $orderCode,
  1783. 'addr' => $old_out_addr_info['addr'],
  1784. 'addr_code' => $old_out_addr_info['addr_code'],
  1785. 'contactor' => $old_out_addr_info['contactor'],
  1786. 'mobile' => $old_out_addr_info['mobile'],
  1787. 'customer_code' => $old_out_addr_info['customer_code'],
  1788. 'receipt_quantity' => $good_num,
  1789. 'arrive_time' => $old_out_addr_info['arrive_time'],
  1790. 'addtime' => date("Y-m-d H:i:s"),
  1791. 'updatetime' => date("Y-m-d H:i:s"),
  1792. // ''=>'',
  1793. ];
  1794. $addrid = Db::name('order_addr')->insertGetId($temp);
  1795. // if($vmp>0){
  1796. $outCode = makeNo("DF");
  1797. $out = [
  1798. "orderCode" => $orderCode,
  1799. "outCode" => $outCode,
  1800. "apply_id" => $rm,
  1801. "apply_name" => $ri,
  1802. 'addrid' => $addrid,
  1803. "post_name" => "",
  1804. "post_code" => "",
  1805. "post_fee" => 0,
  1806. "sendtime" => date("Y-m-d H:i:s"),
  1807. "send_num" => $good_num,
  1808. "check_num" => 0,
  1809. "error_num" => 0,
  1810. "wsm_code" => "",
  1811. "order_type" => 3,
  1812. "status" => 0,
  1813. "addtime" => date("Y-m-d H:i:s"),
  1814. "updatetime" => date("Y-m-d H:i:s")
  1815. ];
  1816. $ou = Db::name("order_out")->insert($out);
  1817. if ($ou == false) throw new Exception('发货地址添加创建失败');
  1818. else {
  1819. //修改状态,添加待办
  1820. ActionLog::logAdd($this->post['token'], [
  1821. "order_code" => $outCode,//出库单号
  1822. "status" => 0,//这里的status是之前的值
  1823. "action_remark" => '',//备注
  1824. "action_type" => "create"//新建create,编辑edit,更改状态status
  1825. ], "CKD", 0, $out);
  1826. ProcessOrder::AddProcess($this->post['token'], [
  1827. "order_type" => 'CKD',
  1828. "order_code" => $outCode,//出库单号
  1829. "order_id" => Db::name("order_out")->getLastInsID(),
  1830. "order_status" => 0, "before_status" => 0
  1831. ]);
  1832. //将发货编号添加到台账中
  1833. $standing_book_data['outCode'][] = $outCode;
  1834. }
  1835. $order['send_num'] += $good_num;
  1836. $order['wsend_num'] -= $good_num;
  1837. if ($order['wsend_num'] < 0) throw new Exception('发货数量已超出总数');
  1838. $ups = Db::name("order_num")->save($order);
  1839. if ($ups) {
  1840. $sen = Db::name("order_send")->save([
  1841. "cgdNo" => $order['cgdNo'],
  1842. "outCode" => $outCode,
  1843. "send_num" => $good_num,
  1844. "status" => 1,
  1845. "addtime" => date("Y-m-d H:i:s"),
  1846. "updatetime" => date("Y-m-d H:i:s")
  1847. ]);
  1848. if ($sen == false) throw new Exception('发货地址添加创建失败');
  1849. }
  1850. // }else{
  1851. // Db::rollback();
  1852. // return error_show(1002,"发货地址添加创建失败");
  1853. // }
  1854. // }
  1855. //处理台账
  1856. if (isset($standing_book_data['outCode'])) {
  1857. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(`outCode`,'," . implode(',', $standing_book_data['outCode']) . "'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `infoNo`='{$standing_book_data['infoNo']}'");
  1858. unset($standing_book_data['outCode']);
  1859. }
  1860. Db::name('standing_book')->where('infoNo', $standing_book_data['infoNo'])->update($standing_book_data);
  1861. Db::name('order_return')
  1862. ->where(['id' => $orid, 'is_reissue' => 0])
  1863. ->update([
  1864. 'is_reissue' => 1,
  1865. 'newOrderCode' => $orderCode,
  1866. 'updatetime' => date('Y-m-d H:i:s')
  1867. ]);
  1868. Db::commit();
  1869. return app_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
  1870. }
  1871. Db::rollback();
  1872. return error_show(1002, "咨询订单创建失败");
  1873. } catch (Exception $e) {
  1874. Db::rollback();
  1875. return error_show(1003, $e->getMessage() . '|' . $e->getFile() . ':' . $e->getLine());
  1876. }
  1877. }
  1878. //创建采购单
  1879. private function createCgd($data,array &$standing_book_da=[]){
  1880. $cgdCode = makeNo("CG");
  1881. $supplier=Db::name("supplier")->where(["code"=>$data['supplierNo'],"is_del"=>0])->find();
  1882. if($supplier==false) throw new Exception('该供应商不存在');
  1883. $wsm= Db::name("warehouse_info")
  1884. ->where(["supplierNo"=>$data["supplierNo"],"companyNo"=>$data['companyNo'],"wsm_type"=>2,"is_del"=>0])
  1885. ->find();
  1886. if($wsm==false){
  1887. $wsm_code = makeNo("WSM");
  1888. $inwsm=[
  1889. "wsm_code"=>$wsm_code,
  1890. "name"=>$supplier['name'],
  1891. "wsm_type"=>2,
  1892. "supplierNo"=>$supplier['code'],
  1893. "addr"=>"",
  1894. "addrs_code"=>"",
  1895. "contactor"=>$data['order_type']==1? $supplier['personid']:$data['createrid'],
  1896. "contactor_name"=>$data['order_type']==1? $supplier['person']:$data['creater'],
  1897. "mobile"=>"",
  1898. "position"=>"",
  1899. "companyNo"=>$data['companyNo'],
  1900. "status"=>1,
  1901. "is_del"=>0,
  1902. "addtime"=>date("Y-m-d H:i:s"),
  1903. "updatetime"=>date("Y-m-d H:i:s")
  1904. ];
  1905. $in = Db::name("warehouse_info")->insert($inwsm);
  1906. if($in==false) throw new Exception('仓库新增失败');
  1907. }else $wsm_code =$wsm['wsm_code'];
  1908. $cg =[
  1909. "cgdNo"=>$cgdCode,
  1910. "bkcode"=>"",
  1911. "wsm_code"=>$wsm_code,
  1912. "cgder"=>$data['creater'],
  1913. "cgder_id"=>$data['createrid'],
  1914. "spuCode"=>$data['spuCode'],
  1915. "skuCode"=>$data['skuCode'],
  1916. "good_name"=>$data['good_name'],
  1917. "good_num"=>$data['good_num'],
  1918. "good_price"=>$data['sale_price'],
  1919. "total_fee"=>round($data['sale_price']*$data['good_num'],2),
  1920. "pakge_fee"=>$data['pakge_fee'],
  1921. "cert_fee"=>$data['cert_fee'],
  1922. "open_fee"=>$data['open_fee'],
  1923. "teach_fee"=>$data['cost_fee'],
  1924. "mark_fee"=>$data['mark_fee'],
  1925. "demo_fee"=>$data['demo_fee'],
  1926. "nake_fee"=>$data['nake_fee'],
  1927. "weight"=>$data['weight'],
  1928. "delivery_fee"=>$data['delivery_fee'],
  1929. "gold_price"=>$data['gold_price'],
  1930. "diff_weight"=>"0",
  1931. "diff_fee"=>"0",
  1932. "supplierNo"=>$data['supplierNo'],
  1933. "supplier_name"=>$supplier['name'],
  1934. "companyNo"=>$data['companyNo'],
  1935. "send_status"=>1,
  1936. "send_num"=>0,
  1937. "wsend_num"=>$data['good_num'],
  1938. "remark"=>"",
  1939. "status"=>0,//0初始化
  1940. "lasttime"=>date("Y-m-d H:i:s"),
  1941. "is_del"=>0,
  1942. "order_type"=>$data['order_type'],
  1943. "order_source"=>$data['order_source'],
  1944. "good_type"=>$data['good_type'],
  1945. "addtime"=>date("Y-m-d H:i:s"),
  1946. "updatetime"=>date("Y-m-d H:i:s")
  1947. ];
  1948. $up =Db::name("purchease_order")->insertGetId($cg);
  1949. if($up){
  1950. //修改状态,添加待办
  1951. ActionLog::logAdd($this->post['token'], [
  1952. "order_code" => $cg['cgdNo'],//销售单code
  1953. "status" => 0,//这里的status是之前的值
  1954. "action_remark" => '',//备注
  1955. "action_type" => "create"//新建create,编辑edit,更改状态status
  1956. ], "CGD", $cg['status'], $cg);
  1957. ProcessOrder::AddProcess($this->post['token'], [
  1958. "order_type" => 'CGD',
  1959. "order_code" => $cg['cgdNo'],//销售单code
  1960. "order_id" => $up,
  1961. "order_status" => $cg['status'],"before_status"=> 0
  1962. ]);
  1963. $standing_book_da = array_merge($standing_book_da, [
  1964. 'orderCode' => $data['orderCode'],
  1965. 'cgdNo' => $cgdCode,
  1966. 'spuCode' => $data['spuCode'],
  1967. 'skuCode' => $data['skuCode'],
  1968. 'order_type' => $data['order_type'],
  1969. 'order_source' => $data['order_source'],
  1970. 'supplierNo' => $data['supplierNo'],
  1971. "companyNo" => $data['companyNo'],
  1972. ]);
  1973. $rela=[
  1974. "orderCode"=>$data['orderCode'],
  1975. "cgdNo"=>$cgdCode,
  1976. "spuCode"=>$data['spuCode'],
  1977. "good_num"=>$data['good_num'],
  1978. "wsend_num"=>$data['good_num'],
  1979. "send_num"=>0,
  1980. "wait_num"=>0,
  1981. "status"=>1,
  1982. "source"=>2
  1983. ];
  1984. Db::name("order_num")->save($rela);
  1985. $stokc =Db::name("good_stock")
  1986. ->where(['spuCode'=>$data['spuCode'],"wsm_code"=>$wsm_code, "is_del"=>0])
  1987. ->find();
  1988. if($stokc==false){
  1989. $stokc=[
  1990. "spuCode"=>$data['spuCode'],
  1991. "wsm_code"=>$wsm_code,
  1992. "wait_in_stock"=>$data['good_num'],
  1993. "wait_out_stock"=>0,
  1994. "usable_stock"=>0,
  1995. "intra_stock"=>0,
  1996. "total_stock"=>0,
  1997. "status"=>1,
  1998. "addtime"=>date("Y-m-d H:i:s"),
  1999. "updatetime"=>date("Y-m-d H:i:s")
  2000. ];
  2001. }else{
  2002. $stokc['wait_in_stock']+=$data['good_num'];
  2003. $stokc['updatetime']=date("Y-m-d H:i:s");
  2004. }
  2005. $stoc= Db::name("good_stock")->save($stokc);
  2006. if($stoc==false) throw new Exception('商品仓库库存修改失败');
  2007. $good_data[] = ['good_log_code' =>$cgdCode, "stock_id" => isset($stoc['id'])?$stoc['id']:Db::name("good_stock")->getLastInsID(), "type" => 1,'stock'=>$data['good_num'], "stock_name" => "wait_in_stock"];
  2008. GoodLog::LogAdd($data['token'],$good_data,"CGD");
  2009. return true;
  2010. }else throw new Exception('商品仓库库存修改失败');
  2011. }
  2012. //创建采购单
  2013. public function RelaCgd($outinfo,array &$standing_book_da=[]){
  2014. $cgd = Db::name("order_bk")->where([["spuCode","=",$outinfo['spuCode']],["is_del","=",0],["balance_num",">=",$outinfo['good_num']],['companyNo',"=",$outinfo['companyNo']]])->lock(true)->find();
  2015. if($cgd==false) throw new Exception('未查询到备库单信息');
  2016. $good=Db::name("good")
  2017. ->where(["spuCode"=>$outinfo['spuCode'],"is_del"=>0])
  2018. ->find();
  2019. if($good==false) throw new Exception('未查询到商品信息');
  2020. $cgdinfo =Db::name("purchease_order")
  2021. ->where(['cgdNo'=>$cgd['cgdNo'],"is_del"=>0])
  2022. ->find();
  2023. if($cgdinfo==false) throw new Exception('未查询到采购单信息');
  2024. $QrdCgd=[
  2025. "cgdNo"=>makeNo("CG"),
  2026. "bkcode" => $cgdinfo['bkcode'],
  2027. 'wsm_code'=>$cgdinfo['wsm_code'],
  2028. "cgder_id"=>$cgdinfo['cgder_id'],
  2029. "cgder"=>$cgdinfo['cgder'],
  2030. "spuCode"=>$cgdinfo['spuCode'],
  2031. "good_name"=>$cgdinfo['good_name'],
  2032. "good_num"=>$outinfo['good_num'],
  2033. "good_price"=>$cgdinfo['good_price'],
  2034. "total_fee"=>round($cgdinfo['good_price']*$outinfo['good_num'],2),
  2035. "pakge_fee"=>$cgdinfo['pakge_fee'],
  2036. "cert_fee"=>$cgdinfo['cert_fee'],
  2037. "open_fee"=>$cgdinfo['open_fee'],
  2038. "delivery_fee"=>$cgdinfo['delivery_fee'],
  2039. "mark_fee"=>$cgdinfo['mark_fee'],
  2040. "teach_fee"=>$cgdinfo['teach_fee'],
  2041. "nake_fee"=>$cgdinfo['nake_fee'],
  2042. "demo_fee"=>$cgdinfo['demo_fee'],
  2043. "weight"=>$cgdinfo['weight'],
  2044. "diff_weight"=>$cgdinfo['diff_weight'],
  2045. "diff_fee"=>$cgdinfo['diff_fee'],
  2046. "gold_price"=>$cgdinfo['gold_price'],
  2047. "supplierNo"=>$cgdinfo['supplierNo'],
  2048. "supplier_name"=>$cgdinfo['supplier_name'],
  2049. "companyNo"=>$cgdinfo['companyNo'],
  2050. "send_status"=>3,
  2051. "send_num"=>$outinfo['good_num'],
  2052. "wsend_num"=>0,
  2053. "remark"=>$cgdinfo['remark'],
  2054. "status"=>3,
  2055. "lasttime"=>$cgdinfo['lasttime'],
  2056. "is_del"=>0,
  2057. "order_type"=>$outinfo['order_type'],
  2058. "order_source"=>$outinfo['order_source'],
  2059. "good_type"=>$cgdinfo['good_type'],
  2060. "addtime"=>date("Y-m-d H:i:s"),
  2061. "updatetime"=>date("Y-m-d H:i:s")
  2062. ];
  2063. $insetrCgd=Db::name("purchease_order")->insert($QrdCgd);
  2064. if($insetrCgd==false) throw new Exception('新增采购单失败');
  2065. else{
  2066. $standing_book_da = array_merge($standing_book_da, [
  2067. 'orderCode' => $outinfo['orderCode'],
  2068. 'cgdNo' => $cgdinfo['cgdNo'],
  2069. 'spuCode' => $outinfo['spuCode'],
  2070. 'order_type' => $QrdCgd['order_type'],
  2071. 'order_source' => $QrdCgd['order_source'],
  2072. 'supplierNo' => $cgdinfo['supplierNo'],
  2073. 'companyNo' => $cgdinfo['companyNo'],
  2074. 'bk_code' => $cgdinfo['bkcode'],
  2075. 'purchease_id' => Db::name('purchease')->where('bk_code', $cgdinfo['bkcode'])->value('id',0),
  2076. ]);
  2077. }
  2078. if($good['is_gold_price']==1 && $good['is_stock']==1){
  2079. $gold = Db::name("gold_price1")
  2080. ->field('id,price')
  2081. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  2082. ->order("addtime desc")
  2083. ->find();
  2084. $ct['cgd_gold_price']=$gold["price"];
  2085. $updat=[
  2086. "sale_price"=>0,
  2087. "total_price"=>0,
  2088. "origin_price"=>$cgdinfo['good_price'],
  2089. "gold_price"=>$gold["price"],
  2090. ];
  2091. Db::name("sale")->where(["orderCode"=>$outinfo['orderCode']])->update($updat);
  2092. // if($upsale==false){
  2093. // return false;
  2094. // }
  2095. }
  2096. $merge_num = Db::name("purchease_order")
  2097. ->where(["bkcode"=>$cgdinfo['bkcode'],"order_type"=>1,"is_del"=>0])
  2098. ->where("order_source","<>",0)
  2099. ->field("sum(send_num)-sum(th_num) as num")
  2100. ->find();
  2101. $cgd['balance_num']=$cgd['total_num']-$merge_num['num'];
  2102. $cgd['merge_num']=$merge_num['num'];
  2103. $cgd['updatetime']=date("Y-m-d H:i:s");
  2104. $up=Db::name("order_bk")->save($cgd);
  2105. if($up==false) throw new Exception('修改备库单失败');
  2106. $data=[
  2107. "orderCode"=>$outinfo['orderCode'],
  2108. "cgdNo"=>$QrdCgd['cgdNo'],
  2109. "spuCode"=>$outinfo['spuCode'],
  2110. "companyNo"=>$outinfo['companyNo'],
  2111. "good_num"=>$outinfo['good_num'],
  2112. "wsend_num"=>$outinfo['good_num'],
  2113. "send_num"=>0,
  2114. "wait_num"=>$outinfo['good_num'],
  2115. "status"=>1,
  2116. "source"=>1,
  2117. ];
  2118. $order =Db::name("order_num")->save($data);
  2119. if($order==false) throw new Exception('修改采购单销售单关联失败');
  2120. }
  2121. }