Sale.php 292 KB

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