After.php 111 KB

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