Sale.php 222 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\admin\model\ProcessOrder;
  5. use Exception;
  6. use think\App;
  7. use think\facade\Cache;
  8. use think\facade\Config;
  9. use think\facade\Db;
  10. use app\admin\model\ActionLog;
  11. use think\facade\Validate;
  12. //销售单
  13. class Sale extends Base
  14. {
  15. public $noble = [];
  16. public function __construct(App $app)
  17. {
  18. parent::__construct($app);
  19. $this->noble = \think\facade\Config::get("noble");
  20. }
  21. public function create()
  22. {
  23. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  24. $orderCode = makeNo("QR");
  25. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  26. if ($customer_code == "") {
  27. return error_show(1002, "客户customer_code不能为空");
  28. }
  29. $customer = Db::name("customer_info")->where(["companyNo" => $customer_code])->find();
  30. if ($customer == false) {
  31. return error_show(1004, "未找到客户数据");
  32. }
  33. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  34. if ($supplierNo == "") {
  35. return error_show(1002, "参数supplierNo不能为空");
  36. }
  37. $supplier = Db::name("business")->where(["companyNo" => $supplierNo])->find();
  38. if ($supplier == false) {
  39. return error_show(1004, "未找到平台供应商数据");
  40. }
  41. $goodtype = isset($this->post['goodtype']) && $this->post['goodtype'] !== "" ? intval($this->post['goodtype']) : "1";
  42. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : "1";
  43. $sendtype = isset($this->post['sendtype']) && $this->post['sendtype'] !== "" ? intval($this->post['sendtype']) : "";
  44. if ($sendtype === '') {
  45. return error_show(1004, "参数sendtype不能为空");
  46. }
  47. $platform_id = isset($this->post['platform_id']) && $this->post['platform_id'] !== "" ? intval($this->post['platform_id']) : "";
  48. if ($platform_id == '') {
  49. return error_show(1004, "参数platform_id不能为空");
  50. }
  51. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] !== "" ? trim($this->post['platform_order']) : "";
  52. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  53. if ($good_code == '') {
  54. return error_show(1004, "参数good_code不能为空");
  55. }
  56. $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval($this->post['good_num']) : "";
  57. if ($good_num === '') {
  58. return error_show(1004, "参数good_num不能为空");
  59. }
  60. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  61. if ($arrtime == "") {
  62. return error_show(1004, "参数arrtime不能为空");
  63. }
  64. $paytime = isset($this->post['paytime']) && $this->post['paytime'] != "" ? $this->post['paytime'] : "";
  65. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? trim($this->post['workNo']) : "";
  66. // $poNo =isset($this->post['poNo'])&&$this->post['poNo']!=""? trim($this->post['poNo']):"";
  67. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  68. ->where(['a.skuCode' => $good_code])->field("b.*,a.skuCode,a.platform_code,a.plat_code")->find();
  69. if ($ct == false) {
  70. return error_show(1002, "未找到商品数据");
  71. }
  72. $goodinfo = $ct;
  73. $is_stock = $ct['is_stock'];
  74. $spuCode = $ct['spuCode'];
  75. $skuCode = $ct['skuCode'];
  76. $is_activity = isset($this->post['is_activity']) && $this->post['is_activity'] !== "" ? $this->post['is_activity'] : "";
  77. $actcode = isset($this->post['act_code']) && $this->post['act_code'] !== "" ? trim($this->post['act_code']) : "";
  78. if ($order_type == 1) {
  79. if ($is_activity === "") {
  80. return error_show(1002, "参数is_activity不能为空");
  81. }
  82. if ($is_activity == 1 && $actcode == "") {
  83. return error_show(1002, "参数act_code不能为空");
  84. }
  85. }
  86. if ($goodinfo['is_stock'] == 1) {
  87. $stock = Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")->where
  88. (["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" => $supplierNo])->field("a.id,a.wait_in_stock,a.usable_stock,a.wait_out_stock")->find();
  89. if ($stock == false || $stock['usable_stock'] < $good_num) {
  90. return error_show(1003, "库存数量不足");
  91. }
  92. // if($stock['usable_stock']>0 && $stock['usable_stock']<$good_num){
  93. // return error_show(1003,"可用库存数量不足");
  94. // }
  95. // if($stock['usable_stock']== 0 && $stock['wait_in_stock']- $stock['presale_stock']<$good_num){
  96. // return error_show(1003,"待入库存数量不足");
  97. // }
  98. $origin_price = 0;
  99. } else {
  100. if ($is_activity == 0) {
  101. $origin = Db::name("good_nake")->where([["spuCode", "=", $spuCode], ["min_num", "<=", $good_num], ["is_del", "=", 0]])->order("min_num desc")->find();
  102. if ($origin == false) {
  103. return error_show(1003, "未找到相关阶梯成本价格");
  104. }
  105. $origin_price = $origin['nake_total'];
  106. }
  107. }
  108. $sale_price = isset($this->post['good_price']) && $this->post['good_price'] !== "" ? floatval($this->post['good_price']) : '';
  109. $ct['cgd_gold_price'] = 0;
  110. if ($goodtype == 1) {
  111. if ($is_activity == 1) {
  112. $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  113. ->where(["a.skuCode" => $skuCode, "a.activity_code" => $actcode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
  114. ->find();
  115. if ($act == false) {
  116. return error_show(1003, "未找到相关活动价");
  117. }
  118. if ($act['moq_num'] > $good_num) {
  119. return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  120. }
  121. if ($act['activity_stock'] < $good_num) {
  122. return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  123. }
  124. $sale_price = $sale_price !== "" ? $sale_price : $act['activity_price'];
  125. $origin_price = $act['cost_price'];
  126. } else {
  127. $good = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->where([["min_num", "<=", $good_num]])->order("min_num desc")->find();
  128. if ($good == false) {
  129. return error_show(1003, "未找到相关阶梯价格");
  130. }
  131. $sale_price = $sale_price !== "" ? $sale_price : $good['sale_price'];
  132. if ($ct['is_gold_price'] == 1 && $is_stock != 1) {
  133. $gold = Db::name("gold_price1")
  134. ->field('id,price')
  135. ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  136. ->order("addtime desc")
  137. ->find();
  138. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  139. $sale_price = $sale_price !== "" ? $sale_price : ($ct['demo_fee'] / $good_num + $ct['open_fee'] / $good_num
  140. + $ct['noble_weight'] * $gold["price"] + $good['cost_fee'] * $ct['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee']);
  141. $ct['cgd_gold_price'] = $gold["price"];
  142. }
  143. }
  144. } else {
  145. $sale_price = 0;
  146. }
  147. $cgd = [
  148. "supplierNo" => $ct['supplierNo'],
  149. "companyNo" => $supplierNo,
  150. "orderCode" => $orderCode,
  151. "spuCode" => $ct['spuCode'],
  152. "skuCode" => $ct['skuCode'],
  153. "good_name" => $ct['good_name'],
  154. "sale_price" => $origin_price,
  155. "total_fee" => $origin_price * $good_num,
  156. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  157. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  158. "open_fee" => $ct['open_fee'],
  159. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  160. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  161. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  162. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  163. "demo_fee" => $ct['demo_fee'],
  164. "good_num" => $good_num,
  165. "good_type" => $goodtype,
  166. "weight" => $ct['noble_weight'],
  167. "gold_price" => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  168. "order_type" => $is_stock == 1 ? 1 : 2,
  169. "order_source" => 1,//1直接下单
  170. "createrid" => $ct['createrid'],
  171. "creater" => $ct['creater'],
  172. 'send_way' => 2
  173. ];
  174. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !== "" ? $this->post['addrlist'] : "";
  175. $send_num = 0;
  176. if ($sendtype == 1) {
  177. if ($addrlist == "" || empty($addrlist) || !is_array($addrlist)) {
  178. return error_show(1004, "参数addrlist不能为空");
  179. }
  180. $send_num = array_sum(array_column($addrlist, "receipt_quantity"));
  181. }
  182. if ($send_num > $good_num) {
  183. return error_show(1004, "发货数量超出订单总数量");
  184. }
  185. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  186. $proof_id = isset($this->post['proof_id']) && $this->post['proof_id'] !== "" ? intval($this->post['proof_id']) : 0;
  187. $apply_id = GetUserInfo($token);
  188. if (empty($apply_id) || $apply_id['code'] != 0) {
  189. return error_show(1002, "申请人数据不存在");
  190. }
  191. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  192. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  193. Db::startTrans();
  194. try {
  195. $data = [
  196. "orderCode" => $orderCode,
  197. "good_code" => $spuCode,
  198. "skuCode" => $skuCode,
  199. "customer_code" => $customer_code,
  200. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  201. "good_num" => $good_num,
  202. "cat_id" => $goodinfo['cat_id'],
  203. "apply_id" => $rm,
  204. "apply_name" => $ri,
  205. "origin_price" => $origin_price,
  206. "sale_price" => $sale_price,
  207. "post_fee" => 0,
  208. "status" => 0,
  209. "send_num" => 0,
  210. "wsend_num" => $good_num,
  211. "send_status" => 0,
  212. "good_type" => $goodtype,
  213. "send_type" => $sendtype,
  214. "supplierNo" => $supplierNo,
  215. "is_del" => 0,
  216. "zxNo" => "",
  217. "platform_order" => $platform_order,
  218. "platform_id" => $platform_id,
  219. "remark" => $remark,
  220. "is_stock" => $is_stock,
  221. "is_activity" => $is_activity === "" ? 0 : $is_activity,
  222. "activity_code" => $actcode,
  223. "order_type" => $is_stock == 1 ? 1 : 2,
  224. "order_source" => 1,//1直接下单
  225. // "poNo"=>$poNo,
  226. 'good_weight' => $ct['noble_weight'],
  227. 'gold_price' => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  228. 'cost_price' => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  229. 'diff_weight' => 0,
  230. 'diff_fee' => 0,
  231. "workNo" => $workNo,
  232. "addtime" => date("Y-m-d H:i:s"),
  233. "updatetime" => date("Y-m-d H:i:s"),
  234. 'total_price' => round($sale_price * $good_num, 2),
  235. 'proof_id' => $proof_id
  236. ];
  237. $paytime == "" ? "" : $data['paytime'] = $paytime;
  238. $datainfo = Db::name('sale')->insert($data, true);
  239. if ($datainfo > 0) {
  240. //修改状态,添加待办
  241. ActionLog::logAdd($this->post['token'], [
  242. "order_code" => $orderCode,//销售单code
  243. "status" => 0,//这里的status是之前的值
  244. "action_remark" => '',//备注
  245. "action_type" => "create"//新建create,编辑edit,更改状态status
  246. ], "XSQRD", 0, $this->post);
  247. ProcessOrder::AddProcess($this->post['token'], [
  248. "order_type" => 'XSQRD',
  249. "order_code" => $orderCode,//销售单code
  250. "order_id" => $datainfo,
  251. "order_status" => 0, "before_status" => 0
  252. ]);
  253. if ($is_activity == 1) {
  254. $actup = [
  255. "activity_stock" => $act['activity_stock'] - $good_num,
  256. "updatetime" => date("Y-m-d H:i:s")
  257. ];
  258. $actupp = Db::name("activity_info")->where(["skuCode" => $skuCode, "activity_code" => $actcode,
  259. "is_del" => 0, "status" => 1])->save($actup);
  260. if ($actupp == false) {
  261. Db::rollback();
  262. return error_show(1002, "活动库存修改失败");
  263. }
  264. }
  265. $outstatus = 0;
  266. $standing_book_da = ['sale_id' => $datainfo, 'customer_code' => $customer_code, 'skuCode' => $skuCode, 'updatetime' => date('Y-m-d H:i:s')];
  267. if ($is_stock == 0) {
  268. $bol = $this->createCgd($cgd, $standing_book_da);
  269. if ($bol == false) {
  270. Db::rollback();
  271. return error_show(1002, "订单创建失败");
  272. }
  273. } else {
  274. $outstatus = 1;
  275. $bol = $this->RelaCgd([
  276. 'orderCode' => $orderCode,
  277. "good_num" => $good_num,
  278. "spuCode" => $spuCode,
  279. "companyNo" => $supplierNo,
  280. "cost_fee" => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  281. 'order_type' => $data['order_type'],
  282. 'order_source' => $data['order_source'],
  283. ], $standing_book_da);
  284. if ($bol == false) {
  285. Db::rollback();
  286. return error_show(1002, "库存商品关联采购单失败");
  287. }
  288. if (isset($stock)) {
  289. // if($stock['usable_stock']==0){
  290. // $stck = ["presale_stock"=>$stock['presale_stock']+$good_num,
  291. // "updatetime"=>date("Y-m-d H:i:s")
  292. // ];
  293. // }else{
  294. $stck = [
  295. "usable_stock" => $stock['usable_stock'] - $good_num,
  296. "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
  297. "updatetime" => date("Y-m-d H:i:s")
  298. ];
  299. // }
  300. $upad = Db::name("good_stock")->where($stock)->update($stck);
  301. if ($upad == false) {
  302. Db::rollback();
  303. return error_show(1002, "库存商品更新库存失败");
  304. }
  305. //商品变动日志表,good_log_code字段存储采购单号
  306. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  307. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  308. GoodLog::LogAdd($this->post['token'], $good_data, "XSQRD");
  309. }
  310. }
  311. if ($sendtype == 1) {
  312. $i=1;
  313. foreach ($addrlist as $value) {
  314. $temp = [];
  315. $addrs = [];
  316. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  317. $addrs['provice_code'] = $value['addr_code'][0];
  318. $addrs['city_code'] = $value['addr_code'][1];
  319. $addrs['area_code'] = $value['addr_code'][2];
  320. $addr = json_encode($addrs);
  321. } else {
  322. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  323. }
  324. $temp['orderCode'] = $orderCode;
  325. $temp['contactor'] = $value['contactor'];
  326. $temp['mobile'] = $value['mobile'];
  327. $temp['addr'] = $value['addr'];
  328. $temp['addr_code'] = $addr;
  329. $temp['customer_code'] = $customer_code;
  330. $temp['receipt_quantity'] = $value['receipt_quantity'];
  331. $temp['post_fee'] = 0;
  332. $temp['is_del'] = 0;
  333. $temp['addtime'] = date("Y-m-d H:i:s");
  334. $temp['updatetime'] = date("Y-m-d H:i:s");
  335. $temp['arrive_time'] = $arrtime;
  336. $vmp = Db::name('order_addr')->insert($temp, true);
  337. if ($vmp > 0) {
  338. // if($is_stock==1) {
  339. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  340. ->lock(true)->find();
  341. if ($order == false) {
  342. return error_show(1004, "未找到可以发货得采购单数据");
  343. }
  344. $num = $value['receipt_quantity'];
  345. $outCode = makeNo("DF");
  346. //改变编码规则,将原来的outCode后两位换成序列号
  347. //str_pad字符串填充
  348. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  349. $i++;
  350. $order['wsend_num'] -= $num;
  351. $order['send_num'] += $num;
  352. $or = Db::name("order_num")->save($order);
  353. if ($or == false) {
  354. Db::rollback();
  355. return error_show(1002, "发货地址添加创建失败");
  356. }
  357. $tep = [
  358. "cgdNo" => $order['cgdNo'],
  359. "outCode" => $outCode,
  360. "send_num" => $num,
  361. "status" => 1,
  362. "addtime" => date("Y-m-d H:i:s"),
  363. "updatetime" => date("Y-m-d H:i:s")
  364. ];
  365. $sen = Db::name("order_send")->save($tep);
  366. if ($sen == false) {
  367. Db::rollback();
  368. return error_show(1002, "发货地址添加创建失败");
  369. }
  370. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  371. if ($cgdinfo == false) {
  372. Db::rollback();
  373. return error_show(1002, "未匹配到采购数据");
  374. }
  375. $out = [
  376. "orderCode" => $orderCode,
  377. "outCode" => $outCode,
  378. "apply_id" => $rm,
  379. "apply_name" => $ri,
  380. "addrid" => $vmp,
  381. "post_name" => "",
  382. "post_code" => "",
  383. "post_fee" => 0,
  384. "sendtime" => date("Y-m-d H:i:s"),
  385. "send_num" => $num,
  386. "check_num" => 0,
  387. "error_num" => 0,
  388. "wsm_code" => $cgdinfo['wsm_code'],
  389. "order_type" => $is_stock == 1 ? 1 : 2,
  390. "status" => $outstatus,
  391. "addtime" => date("Y-m-d H:i:s"),
  392. "updatetime" => date("Y-m-d H:i:s")
  393. ];
  394. $ou = Db::name("order_out")->insertGetId($out);
  395. if ($ou == false) {
  396. Db::rollback();
  397. return error_show(1002, "发货地址添加创建失败");
  398. } else {
  399. //修改状态,添加待办
  400. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  401. "order_code" => $outCode,//出库单号
  402. "status" => $out['status'],//这里的status是之前的值
  403. "action_remark" => '',//备注
  404. "action_type" => "create"//新建create,编辑edit,更改状态status
  405. ], "CKD", $out['status'], $out);
  406. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  407. "order_type" => 'CKD',
  408. "order_code" => $outCode,//出库单号
  409. "order_id" => $ou,
  410. "order_status" => $out['status'], "before_status" => $out['status']
  411. ]);
  412. // $standing_book_da['outCode'] = $outCode;
  413. if (isset($standing_book_da['outCode'])) $standing_book_da['outCode'][] = $outCode;
  414. else $standing_book_da['outCode'] = [$outCode];
  415. }
  416. // }
  417. } else {
  418. Db::rollback();
  419. return error_show(1002, "发货地址添加创建失败");
  420. }
  421. }
  422. }
  423. //处理台账
  424. if (isset($standing_book_da['outCode'])) $standing_book_da['outCode'] = implode(',', $standing_book_da['outCode']);//发货单号拼接
  425. 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')]));
  426. Db::commit();
  427. return error_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
  428. }
  429. Db::rollback();
  430. return error_show(1002, "咨询订单创建失败");
  431. } catch (\Exception $e) {
  432. Db::rollback();
  433. return error_show(1005, $e->getMessage());
  434. }
  435. }
  436. //采购单
  437. public function createCgd($data, array &$standing_book_da = [])
  438. {
  439. $cgdCode = makeNo("CG");
  440. $supplier = Db::name("supplier")->where(["code" => $data['supplierNo'], "is_del" => 0])->find();
  441. if ($supplier == false) {
  442. return false;
  443. }
  444. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "wsm_type" => 2, "is_del" => 0])->find();
  445. if ($wsm == false) {
  446. $wsm_code = makeNo("WSM");
  447. $inwsm = [
  448. "wsm_code" => $wsm_code,
  449. "name" => $supplier['name'],
  450. "wsm_type" => 2,
  451. "supplierNo" => $supplier['code'],
  452. "addr" => "",
  453. "addrs_code" => "",
  454. "contactor" => $data['order_type'] == 1 ? $supplier['personid'] : $data['createrid'],
  455. "contactor_name" => $data['order_type'] == 1 ? $supplier['person'] : $data['creater'],
  456. "mobile" => "",
  457. "position" => "",
  458. "companyNo" => $data['companyNo'],
  459. "status" => 1,
  460. "is_del" => 0,
  461. "addtime" => date("Y-m-d H:i:s"),
  462. "updatetime" => date("Y-m-d H:i:s")
  463. ];
  464. $in = Db::name("warehouse_info")->insert($inwsm);
  465. if ($in == false) {
  466. return false;
  467. }
  468. } else {
  469. $wsm_code = $wsm['wsm_code'];
  470. }
  471. $cg = ["cgdNo" => $cgdCode,
  472. "bkcode" => "",
  473. "wsm_code" => $wsm_code,
  474. "cgder" => $data['creater'],
  475. "cgder_id" => $data['createrid'],
  476. "spuCode" => $data['spuCode'],
  477. "skuCode" => $data['skuCode'],
  478. "good_name" => $data['good_name'],
  479. "good_num" => $data['good_num'],
  480. "good_price" => $data['sale_price'],
  481. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  482. "pakge_fee" => $data['pakge_fee'],
  483. "cert_fee" => $data['cert_fee'],
  484. "open_fee" => $data['open_fee'],
  485. "teach_fee" => $data['cost_fee'],
  486. "mark_fee" => $data['mark_fee'],
  487. "demo_fee" => $data['demo_fee'],
  488. "nake_fee" => $data['nake_fee'],
  489. "weight" => $data['weight'],
  490. "delivery_fee" => $data['delivery_fee'],
  491. "gold_price" => $data['gold_price'],
  492. "diff_weight" => "0",
  493. "diff_fee" => "0",
  494. "supplierNo" => $data['supplierNo'],
  495. "supplier_name" => $supplier['name'],
  496. "companyNo" => $data['companyNo'],
  497. "send_status" => 1,
  498. "send_num" => 0,
  499. "wsend_num" => $data['good_num'],
  500. "remark" => "",
  501. "status" => 0,//0初始化
  502. "lasttime" => date("Y-m-d H:i:s"),
  503. "is_del" => 0,
  504. "order_type" => $data['order_type'],
  505. "order_source" => $data['order_source'],
  506. "good_type" => $data['good_type'],
  507. "addtime" => date("Y-m-d H:i:s"),
  508. "updatetime" => date("Y-m-d H:i:s")
  509. ];
  510. $up = Db::name("purchease_order")->insert($cg, true);
  511. if ($up) {
  512. //修改状态,添加待办
  513. ActionLog::logAdd($this->post['token'], [
  514. "order_code" => $cg['cgdNo'],//销售单code
  515. "status" => 0,//这里的status是之前的值
  516. "action_remark" => '',//备注
  517. "action_type" => "create"//新建create,编辑edit,更改状态status
  518. ], "CGD", $cg['status'], $cg);
  519. ProcessOrder::AddProcess($this->post['token'], [
  520. "order_type" => 'CGD',
  521. "order_code" => $cg['cgdNo'],//销售单code
  522. "order_id" => $up,
  523. "order_status" => $cg['status'], "before_status" => 0
  524. ]);
  525. $standing_book_da = array_merge($standing_book_da, [
  526. 'orderCode' => $data['orderCode'],
  527. 'cgdNo' => $cgdCode,
  528. 'spuCode' => $data['spuCode'],
  529. 'skuCode' => $data['skuCode'],
  530. 'order_type' => $data['order_type'],
  531. 'order_source' => $data['order_source'],
  532. 'supplierNo' => $data['supplierNo'],
  533. "companyNo" => $data['companyNo'],
  534. ]);
  535. $rela = [
  536. "orderCode" => $data['orderCode'],
  537. "cgdNo" => $cgdCode,
  538. "spuCode" => $data['spuCode'],
  539. "good_num" => $data['good_num'],
  540. "wsend_num" => $data['good_num'],
  541. "send_num" => 0,
  542. "wait_num" => 0,
  543. "status" => 1,
  544. "source" => 2
  545. ];
  546. $re = Db::name("order_num")->save($rela);
  547. if ($re == false) {
  548. return false;
  549. } else {
  550. $stokc = Db::name("good_stock")->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])->find();
  551. if ($stokc == false) {
  552. $stokc = [
  553. "spuCode" => $data['spuCode'],
  554. "wsm_code" => $wsm_code,
  555. "wait_in_stock" => $data['good_num'],
  556. "wait_out_stock" => 0,
  557. "usable_stock" => 0,
  558. "intra_stock" => 0,
  559. "total_stock" => 0,
  560. "status" => 1,
  561. "addtime" => date("Y-m-d H:i:s"),
  562. "updatetime" => date("Y-m-d H:i:s")
  563. ];
  564. } else {
  565. $stokc['wait_in_stock'] += $data['good_num'];
  566. $stokc['updatetime'] = date("Y-m-d H:i:s");
  567. }
  568. $stoc = Db::name("good_stock")->save($stokc);
  569. if ($stoc == false) {
  570. return false;
  571. }
  572. $good_data[] = ['good_log_code' => $cgdCode, "stock_id" => isset($stoc['id']) ? $stoc['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1,
  573. 'stock' => $data['good_num'], "stock_name" => "wait_in_stock"];
  574. GoodLog::LogAdd($this->post['token'], $good_data, "CGD");
  575. return true;
  576. }
  577. } else {
  578. return false;
  579. }
  580. }
  581. public function addAddr()
  582. {
  583. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  584. $etid = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->find();
  585. if (empty($etid)) {
  586. return error_show(1002, "未找到数据");
  587. }
  588. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  589. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  590. $apply_id = GetUserInfo($token);
  591. if (empty($apply_id) || $apply_id['code'] != 0) {
  592. return error_show(1002, "申请人数据不存在");
  593. }
  594. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  595. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  596. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !== "" ? $this->post['addrlist'] : "";
  597. if ($addrlist == "" || empty($addrlist) || !is_array($addrlist)) {
  598. return error_show(1004, "参数addrlist不能为空");
  599. }
  600. Db::startTrans();
  601. try {
  602. $i=1;
  603. foreach ($addrlist as $value) {
  604. $temp = [];
  605. $addrs = [];
  606. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  607. $addrs['provice_code'] = $value['addr_code'][0];
  608. $addrs['city_code'] = $value['addr_code'][1];
  609. $addrs['area_code'] = $value['addr_code'][2];
  610. $addr = json_encode($addrs);
  611. } else {
  612. $addr = isset($value['addr_code']) && $value['addr_code'] !== '' ? $value['addr_code'] : '';
  613. }
  614. $temp['orderCode'] = $orderCode;
  615. $temp['contactor'] = $value['contactor'];
  616. $temp['mobile'] = $value['mobile'];
  617. $temp['addr'] = $value['addr'];
  618. $temp['addr_code'] = $addr;
  619. $temp['customer_code'] = $etid['customer_code'];
  620. $temp['receipt_quantity'] = $value['receipt_quantity'];
  621. $temp['post_fee'] = 0;
  622. $temp['is_del'] = 0;
  623. $temp['addtime'] = date("Y-m-d H:i:s");
  624. $temp['updatetime'] = date("Y-m-d H:i:s");
  625. if ($arrtime != "") {
  626. $temp['arrive_time'] = $arrtime;
  627. }
  628. $vmp = Db::name('order_addr')->insert($temp, true);
  629. if ($vmp > 0) {
  630. $num = $value['receipt_quantity'];
  631. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  632. ->find();
  633. if (empty($order) || $order['wsend_num'] < $num) {
  634. return error_show(1004, "未找到可以发货得采购单数据");
  635. }
  636. $outCode = makeNo("DF");
  637. //改变编码规则,将原来的outCode后两位换成序列号
  638. //str_pad字符串填充
  639. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  640. $i++;
  641. $order['send_num'] += $num;
  642. $order['wsend_num'] -= $num;
  643. $or = Db::name("order_num")->save($order);
  644. if ($or == false) {
  645. Db::rollback();
  646. return error_show(1002, "发货地址添加创建失败");
  647. }
  648. $tep = [
  649. "cgdNo" => $order['cgdNo'],
  650. "outCode" => $outCode,
  651. "send_num" => $num,
  652. "status" => 1,
  653. "addtime" => date("Y-m-d H:i:s"),
  654. "updatetime" => date("Y-m-d H:i:s")
  655. ];
  656. $sen = Db::name("order_send")->save($tep);
  657. if ($sen == false) {
  658. Db::rollback();
  659. return error_show(1002, "发货地址添加创建失败");
  660. }
  661. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  662. if ($cgdinfo == false) {
  663. Db::rollback();
  664. return error_show(1002, "未匹配到采购数据");
  665. }
  666. //维护商品所在仓库的库存 --- start
  667. $temp = Db::name('good_stock')
  668. ->field('id,usable_stock,wait_out_stock')
  669. ->where(['spuCode' => $order['spuCode'], 'wsm_code' => $cgdinfo['wsm_code']])
  670. ->find();
  671. if ($etid['is_stock'] == 1) {
  672. if ($temp == false) {
  673. Db::rollback();
  674. return error_show(1004, '库存不足');
  675. }
  676. if (($temp['wait_out_stock'] - $num) < 0) {
  677. Db::rollback();
  678. return error_show(1004, '库存不足');
  679. }
  680. // $up =Db::name('good_stock')
  681. // ->where('id', $temp['id'])
  682. // ->update([
  683. // 'usable_stock' => $temp['usable_stock'] - $num,
  684. // 'wait_out_stock' => $temp['wait_out_stock'] + $num,
  685. // 'updatetime' => date('Y-m-d H:i:s'),
  686. // ]);
  687. // if($up==false){
  688. // Db::rollback();
  689. // return error_show(1004, '库存更新失败');
  690. // }
  691. // $good_data[] = ['good_log_code' =>$outCode, "stock_id" =>$temp['id'], "type" => 1,'stock'=>$num, "stock_name" => "wait_out_stock"];
  692. // $good_data[] = ['good_log_code' =>$outCode, "stock_id" =>$temp['id'], "type" => 2,'stock'=>$num, "stock_name" => "usable_stock"];
  693. // GoodLog::LogAdd($this->post['token'],$good_data,"CKD");
  694. $status = 1;
  695. } else {
  696. if ($temp == false || ($temp['usable_stock'] - $num) < 0) {
  697. $status = 0;
  698. } else {
  699. if (($temp['usable_stock'] - $num) < 0) {
  700. Db::rollback();
  701. return error_show(1004, '库存不足');
  702. }
  703. $up = Db::name('good_stock')
  704. ->where('id', $temp['id'])
  705. ->update([
  706. 'usable_stock' => $temp['usable_stock'] - $num,
  707. 'wait_out_stock' => $temp['wait_out_stock'] + $num,
  708. 'updatetime' => date('Y-m-d H:i:s'),
  709. ]);
  710. if ($up == false) {
  711. Db::rollback();
  712. return error_show(1004, '库存更新失败');
  713. }
  714. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 1, 'stock' => $num, "stock_name" => "wait_out_stock"];
  715. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 2, 'stock' => $num, "stock_name" => "usable_stock"];
  716. GoodLog::LogAdd($this->post['token'], $good_data, "CKD");
  717. $status = 1;
  718. }
  719. }
  720. $out = [
  721. "orderCode" => $orderCode,
  722. "outCode" => $outCode,
  723. "apply_id" => $rm,
  724. "apply_name" => $ri,
  725. "addrid" => $vmp,
  726. "post_name" => "",
  727. "post_code" => "",
  728. "post_fee" => 0,
  729. "sendtime" => date("Y-m-d H:i:s"),
  730. "send_num" => $num,
  731. "check_num" => 0,
  732. "error_num" => 1,
  733. "wsm_code" => $cgdinfo['wsm_code'],
  734. "order_type" => $etid['order_type'],
  735. "status" => $status,
  736. "addtime" => date("Y-m-d H:i:s"),
  737. "updatetime" => date("Y-m-d H:i:s")
  738. ];
  739. $ou = Db::name("order_out")->insertGetId($out);
  740. if ($ou == false) {
  741. Db::rollback();
  742. return error_show(1002, "发货地址添加创建失败");
  743. } else {
  744. //修改状态,添加待办
  745. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  746. "order_code" => $outCode,//出库单号
  747. "status" => 0,//这里的status是之前的值
  748. "action_remark" => '',//备注
  749. "action_type" => "create"//新建create,编辑edit,更改状态status
  750. ], "CKD", 0, $out);
  751. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  752. "order_type" => 'CKD',
  753. "order_code" => $outCode,//出库单号
  754. "order_id" => $ou,
  755. "order_status" => $status, "before_status" => 0
  756. ]);
  757. //处理台账-既然是根据orderCode更新的数据,那么肯定是有数据的,只需要更新就行了
  758. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),',{$outCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  759. }
  760. //维护商品所在仓库的库存 --- end
  761. } else {
  762. Db::rollback();
  763. return error_show(1002, "发货地址添加创建失败");
  764. }
  765. }
  766. Db::commit();
  767. return app_show(0, "发货地址添加完成");
  768. } catch (\Exception $e) {
  769. Db::rollback();
  770. return error_show(1002, $e->getMessage());
  771. }
  772. }
  773. public function consultOrder()
  774. {
  775. $bidNo = isset($this->post['bidNo']) && $this->post['bidNo'] != "" ? trim($this->post['bidNo']) : "";
  776. if ($bidNo == "") {
  777. return error_show(1003, "参数bidNo不能为空");
  778. }
  779. $zxinfo = Db::name("consult_bids")->where(["bidNo" => $bidNo, "is_del" => 0])->find();
  780. if ($zxinfo == false) {
  781. return error_show(1003, "未找到咨询单商品信息");
  782. }
  783. if ($zxinfo['status'] != 6) {
  784. return error_show(1003, "咨询单状态有误无法转单");
  785. }
  786. $zx = Db::name("consult_info")->where(["infoNo" => $zxinfo['infoNo'], "is_del" => 0])->find();
  787. if ($zx == false) {
  788. return error_show(1003, "未找到咨询单信息");
  789. }
  790. if ($zx['status'] != 4) {
  791. return error_show(1003, "咨询单状态有误无法转单");
  792. }
  793. $zxorder = Db::name("consult_order")->where(["zxNo" => $zx["zxNo"], "is_del" => 0])->find();
  794. if ($zxorder == false) {
  795. return error_show(1003, "未找到咨询单信息");
  796. }
  797. $good_num = isset($this->post['good_num']) && $this->post['good_num'] != "" ? intval($this->post['good_num']) : "";
  798. if ($good_num === "") {
  799. return error_show(1003, "参数 good_num 不能为空");
  800. }
  801. $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] != "" ? floatval($this->post['sale_price']) : "";
  802. if ($sale_price === "") {
  803. return error_show(1003, "参数 sale_price 销售单价 不能为空");
  804. }
  805. if ($sale_price < $zxinfo['sale_price']) {
  806. return error_show(1003, "修改的销售单价不能低于原来的销售单价");
  807. }
  808. $sendtype = isset($this->post['sendtype']) && $this->post['sendtype'] != "" ? intval($this->post['sendtype']) : "";
  809. if ($sendtype == "") {
  810. return error_show(1003, "参数sendtype不能为空");
  811. }
  812. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] != "" ? trim($this->post['companyNo']) : "";
  813. if ($companyNo == "") {
  814. return error_show(1003, "参数 companyNo 不能为空");
  815. }
  816. $company = Db::name("business")->where(["companyNo" => $companyNo, "is_del" => 0])->find();
  817. if ($company == false) {
  818. return error_show(1003, "业务公司数据未找到");
  819. }
  820. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  821. $orderCode = makeNo("QR");
  822. $spuCode = $zxinfo['spuCode'];
  823. $skuCode = "";
  824. $is_stock = 0;
  825. $cgd = [
  826. "supplierNo" => $zxinfo['supplierNo'],
  827. "companyNo" => $companyNo,
  828. "spuCode" => $spuCode,
  829. "skuCode" => $skuCode,
  830. "orderCode" => $orderCode,
  831. "good_name" => $zxinfo['good_name'],
  832. "sale_price" => $zxinfo['total_fee'],
  833. "total_fee" => round($zxinfo['total_fee'] * $good_num, 2),
  834. "pakge_fee" => $zxinfo['pakge_fee'],
  835. "cert_fee" => $zxinfo['cert_fee'],
  836. "open_fee" => $zxinfo['open_fee'],
  837. "cost_fee" => $zxinfo['cost_fee'],
  838. "mark_fee" => $zxinfo['mark_fee'],
  839. "demo_fee" => $zxinfo['demo_fee'],
  840. "nake_fee" => $zxinfo['nake_fee'],
  841. "delivery_fee" => $zxinfo['delivery_fee'],
  842. "good_num" => $good_num,
  843. "createrid" => $zxinfo['createrid'],
  844. "creater" => $zxinfo['creater'],
  845. "good_type" => 1,
  846. "weight" => $zxinfo['good_weight'],
  847. "gold_price" => $zxinfo['gold_price'],
  848. "is_diff" => $zxinfo['is_diff'],
  849. "order_type" => 3,
  850. "order_source" => 2,//咨询
  851. 'send_way' => $zxinfo['send_way']
  852. ];
  853. $token = isset($this->post['token']) && $this->post['token'] != "" ? trim($this->post['token']) : "";
  854. if ($token == "") {
  855. return error_show(102, "参数token不能为空");
  856. }
  857. $apply_id = GetUserInfo($token);
  858. if (empty($apply_id) || $apply_id['code'] != 0) {
  859. return error_show(102, "申请人数据不存在");
  860. }
  861. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  862. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  863. $va = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  864. $send_num = 0;
  865. if ($sendtype == 1) {
  866. if ($va == "") {
  867. return error_show(1002, "参数order_addr不能为空");
  868. }
  869. $send_num = array_sum(array_column($va, "receipt_quantity"));
  870. }
  871. if ($good_num < $send_num) {
  872. return error_show(1002, "发货数量不能超过购买数量");
  873. }
  874. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  875. if ($arrtime == "") {
  876. return error_show(1002, "参数arrtime不能为空");
  877. }
  878. //新加字段
  879. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] != "" ? $this->post['platform_order'] : "";
  880. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? $this->post['workNo'] : "";
  881. $proof_type = isset($this->post['proof_type']) && $this->post['proof_type'] != "" ? intval($this->post['proof_type']) : 0;
  882. $proof_url = isset($this->post['proof_url']) && $this->post['proof_url'] != "" ? trim($this->post['proof_url']) : '';
  883. Db::startTrans();
  884. try {
  885. $zx = Db::name("consult_info")->where(["infoNo" => $zxinfo['infoNo'], "is_del" => 0])->lock(true)->find();
  886. if ($zx == false) {
  887. Db::rollback();
  888. return error_show(1003, "未找到咨询单信息");
  889. }
  890. if ($companyNo != $zxorder['companyNo']) {
  891. $zxord = Db::name("consult_order")->where($zxorder)->update(["companyNo" => $companyNo, "updatetime" => date("Y-m-d H:i:s")]);
  892. if ($zxord == false) {
  893. Db::rollback();
  894. return error_show(1003, "咨询单更新业务公司失败");
  895. }
  896. }
  897. $data = [
  898. "orderCode" => $orderCode,
  899. "good_code" => $spuCode,
  900. "skuCode" => $skuCode,
  901. "customer_code" => $zxorder['khNo'],
  902. "good_name" => isset($zxinfo['good_name']) && $zxinfo['good_name'] !== '' ? $zxinfo['good_name'] : '',
  903. "good_num" => $good_num,
  904. "cat_id" => $zxinfo['cat_id'],
  905. "apply_id" => $rm,
  906. "apply_name" => $ri,
  907. "origin_price" => $zxinfo['total_fee'],
  908. "sale_price" => $sale_price,//销售单价支持修改
  909. "post_fee" => 0,
  910. "status" => 0,
  911. "send_num" => 0,
  912. "wsend_num" => $good_num,
  913. "send_status" => 1,
  914. "good_type" => 1,
  915. "send_type" => $sendtype,
  916. "supplierNo" => $companyNo,
  917. "is_del" => 0,
  918. "zxNo" => $bidNo,
  919. "platform_order" => $platform_order,
  920. "platform_id" => $zxorder['platform_code'],
  921. "remark" => $remark,
  922. "arrive_time" => $arrtime,
  923. "is_stock" => 0,
  924. "is_activity" => 0,
  925. "activity_code" => '',
  926. "proof_id" => 0,
  927. "order_type" => 3,
  928. "order_source" => 2,//2咨询
  929. 'good_weight' => $zxinfo['good_weight'],
  930. 'gold_price' => $zxinfo['gold_price'],
  931. 'cost_price' => $zxinfo['sale_cost_fee'],
  932. 'diff_weight' => 0,
  933. 'diff_fee' => 0,
  934. "addtime" => date("Y-m-d H:i:s"),
  935. "updatetime" => date("Y-m-d H:i:s"),
  936. 'total_price' => round($sale_price * $good_num, 2),
  937. 'workNo' => $workNo,
  938. ];
  939. $datainfo = Db::name('sale')->insert($data, true);
  940. if ($datainfo > 0) {
  941. $standing_book_data = [
  942. 'sale_id' => $datainfo,
  943. 'infoNo' => $zxinfo['infoNo'],
  944. 'updatetime' => date('Y-m-d H:i:s')
  945. ];
  946. //修改状态,添加待办
  947. ActionLog::logAdd($this->post['token'], [
  948. "order_code" => $orderCode,//销售单code
  949. "status" => 0,//这里的status是之前的值
  950. "action_remark" => '',//备注
  951. "action_type" => "create"//新建create,编辑edit,更改状态status
  952. ], "XSQRD", $data['status'], $data);
  953. ProcessOrder::AddProcess($this->post['token'], [
  954. "order_type" => 'XSQRD',
  955. "order_code" => $orderCode,//销售单code
  956. "order_id" => $datainfo,
  957. "order_status" => $data['status'], "before_status" => 0
  958. ]);
  959. $old_zx_status = $zx['status'];
  960. $sa = Db::name("consult_info")->where($zx)->save(["status" => 5, "updatetime" => date("Y-m-d H:i:s")]);
  961. if ($sa == false) {
  962. Db::rollback();
  963. return error_show(1002, "咨询单修改失败");
  964. } else {
  965. //修改状态,添加待办
  966. ActionLog::logAdd($this->post['token'], [
  967. "order_code" => $zx['infoNo'],//咨询单详情编号
  968. "status" => $old_zx_status,//这里的status是之前的值
  969. "action_remark" => '',//备注
  970. "action_type" => "status"//新建create,编辑edit,更改状态status
  971. ], "ZXD", 5, $zx);
  972. }
  973. $bol = $this->createCgd($cgd, $standing_book_data);
  974. if ($bol == false) {
  975. Db::rollback();
  976. return error_show(1002, "咨询订单创建失败");
  977. }
  978. $limt = [
  979. "spuCode" => $zxinfo['spuCode'],
  980. "good_name" => $zxinfo['good_name'],
  981. "brand_id" => $zxinfo['brand_id'],
  982. "good_unit" => $zxinfo['unit_id'],
  983. "cat_id" => $zxinfo['cat_id'],
  984. "good_type" => 0,
  985. "moq" => 1,
  986. "customized" => $zxinfo['work_day'],
  987. "tax" => $zxinfo['tax'],
  988. "supplierNo" => $zxinfo["supplierNo"],
  989. "is_auth" => 0,
  990. "craft_desc" => $zxinfo['good_name'],
  991. "good_remark" => "",
  992. "platform_id" => $zxorder['platform_id'],
  993. "good_img" => $zxinfo['good_img'],
  994. "good_thumb_img" => "",
  995. "good_info_img" => "",
  996. "specinfo" => $zxinfo['specinfo'],
  997. "work_day" => $zxinfo['work_day'],
  998. "noble_metal" => $zxinfo['metal_id'],
  999. "is_gold_price" => $zxinfo['is_gold_price'],
  1000. "config" => $zxinfo['config'],
  1001. "other_config" => $zxinfo['other_config'],
  1002. "weight" => $zxinfo['weight'],
  1003. "good_weight" => $zxinfo['good_weight'],
  1004. "is_diff" => $zxinfo['is_diff'],
  1005. "supply_area" => $zxinfo['supply_area'],
  1006. "pay_way" => $zxinfo['pay_way'],
  1007. "send_way" => $zxinfo['send_way'],
  1008. "companyNo" => $companyNo,
  1009. "status" => 1,
  1010. "is_del" => 0,
  1011. "createrid" => $zxinfo['createrid'],
  1012. "creater" => $zxinfo['creater'],
  1013. "addtime" => date("Y-m-d H:i:s"),
  1014. "updatetime" => date("Y-m-d H:i:s"),
  1015. 'proof_type' => $proof_type,//凭证类型
  1016. 'proof_url' => $proof_url,//凭证文件
  1017. ];
  1018. $good = Db::name("good_zixun")->insert($limt);
  1019. if (!$good) {
  1020. Db::rollback();
  1021. return error_show(1006, "咨询商品录入失败");
  1022. }
  1023. if (!empty($va)) {
  1024. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1025. ->find();
  1026. if ($order == false) {
  1027. Db::rollback();
  1028. return error_show(1006, "采购单关联数据未找到");
  1029. }
  1030. $i = 1;
  1031. foreach ($va as $value) {
  1032. $temp = [];
  1033. $addrs = [];
  1034. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  1035. $addrs['provice_code'] = $value['addr_code'][0];
  1036. $addrs['city_code'] = $value['addr_code'][1];
  1037. $addrs['area_code'] = $value['addr_code'][2];
  1038. $addr = json_encode($addrs);
  1039. } else {
  1040. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1041. }
  1042. $temp['orderCode'] = $orderCode;
  1043. $temp['contactor'] = $value['contactor'];
  1044. $temp['mobile'] = $value['mobile'];
  1045. $temp['addr'] = $value['addr'];
  1046. $temp['addr_code'] = $addr;
  1047. $temp['customer_code'] = $zxorder['khNo'];
  1048. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1049. $temp['post_fee'] = 0;
  1050. $temp['is_del'] = 0;
  1051. $temp['addtime'] = date("Y-m-d H:i:s");
  1052. $temp['updatetime'] = date("Y-m-d H:i:s");
  1053. $temp['arrive_time'] = $arrtime;
  1054. $vmp = Db::name('order_addr')->insert($temp, true);
  1055. if ($vmp > 0) {
  1056. $outCode = makeNo("DF");
  1057. //改变编码规则,将原来的outCode后两位换成序列号
  1058. //str_pad字符串填充
  1059. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  1060. $i++;
  1061. $out = [
  1062. "orderCode" => $orderCode,
  1063. "outCode" => $outCode,
  1064. "apply_id" => $rm,
  1065. "apply_name" => $ri,
  1066. "addrid" => $vmp,
  1067. "post_name" => "",
  1068. "post_code" => "",
  1069. "post_fee" => 0,
  1070. "sendtime" => date("Y-m-d H:i:s"),
  1071. "send_num" => $value['receipt_quantity'],
  1072. "check_num" => 0,
  1073. "error_num" => 0,
  1074. "wsm_code" => "",
  1075. "order_type" => 3,
  1076. "status" => 0,
  1077. "addtime" => date("Y-m-d H:i:s"),
  1078. "updatetime" => date("Y-m-d H:i:s")
  1079. ];
  1080. $ou = Db::name("order_out")->insertGetId($out);
  1081. if ($ou == false) {
  1082. Db::rollback();
  1083. return error_show(1002, "发货地址添加创建失败");
  1084. } else {
  1085. //修改状态,添加待办
  1086. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  1087. "order_code" => $outCode,//出库单号
  1088. "status" => 0,//这里的status是之前的值
  1089. "action_remark" => '',//备注
  1090. "action_type" => "create"//新建create,编辑edit,更改状态status
  1091. ], "CKD", 0, $out);
  1092. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  1093. "order_type" => 'CKD',
  1094. "order_code" => $outCode,//出库单号
  1095. "order_id" => $ou,
  1096. "order_status" => 0, "before_status" => 0
  1097. ]);
  1098. //将发货编号添加到台账中
  1099. $standing_book_data['outCode'][] = $outCode;
  1100. }
  1101. $order['send_num'] += $value['receipt_quantity'];
  1102. $order['wsend_num'] -= $value['receipt_quantity'];
  1103. if ($order['wsend_num'] < 0) {
  1104. Db::rollback();
  1105. return error_show(1002, "发货数量已超出总数");
  1106. }
  1107. $ups = Db::name("order_num")->save($order);
  1108. if ($ups) {
  1109. $tep = [
  1110. "cgdNo" => $order['cgdNo'],
  1111. "outCode" => $outCode,
  1112. "send_num" => $value['receipt_quantity'],
  1113. "status" => 1,
  1114. "addtime" => date("Y-m-d H:i:s"),
  1115. "updatetime" => date("Y-m-d H:i:s")
  1116. ];
  1117. $sen = Db::name("order_send")->save($tep);
  1118. if ($sen == false) {
  1119. Db::rollback();
  1120. return error_show(1002, "发货地址添加创建失败");
  1121. }
  1122. }
  1123. } else {
  1124. Db::rollback();
  1125. return error_show(1002, "发货地址添加创建失败");
  1126. }
  1127. }
  1128. }
  1129. //处理台账
  1130. if (isset($standing_book_data['outCode'])) {
  1131. 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']}'");
  1132. unset($standing_book_data['outCode']);
  1133. }
  1134. Db::name('standing_book')->where('infoNo', $standing_book_data['infoNo'])->update($standing_book_data);
  1135. Db::commit();
  1136. return error_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
  1137. }
  1138. Db::rollback();
  1139. return error_show(1002, "咨询订单创建失败");
  1140. } catch (\Exception $e) {
  1141. Db::rollback();
  1142. return error_show(1003, $e->getMessage());
  1143. }
  1144. }
  1145. /**
  1146. * @return \think\response\Json|void
  1147. * @throws \think\db\exception\DataNotFoundException
  1148. * @throws \think\db\exception\DbException
  1149. * @throws \think\db\exception\ModelNotFoundException
  1150. */
  1151. public function projectCreate()
  1152. {
  1153. $planNo = isset($this->post['planNo']) && $this->post['planNo'] != "" ? trim($this->post['planNo']) : "";
  1154. if ($planNo == "") {
  1155. return error_show(1004, "参数planNo不能为空");
  1156. }
  1157. $plan = Db::name("project_plan")->where(["planNo" => $planNo, "is_del" => 0])->find();
  1158. if ($plan == false) {
  1159. return error_show(1004, "未找到方案信息");
  1160. }
  1161. $feedbackid = Db::name("project_plan_rela")->where(["planNo" => $planNo, "is_del" => 0])->column("feedback_id,sale_price,sale_num");
  1162. if ($feedbackid == false) {
  1163. return error_show(1004, "未找到方案反馈信息");
  1164. }
  1165. $project = Db::name("project")->where(["projectNo" => $plan['projectNo'], "is_del" => 0])->find();
  1166. if ($project == false) {
  1167. return error_show(1004, "未找到方案信息");
  1168. }
  1169. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  1170. if ($arrtime == "") {
  1171. return error_show(1002, "参数arrtime不能为空");
  1172. }
  1173. $planinfo = isset($this->post['planinfo']) && !empty($this->post['planinfo']) ? $this->post['planinfo'] : "";
  1174. if ($planinfo == "") {
  1175. return error_show(1002, "参数planinfo不能为空");
  1176. }
  1177. $token = isset($this->post['token']) && $this->post['token'] != "" ? trim($this->post['token']) : "";
  1178. if ($token == "") {
  1179. return error_show(102, "参数token不能为空");
  1180. }
  1181. $apply_id = GetUserInfo($token);
  1182. if (empty($apply_id) || $apply_id['code'] != 0) {
  1183. return error_show(102, "申请人数据不存在");
  1184. }
  1185. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1186. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1187. Db::startTrans();
  1188. try {
  1189. $plan = Db::name("project_plan")->where(["planNo" => $planNo, "is_del" => 0])->lock(true)->find();
  1190. if ($plan == false) {
  1191. Db::rollback();
  1192. return error_show(1004, "未找到方案信息");
  1193. }
  1194. $feedid = array_column($feedbackid, "feedback_id");
  1195. foreach ($planinfo as $key => $value) {
  1196. $good = [];
  1197. $feedback = Db::name("project_feedback")->where(["id" => $value['feedback_id'], "projectNo" => $plan['projectNo'],
  1198. "is_del" => 0])->find();
  1199. if ($feedback == false) {
  1200. Db::rollback();
  1201. return error_show(1002, "反馈数据未找到");
  1202. }
  1203. $key = array_search($feedback['id'], $feedid);
  1204. $price = $feedbackid[$key];
  1205. $good['spuCode'] = $feedback['spuCode'];
  1206. $good['skuCode'] = $feedback['skuCode'];
  1207. $good['good_num'] = $price['sale_num'];
  1208. $good['sendtype'] = $value['sendtype'];
  1209. $good['sale_price'] = $price['sale_price'];
  1210. $good['origin_price'] = $feedback['origin_price'];
  1211. $good['companyNo'] = $project['companyNo'];
  1212. $good['customerNo'] = $project['khNo'];
  1213. $good['pgNo'] = $feedback['pgNo'];
  1214. $good['platform_id'] = $project['platform_id'];
  1215. $good['arrtime'] = $arrtime;
  1216. $good['rm'] = $rm;
  1217. $good['ri'] = $ri;
  1218. $good['send_num'] = 0;
  1219. $standing_book_data = ['projectNo' => $plan['projectNo'], 'updatetime' => date('Y-m-d H:i:s')];
  1220. if ($value['sendtype'] == 1) {
  1221. if (!isset($value['addrlist']) || empty($value['addrlist'])) {
  1222. Db::rollback();
  1223. return error_show(1002, "订单地址不能为空");
  1224. }
  1225. $good['addrlist'] = $value['addrlist'];
  1226. $good['send_num'] = array_sum(array_column($value['addrlist'], "receipt_quantity"));
  1227. }
  1228. if ($feedback['data_source'] == 1) {
  1229. $feed = $this->project_good($good, $standing_book_data);
  1230. if ($feed !== true) {
  1231. Db::rollback();
  1232. switch ($feed) {
  1233. case 1:
  1234. $msg = '库存不足';
  1235. break;
  1236. case 2:
  1237. $msg = '库存更新失败';
  1238. break;
  1239. case 3:
  1240. $msg = '关联采购失败';
  1241. break;
  1242. case 4:
  1243. $msg = '商品信息未找到';
  1244. break;
  1245. default:
  1246. $msg = '咨询订单生成失败';
  1247. }
  1248. return error_show(1002, $msg);
  1249. }
  1250. } else {
  1251. $feed = $this->project_zx($good, $standing_book_data);
  1252. if ($feed == false) {
  1253. Db::rollback();
  1254. return error_show(1002, "咨询订单生成失败");
  1255. }
  1256. }
  1257. $old_fk_status = $feedback['status'];
  1258. $fed = Db::name("project_feedback")->where($feedback)->save(['status' => 2, "updatetime" => date("Y-m-d H:i:s")]);
  1259. if ($fed == false) {
  1260. Db::rollback();
  1261. return error_show(1002, "项目订单生成失败");
  1262. }
  1263. //处理台账(一个项目会生成多个销售订单,第一个更新,其余的新增)
  1264. //项目创建时已有供应商和企业,不能修改
  1265. if (isset($standing_book_data['companyNo'])) unset($standing_book_data['companyNo']);
  1266. if (isset($standing_book_data['customer_code'])) unset($standing_book_data['customer_code']);
  1267. if (isset($standing_book_data['outCode'])) $standing_book_data['outCode'] = implode(',', $standing_book_data['outCode']);
  1268. //普通商品
  1269. if ($feedback['data_source'] == 1) {
  1270. if ($key == 0) Db::name('standing_book')->where('projectNo', $plan['projectNo'])->update($standing_book_data);
  1271. else {
  1272. $temp = Db::name('standing_book')->where('projectNo', $plan['projectNo'])->find();
  1273. if (!empty($temp)) {
  1274. unset($temp['id']);
  1275. $temp = array_merge($temp, $standing_book_data);
  1276. $temp['standBookNo'] = makeNo('IO');
  1277. Db::name('standing_book')->insert($temp);
  1278. }
  1279. }
  1280. } else {
  1281. //项目类竞价单
  1282. Db::name('standing_book')->where(['projectNo' => $plan['projectNo'], 'infoNo' => $standing_book_data['infoNo']])->update($standing_book_data);
  1283. }
  1284. }
  1285. $old_plan_status = $plan['status'];
  1286. // $plan['status']=2;
  1287. // $plan['updatetime']=date("Y-m-d H:i:s");
  1288. $in = Db::name("project_plan")->where($plan)->save(['status' => 2, "updatetime" => date("Y-m-d H:i:s")]);
  1289. if ($in == false) {
  1290. Db::rollback();
  1291. return error_show(1002, "项目订单生成失败");
  1292. } else {
  1293. //修改状态,添加待办,只记录动作
  1294. ActionLog::logAdd($this->post['token'], [
  1295. "order_code" => $planNo,//编码
  1296. "status" => $old_plan_status,//这里的status是之前的值
  1297. "action_remark" => '',//备注
  1298. "action_type" => "edit"//新建create,编辑edit,更改状态status
  1299. ], "PRP", 2, $plan);
  1300. }
  1301. $old_project_status = $project["status"];
  1302. // $project["status"]=6;
  1303. // $project['updatetime']=date("Y-m-d H:i:s");
  1304. $pr = Db::name("project")->where($project)->save(["updatetime" => date("Y-m-d H:i:s"), "status" => 6]);
  1305. if ($pr == false) {
  1306. Db::rollback();
  1307. return error_show(1002, "项目订单生成失败");
  1308. } else {
  1309. //修改状态,添加待办
  1310. ActionLog::logAdd($this->post['token'], [
  1311. "order_code" => $project['projectNo'],//项目编码
  1312. "status" => $old_project_status,//这里的status是之前的值
  1313. "action_remark" => '',//备注
  1314. "action_type" => "status"//新建create,编辑edit,更改状态status
  1315. ], "PRO", 6, $project);
  1316. ProcessOrder::AddProcess($this->post['token'], [
  1317. "order_type" => 'PRO',
  1318. "order_code" => $project['projectNo'],
  1319. "order_id" => $project['id'],
  1320. "order_status" => 6, "before_status" => $old_project_status
  1321. ]);
  1322. }
  1323. //修改状态,添加待办,只记录动作
  1324. ActionLog::logAdd($this->post['token'], [
  1325. "order_code" => $project['projectNo'],//编码,这里是project的编码
  1326. "status" => 1,//这里的status是之前的值
  1327. "action_remark" => '',//备注
  1328. "action_type" => "create"//新建create,编辑edit,更改状态status
  1329. ], "PRT", 2, $this->post);
  1330. $process = ["order_code" => $project['projectNo'], "order_id" => $project['id'], "order_status" => 2, "order_type" => "PRT", "before_status" => 1];
  1331. ProcessOrder::AddProcess($this->post['token'], $process);
  1332. Db::commit();
  1333. return app_show(0, "咨询订单生成成功");
  1334. } catch (\Exception $e) {
  1335. Db::rollback();
  1336. return error_show(1002, $e->getMessage());
  1337. }
  1338. }
  1339. /**
  1340. * @param $data
  1341. * @return false
  1342. * @throws \think\db\exception\DataNotFoundException
  1343. * @throws \think\db\exception\DbException
  1344. * @throws \think\db\exception\ModelNotFoundException
  1345. *
  1346. */
  1347. public function project_zx($data, array &$standing_book_data = [])
  1348. {
  1349. $goodinfo = Db::name("consult_bids")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->find();
  1350. if ($goodinfo == false) {
  1351. return false;
  1352. }
  1353. $orderCode = makeNo("QR");
  1354. $cgd = [
  1355. "supplierNo" => $goodinfo['supplierNo'],
  1356. "companyNo" => $data['companyNo'],
  1357. "spuCode" => $data['spuCode'],
  1358. "skuCode" => "",
  1359. "orderCode" => $orderCode,
  1360. "good_name" => $goodinfo['good_name'],
  1361. "sale_price" => $goodinfo['total_fee'],
  1362. "total_fee" => round($goodinfo['total_fee'] * $data['good_num'], 2),
  1363. "pakge_fee" => $goodinfo['pakge_fee'],
  1364. "cert_fee" => $goodinfo['cert_fee'],
  1365. "open_fee" => $goodinfo['open_fee'],
  1366. "cost_fee" => $goodinfo['cost_fee'],
  1367. "mark_fee" => $goodinfo['mark_fee'],
  1368. "demo_fee" => $goodinfo['demo_fee'],
  1369. "nake_fee" => $goodinfo['nake_fee'],
  1370. "good_num" => $data['good_num'],
  1371. "delivery_fee" => $goodinfo['delivery_fee'],
  1372. "createrid" => $goodinfo['createrid'],
  1373. "creater" => $goodinfo['creater'],
  1374. "weight" => $goodinfo['good_weight'],
  1375. "gold_price" => $goodinfo['gold_price'],
  1376. "good_type" => 1,
  1377. "order_type" => 3,//3咨询商品
  1378. "order_source" => 3,//项目
  1379. "send_way" => $goodinfo['send_way'],
  1380. ];
  1381. $datas = [
  1382. "orderCode" => $orderCode,
  1383. "good_code" => $data['spuCode'],
  1384. "skuCode" => "",
  1385. "customer_code" => $data['customerNo'],
  1386. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  1387. "good_num" => $data['good_num'],
  1388. "cat_id" => $goodinfo['cat_id'],
  1389. "apply_id" => $data['rm'],
  1390. "apply_name" => $data['ri'],
  1391. "origin_price" => $goodinfo['total_fee'],
  1392. "sale_price" => $data['sale_price'],
  1393. "post_fee" => 0,
  1394. "status" => 0,
  1395. "send_num" => 0,
  1396. "wsend_num" => $data['good_num'],
  1397. "send_status" => 1,
  1398. "good_type" => 1,
  1399. "send_type" => $data['sendtype'],
  1400. "supplierNo" => $data['companyNo'],
  1401. "is_del" => 0,
  1402. "zxNo" => $data["pgNo"],
  1403. "platform_order" => "",
  1404. "platform_id" => $data['platform_id'],
  1405. "remark" => "",
  1406. "is_stock" => 0,
  1407. "is_activity" => 0,
  1408. "activity_code" => '',
  1409. "proof_id" => 0,
  1410. "order_type" => 3,//咨询商品
  1411. "order_source" => 3,//项目
  1412. 'good_weight' => $goodinfo['good_weight'],
  1413. 'gold_price' => $goodinfo['gold_price'],
  1414. 'cost_price' => $goodinfo['sale_cost_fee'],
  1415. 'diff_weight' => 0,
  1416. 'diff_fee' => 0,
  1417. "addtime" => date("Y-m-d H:i:s"),
  1418. "updatetime" => date("Y-m-d H:i:s"),
  1419. 'total_price' => round($data['sale_price'] * $data['good_num'], 2),
  1420. ];
  1421. $datainfo = Db::name('sale')->insert($datas, true);
  1422. if ($datainfo > 0) {
  1423. $standing_book_data['sale_id'] = $datainfo;
  1424. $standing_book_data['customer_code'] = $datas['customer_code'];
  1425. $standing_book_data['infoNo'] = $goodinfo['infoNo'];
  1426. //修改状态,添加待办
  1427. ActionLog::logAdd($this->post['token'], [
  1428. "order_code" => $orderCode,//项目编码
  1429. "status" => 0,//这里的status是之前的值
  1430. "action_remark" => '',//备注
  1431. "action_type" => "status"//新建create,编辑edit,更改状态status
  1432. ], "XSQRD", 0, $datas);
  1433. ProcessOrder::AddProcess($this->post['token'], [
  1434. "order_type" => 'XSQRD',
  1435. "order_code" => $orderCode,
  1436. "order_id" => $datainfo,
  1437. "order_status" => 0, "before_status" => 0
  1438. ]);
  1439. $bol = $this->createCgd($cgd, $standing_book_data);
  1440. if ($bol == false) {
  1441. return false;
  1442. }
  1443. $bidstatus = $goodinfo['status'];
  1444. $goodinfo['status'] = 6;
  1445. $goodinfo['updatetime'] = date("Y-m-d H:i:s");
  1446. $sa = Db::name("consult_bids")->save($goodinfo);
  1447. if ($sa == false) {
  1448. return false;
  1449. }
  1450. //修改状态,添加待办
  1451. ActionLog::logAdd($this->post['token'], [
  1452. "order_code" => $goodinfo['bidNo'],//项目编码
  1453. "status" => $bidstatus,//这里的status是之前的值
  1454. "action_remark" => '',//备注
  1455. "action_type" => "status"//新建create,编辑edit,更改状态status
  1456. ], "FKD", 6, $datas);
  1457. $zxorder = Db::name("consult_order")->where(["zxNo" => $goodinfo["zxNo"], "is_del" => 0])->find();
  1458. if ($zxorder == false) {
  1459. return false;
  1460. }
  1461. $limt = [
  1462. "spuCode" => $goodinfo['spuCode'],
  1463. "good_name" => $goodinfo['good_name'],
  1464. "brand_id" => $goodinfo['brand_id'],
  1465. "good_unit" => $goodinfo['unit_id'],
  1466. "cat_id" => $goodinfo['cat_id'],
  1467. "good_type" => 0,
  1468. "moq" => 1,
  1469. "customized" => $goodinfo['work_day'],
  1470. "tax" => $goodinfo['tax'],
  1471. "supplierNo" => $goodinfo["supplierNo"],
  1472. "is_auth" => 0,
  1473. "craft_desc" => $goodinfo['good_name'],
  1474. "good_remark" => "",
  1475. "good_img" => $goodinfo['good_img'],
  1476. "good_thumb_img" => "",
  1477. "good_info_img" => "",
  1478. "platform_id" => $zxorder['platform_id'],
  1479. "specinfo" => $goodinfo['specinfo'],
  1480. "work_day" => $goodinfo['work_day'],
  1481. "noble_metal" => $goodinfo['metal_id'],
  1482. "is_gold_price" => $goodinfo['is_gold_price'],
  1483. "good_weight" => $goodinfo['good_weight'],
  1484. "companyNo" => $zxorder['companyNo'],
  1485. "config" => $goodinfo['config'],
  1486. "other_config" => $goodinfo['other_config'],
  1487. "weight" => $goodinfo['weight'],
  1488. "is_diff" => $goodinfo['is_diff'],
  1489. "supply_area" => $goodinfo['supply_area'],
  1490. "pay_way" => $goodinfo['pay_way'],
  1491. "send_way" => $goodinfo['send_way'],
  1492. "status" => 1,
  1493. "is_del" => 0,
  1494. "createrid" => $goodinfo['createrid'],
  1495. "creater" => $goodinfo['creater'],
  1496. "addtime" => date("Y-m-d H:i:s"),
  1497. "updatetime" => date("Y-m-d H:i:s")
  1498. ];
  1499. $good = Db::name("good_zixun")->insert($limt);
  1500. if (!$good) {
  1501. return false;
  1502. }
  1503. if ($data['sendtype'] == 1 && !empty($data['addrlist'])) {
  1504. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1505. ->find();
  1506. if ($order == false) {
  1507. return false;
  1508. }
  1509. $i = 1;
  1510. foreach ($data['addrlist'] as $value) {
  1511. $temp = [];
  1512. $addrs = [];
  1513. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  1514. $addrs['provice_code'] = $value['addr_code'][0];
  1515. $addrs['city_code'] = $value['addr_code'][1];
  1516. $addrs['area_code'] = $value['addr_code'][2];
  1517. $addr = json_encode($addrs);
  1518. } else {
  1519. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1520. }
  1521. $temp['orderCode'] = $orderCode;
  1522. $temp['contactor'] = $value['contactor'];
  1523. $temp['mobile'] = $value['mobile'];
  1524. $temp['addr'] = $value['addr'];
  1525. $temp['addr_code'] = $addr;
  1526. $temp['customer_code'] = $data['companyNo'];
  1527. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1528. $temp['post_fee'] = 0;
  1529. $temp['is_del'] = 0;
  1530. $temp['addtime'] = date("Y-m-d H:i:s");
  1531. $temp['updatetime'] = date("Y-m-d H:i:s");
  1532. $temp['arrive_time'] = $data['arrtime'];
  1533. $vmp = Db::name('order_addr')->insert($temp, true);
  1534. if ($vmp > 0) {
  1535. $outCode = makeNo("DF");
  1536. //改变编码规则,将原来的outCode后两位换成序列号
  1537. //str_pad字符串填充
  1538. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  1539. $i++;
  1540. $out = [
  1541. "orderCode" => $orderCode,
  1542. "outCode" => $outCode,
  1543. "apply_id" => $data['rm'],
  1544. "apply_name" => $data['ri'],
  1545. "addrid" => $vmp,
  1546. "post_name" => "",
  1547. "post_code" => "",
  1548. "post_fee" => 0,
  1549. "sendtime" => date("Y-m-d H:i:s"),
  1550. "send_num" => $value['receipt_quantity'],
  1551. "check_num" => 0,
  1552. "error_num" => 0,
  1553. "wsm_code" => "",
  1554. "order_type" => 3,//3咨询商品
  1555. "status" => 0,
  1556. "addtime" => date("Y-m-d H:i:s"),
  1557. "updatetime" => date("Y-m-d H:i:s")
  1558. ];
  1559. $ou = Db::name("order_out")->insertGetId($out);
  1560. if ($ou == false) {
  1561. return false;
  1562. }
  1563. //修改状态,添加待办
  1564. ActionLog::logAdd($this->post['token'], [
  1565. "order_code" => $outCode,//项目编码
  1566. "status" => 0,//这里的status是之前的值
  1567. "action_remark" => '',//备注
  1568. "action_type" => "status"//新建create,编辑edit,更改状态status
  1569. ], "CKD", 0, $out);
  1570. ProcessOrder::AddProcess($this->post['token'], [
  1571. "order_type" => 'CKD',
  1572. "order_code" => $outCode,
  1573. "order_id" => $ou,
  1574. "order_status" => 0, "before_status" => 0
  1575. ]);
  1576. $order['send_num'] += $value['receipt_quantity'];
  1577. $order['wsend_num'] -= $value['receipt_quantity'];
  1578. if ($order['wsend_num'] < 0) {
  1579. return false;
  1580. }
  1581. $ups = Db::name("order_num")->save($order);
  1582. if ($ups) {
  1583. $tep = [
  1584. "cgdNo" => $order['cgdNo'],
  1585. "outCode" => $outCode,
  1586. "send_num" => $value['receipt_quantity'],
  1587. "status" => 1,
  1588. "addtime" => date("Y-m-d H:i:s"),
  1589. "updatetime" => date("Y-m-d H:i:s")
  1590. ];
  1591. $sen = Db::name("order_send")->save($tep);
  1592. if ($sen == false) {
  1593. return false;
  1594. }
  1595. $standing_book_data['outCode'][] = $outCode;
  1596. }
  1597. } else {
  1598. return false;
  1599. }
  1600. }
  1601. }
  1602. }
  1603. return true;
  1604. }
  1605. /**
  1606. * @param $data
  1607. * @return bool
  1608. * @throws \think\db\exception\DataNotFoundException
  1609. * @throws \think\db\exception\DbException
  1610. * @throws \think\db\exception\ModelNotFoundException
  1611. */
  1612. public function project_good($data, array &$standing_book_data = [])
  1613. {
  1614. $good = Db::name('good')
  1615. ->alias("a")
  1616. ->leftJoin("good_platform b", "a.spuCode=b.spuCode")
  1617. ->where(["b.skuCode" => $data['skuCode'], "a.is_del" => 0, "b.is_del" => 0])
  1618. ->field("a.*,b.skuCode,b.platform_code")->find();
  1619. if ($good == false) {
  1620. return 4; //商品信息未找到
  1621. }
  1622. $goodlass = Db::name("good_ladder")->where(["skuCode" => $data['skuCode'], "is_del" => 0, "status" => 1])->where([["min_num",
  1623. "<=", $data['good_num']]])->order("min_num desc")->find();
  1624. if ($goodlass == false) {
  1625. return 4;//商品信息未找到
  1626. }
  1627. $sale_price = $goodlass['sale_price'];
  1628. $origin_price = 0;
  1629. if ($good['is_stock'] == 0) {
  1630. $origin = Db::name("good_nake")->where([["spuCode", "=", $data['spuCode']], ["min_num", "<=", $data['good_num']],
  1631. ["is_del", "=", 0]])->order("min_num desc")->find();
  1632. if ($origin == false) {
  1633. return 4;//商品信息未找到
  1634. }
  1635. $origin_price = $origin['nake_total'];
  1636. }
  1637. if ($good['is_gold_price'] == 1 && $good['is_stock'] == 0) {
  1638. $gold = Db::name("gold_price1")
  1639. ->field('id,price')
  1640. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  1641. ->order("addtime desc")
  1642. ->find();
  1643. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  1644. $sale_price = $good['demo_fee'] / $data['good_num'] + $good['open_fee'] / $data['good_num'] + $good['noble_weight'] * $gold["price"] + $goodlass['cost_fee'] * $good['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee'];
  1645. $good['cgd_gold_price'] = $gold["price"];
  1646. }
  1647. $orderCode = makeNo("QR");
  1648. $datas = [
  1649. "orderCode" => $orderCode,
  1650. "good_code" => $data['spuCode'],
  1651. "skuCode" => $data['skuCode'],
  1652. "customer_code" => $data['customerNo'],
  1653. "good_name" => isset($good['good_name']) && $good['good_name'] !== '' ? $good['good_name'] : '',
  1654. "good_num" => $data['good_num'],
  1655. "cat_id" => $good['cat_id'],
  1656. "apply_id" => $data['rm'],
  1657. "apply_name" => $data['ri'],
  1658. "origin_price" => $origin_price,
  1659. "sale_price" => $sale_price,
  1660. "post_fee" => 0,
  1661. "status" => 0,
  1662. "send_num" => 0,
  1663. "wsend_num" => $data['good_num'],
  1664. "send_status" => 1,
  1665. "good_type" => 1,
  1666. "send_type" => $data['sendtype'],
  1667. "supplierNo" => $data['companyNo'],
  1668. "is_del" => 0,
  1669. "zxNo" => $data["pgNo"],
  1670. "platform_order" => "",
  1671. "platform_id" => $data['platform_id'],
  1672. "remark" => "",
  1673. "is_stock" => $good['is_stock'],
  1674. "is_activity" => 0,
  1675. "activity_code" => '',
  1676. "proof_id" => 0,
  1677. "order_type" => $good['is_stock'] == 1 ? 1 : 2,//1备库,2非库存品
  1678. 'order_source' => 3,//3项目
  1679. 'good_weight' => $good['noble_weight'],
  1680. 'gold_price' => isset($good['cgd_gold_price']) ? $good['cgd_gold_price'] : 0,
  1681. 'cost_price' => $goodlass['cost_fee'],
  1682. 'diff_weight' => 0,
  1683. 'diff_fee' => 0,
  1684. "addtime" => date("Y-m-d H:i:s"),
  1685. "updatetime" => date("Y-m-d H:i:s"),
  1686. 'total_price' => round($sale_price * $data['good_num'], 2),
  1687. ];
  1688. $datainfo = Db::name('sale')->insert($datas, true);
  1689. if ($datainfo > 0) {
  1690. $standing_book_data['sale_id'] = $datainfo;
  1691. //修改状态,添加待办
  1692. ActionLog::logAdd($this->post['token'], [
  1693. "order_code" => $orderCode,//项目编码
  1694. "status" => 0,//这里的status是之前的值
  1695. "action_remark" => '',//备注
  1696. "action_type" => "status"//新建create,编辑edit,更改状态status
  1697. ], "XSQRD", 0, $datas);
  1698. ProcessOrder::AddProcess($this->post['token'], [
  1699. "order_type" => 'XSQRD',
  1700. "order_code" => $orderCode,
  1701. "order_id" => $datainfo,
  1702. "order_status" => 0, "before_status" => 0
  1703. ]);
  1704. $cgd = [
  1705. "supplierNo" => $good['supplierNo'],
  1706. "companyNo" => $data['companyNo'],
  1707. "orderCode" => $orderCode,
  1708. "spuCode" => $good['spuCode'],
  1709. "skuCode" => $good['skuCode'],
  1710. "good_name" => $good['good_name'],
  1711. "sale_price" => $origin_price,
  1712. "total_fee" => $origin_price * $data['good_num'],
  1713. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  1714. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  1715. "open_fee" => $good['open_fee'],
  1716. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  1717. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  1718. "demo_fee" => $good['demo_fee'],
  1719. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  1720. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  1721. "good_num" => $data['good_num'],
  1722. "good_type" => 1,
  1723. "weight" => $good['noble_weight'],
  1724. "gold_price" => isset($good['cgd_gold_price']) ? $good['cgd_gold_price'] : 0,
  1725. "createrid" => $good['createrid'],
  1726. "creater" => $good['creater'],
  1727. "order_type" => $good['is_stock'] == 1 ? 1 : 2,
  1728. 'order_source' => 3,//3项目
  1729. 'send_way' => 2
  1730. ];
  1731. if ($good['is_stock'] == 0) {
  1732. $bol = $this->createCgd($cgd, $standing_book_data);
  1733. if ($bol == false) {
  1734. return 3;//关联采购失败
  1735. }
  1736. } else {
  1737. $stock = Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")->where
  1738. (["spuCode" => $good['spuCode'], "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" => $data['companyNo']])->field("a.id,a.usable_stock,a.wait_out_stock")->find();
  1739. if ($stock == false || $stock['usable_stock'] < $data['good_num']) {
  1740. return 1; //库存不足
  1741. }
  1742. $bol = $this->RelaCgd(['orderCode' => $orderCode, "good_num" => $data['good_num'], "spuCode" => $good['spuCode'], "companyNo" => $data['companyNo'], "cost_fee" => $goodlass['cost_fee'], 'order_type' => $cgd['order_type'], 'order_source' => $cgd['order_source']], $standing_book_data);
  1743. if ($bol == false) {
  1744. return 3;//关联采购失败
  1745. }
  1746. if (isset($stock)) {
  1747. $stck = [
  1748. "usable_stock" => $stock['usable_stock'] - $data['good_num'],
  1749. "wait_out_stock" => $stock['wait_out_stock'] + $data['good_num'],
  1750. "updatetime" => date("Y-m-d H:i:s")
  1751. ];
  1752. $upad = Db::name("good_stock")->where($stock)->update($stck);
  1753. if ($upad == false) {
  1754. return 2; //库存更新失败
  1755. }
  1756. //商品变动日志表,good_log_code字段存储采购单号
  1757. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 2, 'stock' => $data['good_num'], "stock_name" => "usable_stock"];
  1758. $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 1, 'stock' => $data['good_num'], "stock_name" => "wait_out_stock"];
  1759. GoodLog::LogAdd($this->post['token'], $good_data, "XSQRD");
  1760. }
  1761. }
  1762. if ($data['sendtype'] == 1 && !empty($data['addrlist'])) {
  1763. $i = 1;
  1764. foreach ($data['addrlist'] as $value) {
  1765. $temp = [];
  1766. $addrs = [];
  1767. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  1768. $addrs['provice_code'] = $value['addr_code'][0];
  1769. $addrs['city_code'] = $value['addr_code'][1];
  1770. $addrs['area_code'] = $value['addr_code'][2];
  1771. $addr = json_encode($addrs);
  1772. } else {
  1773. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1774. }
  1775. $temp['orderCode'] = $orderCode;
  1776. $temp['contactor'] = $value['contactor'];
  1777. $temp['mobile'] = $value['mobile'];
  1778. $temp['addr'] = $value['addr'];
  1779. $temp['addr_code'] = $addr;
  1780. $temp['customer_code'] = $data['companyNo'];
  1781. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1782. $temp['post_fee'] = 0;
  1783. $temp['is_del'] = 0;
  1784. $temp['addtime'] = date("Y-m-d H:i:s");
  1785. $temp['updatetime'] = date("Y-m-d H:i:s");
  1786. $temp['arrive_time'] = $data['arrtime'];
  1787. $vmp = Db::name('order_addr')->insert($temp, true);
  1788. if ($vmp > 0) {
  1789. $standing_book_data['customer_code'] = $temp['customer_code'];
  1790. // if ($good['is_stock'] == 1) {
  1791. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1792. ->lock(true)->find();
  1793. if ($order == false) {
  1794. return 3;
  1795. }
  1796. $num = $value['receipt_quantity'];
  1797. $outCode = makeNo("DF");
  1798. //改变编码规则,将原来的outCode后两位换成序列号
  1799. //str_pad字符串填充
  1800. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  1801. $i++;
  1802. $order['wsend_num'] -= $num;
  1803. $order['send_num'] += $num;
  1804. $or = Db::name("order_num")->save($order);
  1805. if ($or == false) {
  1806. return 3;
  1807. }
  1808. $tep = [
  1809. "cgdNo" => $order['cgdNo'],
  1810. "outCode" => $outCode,
  1811. "send_num" => $num,
  1812. "status" => 1,
  1813. "addtime" => date("Y-m-d H:i:s"),
  1814. "updatetime" => date("Y-m-d H:i:s")
  1815. ];
  1816. $sen = Db::name("order_send")->save($tep);
  1817. if ($sen == false) {
  1818. return 3;
  1819. }
  1820. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  1821. if ($cgdinfo == false) {
  1822. return 4;
  1823. }
  1824. $out = [
  1825. "orderCode" => $orderCode,
  1826. "outCode" => $outCode,
  1827. "apply_id" => $data['rm'],
  1828. "apply_name" => $data['ri'],
  1829. "addrid" => $vmp,
  1830. "post_name" => "",
  1831. "post_code" => "",
  1832. "post_fee" => 0,
  1833. "sendtime" => date("Y-m-d H:i:s"),
  1834. "send_num" => $num,
  1835. "check_num" => 0,
  1836. "error_num" => 0,
  1837. "wsm_code" => $cgdinfo['wsm_code'],
  1838. "order_type" => 1,
  1839. "status" => $good['is_stock'] == 1 ? 1 : 0,
  1840. "addtime" => date("Y-m-d H:i:s"),
  1841. "updatetime" => date("Y-m-d H:i:s")
  1842. ];
  1843. $ou = Db::name("order_out")->insertGetId($out);
  1844. if ($ou == false) {
  1845. return 0;
  1846. } else {
  1847. $standing_book_data['outCode'][] = $outCode;
  1848. //修改状态,添加待办
  1849. ActionLog::logAdd($this->post['token'], [
  1850. "order_code" => $outCode,//出库单号
  1851. "status" => 0,//这里的status是之前的值
  1852. "action_remark" => '',//备注
  1853. "action_type" => "create"//新建create,编辑edit,更改状态status
  1854. ], "CKD", 0, $out);
  1855. ProcessOrder::AddProcess($this->post['token'], [
  1856. "order_type" => 'CKD',
  1857. "order_code" => $outCode,//出库单号
  1858. "order_id" => $ou,
  1859. "order_status" => 0, "before_status" => 0
  1860. ]);
  1861. }
  1862. } else {
  1863. return false;
  1864. }
  1865. }
  1866. }
  1867. // }
  1868. }
  1869. return true;
  1870. }
  1871. public function list()
  1872. {
  1873. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  1874. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  1875. $where[] = ['s.is_del', "=", 0];
  1876. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  1877. if ($orderCode !== "") {
  1878. $where[] = ['s.orderCode', "like", "%$orderCode%"];
  1879. }
  1880. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name'])
  1881. : "";
  1882. if ($apply_name !== "") {
  1883. $where[] = ['s.apply_name', "like", "%$apply_name%"];
  1884. }
  1885. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name'])
  1886. : "";
  1887. if ($good_name !== "") {
  1888. $where[] = ['s.good_name', "like", "%$good_name%"];
  1889. }
  1890. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  1891. if ($good_code !== "") {
  1892. $where[] = ['s.good_code', "like", "%$good_code%"];
  1893. }
  1894. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  1895. if ($customer_code !== "") {
  1896. $where[] = ['s.customer_code', "like", "%$customer_code%"];
  1897. }
  1898. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  1899. if ($supplierNo !== "") {
  1900. $where[] = ['s.supplierNo', "like", "%$supplierNo%"];
  1901. }
  1902. $ordertype = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type'])
  1903. : "";
  1904. if ($ordertype !== "") {
  1905. $where[] = ['s.order_type', "=", $ordertype];
  1906. }
  1907. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  1908. if ($status !== "") {
  1909. $where[] = ['s.status', "=", $status];
  1910. }
  1911. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  1912. if ($start != "") {
  1913. $where[] = ["s.addtime", '>=', $start];
  1914. }
  1915. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  1916. if ($end != "") {
  1917. $end .= " 23:59:59";
  1918. $where[] = ["s.addtime", '<=', $end];
  1919. }
  1920. $role = $this->checkRole();
  1921. if (!empty($role['write'])) {
  1922. $where[] = ["s.apply_id", "in", $role['write']];
  1923. }
  1924. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  1925. if ($company_name !== "") $where[] = ["s.apply_id", 'in', get_company_item_user_by_name($company_name)];
  1926. // if(!empty($role['platform']) ){
  1927. // $where[]=["s.platform_id","in",$role['platform']];
  1928. // }
  1929. $count = Db::name('sale')->alias('s')->where($where)->count();
  1930. $total = ceil($count / $size);
  1931. $page = $page >= $total ? $total : $page;
  1932. $list = Db::name('sale')
  1933. ->alias('s')
  1934. ->field('s.*,u.itemid')
  1935. ->leftJoin("depart_user u", "u.uid=s.apply_id AND u.is_del=0")->where($where)
  1936. ->order("s.addtime desc")
  1937. ->page($page, $size)
  1938. ->cursor();
  1939. $data = [];
  1940. foreach ($list as $value) {
  1941. if ($value['order_type'] == 3) {
  1942. $var = Db::name("good_zixun")->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  1943. $value["speclist"] = isset($var['specinfo']) && $var['specinfo'] != "" ? json_decode($var['specinfo'], true) : "";
  1944. } else {
  1945. $var = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1946. ->where(['a.skuCode' => $value['skuCode']])->find();
  1947. $spec = Db::name("good_spec")->where(["spuCode" => $var['spuCode'], "is_del" => 0])->select()->toArray();
  1948. $speclist = [];
  1949. if (!empty($spec)) {
  1950. foreach ($spec as $val) {
  1951. $temp = [];
  1952. $temp['id'] = $val['id'];
  1953. $temp['spuCode'] = $val['spuCode'];
  1954. $temp['spec_id'] = $val['spec_id'];
  1955. $temp['spec_value_id'] = $val['spec_value_id'];
  1956. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  1957. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  1958. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  1959. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  1960. $speclist[] = $temp;
  1961. }
  1962. }
  1963. $value["speclist"] = empty($speclist) ? [] : $speclist;
  1964. }
  1965. $value['can'] = isset($var['cat_id']) && $var['cat_id'] != 0 ? made($var['cat_id']) : [];
  1966. $value['supplierName'] = '';
  1967. if ($value['supplierNo'] != "") {
  1968. $supplier = Db::name("business")->where(["companyNo" => $value['supplierNo']])->find();
  1969. $value['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  1970. }
  1971. $value['customerName'] = '';
  1972. if ($value['customer_code'] != "") {
  1973. $supplier = Db::name("customer_info")->where(["companyNo" => $value['customer_code']])->find();
  1974. $value['customerName'] = isset($supplier['companyName']) ? $supplier['companyName'] : "";
  1975. }
  1976. $value['useage'] = "";
  1977. if ($value['use_order'] != 0) {
  1978. $use = Db::name("order_use")->where(["id" => $value['use_order']])->find();
  1979. $value['useage'] = isset($use['order_use']) ? $use['order_use'] : "";
  1980. } else {
  1981. $value['use_order'] = '';
  1982. }
  1983. $value['good_num'] -= $value['th_num'];
  1984. $value['send_num'] -= $value['th_num'];
  1985. $value['total_price'] = round($value['total_price'] - $value['th_fee'], 2);
  1986. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  1987. $data[] = $value;
  1988. }
  1989. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  1990. }
  1991. public function edit()
  1992. {
  1993. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  1994. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  1995. if ($id == "") {
  1996. return error_show(1002, "参数id不能为空");
  1997. }
  1998. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  1999. if (empty($etid)) {
  2000. return error_show(1002, "未找到数据");
  2001. }
  2002. // $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  2003. // if($orderCode==""){
  2004. // return error_show(1002,"销售单code不能为空");
  2005. // }
  2006. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  2007. if ($good_code == "") {
  2008. return error_show(1002, "商品code不能为空");
  2009. }
  2010. $ct = Db::name('good')->alias('a')->join('good_type b', 'b.good_code=a.good_code', 'left')
  2011. ->where(['b.type_code' => $good_code])->find();
  2012. if ($ct == "") {
  2013. return error_show(1002, "未找到商品数据");
  2014. }
  2015. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  2016. if ($customer_code == "") {
  2017. return error_show(1002, "客户code不能为空");
  2018. }
  2019. $customer = Db::name("customer_info")->where(["companyNo" => $customer_code])->find();
  2020. if ($customer == false) {
  2021. return error_show(1004, "未找到客户数据");
  2022. }
  2023. $supplierNo = $this->post['supplierNo'] && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  2024. if ($supplierNo == "") {
  2025. return error_show(1002, "参数supplierNo不能为空");
  2026. }
  2027. $supplier = Db::name("supplier")->where(["code" => $supplierNo])->find();
  2028. if ($supplier == false) {
  2029. return error_show(1004, "未找到平台供应商数据");
  2030. }
  2031. $apply_id = GetUserInfo($token);
  2032. if (empty($apply_id) || $apply_id['code'] != 0) {
  2033. return error_show(1002, "申请人数据不存在");
  2034. }
  2035. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2036. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2037. // $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim
  2038. //($this->post['good_name']) : "";
  2039. // $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval
  2040. //($this->post['good_num']) : "";
  2041. // $origin_price = isset($this->post['origin_price']) && $this->post['origin_price'] !== "" ? intval
  2042. // ($this->post['origin_price']) : "";
  2043. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] !== "" ? trim($this->post['sale_price']) : "";
  2044. // $total_price = isset($this->post['total_price']) && $this->post['total_price'] !== "" ? trim($this->post['total_price']) : "";
  2045. // $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? trim($this->post['post_fee']) : "";
  2046. // $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2047. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2048. if ($vr == "") {
  2049. return error_show(1002, "参数order_addr不能为空");
  2050. }
  2051. $vi = isset($this->post['good_stock']) && $this->post['good_stock'] !== "" ? $this->post['good_stock'] : "";
  2052. if ($vi == "") {
  2053. return error_show(1002, "参数good_stock不能为空");
  2054. }
  2055. $good_num = intval(array_sum(array_column($vi, "num")));
  2056. Db::startTrans();
  2057. try {
  2058. $datn = [
  2059. "id" => $id,
  2060. "good_code" => $good_code,
  2061. "customer_code" => $customer_code,
  2062. "good_num" => $good_num,
  2063. "supplierNo" => $supplierNo,
  2064. "apply_id" => $rm,
  2065. "apply_name" => $ri,
  2066. "is_del" => 0,
  2067. "updatetime" => date("Y-m-d H:i:s")
  2068. ];
  2069. $datninfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($datn);
  2070. if ($datninfo > 0) {
  2071. $stn = ["order_code" => $etid['orderCode'], "status" => $etid['status'], "action_remark" => '', "action_type" => "edit"];
  2072. ActionLog::logAdd($this->post['token'], $stn, "XSQRD", $etid['status'], $datn);
  2073. // $order=["order_type"=>'XSQRD',"order_code"=>$etid['orderCode'],"order_id"=>$id,"order_status"=>$etid['status'],"before_status"=> 0];
  2074. // ProcessOrder::AddProcess($this->post['token'],$order);
  2075. //更新台账
  2076. Db::name('standing_book')
  2077. ->where('orderCode', $etid['orderCode'])
  2078. ->update([
  2079. 'customer_code' => $customer_code,
  2080. 'supplierNo' => $supplierNo,
  2081. 'updatetime' => date("Y-m-d H:i:s")
  2082. ]);
  2083. foreach ($vi as $ion) {
  2084. $iten = [];
  2085. isset($ion['id']) && $ion['id'] !== "" ? $iten['id'] = $ion['id'] : '';
  2086. $iten['orderCode'] = $etid['orderCode'];
  2087. $iten['wsm_code'] = $ion['wsm_code'];
  2088. $iten['stock_id'] = 0;
  2089. $iten['num'] = $ion['num'];
  2090. $iten['status'] = 1;
  2091. $iten['sale_price'] = $ct['original_price'];
  2092. isset($ion['id']) && $ion['id'] !== "" ? '' : $iten['addtime'] = date("Y-m-d H:i:s");
  2093. $iten['updatetime'] = date("Y-m-d H:i:s");
  2094. $vp = Db::name('sale_info')->save($iten);
  2095. if ($vp == false) {
  2096. Db::rollback();
  2097. return error_show(1002, "更新失败");
  2098. }
  2099. }
  2100. foreach ($vr as $value) {
  2101. $temp = [];
  2102. isset($value['id']) && $value['id'] !== "" ? $temp['id'] = $value['id'] : '';
  2103. $temp['orderCode'] = $etid['orderCode'];
  2104. $temp['contactor'] = $value['contactor'];
  2105. $temp['mobile'] = $value['mobile'];
  2106. $temp['addr'] = $value['addr'];
  2107. $temp['addr_code'] = $value['addr_code'];
  2108. $temp['customer_code'] = $customer_code;
  2109. $temp['receipt_quantity'] = $value['receipt_quantity'];
  2110. $temp['post_fee'] = 0;
  2111. $temp['is_del'] = $value['is_del'];
  2112. isset($value['id']) && $value['id'] !== "" ? '' : $temp['addtime'] = date("Y-m-d H:i:s");
  2113. $temp['updatetime'] = date("Y-m-d H:i:s");
  2114. $temp['arrive_time'] = date("Y-m-d H:i:s");
  2115. $dat = Db::name('order_addr')->save($temp);
  2116. if ($dat == false) {
  2117. Db::rollback();
  2118. return error_show(1002, "更新失败");
  2119. }
  2120. }
  2121. Db::commit();
  2122. return error_show(0, "更新成功");
  2123. }
  2124. Db::rollback();
  2125. return error_show(1002, "更新失败");
  2126. } catch (Exception $e) {
  2127. Db::rollback();
  2128. return error_show(1005, $e->getMessage());
  2129. }
  2130. }
  2131. public function info()
  2132. {
  2133. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2134. if ($id == "") {
  2135. return error_show(1002, "参数id不能为空");
  2136. }
  2137. $einfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2138. if (empty($einfo)) {
  2139. return error_show(1002, "未找到销售订单数据");
  2140. }
  2141. if ($einfo['order_type'] == 3) {
  2142. $goon = Db::name("good_zixun")->where(["spuCode" => $einfo['good_code'], "is_del" => 0])->find();
  2143. } else {
  2144. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2145. ->where(['a.skuCode' => $einfo['skuCode']])->find();
  2146. }
  2147. if ($goon == false) {
  2148. return error_show(1003, "未找到商品数据");
  2149. } else {
  2150. $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
  2151. $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
  2152. $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  2153. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  2154. $speclist = [];
  2155. if (!empty($spec)) {
  2156. foreach ($spec as $value) {
  2157. $temp = [];
  2158. $temp['id'] = $value['id'];
  2159. $temp['spuCode'] = $value['spuCode'];
  2160. $temp['spec_id'] = $value['spec_id'];
  2161. $temp['spec_value_id'] = $value['spec_value_id'];
  2162. $temp['is_del'] = $value['is_del'];
  2163. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  2164. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  2165. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  2166. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  2167. $speclist[] = $temp;
  2168. }
  2169. }
  2170. $goon["speclist"] = empty($speclist) ? [] : $speclist;
  2171. $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  2172. $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
  2173. $goon['origin_place_cn'] = "";
  2174. $goon['delivery_place_cn'] = "";
  2175. if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
  2176. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2177. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
  2178. $goon['delivery_place_cn'] = GetAddr(json_encode($place));
  2179. }
  2180. if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
  2181. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2182. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
  2183. $goon['origin_place_cn'] = GetAddr(json_encode($place));
  2184. }
  2185. if ($goon['brand_id'] != 0) {
  2186. $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
  2187. $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  2188. } else {
  2189. $goon["brand_name"] = "";
  2190. $goon["brand_id"] = "";
  2191. }
  2192. $supplier = Db::name("supplier")->where(["code" => $goon['supplierNo']])->find();
  2193. $goon['supplierName'] = isset($supplier['name']) ? $supplier['name'] : "";
  2194. $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
  2195. if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
  2196. $company = Db::name("business")->where(["companyNo" => $goon['companyNo']])->find();
  2197. }
  2198. $goon['company'] = isset($company['company']) ? $company['company'] : "";
  2199. }
  2200. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  2201. $in = Db::name('customer_info')->where(['companyNo' => $einfo['customer_code']])->field('companyName')->find();
  2202. $einfo['supplierName'] = '';
  2203. if ($einfo['supplierNo'] != "") {
  2204. $supplier = Db::name("business")->where(["companyNo" => $einfo['supplierNo']])->find();
  2205. $einfo['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  2206. }
  2207. $fo = Db::name('order_addr')->alias("a")->leftJoin("order_out b", "a.id=b.addrid and b.is_del=0")
  2208. ->where(['a.orderCode' => $einfo['orderCode'], 'a.is_del' => 0])->field("a.*,b.post_name,b.post_code,
  2209. b.post_fee,b.sendtime,b.send_num,b.check_num,b.error_num,b.wsm_code,b.order_type,b.status as send_status,b.outCode")
  2210. ->select();
  2211. $addrs = [];
  2212. if (!empty($fo)) {
  2213. foreach ($fo as $value) {
  2214. $value['addr_info'] = "";
  2215. $value['send_num'] = 0;
  2216. if ($value['addr_code'] != "" && !empty($value['addr_code'])) {
  2217. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2218. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $value['addr_code']);
  2219. $addr = GetAddr(json_encode($place));
  2220. $value['addr_info'] = $addr;
  2221. }
  2222. $value['wsm_name'] = "";
  2223. if (isset($value['wsm_code']) && $value['wsm_code'] != "") {
  2224. $wsmcode = Db::name("warehouse_info")->where(["wsm_code" => $value['wsm_code']])->find();
  2225. $value['wsm_name'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  2226. }
  2227. $value['sendtime'] = $value['send_status'] < 2 ? '' : $value['sendtime'];
  2228. $addrs[] = $value;
  2229. }
  2230. }
  2231. $einfo['useage'] = "";
  2232. if ($einfo['use_order'] != 0) {
  2233. $use = Db::name("order_use")->where(["id" => $einfo['use_order']])->find();
  2234. $einfo['useage'] = isset($use['order_use']) ? $use['order_use'] : "";
  2235. } else {
  2236. $einfo['use_order'] = '';
  2237. }
  2238. $einfo['platform_name'] = '';
  2239. if ($einfo['platform_id'] != 0) {
  2240. $plat = Db::name("platform")->where(['id' => $einfo['platform_id']])->find();
  2241. $einfo['platform_name'] = isset($plat['platform_name']) ? $plat['platform_name'] : "";
  2242. }
  2243. $einfo['companyName'] = isset($in['companyName']) ? $in['companyName'] : "";
  2244. $einfo['addrs'] = $addrs;
  2245. $einfo['can'] = $int;
  2246. $einfo['goodinfo'] = $goon;
  2247. $einfo['good_num'] -= $einfo['th_num'];
  2248. $einfo['send_num'] -= $einfo['th_num'];
  2249. $einfo['total_price'] = round($einfo['total_price'] - $einfo['th_fee'], 2);
  2250. //处理凭证
  2251. if ($einfo['order_type'] == 1 || $einfo['order_type'] == 2) {
  2252. $proof = Db::name('good_proof')
  2253. ->field('id,proof_type,proof_url')
  2254. ->where(['id' => $einfo['proof_id'], 'is_del' => 0])
  2255. ->find();
  2256. $einfo['proof_type'] = isset($proof['proof_type']) ? $proof['proof_type'] : '';
  2257. $einfo['proof_url'] = isset($proof['proof_url']) ? $proof['proof_url'] : '';
  2258. } elseif ($einfo['order_type'] == 3) {
  2259. $einfo['proof_type'] = isset($goon['proof_type']) ? $goon['proof_type'] : '';
  2260. $einfo['proof_url'] = isset($goon['proof_url']) ? $goon['proof_url'] : '';
  2261. } else {
  2262. $einfo['proof_type'] = '';
  2263. $einfo['proof_url'] = '';
  2264. }
  2265. if (empty($einfo)) {
  2266. return error_show(1002, "未找到销售订单数据");
  2267. } else {
  2268. return app_show(0, "获取成功", $einfo);
  2269. }
  2270. }
  2271. public function del()
  2272. {
  2273. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2274. if ($id === "") {
  2275. return error_show(1002, "参数id不能为空");
  2276. }
  2277. $ed = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2278. if (empty($ed)) {
  2279. return error_show(1002, "未找到销售订单数据");
  2280. }
  2281. $str = Db::name('sale')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  2282. if ($str) {
  2283. $stc = ["order_code" => $id, "status" => $ed['status'], "action_remark" => '', "action_type" => "delete"];
  2284. ActionLog::logAdd($this->post['token'], $stc, "XSQRD", $ed['status'], ['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  2285. $order = ["order_type" => 'XSQRD', "order_code" => $ed['orderCode'], "order_id" => $id, "order_status" => $ed['status']];
  2286. ProcessOrder::workdel($order);
  2287. return error_show(0, "删除成功");
  2288. } else {
  2289. return error_show(1002, "删除失败");
  2290. }
  2291. }
  2292. //废弃接口
  2293. public function status()
  2294. {
  2295. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2296. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2297. if ($id == "") {
  2298. return error_show(1002, "参数id不能为空");
  2299. }
  2300. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2301. if ($status == "") {
  2302. return error_show(1002, "订单状态不能为空");
  2303. }
  2304. $dio = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2305. if (empty($dio)) {
  2306. return error_show(1002, "销售单信息未找到");
  2307. }
  2308. if ($status == 1 && $dio['order_type'] == 2) {
  2309. $zxinfo = Db::name("consult")->where(["zxNo" => $dio['zxNo'], "is_del" => 0])->find();
  2310. if ($zxinfo == false) {
  2311. return error_show(1004, "未找到咨询单数据");
  2312. }
  2313. $gold['price'] = 0;
  2314. if ($zxinfo['zx_type'] == 2) {
  2315. if ($zxinfo['metals'] == "") {
  2316. return error_show(1004, "未找到咨询单贵金属类型数据");
  2317. }
  2318. $metals = $zxinfo['metals'] == '18K' ? 1 : ($zxinfo['metals'] == '24K' ? 2 : 3);
  2319. $gold = Db::name("gold_price")->where(["type" => $metals])->order("addtime desc")->find();
  2320. if (empty($gold)) {
  2321. return error_show(1004, "未找到咨询单贵金属类型数据");
  2322. }
  2323. $dio['gold_price'] = $gold['price'];
  2324. }
  2325. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $zxinfo['gysNo'], "wsm_type" => 2])->find();
  2326. if (empty($wsm)) {
  2327. return error_show(1002, "供应商仓库未找到");
  2328. }
  2329. }
  2330. $apply_id = GetUserInfo($token);
  2331. if (empty($apply_id) || $apply_id['code'] != 0) {
  2332. return error_show(1002, "申请人数据不存在");
  2333. }
  2334. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2335. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2336. Db::startTrans();
  2337. try {
  2338. $vmp = $dio['status'];
  2339. $dio['status'] = $status;
  2340. $dio['updatetime'] = date("Y-m-d H:i:s");
  2341. $st = Db::name('sale')->save($dio);
  2342. if ($st) {
  2343. $stn = ["order_code" => $dio['orderCode'], "status" => $vmp, "action_remark" => '', "action_type" => "status"];
  2344. ActionLog::logAdd($this->post['token'], $stn, $dio['order_type'] == 2 ? "ZXQRD" : "XSQRD", $dio['status'], $dio);
  2345. $order = ["order_type" => "XSQRD", "order_code" => $dio['orderCode'], "order_id" => $id, "order_status" => $dio['status'], "before_status" => 1];
  2346. ProcessOrder::AddProcess($this->post['token'], $order);
  2347. if ($status == 3 && $dio['order_type'] == 1) {
  2348. $dn = Db::name('sale_info')->where(['orderCode' => $st['orderCode']])->select();
  2349. foreach ($dn as $value) {
  2350. $dm = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $st['good_code']])->find();
  2351. if ($dm == "") {
  2352. Db::rollback();
  2353. return error_show(1003, "未找到商品数据");
  2354. }
  2355. if ($value['num'] > $dm['usable_stock']) {
  2356. Db::rollback();
  2357. return error_show(1002, "超出库存数量");
  2358. }
  2359. $dm['usable_stock'] -= $value['num'];
  2360. $dm['wait_out_stock'] += $value['num'];
  2361. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], "is_del" => 0])->sum("send_num");
  2362. $value['send_num'] = $send ?? 0;
  2363. $value['wsend_num'] = $value['num'] - $value['send_num'];
  2364. $Db = Db::name('good_stock')->update($dm);
  2365. $Db['updatetime'] = date('Y-m-d H:i:s');
  2366. if ($Db == false) {
  2367. Db::rollback();
  2368. return error_show(1002, "状态更新失败");
  2369. }
  2370. }
  2371. }
  2372. if ($status == 1 && $dio['order_type'] == 2) {
  2373. $cgd = makeNo("CG");
  2374. $clll = [
  2375. "cgdNo" => $cgd,
  2376. "bkcode" => $dio['orderCode'],
  2377. "wsm_code" => $wsm['wsm_code'],
  2378. "cgder_id" => Db::name('depart_user')->where('nickname', $zxinfo['saler'])->value('uid') ?? 0,//重名以及多个账号的话…………
  2379. "cgder" => $zxinfo['saler'],
  2380. "good_code" => "GD-" . $zxinfo['cpNo'],
  2381. "good_name" => $zxinfo['cpName'],
  2382. "good_type_code" => $zxinfo['cpNo'],
  2383. "good_num" => $dio['good_num'],
  2384. "good_price" => $zxinfo['total_fee'],
  2385. "total_fee" => round($zxinfo['total_fee'] * $dio['good_num'], 2),
  2386. "pakge_fee" => $zxinfo['package_fee'],
  2387. "cert_fee" => $zxinfo['cert_fee'],
  2388. "open_fee" => $zxinfo['open_fee'],
  2389. "delivery_fee" => $zxinfo['delivery_fee'],
  2390. "mark_fee" => $zxinfo['mark_fee'],
  2391. "teach_fee" => $zxinfo['cost_fee'],
  2392. "demo_fee" => $zxinfo['demo_fee'],
  2393. "nake_fee" => $zxinfo['bare_fee'],
  2394. "weight" => isset($zxinfo['weight']) ? $zxinfo['weight'] : 0,
  2395. "supplierNo" => $zxinfo['gysNo'],
  2396. "supplier_name" => $zxinfo['gysname'],
  2397. "gold_price" => $gold['price'],
  2398. "send_num" => 0,
  2399. "wsend_num" => $dio['good_num'],
  2400. "remark" => '',
  2401. "lasttime" => date("Y-m-d H:i:s"),
  2402. "is_del" => 0,
  2403. "status" => $dio['send_type'] == 1 ? 3 : 0,
  2404. "order_type" => 2,
  2405. "order_source" => 3,//3项目,废弃接口???
  2406. "addtime" => date("Y-m-d H:i:s"),
  2407. "updatetime" => date("Y-m-d H:i:s")
  2408. ];
  2409. $cgin = Db::name("purchease_order")->insert($clll);
  2410. // var_dump(Db::name("purchease_order")->getLastSql(),$clll);
  2411. if ($cgin == false) {
  2412. $stb = ["order_code" => $cgd, "status" => $clll['status'], "action_remark" => '', "action_type" => "create"];
  2413. ActionLog::logAdd($this->post['token'], $stb, "xsd", $clll['status'], $stb);
  2414. Db::rollback();
  2415. return error_show(1002, "咨询采购单创建商品失败");
  2416. }
  2417. if ($dio['send_type'] == 1) {
  2418. $addr = Db::name('order_addr')->where(["orderCode" => $dio['orderCode'], 'is_del' => 0])->select();
  2419. $i = 1;
  2420. foreach ($addr as $value) {
  2421. $outCode = makeNo("DF");
  2422. //改变编码规则,将原来的outCode后两位换成序列号
  2423. //str_pad字符串填充
  2424. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  2425. $i++;
  2426. $data = [
  2427. "wsm_code" => $wsm['wsm_code'],
  2428. "orderCode" => $dio['orderCode'],
  2429. "outCode" => $outCode,
  2430. "order_type" => $dio['order_type'],
  2431. "apply_id" => $rm,
  2432. "apply_name" => $ri,
  2433. "addrid" => $value['id'],
  2434. "post_name" => '',
  2435. "post_code" => '',
  2436. "post_fee" => '',
  2437. "sendtime" => date("Y-m-d H:i:s"),
  2438. "send_num" => $value['receipt_quantity'],
  2439. "check_num" => 0,
  2440. "error_num" => 0,
  2441. "status" => 0,
  2442. "addtime" => date("Y-m-d H:i:s"),
  2443. "updatetime" => date("Y-m-d H:i:s")
  2444. ];
  2445. $datainfo = Db::name('order_out')->insertGetId($data);
  2446. if (!$datainfo) {
  2447. $sti = ["order_code" => $dio['orderCode'], "status" => 0, "action_remark" => '', "action_type" => "create"];
  2448. ActionLog::logAdd($this->post['token'], $sti, "xsd", 0, $sti);
  2449. Db::rollback();
  2450. return error_show(1003, "创建失败");
  2451. } else {
  2452. //修改状态,添加待办
  2453. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  2454. "order_code" => $outCode,//出库单号
  2455. "status" => 0,//这里的status是之前的值
  2456. "action_remark" => '',//备注
  2457. "action_type" => "create"//新建create,编辑edit,更改状态status
  2458. ], "CKD", 0, $data);
  2459. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  2460. "order_type" => 'CKD',
  2461. "order_code" => $outCode,//出库单号
  2462. "order_id" => $datainfo,
  2463. "order_status" => 0, "before_status" => 0
  2464. ]);
  2465. }
  2466. }
  2467. $wsm_in_code = makeNo("CF");
  2468. $orin = [
  2469. "wsm_in_code" => $wsm_in_code,
  2470. "cgdNo" => $cgd,
  2471. "wsm_code" => $wsm['wsm_code'],
  2472. "wsm_reaper" => "",
  2473. "send_num" => $dio['good_num'],
  2474. "post_company" => '',
  2475. "post_code" => '',
  2476. "post_fee" => '',
  2477. "sendtime" => date("Y-m-d H:i:s"),
  2478. "apply_id" => $rm,
  2479. "apply_name" => $ri,
  2480. "wait_num" => 0,
  2481. "status" => 4,
  2482. "addtime" => date("Y-m-d H:i:s"),
  2483. "updatetime" => date("Y-m-d H:i:s")
  2484. ];
  2485. $win = Db::name("purchease_in")->insert($orin);
  2486. if (!$win) {
  2487. $stv = ["order_code" => $wsm_in_code, "status" => 4, "action_remark" => '', "action_type" => "create"];
  2488. ActionLog::logAdd($this->post['token'], $stv, "xsd", 4, $stv);
  2489. Db::rollback();
  2490. return error_show(1003, "创建失败");
  2491. }
  2492. //维护台账
  2493. Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `cgdNo`='{$cgd}'");
  2494. $good = Db::name("good_stock")->where(["wsm_code" => $wsm['wsm_code'], "good_type_code" => $zxinfo['cpNo'], "is_del" => 0])->find();
  2495. if (empty($good)) {
  2496. $good = [
  2497. "good_type_code" => $zxinfo['cpNo'],
  2498. "wsm_code" => $wsm['wsm_code'],
  2499. "usable_stock" => 0,
  2500. "wait_out_stock" => 0,
  2501. "total_stock" => 0,
  2502. "addtime" => date("Y-m-d H:i:s"),
  2503. "updatetime" => date("Y-m-d H:i:s"),
  2504. ];
  2505. }
  2506. $good['wait_out_stock'] += $dio['good_num'];
  2507. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  2508. $good['updatetime'] = date("Y-m-d H:i:s");
  2509. $upd = Db::name("good_stock")->save($good);
  2510. if (!$upd) {
  2511. $ste = ["order_code" => $zxinfo['cpNo'], "status" => 0, "action_remark" => '', "action_type" => "create"];
  2512. ActionLog::logAdd($this->post['token'], $ste, "xsd", 0, $ste);
  2513. Db::rollback();
  2514. return error_show(1003, "创建失败");
  2515. }
  2516. }
  2517. }
  2518. }
  2519. Db::commit();
  2520. return error_show(0, "订单状态更新成功");
  2521. } catch (Exception $e) {
  2522. Db::rollback();
  2523. return error_show(1005, $e->getMessage());
  2524. }
  2525. }
  2526. public function fee()
  2527. {
  2528. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  2529. if ($id == "") {
  2530. return error_show(1002, "参数id不能为空");
  2531. }
  2532. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  2533. if (empty($etid)) {
  2534. return error_show(1002, "未找到数据");
  2535. }
  2536. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2537. if ($vr == "") {
  2538. return error_show(1002, "参数order_addr不能为空");
  2539. }
  2540. Db::startTrans();
  2541. try {
  2542. $data = [
  2543. "id" => $id,
  2544. "status" => 2,
  2545. "is_del" => 0,
  2546. "updatetime" => date("Y-m-d H:i:s")
  2547. ];
  2548. $datainfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($data);
  2549. if ($datainfo) {
  2550. $stx = ["order_code" => $etid['orderCode'], "status" => $etid['status'], "action_remark" => '', "action_type" => "edit"];
  2551. ActionLog::logAdd($this->post['token'], $stx, "XSQRD", 2, $data);
  2552. $order = ["order_type" => "XSQRD", "order_code" => $etid['orderCode'], "order_id" => $id, "order_status" => $data['status'], "before_status" => 0];
  2553. ProcessOrder::AddProcess($this->post['token'], $order);
  2554. foreach ($vr as $value) {
  2555. $tm = [];
  2556. $tm['id'] = $value['id'];
  2557. $tm['orderCode'] = $etid['orderCode'];
  2558. $tm['post_fee'] = $value['post_fee'];
  2559. $tm['updatetime'] = date("Y-m-d H:i:s");
  2560. $dn = Db::name('order_addr')->save($tm);
  2561. if ($dn == false) {
  2562. Db::rollback();
  2563. return error_show(1002, "更新失败");
  2564. }
  2565. }
  2566. Db::commit();
  2567. return error_show(0, "更新成功");
  2568. }
  2569. Db::rollback();
  2570. return error_show(1003, "更新失败");
  2571. } catch (Exception $e) {
  2572. Db::rollback();
  2573. return error_show(1005, $e->getMessage());
  2574. }
  2575. }
  2576. // 废弃
  2577. public function out()
  2578. {
  2579. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2580. $outCode = makeNo("DF");
  2581. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  2582. if ($orderCode == "") {
  2583. return error_show(1002, "参数orderCoder不能为空");
  2584. }
  2585. $der = Db::name('sale')->where(['orderCode' => $orderCode])->find();
  2586. if ($der == "") {
  2587. return error_show(1002, "未找到出库订单单号");
  2588. }
  2589. // $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  2590. // if ($wsm_code == "") {
  2591. // return error_show(1002, "参数wsm_code不能为空");
  2592. // }
  2593. // $wsm = Db::name('sale_info')->where(['wsm_code' => $wsm_code, 'orderCode' => $der['orderCode']])->find();
  2594. // if ($wsm == "") {
  2595. // return error_show(1002, "未找到仓库编码");
  2596. // }
  2597. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !== "" ? trim($this->post['post_name']) : "";
  2598. if ($post_name == "") {
  2599. return error_show(1002, "物流公司不能为空");
  2600. }
  2601. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
  2602. if ($post_code == "") {
  2603. return error_show(1002, "物流单号不能为空");
  2604. }
  2605. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
  2606. if ($post_fee == "") {
  2607. return error_show(1002, "物流费不能为空");
  2608. }
  2609. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  2610. if ($send_num == "") {
  2611. return error_show(1002, "发货数量不能为空");
  2612. }
  2613. if ($send_num > $der['wsend_num']) {
  2614. return error_show(1002, "超出可发货数量");
  2615. }
  2616. $apply_id = GetUserInfo($token);
  2617. if (empty($apply_id) || $apply_id['code'] != 0) {
  2618. return error_show(1002, "申请人数据不存在");
  2619. }
  2620. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2621. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2622. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !== "" ? intval($this->post['addrid']) : "";
  2623. $dr = Db::name('order_addr')->where(['id' => $addrid, 'is_del' => 0])->find();
  2624. if (empty($dr)) {
  2625. return error_show(1003, "收货人信息未找到");
  2626. }
  2627. if ($dr['orderCode'] !== $orderCode) {
  2628. return error_show(1002, "订单code不存在");
  2629. }
  2630. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  2631. // $check_num = isset($this->post['check_num']) && $this->post['check_num'] !=="" ? $this->post['check_num'] :"";
  2632. // $error_num = isset($this->post['error_num']) && $this->post['error_num'] !=="" ? $this->post['error_num'] :"";
  2633. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "1";
  2634. //$order_type = isset($this->post['order_type']) && $this->post['order_type'] !=="" ? $this->post['order_type'] :"1";
  2635. //$dn=Db::name('sale_info')->where(['wsm_code'=>$outCode])->find();
  2636. Db::startTrans();
  2637. try {
  2638. $data = [
  2639. "wsm_code" => "",
  2640. "orderCode" => $orderCode,
  2641. "outCode" => $outCode,
  2642. "order_type" => $der['order_type'],
  2643. "apply_id" => $rm,
  2644. "apply_name" => $ri,
  2645. "addrid" => $dr['id'],
  2646. "post_name" => $post_name,
  2647. "post_code" => $post_code,
  2648. "post_fee" => $post_fee,
  2649. "sendtime" => $sendtime,
  2650. "send_num" => $send_num,
  2651. "check_num" => 0,
  2652. "error_num" => 0,
  2653. "status" => $status,
  2654. "addtime" => date("Y-m-d H:i:s"),
  2655. "updatetime" => date("Y-m-d H:i:s")
  2656. ];
  2657. $datainfo = Db::name('order_out')->insert($data, true);
  2658. if ($datainfo > 0) {
  2659. $stx = ["order_code" => $outCode, "status" => $status, "action_remark" => '', "action_type" => "create"];
  2660. ActionLog::logAdd($this->post['token'], $stx, $der['order_type'] == 1 ? "CKD" : 'ZXCKD', $status, $data);
  2661. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $datainfo, "order_status" => $data['status'], "before_status" => 0];
  2662. ProcessOrder::AddProcess($this->post['token'], $order);
  2663. if ($status == 1) {
  2664. $item = $der['status'];
  2665. $der['send_num'] += $send_num;
  2666. $der['wsend_num'] -= $send_num;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  2667. $der['send_status'] = $der['send_num'] == 0 ? 1 : $der['wsend_num'] == 0 ? 3 : 2;
  2668. $der['status'] = $der['send_num'] == 0 ? 3 : $der['wsend_num'] == 0 ? 5 : 4;
  2669. $der['updatetime'] = date("Y-m-d H:i:s");
  2670. $si = Db::name('sale')->save($der);
  2671. if ($si == false) {
  2672. Db::rollback();
  2673. return error_show(1003, "更新失败");
  2674. }
  2675. $stx = ["order_code" => $orderCode, "status" => $item, "action_remark" => '', "action_type" => "edit"];
  2676. ActionLog::logAdd($this->post['token'], $stx, "XSQRD", $der['status'], $der);
  2677. $order = ["order_type" => "XSQRD", "order_code" => $orderCode, "order_id" => $der['id'], "order_status" => $data['status'], "before_status" => 0];
  2678. ProcessOrder::AddProcess($this->post['token'], $order);
  2679. }
  2680. Db::commit();
  2681. return error_show(0, "创建成功");
  2682. } else {
  2683. Db::rollback();
  2684. return error_show(1003, "创建失败");
  2685. }
  2686. } catch (Exception $e) {
  2687. Db::rollback();
  2688. return error_show(1005, $e->getMessage());
  2689. }
  2690. }
  2691. // 废弃
  2692. public function customer()
  2693. {
  2694. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  2695. if (empty($outCode)) {
  2696. return error_show(1002, "销售订单编号不能为空");
  2697. }
  2698. $codeinfo = Db::name('order_out')->where(['outCode' => $outCode])->find();
  2699. if ($codeinfo == "") {
  2700. return error_show(1003, "未找到订单数据");
  2701. }
  2702. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  2703. // if($orderCode==""){
  2704. // return error_show(1002,"参数orderCoder不能为空");
  2705. // }
  2706. $dr = Db::name('sale')->where(['orderCode' => $codeinfo['orderCode']])->find();
  2707. if ($dr == "") {
  2708. return error_show(1002, "未找到销售订单");
  2709. }
  2710. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "";
  2711. if ($status == "") {
  2712. return error_show(10002, "状态不能为空");
  2713. }
  2714. Db::startTrans();
  2715. try {
  2716. $time = $codeinfo['status'];
  2717. $codeinfo['status'] = $status;
  2718. $codeinfo['updatetime'] = date("Y-m-d H:i:s");
  2719. $cust = Db::name('order_out')->save($codeinfo);
  2720. if ($cust) {
  2721. $stx = ["order_code" => $outCode, "status" => $time, "action_remark" => '', "action_type" => "status"];
  2722. ActionLog::logAdd($this->post['token'], $stx, "CKD", $codeinfo['status'], $codeinfo);
  2723. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $codeinfo['id'], "order_status" => $codeinfo['status'], "before_status" => 0];
  2724. ProcessOrder::AddProcess($this->post['token'], $order);
  2725. if ($status == 1) {
  2726. $stokc = Db::name("good_stock")->where(['spuCode' => $codeinfo['spuCode'], "wsm_code" => $codeinfo['wsm_code'],
  2727. "is_del" => 0])->find();
  2728. if ($stokc == false) {
  2729. Db::rollback();
  2730. return error_show(1002, "未找库存到数据");
  2731. } else {
  2732. if ($codeinfo['send_num'] > $stokc['usable_stock']) {
  2733. Db::rollback();
  2734. return error_show(1002, "超出库存数量");
  2735. }
  2736. $stokc['wait_out_stock'] += $codeinfo['send_num'];
  2737. $stokc['usable_stock'] -= $codeinfo['send_num'];
  2738. $stokc['updatetime'] = date("Y-m-d H:i:s");
  2739. }
  2740. $stoc = Db::name("good_stock")->save($stokc);
  2741. if ($stoc == false) {
  2742. Db::rollback();
  2743. return error_show(1002, "库存更新失败");
  2744. }
  2745. $item = $dr['status'];
  2746. $dr['send_num'] += $codeinfo['send_num'];
  2747. $dr['wsend_num'] -= $codeinfo['send_num'];//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  2748. $dr['send_status'] = $dr['send_num'] == 0 ? 1 : $dr['wsend_num'] == 0 ? 3 : 2;
  2749. $dr['status'] = $dr['send_num'] == 0 ? 3 : $dr['wsend_num'] == 0 ? 5 : 4;
  2750. $dr['updatetime'] = date("Y-m-d H:i:s");
  2751. $si = Db::name('sale')->save($dr);
  2752. if ($si == false) {
  2753. Db::rollback();
  2754. return error_show(1003, "更新失败");
  2755. }
  2756. $stx = ["order_code" => $dr['orderCode'], "status" => $item, "action_remark" => '', "action_type" => "edit"];
  2757. ActionLog::logAdd($this->post['token'], $stx, $dr['order_type'] == 2 ? "ZXQRD" : "XSQRD", $dr['status'],
  2758. $dr);
  2759. $order = ["order_type" => "XSQRD", "order_code" => $dr['orderCode'], "order_id" => $dr['id'], "order_status" => $dr['status'], "before_status" => 0];
  2760. ProcessOrder::AddProcess($this->post['token'], $order);
  2761. //商品变动日志表,good_log_code字段存储销售订单编号
  2762. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 1, 'stock' => $codeinfo['send_num'], "stock_name" => "wait_out_stock"];
  2763. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "usable_stock"];
  2764. GoodLog::LogAdd($this->post['token'], $good_data, 'CKD');
  2765. }
  2766. if ($status == 3) {
  2767. $var = Db::name('good_stock')->where(['wsm_code' => $codeinfo['wsm_code'], 'good_type_code' => $dr['good_code']])->find();
  2768. if ($var == false) {
  2769. Db::rollback();
  2770. return error_show(1002, "未找到商品数据");
  2771. }
  2772. if ($codeinfo['send_num'] > $var['intra_stock']) {
  2773. Db::rollback();
  2774. return error_show(1003, "超出在途库存数量");
  2775. }
  2776. $var['intra_stock'] -= $codeinfo['send_num'];
  2777. $var['updatetime'] = date("Y-m-d H:i:s");
  2778. $stre = Db::name('good_stock')->save($var);
  2779. //商品变动日志表,good_log_code字段存储销售订单编号
  2780. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $var['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "intra_stock"];
  2781. GoodLog::LogAdd($this->post['token'], $good_data, $codeinfo['order_type'] == 2 ? "ZXCKD" : "CKD");
  2782. if ($stre == false) {
  2783. Db::rollback();
  2784. return error_show(1002, "状态更新失败");
  2785. }
  2786. }
  2787. Db::commit();
  2788. return error_show(0, "出库订单更新成功");
  2789. } else {
  2790. Db::rollback();
  2791. return error_show(1003, "出库订单更新失败");
  2792. }
  2793. } catch (Exception $e) {
  2794. Db::rollback();
  2795. return error_show(1005, $e->getMessage());
  2796. }
  2797. }
  2798. public function difflist()
  2799. {
  2800. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  2801. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  2802. $where = [];
  2803. $cgdNo = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  2804. if ($cgdNo != "") {
  2805. $where[] = ['a.orderCode', "like", "%$cgdNo%"];
  2806. }
  2807. $diffNo = isset($this->post['diffCode']) && $this->post['diffCode'] != "" ? trim($this->post['diffCode']) : "";
  2808. if ($diffNo != "") {
  2809. $where[] = ['a.diffCode', "like", "%$diffNo%"];
  2810. }
  2811. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2812. if ($status !== "") {
  2813. $where[] = ['a.status', "=", $status];
  2814. }
  2815. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  2816. if ($start != "") {
  2817. $where[] = ["a.addtime", '>=', $start];
  2818. }
  2819. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  2820. if ($end != "") {
  2821. $where[] = ["a.addtime", '<=', $end];
  2822. }
  2823. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  2824. if ($supplierNo != "") {
  2825. $where[] = ['c.supplierNo', "like", "%$supplierNo%"];
  2826. }
  2827. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  2828. if ($companyNo != "") {
  2829. $where[] = ['c.customer_code', "like", "%$companyNo%"];
  2830. }
  2831. // $role=$this->checkRole();
  2832. // if(!empty($role['write'])){
  2833. // $where[]=["apply_id","in",$role['write']];
  2834. // }
  2835. // if(!empty($role['platform']) ){
  2836. // $where[]=["c.platform_id","in",$role['platform']];
  2837. // }
  2838. $count = Db::name('sale_diff')->alias('a')
  2839. ->join("sale c", "c.orderCode=a.orderCode", "left")
  2840. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  2841. ->where($where)->count();
  2842. $total = ceil($count / $size);
  2843. $page = $page >= $total ? intval($total) : $page;
  2844. $list = Db::name('sale_diff')->alias('a')
  2845. ->join("sale c", "c.orderCode=a.orderCode", "left")
  2846. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  2847. ->where($where)->page($page, $size)->field("a.*,c.customer_code,v.companyName,c.skuCode,c.order_type,c.supplierNo")
  2848. ->order("a.addtime desc")->select();
  2849. $data = [];
  2850. foreach ($list as $value) {
  2851. if ($value['order_type'] == 3) {
  2852. $goon = Db::name("good_zixun")->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  2853. } else {
  2854. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2855. ->where(['a.skuCode' => $value['skuCode']])->find();
  2856. }
  2857. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  2858. $wsm = Db::name("business")->where(['companyNo' => $value['supplierNo']])->find();
  2859. $value['supplierNo'] = isset($wsm['companyNo']) ? $wsm['companyNo'] : "";
  2860. $value['supplierName'] = isset($wsm['company']) ? $wsm['company'] : "";
  2861. $data[] = $value;
  2862. }
  2863. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  2864. }
  2865. /**
  2866. * @return \think\response\Json|void
  2867. * @throws \think\db\exception\DataNotFoundException
  2868. * @throws \think\db\exception\DbException
  2869. * @throws \think\db\exception\ModelNotFoundException
  2870. */
  2871. //工差单审核
  2872. public function diffcheck()
  2873. {
  2874. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2875. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2876. if ($id === "") {
  2877. return error_show(1004, "参数id不能为空");
  2878. }
  2879. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  2880. if (empty($info)) {
  2881. return error_show(1004, "订单数据未找到");
  2882. }
  2883. $sale = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  2884. if ($sale == false) {
  2885. return error_show(1004, "订单数据未找到");
  2886. }
  2887. $cgd = Db::name("purchease_diff")->where(['id' => $info['cgd_diffid']])->find();
  2888. if ($cgd == false) {
  2889. return error_show(1004, "采购工差单数据未找到");
  2890. }
  2891. $is_act = isset($this->post['is_act']) && $this->post['is_act'] !== "" ? intval($this->post['is_act']) : "";
  2892. if ($is_act === '') {
  2893. return error_show(1004, "参数is_act不能为空");
  2894. }
  2895. $customer_remark = isset($this->post['customer_remark']) && $this->post['customer_remark'] !== "" ? trim($this->post['customer_remark']) : "";
  2896. if ($customer_remark === '') {
  2897. return error_show(1004, "参数customer_remark不能为空");
  2898. }
  2899. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "2";
  2900. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  2901. $item = $info['status'];
  2902. $info['is_act'] = $is_act;
  2903. $info['customer_remark'] = $customer_remark;
  2904. $info['status'] = $status;
  2905. $info['remark'] = $remark;
  2906. $info['updatetime'] = date("Y-m-d H:i:s");
  2907. Db::startTrans();
  2908. try {
  2909. $up = Db::name("sale_diff")->save($info);
  2910. if ($up) {
  2911. $stx = ["order_code" => $info["orderCode"], "status" => $item, "action_remark" => '', "action_type" => "edit"];
  2912. ActionLog::logAdd($this->post['token'], $stx, "XSGCD", $info['status'], $info);
  2913. $order = ["order_type" => "XSGCD", "order_code" => $info["orderCode"], "order_id" => $info['id'], "order_status" => $info['status'], "before_status" => $item];
  2914. ProcessOrder::AddProcess($this->post['token'], $order);
  2915. $upda = [
  2916. "status" => $status,
  2917. "remark" => $remark,
  2918. "updatetime" => date("Y-m-d H:i:s"),
  2919. ];
  2920. $old_purchease_diff = Db::name("purchease_diff")
  2921. ->field('id,cgdNo,status')
  2922. ->where(["id" => $info['cgd_diffid']])
  2923. ->find();
  2924. $cgdup = Db::name("purchease_diff")->where(["id" => $info['cgd_diffid']])->save($upda);
  2925. if ($cgdup) {
  2926. if ($status == 2) {
  2927. if ($customer_remark == 1) {
  2928. $sale['diff_weight'] = $info['diff_weight'];
  2929. $sale['diff_fee'] = $info['diff_price'];
  2930. $sale['updatetime'] = date("Y-m-d H:i:s");
  2931. $sal = Db::name("sale")->save($sale);
  2932. if ($sal == false) {
  2933. Db::rollback();
  2934. return error_show(1003, "订单更新工差失败");
  2935. }
  2936. }
  2937. $cgdup = Db::name("purchease_order")->where(["cgdNo" => $cgd['cgdNo']])->save
  2938. (["diff_weight" => $cgd['diff_weight'], "diff_fee" => $cgd['diff_price'], "updatetime" => date("Y-m-d H:i:s")]);
  2939. if ($cgdup == false) {
  2940. Db::rollback();
  2941. return error_show(1003, "采购订单更新工差失败");
  2942. }
  2943. }
  2944. $a = ["order_code" => $old_purchease_diff["cgdNo"], "status" => $old_purchease_diff['status'], "action_remark" => '', "action_type" => "status"];
  2945. ActionLog::logAdd($this->post['token'], $a, "CGGCD", $upda['status'], $info);
  2946. $b = ["order_type" => "CGGCD", "order_code" => $old_purchease_diff["cgdNo"], "order_id" => $old_purchease_diff['id'], "order_status" => $upda['status'], "before_status" => $old_purchease_diff['status']];
  2947. ProcessOrder::AddProcess($this->post['token'], $b);
  2948. Db::commit();
  2949. return app_show(0, "更新成功");
  2950. }
  2951. }
  2952. Db::rollback();
  2953. return error_show(1003, "更新失败");
  2954. } catch (\Exception $e) {
  2955. Db::rollback();
  2956. return error_show(1003, $e->getMessage());
  2957. }
  2958. }
  2959. /**
  2960. * @return \think\response\Json|void
  2961. * @throws \think\db\exception\DataNotFoundException
  2962. * @throws \think\db\exception\DbException
  2963. * @throws \think\db\exception\ModelNotFoundException
  2964. */
  2965. public function diffstatus()
  2966. {
  2967. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2968. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2969. if ($id === "") {
  2970. return error_show(1004, "参数id不能为空");
  2971. }
  2972. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  2973. if ($info == false) {
  2974. return error_show(1004, "工差单数据未找到");
  2975. }
  2976. $sale = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  2977. if ($sale == false) {
  2978. return error_show(1004, "订单数据未找到");
  2979. }
  2980. $cgd = Db::name("purchease_diff")->where(['id' => $info['cgd_diffid']])->find();
  2981. if ($cgd == false) {
  2982. return error_show(1004, "采购工差单数据未找到");
  2983. }
  2984. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "3";
  2985. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  2986. $ite = $info['status'];
  2987. Db::startTrans();
  2988. try {
  2989. $upda = [
  2990. "status" => $status,
  2991. "remark" => $remark,
  2992. "updatetime" => date("Y-m-d H:i:s"),
  2993. ];
  2994. $up = Db::name("sale_diff")->where($info)->save($upda);
  2995. if ($up) {
  2996. $stx = ["order_code" => $info["orderCode"], "status" => $ite, "action_remark" => '', "action_type" => "status"];
  2997. ActionLog::logAdd($this->post['token'], $stx, "XSGCD", $status, $info);
  2998. $order = ["order_type" => "XSGCD", "order_code" => $info["orderCode"], "order_id" => $info['id'],
  2999. "order_status" => $status, "before_status" => $ite];
  3000. ProcessOrder::AddProcess($this->post['token'], $order);
  3001. $cgdup = Db::name("purchease_diff")->where(["id" => $info['cgd_diffid']])->save($upda);
  3002. if ($cgdup) {
  3003. if ($status == 2 && $info['customer_remark'] == 1) {
  3004. $sale['diff_weight'] = $info['diff_weight'];
  3005. $sale['diff_fee'] = $info['diff_price'];
  3006. $sale['updatetime'] = date("Y-m-d H:i:s");
  3007. $sal = Db::name("sale")->save($sale);
  3008. if ($sal == false) {
  3009. Db::rollback();
  3010. return error_show(1003, "订单更新工差失败");
  3011. }
  3012. }
  3013. Db::commit();
  3014. return app_show(0, "更新成功");
  3015. }
  3016. }
  3017. Db::rollback();
  3018. return error_show(1003, "更新失败");
  3019. } catch (\Exception $e) {
  3020. Db::rollback();
  3021. return error_show(1003, $e->getMessage());
  3022. }
  3023. }
  3024. /**
  3025. * @return \think\response\Json|void
  3026. * @throws \think\db\exception\DataNotFoundException
  3027. * @throws \think\db\exception\DbException
  3028. * @throws \think\db\exception\ModelNotFoundException
  3029. */
  3030. public function diffinfo()
  3031. {
  3032. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3033. if ($id === "") {
  3034. return error_show(1004, "参数id不能为空");
  3035. }
  3036. $info = Db::name('sale_diff')->where(["id" => $id])->find();
  3037. if (empty($info)) {
  3038. return error_show(1004, "工差订单数据未找到");
  3039. }
  3040. $einfo = Db::name('sale')->where(['orderCode' => $info['orderCode'], 'is_del' => 0])->find();
  3041. if ($einfo == false) {
  3042. return error_show(1002, "未找到销售订单数据");
  3043. }
  3044. if ($einfo['order_type'] == 3) {
  3045. $goon = Db::name("good_zixun")->where(["spuCode" => $einfo['good_code'], "is_del" => 0])->find();
  3046. } else {
  3047. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3048. ->where(['a.skuCode' => $einfo['skuCode']])->find();
  3049. }
  3050. if ($goon == false) {
  3051. return error_show(1002, "未找到商品数据");
  3052. }
  3053. $info['skuCode'] = isset($goon['skuCode']) ? $goon['skuCode'] : "";
  3054. $info['spuCode'] = isset($goon['spuCode']) ? $goon['spuCode'] : "";
  3055. $info['order_type'] = isset($einfo['order_type']) ? $einfo['order_type'] : "";
  3056. $info['can'] = isset($info['cat_id']) && $info['cat_id'] !== 0 ? made($info['cat_id']) : [];
  3057. return app_show(0, "获取成功", $info);
  3058. }
  3059. public function saleout()
  3060. {
  3061. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  3062. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  3063. $where = [["a.is_del", "=", 0]];
  3064. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  3065. if ($orderCode !== "") {
  3066. $where[] = ['a.orderCode', "like", "%$orderCode%"];
  3067. }
  3068. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? trim($this->post['order_type']) : "";
  3069. if ($order_type !== "") {
  3070. $where[] = ['a.order_type', "=", $order_type];
  3071. }
  3072. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3073. if ($outCode !== "") {
  3074. $where[] = ['a.outCode', "like", "%$outCode%"];
  3075. }
  3076. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  3077. if ($apply_name !== "") {
  3078. $where[] = ['a.apply_name', "like", "%$apply_name%"];
  3079. }
  3080. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  3081. if ($good_code !== "") {
  3082. $where[] = ['b.good_code', "like", "%$good_code%"];
  3083. }
  3084. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  3085. if ($good_name !== "") {
  3086. $where[] = ['b.good_name', "like", "%$good_name%"];
  3087. }
  3088. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  3089. if ($status !== "") {
  3090. $where[] = ['a.status', "=", $status];
  3091. }
  3092. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  3093. if ($start != "") {
  3094. $where[] = ["a.addtime", '>=', $start];
  3095. }
  3096. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  3097. if ($end != "") {
  3098. $where[] = ["a.addtime", '<=', $end];
  3099. }
  3100. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  3101. if ($companyNo != "") {
  3102. $where[] = ['b.customer_code', "like", "%$companyNo%"];
  3103. }
  3104. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] !== "" ? $this->post['cgdNo'] : "";
  3105. if ($cgdNo != "") {
  3106. $where[] = ["os.cgdNo", 'like', '%' . $cgdNo . '%'];
  3107. }
  3108. $role = $this->checkRole();
  3109. $condition = '';
  3110. if (!empty($role['write']) && $this->uid != "") {
  3111. // $where[]=["a.apply_id","in",$role['write']];
  3112. $condition .= " (b.is_stock=1 and n.contactor = {$this->uid}) or (b.is_stock=0 and wpo.cgder_id = {$this->uid}) or a.apply_id in (" . implode(',',
  3113. $role['write']) . ")";
  3114. }
  3115. // if(!empty($role['platform']) ){
  3116. // $where[]=["b.platform_id","in",$role['platform']];
  3117. // }
  3118. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  3119. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  3120. //发货时间筛选
  3121. $start_sendtime = isset($this->post['start_sendtime']) && $this->post['start_sendtime'] !== "" ? $this->post['start_sendtime'] : "";
  3122. $end_sendtime = isset($this->post['end_sendtime']) && $this->post['end_sendtime'] !== "" ? $this->post['end_sendtime'] : "";
  3123. if ($start_sendtime != "" && $end_sendtime != "") {
  3124. $where[] = ["a.sendtime", 'between', [$start_sendtime." 00:00:00", $end_sendtime." 223:59:59"]];
  3125. $where[] = ["a.status", '>=', 2];//搜索发货时间时,要指定状态为已发货及之后的状态值(0待发货,1待库管发货,2已发货待收货,3已收货,4已全部退货',)
  3126. }
  3127. $count = Db::name('order_out')
  3128. ->alias('a')
  3129. ->join("sale b", "b.orderCode=a.orderCode", "left")
  3130. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  3131. ->join("warehouse_info n", "n.wsm_code=a.wsm_code", "left")
  3132. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  3133. ->leftJoin("order_send os", "os.outCode=a.outCode")
  3134. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  3135. ->where($where)
  3136. ->where($condition)
  3137. ->count();
  3138. $total = ceil($count / $size);
  3139. $page = $page >= $total ? $total : $page;
  3140. $list = Db::name('order_out')
  3141. ->alias('a')
  3142. ->join("sale b", "b.orderCode=a.orderCode", "left")
  3143. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  3144. ->join("warehouse_info n", "n.wsm_code=a.wsm_code AND n.is_del=0", "left")
  3145. ->leftJoin("order_send os", "os.outCode=a.outCode")
  3146. ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  3147. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  3148. ->field("a.*,b.order_type,b.order_source,b.good_name,b.good_code,b.skuCode,b.supplierNo,b.customer_code,v.companyName,b.origin_price,b.sale_price,b.total_price,os.cgdNo,u.itemid")
  3149. ->where($where)
  3150. ->where($condition)
  3151. ->order("addtime desc")
  3152. ->page($page, $size)
  3153. ->cursor();
  3154. $data = [];
  3155. foreach ($list as $value) {
  3156. $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  3157. ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  3158. $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find();
  3159. if($addr){
  3160. $addinfo =$addr['addr_code']!=''?json_decode($addr['addr_code'],true)??$addr['addr_code']: '' ;
  3161. if(is_string($addinfo) &&$addinfo!=''){
  3162. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  3163. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$addr['addr_code']);
  3164. }
  3165. $addr['addr_cn'] =GetAddr(json_encode($addinfo));
  3166. }
  3167. if ($value['order_type'] == 3) {
  3168. $goon = Db::name("good_zixun")->field('id,cat_id')->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  3169. } else {
  3170. $goon = Db::name('good_platform')->field('a.id,b.cat_id')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $value['skuCode']])->find();
  3171. }
  3172. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  3173. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  3174. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  3175. $value['addr'] = isset($addr['addr']) ? $addr['addr_cn'].$addr['addr'] : "";
  3176. $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  3177. $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  3178. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  3179. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  3180. $value['sendtime'] = $value['status'] < 2 ? '' : $value['sendtime'];
  3181. $data[] = $value;
  3182. }
  3183. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  3184. }
  3185. public function outinfo()
  3186. {
  3187. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3188. if ($outCode == "") {
  3189. return error_show(1002, "参数outcode不能为空");
  3190. }
  3191. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find();
  3192. if (empty($codeinfo)) {
  3193. return error_show(1002, "未找到出库数据");
  3194. }
  3195. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  3196. if ($item['order_type'] == 3) {
  3197. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find();
  3198. if ($goodinfo == false) {
  3199. return error_show(1004, "未找到商品数据");
  3200. }
  3201. } else {
  3202. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3203. ->where(['a.skuCode' => $item['skuCode']])->find();
  3204. if ($goodinfo == false) {
  3205. return error_show(1002, "未找到商品数据");
  3206. }
  3207. }
  3208. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  3209. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  3210. if($addr){
  3211. $addinfo =$addr['addr_code']!=''?json_decode($addr['addr_code'],true)??$addr['addr_code']: '' ;
  3212. if(is_string($addinfo)&&$addinfo!=''){
  3213. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  3214. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$addr['addr_code']);
  3215. }
  3216. $addr['addr_cn'] =GetAddr(json_encode($addinfo));
  3217. }
  3218. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  3219. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  3220. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  3221. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  3222. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  3223. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  3224. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  3225. $codeinfo['addr'] = $addr['addr_cn']??'';
  3226. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  3227. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  3228. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  3229. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  3230. $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime'];
  3231. $codeinfo['can'] = $int;
  3232. $codeinfo['order_source'] = $item['order_source'];
  3233. $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find();
  3234. $codeinfo['order_return'] = $orderReturn;
  3235. return app_show(0, "获取成功", $codeinfo);
  3236. }
  3237. public function outadd()
  3238. {
  3239. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3240. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  3241. if ($orderCode == "") {
  3242. return error_show(1002, "参数orderCoder不能为空");
  3243. }
  3244. $der = Db::name('sale')->where(['orderCode' => $orderCode, "is_del" => 0])->find();
  3245. if ($der == "") {
  3246. return error_show(1002, "未找到出库订单单号");
  3247. }
  3248. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  3249. if ($send_num == "") {
  3250. return error_show(1002, "发货数量不能为空");
  3251. }
  3252. $wsed = Db::name("order_out")->where(["orderCode" => $orderCode])->where([["status", "<", 2], ["is_del", "=", 0]])->sum
  3253. ('send_num');
  3254. if ($send_num > $der['wsend_num'] - $wsed) {
  3255. return error_show(1002, "超出可发货数量");
  3256. }
  3257. $contactor = isset($this->post['contactor']) && $this->post['contactor'] != "" ? trim($this->post['contactor']) : "";
  3258. if ($contactor == "") {
  3259. return error_show(1002, "参数contactor不能为空");
  3260. }
  3261. $mobile = isset($this->post['mobile']) && $this->post['mobile'] != "" ? trim($this->post['mobile']) : "";
  3262. if ($mobile == "") {
  3263. return error_show(1002, "参数mobile不能为空");
  3264. }
  3265. $addr = isset($this->post['addr']) && $this->post['addr'] != "" ? trim($this->post['addr']) : "";
  3266. if ($addr == "") {
  3267. return error_show(1002, "参数addr不能为空");
  3268. }
  3269. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code'] != "" ? trim($this->post['addr_code']) : "";
  3270. if ($addr_code == "") {
  3271. return error_show(1002, "参数addr_code不能为空");
  3272. }
  3273. $apply_id = GetUserInfo($token);
  3274. if (empty($apply_id) || $apply_id['code'] != 0) {
  3275. return error_show(1002, "申请人数据不存在");
  3276. }
  3277. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  3278. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  3279. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  3280. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  3281. ->find();
  3282. if (empty($order)) {
  3283. return error_show(1004, "未找到可以发货得采购单数据");
  3284. }
  3285. Db::startTrans();
  3286. try {
  3287. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  3288. ->lock(true)->find();
  3289. if ($order == false) {
  3290. Db::rollback();
  3291. return error_show(1004, "未找到可以发货得采购单数据");
  3292. }
  3293. $addrlst = [
  3294. "orderCode" => $orderCode,
  3295. "addr" => $addr,
  3296. "addr_code" => json_encode($addr_code),
  3297. "contactor" => $contactor,
  3298. "mobile" => $mobile,
  3299. "customer_code" => $der['customer_code'],
  3300. "post_fee" => $der['post_fee'],
  3301. "arrive_time" => $sendtime,
  3302. "receipt_quantity" => $send_num,
  3303. "is_del" => 0,
  3304. "addtime" => date("Y-m-d H:i:s"),
  3305. "updatetime" => date("Y-m-d H:i:s")
  3306. ];
  3307. $addrid = Db::name("order_addr")->insert($addrlst, true);
  3308. if ($addrid > 0) {
  3309. // $der['send_num'] +=$send_num;
  3310. // $der['wsend_num'] -= $send_num;
  3311. // $der['send_status'] = $der['send_num'] == 0 ? 1 : $der['wsend_num'] == 0 ? 3 : 2;
  3312. // $der['status'] = $der['send_num'] == 0 ? 0 : $der['wsend_num'] == 0 ?2 :1;
  3313. // $der['updatetime'] = date("Y-m-d H:i:s");
  3314. // $si = Db::name('sale')->save($der);
  3315. // if($si==false){
  3316. // Db::rollback();
  3317. // return error_show(1002,"出库单新建失败");
  3318. // }
  3319. $outCode = makeNo("DF");
  3320. $cgd = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  3321. if ($cgd == false) {
  3322. Db::rollback();
  3323. return error_show(1002, "未找到对应的采购单数据");
  3324. }
  3325. $order['wsend_num'] -= $send_num;
  3326. $order['send_num'] += $send_num;
  3327. $or = Db::name("order_num")->save($order);
  3328. if ($or == false) {
  3329. Db::rollback();
  3330. return error_show(1002, "发货地址添加创建失败");
  3331. }
  3332. $tep = [
  3333. "cgdNo" => $order['cgdNo'],
  3334. "outCode" => $outCode,
  3335. "send_num" => $send_num,
  3336. "status" => 1,
  3337. "addtime" => date("Y-m-d H:i:s"),
  3338. "updatetime" => date("Y-m-d H:i:s")
  3339. ];
  3340. $sen = Db::name("order_send")->save($tep);
  3341. if ($sen == false) {
  3342. Db::rollback();
  3343. return error_show(1002, "发货地址添加创建失败");
  3344. }
  3345. $data = [
  3346. "wsm_code" => isset($cgd['wsm_code']) ? $cgd['wsm_code'] : "",
  3347. "orderCode" => $orderCode,
  3348. "outCode" => $outCode,
  3349. "order_type" => $der['order_type'],
  3350. "apply_id" => $rm,
  3351. "apply_name" => $ri,
  3352. "addrid" => $addrid,
  3353. "post_name" => "",
  3354. "post_code" => "",
  3355. "post_fee" => "",
  3356. "sendtime" => $sendtime,
  3357. "send_num" => $send_num,
  3358. "check_num" => 0,
  3359. "error_num" => 0,
  3360. "status" => 1,
  3361. "addtime" => date("Y-m-d H:i:s"),
  3362. "updatetime" => date("Y-m-d H:i:s")
  3363. ];
  3364. if ($der['is_stock'] == 0) {
  3365. //维护商品所在仓库的库存 --- start
  3366. $temp = Db::name('good_stock')
  3367. ->field('id,usable_stock,wait_out_stock')
  3368. ->where(['spuCode' => $der['good_code'], 'wsm_code' => $cgd['wsm_code']])
  3369. ->find();
  3370. if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 1) {
  3371. Db::rollback();
  3372. return error_show(1004, '库存不足');
  3373. } else {
  3374. if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 2) {
  3375. $data['status'] = 0;
  3376. } else {
  3377. $up = Db::name('good_stock')
  3378. ->where('id', $temp['id'])
  3379. ->update([
  3380. 'usable_stock' => $temp['usable_stock'] - $send_num,
  3381. 'wait_out_stock' => $temp['wait_out_stock'] + $send_num,
  3382. 'updatetime' => date('Y-m-d H:i:s'),
  3383. ]);
  3384. if ($up == false) {
  3385. Db::rollback();
  3386. return error_show(1004, '库存更新失败');
  3387. }
  3388. // ::todo
  3389. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  3390. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 2, 'stock' => $send_num, "stock_name" => "usable_stock"];
  3391. GoodLog::LogAdd($this->post['token'], $good_data, "CKD");
  3392. }
  3393. }
  3394. }
  3395. $datainfo = Db::name('order_out')->insert($data, true);
  3396. if ($datainfo > 0) {
  3397. //修改状态,添加待办
  3398. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  3399. "order_code" => $data['outCode'],//出库单号
  3400. "status" => $data['status'],//这里的status是之前的值
  3401. "action_remark" => '',//备注
  3402. "action_type" => "create"//新建create,编辑edit,更改状态status
  3403. ], "CKD", $data['status'], $data);
  3404. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  3405. "order_type" => 'CKD',
  3406. "order_code" => $data['outCode'],//出库单号
  3407. "order_id" => $datainfo,
  3408. "order_status" => $data['status'], "before_status" => 0
  3409. ]);
  3410. //维护台账记录
  3411. Db::name('standing_book')
  3412. ->where('orderCode', $orderCode)
  3413. ->update([
  3414. 'order_type' => $der['order_type'],
  3415. 'order_source' => $der['order_source'],
  3416. 'cgdNo' => $order['cgdNo'],
  3417. 'customer_code' => $der['customer_code'],
  3418. 'updatetime' => date('Y-m-d H:i:s'),
  3419. ]);
  3420. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),',{$outCode}') WHERE `cgdNo`='{$cgd['cgdNo']}'");
  3421. Db::commit();
  3422. return app_show(0, "出库单新建成功");
  3423. }
  3424. }
  3425. Db::rollback();
  3426. return error_show(1004, "出库单新建失败");
  3427. } catch (Exception $e) {
  3428. Db::rollback();
  3429. return error_show(1005, $e->getMessage());
  3430. }
  3431. }
  3432. //库管发货
  3433. public function outSend()
  3434. {
  3435. $outCode = isset($this->post['outCode']) && $this->post['outCode'] != "" ? trim($this->post['outCode']) : "";
  3436. if ($outCode == "") {
  3437. return error_show(1004, "参数outCode不能为空");
  3438. }
  3439. $outinfo = Db::name("order_out")->where(["outCode" => $outCode, "is_del" => 0])->find();
  3440. if ($outinfo == false) {
  3441. return error_show(1004, "发货数据未找到");
  3442. }
  3443. $einfo = Db::name('sale')->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->find();
  3444. if (empty($einfo)) {
  3445. return error_show(1002, "未找到销售订单数据");
  3446. }
  3447. $is_reurn = Db::name("sale_return")->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->where("status", "<", 4)->find();
  3448. if ($is_reurn != false) {
  3449. return error_show(1002, "销售单存在退货未处理完成");
  3450. }
  3451. $post_name = isset($this->post['post_name']) && $this->post['post_name'] != "" ? trim($this->post['post_name']) : "";
  3452. if ($post_name == "") {
  3453. return error_show(1004, "参数post_name不能为空");
  3454. }
  3455. $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : "";
  3456. if ($post_code == "") {
  3457. return error_show(1004, "参数post_code不能为空");
  3458. }
  3459. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  3460. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] != "" ? floatval($this->post['post_fee']) : "";
  3461. $outinfo['post_name'] = $post_name;
  3462. $outinfo['post_code'] = $post_code;
  3463. $outinfo['post_fee'] = $post_fee;
  3464. $outinfo['sendtime'] = date("Y-m-d H:i:s");
  3465. $old_outinfo_status = $outinfo['status'];
  3466. $outinfo['status'] = 2;
  3467. $outinfo['updatetime'] = date("Y-m-d H:i:s");
  3468. $outinfo['remark'] = $remark;
  3469. if ($einfo['wsend_num'] < $outinfo['send_num']) {
  3470. return error_show(1004, "订单未发货数量不足");
  3471. }
  3472. Db::startTrans();
  3473. try {
  3474. $up = Db::name("order_out")->save($outinfo);
  3475. $user_info = GetUserInfo($this->post['token']);
  3476. $uid = isset($user_info['data']['id']) ? $user_info['data']['id'] : 0;
  3477. $uname = isset($user_info['data']['nickname']) ? $user_info['data']['nickname'] : '';
  3478. if ($up) {
  3479. //修改状态,添加待办
  3480. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  3481. "order_code" => $outinfo['outCode'],//出库单号
  3482. "status" => $old_outinfo_status,//这里的status是之前的值
  3483. "action_remark" => '',//备注
  3484. "action_type" => "edit"//新建create,编辑edit,更改状态status
  3485. ], "CKD", $outinfo['status'], $outinfo);
  3486. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  3487. "order_type" => 'CKD',
  3488. "order_code" => $outinfo['outCode'],//出库单号
  3489. "order_id" => $outinfo['id'],
  3490. "order_status" => $outinfo['status'],
  3491. "before_status" => $old_outinfo_status,
  3492. 'wait_id'=>$outinfo['apply_id'],
  3493. 'wait_name'=>$outinfo['apply_name'],
  3494. ]);
  3495. $orderstatus = $einfo['status'];
  3496. $einfo['send_num'] += $outinfo['send_num'];
  3497. $einfo['wsend_num'] -= $outinfo['send_num'];
  3498. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  3499. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  3500. $einfo['updatetime'] = date("Y-m-d H:i:s");
  3501. $saleup = Db::name("sale")->save($einfo);
  3502. if ($saleup == false) {
  3503. Db::rollback();
  3504. return error_show(1002, "销售单状态更新失败");
  3505. }
  3506. // }
  3507. //修改状态,添加待办
  3508. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  3509. "order_code" => $einfo['orderCode'],//出库单号
  3510. "status" => $orderstatus,//这里的status是之前的值
  3511. "action_remark" => '',//备注
  3512. "action_type" => "edit"//新建create,编辑edit,更改状态status
  3513. ], "XSQRD", $einfo['status'], $einfo);
  3514. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  3515. "order_type" => 'XSQRD',
  3516. "order_code" => $einfo['orderCode'],//出库单号
  3517. "order_id" => $einfo['id'],
  3518. "order_status" => $einfo['status'], "before_status" => $orderstatus
  3519. ]);
  3520. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  3521. if ($stokc == false) {
  3522. Db::rollback();
  3523. return error_show(1002, "未找到库存数据");
  3524. } else {
  3525. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  3526. Db::rollback();
  3527. return error_show(1002, "超出库存数量");
  3528. }
  3529. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  3530. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  3531. // $stokc['intra_stock']+=$outinfo['send_num'];
  3532. $stokc['updatetime'] = date("Y-m-d H:i:s");
  3533. }
  3534. $stoc = Db::name("good_stock")->save($stokc);
  3535. if ($stoc == false) {
  3536. Db::rollback();
  3537. return error_show(1002, "库存更新失败");
  3538. }
  3539. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stokc['id'], "type" => 2, 'stock' => $outinfo['send_num'], "stock_name" => "wait_out_stock"];
  3540. GoodLog::LogAdd(['id' => $uid, 'nickname' => $uname], $good_data, "CKD");
  3541. Cache::store("redis")->handler()->lPush("SENDOUT", $outCode);
  3542. Db::commit();
  3543. } else {
  3544. Db::rollback();
  3545. return error_show(1004, "发货失败");
  3546. }
  3547. //如果是有赞订单的话,将发货信息推到有赞
  3548. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  3549. if ($einfo['order_source'] == 5) {
  3550. $res = curl_request(config('app.yz_domain') . 'api/yz_out_send', ['orderCode' => $einfo['orderCode'], 'out_stype' => $post_name, 'post_code' => $post_code, 'uid' => $uid, 'uname' => $uname, 'order_out' => $outCode]);
  3551. $res = json_decode($res, true);
  3552. if ($res['code'] != 0) return app_show(0, '发货成功,' . $res['message']);
  3553. }
  3554. return app_show(0, "发货成功");
  3555. } catch (\Exception $e) {
  3556. Db::rollback();
  3557. return error_show(1004, $e->getMessage());
  3558. }
  3559. }
  3560. public function RelaCgd($outinfo, array &$standing_book_da = [])
  3561. {
  3562. $cgd = Db::name("order_bk")->where([["spuCode", "=", $outinfo['spuCode']], ["is_del", "=", 0], ["balance_num", ">=",
  3563. $outinfo['good_num']], ['companyNo', "=", $outinfo['companyNo']]])->lock(true)->find();
  3564. if ($cgd == false) {
  3565. return false;
  3566. }
  3567. $good = Db::name("good")->where(["spuCode" => $outinfo['spuCode'], "is_del" => 0])->find();
  3568. if ($good == false) {
  3569. return false;
  3570. }
  3571. $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
  3572. if ($cgdinfo == false) {
  3573. return false;
  3574. }
  3575. $QrdCgd = [
  3576. "cgdNo" => makeNo("CG"),
  3577. "bkcode" => $cgdinfo['bkcode'],
  3578. 'wsm_code' => $cgdinfo['wsm_code'],
  3579. "cgder_id" => $cgdinfo['cgder_id'],
  3580. "cgder" => $cgdinfo['cgder'],
  3581. "spuCode" => $cgdinfo['spuCode'],
  3582. "good_name" => $cgdinfo['good_name'],
  3583. "good_num" => $outinfo['good_num'],
  3584. "good_price" => $cgdinfo['good_price'],
  3585. "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  3586. "pakge_fee" => $cgdinfo['pakge_fee'],
  3587. "cert_fee" => $cgdinfo['cert_fee'],
  3588. "open_fee" => $cgdinfo['open_fee'],
  3589. "delivery_fee" => $cgdinfo['delivery_fee'],
  3590. "mark_fee" => $cgdinfo['mark_fee'],
  3591. "teach_fee" => $cgdinfo['teach_fee'],
  3592. "nake_fee" => $cgdinfo['nake_fee'],
  3593. "demo_fee" => $cgdinfo['demo_fee'],
  3594. "weight" => $cgdinfo['weight'],
  3595. "diff_weight" => $cgdinfo['diff_weight'],
  3596. "diff_fee" => $cgdinfo['diff_fee'],
  3597. "gold_price" => $cgdinfo['gold_price'],
  3598. "supplierNo" => $cgdinfo['supplierNo'],
  3599. "supplier_name" => $cgdinfo['supplier_name'],
  3600. "companyNo" => $cgdinfo['companyNo'],
  3601. "send_status" => 3,
  3602. "send_num" => $outinfo['good_num'],
  3603. "wsend_num" => 0,
  3604. "remark" => $cgdinfo['remark'],
  3605. "status" => 3,
  3606. "lasttime" => $cgdinfo['lasttime'],
  3607. "is_del" => 0,
  3608. "order_type" => $outinfo['order_type'],
  3609. "order_source" => $outinfo['order_source'],
  3610. "good_type" => $cgdinfo['good_type'],
  3611. "addtime" => date("Y-m-d H:i:s"),
  3612. "updatetime" => date("Y-m-d H:i:s")
  3613. ];
  3614. $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  3615. if ($insetrCgd == false) {
  3616. return false;
  3617. } else {
  3618. $standing_book_da = array_merge($standing_book_da, [
  3619. 'orderCode' => $outinfo['orderCode'],
  3620. 'cgdNo' => $cgdinfo['cgdNo'],
  3621. 'spuCode' => $outinfo['spuCode'],
  3622. 'order_type' => $QrdCgd['order_type'],
  3623. 'order_source' => $QrdCgd['order_source'],
  3624. 'supplierNo' => $cgdinfo['supplierNo'],
  3625. 'companyNo' => $cgdinfo['companyNo'],
  3626. 'bk_code' => $cgdinfo['bkcode'],
  3627. 'purchease_id' => Db::name('purchease')->where('bk_code', $cgdinfo['bkcode'])->value('id'),
  3628. ]);
  3629. }
  3630. if ($good['is_gold_price'] == 1 && $good['is_stock'] == 1) {
  3631. $gold = Db::name("gold_price1")
  3632. ->field('id,price')
  3633. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  3634. ->order("addtime desc")
  3635. ->find();
  3636. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  3637. $sale_price = $good['demo_fee'] / $outinfo['good_num'] + $good['open_fee'] / $outinfo['good_num'] + $good['noble_weight'] * $gold["price"] + $outinfo['cost_fee'] * $good['noble_weight'] + $cgdinfo['pakge_fee'] + $cgdinfo['mark_fee'] + $cgdinfo['cert_fee'] + $cgdinfo['nake_fee'] + $cgdinfo['delivery_fee'];
  3638. $ct['cgd_gold_price'] = $gold["price"];
  3639. $updat = [
  3640. "sale_price" => $sale_price,
  3641. "total_price" => round($sale_price * $outinfo['good_num'], 2),
  3642. "origin_price" => $cgdinfo['good_price'],
  3643. "gold_price" => $gold["price"],
  3644. ];
  3645. $upsale = Db::name("sale")->where(["orderCode" => $outinfo['orderCode']])->update($updat);
  3646. if ($upsale == false) {
  3647. return false;
  3648. }
  3649. }
  3650. $merge_num = Db::name("purchease_order")->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => 1, "is_del" => 0])
  3651. ->where("order_source", "<>", 0)
  3652. ->field("sum(send_num)-sum(th_num) as num")->find();
  3653. $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  3654. $cgd['merge_num'] = $merge_num['num'];
  3655. $cgd['updatetime'] = date("Y-m-d H:i:s");
  3656. $up = Db::name("order_bk")->save($cgd);
  3657. if ($up == false) {
  3658. return false;
  3659. }
  3660. $data = [
  3661. "orderCode" => $outinfo['orderCode'],
  3662. "cgdNo" => $QrdCgd['cgdNo'],
  3663. "spuCode" => $outinfo['spuCode'],
  3664. "companyNo" => $outinfo['companyNo'],
  3665. "good_num" => $outinfo['good_num'],
  3666. "wsend_num" => $outinfo['good_num'],
  3667. "send_num" => 0,
  3668. "wait_num" => $outinfo['good_num'],
  3669. "status" => 1,
  3670. "source" => 1,
  3671. ];
  3672. $order = Db::name("order_num")->save($data);
  3673. if ($order == false) {
  3674. return false;
  3675. }
  3676. return true;
  3677. }
  3678. public function addother()
  3679. {
  3680. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  3681. if ($orderCode == "") {
  3682. return error_show(1004, "参数orderCode不能为空");
  3683. }
  3684. $order = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->find();
  3685. if ($order == false) {
  3686. return error_show(1004, "未找到订单数据");
  3687. }
  3688. $paytime = isset($this->post['paytime']) && $this->post['paytime'] != "" ? $this->post['paytime'] : "";
  3689. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? trim($this->post['workNo']) : "";
  3690. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] != "" ? trim($this->post['platform_order']) : "";
  3691. $paytime == "" ? "" : $order['paytime'] = $paytime;
  3692. $order['workNo'] = $workNo;
  3693. $order['platform_order'] = $platform_order;
  3694. $order['updatetime'] = date("Y-m-d H:i:s");
  3695. $sa = Db::name("sale")->save($order);
  3696. if ($sa) {
  3697. return app_show(0, "更新成功");
  3698. } else {
  3699. return error_show(1004, "更新失败");
  3700. }
  3701. }
  3702. public function getPrice()
  3703. {
  3704. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] != "" ? trim($this->post['skuCode']) : "";
  3705. if ($skuCode === "") {
  3706. return error_show(1003, "参数skuCode不能为空");
  3707. }
  3708. $is_activity = isset($this->post['is_activity']) && $this->post['is_activity'] !== "" ? intval($this->post['is_activity'])
  3709. : "";
  3710. if ($is_activity === "") {
  3711. return error_show(1003, "参数is_activity不能为空");
  3712. }
  3713. $sale_num = isset($this->post['sale_num']) && $this->post['sale_num'] !== "" ? intval($this->post['sale_num']) : "";
  3714. if ($sale_num === "") {
  3715. return error_show(1003, "参数sale_num不能为空");
  3716. }
  3717. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3718. ->where(['a.skuCode' => $skuCode])->find();
  3719. if ($ct == false) {
  3720. return error_show(1002, "未找到商品数据");
  3721. }
  3722. $stock = 0;
  3723. if ($ct['is_stock'] == 1) {
  3724. $good_stock = Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")->where
  3725. (["spuCode" => $ct['spuCode'], "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5])->field("a.id,a.usable_stock,a.wait_out_stock")->find();
  3726. // $good_stock = Db::name("good_stock")->where(["spuCode" => $ct['spuCode'], "is_del" => 0])->find();
  3727. $stock = isset($good_stock['usable_stock']) ? $good_stock['usable_stock'] : "0";
  3728. }
  3729. if ($is_activity == 1) {
  3730. $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  3731. ->where(["a.skuCode" => $skuCode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])->find();
  3732. if ($act == false) {
  3733. return error_show(1003, "未找到相关活动价");
  3734. }
  3735. if ($act['moq_num'] > $sale_num) {
  3736. return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  3737. }
  3738. if ($act['activity_stock'] < $sale_num) {
  3739. return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  3740. }
  3741. return app_show(0, "获取成功", ['sale_price' => $act['activity_price'], "stock" => $act['activity_stock']]);
  3742. } else {
  3743. //good_nake 成本阶梯 good_ladder销售阶梯
  3744. $origin = Db::name("good_nake")->where([["spuCode", "=", $ct['spuCode']], ["is_del", "=", 0]])->order("min_num asc")->find();
  3745. if (!$origin) {
  3746. return error_show(1003, "没有找到成本数据");
  3747. }
  3748. $good = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->order("min_num asc")->find();
  3749. if (!$good) {
  3750. return error_show(1003, "未找到相关阶梯价格");
  3751. }
  3752. if (!isset($origin['min_num'])) $origin['min_num'] = 0;
  3753. //如果是库存品的话,只取销售的库存
  3754. if ($ct['is_stock'] == 1) $lastnum = $good['min_num'];
  3755. else $lastnum = max($origin['min_num'] ?? 0, $good['min_num']);
  3756. if ($sale_num < $lastnum) {
  3757. return error_show(1003, "商品不满足起订量{$lastnum}");
  3758. }
  3759. $good_temp = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->where('min_num', '<=', $sale_num)->order("min_num desc")->find();
  3760. if (!$good_temp) {
  3761. return error_show(1003, "未找到相关阶梯价格");
  3762. }
  3763. $cat_top_list = made($ct['cat_id']);
  3764. $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
  3765. if ($ct['is_gold_price'] == 1 && $cat_top_id == 6) {
  3766. $gold = Db::name("gold_price1")
  3767. ->field('id,price')
  3768. ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  3769. ->order("addtime desc")
  3770. ->find();
  3771. $saleprice = $ct['demo_fee'] / $sale_num + $ct['open_fee'] / $sale_num + $ct['noble_weight'] * $gold["price"] + $good_temp['cost_fee'] * $ct['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee'];
  3772. return app_show(0, "获取成功", ['sale_price' => $saleprice, "stock" => $stock]);
  3773. }
  3774. return app_show(0, "获取成功", ['sale_price' => $good_temp['sale_price'], "stock" => $stock]);
  3775. }
  3776. }
  3777. public function saleuse()
  3778. {
  3779. $orderCode = isset($this->post['orderCode']) && !empty($this->post['orderCode']) ? $this->post['orderCode'] : "";
  3780. if ($orderCode == "") {
  3781. return error_show(1003, "参数orderCode不能为空");
  3782. }
  3783. $order = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->select()->toArray();
  3784. if (empty($order)) {
  3785. return error_show(1003, "订单数据未找到");
  3786. }
  3787. $useid = isset($this->post['useid']) && $this->post['useid'] != "" ? intval($this->post['useid']) : "";
  3788. if ($useid == "") {
  3789. return error_show(1003, "参数useid不能为空");
  3790. }
  3791. $use = Db::name("order_use")->where(["id" => $useid, "is_del" => 0])->find();
  3792. if ($use == false) {
  3793. return error_show(1003, "用途数据未找到");
  3794. }
  3795. $asve = ['use_order' => $useid, "updatetime" => date("Y-m-d H:i:s")];
  3796. $up = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->update($asve);
  3797. if ($up > 0) {
  3798. return app_show(0, "更新成功");
  3799. } else {
  3800. return error_show(1003, "更新失败");
  3801. }
  3802. }
  3803. public function goodzxinfo()
  3804. {
  3805. $orderCode = isset($this->post['spuCode']) && $this->post['spuCode'] != '' ? trim($this->post['spuCode']) : "";
  3806. if ($orderCode == "") {
  3807. return error_show(1003, "参数spuCode不能为空");
  3808. }
  3809. $order = Db::name("good_zixun")->where(["spuCode" => $orderCode, "is_del" => 0])->find();
  3810. if (empty($order)) {
  3811. return error_show(1003, "订单数据未找到");
  3812. }
  3813. $unit = Db::name("unit")->where(["id" => $order['good_unit']])->find();
  3814. if ($order['brand_id'] != 0) {
  3815. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  3816. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  3817. } else {
  3818. $order["brand_name"] = "";
  3819. $order["brand_id"] = "";
  3820. }
  3821. $order['specinfo'] = json_decode($order['specinfo'], true);
  3822. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  3823. $order['cat_info'] = made($order['cat_id'], []);
  3824. $order['noble_name'] = isset($order['noble_metal']) && $order['noble_metal'] != 0 ? $this->noble[$order['noble_metal']] : "";
  3825. if ($order['is_gold_price'] == 1) {
  3826. $price = Db::name("gold_price1")->where(["type" => $order['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  3827. }
  3828. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  3829. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  3830. $palt = Db::name("platform")->where(["id" => $order['platform_id']])->find();
  3831. $order['platform_name'] = isset($palt['platform_name']) ? $palt['platform_name'] : "";
  3832. $company = Db::name("business")->where(["companyNo" => $order['companyNo']])->find();
  3833. $order['company'] = isset($company['company']) ? $company['company'] : "";
  3834. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  3835. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  3836. return app_show(0, "获取成功", $order);
  3837. }
  3838. //获取采反商品详情,当数据不在good_zixun中,模仿goodzxinfo
  3839. public function getGoodZxInfoByNotZixun()
  3840. {
  3841. $param = $this->request->only(['spuCode'], 'post', 'trim');
  3842. $val = Validate::rule(['spuCode' => 'require']);
  3843. if (!$val->check($param)) return error_show(1004, $val->getError());
  3844. $order = Db::name('consult_bids')
  3845. ->field(true)
  3846. ->where(['spuCode' => $param['spuCode'], 'is_del' => 0])
  3847. ->find();
  3848. if (empty($order)) return error_show(1003, "数据未找到");
  3849. // $orderCode= isset($this->post['spuCode'])&&$this->post['spuCode']!=''?trim($this->post['spuCode']):"";
  3850. // if($orderCode==""){
  3851. // return error_show(1003,"参数spuCode不能为空");
  3852. // }
  3853. // $order= Db::name("good_zixun")->where(["spuCode"=>$orderCode,"is_del"=>0])->find();
  3854. // if(empty($order)){
  3855. // return error_show(1003,"订单数据未找到");
  3856. // }
  3857. $unit = Db::name("unit")->where(["id" => $order['unit_id']])->find();
  3858. if ($order['brand_id'] != 0) {
  3859. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  3860. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  3861. } else {
  3862. $order["brand_name"] = "";
  3863. $order["brand_id"] = "";
  3864. }
  3865. $order['specinfo'] = json_decode($order['specinfo'], true);
  3866. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  3867. $order['cat_info'] = made($order['cat_id'], []);
  3868. $order['noble_name'] = isset($order['metal_id']) && $order['metal_id'] != 0 ? $this->noble[$order['metal_id']] : "";
  3869. if ($order['is_gold_price'] == 1) {
  3870. $price = Db::name("gold_price1")->where(["type" => $order['metal_id'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  3871. }
  3872. $supplier = Db::name("supplier")->where(["code" => $order['supplierNo']])->find();
  3873. $order['supplier_name'] = isset($supplier['name']) ? $supplier['name'] : "";
  3874. // $palt =Db::name("platform")->where(["id"=>$order['platform_id']])->find();
  3875. // $order['platform_name']=isset($palt['platform_name']) ? $palt['platform_name']:"";
  3876. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  3877. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  3878. return app_show(0, "获取成功", $order);
  3879. }
  3880. //发货申请单导出
  3881. public function exportSaleOut()
  3882. {
  3883. $outCodes = $this->request->post('outCodes', [], 'trim');
  3884. if (empty($outCodes)) return error_show(1004, '要导出的发货申请单编号不能为空');
  3885. $i = 1;
  3886. $send_type = [1 => '直接发货', 2 => '延时发货'];
  3887. $status = [0 => '待发货', 1 => '待库管发货', 2 => '已发货待收货', 3 => '已收货', 4 => '已全部退货'];
  3888. $list = Db::name("order_out")
  3889. ->alias('a')
  3890. ->field('"" as 序号,a.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,b.orderCode as 确认单号,b.addtime as 确认单时间,b.good_code as 产品编号,b.good_name as 产品名称,"" as 规格,"" as 单位,wi.name as 供应商名称,s.code as 供应商编号,po.nake_fee as 裸价,a.post_fee as 物流费,b.send_type as 发货方式,po.good_price as 采购单价,b.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,a.send_num 发货数量,oa.contactor as 收货人,oa.mobile as 联系方式,oa.addr as 收货地址,oa.addr_code,oa.arrive_time as 到货时间,a.outCode as 发货申请单号,po.order_type,b.platform_order 平台订单号,a.post_name 发货申请单物流公司,a.post_code 发货申请单物流单号,a.post_fee 发货申请单物流费用,a.remark 发货申请单备注,a.apply_name 申请人,b.cat_id 商品分类')
  3891. ->whereIn('a.outCode', $outCodes)
  3892. ->order("a.addtime desc")
  3893. ->withAttr('序号', function () use (&$i) {
  3894. return $i++;
  3895. })->withAttr('采购单状态', function ($val) use ($status) {
  3896. return isset($status[$val]) ? $status[$val] : '';
  3897. })->withAttr('发货方式', function ($val) use ($send_type) {
  3898. return isset($send_type[$val]) ? $send_type[$val] : '';
  3899. })->where("a.is_del", "=", 0)
  3900. ->leftJoin("sale b", "b.orderCode=a.orderCode")
  3901. ->leftJoin("order_addr oa", "oa.id=a.addrid AND oa.is_del=0")
  3902. ->leftJoin("warehouse_info wi", "wi.wsm_code=a.wsm_code AND wi.is_del=0")
  3903. ->leftJoin("supplier s", "s.code=wi.supplierNo AND s.is_del=0")
  3904. ->leftJoin('order_send os', 'os.outCode=a.outCode ')
  3905. ->leftJoin("purchease_order po", "po.cgdNo=os.cgdNo AND po.is_del=0")
  3906. ->select()
  3907. ->toArray();
  3908. foreach ($list as &$value) {
  3909. if ($value['order_type'] == 3) {
  3910. $temp = Db::name("good_zixun")
  3911. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  3912. ->field('id,specinfo,good_unit')
  3913. ->find();
  3914. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  3915. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  3916. $speclist = [];
  3917. foreach ($specinfo as $val) {
  3918. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  3919. }
  3920. } else {
  3921. $good_unit = Db::name('good_basic')
  3922. ->where(['spuCode' => $value['产品编号']])
  3923. ->value('good_unit', 0);
  3924. $spec = Db::name("good_spec")
  3925. ->field('id,spec_id,spec_value_id')
  3926. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  3927. ->select()
  3928. ->toArray();
  3929. $speclist = [];
  3930. if (!empty($spec)) {
  3931. foreach ($spec as $val) {
  3932. $speclist[] = Db::name("specs")->where(["id" => $val['spec_id']])->value('spec_name', '') . ':' . Db::name("spec_value")->where(["id" => $val['spec_value_id']])->value('spec_value', '');
  3933. }
  3934. }
  3935. }
  3936. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  3937. $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
  3938. $value['商品分类'] = implode('/', array_column(made($value['商品分类']), 'name'));
  3939. $value['发货申请单物流单号'] = ' ' . $value['发货申请单物流单号'];//添加空格防止将数字转为科学计数法(比如,韵达的快递单号就是纯数字)
  3940. $addinfo =$value['addr_code']!=''?json_decode($value['addr_code'],true)??$value['addr_code']: '' ;
  3941. if(is_string($addinfo)&&$addinfo!=''){
  3942. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  3943. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$value['addr_code']);
  3944. }
  3945. $addr_cn =GetAddr(json_encode($addinfo));
  3946. $value['收货地址'] = $addr_cn.$value['收货地址'];
  3947. unset($value['order_type']);
  3948. unset($value['addr_code']);
  3949. }
  3950. if (empty($list)) $list[] = '没有可供导出的数据';
  3951. $headerArr = array_keys($list[0]);
  3952. excelSave('发货申请单导出' . date('YmdHis'), $headerArr, $list);
  3953. }
  3954. //发货申请单 签收(目前只改动状态,后面要实时查询物流信息,以判断是否签收)
  3955. public function saleoutReceipt()
  3956. {
  3957. $outCode = $this->request->post('outCode', '', 'trim');
  3958. if (empty($outCode)) return error_show(1004, '发货申请单编号不能为为空');
  3959. Db::startTrans();
  3960. try {
  3961. $rs = Db::name('order_out')
  3962. ->field('id,orderCode')
  3963. ->where(['outCode' => $outCode, 'status' => 2, "is_del" => 0])
  3964. ->find();
  3965. if (empty($rs)) throw new Exception('该出库单不存在或不允许确认收货');
  3966. //发货单收货
  3967. $res_order_out = Db::name('order_out')
  3968. ->where(['id' => $rs['id'], 'status' => 2, "is_del" => 0])
  3969. ->update(['status' => 3, 'updatetime' => date('Y-m-d H:i:s')]);
  3970. //查询该发货单所属的销售单,是否还有未收货的其他发货单
  3971. $other_res_sale = Db::name('order_out')
  3972. ->where(['orderCode' => $rs['orderCode'], 'status' => 2, "is_del" => 0])
  3973. ->where('id', '<>', $rs['id'])
  3974. ->find();
  3975. if (empty($other_res_sale)) {
  3976. //所属销售单也确认收货
  3977. Db::name('sale')
  3978. ->where(['orderCode' => $rs['orderCode'], 'status' => 0])
  3979. ->update(['status' => 2, 'updatetime' => date('Y-m-d H:i:s')]);
  3980. }
  3981. if ($res_order_out) {
  3982. Db::commit();
  3983. return app_show(0, '收货完成');
  3984. } else {
  3985. Db::rollback();
  3986. error_show(1005, '收货失败');
  3987. }
  3988. } catch (\Exception $e) {
  3989. Db::rollback();
  3990. return error_show(1005, $e->getMessage());
  3991. }
  3992. }
  3993. //库管批量发货
  3994. public function outSendBatchByImport()
  3995. {
  3996. $param = $this->request->only(['list', 'token'], 'post', 'trim');
  3997. if (empty($param['list'])) return error_show(1005, 'list参数不能为空');
  3998. $user = GetUserInfo($param['token']);
  3999. $createrid = isset($user['data']['id']) ? $user['data']['id'] : 0;
  4000. $creater = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  4001. //所有发货单信息
  4002. $order_out_infos = Db::name("order_out")
  4003. ->where('status', 1)
  4004. ->where('is_del', 0)
  4005. ->whereIn('outCode', array_column($param['list'], 'outCode'))
  4006. ->column('id,status,send_num,orderCode,wsm_code,apply_id,apply_name', 'outCode');
  4007. //处理数据
  4008. Db::startTrans();
  4009. try {
  4010. $good_data = [];
  4011. $temp_out_codes = [];
  4012. foreach ($param['list'] as $value) {
  4013. if (!isset($order_out_infos[$value['outCode']])) throw new Exception($value['outCode'] . '该发货单未找到或状态不允许发货');
  4014. //每次循环都需要重新查询对应数据,为了数据及时更新
  4015. $sale_infos = Db::name("sale")
  4016. ->where('orderCode', $order_out_infos[$value['outCode']]['orderCode'])
  4017. ->where('is_del', 0)
  4018. ->column('id,send_num,wsend_num,status,good_code,order_source', 'orderCode');
  4019. if (!isset($sale_infos[$order_out_infos[$value['outCode']]['orderCode']])) throw new Exception($value['outCode'] . '该发货单对应的销售单未找到');
  4020. //判断发货单是否重复
  4021. if (isset($temp_out_codes[$value['outCode']])) {
  4022. Db::rollback();
  4023. return app_show(1005, '下列发货申请单号重复', [$value]);
  4024. } else $temp_out_codes[$value['outCode']] = $value['outCode'];
  4025. //【发货单】
  4026. //查询已有发货单信息及状态
  4027. //更新发货单
  4028. $up = Db::name("order_out")
  4029. ->where(['id' => $order_out_infos[$value['outCode']]['id'], 'outCode' => $value['outCode'], 'status' => 1, "is_del" => 0])
  4030. ->update([
  4031. 'post_name' => isset($value['post_name']) ? $value['post_name'] : '',
  4032. 'post_code' => $value['post_code'],
  4033. 'post_fee' => $value['post_fee'],
  4034. 'sendtime' => date('Y-m-d H:i:s'),
  4035. 'status' => 2,
  4036. 'updatetime' => date('Y-m-d H:i:s'),
  4037. 'remark' => isset($value['remark']) ? $value['remark'] : '',
  4038. ]);
  4039. if ($up) {
  4040. //修改状态,添加待办
  4041. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  4042. "order_code" => $value['outCode'],//出库单号
  4043. "status" => $order_out_infos[$value['outCode']]['status'],//这里的status是之前的值
  4044. "action_remark" => '',//备注
  4045. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4046. ], "CKD", 2, $order_out_infos[$value['outCode']]);
  4047. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  4048. "order_type" => 'CKD',
  4049. "order_code" => $value['outCode'],//出库单号
  4050. "order_id" => $order_out_infos[$value['outCode']]['id'],
  4051. "order_status" => 2,
  4052. "before_status" => $order_out_infos[$value['outCode']]['status'],
  4053. 'wait_id' => $order_out_infos[$value['outCode']]['apply_id'],
  4054. 'wait_name' => $order_out_infos[$value['outCode']]['apply_name'],
  4055. ]);
  4056. //【销售单】
  4057. $einfo = [];
  4058. $einfo['send_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['send_num'] + $order_out_infos[$value['outCode']]['send_num'];
  4059. $einfo['wsend_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['wsend_num'] - $order_out_infos[$value['outCode']]['send_num'];
  4060. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  4061. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  4062. $einfo['updatetime'] = date("Y-m-d H:i:s");
  4063. $saleup = Db::name("sale")
  4064. ->where('id', $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'])
  4065. ->update($einfo);
  4066. if ($saleup == false) throw new Exception($value['outCode'] . '销售单状态更新失败');
  4067. //修改状态,添加待办
  4068. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  4069. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//销售单号
  4070. "status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],//这里的status是之前的值
  4071. "action_remark" => '',//备注
  4072. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4073. ], "XSQRD", $einfo['status'], $einfo);
  4074. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  4075. "order_type" => 'XSQRD',
  4076. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//出库单号
  4077. "order_id" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'],
  4078. "order_status" => $einfo['status'], "before_status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status']
  4079. ]);
  4080. //【库存】
  4081. $stock = Db::name("good_stock")
  4082. ->field('id,wait_out_stock,usable_stock,total_stock,updatetime')
  4083. ->where([
  4084. "is_del" => 0,
  4085. 'spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'],
  4086. "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'],
  4087. ])->find();
  4088. if ($stock == false) throw new Exception($value['outCode'] . '未找到库存数据');
  4089. else {
  4090. if ($order_out_infos[$value['outCode']]['send_num'] > $stock['wait_out_stock']) throw new Exception($value['outCode'] . '超出库存数量');
  4091. $stock['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  4092. $stock['total_stock'] = $stock['wait_out_stock'] + $stock['usable_stock'];
  4093. $stock['updatetime'] = date("Y-m-d H:i:s");
  4094. $stock_rs = Db::name("good_stock")
  4095. ->where('id', $stock['id'])
  4096. ->update($stock);
  4097. if ($stock_rs == false) throw new Exception($value['outCode'] . '库存更新失败');
  4098. $good_data[] = [
  4099. 'good_log_code' => $value['outCode'],
  4100. 'stock_id' => $stock['id'],
  4101. 'type' => 2,
  4102. 'stock' => $order_out_infos[$value['outCode']]['send_num'],
  4103. 'stock_name' => 'wait_out_stock'
  4104. ];
  4105. }
  4106. } else throw new Exception($value['outCode'] . '发货失败');
  4107. Cache::store("redis")->handler()->lPush("SENDOUT",$value['outCode']);
  4108. }
  4109. if (!empty($good_data)) GoodLog::LogAdd(['id' => $createrid, 'nickname' => $creater], $good_data, "CKD");
  4110. Db::commit();
  4111. return app_show(0, '发货成功');
  4112. } catch (Exception $exception) {
  4113. Db::rollback();
  4114. return error_show(1004, $exception->getMessage());
  4115. }
  4116. }
  4117. //获取物流信息
  4118. public function getLogisticsInformation()
  4119. {
  4120. $post_code = $this->request->post('post_code', '', 'trim');
  4121. if (empty($post_code)) return error_show(1004, '快递单号不能为空');
  4122. $postCode = explode(",", $post_code);
  4123. $rs = Db::name('express_data')
  4124. ->where('post_code', $postCode[0])
  4125. ->where('status', ">=", 0)
  4126. ->withAttr('post_data', function ($val) {
  4127. return json_decode($val, true);
  4128. })->findOrEmpty();
  4129. settype($rs, 'object');//格式统一转为对象
  4130. return app_show(0, '请求成功', $rs);
  4131. }
  4132. }