After.php 152 KB

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