123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742 |
- <?php
- /**
- * Created by Sublime.
- * User: hao
- * Date: 19/08/30
- * Time: 上午11:20
- */
- namespace App\Http\Controllers\Admin;
- use App\CustomerOrderDay;
- use App\Http\Controllers\Controller;
- use App\Logs;
- use App\CustTotal;
- use App\CustDetail;
- use App\AdCost;
- use App\Order;
- use App\DistrictRoi7;
- use App\DistrictRoi30;
- use App\DistrictRoi15;
- use App\DistrictRoi45;
- use App\DistrictRoi60;
- use App\SalerTargets;
- use App\TemplatesLog;
- use App\OrderGoodsSkus;
- use App\CustomerInfo;
- use App\CustomerDeposit;
- use App\CustomerConsum;
- use App\Admin;
- use App\Goods;
- use App\GoodsSkus;
- use App\RedisModel as Redis;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- class StatisticsController extends Controller
- {
- protected $warehouse = [];
- public function __construct()
- {
- $this->warehouse = config('constants.WAREHOUSE');
- }
- /**
- * 当日数据统计
- */
- public function fanDay(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $self_role = session('role_name');
- $team_id = $request->input('team_id');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员' && $self_role != '财务'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- if($admin_id>0 && !$team_id){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- #进粉成本
- $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
- #当日微信粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids, $admin_id){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
- $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(freight_cost) as freight_cost, sum(cost) as goods_cost'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- #物流成本
- $v['freight_cost'] = $order->freight_cost;
- #货品成本
- $v['goods_cost'] = $order->goods_cost;
- #新加逻辑 -- 销售筛选
- if($admin_id>0){
- //预估当日投放 当日公众进粉
- # 1. 当前团队销售加粉占比
- $teamFanAdd = CustDetail::select(DB::raw('sum(fan_add) as team_fan_add'))->whereIn('admin_id', $sale_ids)->where('dtime', $v['dtime'])->where('is_del', 0)->first();
- $salerfanRate = $teamFanAdd->team_fan_add>0 ? round($v['wx_fan_add'] / $teamFanAdd->team_fan_add, 3) * 1000 : 0;
- #预估该销售投放金额
- $v['total_cost'] = round( $salerfanRate * $v['total_cost'] / 1000, 2);
- #预估进粉
- $v['total_fan_add'] = round( $salerfanRate * $v['total_fan_add'] / 1000);
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- }
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员' && $self_role != '财务') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->where(function($query) use($self_role, $team_id){
- if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员' && $self_role != '财务') $query->where('team_id', $team_id);
- })->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/fanDay', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- ]);
- }
- public function fanDay_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $self_role = session('role_name');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员' && $self_role != '财务'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="dangrishuj.xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日投放金额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日公众号进粉")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日订单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日货品成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日物流成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日老粉主动咨询量")."</th>
- </tr>";
- foreach ($result as $k => $v)
- {
- #进粉成本
- $v['cost_fan'] = $v['total_fan_add']>0 ? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
- #当日微信粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
- $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
- $v['new_reply_rate'] = $v['wx_fan_add']>0 ? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0 ? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(freight_cost) as freight_cost, sum(cost) as goods_cost'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- #物流成本
- $v['freight_cost'] = $order->freight_cost;
- #货品成本
- $v['goods_cost'] = $order->goods_cost;
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['total_cost']."</td>";
- $data_str .= "<td>".$v['total_fan_add']."</td>";
- $data_str .= "<td>".$v['cost_fan']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['cost_wx_fan']."</td>";
- $data_str .= "<td>".$v['order_count']."</td>";
- $data_str .= "<td>".$v['order_amount']."</td>";
- $data_str .= "<td>".$v['goods_cost']."</td>";
- $data_str .= "<td>".$v['freight_cost']."</td>";
- $data_str .= "<td>".$v['total_new_reply']."</td>";
- $data_str .= "<td>".$v['new_reply_rate']."</td>";
- $data_str .= "<td>".$v['total_old_consult']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 当日数据统计
- */
- public function orderDay(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $self_role = session('role_name');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
- $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime, $end_time){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime','<', $end_time);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime, $end_time){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime','<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- #当日微信好友数量
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(cost) as order_cost, sum(freight_cost) as freight_cost'))->whereIn('receiverMobile', $phones)->where('createTime','<',$end_time)->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- $old_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>', $v['dtime'].' 23:59:59')->where('createTime','<', $end_time)->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 3.老粉成单数
- $v['old_order_count'] = $old_order->order_count;
- // 4.老粉成交额
- $v['old_order_amount'] = $old_order->order_amount;
- // 5.总成交额
- $v['order_amount'] = $order->order_amount;
- #复购单数、复购成交额
- $fugou = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where('is_fugou', 1)->where('createTime','<', $end_time)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['fugou_order_count'] = $fugou->order_count;
- $v['fugou_order_amount'] = $fugou->order_amount;
- #货品成本
- $v['order_cost'] = $order->order_cost;
- //加物流成本
- $v['freight_cost'] = $order->freight_cost;
- #毛利
- $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'] - $v['freight_cost'];
- //综合成单率 综合成单率=订单数/微信好友数
- $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
- //新粉成单率
- $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
- //复购率
- $fugou_order_count_no = $order->order_count - $fugou->order_count;
- $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
- //加roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('statistics/orderDay', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- ]);
- }
- public function orderDay_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $self_role = session('role_name');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime, $end_time){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime','<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="分片数据统计.xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉数量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","总投放成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","货品成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","毛利")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","综合ROI")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉ROI")."</th>
- </tr>";
- foreach ($result as $k => $v)
- {
- #当日微信好友数量
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(cost) as order_cost'))->whereIn('receiverMobile', $phones)->where('createTime', '<', $end_time)->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- $old_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>', $v['dtime'].' 23:59:59')->where('createTime','<', $end_time)->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 3.老粉成单数
- $v['old_order_count'] = $old_order->order_count;
- // 4.老粉成交额
- $v['old_order_amount'] = $old_order->order_amount;
- // 5.总成交额
- $v['order_amount'] = $order->order_amount;
- #复购单数、复购成交额
- $fugou = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where('is_fugou', 1)->where('createTime','<', $end_time)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['fugou_order_count'] = $fugou->order_count;
- $v['fugou_order_amount'] = $fugou->order_amount;
- #货品成本
- $v['order_cost'] = $order->order_cost;
- #毛利
- $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'];
- //综合成单率 综合成单率=订单数/微信好友数
- $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
- //新粉成单率
- $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
- //复购率
- $fugou_order_count_no = $order->order_count - $fugou->order_count;
- $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
- //加roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['total_fan_add']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['new_order_count']."</td>";
- $data_str .= "<td>".$v['new_order_amount']."</td>";
- $data_str .= "<td>".$v['old_order_count']."</td>";
- $data_str .= "<td>".$v['old_order_amount']."</td>";
- $data_str .= "<td>".$v['fugou_order_count']."</td>";
- $data_str .= "<td>".$v['fugou_order_amount']."</td>";
- $data_str .= "<td>".$v['order_amount']."</td>";
- $data_str .= "<td>".$v['total_cost']."</td>";
- $data_str .= "<td>".$v['order_cost']."</td>";
- $data_str .= "<td>".$v['profit']."</td>";
- $data_str .= "<td>".$v['new_order_rate']."</td>";
- $data_str .= "<td>".$v['order_rate']."</td>";
- $data_str .= "<td>".$v['fugou_rate']."</td>";
- $data_str .= "<td>".$v['total_roi']."</td>";
- $data_str .= "<td>".$v['new_roi']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 当日数据统计(投放)
- */
- public function throwDay(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $self_role = session('role_name');
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $_data = $this->throwCtimePage($stime, $etime, $offset, $pageSize);
- $count = $_data['count'];
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $end_time = date('Y-m-d');
- foreach($_data['data'] as $k=>&$v){
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->where('is_del',0)->where('dtime', $v['date'])->first();
- $v['total_cost'] = isset($result->total_cost) ? $result->total_cost : '';
- $v['total_fan_add'] = isset($result->total_fan_add) ? $result->total_fan_add : '';
- $v['dtime'] = $v['date'];
- #进粉成本
- $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
- #当日微信粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
- $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日加粉订单总计:
- #截止昨天
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(cost) as order_cost, sum(freight_cost) as freight_cost'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->where('createTime','<',$end_time)->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 1.当日粉累计成单数
- $v['order_count'] = $order->order_count;
- // 2.当日粉累计成交额
- $v['order_amount'] = $order->order_amount;
- #当日新粉roi=当日新增粉丝成交销售额/当日投放消耗额*100%
- $v['new_roi'] = $v['total_cost']>0? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- # 综合ROI=当日加粉累计成交额/当日投放消耗额*100%
- $v['roi'] = $v['total_cost']>0? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- # 导粉率=当日个人微信号粉丝数/当日公众号粉丝数*100%
- $v['fan_rate'] = $v['total_fan_add']>0? round($v['wx_fan_add'] / $v['total_fan_add'], 4) * 100 .'%' : '';
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('statistics/throwDay', ['result' =>$_data['data'],
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- ]);
- }
- public function throwDay_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $_data = $this->throwCtimePage($stime, $etime);
- $end_time = date('Y-m-d');
- foreach($_data as $k=>&$v){
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->where('is_del',0)->where('dtime', $v['date'])->first();
- $v['total_cost'] = isset($result->total_cost) ? $result->total_cost : '';
- $v['total_fan_add'] = isset($result->total_fan_add) ? $result->total_fan_add : '';
- $v['dtime'] = $v['date'];
- #进粉成本
- $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
- #当日微信粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
- $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日加粉订单总计:
- #截止昨天
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(cost) as order_cost, sum(freight_cost) as freight_cost'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->where('createTime','<',$end_time)->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 1.当日粉累计成单数
- $v['order_count'] = $order->order_count;
- // 2.当日粉累计成交额
- $v['order_amount'] = $order->order_amount;
- #当日新粉roi=当日新增粉丝成交销售额/当日投放消耗额*100%
- $v['new_roi'] = $v['total_cost']>0? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- # 综合ROI=当日加粉累计成交额/当日投放消耗额*100%
- $v['roi'] = $v['total_cost']>0? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- # 导粉率=当日个人微信号粉丝数/当日公众号粉丝数*100%
- $v['fan_rate'] = $v['total_fan_add']>0? round($v['wx_fan_add'] / $v['total_fan_add'], 4) * 100 .'%' : '';
- }
- $indexKey = ['dtime','total_cost','total_fan_add','cost_fan','wx_fan_add','cost_wx_fan','new_roi','roi','fan_rate'];
- $title = ['日期', '当日投放金额', '当日公众号进粉', '当日进粉成本', '当日微信粉','当日微信粉成本', '当日新粉ROI', '综合ROI', '导粉率'];
-
- $filename = 'toufang_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($_data, $filename, $indexKey, $title);
-
- }
-
- /**
- * 销售可看当日数据表
- *
- */
- public function fanDaySaler(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $is_self_team = (int)$request->input('is_self_team');
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
-
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- #假如查看团队的
- if($is_self_team == 1){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- $admin_id = 0;
- }else{
- $team_id = null;
- }
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- if($end_time) $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult, sum(new_consult) as total_new_consult, dtime'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- if($end_time) $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.新粉成交转化率
- $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
- //累计老粉量
- $dtime = $v['dtime'];
- $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if($sale_ids) $query->whereIn('admin_id', $sale_ids);
- if($dtime) $query->where('dtime', '<', $dtime);
- })->where('is_del',0)->sum('fan_add');
- $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- // 3.老粉成单数
- $v['old_order_count'] = $order->order_count - $v['new_order_count'];
- // 4.老粉成交额
- $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
- // 5.老粉成交转化率
- $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/fanDaySaler', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'is_self_team' =>$is_self_team,
- 'self_role' =>$self_role,
- ]);
- }
- public function fanDaySaler_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $is_self_team = (int)$request->input('is_self_team');
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
-
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- #假如查看团队的
- if($is_self_team == 1){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- $admin_id = 0;
- }else{
- $team_id = null;
- }
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult, sum(new_consult) as total_new_consult, dtime'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- if($end_time) $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="当日数据统计(销售).xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
-
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉增加量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交单量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交转化率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","累计老粉量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交单量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交转化率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日单量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
- </tr>";
- foreach ($result as $k => $v)
- {
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.新粉成交转化率
- $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
- //累计老粉量
- $dtime = $v['dtime'];
- $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if($sale_ids) $query->whereIn('admin_id', $sale_ids);
- if($dtime) $query->where('dtime', '<', $dtime);
- })->where('is_del',0)->sum('fan_add');
- $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- // 3.老粉成单数
- $v['old_order_count'] = $order->order_count - $v['new_order_count'];
- // 4.老粉成交额
- $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
- // 5.老粉成交转化率
- $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['total_new_reply']."</td>";
- $data_str .= "<td>".$v['new_reply_rate']."</td>";
- $data_str .= "<td>".$v['total_new_consult']."</td>";
- $data_str .= "<td>".$v['new_consult_rate']."</td>";
- $data_str .= "<td>".$v['new_order_count']."</td>";
- $data_str .= "<td>".$v['new_order_amount']."</td>";
- $data_str .= "<td>".$v['new_order_rate']."</td>";
- $data_str .= "<td>".$v['wx_old_fan']."</td>";
- $data_str .= "<td>".$v['total_old_consult']."</td>";
- $data_str .= "<td>".$v['old_consult_rate']."</td>";
- $data_str .= "<td>".$v['old_order_count']."</td>";
- $data_str .= "<td>".$v['old_order_amount']."</td>";
- $data_str .= "<td>".$v['old_order_rate']."</td>";
- $data_str .= "<td>".$v['order_count']."</td>";
- $data_str .= "<td>".$v['order_amount']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 销售可看当日分片数据
- *
- */
- public function orderDaySaler(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $is_self_team = (int)$request->input('is_self_team');
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
-
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- #假如查看团队的
- if($is_self_team == 1){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- $admin_id = 0;
- }else{
- $team_id = null;
- }
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult, sum(new_consult) as total_new_consult, dtime'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- #当日老粉成单
- $old_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>', $v['dtime'].' 23:59:59')->where('createTime','<', $end_time)->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日老粉成单数
- $v['old_order_count'] = $old_order->order_count;
- // 2.当日老粉成交额
- $v['old_order_amount'] = $old_order->order_amount;
- #复购单数、复购成交额
- $fugou = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where('is_fugou', 1)->where('createTime','<', $end_time)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['fugou_order_count'] = $fugou->order_count;
- $v['fugou_order_amount'] = $fugou->order_amount;
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('createTime','<',$end_time)->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- //总成交额
- $v['order_amount'] = $order->order_amount;
- //综合成单率 综合成单率=订单数/微信好友数
- $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
- //新粉成单率
- $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
- //复购率
- $fugou_order_count_no = $order->order_count - $fugou->order_count;
- $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/orderDaySaler', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'is_self_team' =>$is_self_team,
- 'self_role' =>$self_role,
- ]);
- }
- public function orderDaySaler_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $is_self_team = (int)$request->input('is_self_team');
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
-
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- #假如查看团队的
- if($is_self_team == 1){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- $admin_id = 0;
- }else{
- $team_id = null;
- }
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult, sum(new_consult) as total_new_consult, dtime'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="当日数据统计(销售).xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
-
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
-
- </tr>";
- foreach ($result as $k => $v)
- {
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- #当日老粉成单
- $old_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>', $v['dtime'].' 23:59:59')->where('createTime','<', $end_time)->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日老粉成单数
- $v['old_order_count'] = $old_order->order_count;
- // 2.当日老粉成交额
- $v['old_order_amount'] = $old_order->order_amount;
- #复购单数、复购成交额
- $fugou = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where('is_fugou', 1)->where('createTime','<', $end_time)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['fugou_order_count'] = $fugou->order_count;
- $v['fugou_order_amount'] = $fugou->order_amount;
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('createTime','<',$end_time)->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- //总成交额
- $v['order_amount'] = $order->order_amount;
- //综合成单率 综合成单率=订单数/微信好友数
- $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
- //新粉成单率
- $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
- //复购率
- $fugou_order_count_no = $order->order_count - $fugou->order_count;
- $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['new_order_count']."</td>";
- $data_str .= "<td>".$v['new_order_amount']."</td>";
- $data_str .= "<td>".$v['old_order_count']."</td>";
- $data_str .= "<td>".$v['old_order_amount']."</td>";
- $data_str .= "<td>".$v['fugou_order_count']."</td>";
- $data_str .= "<td>".$v['fugou_order_amount']."</td>";
- $data_str .= "<td>".$v['order_amount']."</td>";
- $data_str .= "<td>".$v['new_order_rate']."</td>";
- $data_str .= "<td>".$v['fugou_rate']."</td>";
- $data_str .= "<td>".$v['order_rate']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 成单率梯形图
- */
- public function orderRateList(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $self_role = session('role_name');
- $team_id = $request->input('team_id');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- if($team_id>0){
- $result = $this->orderTeamRateList($stime, $etime, $team_id);
- return view('statistics/orderRateList', $result);
- }
- $_day = 30;
- if($stime == '' && $etime == ''){
- $stime = date("Y-m-d", strtotime('-32 day'));
- $etime = date("Y-m-d", strtotime('-2 day'));
- }else{
- if($stime){
- $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
- }
- }
- #统计天数
- $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
- $day_count = DB::table('cust_day_remain')->where('month_time', $month)->where(function($query) use($stime, $etime){
- if($stime) $query->where('idate','>=', $stime);
- if($etime) $query->where('idate','<=', $etime);
- })->count();
- $result = DB::table('cust_day_remain')->where(function($query) use($stime, $etime){
- if($stime) $query->where('idate','>=', $stime);
- if($etime) $query->where('idate','<=', $etime);
- })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
- if($stime)
- #获取列
- $columns = array();
- $i_max = 31;
- for($i=0; $i<$i_max; $i++){
- $columns[] = $i;
- }
- if($_day >= 45){
- $columns[] = 45;
- }
- if($_day >= 60){
- $columns[] = 60;
- }
- if($_day >= 75){
- $columns[] = 75;
- }
- if($_day >= 90){
- $columns[] = 90;
- }
- #数据整合
- $data = array();
- foreach($result as $k=>$v){
- $key = $v->idate;
- $rate_data = json_decode($v->order_rate_data, true);
- $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
- }
- #数据格式化
- $new_data = array();
- foreach($data as $k=>$v){
- //当日加粉数
- $fan_add = DB::table('cust_day_detail')->where('dtime', $k)->where('is_del',0)->sum('fan_add');
- $new_data[$k]['fan_add'] = $fan_add;
- foreach($columns as $val){
- $n = $val;
- $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
- }
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- //echo '<pre>';
- //print_r($new_data);print_r($columns);exit;
- return view('statistics/orderRateList', ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList]);
- }
- /**
- * 团队成单率梯形图
- */
- public function orderTeamRateList($stime, $etime, $team_id){
- $_day = 30;
- if($stime == '' && $etime == ''){
- $stime = date("Y-m-d", strtotime('-32 day'));
- $etime = date("Y-m-d", strtotime('-2 day'));
- }else{
- if($stime){
- $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
- }
- }
- #统计天数
- $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
- $day_count = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('month_time', $month)->where(function($query) use($stime, $etime){
- if($stime) $query->where('idate','>=', $stime);
- if($etime) $query->where('idate','<=', $etime);
- })->count();
- $result = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where(function($query) use($stime, $etime){
- if($stime) $query->where('idate','>=', $stime);
- if($etime) $query->where('idate','<=', $etime);
- })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
- if($stime)
- #获取列
- $columns = array();
- $i_max = 31;
- for($i=0; $i<$i_max; $i++){
- $columns[] = $i;
- }
- if($_day >= 45){
- $columns[] = 45;
- }
- if($_day >= 60){
- $columns[] = 60;
- }
- if($_day >= 75){
- $columns[] = 75;
- }
- if($_day >= 90){
- $columns[] = 90;
- }
- #数据整合
- $data = array();
- foreach($result as $k=>$v){
- $key = $v->idate;
- $rate_data = json_decode($v->order_rate_data, true);
- $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
- }
- #团队成员:
- $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- #数据格式化
- $new_data = array();
- foreach($data as $k=>$v){
- //当日加粉数
- $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('dtime', $k)->where('is_del',0)->sum('fan_add');
- $new_data[$k]['fan_add'] = $fan_add;
- foreach($columns as $val){
- $n = $val;
- $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
- }
- }
- //echo '<pre>';
- //print_r($new_data);print_r($columns);exit;
- $self_role = session('role_name');
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList];
- }
- /**
- * orderSaleRank 实时销售数据展榜
- */
- public function orderSaleRank(Request $request){
- header('Location:https://datav.aliyuncs.com/share/db4691abb6c03ebe10f6a97ac36544cd');
- exit;
- }
- /**
- * orderDistrict 实时订单地域分布
- */
- public function orderDistrict(Request $request){
- header('Location:https://datav.aliyuncs.com/share/423b408234ea48d49a2a4ee7f33fa4e4');
- exit;
- }
- /**
- * 地区roi
- */
- public function districtRoi(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $ret = AdCost::select('id')->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('ad_time')->groupBy('city')->get();
- $ret = json_decode(json_encode($ret), true);
- $count = count($ret);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city, ad_time'))->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉收入
- //当日加粉
- $city_name = str_replace('市', '', $v['city']);
- $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['ad_time'])->where('createTime','<=', $v['ad_time'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.当日粉丝总成交额
- $v['order_amount'] = $order->order_amount;
- // 总单数
- $v['order_count'] = $order->order_count;
- //新粉roi
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_fan_count'] = count($phones);
- }
- //总计
- $total_data = [
- 'cost' => 0,
- 'fan_count' => 0,
- 'order_count' => 0,
- 'order_amount' => 0,
- 'roi' => 0,
- 'conversion_times' => 0,
- 'new_order_count' => 0,
- 'new_order_amount' => 0,
- 'new_roi' => 0,
- ];
- //投入
- $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->first();
- $total_data['cost'] = $adcost->total_cost;
- $total_data['conversion_times'] = $adcost->conversion_times;
- //总新粉
- $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('fanTime', '>=', $stime);
- if($etime) $query->where('fanTime', '<=', $etime);
- if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
- })->lists('phone');
- $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
- //总收入
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
- $total_data['order_count'] = $order->order_count;
- $total_data['order_amount'] = $order->order_amount;
- $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
- #总新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
- $total_data['new_order_count'] = $new_order->order_count;
- $total_data['new_order_amount'] = $new_order->order_amount;
- $total_data['new_roi'] = $total_data['cost']>0 ? round($total_data['new_order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
- return view('statistics/districtRoi', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'city' =>$city,
- 'total_data' =>$total_data,
- ]);
- }
- /**
- * 导出地域roi报表
- */
- public function districtRoi_export(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city, ad_time'))->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉收入
- //当日加粉
- $city_name = str_replace('市', '', $v['city']);
- $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['ad_time'])->where('createTime','<=', $v['ad_time'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.当日粉丝总成交额
- $v['order_amount'] = $order->order_amount;
- // 总单数
- $v['order_count'] = $order->order_count;
- //新粉roi
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_fan_count'] = count($phones);
- }
- //总计
- $total_data = [
- 'cost' => 0,
- 'fan_count' => 0,
- 'order_count' => 0,
- 'order_amount' => 0,
- 'roi' => 0,
- 'conversion_times' => 0,
- 'new_order_count' => 0,
- 'new_order_amount' => 0,
- 'new_roi' => 0,
- ];
- //投入
- $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->first();
- $total_data['cost'] = $adcost->total_cost;
- $total_data['conversion_times'] = $adcost->conversion_times;
- //总新粉
- $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('fanTime', '>=', $stime);
- if($etime) $query->where('fanTime', '<=', $etime);
- if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
- })->lists('phone');
- $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
- //总收入
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
- $total_data['order_count'] = $order->order_count;
- $total_data['order_amount'] = $order->order_amount;
- $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
- #总新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
- $total_data['new_order_count'] = $new_order->order_count;
- $total_data['new_order_amount'] = $new_order->order_amount;
- $total_data['new_roi'] = $total_data['cost']>0 ? round($total_data['new_order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
- $indexKey = ['ad_time','city','total_cost','conversion_times','new_fan_count','new_order_count','new_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'district_roi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 团队订单财务明细
- */
- public function teamFinanceList(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = Order::where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time')->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', $team_id);
- if($stime ) $query->where('createTime', '>=', $stime);
- if($etime ) $query->where('createTime', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->orderBy('team_id', 'asc');
- $result = $resultQuery->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
-
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- $v['true_amount'] = $v['receivedAmount'] + $v['refund_price'] - $v['aftersale_fee'];
- $v['payment_type'] = $payArr[$v['payment_type']];
- $v['ctime'] = $v['createTime'];
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('/statistics/teamFinanceList',[
- 'result' => $result,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'teamlist' => $teamList,
- ]);
- }
- /**
- * 团队订单财务汇总列表
- */
- public function teamFinanceTotal(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $_data = $this->teamCtimePage($stime, $etime, $team_id, $offset, $pageSize);
- $count = $_data['count'];
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teams = DB::table('teams')->lists('name', 'id');
- foreach($_data['data'] as $k=>&$v){
- $order_result = Order::select(DB::raw('team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(if(payment_type=1,receivedAmount,null)) as wx_money, sum(if(payment_type=2,receivedAmount,null)) as zfm_money, sum(if(payment_type=3,receivedAmount,null)) as gth_money, sum(if(payment_type=4,receivedAmount,null)) as vip_money'))->where('is_del', 0)->where('createTime','>=', $v['date'])->where('createTime','<=', $v['date'].' 23:59:59')->where('team_id', $v['team_id'])->first();
- $v['order_count'] = isset($order_result->order_count) ? $order_result->order_count : '';
- $v['receivedAmount'] = isset($order_result->receivedAmount) ? $order_result->receivedAmount : '';
- $v['wx_money'] = isset($order_result->wx_money) ? $order_result->wx_money : '';
-
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
- #当天售后和退补
- $ctime = $v['ctime'] = $v['date'];
- $team = $v['team_id'];
- $aftersale_info = Order::select(DB::raw('count(1) as aftersale_count, sum(aftersale_fee) as aftersale_fee'))->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('aftersale_time', '>=', $ctime);
- $query->where('aftersale_time', '<=', $ctime.' 23:59:59');
- })->where('is_del', 0)->where('aftersale_fee', '>', 0)->first();
- $v['aftersale_count'] = $aftersale_info->aftersale_count;
- $v['aftersale_fee'] = $aftersale_info->aftersale_fee;
- #退补
- $refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['refund_count'] = $refund_info->refund_count;
- $v['refund_price'] = $refund_info->refund_price;
- //wx退补
- $wx_refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where('refund_type', 1)->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['wx_refund_count'] = $wx_refund_info->refund_count;
- $v['wx_refund_price'] = $wx_refund_info->refund_price;
- //付款码退补
- $zfm_refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where('refund_type', 2)->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['zfm_refund_count'] = $zfm_refund_info->refund_count;
- $v['zfm_refund_price'] = $zfm_refund_info->refund_price;
- //个体户退补
- $gth_refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where('refund_type', 3)->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['gth_refund_count'] = $gth_refund_info->refund_count;
- $v['gth_refund_price'] = $gth_refund_info->refund_price;
- //充值卡退补
- $czk_refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where('refund_type', 4)->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['czk_refund_count'] = $czk_refund_info->refund_count;
- $v['czk_refund_price'] = $czk_refund_info->refund_price;
- #发货
- $send_info = Order::select(DB::raw('count(1) as send_count, sum(cost) as cost, sum(freight_cost) as freight_cost'))->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('send_time', '=', $ctime);
- })->where('is_del', 0)->where('logistics_id', '>', '')->first();
- $v['send_count'] = $send_info->send_count;
- $v['cost'] = $send_info->cost;
- $v['freight_cost'] = $send_info->freight_cost;
- $v['zfm_money'] = isset($order_result->zfm_money) ? $order_result->zfm_money : '';
- $v['gth_money'] = isset($order_result->gth_money) ? $order_result->gth_money : '';
- $v['vip_money'] = isset($order_result->vip_money) ? $order_result->vip_money : '';
- $v['wx_true_amount'] = $v['wx_money'] + $v['wx_refund_price'] - $v['aftersale_fee'];
- $v['zfm_true_amount'] = $v['zfm_money'] + $v['zfm_refund_price'];
- $v['gth_true_amount'] = $v['gth_money'] + $v['gth_refund_price'];
- $v['czk_true_amount'] = $v['vip_money'] + $v['czk_refund_price'];
- #毛利
- $v['profit'] = $v['wx_true_amount'] + $v['zfm_true_amount'] + $v['gth_true_amount'] + $v['czk_true_amount'] - $v['cost'] - $v['freight_cost'];
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('/statistics/teamFinanceTotal',[
- 'result' => $_data['data'],
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'teamlist' => $teamList,
- ]);
- }
- /**
- * 团队财务明细导出
- */
- public function teamFinanceList_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
-
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time')->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', $team_id);
- if($stime ) $query->where('createTime', '>=', $stime);
- if($etime ) $query->where('createTime', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->orderBy('team_id', 'asc');
- $result = $resultQuery->orderBy('createTime','desc')->get();
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- //$v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
- $v['payment_type'] = $payArr[$v['payment_type']];
- }
- $indexKey = ['team_name','createTime','id','receivedAmount','cost','freight_cost', 'payment_type'];
- $title = ['团队', '时间', '订单编号', '成交金额','供应商成本', '物流成本', '支付方式'];
- $filename = 'dingdanmingxi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 团队财务汇总导出
- */
- public function teamFinanceTotal_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
-
- $teams = DB::table('teams')->lists('name', 'id');
- $result = $this->teamCtimePage_export($stime, $etime, $team_id);
- foreach($result as $k=>&$v){
- $order_result = Order::select(DB::raw('team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(if(payment_type=1,receivedAmount,null)) as wx_money, sum(if(payment_type=2,receivedAmount,null)) as zfm_money, sum(if(payment_type=3,receivedAmount,null)) as gth_money, sum(if(payment_type=4,receivedAmount,null)) as vip_money'))->where('is_del', 0)->where('createTime','>=', $v['date'])->where('createTime','<=', $v['date'].' 23:59:59')->where('team_id', $v['team_id'])->first();
- $v['order_count'] = isset($order_result->order_count) ? $order_result->order_count : '';
- $v['receivedAmount'] = isset($order_result->receivedAmount) ? $order_result->receivedAmount : '';
- $v['wx_money'] = isset($order_result->wx_money) ? $order_result->wx_money : '';
-
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
- #当天售后和退补
- $ctime = $v['ctime'] = $v['date'];
- $team = $v['team_id'];
- $aftersale_info = Order::select(DB::raw('count(1) as aftersale_count, sum(aftersale_fee) as aftersale_fee'))->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('aftersale_time', '>=', $ctime);
- $query->where('aftersale_time', '<=', $ctime.' 23:59:59');
- })->where('is_del', 0)->where('aftersale_fee', '>', 0)->first();
- $v['aftersale_count'] = $aftersale_info->aftersale_count;
- $v['aftersale_fee'] = $aftersale_info->aftersale_fee;
- #退补
- $refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['refund_count'] = $refund_info->refund_count;
- $v['refund_price'] = $refund_info->refund_price;
- //wx退补
- $wx_refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where('refund_type', 1)->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['wx_refund_count'] = $wx_refund_info->refund_count;
- $v['wx_refund_price'] = $wx_refund_info->refund_price;
- //付款码退补
- $zfm_refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where('refund_type', 2)->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['zfm_refund_count'] = $zfm_refund_info->refund_count;
- $v['zfm_refund_price'] = $zfm_refund_info->refund_price;
- //个体户退补
- $gth_refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where('refund_type', 3)->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['gth_refund_count'] = $gth_refund_info->refund_count;
- $v['gth_refund_price'] = $gth_refund_info->refund_price;
- //充值卡退补
- $czk_refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where('refund_type', 4)->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('refund_time', '=', $ctime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
- $v['czk_refund_count'] = $czk_refund_info->refund_count;
- $v['czk_refund_price'] = $czk_refund_info->refund_price;
- #发货
- $send_info = Order::select(DB::raw('count(1) as send_count, sum(cost) as cost, sum(freight_cost) as freight_cost'))->where(function($query) use($team, $ctime){
- $query->where('team_id', $team);
- $query->where('send_time', '=', $ctime);
- })->where('is_del', 0)->where('logistics_id', '>', '')->first();
- $v['send_count'] = $send_info->send_count;
- $v['cost'] = $send_info->cost;
- $v['freight_cost'] = $send_info->freight_cost;
- $v['zfm_money'] = isset($order_result->zfm_money) ? $order_result->zfm_money : '';
- $v['gth_money'] = isset($order_result->gth_money) ? $order_result->gth_money : '';
- $v['vip_money'] = isset($order_result->vip_money) ? $order_result->vip_money : '';
- $v['wx_true_amount'] = $v['wx_money'] + $v['wx_refund_price'] - $v['aftersale_fee'];
- $v['zfm_true_amount'] = $v['zfm_money'] + $v['zfm_refund_price'];
- $v['gth_true_amount'] = $v['gth_money'] + $v['gth_refund_price'];
- $v['czk_true_amount'] = $v['vip_money'] + $v['czk_refund_price'];
- #毛利
- $v['profit'] = $v['wx_true_amount'] + $v['zfm_true_amount'] + $v['gth_true_amount'] + $v['czk_true_amount'] - $v['cost'] - $v['freight_cost'];
- }
- $indexKey = ['team_name','ctime','order_count','receivedAmount','wx_money','zfm_money','gth_money', 'vip_money', 'aftersale_count','aftersale_fee','wx_refund_count','wx_refund_price','wx_true_amount','zfm_refund_count','zfm_refund_price','zfm_true_amount','gth_refund_count','gth_refund_price','gth_true_amount','czk_refund_count','czk_refund_price','czk_true_amount', 'send_count', 'cost', 'freight_cost', 'profit'];
- $title = ['团队', '时间', '总单数', '成交金额', '微信支付金额','付款码支付金额', '个体户支付', '充值卡支付金额', '售后单数', '售后金额', '微信退补单数', '微信退补差价', '微信实际金额', '付款码退补单数', '付款码退补差价', '付款码实际金额', '个体户退补单数', '个体户退补差价', '个体户实际金额', '充值卡退补单数', '充值卡退补差价', '充值卡实际金额', '发货单数', '货品成本', '物流成本', '毛利'];
-
- $filename = 'dingdanhuizong_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 售后财务明细
- */
- public function aftersaleFinanceList(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $warehouse = (int)$request->input('warehouse');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = Order::where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime) $query->where('aftersale_time', '>=', $stime);
- if($etime) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->where('aftersale_fee', '>', 0)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time', 'warehouse')->where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime ) $query->where('aftersale_time', '>=', $stime);
- if($etime ) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->where('aftersale_fee', '>', 0);
- $result = $resultQuery->orderBy('aftersale_time','desc')->offset($offset)->limit($pageSize)->get();
-
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- $wareArr = $this->warehouse;
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- //$v['true_amount'] = $v['receivedAmount'] + $v['refund_price'] - $v['aftersale_fee'];
- $v['payment_type'] = $payArr[$v['payment_type']];
- $v['ctime'] = $v['aftersale_time'];
- $v['warehouse'] = $wareArr[$v['warehouse']];
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('/statistics/aftersaleFinanceList',[
- 'result' => $result,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'teamlist' => $teamList,
- 'warehouse' => $warehouse,
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /**
- * 退补财务明细
- */
- public function refundFinanceList(Request $request){
- $team_id = (int)$request->input('team_id');
- $warehouse = (int)$request->input('warehouse');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = Order::where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime) $query->where('refund_time', '>=', $stime);
- if($etime) $query->where('refund_time', '<=', $etime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'refund_price', 'payment_type', 'refund_time', 'refund_note', 'refund_type', 'warehouse')->where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime ) $query->where('refund_time', '>=', $stime);
- if($etime ) $query->where('refund_time', '<=', $etime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1);
- $result = $resultQuery->orderBy('refund_time','desc')->offset($offset)->limit($pageSize)->get();
-
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- $wareArr = $this->warehouse;
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- $v['payment_type'] = $payArr[$v['payment_type']];
- $v['ctime'] = $v['refund_time'];
- //退补支付方式
- if($v['refund_type'] == 1){
- $v['refund_type'] = '微信支付';
- }elseif($v['refund_type'] == 2){
- $v['refund_type'] = '付款码支付';
- }elseif($v['refund_type'] == 3){
- $v['refund_type'] = '个体户支付';
- }elseif($v['refund_type'] == 4){
- $v['refund_type'] = '充值卡支付';
- }else{
- $v['refund_type'] = '';
- }
- $v['warehouse'] = $wareArr[$v['warehouse']];
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('/statistics/refundFinanceList',[
- 'result' => $result,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'teamlist' => $teamList,
- 'warehouse' => $warehouse,
- 'warehouseArr' => $this->warehouse
- ]);
- }
- /**
- * 发货财务明细
- */
- public function sendFinanceList(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $warehouse = (int)$request->input('warehouse');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = Order::where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime) $query->where('send_time', '>=', $stime);
- if($etime) $query->where('send_time', '<=', $etime);
- })->where('is_del', 0)->where('logistics_id', '>', '')->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'cost', 'freight_cost', 'payment_type', 'send_time', 'warehouse', 'receiverName', 'logistics_id')->where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime ) $query->where('send_time', '>=', $stime);
- if($etime ) $query->where('send_time', '<=', $etime);
- })->where('is_del', 0)->where('logistics_id', '>', '');
- $result = $resultQuery->orderBy('send_time','desc')->offset($offset)->limit($pageSize)->get();
-
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- $wareArr = $this->warehouse;
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- $v['payment_type'] = $payArr[$v['payment_type']];
- $v['ctime'] = $v['send_time'];
- $v['warehouse'] = $wareArr[$v['warehouse']];
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('/statistics/sendFinanceList',[
- 'result' => $result,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'teamlist' => $teamList,
- 'warehouse' => $warehouse,
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /**
- * 售后财务明细
- */
- public function aftersaleFinanceList_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $warehouse = (int)$request->input('warehouse');
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time', 'warehouse')->where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime ) $query->where('aftersale_time', '>=', $stime);
- if($etime ) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->where('aftersale_fee', '>', 0);
- $result = $resultQuery->orderBy('aftersale_time','desc')->get();
-
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- $wareArr = $this->warehouse;
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- //$v['true_amount'] = $v['receivedAmount'] + $v['refund_price'] - $v['aftersale_fee'];
- $v['payment_type'] = $payArr[$v['payment_type']];
- $v['ctime'] = $v['aftersale_time'];
- $v['warehouse'] = $wareArr[$v['warehouse']];
- }
- $indexKey = ['team_name','ctime','id','receivedAmount','aftersale_fee', 'payment_type', 'warehouse'];
- $title = ['团队', '售后时间', '订单编号', '成交金额','售后金额', '支付方式', '发货仓库'];
- $filename = 'shouhoumingxi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 退补财务明细
- */
- public function refundFinanceList_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $warehouse = (int)$request->input('warehouse');
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'refund_price', 'payment_type', 'refund_time', 'refund_note', 'refund_type', 'warehouse')->where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime ) $query->where('refund_time', '>=', $stime);
- if($etime ) $query->where('refund_time', '<=', $etime);
- })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1);
- $result = $resultQuery->orderBy('refund_time','desc')->get();
-
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- $wareArr = $this->warehouse;
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- $v['payment_type'] = $payArr[$v['payment_type']];
- $v['ctime'] = $v['refund_time'];
- //退补支付方式
- if($v['refund_type'] == 1){
- $v['refund_type'] = '微信支付';
- }elseif($v['refund_type'] == 2){
- $v['refund_type'] = '付款码支付';
- }elseif($v['refund_type'] == 3){
- $v['refund_type'] = '个体户支付';
- }elseif($v['refund_type'] == 4){
- $v['refund_type'] = '充值卡支付';
- }else{
- $v['refund_type'] = '';
- }
- $v['warehouse'] = $wareArr[$v['warehouse']];
- }
- $indexKey = ['team_name','ctime','id','receivedAmount', 'payment_type','refund_price', 'refund_type', 'warehouse'];
- $title = ['团队', '退补时间', '订单编号', '成交金额', '支付方式', '退补差价', '退补支付方式', '发货仓库'];
- $filename = 'tuibumingxi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 发货财务明细
- */
- public function sendFinanceList_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $warehouse = (int)$request->input('warehouse');
-
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'cost', 'freight_cost', 'payment_type', 'send_time', 'warehouse', 'receiverName', 'logistics_id')->where(function($query) use($team_id, $stime, $etime, $warehouse){
- if($team_id) $query->where('team_id', $team_id);
- if($warehouse) $query->where('warehouse', $warehouse);
- if($stime ) $query->where('send_time', '>=', $stime);
- if($etime ) $query->where('send_time', '<=', $etime);
- })->where('is_del', 0)->where('logistics_id', '>', '');
- $result = $resultQuery->orderBy('send_time','desc')->get();
-
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- $wareArr = $this->warehouse;
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- $v['payment_type'] = $payArr[$v['payment_type']];
- $v['ctime'] = $v['send_time'];
- $v['warehouse'] = $wareArr[$v['warehouse']];
- }
- $indexKey = ['team_name','ctime','id','receivedAmount', 'cost', 'freight_cost', 'payment_type', 'warehouse', 'receiverName', 'logistics_id'];
- $title = ['团队', '发货时间', '订单编号', '成交金额','货品成本', '物流成本', '支付方式', '发货仓库', '收货人', '物流单号'];
- $filename = 'fahuomingxi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 团队订单财务汇总列表
- */
- public function teamFinanceTotal2(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $aftersale = (int)$request->input('aftersale');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- if($aftersale == 1){
- $countQuery = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id'));
- }else{
- $countQuery = Order::select(DB::raw('left(createTime, 10) as ctime, team_id'));
- }
- $count = $countQuery->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
- if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
- if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
- if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teams = DB::table('teams')->lists('name', 'id');
- if($aftersale == 1){
- $result = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('aftersale_time', '>=', $stime);
- if($etime) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('aftersale_time','desc')->offset($offset)->limit($pageSize)->get();
- }else{
- $result = Order::select(DB::raw('left(createTime, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
- }
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
- $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
- $v['zfm_money'] = $v['receivedAmount'] - $v['wx_money'];
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('/statistics/teamFinanceTotal',[
- 'result' => $result,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'teamlist' => $teamList,
- 'aftersale' => $aftersale,
- ]);
- }
- /**
- * 团队财务明细导出
- */
- public function teamFinanceList_export2(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $aftersale = $request->input('aftersale');
-
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time')->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
- if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
- if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
- if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->orderBy('team_id', 'asc');
- if($aftersale == 1){
- $result = $resultQuery->orderBy('aftersale_time','desc')->get();
- }else{
- $result = $resultQuery->orderBy('createTime','desc')->get();
- }
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
- $v['payment_type'] = $payArr[$v['payment_type']];
- }
- if($aftersale == 0) $indexKey = ['team_name','createTime','id','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost', 'payment_type'];
- if($aftersale == 1) $indexKey = ['team_name','aftersale_time','id','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost', 'payment_type'];
- $title = ['团队', '时间', '订单编号', '成交金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本', '支付方式'];
- $filename = $aftersale==0 ? 'dingdanmingxi'.date('Y-m-d_H').'.xlsx' : 'shouhoumingxi'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 团队财务汇总导出
- */
- public function teamFinanceTotal_export2(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $aftersale = $request->input('aftersale');
-
- $teams = DB::table('teams')->lists('name', 'id');
- if($aftersale == 1){
- $result = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('aftersale_time', '>=', $stime);
- if($etime) $query->where('aftersale_time', '<=', $etime.' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('aftersale_time','desc')->get();
- }else{
- $result = Order::select(DB::raw('left(createTime, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->get();
- }
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
- $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
- $v['zfm_money'] = $v['receivedAmount'] - $v['wx_money'];
- }
- if($aftersale==0){
- $indexKey = ['team_name','ctime','order_count','receivedAmount','wx_money','zfm_money','aftersale_fee','refund_price','true_amount','cost','freight_cost'];
- $title = ['团队', '时间', '总单数', '成交金额', '微信支付金额','付款码支付金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本'];
- }else{
- $indexKey = ['team_name','ctime','order_count','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost'];
- $title = ['团队', '时间', '总单数', '成交金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本'];
- }
- $filename = $aftersale==0 ? 'dingdanhuizong_'.date('Y-m-d_H').'.xlsx' : 'shouhouhuizong_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地区汇总roi
- */
- public function districtTotalRoi(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $ret = AdCost::select('id')->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->get();
- $ret = json_decode(json_encode($ret), true);
- $count = count($ret);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉收入
- //加粉
- $city_name = str_replace('市', '', $v['city']);
- $phones = DB::table('customers')->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
- #订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
-
- #汇总新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
- // 1.新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.粉丝总成交额
- $v['order_amount'] = $order->order_amount;
- // 总单数
- $v['order_count'] = $order->order_count;
- //新粉roi
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_fan_count'] = count($phones);
- }
- return view('statistics/districtTotalRoi', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- ]);
- }
- /**
- * 地域roi7日
- */
- public function districtRoi7day(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = DistrictRoi7::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = DistrictRoi7::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- return view('statistics/districtRoi7day', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 地域roi7日
- */
- public function districtRoi7day_export(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = DistrictRoi7::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- $indexKey = ['ad_time','city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '7日累计成单数', '7日累计收入', '7日累计ROI'];
- $filename = 'districtRoi7day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi7日
- */
- public function districtRoi7dayTotal(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = DistrictRoi7::where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = DistrictRoi7::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- return view('statistics/districtRoi7dayTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- ]);
- }
- /**
- * 地域roi7日
- */
- public function districtRoi7dayTotal_export(Request $request){
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = DistrictRoi7::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount,sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '7日累计成单数', '7日累计收入', '7日累计ROI'];
- $filename = 'districtRoi7day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi30日
- */
- public function districtRoi30day(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = districtRoi30::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = districtRoi30::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- return view('statistics/districtRoi30day', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 地域roi30日
- */
- public function districtRoi30day_export(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = districtRoi30::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- $indexKey = ['ad_time','city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'districtRoi30day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi30日汇总
- */
- public function districtRoi30dayTotal(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = districtRoi30::where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = districtRoi30::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- return view('statistics/districtRoi30dayTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- ]);
- }
- /**
- * 地域roi30日
- */
- public function districtRoi30dayTotal_export(Request $request){
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = districtRoi30::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount,sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'districtRoi30day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi15日
- */
- public function districtRoi15day(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = districtRoi15::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = districtRoi15::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- return view('statistics/districtRoi15day', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 地域roi15日
- */
- public function districtRoi15day_export(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = districtRoi15::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- $indexKey = ['ad_time','city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'districtRoi15day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi15日汇总
- */
- public function districtRoi15dayTotal(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = districtRoi15::where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = districtRoi15::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- return view('statistics/districtRoi15dayTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- ]);
- }
- /**
- * 地域roi15日
- */
- public function districtRoi15dayTotal_export(Request $request){
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = districtRoi15::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount,sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'districtRoi15day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi45日
- */
- public function districtRoi45day(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = districtRoi45::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = districtRoi45::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- return view('statistics/districtRoi45day', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 地域roi45日
- */
- public function districtRoi45day_export(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = districtRoi45::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- $indexKey = ['ad_time','city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'districtRoi45day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi45日汇总
- */
- public function districtRoi45dayTotal(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = districtRoi45::where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = districtRoi45::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- return view('statistics/districtRoi45dayTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- ]);
- }
- /**
- * 地域roi45日
- */
- public function districtRoi45dayTotal_export(Request $request){
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = districtRoi45::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount,sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'districtRoi45day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 销售月度业绩报表
- */
- public function salerBonus(Request $request){
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $mstime = $request->input('stime');
- $metime = $request->input('etime');
- if($mstime){
- $stime = date('Y-m-01', strtotime($mstime));
- }else{
- $stime = null;
- }
- if($metime){
- $etime = date('Y-m-01', strtotime($metime. ' 1 month'));
- }else{
- $etime = null;
- }
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0 && !$admin_id){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $ret = Order::select(DB::raw('left(createTime, 7) as month, admin_id'))->where('is_del', 0)->where('status', '>=', 2)->where(function($query) use($admin_id, $sale_ids, $stime, $etime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($stime)) $query->where('createTime', '>=', $stime);
- if(!empty($etime)) $query->where('createTime', '<', $etime);
- })->groupBy('month')->groupBy('admin_id')->get();
- $count = count($ret);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Order::select(DB::raw('left(createTime, 7) as month, admin_id, count(1) as order_count, sum(receivedAmount) as order_amount'))->where('is_del', 0)->where('status', '>=', 2)->where(function($query) use($admin_id, $sale_ids, $stime, $etime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($stime)) $query->where('createTime', '>=', $stime);
- if(!empty($etime)) $query->where('createTime', '<', $etime);
- })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $m_stime = date('Y-m-01', strtotime($v['month']));
- $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
- //加粉数
- $fan_count = DB::table('cust_day_detail')->select(DB::raw('sum(fan_add) as fan_count, admin_name'))->where('dtime', '>=', $m_stime)->where('dtime', '<', $m_etime)->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- $v['fan_count'] = $fan_count->fan_count;
- $v['admin_name'] = $fan_count->admin_name;
- if(empty($v['admin_name'])){
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
- }
- #计算提成
- $v['bonus'] = $this->getSalerBonus($v['order_amount']);
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/salerBonus', ['result'=>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'stime' =>$mstime,
- 'etime' =>$metime,
- 'self_role' =>$self_role,
- ]);
- }
- /**
- * 计算提成
- */
- public function getSalerBonus($amount){
- $result = [
- 0 => 0, //总的
- 1 => 0,
- 2 => 0,
- 3 => 0
- ];
- if($amount>150000){
- $result[1] = round(100000 * 0.03);
- $result[2] = round(50000 * 0.04);
- $result[3] = round( ($amount - 150000) * 0.05 , 2);
- $result[0] = $result[1] + $result[2] + $result[3];
- }elseif($amount>100000){
- $result[1] = round(100000 * 0.03);
- $result[2] = round(($amount - 100000) * 0.04, 2);
- $result[0] = $result[1] + $result[2];
- }else{
- $result[1] = round($amount * 0.03, 2);
- $result[0] = $result[1];
- }
- return $result;
- }
- /**
- * 销售月度业绩报表
- */
- public function salerBonus_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- if($stime){
- $stime = date('Y-m-01', strtotime($stime));
- }
- if($etime){
- $etime = date('Y-m-01', strtotime($etime. ' 1 month'));
- }
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0 && !$admin_id){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = Order::select(DB::raw('left(createTime, 7) as month, admin_id, count(1) as order_count, sum(receivedAmount) as order_amount'))->where('is_del', 0)->where('status', '>=', 2)->where(function($query) use($admin_id, $sale_ids, $stime, $etime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($stime)) $query->where('createTime', '>=', $stime);
- if(!empty($etime)) $query->where('createTime', '<', $etime);
- })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $m_stime = date('Y-m-01', strtotime($v['month']));
- $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
- //加粉数
- $fan_count = DB::table('cust_day_detail')->select(DB::raw('sum(fan_add) as fan_count, admin_name'))->where('dtime', '>=', $m_stime)->where('dtime', '<', $m_etime)->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- $v['fan_count'] = $fan_count->fan_count;
- $v['admin_name'] = $fan_count->admin_name;
- if(empty($v['admin_name'])){
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
- }
- #计算提成
- $bonus = $this->getSalerBonus($v['order_amount']);
- $v['bonus0'] = $bonus[0];
- $v['bonus1'] = $bonus[1];
- $v['bonus2'] = $bonus[2];
- $v['bonus3'] = $bonus[3];
- }
- $indexKey = ['month','admin_name','order_count','order_amount','fan_count','bonus0','bonus1','bonus2','bonus3'];
- $title = ['月份', '销售名', '总单量', '总成交额', '总加粉数', '总提成', '一阶提成', '二阶提成', '三阶提成'];
- $filename = 'xiaoshouyj_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 团队财务汇总时间格式处理
- */
- public function teamCtimePage($stime, $etime, $team_id, $offset, $pageSize, $sdate=''){
- $result = array();
- if( empty($sdate) ) $sdate = '2019-09-04';
- $edate = date('Y-m-d');
- if($stime && $stime>$sdate){
- $sdate = $stime;
- }
- if($etime && $etime<$edate){
- $edate = $etime;
- }
- $teams = Admin::getTeams();
- if($team_id>0){
- $teams = [$team_id];
- }
- $d = (int)(strtotime($edate) - strtotime($sdate)) / 86400 + 1;
- for($i=$d-1; $i>=0; $i--){
- $dtime = $sdate. '+'.$i.' day';
- $date = date('Y-m-d', strtotime($dtime));
- foreach($teams as $team_id){
- $result[] = ['date'=>$date, 'team_id'=>$team_id];
- }
- }
- $count = count($result);
- $ret['data'] = array_slice($result, $offset, $pageSize);
- $ret['count'] = $count;
- return $ret;
- }
- /**
- * 团队财务汇总时间格式处理
- */
- public function teamCtimePage_export($stime, $etime, $team_id, $sdate=''){
- $result = array();
- if( empty($sdate) ) $sdate = '2019-09-04';
- $edate = date('Y-m-d');
- if($stime && $stime>$sdate){
- $sdate = $stime;
- }
- if($etime && $etime<$edate){
- $edate = $etime;
- }
- $teams = Admin::getTeams();
- if($team_id>0){
- $teams = [$team_id];
- }
- $d = (int)(strtotime($edate) - strtotime($sdate)) / 86400 + 1;
- for($i=$d-1; $i>=0; $i--){
- $dtime = $sdate. '+'.$i.' day';
- $date = date('Y-m-d', strtotime($dtime));
- foreach($teams as $team_id){
- $result[] = ['date'=>$date, 'team_id'=>$team_id];
- }
- }
- return $result;
- }
- /**
- * 投放数据时间格式 分页
- */
- public function throwCtimePage($stime, $etime, $offset='', $pageSize=''){
- $result = array();
- if( empty($sdate) ) $sdate = '2019-07-03';
- $edate = date('Y-m-d', strtotime('-1 day'));
- if($stime && $stime>$sdate){
- $sdate = $stime;
- }
- if($etime && $etime<$edate){
- $edate = $etime;
- }
- $d = (int)(strtotime($edate) - strtotime($sdate)) / 86400 + 1;
- for($i=$d-1; $i>=0; $i--){
- $dtime = $sdate. '+'.$i.' day';
- $date = date('Y-m-d', strtotime($dtime));
- $result[] = ['date'=>$date];
- }
-
- if( $pageSize>0 ){
- $count = count($result);
- $ret['data'] = array_slice($result, $offset, $pageSize);
- $ret['count'] = $count;
- }else{
- $ret = $result;
- }
- return $ret;
- }
- /**
- * 销售月业绩完成度报表
- */
- public function salerTargetProgress(Request $request){
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $mstime = $request->input('stime');
- $metime = $request->input('etime');
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0 && !$admin_id){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = DB::table('saler_targets')->where(function($query) use($admin_id, $sale_ids, $mstime, $metime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($mstime)) $query->where('month', '>=', $mstime);
- if(!empty($metime)) $query->where('month', '<=', $metime);
- })->count();
-
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = DB::table('saler_targets')->where(function($query) use($admin_id, $sale_ids, $mstime, $metime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($mstime)) $query->where('month', '>=', $mstime);
- if(!empty($metime)) $query->where('month', '<=', $metime);
- })->orderBy('month', 'desc')->offset($offset)->limit($pageSize)->get();
- if($mstime){
- $stime = date('Y-m-01', strtotime($mstime));
- }else{
- $stime = null;
- }
- if($metime){
- $etime = date('Y-m-01', strtotime($metime. ' 1 month'));
- }else{
- $etime = null;
- }
- //月份天数数组:
- $m_day = [1=>31,2=>28,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>31];
- if(date('Y') % 4 == 0){
- $m_day[2] = 29;
- }
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $m_stime = date('Y-m-01', strtotime($v['month']));
- $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
- //总单数
- $order = Order::select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('admin_id', $v['admin_id'])->where('is_del', 0)->where('createTime','>=',$m_stime)->where('createTime', '<', $m_etime)->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- //销售额完成率
- $v['target_rate'] = $v['amount']>0 ? round( $order->order_amount / $v['amount'] , 4) : '';
- //时间进度
- //当前月
- $tmonth = date('Y-m');
- $m = (int)date('m');
- $d = (int)date('d');
- $days = $m_day[$m];
- if( $v['month'] == $tmonth ){
- $v['time_rate'] = round($d / $days, 4);
- //剩余每天任务
- if( $v['target_rate'] >= 1){
- $v['day_remain'] = '';
- }else{
- $v['day_remain'] = round( ($v['amount'] - $order->order_amount) / $days, 4 );
- }
- }elseif( $v['month'] < $tmonth ){
- $v['time_rate'] = 1;
- $v['day_remain'] = '';
- }elseif( $v['month'] > $tmonth ){
- $v['time_rate'] = 0;
- $v['day_remain'] = round( $v['amount'] / $days, 4 );
- }
- //粉数数据
- $fan_count = DB::table('cust_day_detail')->select(DB::raw('sum(fan_add) as fan_count, sum(new_reply) as new_reply_count, sum(new_consult) as new_consult_count, admin_name'))->where('dtime', '>=', $m_stime)->where('dtime', '<', $m_etime)->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- $v['fan_count'] = $fan_count->fan_count;
- $v['new_reply_count'] = $fan_count->new_reply_count;
- $v['new_consult_count'] = $fan_count->new_consult_count;
- $v['admin_name'] = $fan_count->admin_name;
- if(empty($v['admin_name'])){
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('username');
- }
- //新粉成单
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('cu.fanTime','>=',$m_stime)->where('cu.fanTime', '<', $m_etime)->where('order.admin_id', $v['admin_id'])->first();
- $v['new_order_count'] = $new_order->order_count;
- $v['new_order_amount'] = $new_order->order_amount;
- //老粉成单
- $v['old_order_count'] = $order->order_count - $new_order->order_count;
- $v['old_order_amount'] = $order->order_amount - $new_order->order_amount;
- //复购单数
- //总单数
- $fugou_order = Order::select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('admin_id', $v['admin_id'])->where('is_del', 0)->where('createTime','>=',$m_stime)->where('createTime', '<', $m_etime)->where('is_fugou', 1)->first();
- $v['fugou_order_count'] = $fugou_order->order_count;
- $v['fugou_order_amount'] = $fugou_order->order_amount;
- //新粉转化
- $phones = DB::table('customers')->select(DB::raw('distinct(customers.phone)'))->leftJoin('order', 'customers.phone', '=', 'order.receiverMobile')->where('customers.fanTime','>=',$m_stime)->where('customers.fanTime','<',$m_etime)->where('order.admin_id', $v['admin_id'])->get();
- $new_orderfan_count = count($phones);
- $v['new_fan_rate'] = $fan_count->fan_count>0 ? round( $new_orderfan_count / $fan_count->fan_count, 4 ) : '';
- //客单价
- $v['order_amount_avg'] = $v['order_count'] > 0 ? round( $v['order_amount'] / $v['order_count'], 4 ) : '';
- $v['new_orderfan_count'] = $new_orderfan_count;
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/salerTargetProgress', ['result'=>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'stime' =>$mstime,
- 'etime' =>$metime,
- 'self_role' =>$self_role,
- ]);
- }
- /**
- * @return \Illuminate\View\View
- */
- public function salerTargetCreate()
- {
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('statistics/salerTargetCreate',['adminlist'=>$adminList, 'teamlist'=>$teamList]);
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function salerTargetStore(Request $request)
- {
- $admin_id = intval($request->input('admin_id'));
- $this->validate($request, [
- 'admin_id' => 'required|integer|min:1',
- 'month' => 'required|unique:saler_targets,month,null,month,admin_id,'.$admin_id,
- 'amount' => 'required',
- ], [
- 'admin_id.required' => '销售不能为空',
- 'admin_id.min' => '销售不能为空',
- 'month.required' => '日期不能为空',
- 'month.unique' => '已经添加过',
- 'amount.required' => '销售额不能为空',
-
- ]);
- $salerTargets = new SalerTargets();
- $salerTargets->admin_id = intval($request->input('admin_id'));
- $salerTargets->month = trim($request->input('month'));
- $salerTargets->amount = trim($request->input('amount'));
- $salerTargets->save();
- return redirect('/admin/statistics/salerTargetProgress')->with('info', '添加成功');
- }
- /**
- * @param $id
- * @return \Illuminate\View\View
- */
- public function salerTargetEdit($id)
- {
- $salerTargets = salerTargetss::findOrFail($id);
- return view('salerTargets/edit', ['salerTargets' => $salerTargets, 'id'=>$id]);
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function salerTargetUpdate(Request $request)
- {
- $id = (int)$request->input('id');
- $this->validate($request, [
- 'url' => 'required|unique:salerTargetss,url,'.$id.',id',
- ], [
- 'url.required' => '模板链接不能为空',
- 'url.unique' => '该模板链接已存在',
-
- ]);
- $salerTargets = salerTargetss::findOrFail($id);
- $salerTargets->url = trim($request->input('url'));
- $salerTargets->note = trim($request->input('note'));
-
- //图片上传 阿里云oss
- if ($request->hasFile('img') && $request->file('img')->isValid()) {
- $file = $request->file('img');
- $ossClient=new oss();
- // 上传阿里云
- $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
- $img=$file['oss-request-url'];
- $salerTargets->img=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
- }
- $salerTargets->save();
- return redirect('/admin/salerTargets/index')->with('info', '修改模板成功');
- }
- /**
- * 销售月业绩完成度报表导出
- */
- public function salerTargetProgress_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $mstime = $request->input('stime');
- $metime = $request->input('etime');
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0 && !$admin_id){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = DB::table('saler_targets')->where(function($query) use($admin_id, $sale_ids, $mstime, $metime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($mstime)) $query->where('month', '>=', $mstime);
- if(!empty($metime)) $query->where('month', '<=', $metime);
- })->orderBy('month', 'desc')->get();
- if($mstime){
- $stime = date('Y-m-01', strtotime($mstime));
- }else{
- $stime = null;
- }
- if($metime){
- $etime = date('Y-m-01', strtotime($metime. ' 1 month'));
- }else{
- $etime = null;
- }
- //月份天数数组:
- $m_day = [1=>31,2=>28,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>31];
- if(date('Y') % 4 == 0){
- $m_day[2] = 29;
- }
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $m_stime = date('Y-m-01', strtotime($v['month']));
- $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
- //总单数
- $order = Order::select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('admin_id', $v['admin_id'])->where('is_del', 0)->where('createTime','>=',$m_stime)->where('createTime', '<', $m_etime)->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- //销售额完成率
- $v['target_rate'] = $v['amount']>0 ? round( $order->order_amount / $v['amount'] , 4) : '';
- //时间进度
- //当前月
- $tmonth = date('Y-m');
- $m = date('m');
- $d = date('d');
- $days = $m_day[$m];
- if( $v['month'] == $tmonth ){
- $v['time_rate'] = round($d / $days, 4);
- //剩余每天任务
- if( $v['target_rate'] >= 1){
- $v['day_remain'] = '';
- }else{
- $v['day_remain'] = round( ($v['amount'] - $order->order_amount) / $days, 4 );
- }
- }elseif( $v['month'] < $tmonth ){
- $v['time_rate'] = 1;
- $v['day_remain'] = '';
- }elseif( $v['month'] > $tmonth ){
- $v['time_rate'] = 0;
- $v['day_remain'] = round( $v['amount'] / $days, 4 );
- }
- //粉数数据
- $fan_count = DB::table('cust_day_detail')->select(DB::raw('sum(fan_add) as fan_count, sum(new_reply) as new_reply_count, sum(new_consult) as new_consult_count, admin_name'))->where('dtime', '>=', $m_stime)->where('dtime', '<', $m_etime)->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- $v['fan_count'] = $fan_count->fan_count;
- $v['new_reply_count'] = $fan_count->new_reply_count;
- $v['new_consult_count'] = $fan_count->new_consult_count;
- $v['admin_name'] = $fan_count->admin_name;
- if(empty($v['admin_name'])){
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('username');
- }
- //新粉成单
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('cu.fanTime','>=',$m_stime)->where('cu.fanTime', '<', $m_etime)->where('order.admin_id', $v['admin_id'])->first();
- $v['new_order_count'] = $new_order->order_count;
- $v['new_order_amount'] = $new_order->order_amount;
- //老粉成单
- $v['old_order_count'] = $order->order_count - $new_order->order_count;
- $v['old_order_amount'] = $order->order_amount - $new_order->order_amount;
- //复购单数
- //总单数
- $fugou_order = Order::select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('admin_id', $v['admin_id'])->where('is_del', 0)->where('createTime','>=',$m_stime)->where('createTime', '<', $m_etime)->where('is_fugou', 1)->first();
- $v['fugou_order_count'] = $fugou_order->order_count;
- $v['fugou_order_amount'] = $fugou_order->order_amount;
- //新粉转化
- $phones = DB::table('customers')->select(DB::raw('distinct(customers.phone)'))->leftJoin('order', 'customers.phone', '=', 'order.receiverMobile')->where('customers.fanTime','>=',$m_stime)->where('customers.fanTime','<',$m_etime)->where('order.admin_id', $v['admin_id'])->get();
- $new_orderfan_count = count($phones);
- $v['new_fan_rate'] = $fan_count->fan_count>0 ? round( $new_orderfan_count / $fan_count->fan_count, 4 ) : '';
- //客单价
- $v['order_amount_avg'] = $v['order_count'] > 0 ? round( $v['order_amount'] / $v['order_count'], 4 ) : '';
- $v['new_orderfan_count'] = $new_orderfan_count;
- }
- $indexKey = ['admin_name','month','order_count','order_amount','amount','target_rate','time_rate','day_remain', 'fan_count', 'new_reply_count', 'new_consult_count', 'new_order_count', 'new_order_amount', 'old_order_count', 'old_order_amount', 'fugou_order_count', 'fugou_order_amount', 'order_count', 'order_amount', 'new_orderfan_count', 'new_fan_rate', 'order_amount_avg'];
- $title = ['销售', '月份', '总订单', '总销售额', '目标销售额','销售额完成率', '时间进度', '剩余每天销售任务', '粉丝数', '初步响应', '单品咨询', '新粉当日成单', '新粉流水', '老粉成单', '老粉流水', '复购成单', '复购流水', '总订单', '总流水', '新粉下单人数', '新粉丝转化', '客单价'];
-
- $filename = 'xiaoshouyuebao_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 仓库发货财务报表
- */
- public function warehouseFinanceList(Request $request){
- $warehouse = (int)$request->input('warehouse');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count =Order::select('warehouse')->where(function($query) use($stime, $etime, $warehouse){
- if($stime) $query->where('send_time', '>=', $stime);
- if($etime) $query->where('send_time', '<=', $etime);
- if($warehouse>0) $query->where('warehouse', '=', $warehouse);
- })->whereNotNull('send_time')->where('warehouse','>',0)->where('logistics_id','>','')->where('is_del', 0)->groupBy('warehouse')->groupBy('send_time')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Order::select(DB::raw('send_time,warehouse,sum(cost) as goods_cost, sum(freight_cost) as freight_cost, count(1) as order_count'))->where(function($query) use($stime, $etime, $warehouse){
- if($stime) $query->where('send_time', '>=', $stime);
- if($etime) $query->where('send_time', '<=', $etime);
- if($warehouse>0) $query->where('warehouse', '=', $warehouse);
- })->whereNotNull('send_time')->where('warehouse','>',0)->where('logistics_id','>','')->where('is_del', 0)->groupBy('warehouse')->groupBy('send_time')->orderBy('send_time', 'desc')->offset($offset)->limit($pageSize)->get();
- $wareArr = $this->warehouse;
- foreach($result as $k=>&$item){
- $item->warehouse = $wareArr[$item->warehouse];
- }
- //汇总
- $total = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, count(1) as order_count'))->where(function($query) use($stime, $etime, $warehouse){
- if($stime) $query->where('send_time', '>=', $stime);
- if($etime) $query->where('send_time', '<=', $etime);
- if($warehouse>0) $query->where('warehouse', '=', $warehouse);
- })->whereNotNull('send_time')->where('warehouse','>',0)->where('logistics_id','>','')->where('is_del', 0)->first();
- return view('statistics/warehouseFinanceList', ['result'=>$result,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'warehouse' => $warehouse,
- 'stime' => $stime,
- 'etime' => $etime,
- 'total' => $total,
- 'warehouseArr' => $this->warehouse,
- ]);
- }
- /**
- * 仓库发货财务报表导出
- */
- public function warehouseFinanceList_export(Request $request){
- $warehouse = (int)$request->input('warehouse');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
-
- $result = Order::select(DB::raw('send_time,warehouse,sum(cost) as goods_cost, sum(freight_cost) as freight_cost, count(1) as order_count'))->where(function($query) use($stime, $etime, $warehouse){
- if($stime) $query->where('send_time', '>=', $stime);
- if($etime) $query->where('send_time', '<=', $etime);
- if($warehouse>0) $query->where('warehouse', '=', $warehouse);
- })->whereNotNull('send_time')->where('warehouse','>',0)->where('logistics_id','>','')->where('is_del', 0)->groupBy('warehouse')->groupBy('send_time')->orderBy('send_time', 'desc')->get();
- $wareArr = $this->warehouse;
- foreach($result as $k=>&$item){
- $item->warehouse = $wareArr[$item->warehouse];
- }
- $indexKey = ['send_time','warehouse','order_count','goods_cost','freight_cost'];
- $title = ['日期', '仓库', '单数', '货品成本', '物流成本'];
-
- $filename = 'cangkufahuo_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi7 15,30,45,60日汇总
- */
- public function districtRoiAllTotal(Request $request){
- $only_roi = (int)$request->input('only_roi');
- $sort = (int)$request->input('sort'); //排序 0:7日降序 1:15日降序 2:30日降序 3:45日降序 4:60日降序
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city){
- $city = str_replace('市', '', $city); //单独搜城市不走redis 不用排序
- }else{
- //从redis取数据
- $redis_key = 'seafood.districtRoiAllTotal';
- $redisData = Redis::get($redis_key);
- if(!empty($redisData)){
- $result = json_decode($redisData, true);
- switch ($sort) {
- case '0':
- $sort_c = 'total_roi';
- break;
- case '1':
- $sort_c = 'total_roi15_sort';
- break;
- case '2':
- $sort_c = 'total_roi30_sort';
- break;
- case '3':
- $sort_c = 'total_roi45_sort';
- break;
- case '4':
- $sort_c = 'total_roi60_sort';
- break;
- default:
- $sort_c = 'total_roi';
- break;
- }
- if($sort>0){
- $sortArr=array_column($result, $sort_c);
- array_multisort($sortArr, SORT_DESC, $result);
- }
- $count = count($result);
- $result = array_slice($result, $offset, $pageSize);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- return view('statistics/districtRoiAllTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- 'only_roi' =>$only_roi,
- 'sort' =>$sort,
- ]);
- }
- }
- $result = DistrictRoi7::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', $city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- $v['total_roi_sort'] = rtrim($v['total_roi'], '%') * 100;
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- //15日
- $data15 = DistrictRoi15::select(DB::raw('sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where('city', $v['city'])->first();
- if(isset($data15->cost)){
- $v['cost15'] = $data15->cost;
- $v['order_count15'] = $data15->order_count;
- $v['order_amount15'] = round($data15->sum_order_amount,2);
- $v['total_roi15'] = $v['cost15']>0 ? round($data15->sum_order_amount / $v['cost15'], 4) * 100 .'%' : '';
- $v['total_roi15_sort'] = rtrim($v['total_roi15'], '%') * 100;
- }else{
- $v['cost15'] = '';
- $v['order_count15'] = '';
- $v['order_amount15'] = '';
- $v['total_roi15'] = '';
- $v['total_roi15_sort'] = 0;
- }
- //30日
- $data30 = DistrictRoi30::select(DB::raw('sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where('city', $v['city'])->first();
- if(isset($data30->cost)){
- $v['cost30'] = $data30->cost;
- $v['order_count30'] = $data30->order_count;
- $v['order_amount30'] = round($data30->sum_order_amount,2);
- $v['total_roi30'] = $v['cost30']>0 ? round($data30->sum_order_amount / $v['cost30'], 4) * 100 .'%' : '';
- $v['total_roi30_sort'] = rtrim($v['total_roi30'], '%') * 100;
- }else{
- $v['cost30'] = '';
- $v['order_count30'] = '';
- $v['order_amount30'] = '';
- $v['total_roi30'] = '';
- $v['total_roi30_sort'] = 0;
- }
- //45日
- $data45 = DistrictRoi45::select(DB::raw('sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where('city', $v['city'])->first();
- if(isset($data45->cost)){
- $v['cost45'] = $data45->cost;
- $v['order_count45'] = $data45->order_count;
- $v['order_amount45'] = round($data45->sum_order_amount,2);
- $v['total_roi45'] = $v['cost45']>0 ? round($data45->sum_order_amount / $v['cost45'], 4) * 100 .'%' : '';
- $v['total_roi45_sort'] = rtrim($v['total_roi45'], '%') * 100;
- }else{
- $v['cost45'] = '';
- $v['order_count45'] = '';
- $v['order_amount45'] = '';
- $v['total_roi45'] = '';
- $v['total_roi45_sort'] = 0;
- }
- //60日
- $data60 = DistrictRoi60::select(DB::raw('sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where('city', $v['city'])->first();
- if(isset($data60->cost)){
- $v['cost60'] = $data60->cost;
- $v['order_count60'] = $data60->order_count;
- $v['order_amount60'] = round($data60->sum_order_amount,2);
- $v['total_roi60'] = $v['cost60']>0 ? round($data60->sum_order_amount / $v['cost60'], 4) * 100 .'%' : '';
- $v['total_roi60_sort'] = rtrim($v['total_roi60'], '%') * 100;
- }else{
- $v['cost60'] = '';
- $v['order_count60'] = '';
- $v['order_amount60'] = '';
- $v['total_roi60'] = '';
- $v['total_roi60_sort'] = 0;
- }
- }
- $count = count($result);
- if(!$city){
- Redis::set($redis_key, json_encode($result));
- //计算今日剩余时间
- $_time = strtotime(date('Y-m-d'). '23:59:59') - time();
- Redis::expire($redis_key, $_time);
- switch ($sort) {
- case '0':
- $sort_c = 'total_roi';
- break;
- case '1':
- $sort_c = 'total_roi15_sort';
- break;
- case '2':
- $sort_c = 'total_roi30_sort';
- break;
- case '3':
- $sort_c = 'total_roi45_sort';
- break;
- case '4':
- $sort_c = 'total_roi60_sort';
- break;
- default:
- $sort_c = 'total_roi';
- break;
- }
- if($sort>0){
- $sortArr=array_column($result, $sort_c);
- array_multisort($sortArr, SORT_DESC, $result);
- }
- $result = array_slice($result, $offset, $pageSize);
- }
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- return view('statistics/districtRoiAllTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- 'only_roi' =>$only_roi,
- 'sort' =>$sort,
- ]);
- }
- /**
- * 地域roi汇总导出
- */
- public function districtRoiAllTotal_export(Request $request){
- $only_roi = (int)$request->input('only_roi');
- $sort = (int)$request->input('sort'); //排序 0:7日降序 1:15日降序 2:30日降序 3:45日降序 4:60日降序
- $city = $request->input('city');
-
- if($city){
- $city = str_replace('市', '', $city); //单独搜城市不走redis 不用排序
- }else{
- //从redis取数据
- $redis_key = 'seafood.districtRoiAllTotal';
- $redisData = Redis::get($redis_key);
- if(!empty($redisData)){
- $result = json_decode($redisData, true);
- switch ($sort) {
- case '0':
- $sort_c = 'total_roi';
- break;
- case '1':
- $sort_c = 'total_roi15_sort';
- break;
- case '2':
- $sort_c = 'total_roi30_sort';
- break;
- case '3':
- $sort_c = 'total_roi45_sort';
- break;
- case '4':
- $sort_c = 'total_roi60_sort';
- break;
- default:
- $sort_c = 'total_roi';
- break;
- }
- if($sort>0){
- $sortArr=array_column($result, $sort_c);
- array_multisort($sortArr, SORT_DESC, $result);
- }
-
- if($only_roi == 1){
- $indexKey = ['city','total_roi','total_roi15','total_roi30','total_roi45','total_roi60'];
- $title = ['城市', '7日累计ROI', '15日累计ROI', '30日累计ROI', '45日累计ROI', '60日累计ROI'];
- }
- else{
- $indexKey = ['city','cost','order_count','order_amount','total_roi','cost15','order_count15','order_amount15','total_roi15','cost30','order_count30','order_amount30','total_roi30','cost45','order_count45','order_amount45','total_roi45','cost60','order_count60','order_amount60','total_roi60'];
- $title = ['城市', '7日累计投放成本', '7日累计成单数', '7日累计收入', '7日累计ROI', '15日累计投放成本', '15日累计成单数', '15日累计收入', '15日累计ROI', '30日累计投放成本', '30日累计成单数', '30日累计收入', '30日累计ROI', '45日累计投放成本', '45日累计成单数', '45日累计收入', '45日累计ROI', '60日累计投放成本', '60日累计成单数', '60日累计收入', '60日累计ROI'];
- }
- $filename = 'districtRoiAll_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- }
- $result = DistrictRoi7::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', $city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- $v['total_roi_sort'] = rtrim($v['total_roi'], '%') * 100;
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- //15日
- $data15 = DistrictRoi15::select(DB::raw('sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where('city', $v['city'])->first();
- if(isset($data15->cost)){
- $v['cost15'] = $data15->cost;
- $v['order_count15'] = $data15->order_count;
- $v['order_amount15'] = round($data15->sum_order_amount,2);
- $v['total_roi15'] = $v['cost15']>0 ? round($data15->sum_order_amount / $v['cost15'], 4) * 100 .'%' : '';
- $v['total_roi15_sort'] = rtrim($v['total_roi15'], '%') * 100;
- }else{
- $v['cost15'] = '';
- $v['order_count15'] = '';
- $v['order_amount15'] = '';
- $v['total_roi15'] = '';
- $v['total_roi15_sort'] = 0;
- }
- //30日
- $data30 = DistrictRoi30::select(DB::raw('sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where('city', $v['city'])->first();
- if(isset($data30->cost)){
- $v['cost30'] = $data30->cost;
- $v['order_count30'] = $data30->order_count;
- $v['order_amount30'] = round($data30->sum_order_amount,2);
- $v['total_roi30'] = $v['cost30']>0 ? round($data30->sum_order_amount / $v['cost30'], 4) * 100 .'%' : '';
- $v['total_roi30_sort'] = rtrim($v['total_roi30'], '%') * 100;
- }else{
- $v['cost30'] = '';
- $v['order_count30'] = '';
- $v['order_amount30'] = '';
- $v['total_roi30'] = '';
- $v['total_roi30_sort'] = 0;
- }
- //45日
- $data45 = DistrictRoi45::select(DB::raw('sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where('city', $v['city'])->first();
- if(isset($data45->cost)){
- $v['cost45'] = $data45->cost;
- $v['order_count45'] = $data45->order_count;
- $v['order_amount45'] = round($data45->sum_order_amount,2);
- $v['total_roi45'] = $v['cost45']>0 ? round($data45->sum_order_amount / $v['cost45'], 4) * 100 .'%' : '';
- $v['total_roi45_sort'] = rtrim($v['total_roi45'], '%') * 100;
- }else{
- $v['cost45'] = '';
- $v['order_count45'] = '';
- $v['order_amount45'] = '';
- $v['total_roi45'] = '';
- $v['total_roi45_sort'] = 0;
- }
- //60日
- $data60 = DistrictRoi60::select(DB::raw('sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where('city', $v['city'])->first();
- if(isset($data60->cost)){
- $v['cost60'] = $data60->cost;
- $v['order_count60'] = $data60->order_count;
- $v['order_amount60'] = round($data60->sum_order_amount,2);
- $v['total_roi60'] = $v['cost60']>0 ? round($data60->sum_order_amount / $v['cost60'], 4) * 100 .'%' : '';
- $v['total_roi60_sort'] = rtrim($v['total_roi60'], '%') * 100;
- }else{
- $v['cost60'] = '';
- $v['order_count60'] = '';
- $v['order_amount60'] = '';
- $v['total_roi60'] = '';
- $v['total_roi60_sort'] = 0;
- }
- }
- if(!$city){
- Redis::set($redis_key, json_encode($result));
- //计算今日剩余时间
- $_time = strtotime(date('Y-m-d'). '23:59:59') - time();
- Redis::expire($redis_key, $_time);
- switch ($sort) {
- case '0':
- $sort_c = 'total_roi';
- break;
- case '1':
- $sort_c = 'total_roi15_sort';
- break;
- case '2':
- $sort_c = 'total_roi30_sort';
- break;
- case '3':
- $sort_c = 'total_roi45_sort';
- break;
- case '4':
- $sort_c = 'total_roi60_sort';
- break;
- default:
- $sort_c = 'total_roi';
- break;
- }
- if($sort>0){
- $sortArr=array_column($result, $sort_c);
- array_multisort($sortArr, SORT_DESC, $result);
- }
- }
- if($only_roi == 1){
- $indexKey = ['city','total_roi','total_roi15','total_roi30','total_roi45','total_roi60'];
- $title = ['城市', '7日累计ROI', '15日累计ROI', '30日累计ROI', '45日累计ROI', '60日累计ROI'];
- }
- else{
- $indexKey = ['city','cost','order_count','order_amount','total_roi','cost15','order_count15','order_amount15','total_roi15','cost30','order_count30','order_amount30','total_roi30','cost45','order_count45','order_amount45','total_roi45','cost60','order_count60','order_amount60','total_roi60'];
- $title = ['城市', '7日累计投放成本', '7日累计成单数', '7日累计收入', '7日累计ROI', '15日累计投放成本', '15日累计成单数', '15日累计收入', '15日累计ROI', '30日累计投放成本', '30日累计成单数', '30日累计收入', '30日累计ROI', '45日累计投放成本', '45日累计成单数', '45日累计收入', '45日累计ROI', '60日累计投放成本', '60日累计成单数', '60日累计收入', '60日累计ROI'];
- }
- $filename = 'districtRoiAll_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 销售粉丝复购率
- */
- public function salerOrderFugou(Request $request){
- //获取销售
- $saler_id = (int)$request->input('admin_id');
- $team_id = (int)$request->input('team_id');
- $sort = (int)$request->input('sort');
- switch ($sort) {
- case '1':
- $_sort_name = 'cust_fugou_rate_sort';
- break;
- case '2':
- $_sort_name = 'cust_order_rate';
- break;
- default:
- $_sort_name = 'fugou_rate_sort';
- break;
- }
- //假如有团队筛选,检索销售队员
- $saler_ids = null;
- $team_ids = Admin::getTeams();
- if($saler_id>0){
- $saler_ids = [$saler_id];
- }elseif($team_id>0){
- $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }else{
- $saler_ids = DB::table('admin')->whereIn('team_id', $team_ids)->lists('id');
- }
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = count($saler_ids);
-
- //销售名字
- $name_arr = DB::table('admin')->whereIn('id', $saler_ids)->lists('realname', 'id');
- $result = array();
- foreach($saler_ids as $k=>$admin_id){
- $result[$k]['admin_id'] = $admin_id;
- //pv
- $temp = TemplatesLog::select(DB::raw('count(1) as tcount, count(if(type=1,true,null)) as pv_count'))->where('admin_id',$admin_id)->first();
- $result[$k]['tcount'] = $temp->tcount;
- $result[$k]['pv_count'] = $temp->pv_count;
- //加粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add, admin_name'))->where('admin_id', $admin_id)->where('is_del', 0)->first();
- if(!empty($custDetail)){
- $result[$k]['fan_add'] = $custDetail->fan_add;
- }else{
- $result[$k]['fan_add'] = '';
- }
- $result[$k]['admin_name'] = $name_arr[$admin_id];
- $result[$k]['long_count'] = $result[$k]['tcount'] - $result[$k]['pv_count'];
- //点击率
- $result[$k]['click_rate'] = $result[$k]['pv_count']>0 && $result[$k]['long_count']>0 ? round( $result[$k]['long_count'] / $result[$k]['pv_count'], 4 ) * 100 . '%' : '';
- $result[$k]['change_rate'] = $result[$k]['long_count']>0 && $result[$k]['fan_add']>0 ? round( $result[$k]['fan_add'] / $result[$k]['long_count'], 4 ) * 100 . '%' : '';
- //下单数据统计
- $order = Order::select(DB::raw('count(1) as order_count,sum(receivedAmount) as order_amount,count(distinct(receiverMobile)) as cust_count'))->where('admin_id', $admin_id)->where('is_able', 1)->where('is_del', 0)->first();
- $result[$k]['order_count'] = $order->order_count;
- $result[$k]['order_amount'] = $order->order_amount;
- //下单用户数
- $customer_count = $order->cust_count;
- //复购用户数 = 下单超过一单的用户数
- $order_cust_fugou = Order::select(DB::raw('count(1) as counts,receiverMobile'))->where('admin_id', $admin_id)->where('is_able', 1)->where('is_del', 0)->groupBy('receiverMobile')->having('counts', '>', 1)->get();
- $customer_fugou_count = count($order_cust_fugou);
- //复购单= 总单-用户数
- $fugou_count = $order->order_count - $order->cust_count;
- $no_fugou_count = $order->order_count - $fugou_count;
- //复购率
- $fugou_rate = $no_fugou_count>0 ? round( $fugou_count / $no_fugou_count, 4 ) * 100 . '%' : '';
- $cust_fugou_rate = $customer_count>0 ? round( $customer_fugou_count / $customer_count, 4 ) * 100 . '%' : '';
- $result[$k]['customer_count'] = $customer_count;
- $result[$k]['customer_fugou_count'] = $customer_fugou_count;
- $result[$k]['fugou_rate'] = $fugou_rate;
- $result[$k]['fugou_count'] = $fugou_count;
- $result[$k]['cust_fugou_rate'] = $cust_fugou_rate;
- $result[$k]['fugou_rate_sort'] = $no_fugou_count>0 ? round( $fugou_count / $no_fugou_count, 4 ) * 10000 : 0;
- $result[$k]['cust_fugou_rate_sort'] = $customer_count>0 ? round( $customer_fugou_count / $customer_count, 4 ) * 10000 : 0;
- //用户转化率
- $result[$k]['cust_order_rate'] = $result[$k]['fan_add']>0 ? round( $customer_count / $result[$k]['fan_add'], 4 ) * 100 . '%' : '';
- }
- //按复购率排序
- $fugou_rate_arr = array_column($result, $_sort_name);
- array_multisort($fugou_rate_arr, SORT_DESC, $result);
- $result = array_slice($result, $offset, $pageSize);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/salerOrderFugou', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'admin_id' =>$saler_id,
- 'team_id' =>$team_id,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'sort' =>$sort,
- ]);
- }
- /**
- * 销售粉丝复购率导出
- */
- public function salerOrderFugou_export(Request $request){
- //获取销售
- $saler_id = (int)$request->input('admin_id');
- $team_id = (int)$request->input('team_id');
- $sort = (int)$request->input('sort');
- switch ($sort) {
- case '1':
- $_sort_name = 'cust_fugou_rate_sort';
- break;
- case '2':
- $_sort_name = 'cust_order_rate';
- break;
- default:
- $_sort_name = 'fugou_rate_sort';
- break;
- }
- //假如有团队筛选,检索销售队员
- $saler_ids = null;
- $team_ids = Admin::getTeams();
- if($saler_id>0){
- $saler_ids = [$saler_id];
- }elseif($team_id>0){
- $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }else{
- $saler_ids = DB::table('admin')->whereIn('team_id', $team_ids)->lists('id');
- }
- //销售名字
- $name_arr = DB::table('admin')->whereIn('id', $saler_ids)->lists('realname', 'id');
- $result = array();
- foreach($saler_ids as $k=>$admin_id){
- $result[$k]['admin_id'] = $admin_id;
- //pv
- $temp = TemplatesLog::select(DB::raw('count(1) as tcount, count(if(type=1,true,null)) as pv_count'))->where('admin_id',$admin_id)->first();
- $result[$k]['tcount'] = $temp->tcount;
- $result[$k]['pv_count'] = $temp->pv_count;
- //加粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add, admin_name'))->where('admin_id', $admin_id)->where('is_del', 0)->first();
- if(!empty($custDetail)){
- $result[$k]['fan_add'] = $custDetail->fan_add;
- }else{
- $result[$k]['fan_add'] = '';
- }
- $result[$k]['admin_name'] = $name_arr[$admin_id];
- $result[$k]['long_count'] = $result[$k]['tcount'] - $result[$k]['pv_count'];
- //点击率
- $result[$k]['click_rate'] = $result[$k]['pv_count']>0 && $result[$k]['long_count']>0 ? round( $result[$k]['long_count'] / $result[$k]['pv_count'], 4 ) * 100 . '%' : '';
- $result[$k]['change_rate'] = $result[$k]['long_count']>0 && $result[$k]['fan_add']>0 ? round( $result[$k]['fan_add'] / $result[$k]['long_count'], 4 ) * 100 . '%' : '';
- //下单数据统计
- $order = Order::select(DB::raw('count(1) as order_count,sum(receivedAmount) as order_amount,count(distinct(receiverMobile)) as cust_count'))->where('admin_id', $admin_id)->where('is_able', 1)->where('is_del', 0)->first();
- $result[$k]['order_count'] = $order->order_count;
- $result[$k]['order_amount'] = $order->order_amount;
- //下单用户数
- $customer_count = $order->cust_count;
- //复购用户数 = 下单超过一单的用户数
- $order_cust_fugou = Order::select(DB::raw('count(1) as counts,receiverMobile'))->where('admin_id', $admin_id)->where('is_able', 1)->where('is_del', 0)->groupBy('receiverMobile')->having('counts', '>', 1)->get();
- $customer_fugou_count = count($order_cust_fugou);
- //复购单= 总单-用户数
- $fugou_count = $order->order_count - $order->cust_count;
- $no_fugou_count = $order->order_count - $fugou_count;
- //复购率
- $fugou_rate = $no_fugou_count>0 ? round( $fugou_count / $no_fugou_count, 4 ) * 100 . '%' : '';
- $cust_fugou_rate = $customer_count>0 ? round( $customer_fugou_count / $customer_count, 4 ) * 100 . '%' : '';
- $result[$k]['customer_count'] = $customer_count;
- $result[$k]['customer_fugou_count'] = $customer_fugou_count;
- $result[$k]['fugou_rate'] = $fugou_rate;
- $result[$k]['fugou_count'] = $fugou_count;
- $result[$k]['cust_fugou_rate'] = $cust_fugou_rate;
- $result[$k]['fugou_rate_sort'] = $no_fugou_count>0 ? round( $fugou_count / $no_fugou_count, 4 ) * 10000 : 0;
- $result[$k]['cust_fugou_rate_sort'] = $customer_count>0 ? round( $customer_fugou_count / $customer_count, 4 ) * 10000 : 0;
- //用户转化率
- $result[$k]['cust_order_rate'] = $result[$k]['fan_add']>0 ? round( $customer_count / $result[$k]['fan_add'], 4 ) * 100 . '%' : '';
- }
- //按复购率排序
- $fugou_rate_arr = array_column($result, $_sort_name);
- array_multisort($fugou_rate_arr, SORT_DESC, $result);
- //$indexKey = ['admin_name','pv_count','long_count','fan_add','change_rate','customer_count','customer_fugou_count','order_count','order_amount','fugou_rate'];
- $indexKey = ['admin_name','fan_add','customer_count','cust_order_rate','order_count','order_amount', 'fugou_count', 'fugou_rate', 'customer_fugou_count', 'cust_fugou_rate'];
- //$title = ['销售', 'PV量', '累计预估加粉数', '累计加粉数', '加粉转化率', '下单用户数', '复购用户数', '累计下单数', '累计销售额', '复购率'];
- $title = ['销售', '累计加粉数', '下单用户数', '用户转化率', '累计下单数', '累计销售额', '复购订单数', '订单复购率', '复购用户数', '用户复购率'];
- $filename = 'xiaoshoufugou_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 每日累计汇总数据
- */
- public function dayGrandTotal(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $_start = '2019-09-04';
- $data = array();
- if($page == 1){
- $pageSize = $pageSize - 1;
- $offset = ($page-1) * $pageSize;
- //今日数据实时计算
- $idate = date('Y-m-d');
- $data['idate'] = $idate;
- //总投入
- $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost');
- //订单信息
- $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('createTime','>=',$_start)->where('is_del',0)->first();
- $data['goods_cost'] = $order->goods_cost;
- $data['freight_cost'] = $order->freight_cost;
- $data['aftersale_cost'] = $order->aftersale_cost;
- $data['refund_fee'] = $order->refund_fee;
- $data['order_count'] = $order->order_count;
- $data['order_amount'] = $order->order_amount;
- $data['cust_count'] = $order->cust_count;
- //加粉
- $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
- //毛利 = 总销售额-总商品成本-总运费-总售后
- $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
- $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) * 100 .'%' : '';
- //总新粉单数
- $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.createTime','>=',$_start)->first();
- $data['new_order_count'] = $new_order->order_count;
- //总老粉单数
- $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
- //总复购订单
- $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
- //总复购率
- $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : '';
- //7 - 60 roi汇总
- $data['cost7'] = DB::table('roi_total')->where('type', 1)->sum('cost');
- $data['order_count7'] = DB::table('roi_total')->where('type', 1)->sum('order_count');
- $data['order_amount7'] = DB::table('roi_total')->where('type', 1)->sum('order_amount');
- $data['roi7'] = $data['cost7']>0? round($data['order_amount7'] / $data['cost7'], 4) * 100 .'%' : '';
- $data['cost15'] = DB::table('roi_total')->where('type', 2)->sum('cost');
- $data['order_count15'] = DB::table('roi_total')->where('type', 2)->sum('order_count');
- $data['order_amount15'] = DB::table('roi_total')->where('type', 2)->sum('order_amount');
- $data['roi15'] = $data['cost15']>0? round($data['order_amount15'] / $data['cost15'], 4) * 100 .'%' : '';
- $data['cost30'] = DB::table('roi_total')->where('type', 3)->sum('cost');
- $data['order_count30'] = DB::table('roi_total')->where('type', 3)->sum('order_count');
- $data['order_amount30'] = DB::table('roi_total')->where('type', 3)->sum('order_amount');
- $data['roi30'] = $data['cost30']>0? round($data['order_amount30'] / $data['cost30'], 4) * 100 .'%' : '';
- $data['cost45'] = DB::table('roi_total')->where('type', 4)->sum('cost');
- $data['order_count45'] = DB::table('roi_total')->where('type', 4)->sum('order_count');
- $data['order_amount45'] = DB::table('roi_total')->where('type', 4)->sum('order_amount');
- $data['roi45'] = $data['cost45']>0? round($data['order_amount45'] / $data['cost45'], 4) * 100 .'%' : '';
- $data['cost60'] = DB::table('roi_total')->where('type', 5)->sum('cost');
- $data['order_count60'] = DB::table('roi_total')->where('type', 5)->sum('order_count');
- $data['order_amount60'] = DB::table('roi_total')->where('type', 5)->sum('order_amount');
- $data['roi60'] = $data['cost60']>0? round($data['order_amount60'] / $data['cost60'], 4) * 100 .'%' : '';
- }else{
- $offset = ($page-1) * $pageSize - 1;
- }
- //查询历史数据
- $count = DB::table('day_grand_total')->count();
- $count = $count + 1;
- $result = DB::table('day_grand_total')->orderBy('idate', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
- $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
- $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
- $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
- $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
- $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
- $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
- }
- if($page == 1){
- $result = array_merge([$data], $result);
- }
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/20);
- }else{
- // 总页数
- $pages = 1;
- }
- return view('statistics/dayGrandTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- ]);
- }
- /**
- * 每日累计汇总数据导出
- */
- public function dayGrandTotal_export(Request $request){
- $_start = '2019-09-04';
- $data = array();
- //今日数据实时计算
- $idate = date('Y-m-d');
- $data['idate'] = $idate;
- //总投入
- $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost');
- //订单信息
- $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('createTime','>=',$_start)->where('is_del',0)->first();
- $data['goods_cost'] = $order->goods_cost;
- $data['freight_cost'] = $order->freight_cost;
- $data['aftersale_cost'] = $order->aftersale_cost;
- $data['refund_fee'] = $order->refund_fee;
- $data['order_count'] = $order->order_count;
- $data['order_amount'] = $order->order_amount;
- $data['cust_count'] = $order->cust_count;
- //加粉
- $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
- //毛利 = 总销售额-总商品成本-总运费-总售后
- $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
- $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) * 100 .'%' : '';
- //总新粉单数
- $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.createTime','>=',$_start)->first();
- $data['new_order_count'] = $new_order->order_count;
- //总老粉单数
- $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
- //总复购订单
- $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
- //总复购率
- $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : '';
- //7 - 60 roi汇总
- $data['cost7'] = DB::table('roi_total')->where('type', 1)->sum('cost');
- $data['order_count7'] = DB::table('roi_total')->where('type', 1)->sum('order_count');
- $data['order_amount7'] = DB::table('roi_total')->where('type', 1)->sum('order_amount');
- $data['roi7'] = $data['cost7']>0? round($data['order_amount7'] / $data['cost7'], 4) * 100 .'%' : '';
- $data['cost15'] = DB::table('roi_total')->where('type', 2)->sum('cost');
- $data['order_count15'] = DB::table('roi_total')->where('type', 2)->sum('order_count');
- $data['order_amount15'] = DB::table('roi_total')->where('type', 2)->sum('order_amount');
- $data['roi15'] = $data['cost15']>0? round($data['order_amount15'] / $data['cost15'], 4) * 100 .'%' : '';
- $data['cost30'] = DB::table('roi_total')->where('type', 3)->sum('cost');
- $data['order_count30'] = DB::table('roi_total')->where('type', 3)->sum('order_count');
- $data['order_amount30'] = DB::table('roi_total')->where('type', 3)->sum('order_amount');
- $data['roi30'] = $data['cost30']>0? round($data['order_amount30'] / $data['cost30'], 4) * 100 .'%' : '';
- $data['cost45'] = DB::table('roi_total')->where('type', 4)->sum('cost');
- $data['order_count45'] = DB::table('roi_total')->where('type', 4)->sum('order_count');
- $data['order_amount45'] = DB::table('roi_total')->where('type', 4)->sum('order_amount');
- $data['roi45'] = $data['cost45']>0? round($data['order_amount45'] / $data['cost45'], 4) * 100 .'%' : '';
- $data['cost60'] = DB::table('roi_total')->where('type', 5)->sum('cost');
- $data['order_count60'] = DB::table('roi_total')->where('type', 5)->sum('order_count');
- $data['order_amount60'] = DB::table('roi_total')->where('type', 5)->sum('order_amount');
- $data['roi60'] = $data['cost60']>0? round($data['order_amount60'] / $data['cost60'], 4) * 100 .'%' : '';
- $result = DB::table('day_grand_total')->orderBy('idate', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
- $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
- $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
- $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
- $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
- $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
- $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
- }
- $result = array_merge([$data], $result);
- $indexKey = ['idate','throw_cost','cust_count','order_count','order_amount','goods_cost', 'freight_cost', 'aftersale_cost', 'refund_fee', 'profit', 'roi', 'fan_count', 'new_order_count', 'old_order_count', 'fugou_order_count', 'fugou_rate', 'roi7', 'roi15', 'roi30', 'roi45', 'roi60'];
- $title = ['汇总日期', '总投放', '总客户数', '总下单数', '总销售额', '总货品成本', '总运费成本', '总售后', '总退补', '总毛利', '累计ROI', '总加粉数', '总新粉单数', '总老粉单数', '总复购单数', '总复购率', '累计7日roi', '累计15日roi', '累计30日roi', '累计45日roi', '累计60日roi'];
- $filename = 'huizongshuj_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 近30天销量排行
- */
- public function volumeRank(Request $request){
- $stime = date('Y-m-d', strtotime('-30 day'));
- $result = OrderGoodsSkus::select(DB::raw('goods_id, sku_id, sum(send_num) as volume, count(1) as counts'))->where('create_time', '>=', $stime)->where('send_num','>',0)->where('is_del', 0)->groupBy('sku_id')->orderBy('volume', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $goods = Goods::where('id', $v['goods_id'])->first();
- $skus = GoodsSkus::select('is_weigh', 'propsName', 'price')->where('id', $v['sku_id'])->first();
- $v['name'] = $goods->name;
- $v['propsName'] = $skus->propsName;
- $v['propsName'] .= !empty($skus) && $skus->is_weigh == 1 ? '【称重商品】' : '';
- $v['price'] = $skus->price;
- if($skus->is_weigh == 0) $v['volume'] = (int)$v['volume'];
- $v['amount'] = round($v['price'] * $v['volume'], 2);
- }
- return view('statistics/volumeRank', ['result' =>$result,
- ]);
- }
- public function volumeRank_export(Request $request){
- $stime = date('Y-m-d', strtotime('-30 day'));
- $result = OrderGoodsSkus::select(DB::raw('goods_id, sku_id, sum(send_num) as volume, count(1) as counts'))->where('create_time', '>=', $stime)->where('send_num','>',0)->where('is_del', 0)->groupBy('sku_id')->orderBy('volume', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $goods = Goods::where('id', $v['goods_id'])->first();
- $skus = GoodsSkus::select('is_weigh', 'propsName', 'price')->where('id', $v['sku_id'])->first();
- $v['name'] = $goods->name;
- $v['propsName'] = $skus->propsName;
- $v['propsName'] .= !empty($skus) && $skus->is_weigh == 1 ? '【称重商品】' : '';
- $v['price'] = $skus->price;
- if($skus->is_weigh == 0) $v['volume'] = (int)$v['volume'];
- $v['amount'] = round($v['price'] * $v['volume'], 2);
- $v['rank_id'] = $k+1;
- }
- $indexKey = ['rank_id','name','propsName','price','counts','volume','amount'];
- $title = ['排名', '商品标题', '商品规格', '价格', '最近30日成单量', '最近30日销量', '近30日销售额'];
- $filename = 'shangpingpaihang_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 分团队每日roi汇总数据
- */
- public function dayGrandTeamTotal(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
-
- $offset = ($page-1) * $pageSize;
-
- $self_role = session('role_name');
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //查询历史数据
- $count = DB::table('day_grand_team_total')->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- })->count();
- $teamArr = Admin::getTeamsName();
- $result = DB::table('day_grand_team_total')->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- })->orderBy('idate', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
- $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
- $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
- $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
- $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
- $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
- $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
- $v['roi90'] = $v['cost90']>0? round($v['order_amount90'] / $v['cost90'], 4) * 100 .'%' : '';
- $v['team_name'] = $teamArr[$v['team_id']];
- }
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/20);
- }else{
- // 总页数
- $pages = 1;
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('statistics/dayGrandTeamTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'team_id' =>$team_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 每日累计汇总数据导出
- */
- public function dayGrandTeamTotal_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
-
- $self_role = session('role_name');
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- $teamArr = Admin::getTeamsName();
- $result = DB::table('day_grand_team_total')->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- })->orderBy('idate', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
- $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
- $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
- $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
- $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
- $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
- $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
- $v['team_name'] = $teamArr[$v['team_id']];
- }
- $indexKey = ['idate','team_name','throw_cost','cust_count','order_count','order_amount','goods_cost', 'freight_cost', 'aftersale_cost', 'refund_fee', 'profit', 'roi', 'fan_count', 'new_order_count', 'old_order_count', 'fugou_order_count', 'fugou_rate', 'roi7', 'roi15', 'roi30', 'roi45', 'roi60'];
- $title = ['汇总日期', '团队', '总投放', '总客户数', '总下单数', '总销售额', '总货品成本', '总运费成本', '总售后', '总退补', '总毛利', '累计ROI', '总加粉数', '总新粉单数', '总老粉单数', '总复购单数', '总复购率', '累计7日roi', '累计15日roi', '累计30日roi', '累计45日roi', '累计60日roi'];
- $filename = 'teamRoi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- public function customerOrder(Request $request) {
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = CustomerOrderDay::count();
- $data = CustomerOrderDay::offset($offset)->limit($pageSize)->orderBy('grossProfit','desc')->get();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/20);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = json_decode(json_encode($data),true);
- $result = array_map(function($value){
- $item = $value;
- $phone = $value['phone'];
- $item['phone'] = is_numeric($phone) ? substr_replace($phone, '****', 3, 4) : $phone;
- return $item;
- },$result);
- return view('statistics/customerOrder', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- ]);
- }
- function findNum($str=''){
- $str=trim($str);
- if(empty($str)){return '';}
- $reg='/(\d{3}(\.\d+)?)/is';//匹配数字的正则表达式
- preg_match_all($reg,$str,$result);
- if(is_array($result)&&!empty($result)&&!empty($result[1])&&!empty($result[1][0])){
- return $result[1][0];
- }
- return '';
- }
- /**
- * 分销售每日roi汇总数据
- */
- public function dayGrandSalerTotal(Request $request){
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
-
- $offset = ($page-1) * $pageSize;
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
-
- //查询历史数据
- $count = DB::table('day_grand_saler_total')->where(function($query) use($team_id, $stime, $etime, $admin_id){
- if($team_id) $query->where('team_id', $team_id);
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- })->count();
- $teamArr = DB::table('teams')->lists('name', 'id');
- $adminArr = DB::table('admin')->lists('realname', 'id');
- $result = DB::table('day_grand_saler_total')->where(function($query) use($team_id, $stime, $etime, $admin_id){
- if($team_id) $query->where('team_id', $team_id);
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- })->orderBy('idate', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
- $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
- $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
- $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
- $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
- $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
- $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
- $v['team_name'] = $teamArr[$v['team_id']];
- $v['admin_name'] = $adminArr[$v['admin_id']];
- }
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/20);
- }else{
- // 总页数
- $pages = 1;
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
- if($self_role == '管理员') $query->where('team_id', $team_id);
- })->where('id','>', 1)->where('is_use', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/dayGrandSalerTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'self_role' =>$self_role,
- ]);
- }
- /**
- * 每日累计汇总数据导出
- */
- public function dayGrandSalerTotal_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
-
- $self_role = session('role_name');
- if($self_role == '销售' || $self_role == '分销销售'){
- $admin_id = session('admin_id');
- }
- if($self_role == '管理员'){
- //只能看自己团队的
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- $teamArr = DB::table('teams')->lists('name', 'id');
- $adminArr = DB::table('admin')->lists('realname', 'id');
- $result = DB::table('day_grand_saler_total')->where(function($query) use($team_id, $stime, $etime, $admin_id){
- if($team_id) $query->where('team_id', $team_id);
- if($admin_id) $query->where('admin_id', $admin_id);
- if($stime) $query->where('idate', '>=', $stime);
- if($etime) $query->where('idate', '<=', $etime);
- })->orderBy('idate', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
- $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
- $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
- $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
- $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
- $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
- $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
- $v['team_name'] = $teamArr[$v['team_id']];
- $v['admin_name'] = $adminArr[$v['admin_id']];
- }
- $indexKey = ['idate','admin_name','team_name','throw_cost','cust_count','order_count','order_amount','goods_cost', 'freight_cost', 'aftersale_cost', 'refund_fee', 'profit', 'roi', 'fan_count', 'new_order_count', 'old_order_count', 'fugou_order_count', 'fugou_rate', 'roi7', 'roi15', 'roi30', 'roi45', 'roi60'];
- $title = ['汇总日期','销售', '所属团队', '总投放', '总客户数', '总下单数', '总销售额', '总货品成本', '总运费成本', '总售后', '总退补', '总毛利', '累计ROI', '总加粉数', '总新粉单数', '总老粉单数', '总复购单数', '总复购率', '累计7日roi', '累计15日roi', '累计30日roi', '累计45日roi', '累计60日roi'];
- $filename = 'salerRoi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 充值日报表
- */
- public function depositDayReport(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- //获取团队及销售
- $team_ids = Admin::getTeams();
- $team_name_arr = Admin::getTeamsName();
- $saler_ids_arr = Admin::getTeamSalers($team_ids);
- $_data = $this->teamCtimePage($stime, $etime, $team_id, $offset, $pageSize, '2019-10-30');
- $result = $_data['data'];
- foreach($result as $k=>&$v){
- #团队销售
- $saler_ids = $saler_ids_arr[$v['team_id']];
- $today_deposit = CustomerDeposit::select(DB::raw('sum(pay_amount) as pay_amount, sum(discount) as discount, sum(deposit_amount) as deposit_amount, sum(if(payment_type=3,pay_amount,null)) as gth_amount'))->where('is_del', 0)->whereRaw('left(pay_time,10)="'.$v['date'].'"')->whereIn('admin_id', $saler_ids)->first();
- $v['pay_amount'] = $today_deposit->pay_amount;
- $v['discount'] = $today_deposit->discount;
- $v['deposit_amount'] = $today_deposit->deposit_amount;
- $v['gth_amount'] = $today_deposit->gth_amount;
- $v['wx_amount'] = $v['pay_amount'] - $v['gth_amount'];
- //团队名称
- $v['team_name'] = $team_name_arr[$v['team_id']];
- //累计充值金额
- $total_info = CustomerDeposit::select(DB::raw('sum(pay_amount) as pay_amount_total, sum(discount) as discount_total, sum(deposit_amount) as deposit_amount_total'))->whereIn('admin_id', $saler_ids)->where('pay_time', '<=', $v['date'] . ' 23:59:59')->where('is_del', 0)->first();
- $v['pay_amount_total'] = $total_info->pay_amount_total;
- $v['discount_total'] = $total_info->discount_total;
- $v['deposit_amount_total'] = $total_info->deposit_amount_total;
- //当日消费,订单数
- $consum = Order::select(DB::raw('count(1) as order_count, sum(receivedAmount) as order_amount'))->where('team_id', $v['team_id'])->where('payment_type', 4)->where('is_del', 0)->where('createTime', '>=', $v['date'])->where('createTime', '<=', $v['date'] . ' 23:59:59')->first();
- $v['order_count'] = $consum->order_count;
- $v['order_amount'] = $consum->order_amount;
- //剩余实的金额
- //$v['day_amount'] = $v['deposit_amount'] - $v['order_amount'];
- }
- $count = $_data['count'];
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/20);
- }else{
- // 总页数
- $pages = 1;
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
-
- return view('statistics/depositDayReport', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'team_id' =>$team_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 充值日报表
- */
- public function depositDayReport_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
-
- //获取团队及销售
- $team_ids = Admin::getTeams();
- $team_name_arr = Admin::getTeamsName();
- $saler_ids_arr = Admin::getTeamSalers($team_ids);
- $_data = $this->teamCtimePage_export($stime, $etime, $team_id, '2019-10-30');
- $result = $_data;
- foreach($result as $k=>&$v){
- #团队销售
- $saler_ids = $saler_ids_arr[$v['team_id']];
- $today_deposit = CustomerDeposit::select(DB::raw('sum(pay_amount) as pay_amount, sum(discount) as discount, sum(deposit_amount) as deposit_amount, sum(if(payment_type=3,pay_amount,null)) as gth_amount'))->where('is_del', 0)->whereRaw('left(pay_time,10)="'.$v['date'].'"')->whereIn('admin_id', $saler_ids)->first();
- $v['pay_amount'] = $today_deposit->pay_amount;
- $v['discount'] = $today_deposit->discount;
- $v['deposit_amount'] = $today_deposit->deposit_amount;
- $v['gth_amount'] = $today_deposit->gth_amount;
- $v['wx_amount'] = $v['pay_amount'] - $v['gth_amount'];
- //团队名称
- $v['team_name'] = $team_name_arr[$v['team_id']];
- //累计充值金额
- $total_info = CustomerDeposit::select(DB::raw('sum(pay_amount) as pay_amount_total, sum(discount) as discount_total, sum(deposit_amount) as deposit_amount_total'))->whereIn('admin_id', $saler_ids)->where('pay_time', '<=', $v['date'] . ' 23:59:59')->where('is_del', 0)->first();
- $v['pay_amount_total'] = $total_info->pay_amount_total;
- $v['discount_total'] = $total_info->discount_total;
- $v['deposit_amount_total'] = $total_info->deposit_amount_total;
- //当日消费,订单数
- $consum = Order::select(DB::raw('count(1) as order_count, sum(receivedAmount) as order_amount'))->where('team_id', $v['team_id'])->where('payment_type', 4)->where('is_del', 0)->where('createTime', '>=', $v['date'])->where('createTime', '<=', $v['date'] . ' 23:59:59')->first();
- $v['order_count'] = $consum->order_count;
- $v['order_amount'] = $consum->order_amount;
- }
- $indexKey = ['team_name','date','pay_amount','discount','deposit_amount','wx_amount','gth_amount','pay_amount_total','discount_total','deposit_amount_total','order_amount','order_count'];
- $title = ['团队', '日期', '当日充值金额', '当日赠送金额', '当日实得金额', '当日微信收款','当日个体户收款', '累计充值金额', '累计赠送金额', '累计实得总金额', '当日消费金额', '当日订单数'];
- $filename = 'chongzhihuizong_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 充值卡充值明细
- */
- public function depositDetail(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $saler_ids = null;
- if($team_id>0){
- $saler_ids_arr = Admin::getTeamSalers([$team_id]);
- $saler_ids = $saler_ids_arr[$team_id];
- }
- $count = CustomerDeposit::where('is_del', 0)->where(function($query) use($saler_ids, $stime, $etime){
- if($saler_ids !==null) $query->whereIn('admin_id', $saler_ids);
- if($stime) $query->where('pay_time', '>=', $stime);
- if($etime) $query->where('pay_time', '<=', $etime. ' 23:59:59');
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/20);
- }else{
- // 总页数
- $pages = 1;
- }
- $team_name_arr = Admin::getTeamsName();
- $result = CustomerDeposit::where('is_del', 0)->where(function($query) use($saler_ids, $stime, $etime){
- if($saler_ids !==null) $query->whereIn('admin_id', $saler_ids);
- if($stime) $query->where('pay_time', '>=', $stime);
- if($etime) $query->where('pay_time', '<=', $etime. ' 23:59:59');
- })->orderBy('pay_time', 'desc')->offset($offset)->limit($pageSize)->get();
- foreach($result as $k=>&$v){
- //团队
- $admin = Admin::where('id', $v['admin_id'])->first();
- $v['admin_name'] = $admin->realname;
- $v['team_name'] = $team_name_arr[$admin->team_id];
- //客户名
- $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
- $v['payment_type'] = $v['payment_type'] == 3 ? '个体户' : '微信';
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
-
- return view('statistics/depositDetail', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'team_id' =>$team_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 充值卡充值明细
- */
- public function depositDetail_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $saler_ids = null;
- if($team_id>0){
- $saler_ids_arr = Admin::getTeamSalers([$team_id]);
- $saler_ids = $saler_ids_arr[$team_id];
- }
- $team_name_arr = Admin::getTeamsName();
- $result = CustomerDeposit::where('is_del', 0)->where(function($query) use($saler_ids, $stime, $etime){
- if($saler_ids !==null) $query->whereIn('admin_id', $saler_ids);
- if($stime) $query->where('pay_time', '>=', $stime);
- if($etime) $query->where('pay_time', '<=', $etime. ' 23:59:59');
- })->orderBy('pay_time', 'desc')->get();
- foreach($result as $k=>&$v){
- //团队
- $admin = Admin::where('id', $v['admin_id'])->first();
- $v['admin_name'] = $admin->realname;
- $v['team_name'] = $team_name_arr[$admin->team_id];
- //客户名
- $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
- $v['payment_type'] = $v['payment_type'] == 3 ? '个体户' : '微信';
- }
- $indexKey = ['team_name','pay_time','cust_name','admin_name','pay_amount','discount','deposit_amount','payment_type'];
- $title = ['团队', '充值时间', '客户名称', '销售名称', '充值金额', '赠送金额', '实得金额', '充值方式'];
- $filename = 'chongzhimingxi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 充值卡消费明细
- */
- public function consumDetail(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = Order::where('payment_type', 4)->where(function($query) use($stime, $etime, $team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/20);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Order::select('id', 'team_id', 'createTime', 'admin_name', 'receivedAmount')->where('payment_type', 4)->where(function($query) use($stime, $etime, $team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->orderBy('createTime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- $team_name_arr = Admin::getTeamsName();
- foreach($result as $k=>&$v){
- $v['team_name'] = $team_name_arr[$v['team_id']];
- //消费记录
- $consum = CustomerConsum::where('order_id', $v['id'])->where('type', 0)->first();
- $v['money'] = $consum->money;
- $v['phone'] = $consum->phone;
- //客户名
- $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
-
- return view('statistics/consumDetail', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'team_id' =>$team_id,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 充值卡消费明细
- */
- public function consumDetail_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $result = Order::select('id', 'team_id', 'createTime', 'admin_name', 'receivedAmount')->where('payment_type', 4)->where(function($query) use($stime, $etime, $team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime. ' 23:59:59');
- })->where('is_del', 0)->orderBy('createTime', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- $team_name_arr = Admin::getTeamsName();
- foreach($result as $k=>&$v){
- $v['team_name'] = $team_name_arr[$v['team_id']];
- //消费记录
- $consum = CustomerConsum::where('order_id', $v['id'])->where('type', 0)->first();
- $v['money'] = $consum->money;
- $v['phone'] = $consum->phone;
- //客户名
- $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
- }
- $indexKey = ['team_name', 'id', 'createTime','cust_name','money','admin_name'];
- $title = ['团队', '订单号', '下单时间', '客户名称', '消费金额', '销售名称'];
- $filename = 'xiaofeimingxi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 客户充值月报
- */
- public function customerMonthAccount(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $_data = $this->depositCtimePage($team_id, $stime, $etime, $offset, $pageSize);
- $count = $_data['count'];
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/20);
- }else{
- // 总页数
- $pages = 1;
- }
- $team_name_arr = Admin::getTeamsName();
- $result = $_data['data'];
- foreach($result as $k=>&$v){
- $m_consum = CustomerConsum::select(DB::raw('sum(money) as money'))->whereRaw('left(create_time, 7)="'.$v['date'].'"')->where('phone', $v['phone'])->where('is_del', 0)->first();
- $v['money'] = $m_consum->money;
- $v['month'] = $v['date'];
- //用户
- $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
- //销售 -- 团队
- $admin_id = CustomerInfo::where('phone', $v['phone'])->pluck('admin_id');
- $v['team_id'] = Admin::where('id', $admin_id)->pluck('team_id');
- $v['team_name'] = $team_name_arr[$v['team_id']];
- //累计数据
- $end = date('Y-m-1', strtotime($v['month'].' +1 month'));
- //充值
- $deposit = CustomerDeposit::select(DB::raw('sum(pay_amount) as pay_amount_total, sum(discount) as discount_total, sum(deposit_amount) as deposit_amount_total'))->where('phone', $v['phone'])->where('is_del', 0)->where('pay_time', '<', $end)->first();
- $v['pay_amount_total'] = $deposit->pay_amount_total;
- $v['discount_total'] = $deposit->discount_total;
- $v['deposit_amount_total'] = $deposit->deposit_amount_total;
- //消费
- $consum = CustomerConsum::select(DB::raw('sum(money) as money'))->where('phone', $v['phone'])->where('is_del', 0)->where('create_time', '<', $end)->first();
- $v['consum_total'] = $consum->money;
- $v['balance'] = $v['deposit_amount_total'] - $v['consum_total'];
- }
- $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('statistics/customerMonthAccount', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'team_id' => $team_id,
- 'teamlist' => $teamList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 客户充值月报
- */
- public function customerMonthAccount_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $result = $this->depositCtimePage($team_id,$stime, $etime, 0, 0, 1);
- $team_name_arr = Admin::getTeamsName();
- foreach($result as $k=>&$v){
- $m_consum = CustomerConsum::select(DB::raw('sum(money) as money'))->whereRaw('left(create_time, 7)="'.$v['date'].'"')->where('phone', $v['phone'])->where('is_del', 0)->first();
- $v['money'] = $m_consum->money;
- $v['month'] = $v['date'];
- //用户
- $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
- //销售 -- 团队
- $admin_id = CustomerInfo::where('phone', $v['phone'])->pluck('admin_id');
- $v['team_id'] = Admin::where('id', $admin_id)->pluck('team_id');
- $v['team_name'] = $team_name_arr[$v['team_id']];
- //累计数据
- $end = date('Y-m-1', strtotime($v['month'].' +1 month'));
- //充值
- $deposit = CustomerDeposit::select(DB::raw('sum(pay_amount) as pay_amount_total, sum(discount) as discount_total, sum(deposit_amount) as deposit_amount_total'))->where('phone', $v['phone'])->where('is_del', 0)->where('pay_time', '<', $end)->first();
- $v['pay_amount_total'] = $deposit->pay_amount_total;
- $v['discount_total'] = $deposit->discount_total;
- $v['deposit_amount_total'] = $deposit->deposit_amount_total;
- //消费
- $consum = CustomerConsum::select(DB::raw('sum(money) as money'))->where('phone', $v['phone'])->where('is_del', 0)->where('create_time', '<', $end)->first();
- $v['consum_total'] = $consum->money;
- $v['balance'] = $v['deposit_amount_total'] - $v['consum_total'];
- }
- $indexKey = ['month', 'team_name', 'cust_name', 'pay_amount_total','discount_total','deposit_amount_total','money','consum_total','balance'];
- $title = ['月份', '团队', '充值用户', '累计充值', '累计赠送', '累计实得', '当月消费', '累计消费', '剩余实得'];
- $filename = 'chongzhiyuebao_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
-
- /**
- * 充值客户财务月报时间格式处理
- */
- public function depositCtimePage($team_id, $stime, $etime, $offset, $pageSize, $is_export=0){
- $result = array();
- $sdate = '2019-11';
- $edate = date('Y-m');
- if($stime && $stime>$sdate){
- $sdate = $stime;
- }
- if($etime && $etime<$edate){
- $edate = $etime;
- }
- if($sdate> $edate){
- return ['data'=>[], 'count'=>0];
- }
- $saler_ids = null;
- if($team_id>0){
- $saler_ids = Admin::where('team_id', $team_id)->lists('id');
- }
- /*$custs = CustomerInfo::where(function($query) use($saler_ids){
- if($saler_ids !== null) $query->whereIn('admin_id', $saler_ids);
- })->lists('phone');*/
- for($i=0;$i<36;$i++){
- $d = $sdate. ' +'.$i.' month';
- $date = date('Y-m', strtotime($d));
- $cust_end_time = date('Y-m-01', strtotime($date.' +1 month'));
- #逻辑优化,只统计当月及之前创建的客户
- $custs = CustomerInfo::where(function($query) use($saler_ids){
- if($saler_ids !== null) $query->whereIn('admin_id', $saler_ids);
- })->where('create_time', '<', $cust_end_time)->lists('phone');
- foreach($custs as $phone){
- $result[] = ['date'=>$date, 'phone'=>$phone];
- }
- if($date == $edate){
- break;
- }
- }
- if($is_export == 1){
- return $result;
- }
- $count = count($result);
- $ret['data'] = array_slice($result, $offset, $pageSize);
- $ret['count'] = $count;
- return $ret;
- }
- public function newFanMonthAmountList(Request $request) {
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id',17);
- $self_role = session('role_name');
- #销售和审核人员只能看自己团队的
- if($self_role == '管理员' || $self_role == '销售'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- if($stime == '' && $etime == ''){
- $stime = date("Y-m", strtotime('-13 month'));
- $etime = date("Y-m", strtotime('-1 month'));
- }
- $result = DB::table('cust_month_remain')->where('team_id',$team_id)
- ->where(function($query) use($stime, $etime, $team_id){
- if($stime) $query->where('idate','>=', $stime.'-01');
- if($etime) $query->where('idate','<=', $etime.'-01');
- })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
- #获取列
- $columns = array();
- $i_max = 13;
- for($i=0; $i<$i_max; $i++){
- $columns[] = $i;
- }
- #数据整合
- $data = array();
- foreach($result as $k=>$v){
- $key = $v->idate;
- $amount_data = json_decode($v->amount_data, true);
- $data[$key] = isset($data[$key]) ? array_merge($data[$key], $amount_data) : $amount_data;
- }
- $saler_ids = null;
- if( $team_id>0 ){
- $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- #数据格式化
- $new_data = array();
- foreach($data as $k=>$v){
- //当月加粉数
- $k =date('Y-m',strtotime($k));
- $end = $this->getthemonth($k);
- $fan_add = DB::table('cust_day_detail')->where('dtime', '>=', $k)->where('dtime', '<=', $end)->where('is_del',0)->where(function($query) use ($saler_ids) {
- if( $saler_ids ) $query->whereIn('admin_id',$saler_ids);
- })->sum('fan_add');
- $new_data[$k]['fan_add'] = $fan_add;
- foreach($columns as $val){
- $n = $val;
- $new_data[$k]['amount'][] = isset($v[$n]['amount']) ? $v[$n]['amount'] : '';
- }
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($team_id>0 && ($self_role=='管理员' || $self_role =='销售')) $query->where('id', $team_id);
- })->where('type', 1)->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('statistics/newFanMonthAmountList', [
- 'result' =>$new_data,
- 'columns'=>$columns,
- 'stime'=>$stime,
- 'etime'=>$etime,
- 'team_id'=>$team_id,
- 'teamlist'=>$teamList,
- ]);
- }
- public function getthemonth($date)
- {
- $firstday = date('Y-m-01', strtotime($date));
- $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
- return $lastday;
- }
-
- }
|