After.php 141 KB

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