After.php 149 KB

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