After.php 118 KB

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