Sale.php 291 KB

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