After.php 109 KB

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