After.php 117 KB

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