After.php 144 KB

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