After.php 123 KB

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