After.php 143 KB

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