Sale.php 296 KB

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