Sale.php 232 KB

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