After.php 117 KB

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