After.php 152 KB

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