Brak opisu

StatisticsController.php 287KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742
  1. <?php
  2. /**
  3. * Created by Sublime.
  4. * User: hao
  5. * Date: 19/08/30
  6. * Time: 上午11:20
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\CustomerOrderDay;
  10. use App\Http\Controllers\Controller;
  11. use App\Logs;
  12. use App\CustTotal;
  13. use App\CustDetail;
  14. use App\AdCost;
  15. use App\Order;
  16. use App\DistrictRoi7;
  17. use App\DistrictRoi30;
  18. use App\DistrictRoi15;
  19. use App\DistrictRoi45;
  20. use App\DistrictRoi60;
  21. use App\SalerTargets;
  22. use App\TemplatesLog;
  23. use App\OrderGoodsSkus;
  24. use App\CustomerInfo;
  25. use App\CustomerDeposit;
  26. use App\CustomerConsum;
  27. use App\Admin;
  28. use App\Goods;
  29. use App\GoodsSkus;
  30. use App\RedisModel as Redis;
  31. use Illuminate\Http\Request;
  32. use Illuminate\Support\Facades\DB;
  33. class StatisticsController extends Controller
  34. {
  35. protected $warehouse = [];
  36. public function __construct()
  37. {
  38. $this->warehouse = config('constants.WAREHOUSE');
  39. }
  40. /**
  41. * 当日数据统计
  42. */
  43. public function fanDay(Request $request){
  44. $page = (int)$request->input('page');
  45. $pageSize = 20;
  46. if($page<=0){
  47. $page = 1;
  48. }
  49. $offset = ($page-1) * $pageSize;
  50. $stime = $request->input('stime');
  51. $etime = $request->input('etime');
  52. $team_id = (int)$request->input('team_id');
  53. $admin_id = (int)$request->input('admin_id');
  54. $self_role = session('role_name');
  55. $team_id = $request->input('team_id');
  56. #只能看自己团队的
  57. if($self_role != '超级管理员' && $self_role != '售后管理员' && $self_role != '财务'){
  58. $self_id = session('admin_id');
  59. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  60. }
  61. if($admin_id>0 && !$team_id){
  62. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  63. }
  64. //假如有团队筛选,检索销售队员
  65. $sale_ids = null;
  66. if($team_id>0){
  67. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  68. }
  69. $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime){
  70. if($team_id) $query->where('team_id', '=', $team_id);
  71. if($stime) $query->where('dtime', '>=', $stime);
  72. if($etime) $query->where('dtime', '<=', $etime);
  73. })->where('is_del',0)->first();
  74. $count = $count->total;
  75. if ($count > 1) {
  76. // 总页数
  77. $pages = ceil($count/$pageSize);
  78. }else{
  79. // 总页数
  80. $pages = 1;
  81. }
  82. $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){
  83. if($team_id) $query->where('team_id', '=', $team_id);
  84. if($stime) $query->where('dtime', '>=', $stime);
  85. if($etime) $query->where('dtime', '<=', $etime);
  86. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  87. $result = json_decode(json_encode($result),true);
  88. foreach($result as $k=>&$v){
  89. #进粉成本
  90. $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  91. #当日微信粉
  92. $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){
  93. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  94. if($admin_id>0) $query->where('admin_id', $admin_id);
  95. })->first();
  96. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  97. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  98. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  99. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  100. #当日微信粉成本
  101. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  102. #当日订单数、销售额
  103. $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){
  104. if($team_id>0) $query->where('team_id', $team_id);
  105. if($admin_id>0) $query->where('admin_id', $admin_id);
  106. })->first();
  107. $v['order_count'] = $order->order_count;
  108. $v['order_amount'] = $order->order_amount;
  109. #物流成本
  110. $v['freight_cost'] = $order->freight_cost;
  111. #货品成本
  112. $v['goods_cost'] = $order->goods_cost;
  113. #新加逻辑 -- 销售筛选
  114. if($admin_id>0){
  115. //预估当日投放 当日公众进粉
  116. # 1. 当前团队销售加粉占比
  117. $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();
  118. $salerfanRate = $teamFanAdd->team_fan_add>0 ? round($v['wx_fan_add'] / $teamFanAdd->team_fan_add, 3) * 1000 : 0;
  119. #预估该销售投放金额
  120. $v['total_cost'] = round( $salerfanRate * $v['total_cost'] / 1000, 2);
  121. #预估进粉
  122. $v['total_fan_add'] = round( $salerfanRate * $v['total_fan_add'] / 1000);
  123. #当日微信粉成本
  124. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  125. }
  126. }
  127. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  128. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员' && $self_role != '财务') $query->where('id', $team_id);
  129. })->where('type', 1)->get();
  130. $teamList = json_decode(json_encode($teamList), true);
  131. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->where(function($query) use($self_role, $team_id){
  132. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员' && $self_role != '财务') $query->where('team_id', $team_id);
  133. })->get();
  134. $adminList = json_decode(json_encode($adminList), true);
  135. return view('statistics/fanDay', ['result' =>$result,
  136. 'page' =>$page,
  137. 'count' =>$count,
  138. 'pages' =>$pages,
  139. 'teamlist' =>$teamList,
  140. 'adminlist' =>$adminList,
  141. 'stime' =>$stime,
  142. 'etime' =>$etime,
  143. 'team_id' =>$team_id,
  144. 'admin_id' =>$admin_id,
  145. ]);
  146. }
  147. public function fanDay_export(Request $request){
  148. $stime = $request->input('stime');
  149. $etime = $request->input('etime');
  150. $team_id = (int)$request->input('team_id');
  151. $self_role = session('role_name');
  152. #只能看自己团队的
  153. if($self_role != '超级管理员' && $self_role != '售后管理员' && $self_role != '财务'){
  154. $self_id = session('admin_id');
  155. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  156. }
  157. //假如有团队筛选,检索销售队员
  158. $sale_ids = null;
  159. if($team_id>0){
  160. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  161. }
  162. $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){
  163. if($team_id) $query->where('team_id', '=', $team_id);
  164. if($stime) $query->where('dtime', '>=', $stime);
  165. if($etime) $query->where('dtime', '<=', $etime);
  166. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  167. $result = json_decode(json_encode($result),true);
  168. $filename="dangrishuj.xls";
  169. header("Content-type:application/vnd.ms-excel");
  170. Header("Accept-Ranges:bytes");
  171. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  172. header("Pragma: no-cache");
  173. header("Expires: 0");
  174. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  175. xmlns:x="urn:schemas-microsoft-com:office:excel"
  176. xmlns="http://www.w3.org/TR/REC-html40">
  177. <head>
  178. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  179. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  180. <!--[if gte mso 9]><xml>
  181. <x:ExcelWorkbook>
  182. <x:ExcelWorksheets>
  183. <x:ExcelWorksheet>
  184. <x:Name></x:Name>
  185. <x:WorksheetOptions>
  186. <x:DisplayGridlines/>
  187. </x:WorksheetOptions>
  188. </x:ExcelWorksheet>
  189. </x:ExcelWorksheets>
  190. </x:ExcelWorkbook>
  191. </xml><![endif]-->
  192. </head>';
  193. $data_str .= "
  194. <table>
  195. <tr>
  196. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  197. <th>".iconv("UTF-8", "GB2312//IGNORE","当日投放金额")."</th>
  198. <th>".iconv("UTF-8", "GB2312//IGNORE","当日公众号进粉")."</th>
  199. <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉成本")."</th>
  200. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉")."</th>
  201. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉成本")."</th>
  202. <th>".iconv("UTF-8", "GB2312//IGNORE","当日订单数")."</th>
  203. <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
  204. <th>".iconv("UTF-8", "GB2312//IGNORE","当日货品成本")."</th>
  205. <th>".iconv("UTF-8", "GB2312//IGNORE","当日物流成本")."</th>
  206. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复量")."</th>
  207. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复率")."</th>
  208. <th>".iconv("UTF-8", "GB2312//IGNORE","当日老粉主动咨询量")."</th>
  209. </tr>";
  210. foreach ($result as $k => $v)
  211. {
  212. #进粉成本
  213. $v['cost_fan'] = $v['total_fan_add']>0 ? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  214. #当日微信粉
  215. $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){
  216. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  217. })->first();
  218. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  219. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  220. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  221. $v['new_reply_rate'] = $v['wx_fan_add']>0 ? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  222. #当日微信粉成本
  223. $v['cost_wx_fan'] = $v['wx_fan_add']>0 ? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  224. #当日订单数、销售额
  225. $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){
  226. if($team_id>0) $query->where('team_id', $team_id);
  227. })->first();
  228. $v['order_count'] = $order->order_count;
  229. $v['order_amount'] = $order->order_amount;
  230. #物流成本
  231. $v['freight_cost'] = $order->freight_cost;
  232. #货品成本
  233. $v['goods_cost'] = $order->goods_cost;
  234. $data_str .= "<tr>";
  235. $data_str .= "<td>".$v['dtime']."</td>";
  236. $data_str .= "<td>".$v['total_cost']."</td>";
  237. $data_str .= "<td>".$v['total_fan_add']."</td>";
  238. $data_str .= "<td>".$v['cost_fan']."</td>";
  239. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  240. $data_str .= "<td>".$v['cost_wx_fan']."</td>";
  241. $data_str .= "<td>".$v['order_count']."</td>";
  242. $data_str .= "<td>".$v['order_amount']."</td>";
  243. $data_str .= "<td>".$v['goods_cost']."</td>";
  244. $data_str .= "<td>".$v['freight_cost']."</td>";
  245. $data_str .= "<td>".$v['total_new_reply']."</td>";
  246. $data_str .= "<td>".$v['new_reply_rate']."</td>";
  247. $data_str .= "<td>".$v['total_old_consult']."</td>";
  248. $data_str .= "</tr>";
  249. }
  250. $data_str .= "</table>";
  251. echo $data_str;
  252. exit;
  253. }
  254. /**
  255. * 当日数据统计
  256. */
  257. public function orderDay(Request $request){
  258. $page = (int)$request->input('page');
  259. $pageSize = 20;
  260. if($page<=0){
  261. $page = 1;
  262. }
  263. $offset = ($page-1) * $pageSize;
  264. $stime = $request->input('stime');
  265. $etime = $request->input('etime');
  266. $team_id = (int)$request->input('team_id');
  267. $self_role = session('role_name');
  268. #只能看自己团队的
  269. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  270. $self_id = session('admin_id');
  271. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  272. }
  273. //假如有团队筛选,检索销售队员
  274. $sale_ids = null;
  275. if($team_id>0){
  276. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  277. }
  278. //规定只统计前天及以前的数据
  279. $end_time = date('Y-m-d');
  280. $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime, $end_time){
  281. if($team_id) $query->where('team_id', '=', $team_id);
  282. if($stime) $query->where('dtime', '>=', $stime);
  283. if($etime) $query->where('dtime', '<=', $etime);
  284. $query->where('dtime','<', $end_time);
  285. })->where('is_del',0)->first();
  286. $count = $count->total;
  287. if ($count > 1) {
  288. // 总页数
  289. $pages = ceil($count/$pageSize);
  290. }else{
  291. // 总页数
  292. $pages = 1;
  293. }
  294. $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){
  295. if($team_id) $query->where('team_id', '=', $team_id);
  296. if($stime) $query->where('dtime', '>=', $stime);
  297. if($etime) $query->where('dtime', '<=', $etime);
  298. $query->where('dtime','<', $end_time);
  299. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  300. $result = json_decode(json_encode($result),true);
  301. foreach($result as $k=>&$v){
  302. #当日微信好友数量
  303. $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){
  304. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  305. })->first();
  306. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  307. //当日加粉
  308. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  309. #当日加粉订单总计:
  310. $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){
  311. if($team_id>0) $query->where('team_id', $team_id);
  312. })->first();
  313. #当日新粉成单:
  314. $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){
  315. if($team_id>0) $query->where('team_id', $team_id);
  316. })->first();
  317. // 1.当日新粉成单数
  318. $v['new_order_count'] = $new_order->order_count;
  319. // 2.当日新粉成交额
  320. $v['new_order_amount'] = $new_order->order_amount;
  321. $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){
  322. if($team_id>0) $query->where('team_id', $team_id);
  323. })->first();
  324. // 3.老粉成单数
  325. $v['old_order_count'] = $old_order->order_count;
  326. // 4.老粉成交额
  327. $v['old_order_amount'] = $old_order->order_amount;
  328. // 5.总成交额
  329. $v['order_amount'] = $order->order_amount;
  330. #复购单数、复购成交额
  331. $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){
  332. if($team_id>0) $query->where('team_id', $team_id);
  333. })->first();
  334. $v['fugou_order_count'] = $fugou->order_count;
  335. $v['fugou_order_amount'] = $fugou->order_amount;
  336. #货品成本
  337. $v['order_cost'] = $order->order_cost;
  338. //加物流成本
  339. $v['freight_cost'] = $order->freight_cost;
  340. #毛利
  341. $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'] - $v['freight_cost'];
  342. //综合成单率 综合成单率=订单数/微信好友数
  343. $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  344. //新粉成单率
  345. $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  346. //复购率
  347. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  348. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  349. //加roi
  350. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  351. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  352. }
  353. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  354. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  355. })->where('type', 1)->get();
  356. $teamList = json_decode(json_encode($teamList), true);
  357. return view('statistics/orderDay', ['result' =>$result,
  358. 'page' =>$page,
  359. 'count' =>$count,
  360. 'pages' =>$pages,
  361. 'teamlist' =>$teamList,
  362. 'stime' =>$stime,
  363. 'etime' =>$etime,
  364. 'team_id' =>$team_id,
  365. ]);
  366. }
  367. public function orderDay_export(Request $request){
  368. $stime = $request->input('stime');
  369. $etime = $request->input('etime');
  370. $team_id = (int)$request->input('team_id');
  371. $self_role = session('role_name');
  372. #只能看自己团队的
  373. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  374. $self_id = session('admin_id');
  375. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  376. }
  377. //假如有团队筛选,检索销售队员
  378. $sale_ids = null;
  379. if($team_id>0){
  380. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  381. }
  382. //规定只统计前天及以前的数据
  383. $end_time = date('Y-m-d');
  384. $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){
  385. if($team_id) $query->where('team_id', '=', $team_id);
  386. if($stime) $query->where('dtime', '>=', $stime);
  387. if($etime) $query->where('dtime', '<=', $etime);
  388. $query->where('dtime','<', $end_time);
  389. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  390. $result = json_decode(json_encode($result),true);
  391. $filename="分片数据统计.xls";
  392. header("Content-type:application/vnd.ms-excel");
  393. Header("Accept-Ranges:bytes");
  394. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  395. header("Pragma: no-cache");
  396. header("Expires: 0");
  397. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  398. xmlns:x="urn:schemas-microsoft-com:office:excel"
  399. xmlns="http://www.w3.org/TR/REC-html40">
  400. <head>
  401. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  402. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  403. <!--[if gte mso 9]><xml>
  404. <x:ExcelWorkbook>
  405. <x:ExcelWorksheets>
  406. <x:ExcelWorksheet>
  407. <x:Name></x:Name>
  408. <x:WorksheetOptions>
  409. <x:DisplayGridlines/>
  410. </x:WorksheetOptions>
  411. </x:ExcelWorksheet>
  412. </x:ExcelWorksheets>
  413. </x:ExcelWorkbook>
  414. </xml><![endif]-->
  415. </head>';
  416. $data_str .= "
  417. <table>
  418. <tr>
  419. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  420. <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉数量")."</th>
  421. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
  422. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
  423. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
  424. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
  425. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  426. <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
  427. <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
  428. <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
  429. <th>".iconv("UTF-8", "GB2312//IGNORE","总投放成本")."</th>
  430. <th>".iconv("UTF-8", "GB2312//IGNORE","货品成本")."</th>
  431. <th>".iconv("UTF-8", "GB2312//IGNORE","毛利")."</th>
  432. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
  433. <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
  434. <th>".iconv("UTF-8", "GB2312//IGNORE","复购率")."</th>
  435. <th>".iconv("UTF-8", "GB2312//IGNORE","综合ROI")."</th>
  436. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉ROI")."</th>
  437. </tr>";
  438. foreach ($result as $k => $v)
  439. {
  440. #当日微信好友数量
  441. $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){
  442. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  443. })->first();
  444. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  445. //当日加粉
  446. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  447. #当日加粉订单总计:
  448. $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){
  449. if($team_id>0) $query->where('team_id', $team_id);
  450. })->first();
  451. #当日新粉成单:
  452. $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){
  453. if($team_id>0) $query->where('team_id', $team_id);
  454. })->first();
  455. // 1.当日新粉成单数
  456. $v['new_order_count'] = $new_order->order_count;
  457. // 2.当日新粉成交额
  458. $v['new_order_amount'] = $new_order->order_amount;
  459. $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){
  460. if($team_id>0) $query->where('team_id', $team_id);
  461. })->first();
  462. // 3.老粉成单数
  463. $v['old_order_count'] = $old_order->order_count;
  464. // 4.老粉成交额
  465. $v['old_order_amount'] = $old_order->order_amount;
  466. // 5.总成交额
  467. $v['order_amount'] = $order->order_amount;
  468. #复购单数、复购成交额
  469. $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){
  470. if($team_id>0) $query->where('team_id', $team_id);
  471. })->first();
  472. $v['fugou_order_count'] = $fugou->order_count;
  473. $v['fugou_order_amount'] = $fugou->order_amount;
  474. #货品成本
  475. $v['order_cost'] = $order->order_cost;
  476. #毛利
  477. $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'];
  478. //综合成单率 综合成单率=订单数/微信好友数
  479. $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  480. //新粉成单率
  481. $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  482. //复购率
  483. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  484. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  485. //加roi
  486. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  487. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  488. $data_str .= "<tr>";
  489. $data_str .= "<td>".$v['dtime']."</td>";
  490. $data_str .= "<td>".$v['total_fan_add']."</td>";
  491. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  492. $data_str .= "<td>".$v['new_order_count']."</td>";
  493. $data_str .= "<td>".$v['new_order_amount']."</td>";
  494. $data_str .= "<td>".$v['old_order_count']."</td>";
  495. $data_str .= "<td>".$v['old_order_amount']."</td>";
  496. $data_str .= "<td>".$v['fugou_order_count']."</td>";
  497. $data_str .= "<td>".$v['fugou_order_amount']."</td>";
  498. $data_str .= "<td>".$v['order_amount']."</td>";
  499. $data_str .= "<td>".$v['total_cost']."</td>";
  500. $data_str .= "<td>".$v['order_cost']."</td>";
  501. $data_str .= "<td>".$v['profit']."</td>";
  502. $data_str .= "<td>".$v['new_order_rate']."</td>";
  503. $data_str .= "<td>".$v['order_rate']."</td>";
  504. $data_str .= "<td>".$v['fugou_rate']."</td>";
  505. $data_str .= "<td>".$v['total_roi']."</td>";
  506. $data_str .= "<td>".$v['new_roi']."</td>";
  507. $data_str .= "</tr>";
  508. }
  509. $data_str .= "</table>";
  510. echo $data_str;
  511. exit;
  512. }
  513. /**
  514. * 当日数据统计(投放)
  515. */
  516. public function throwDay(Request $request){
  517. $page = (int)$request->input('page');
  518. $pageSize = 20;
  519. if($page<=0){
  520. $page = 1;
  521. }
  522. $offset = ($page-1) * $pageSize;
  523. $stime = $request->input('stime');
  524. $etime = $request->input('etime');
  525. $team_id = (int)$request->input('team_id');
  526. $self_role = session('role_name');
  527. if($self_role == '管理员'){
  528. //只能看自己团队的
  529. $self_id = session('admin_id');
  530. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  531. }
  532. //假如有团队筛选,检索销售队员
  533. $sale_ids = null;
  534. if($team_id>0){
  535. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  536. }
  537. $_data = $this->throwCtimePage($stime, $etime, $offset, $pageSize);
  538. $count = $_data['count'];
  539. if ($count > 1) {
  540. // 总页数
  541. $pages = ceil($count/$pageSize);
  542. }else{
  543. // 总页数
  544. $pages = 1;
  545. }
  546. $end_time = date('Y-m-d');
  547. foreach($_data['data'] as $k=>&$v){
  548. $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){
  549. if($team_id>0) $query->where('team_id', $team_id);
  550. })->where('is_del',0)->where('dtime', $v['date'])->first();
  551. $v['total_cost'] = isset($result->total_cost) ? $result->total_cost : '';
  552. $v['total_fan_add'] = isset($result->total_fan_add) ? $result->total_fan_add : '';
  553. $v['dtime'] = $v['date'];
  554. #进粉成本
  555. $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  556. #当日微信粉
  557. $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){
  558. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  559. })->first();
  560. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  561. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  562. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  563. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  564. #当日微信粉成本
  565. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  566. #当日订单数、销售额
  567. $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){
  568. if($team_id>0) $query->where('team_id', $team_id);
  569. })->first();
  570. $v['order_count'] = $order->order_count;
  571. $v['order_amount'] = $order->order_amount;
  572. //当日加粉
  573. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  574. #当日加粉订单总计:
  575. #截止昨天
  576. $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){
  577. if($team_id>0) $query->where('team_id', $team_id);
  578. })->where('createTime','<',$end_time)->first();
  579. #当日新粉成单:
  580. $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){
  581. if($team_id>0) $query->where('team_id', $team_id);
  582. })->first();
  583. // 1.当日新粉成单数
  584. $v['new_order_count'] = $new_order->order_count;
  585. // 2.当日新粉成交额
  586. $v['new_order_amount'] = $new_order->order_amount;
  587. // 1.当日粉累计成单数
  588. $v['order_count'] = $order->order_count;
  589. // 2.当日粉累计成交额
  590. $v['order_amount'] = $order->order_amount;
  591. #当日新粉roi=当日新增粉丝成交销售额/当日投放消耗额*100%
  592. $v['new_roi'] = $v['total_cost']>0? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  593. # 综合ROI=当日加粉累计成交额/当日投放消耗额*100%
  594. $v['roi'] = $v['total_cost']>0? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  595. # 导粉率=当日个人微信号粉丝数/当日公众号粉丝数*100%
  596. $v['fan_rate'] = $v['total_fan_add']>0? round($v['wx_fan_add'] / $v['total_fan_add'], 4) * 100 .'%' : '';
  597. }
  598. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
  599. if($self_role == '管理员') $query->where('id', $team_id);
  600. })->where('type', 1)->get();
  601. $teamList = json_decode(json_encode($teamList), true);
  602. return view('statistics/throwDay', ['result' =>$_data['data'],
  603. 'page' =>$page,
  604. 'count' =>$count,
  605. 'pages' =>$pages,
  606. 'teamlist' =>$teamList,
  607. 'stime' =>$stime,
  608. 'etime' =>$etime,
  609. 'team_id' =>$team_id,
  610. ]);
  611. }
  612. public function throwDay_export(Request $request){
  613. $stime = $request->input('stime');
  614. $etime = $request->input('etime');
  615. $team_id = (int)$request->input('team_id');
  616. //假如有团队筛选,检索销售队员
  617. $sale_ids = null;
  618. if($team_id>0){
  619. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  620. }
  621. $_data = $this->throwCtimePage($stime, $etime);
  622. $end_time = date('Y-m-d');
  623. foreach($_data as $k=>&$v){
  624. $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){
  625. if($team_id>0) $query->where('team_id', $team_id);
  626. })->where('is_del',0)->where('dtime', $v['date'])->first();
  627. $v['total_cost'] = isset($result->total_cost) ? $result->total_cost : '';
  628. $v['total_fan_add'] = isset($result->total_fan_add) ? $result->total_fan_add : '';
  629. $v['dtime'] = $v['date'];
  630. #进粉成本
  631. $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  632. #当日微信粉
  633. $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){
  634. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  635. })->first();
  636. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  637. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  638. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  639. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  640. #当日微信粉成本
  641. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  642. #当日订单数、销售额
  643. $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){
  644. if($team_id>0) $query->where('team_id', $team_id);
  645. })->first();
  646. $v['order_count'] = $order->order_count;
  647. $v['order_amount'] = $order->order_amount;
  648. //当日加粉
  649. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  650. #当日加粉订单总计:
  651. #截止昨天
  652. $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){
  653. if($team_id>0) $query->where('team_id', $team_id);
  654. })->where('createTime','<',$end_time)->first();
  655. #当日新粉成单:
  656. $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){
  657. if($team_id>0) $query->where('team_id', $team_id);
  658. })->first();
  659. // 1.当日新粉成单数
  660. $v['new_order_count'] = $new_order->order_count;
  661. // 2.当日新粉成交额
  662. $v['new_order_amount'] = $new_order->order_amount;
  663. // 1.当日粉累计成单数
  664. $v['order_count'] = $order->order_count;
  665. // 2.当日粉累计成交额
  666. $v['order_amount'] = $order->order_amount;
  667. #当日新粉roi=当日新增粉丝成交销售额/当日投放消耗额*100%
  668. $v['new_roi'] = $v['total_cost']>0? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  669. # 综合ROI=当日加粉累计成交额/当日投放消耗额*100%
  670. $v['roi'] = $v['total_cost']>0? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  671. # 导粉率=当日个人微信号粉丝数/当日公众号粉丝数*100%
  672. $v['fan_rate'] = $v['total_fan_add']>0? round($v['wx_fan_add'] / $v['total_fan_add'], 4) * 100 .'%' : '';
  673. }
  674. $indexKey = ['dtime','total_cost','total_fan_add','cost_fan','wx_fan_add','cost_wx_fan','new_roi','roi','fan_rate'];
  675. $title = ['日期', '当日投放金额', '当日公众号进粉', '当日进粉成本', '当日微信粉','当日微信粉成本', '当日新粉ROI', '综合ROI', '导粉率'];
  676. $filename = 'toufang_'.date('Y-m-d_H').'.xlsx';
  677. return Order::export_excel($_data, $filename, $indexKey, $title);
  678. }
  679. /**
  680. * 销售可看当日数据表
  681. *
  682. */
  683. public function fanDaySaler(Request $request){
  684. $page = (int)$request->input('page');
  685. $pageSize = 20;
  686. if($page<=0){
  687. $page = 1;
  688. }
  689. $offset = ($page-1) * $pageSize;
  690. $stime = $request->input('stime');
  691. $etime = $request->input('etime');
  692. $team_id = (int)$request->input('team_id');
  693. $admin_id = (int)$request->input('admin_id');
  694. $is_self_team = (int)$request->input('is_self_team');
  695. //规定只统计前天及以前的数据
  696. $end_time = date('Y-m-d');
  697. $self_role = session('role_name');
  698. if($self_role == '销售' || $self_role == '分销销售'){
  699. $admin_id = session('admin_id');
  700. #假如查看团队的
  701. if($is_self_team == 1){
  702. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  703. $admin_id = 0;
  704. }else{
  705. $team_id = null;
  706. }
  707. }
  708. if($self_role == '管理员'){
  709. //只能看自己团队的
  710. $self_id = session('admin_id');
  711. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  712. }
  713. //假如有团队筛选,检索销售队员
  714. $sale_ids = null;
  715. if($team_id>0){
  716. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  717. }
  718. $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
  719. if($admin_id) $query->where('admin_id', '=', $admin_id);
  720. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  721. if($stime) $query->where('dtime', '>=', $stime);
  722. if($etime) $query->where('dtime', '<=', $etime);
  723. if($end_time) $query->where('dtime', '<', $end_time);
  724. })->where('is_del',0)->first();
  725. $count = $count->total;
  726. if ($count > 1) {
  727. // 总页数
  728. $pages = ceil($count/$pageSize);
  729. }else{
  730. // 总页数
  731. $pages = 1;
  732. }
  733. $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){
  734. if($admin_id) $query->where('admin_id', '=', $admin_id);
  735. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  736. if($stime) $query->where('dtime', '>=', $stime);
  737. if($etime) $query->where('dtime', '<=', $etime);
  738. if($end_time) $query->where('dtime', '<', $end_time);
  739. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  740. $result = json_decode(json_encode($result),true);
  741. foreach($result as $k=>&$v){
  742. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  743. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  744. //当日加粉
  745. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  746. #当日新粉成单:
  747. $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){
  748. if($team_id>0) $query->where('team_id', $team_id);
  749. if($admin_id>0) $query->where('admin_id', $admin_id);
  750. })->first();
  751. // 1.当日新粉成单数
  752. $v['new_order_count'] = $new_order->order_count;
  753. // 2.当日新粉成交额
  754. $v['new_order_amount'] = $new_order->order_amount;
  755. // 3.新粉成交转化率
  756. $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
  757. //累计老粉量
  758. $dtime = $v['dtime'];
  759. $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
  760. if($admin_id) $query->where('admin_id', '=', $admin_id);
  761. if($sale_ids) $query->whereIn('admin_id', $sale_ids);
  762. if($dtime) $query->where('dtime', '<', $dtime);
  763. })->where('is_del',0)->sum('fan_add');
  764. $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
  765. #当日订单数、销售额
  766. $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){
  767. if($team_id>0) $query->where('team_id', $team_id);
  768. if($admin_id>0) $query->where('admin_id', $admin_id);
  769. })->first();
  770. $v['order_count'] = $order->order_count;
  771. $v['order_amount'] = $order->order_amount;
  772. // 3.老粉成单数
  773. $v['old_order_count'] = $order->order_count - $v['new_order_count'];
  774. // 4.老粉成交额
  775. $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
  776. // 5.老粉成交转化率
  777. $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
  778. }
  779. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
  780. if($self_role == '管理员') $query->where('id', $team_id);
  781. })->where('type', 1)->get();
  782. $teamList = json_decode(json_encode($teamList), true);
  783. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
  784. if($self_role == '管理员') $query->where('team_id', $team_id);
  785. })->where('id','>', 1)->where('is_use', 1)->get();
  786. $adminList = json_decode(json_encode($adminList), true);
  787. return view('statistics/fanDaySaler', ['result' =>$result,
  788. 'page' =>$page,
  789. 'count' =>$count,
  790. 'pages' =>$pages,
  791. 'teamlist' =>$teamList,
  792. 'adminlist' =>$adminList,
  793. 'stime' =>$stime,
  794. 'etime' =>$etime,
  795. 'team_id' =>$team_id,
  796. 'admin_id' =>$admin_id,
  797. 'is_self_team' =>$is_self_team,
  798. 'self_role' =>$self_role,
  799. ]);
  800. }
  801. public function fanDaySaler_export(Request $request){
  802. $stime = $request->input('stime');
  803. $etime = $request->input('etime');
  804. $team_id = (int)$request->input('team_id');
  805. $admin_id = (int)$request->input('admin_id');
  806. $is_self_team = (int)$request->input('is_self_team');
  807. //规定只统计前天及以前的数据
  808. $end_time = date('Y-m-d');
  809. $self_role = session('role_name');
  810. if($self_role == '销售' || $self_role == '分销销售'){
  811. $admin_id = session('admin_id');
  812. #假如查看团队的
  813. if($is_self_team == 1){
  814. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  815. $admin_id = 0;
  816. }else{
  817. $team_id = null;
  818. }
  819. }
  820. if($self_role == '管理员'){
  821. //只能看自己团队的
  822. $self_id = session('admin_id');
  823. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  824. }
  825. //假如有团队筛选,检索销售队员
  826. $sale_ids = null;
  827. if($team_id>0){
  828. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  829. }
  830. $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){
  831. if($admin_id) $query->where('admin_id', '=', $admin_id);
  832. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  833. if($stime) $query->where('dtime', '>=', $stime);
  834. if($etime) $query->where('dtime', '<=', $etime);
  835. if($end_time) $query->where('dtime', '<', $end_time);
  836. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  837. $result = json_decode(json_encode($result),true);
  838. $filename="当日数据统计(销售).xls";
  839. header("Content-type:application/vnd.ms-excel");
  840. Header("Accept-Ranges:bytes");
  841. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  842. header("Pragma: no-cache");
  843. header("Expires: 0");
  844. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  845. xmlns:x="urn:schemas-microsoft-com:office:excel"
  846. xmlns="http://www.w3.org/TR/REC-html40">
  847. <head>
  848. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  849. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  850. <!--[if gte mso 9]><xml>
  851. <x:ExcelWorkbook>
  852. <x:ExcelWorksheets>
  853. <x:ExcelWorksheet>
  854. <x:Name></x:Name>
  855. <x:WorksheetOptions>
  856. <x:DisplayGridlines/>
  857. </x:WorksheetOptions>
  858. </x:ExcelWorksheet>
  859. </x:ExcelWorksheets>
  860. </x:ExcelWorkbook>
  861. </xml><![endif]-->
  862. </head>';
  863. $data_str .= "
  864. <table>
  865. <tr>
  866. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  867. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉增加量")."</th>
  868. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复")."</th>
  869. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复率")."</th>
  870. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价量")."</th>
  871. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价率")."</th>
  872. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交单量")."</th>
  873. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交额")."</th>
  874. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交转化率")."</th>
  875. <th>".iconv("UTF-8", "GB2312//IGNORE","累计老粉量")."</th>
  876. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价量")."</th>
  877. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价率")."</th>
  878. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交单量")."</th>
  879. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  880. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交转化率")."</th>
  881. <th>".iconv("UTF-8", "GB2312//IGNORE","当日单量")."</th>
  882. <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
  883. </tr>";
  884. foreach ($result as $k => $v)
  885. {
  886. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  887. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  888. //当日加粉
  889. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  890. #当日新粉成单:
  891. $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){
  892. if($team_id>0) $query->where('team_id', $team_id);
  893. if($admin_id>0) $query->where('admin_id', $admin_id);
  894. })->first();
  895. // 1.当日新粉成单数
  896. $v['new_order_count'] = $new_order->order_count;
  897. // 2.当日新粉成交额
  898. $v['new_order_amount'] = $new_order->order_amount;
  899. // 3.新粉成交转化率
  900. $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
  901. //累计老粉量
  902. $dtime = $v['dtime'];
  903. $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
  904. if($admin_id) $query->where('admin_id', '=', $admin_id);
  905. if($sale_ids) $query->whereIn('admin_id', $sale_ids);
  906. if($dtime) $query->where('dtime', '<', $dtime);
  907. })->where('is_del',0)->sum('fan_add');
  908. $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
  909. #当日订单数、销售额
  910. $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){
  911. if($team_id>0) $query->where('team_id', $team_id);
  912. if($admin_id>0) $query->where('admin_id', $admin_id);
  913. })->first();
  914. $v['order_count'] = $order->order_count;
  915. $v['order_amount'] = $order->order_amount;
  916. // 3.老粉成单数
  917. $v['old_order_count'] = $order->order_count - $v['new_order_count'];
  918. // 4.老粉成交额
  919. $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
  920. // 5.老粉成交转化率
  921. $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
  922. $data_str .= "<tr>";
  923. $data_str .= "<td>".$v['dtime']."</td>";
  924. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  925. $data_str .= "<td>".$v['total_new_reply']."</td>";
  926. $data_str .= "<td>".$v['new_reply_rate']."</td>";
  927. $data_str .= "<td>".$v['total_new_consult']."</td>";
  928. $data_str .= "<td>".$v['new_consult_rate']."</td>";
  929. $data_str .= "<td>".$v['new_order_count']."</td>";
  930. $data_str .= "<td>".$v['new_order_amount']."</td>";
  931. $data_str .= "<td>".$v['new_order_rate']."</td>";
  932. $data_str .= "<td>".$v['wx_old_fan']."</td>";
  933. $data_str .= "<td>".$v['total_old_consult']."</td>";
  934. $data_str .= "<td>".$v['old_consult_rate']."</td>";
  935. $data_str .= "<td>".$v['old_order_count']."</td>";
  936. $data_str .= "<td>".$v['old_order_amount']."</td>";
  937. $data_str .= "<td>".$v['old_order_rate']."</td>";
  938. $data_str .= "<td>".$v['order_count']."</td>";
  939. $data_str .= "<td>".$v['order_amount']."</td>";
  940. $data_str .= "</tr>";
  941. }
  942. $data_str .= "</table>";
  943. echo $data_str;
  944. exit;
  945. }
  946. /**
  947. * 销售可看当日分片数据
  948. *
  949. */
  950. public function orderDaySaler(Request $request){
  951. $page = (int)$request->input('page');
  952. $pageSize = 20;
  953. if($page<=0){
  954. $page = 1;
  955. }
  956. $offset = ($page-1) * $pageSize;
  957. $stime = $request->input('stime');
  958. $etime = $request->input('etime');
  959. $team_id = (int)$request->input('team_id');
  960. $admin_id = (int)$request->input('admin_id');
  961. $is_self_team = (int)$request->input('is_self_team');
  962. //规定只统计前天及以前的数据
  963. $end_time = date('Y-m-d');
  964. $self_role = session('role_name');
  965. if($self_role == '销售' || $self_role == '分销销售'){
  966. $admin_id = session('admin_id');
  967. #假如查看团队的
  968. if($is_self_team == 1){
  969. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  970. $admin_id = 0;
  971. }else{
  972. $team_id = null;
  973. }
  974. }
  975. if($self_role == '管理员'){
  976. //只能看自己团队的
  977. $self_id = session('admin_id');
  978. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  979. }
  980. //假如有团队筛选,检索销售队员
  981. $sale_ids = null;
  982. if($team_id>0){
  983. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  984. }
  985. $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
  986. if($admin_id) $query->where('admin_id', '=', $admin_id);
  987. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  988. if($stime) $query->where('dtime', '>=', $stime);
  989. if($etime) $query->where('dtime', '<=', $etime);
  990. $query->where('dtime', '<', $end_time);
  991. })->where('is_del',0)->first();
  992. $count = $count->total;
  993. if ($count > 1) {
  994. // 总页数
  995. $pages = ceil($count/$pageSize);
  996. }else{
  997. // 总页数
  998. $pages = 1;
  999. }
  1000. $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){
  1001. if($admin_id) $query->where('admin_id', '=', $admin_id);
  1002. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  1003. if($stime) $query->where('dtime', '>=', $stime);
  1004. if($etime) $query->where('dtime', '<=', $etime);
  1005. $query->where('dtime', '<', $end_time);
  1006. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  1007. $result = json_decode(json_encode($result),true);
  1008. foreach($result as $k=>&$v){
  1009. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  1010. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  1011. //当日加粉
  1012. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  1013. #当日新粉成单:
  1014. $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){
  1015. if($team_id>0) $query->where('team_id', $team_id);
  1016. if($admin_id>0) $query->where('admin_id', $admin_id);
  1017. })->first();
  1018. // 1.当日新粉成单数
  1019. $v['new_order_count'] = $new_order->order_count;
  1020. // 2.当日新粉成交额
  1021. $v['new_order_amount'] = $new_order->order_amount;
  1022. #当日老粉成单
  1023. $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){
  1024. if($team_id>0) $query->where('team_id', $team_id);
  1025. if($admin_id>0) $query->where('admin_id', $admin_id);
  1026. })->first();
  1027. // 1.当日老粉成单数
  1028. $v['old_order_count'] = $old_order->order_count;
  1029. // 2.当日老粉成交额
  1030. $v['old_order_amount'] = $old_order->order_amount;
  1031. #复购单数、复购成交额
  1032. $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){
  1033. if($team_id>0) $query->where('team_id', $team_id);
  1034. if($admin_id>0) $query->where('admin_id', $admin_id);
  1035. })->first();
  1036. $v['fugou_order_count'] = $fugou->order_count;
  1037. $v['fugou_order_amount'] = $fugou->order_amount;
  1038. #当日加粉订单总计:
  1039. $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){
  1040. if($team_id>0) $query->where('team_id', $team_id);
  1041. if($admin_id>0) $query->where('admin_id', $admin_id);
  1042. })->first();
  1043. //总成交额
  1044. $v['order_amount'] = $order->order_amount;
  1045. //综合成单率 综合成单率=订单数/微信好友数
  1046. $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
  1047. //新粉成单率
  1048. $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
  1049. //复购率
  1050. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  1051. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  1052. }
  1053. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
  1054. if($self_role == '管理员') $query->where('id', $team_id);
  1055. })->where('type', 1)->get();
  1056. $teamList = json_decode(json_encode($teamList), true);
  1057. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
  1058. if($self_role == '管理员') $query->where('team_id', $team_id);
  1059. })->where('id','>', 1)->where('is_use', 1)->get();
  1060. $adminList = json_decode(json_encode($adminList), true);
  1061. return view('statistics/orderDaySaler', ['result' =>$result,
  1062. 'page' =>$page,
  1063. 'count' =>$count,
  1064. 'pages' =>$pages,
  1065. 'teamlist' =>$teamList,
  1066. 'adminlist' =>$adminList,
  1067. 'stime' =>$stime,
  1068. 'etime' =>$etime,
  1069. 'team_id' =>$team_id,
  1070. 'admin_id' =>$admin_id,
  1071. 'is_self_team' =>$is_self_team,
  1072. 'self_role' =>$self_role,
  1073. ]);
  1074. }
  1075. public function orderDaySaler_export(Request $request){
  1076. $stime = $request->input('stime');
  1077. $etime = $request->input('etime');
  1078. $team_id = (int)$request->input('team_id');
  1079. $admin_id = (int)$request->input('admin_id');
  1080. $is_self_team = (int)$request->input('is_self_team');
  1081. //规定只统计前天及以前的数据
  1082. $end_time = date('Y-m-d');
  1083. $self_role = session('role_name');
  1084. if($self_role == '销售' || $self_role == '分销销售'){
  1085. $admin_id = session('admin_id');
  1086. #假如查看团队的
  1087. if($is_self_team == 1){
  1088. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  1089. $admin_id = 0;
  1090. }else{
  1091. $team_id = null;
  1092. }
  1093. }
  1094. if($self_role == '管理员'){
  1095. //只能看自己团队的
  1096. $self_id = session('admin_id');
  1097. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  1098. }
  1099. //假如有团队筛选,检索销售队员
  1100. $sale_ids = null;
  1101. if($team_id>0){
  1102. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  1103. }
  1104. $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){
  1105. if($admin_id) $query->where('admin_id', '=', $admin_id);
  1106. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  1107. if($stime) $query->where('dtime', '>=', $stime);
  1108. if($etime) $query->where('dtime', '<=', $etime);
  1109. $query->where('dtime', '<', $end_time);
  1110. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  1111. $result = json_decode(json_encode($result),true);
  1112. $filename="当日数据统计(销售).xls";
  1113. header("Content-type:application/vnd.ms-excel");
  1114. Header("Accept-Ranges:bytes");
  1115. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  1116. header("Pragma: no-cache");
  1117. header("Expires: 0");
  1118. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  1119. xmlns:x="urn:schemas-microsoft-com:office:excel"
  1120. xmlns="http://www.w3.org/TR/REC-html40">
  1121. <head>
  1122. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  1123. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  1124. <!--[if gte mso 9]><xml>
  1125. <x:ExcelWorkbook>
  1126. <x:ExcelWorksheets>
  1127. <x:ExcelWorksheet>
  1128. <x:Name></x:Name>
  1129. <x:WorksheetOptions>
  1130. <x:DisplayGridlines/>
  1131. </x:WorksheetOptions>
  1132. </x:ExcelWorksheet>
  1133. </x:ExcelWorksheets>
  1134. </x:ExcelWorkbook>
  1135. </xml><![endif]-->
  1136. </head>';
  1137. $data_str .= "
  1138. <table>
  1139. <tr>
  1140. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  1141. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
  1142. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
  1143. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
  1144. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
  1145. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  1146. <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
  1147. <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
  1148. <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
  1149. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
  1150. <th>".iconv("UTF-8", "GB2312//IGNORE","复购率")."</th>
  1151. <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
  1152. </tr>";
  1153. foreach ($result as $k => $v)
  1154. {
  1155. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  1156. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  1157. //当日加粉
  1158. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  1159. #当日新粉成单:
  1160. $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){
  1161. if($team_id>0) $query->where('team_id', $team_id);
  1162. if($admin_id>0) $query->where('admin_id', $admin_id);
  1163. })->first();
  1164. // 1.当日新粉成单数
  1165. $v['new_order_count'] = $new_order->order_count;
  1166. // 2.当日新粉成交额
  1167. $v['new_order_amount'] = $new_order->order_amount;
  1168. #当日老粉成单
  1169. $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){
  1170. if($team_id>0) $query->where('team_id', $team_id);
  1171. if($admin_id>0) $query->where('admin_id', $admin_id);
  1172. })->first();
  1173. // 1.当日老粉成单数
  1174. $v['old_order_count'] = $old_order->order_count;
  1175. // 2.当日老粉成交额
  1176. $v['old_order_amount'] = $old_order->order_amount;
  1177. #复购单数、复购成交额
  1178. $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){
  1179. if($team_id>0) $query->where('team_id', $team_id);
  1180. if($admin_id>0) $query->where('admin_id', $admin_id);
  1181. })->first();
  1182. $v['fugou_order_count'] = $fugou->order_count;
  1183. $v['fugou_order_amount'] = $fugou->order_amount;
  1184. #当日加粉订单总计:
  1185. $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){
  1186. if($team_id>0) $query->where('team_id', $team_id);
  1187. if($admin_id>0) $query->where('admin_id', $admin_id);
  1188. })->first();
  1189. //总成交额
  1190. $v['order_amount'] = $order->order_amount;
  1191. //综合成单率 综合成单率=订单数/微信好友数
  1192. $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
  1193. //新粉成单率
  1194. $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
  1195. //复购率
  1196. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  1197. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  1198. $data_str .= "<tr>";
  1199. $data_str .= "<td>".$v['dtime']."</td>";
  1200. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  1201. $data_str .= "<td>".$v['new_order_count']."</td>";
  1202. $data_str .= "<td>".$v['new_order_amount']."</td>";
  1203. $data_str .= "<td>".$v['old_order_count']."</td>";
  1204. $data_str .= "<td>".$v['old_order_amount']."</td>";
  1205. $data_str .= "<td>".$v['fugou_order_count']."</td>";
  1206. $data_str .= "<td>".$v['fugou_order_amount']."</td>";
  1207. $data_str .= "<td>".$v['order_amount']."</td>";
  1208. $data_str .= "<td>".$v['new_order_rate']."</td>";
  1209. $data_str .= "<td>".$v['fugou_rate']."</td>";
  1210. $data_str .= "<td>".$v['order_rate']."</td>";
  1211. $data_str .= "</tr>";
  1212. }
  1213. $data_str .= "</table>";
  1214. echo $data_str;
  1215. exit;
  1216. }
  1217. /**
  1218. * 成单率梯形图
  1219. */
  1220. public function orderRateList(Request $request){
  1221. $stime = $request->input('stime');
  1222. $etime = $request->input('etime');
  1223. $self_role = session('role_name');
  1224. $team_id = $request->input('team_id');
  1225. #只能看自己团队的
  1226. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  1227. $self_id = session('admin_id');
  1228. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  1229. }
  1230. if($team_id>0){
  1231. $result = $this->orderTeamRateList($stime, $etime, $team_id);
  1232. return view('statistics/orderRateList', $result);
  1233. }
  1234. $_day = 30;
  1235. if($stime == '' && $etime == ''){
  1236. $stime = date("Y-m-d", strtotime('-32 day'));
  1237. $etime = date("Y-m-d", strtotime('-2 day'));
  1238. }else{
  1239. if($stime){
  1240. $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
  1241. }
  1242. }
  1243. #统计天数
  1244. $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
  1245. $day_count = DB::table('cust_day_remain')->where('month_time', $month)->where(function($query) use($stime, $etime){
  1246. if($stime) $query->where('idate','>=', $stime);
  1247. if($etime) $query->where('idate','<=', $etime);
  1248. })->count();
  1249. $result = DB::table('cust_day_remain')->where(function($query) use($stime, $etime){
  1250. if($stime) $query->where('idate','>=', $stime);
  1251. if($etime) $query->where('idate','<=', $etime);
  1252. })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
  1253. if($stime)
  1254. #获取列
  1255. $columns = array();
  1256. $i_max = 31;
  1257. for($i=0; $i<$i_max; $i++){
  1258. $columns[] = $i;
  1259. }
  1260. if($_day >= 45){
  1261. $columns[] = 45;
  1262. }
  1263. if($_day >= 60){
  1264. $columns[] = 60;
  1265. }
  1266. if($_day >= 75){
  1267. $columns[] = 75;
  1268. }
  1269. if($_day >= 90){
  1270. $columns[] = 90;
  1271. }
  1272. #数据整合
  1273. $data = array();
  1274. foreach($result as $k=>$v){
  1275. $key = $v->idate;
  1276. $rate_data = json_decode($v->order_rate_data, true);
  1277. $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
  1278. }
  1279. #数据格式化
  1280. $new_data = array();
  1281. foreach($data as $k=>$v){
  1282. //当日加粉数
  1283. $fan_add = DB::table('cust_day_detail')->where('dtime', $k)->where('is_del',0)->sum('fan_add');
  1284. $new_data[$k]['fan_add'] = $fan_add;
  1285. foreach($columns as $val){
  1286. $n = $val;
  1287. $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
  1288. }
  1289. }
  1290. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  1291. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  1292. })->where('type', 1)->get();
  1293. $teamList = json_decode(json_encode($teamList), true);
  1294. //echo '<pre>';
  1295. //print_r($new_data);print_r($columns);exit;
  1296. return view('statistics/orderRateList', ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList]);
  1297. }
  1298. /**
  1299. * 团队成单率梯形图
  1300. */
  1301. public function orderTeamRateList($stime, $etime, $team_id){
  1302. $_day = 30;
  1303. if($stime == '' && $etime == ''){
  1304. $stime = date("Y-m-d", strtotime('-32 day'));
  1305. $etime = date("Y-m-d", strtotime('-2 day'));
  1306. }else{
  1307. if($stime){
  1308. $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
  1309. }
  1310. }
  1311. #统计天数
  1312. $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
  1313. $day_count = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('month_time', $month)->where(function($query) use($stime, $etime){
  1314. if($stime) $query->where('idate','>=', $stime);
  1315. if($etime) $query->where('idate','<=', $etime);
  1316. })->count();
  1317. $result = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where(function($query) use($stime, $etime){
  1318. if($stime) $query->where('idate','>=', $stime);
  1319. if($etime) $query->where('idate','<=', $etime);
  1320. })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
  1321. if($stime)
  1322. #获取列
  1323. $columns = array();
  1324. $i_max = 31;
  1325. for($i=0; $i<$i_max; $i++){
  1326. $columns[] = $i;
  1327. }
  1328. if($_day >= 45){
  1329. $columns[] = 45;
  1330. }
  1331. if($_day >= 60){
  1332. $columns[] = 60;
  1333. }
  1334. if($_day >= 75){
  1335. $columns[] = 75;
  1336. }
  1337. if($_day >= 90){
  1338. $columns[] = 90;
  1339. }
  1340. #数据整合
  1341. $data = array();
  1342. foreach($result as $k=>$v){
  1343. $key = $v->idate;
  1344. $rate_data = json_decode($v->order_rate_data, true);
  1345. $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
  1346. }
  1347. #团队成员:
  1348. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  1349. #数据格式化
  1350. $new_data = array();
  1351. foreach($data as $k=>$v){
  1352. //当日加粉数
  1353. $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('dtime', $k)->where('is_del',0)->sum('fan_add');
  1354. $new_data[$k]['fan_add'] = $fan_add;
  1355. foreach($columns as $val){
  1356. $n = $val;
  1357. $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
  1358. }
  1359. }
  1360. //echo '<pre>';
  1361. //print_r($new_data);print_r($columns);exit;
  1362. $self_role = session('role_name');
  1363. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  1364. if($self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  1365. })->where('type', 1)->get();
  1366. $teamList = json_decode(json_encode($teamList), true);
  1367. return ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList];
  1368. }
  1369. /**
  1370. * orderSaleRank 实时销售数据展榜
  1371. */
  1372. public function orderSaleRank(Request $request){
  1373. header('Location:https://datav.aliyuncs.com/share/db4691abb6c03ebe10f6a97ac36544cd');
  1374. exit;
  1375. }
  1376. /**
  1377. * orderDistrict 实时订单地域分布
  1378. */
  1379. public function orderDistrict(Request $request){
  1380. header('Location:https://datav.aliyuncs.com/share/423b408234ea48d49a2a4ee7f33fa4e4');
  1381. exit;
  1382. }
  1383. /**
  1384. * 地区roi
  1385. */
  1386. public function districtRoi(Request $request){
  1387. $page = (int)$request->input('page');
  1388. $pageSize = 20;
  1389. if($page<=0){
  1390. $page = 1;
  1391. }
  1392. $offset = ($page-1) * $pageSize;
  1393. $stime = $request->input('stime');
  1394. $etime = $request->input('etime');
  1395. $city = $request->input('city');
  1396. if($city !== null){
  1397. $city = str_replace('市', '', $city);
  1398. }
  1399. $ret = AdCost::select('id')->where(function($query) use($stime, $etime, $city){
  1400. if($stime) $query->where('ad_time', '>=', $stime);
  1401. if($etime) $query->where('ad_time', '<=', $etime);
  1402. if($city) $query->where('city', 'like', '%'.$city.'%');
  1403. })->groupBy('ad_time')->groupBy('city')->get();
  1404. $ret = json_decode(json_encode($ret), true);
  1405. $count = count($ret);
  1406. if ($count > 1) {
  1407. // 总页数
  1408. $pages = ceil($count/$pageSize);
  1409. }else{
  1410. // 总页数
  1411. $pages = 1;
  1412. }
  1413. $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){
  1414. if($stime) $query->where('ad_time', '>=', $stime);
  1415. if($etime) $query->where('ad_time', '<=', $etime);
  1416. if($city) $query->where('city', 'like', '%'.$city.'%');
  1417. })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->offset($offset)->limit($pageSize)->get();
  1418. $result = json_decode(json_encode($result), true);
  1419. foreach($result as $k=>&$v){
  1420. //新粉收入
  1421. //当日加粉
  1422. $city_name = str_replace('市', '', $v['city']);
  1423. $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  1424. #当日加粉订单总计:
  1425. $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();
  1426. #当日新粉成单:
  1427. $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();
  1428. // 1.当日新粉成单数
  1429. $v['new_order_count'] = $new_order->order_count;
  1430. // 2.当日新粉成交额
  1431. $v['new_order_amount'] = $new_order->order_amount;
  1432. // 3.当日粉丝总成交额
  1433. $v['order_amount'] = $order->order_amount;
  1434. // 总单数
  1435. $v['order_count'] = $order->order_count;
  1436. //新粉roi
  1437. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1438. //累计roi
  1439. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1440. $v['new_fan_count'] = count($phones);
  1441. }
  1442. //总计
  1443. $total_data = [
  1444. 'cost' => 0,
  1445. 'fan_count' => 0,
  1446. 'order_count' => 0,
  1447. 'order_amount' => 0,
  1448. 'roi' => 0,
  1449. 'conversion_times' => 0,
  1450. 'new_order_count' => 0,
  1451. 'new_order_amount' => 0,
  1452. 'new_roi' => 0,
  1453. ];
  1454. //投入
  1455. $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
  1456. if($stime) $query->where('ad_time', '>=', $stime);
  1457. if($etime) $query->where('ad_time', '<=', $etime);
  1458. if($city) $query->where('city', 'like', '%'.$city.'%');
  1459. })->first();
  1460. $total_data['cost'] = $adcost->total_cost;
  1461. $total_data['conversion_times'] = $adcost->conversion_times;
  1462. //总新粉
  1463. $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
  1464. if($stime) $query->where('fanTime', '>=', $stime);
  1465. if($etime) $query->where('fanTime', '<=', $etime);
  1466. if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
  1467. })->lists('phone');
  1468. $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
  1469. //总收入
  1470. $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();
  1471. $total_data['order_count'] = $order->order_count;
  1472. $total_data['order_amount'] = $order->order_amount;
  1473. $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1474. #总新粉成单:
  1475. $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();
  1476. $total_data['new_order_count'] = $new_order->order_count;
  1477. $total_data['new_order_amount'] = $new_order->order_amount;
  1478. $total_data['new_roi'] = $total_data['cost']>0 ? round($total_data['new_order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1479. return view('statistics/districtRoi', ['result' =>$result,
  1480. 'page' =>$page,
  1481. 'count' =>$count,
  1482. 'pages' =>$pages,
  1483. 'stime' =>$stime,
  1484. 'etime' =>$etime,
  1485. 'city' =>$city,
  1486. 'total_data' =>$total_data,
  1487. ]);
  1488. }
  1489. /**
  1490. * 导出地域roi报表
  1491. */
  1492. public function districtRoi_export(Request $request){
  1493. $stime = $request->input('stime');
  1494. $etime = $request->input('etime');
  1495. $city = $request->input('city');
  1496. if($city !== null){
  1497. $city = str_replace('市', '', $city);
  1498. }
  1499. $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){
  1500. if($stime) $query->where('ad_time', '>=', $stime);
  1501. if($etime) $query->where('ad_time', '<=', $etime);
  1502. if($city) $query->where('city', 'like', '%'.$city.'%');
  1503. })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->get();
  1504. $result = json_decode(json_encode($result), true);
  1505. foreach($result as $k=>&$v){
  1506. //新粉收入
  1507. //当日加粉
  1508. $city_name = str_replace('市', '', $v['city']);
  1509. $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  1510. #当日加粉订单总计:
  1511. $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();
  1512. #当日新粉成单:
  1513. $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();
  1514. // 1.当日新粉成单数
  1515. $v['new_order_count'] = $new_order->order_count;
  1516. // 2.当日新粉成交额
  1517. $v['new_order_amount'] = $new_order->order_amount;
  1518. // 3.当日粉丝总成交额
  1519. $v['order_amount'] = $order->order_amount;
  1520. // 总单数
  1521. $v['order_count'] = $order->order_count;
  1522. //新粉roi
  1523. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1524. //累计roi
  1525. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1526. $v['new_fan_count'] = count($phones);
  1527. }
  1528. //总计
  1529. $total_data = [
  1530. 'cost' => 0,
  1531. 'fan_count' => 0,
  1532. 'order_count' => 0,
  1533. 'order_amount' => 0,
  1534. 'roi' => 0,
  1535. 'conversion_times' => 0,
  1536. 'new_order_count' => 0,
  1537. 'new_order_amount' => 0,
  1538. 'new_roi' => 0,
  1539. ];
  1540. //投入
  1541. $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
  1542. if($stime) $query->where('ad_time', '>=', $stime);
  1543. if($etime) $query->where('ad_time', '<=', $etime);
  1544. if($city) $query->where('city', 'like', '%'.$city.'%');
  1545. })->first();
  1546. $total_data['cost'] = $adcost->total_cost;
  1547. $total_data['conversion_times'] = $adcost->conversion_times;
  1548. //总新粉
  1549. $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
  1550. if($stime) $query->where('fanTime', '>=', $stime);
  1551. if($etime) $query->where('fanTime', '<=', $etime);
  1552. if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
  1553. })->lists('phone');
  1554. $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
  1555. //总收入
  1556. $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();
  1557. $total_data['order_count'] = $order->order_count;
  1558. $total_data['order_amount'] = $order->order_amount;
  1559. $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1560. #总新粉成单:
  1561. $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();
  1562. $total_data['new_order_count'] = $new_order->order_count;
  1563. $total_data['new_order_amount'] = $new_order->order_amount;
  1564. $total_data['new_roi'] = $total_data['cost']>0 ? round($total_data['new_order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1565. $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'];
  1566. $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  1567. $filename = 'district_roi_'.date('Y-m-d_H').'.xlsx';
  1568. return Order::export_excel($result, $filename, $indexKey, $title);
  1569. }
  1570. /**
  1571. * 团队订单财务明细
  1572. */
  1573. public function teamFinanceList(Request $request){
  1574. $team_id = (int)$request->input('team_id');
  1575. $stime = $request->input('stime');
  1576. $etime = $request->input('etime');
  1577. $page = (int)$request->input('page');
  1578. $pageSize = 20;
  1579. if($page<=0){
  1580. $page = 1;
  1581. }
  1582. $offset = ($page-1) * $pageSize;
  1583. $count = Order::where(function($query) use($team_id, $stime, $etime){
  1584. if($team_id) $query->where('team_id', $team_id);
  1585. if($stime) $query->where('createTime', '>=', $stime);
  1586. if($etime) $query->where('createTime', '<=', $etime. ' 23:59:59');
  1587. })->where('is_del', 0)->count();
  1588. if ($count > 1) {
  1589. // 总页数
  1590. $pages = ceil($count/$pageSize);
  1591. }else{
  1592. // 总页数
  1593. $pages = 1;
  1594. }
  1595. $teams = DB::table('teams')->lists('name', 'id');
  1596. $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){
  1597. if($team_id) $query->where('team_id', $team_id);
  1598. if($stime ) $query->where('createTime', '>=', $stime);
  1599. if($etime ) $query->where('createTime', '<=', $etime. ' 23:59:59');
  1600. })->where('is_del', 0)->orderBy('team_id', 'asc');
  1601. $result = $resultQuery->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
  1602. $result = json_decode(json_encode($result), true);
  1603. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  1604. foreach($result as $k=>&$v){
  1605. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  1606. $v['true_amount'] = $v['receivedAmount'] + $v['refund_price'] - $v['aftersale_fee'];
  1607. $v['payment_type'] = $payArr[$v['payment_type']];
  1608. $v['ctime'] = $v['createTime'];
  1609. }
  1610. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  1611. $teamList = json_decode(json_encode($teamList), true);
  1612. return view('/statistics/teamFinanceList',[
  1613. 'result' => $result,
  1614. 'team_id' => $team_id,
  1615. 'stime' => $stime,
  1616. 'etime' => $etime,
  1617. 'page' => $page,
  1618. 'count' => $count,
  1619. 'pages' => $pages,
  1620. 'teamlist' => $teamList,
  1621. ]);
  1622. }
  1623. /**
  1624. * 团队订单财务汇总列表
  1625. */
  1626. public function teamFinanceTotal(Request $request){
  1627. $team_id = (int)$request->input('team_id');
  1628. $stime = $request->input('stime');
  1629. $etime = $request->input('etime');
  1630. $page = (int)$request->input('page');
  1631. $pageSize = 20;
  1632. if($page<=0){
  1633. $page = 1;
  1634. }
  1635. $offset = ($page-1) * $pageSize;
  1636. $_data = $this->teamCtimePage($stime, $etime, $team_id, $offset, $pageSize);
  1637. $count = $_data['count'];
  1638. if ($count > 1) {
  1639. // 总页数
  1640. $pages = ceil($count/$pageSize);
  1641. }else{
  1642. // 总页数
  1643. $pages = 1;
  1644. }
  1645. $teams = DB::table('teams')->lists('name', 'id');
  1646. foreach($_data['data'] as $k=>&$v){
  1647. $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();
  1648. $v['order_count'] = isset($order_result->order_count) ? $order_result->order_count : '';
  1649. $v['receivedAmount'] = isset($order_result->receivedAmount) ? $order_result->receivedAmount : '';
  1650. $v['wx_money'] = isset($order_result->wx_money) ? $order_result->wx_money : '';
  1651. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
  1652. #当天售后和退补
  1653. $ctime = $v['ctime'] = $v['date'];
  1654. $team = $v['team_id'];
  1655. $aftersale_info = Order::select(DB::raw('count(1) as aftersale_count, sum(aftersale_fee) as aftersale_fee'))->where(function($query) use($team, $ctime){
  1656. $query->where('team_id', $team);
  1657. $query->where('aftersale_time', '>=', $ctime);
  1658. $query->where('aftersale_time', '<=', $ctime.' 23:59:59');
  1659. })->where('is_del', 0)->where('aftersale_fee', '>', 0)->first();
  1660. $v['aftersale_count'] = $aftersale_info->aftersale_count;
  1661. $v['aftersale_fee'] = $aftersale_info->aftersale_fee;
  1662. #退补
  1663. $refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where(function($query) use($team, $ctime){
  1664. $query->where('team_id', $team);
  1665. $query->where('refund_time', '=', $ctime);
  1666. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1667. $v['refund_count'] = $refund_info->refund_count;
  1668. $v['refund_price'] = $refund_info->refund_price;
  1669. //wx退补
  1670. $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){
  1671. $query->where('team_id', $team);
  1672. $query->where('refund_time', '=', $ctime);
  1673. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1674. $v['wx_refund_count'] = $wx_refund_info->refund_count;
  1675. $v['wx_refund_price'] = $wx_refund_info->refund_price;
  1676. //付款码退补
  1677. $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){
  1678. $query->where('team_id', $team);
  1679. $query->where('refund_time', '=', $ctime);
  1680. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1681. $v['zfm_refund_count'] = $zfm_refund_info->refund_count;
  1682. $v['zfm_refund_price'] = $zfm_refund_info->refund_price;
  1683. //个体户退补
  1684. $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){
  1685. $query->where('team_id', $team);
  1686. $query->where('refund_time', '=', $ctime);
  1687. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1688. $v['gth_refund_count'] = $gth_refund_info->refund_count;
  1689. $v['gth_refund_price'] = $gth_refund_info->refund_price;
  1690. //充值卡退补
  1691. $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){
  1692. $query->where('team_id', $team);
  1693. $query->where('refund_time', '=', $ctime);
  1694. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1695. $v['czk_refund_count'] = $czk_refund_info->refund_count;
  1696. $v['czk_refund_price'] = $czk_refund_info->refund_price;
  1697. #发货
  1698. $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){
  1699. $query->where('team_id', $team);
  1700. $query->where('send_time', '=', $ctime);
  1701. })->where('is_del', 0)->where('logistics_id', '>', '')->first();
  1702. $v['send_count'] = $send_info->send_count;
  1703. $v['cost'] = $send_info->cost;
  1704. $v['freight_cost'] = $send_info->freight_cost;
  1705. $v['zfm_money'] = isset($order_result->zfm_money) ? $order_result->zfm_money : '';
  1706. $v['gth_money'] = isset($order_result->gth_money) ? $order_result->gth_money : '';
  1707. $v['vip_money'] = isset($order_result->vip_money) ? $order_result->vip_money : '';
  1708. $v['wx_true_amount'] = $v['wx_money'] + $v['wx_refund_price'] - $v['aftersale_fee'];
  1709. $v['zfm_true_amount'] = $v['zfm_money'] + $v['zfm_refund_price'];
  1710. $v['gth_true_amount'] = $v['gth_money'] + $v['gth_refund_price'];
  1711. $v['czk_true_amount'] = $v['vip_money'] + $v['czk_refund_price'];
  1712. #毛利
  1713. $v['profit'] = $v['wx_true_amount'] + $v['zfm_true_amount'] + $v['gth_true_amount'] + $v['czk_true_amount'] - $v['cost'] - $v['freight_cost'];
  1714. }
  1715. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  1716. $teamList = json_decode(json_encode($teamList), true);
  1717. return view('/statistics/teamFinanceTotal',[
  1718. 'result' => $_data['data'],
  1719. 'team_id' => $team_id,
  1720. 'stime' => $stime,
  1721. 'etime' => $etime,
  1722. 'page' => $page,
  1723. 'count' => $count,
  1724. 'pages' => $pages,
  1725. 'teamlist' => $teamList,
  1726. ]);
  1727. }
  1728. /**
  1729. * 团队财务明细导出
  1730. */
  1731. public function teamFinanceList_export(Request $request){
  1732. $team_id = (int)$request->input('team_id');
  1733. $stime = $request->input('stime');
  1734. $etime = $request->input('etime');
  1735. $teams = DB::table('teams')->lists('name', 'id');
  1736. $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){
  1737. if($team_id) $query->where('team_id', $team_id);
  1738. if($stime ) $query->where('createTime', '>=', $stime);
  1739. if($etime ) $query->where('createTime', '<=', $etime. ' 23:59:59');
  1740. })->where('is_del', 0)->orderBy('team_id', 'asc');
  1741. $result = $resultQuery->orderBy('createTime','desc')->get();
  1742. $result = json_decode(json_encode($result), true);
  1743. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  1744. foreach($result as $k=>&$v){
  1745. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  1746. //$v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
  1747. $v['payment_type'] = $payArr[$v['payment_type']];
  1748. }
  1749. $indexKey = ['team_name','createTime','id','receivedAmount','cost','freight_cost', 'payment_type'];
  1750. $title = ['团队', '时间', '订单编号', '成交金额','供应商成本', '物流成本', '支付方式'];
  1751. $filename = 'dingdanmingxi_'.date('Y-m-d_H').'.xlsx';
  1752. return Order::export_excel($result, $filename, $indexKey, $title);
  1753. }
  1754. /**
  1755. * 团队财务汇总导出
  1756. */
  1757. public function teamFinanceTotal_export(Request $request){
  1758. $team_id = (int)$request->input('team_id');
  1759. $stime = $request->input('stime');
  1760. $etime = $request->input('etime');
  1761. $teams = DB::table('teams')->lists('name', 'id');
  1762. $result = $this->teamCtimePage_export($stime, $etime, $team_id);
  1763. foreach($result as $k=>&$v){
  1764. $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();
  1765. $v['order_count'] = isset($order_result->order_count) ? $order_result->order_count : '';
  1766. $v['receivedAmount'] = isset($order_result->receivedAmount) ? $order_result->receivedAmount : '';
  1767. $v['wx_money'] = isset($order_result->wx_money) ? $order_result->wx_money : '';
  1768. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
  1769. #当天售后和退补
  1770. $ctime = $v['ctime'] = $v['date'];
  1771. $team = $v['team_id'];
  1772. $aftersale_info = Order::select(DB::raw('count(1) as aftersale_count, sum(aftersale_fee) as aftersale_fee'))->where(function($query) use($team, $ctime){
  1773. $query->where('team_id', $team);
  1774. $query->where('aftersale_time', '>=', $ctime);
  1775. $query->where('aftersale_time', '<=', $ctime.' 23:59:59');
  1776. })->where('is_del', 0)->where('aftersale_fee', '>', 0)->first();
  1777. $v['aftersale_count'] = $aftersale_info->aftersale_count;
  1778. $v['aftersale_fee'] = $aftersale_info->aftersale_fee;
  1779. #退补
  1780. $refund_info = Order::select(DB::raw('count(1) as refund_count, sum(refund_price) as refund_price'))->where(function($query) use($team, $ctime){
  1781. $query->where('team_id', $team);
  1782. $query->where('refund_time', '=', $ctime);
  1783. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1784. $v['refund_count'] = $refund_info->refund_count;
  1785. $v['refund_price'] = $refund_info->refund_price;
  1786. //wx退补
  1787. $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){
  1788. $query->where('team_id', $team);
  1789. $query->where('refund_time', '=', $ctime);
  1790. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1791. $v['wx_refund_count'] = $wx_refund_info->refund_count;
  1792. $v['wx_refund_price'] = $wx_refund_info->refund_price;
  1793. //付款码退补
  1794. $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){
  1795. $query->where('team_id', $team);
  1796. $query->where('refund_time', '=', $ctime);
  1797. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1798. $v['zfm_refund_count'] = $zfm_refund_info->refund_count;
  1799. $v['zfm_refund_price'] = $zfm_refund_info->refund_price;
  1800. //个体户退补
  1801. $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){
  1802. $query->where('team_id', $team);
  1803. $query->where('refund_time', '=', $ctime);
  1804. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1805. $v['gth_refund_count'] = $gth_refund_info->refund_count;
  1806. $v['gth_refund_price'] = $gth_refund_info->refund_price;
  1807. //充值卡退补
  1808. $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){
  1809. $query->where('team_id', $team);
  1810. $query->where('refund_time', '=', $ctime);
  1811. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->first();
  1812. $v['czk_refund_count'] = $czk_refund_info->refund_count;
  1813. $v['czk_refund_price'] = $czk_refund_info->refund_price;
  1814. #发货
  1815. $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){
  1816. $query->where('team_id', $team);
  1817. $query->where('send_time', '=', $ctime);
  1818. })->where('is_del', 0)->where('logistics_id', '>', '')->first();
  1819. $v['send_count'] = $send_info->send_count;
  1820. $v['cost'] = $send_info->cost;
  1821. $v['freight_cost'] = $send_info->freight_cost;
  1822. $v['zfm_money'] = isset($order_result->zfm_money) ? $order_result->zfm_money : '';
  1823. $v['gth_money'] = isset($order_result->gth_money) ? $order_result->gth_money : '';
  1824. $v['vip_money'] = isset($order_result->vip_money) ? $order_result->vip_money : '';
  1825. $v['wx_true_amount'] = $v['wx_money'] + $v['wx_refund_price'] - $v['aftersale_fee'];
  1826. $v['zfm_true_amount'] = $v['zfm_money'] + $v['zfm_refund_price'];
  1827. $v['gth_true_amount'] = $v['gth_money'] + $v['gth_refund_price'];
  1828. $v['czk_true_amount'] = $v['vip_money'] + $v['czk_refund_price'];
  1829. #毛利
  1830. $v['profit'] = $v['wx_true_amount'] + $v['zfm_true_amount'] + $v['gth_true_amount'] + $v['czk_true_amount'] - $v['cost'] - $v['freight_cost'];
  1831. }
  1832. $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'];
  1833. $title = ['团队', '时间', '总单数', '成交金额', '微信支付金额','付款码支付金额', '个体户支付', '充值卡支付金额', '售后单数', '售后金额', '微信退补单数', '微信退补差价', '微信实际金额', '付款码退补单数', '付款码退补差价', '付款码实际金额', '个体户退补单数', '个体户退补差价', '个体户实际金额', '充值卡退补单数', '充值卡退补差价', '充值卡实际金额', '发货单数', '货品成本', '物流成本', '毛利'];
  1834. $filename = 'dingdanhuizong_'.date('Y-m-d_H').'.xlsx';
  1835. return Order::export_excel($result, $filename, $indexKey, $title);
  1836. }
  1837. /**
  1838. * 售后财务明细
  1839. */
  1840. public function aftersaleFinanceList(Request $request){
  1841. $team_id = (int)$request->input('team_id');
  1842. $stime = $request->input('stime');
  1843. $etime = $request->input('etime');
  1844. $warehouse = (int)$request->input('warehouse');
  1845. $page = (int)$request->input('page');
  1846. $pageSize = 20;
  1847. if($page<=0){
  1848. $page = 1;
  1849. }
  1850. $offset = ($page-1) * $pageSize;
  1851. $count = Order::where(function($query) use($team_id, $stime, $etime, $warehouse){
  1852. if($team_id) $query->where('team_id', $team_id);
  1853. if($warehouse) $query->where('warehouse', $warehouse);
  1854. if($stime) $query->where('aftersale_time', '>=', $stime);
  1855. if($etime) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  1856. })->where('is_del', 0)->where('aftersale_fee', '>', 0)->count();
  1857. if ($count > 1) {
  1858. // 总页数
  1859. $pages = ceil($count/$pageSize);
  1860. }else{
  1861. // 总页数
  1862. $pages = 1;
  1863. }
  1864. $teams = DB::table('teams')->lists('name', 'id');
  1865. $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){
  1866. if($team_id) $query->where('team_id', $team_id);
  1867. if($warehouse) $query->where('warehouse', $warehouse);
  1868. if($stime ) $query->where('aftersale_time', '>=', $stime);
  1869. if($etime ) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  1870. })->where('is_del', 0)->where('aftersale_fee', '>', 0);
  1871. $result = $resultQuery->orderBy('aftersale_time','desc')->offset($offset)->limit($pageSize)->get();
  1872. $result = json_decode(json_encode($result), true);
  1873. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  1874. $wareArr = $this->warehouse;
  1875. foreach($result as $k=>&$v){
  1876. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  1877. //$v['true_amount'] = $v['receivedAmount'] + $v['refund_price'] - $v['aftersale_fee'];
  1878. $v['payment_type'] = $payArr[$v['payment_type']];
  1879. $v['ctime'] = $v['aftersale_time'];
  1880. $v['warehouse'] = $wareArr[$v['warehouse']];
  1881. }
  1882. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  1883. $teamList = json_decode(json_encode($teamList), true);
  1884. return view('/statistics/aftersaleFinanceList',[
  1885. 'result' => $result,
  1886. 'team_id' => $team_id,
  1887. 'stime' => $stime,
  1888. 'etime' => $etime,
  1889. 'page' => $page,
  1890. 'count' => $count,
  1891. 'pages' => $pages,
  1892. 'teamlist' => $teamList,
  1893. 'warehouse' => $warehouse,
  1894. 'warehouseArr' => $this->warehouse,
  1895. ]);
  1896. }
  1897. /**
  1898. * 退补财务明细
  1899. */
  1900. public function refundFinanceList(Request $request){
  1901. $team_id = (int)$request->input('team_id');
  1902. $warehouse = (int)$request->input('warehouse');
  1903. $stime = $request->input('stime');
  1904. $etime = $request->input('etime');
  1905. $page = (int)$request->input('page');
  1906. $pageSize = 20;
  1907. if($page<=0){
  1908. $page = 1;
  1909. }
  1910. $offset = ($page-1) * $pageSize;
  1911. $count = Order::where(function($query) use($team_id, $stime, $etime, $warehouse){
  1912. if($team_id) $query->where('team_id', $team_id);
  1913. if($warehouse) $query->where('warehouse', $warehouse);
  1914. if($stime) $query->where('refund_time', '>=', $stime);
  1915. if($etime) $query->where('refund_time', '<=', $etime);
  1916. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1)->count();
  1917. if ($count > 1) {
  1918. // 总页数
  1919. $pages = ceil($count/$pageSize);
  1920. }else{
  1921. // 总页数
  1922. $pages = 1;
  1923. }
  1924. $teams = DB::table('teams')->lists('name', 'id');
  1925. $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){
  1926. if($team_id) $query->where('team_id', $team_id);
  1927. if($warehouse) $query->where('warehouse', $warehouse);
  1928. if($stime ) $query->where('refund_time', '>=', $stime);
  1929. if($etime ) $query->where('refund_time', '<=', $etime);
  1930. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1);
  1931. $result = $resultQuery->orderBy('refund_time','desc')->offset($offset)->limit($pageSize)->get();
  1932. $result = json_decode(json_encode($result), true);
  1933. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  1934. $wareArr = $this->warehouse;
  1935. foreach($result as $k=>&$v){
  1936. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  1937. $v['payment_type'] = $payArr[$v['payment_type']];
  1938. $v['ctime'] = $v['refund_time'];
  1939. //退补支付方式
  1940. if($v['refund_type'] == 1){
  1941. $v['refund_type'] = '微信支付';
  1942. }elseif($v['refund_type'] == 2){
  1943. $v['refund_type'] = '付款码支付';
  1944. }elseif($v['refund_type'] == 3){
  1945. $v['refund_type'] = '个体户支付';
  1946. }elseif($v['refund_type'] == 4){
  1947. $v['refund_type'] = '充值卡支付';
  1948. }else{
  1949. $v['refund_type'] = '';
  1950. }
  1951. $v['warehouse'] = $wareArr[$v['warehouse']];
  1952. }
  1953. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  1954. $teamList = json_decode(json_encode($teamList), true);
  1955. return view('/statistics/refundFinanceList',[
  1956. 'result' => $result,
  1957. 'team_id' => $team_id,
  1958. 'stime' => $stime,
  1959. 'etime' => $etime,
  1960. 'page' => $page,
  1961. 'count' => $count,
  1962. 'pages' => $pages,
  1963. 'teamlist' => $teamList,
  1964. 'warehouse' => $warehouse,
  1965. 'warehouseArr' => $this->warehouse
  1966. ]);
  1967. }
  1968. /**
  1969. * 发货财务明细
  1970. */
  1971. public function sendFinanceList(Request $request){
  1972. $team_id = (int)$request->input('team_id');
  1973. $stime = $request->input('stime');
  1974. $etime = $request->input('etime');
  1975. $warehouse = (int)$request->input('warehouse');
  1976. $page = (int)$request->input('page');
  1977. $pageSize = 20;
  1978. if($page<=0){
  1979. $page = 1;
  1980. }
  1981. $offset = ($page-1) * $pageSize;
  1982. $count = Order::where(function($query) use($team_id, $stime, $etime, $warehouse){
  1983. if($team_id) $query->where('team_id', $team_id);
  1984. if($warehouse) $query->where('warehouse', $warehouse);
  1985. if($stime) $query->where('send_time', '>=', $stime);
  1986. if($etime) $query->where('send_time', '<=', $etime);
  1987. })->where('is_del', 0)->where('logistics_id', '>', '')->count();
  1988. if ($count > 1) {
  1989. // 总页数
  1990. $pages = ceil($count/$pageSize);
  1991. }else{
  1992. // 总页数
  1993. $pages = 1;
  1994. }
  1995. $teams = DB::table('teams')->lists('name', 'id');
  1996. $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){
  1997. if($team_id) $query->where('team_id', $team_id);
  1998. if($warehouse) $query->where('warehouse', $warehouse);
  1999. if($stime ) $query->where('send_time', '>=', $stime);
  2000. if($etime ) $query->where('send_time', '<=', $etime);
  2001. })->where('is_del', 0)->where('logistics_id', '>', '');
  2002. $result = $resultQuery->orderBy('send_time','desc')->offset($offset)->limit($pageSize)->get();
  2003. $result = json_decode(json_encode($result), true);
  2004. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  2005. $wareArr = $this->warehouse;
  2006. foreach($result as $k=>&$v){
  2007. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  2008. $v['payment_type'] = $payArr[$v['payment_type']];
  2009. $v['ctime'] = $v['send_time'];
  2010. $v['warehouse'] = $wareArr[$v['warehouse']];
  2011. }
  2012. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  2013. $teamList = json_decode(json_encode($teamList), true);
  2014. return view('/statistics/sendFinanceList',[
  2015. 'result' => $result,
  2016. 'team_id' => $team_id,
  2017. 'stime' => $stime,
  2018. 'etime' => $etime,
  2019. 'page' => $page,
  2020. 'count' => $count,
  2021. 'pages' => $pages,
  2022. 'teamlist' => $teamList,
  2023. 'warehouse' => $warehouse,
  2024. 'warehouseArr' => $this->warehouse,
  2025. ]);
  2026. }
  2027. /**
  2028. * 售后财务明细
  2029. */
  2030. public function aftersaleFinanceList_export(Request $request){
  2031. $team_id = (int)$request->input('team_id');
  2032. $stime = $request->input('stime');
  2033. $etime = $request->input('etime');
  2034. $warehouse = (int)$request->input('warehouse');
  2035. $teams = DB::table('teams')->lists('name', 'id');
  2036. $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){
  2037. if($team_id) $query->where('team_id', $team_id);
  2038. if($warehouse) $query->where('warehouse', $warehouse);
  2039. if($stime ) $query->where('aftersale_time', '>=', $stime);
  2040. if($etime ) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  2041. })->where('is_del', 0)->where('aftersale_fee', '>', 0);
  2042. $result = $resultQuery->orderBy('aftersale_time','desc')->get();
  2043. $result = json_decode(json_encode($result), true);
  2044. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  2045. $wareArr = $this->warehouse;
  2046. foreach($result as $k=>&$v){
  2047. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  2048. //$v['true_amount'] = $v['receivedAmount'] + $v['refund_price'] - $v['aftersale_fee'];
  2049. $v['payment_type'] = $payArr[$v['payment_type']];
  2050. $v['ctime'] = $v['aftersale_time'];
  2051. $v['warehouse'] = $wareArr[$v['warehouse']];
  2052. }
  2053. $indexKey = ['team_name','ctime','id','receivedAmount','aftersale_fee', 'payment_type', 'warehouse'];
  2054. $title = ['团队', '售后时间', '订单编号', '成交金额','售后金额', '支付方式', '发货仓库'];
  2055. $filename = 'shouhoumingxi_'.date('Y-m-d_H').'.xlsx';
  2056. return Order::export_excel($result, $filename, $indexKey, $title);
  2057. }
  2058. /**
  2059. * 退补财务明细
  2060. */
  2061. public function refundFinanceList_export(Request $request){
  2062. $team_id = (int)$request->input('team_id');
  2063. $stime = $request->input('stime');
  2064. $etime = $request->input('etime');
  2065. $warehouse = (int)$request->input('warehouse');
  2066. $teams = DB::table('teams')->lists('name', 'id');
  2067. $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){
  2068. if($team_id) $query->where('team_id', $team_id);
  2069. if($warehouse) $query->where('warehouse', $warehouse);
  2070. if($stime ) $query->where('refund_time', '>=', $stime);
  2071. if($etime ) $query->where('refund_time', '<=', $etime);
  2072. })->where('is_del', 0)->where('refund_time', '>', '')->where('is_refund', 1);
  2073. $result = $resultQuery->orderBy('refund_time','desc')->get();
  2074. $result = json_decode(json_encode($result), true);
  2075. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  2076. $wareArr = $this->warehouse;
  2077. foreach($result as $k=>&$v){
  2078. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  2079. $v['payment_type'] = $payArr[$v['payment_type']];
  2080. $v['ctime'] = $v['refund_time'];
  2081. //退补支付方式
  2082. if($v['refund_type'] == 1){
  2083. $v['refund_type'] = '微信支付';
  2084. }elseif($v['refund_type'] == 2){
  2085. $v['refund_type'] = '付款码支付';
  2086. }elseif($v['refund_type'] == 3){
  2087. $v['refund_type'] = '个体户支付';
  2088. }elseif($v['refund_type'] == 4){
  2089. $v['refund_type'] = '充值卡支付';
  2090. }else{
  2091. $v['refund_type'] = '';
  2092. }
  2093. $v['warehouse'] = $wareArr[$v['warehouse']];
  2094. }
  2095. $indexKey = ['team_name','ctime','id','receivedAmount', 'payment_type','refund_price', 'refund_type', 'warehouse'];
  2096. $title = ['团队', '退补时间', '订单编号', '成交金额', '支付方式', '退补差价', '退补支付方式', '发货仓库'];
  2097. $filename = 'tuibumingxi_'.date('Y-m-d_H').'.xlsx';
  2098. return Order::export_excel($result, $filename, $indexKey, $title);
  2099. }
  2100. /**
  2101. * 发货财务明细
  2102. */
  2103. public function sendFinanceList_export(Request $request){
  2104. $team_id = (int)$request->input('team_id');
  2105. $stime = $request->input('stime');
  2106. $etime = $request->input('etime');
  2107. $warehouse = (int)$request->input('warehouse');
  2108. $teams = DB::table('teams')->lists('name', 'id');
  2109. $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){
  2110. if($team_id) $query->where('team_id', $team_id);
  2111. if($warehouse) $query->where('warehouse', $warehouse);
  2112. if($stime ) $query->where('send_time', '>=', $stime);
  2113. if($etime ) $query->where('send_time', '<=', $etime);
  2114. })->where('is_del', 0)->where('logistics_id', '>', '');
  2115. $result = $resultQuery->orderBy('send_time','desc')->get();
  2116. $result = json_decode(json_encode($result), true);
  2117. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  2118. $wareArr = $this->warehouse;
  2119. foreach($result as $k=>&$v){
  2120. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  2121. $v['payment_type'] = $payArr[$v['payment_type']];
  2122. $v['ctime'] = $v['send_time'];
  2123. $v['warehouse'] = $wareArr[$v['warehouse']];
  2124. }
  2125. $indexKey = ['team_name','ctime','id','receivedAmount', 'cost', 'freight_cost', 'payment_type', 'warehouse', 'receiverName', 'logistics_id'];
  2126. $title = ['团队', '发货时间', '订单编号', '成交金额','货品成本', '物流成本', '支付方式', '发货仓库', '收货人', '物流单号'];
  2127. $filename = 'fahuomingxi_'.date('Y-m-d_H').'.xlsx';
  2128. return Order::export_excel($result, $filename, $indexKey, $title);
  2129. }
  2130. /**
  2131. * 团队订单财务汇总列表
  2132. */
  2133. public function teamFinanceTotal2(Request $request){
  2134. $team_id = (int)$request->input('team_id');
  2135. $stime = $request->input('stime');
  2136. $etime = $request->input('etime');
  2137. $aftersale = (int)$request->input('aftersale');
  2138. $page = (int)$request->input('page');
  2139. $pageSize = 20;
  2140. if($page<=0){
  2141. $page = 1;
  2142. }
  2143. $offset = ($page-1) * $pageSize;
  2144. if($aftersale == 1){
  2145. $countQuery = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id'));
  2146. }else{
  2147. $countQuery = Order::select(DB::raw('left(createTime, 10) as ctime, team_id'));
  2148. }
  2149. $count = $countQuery->where(function($query) use($team_id, $stime, $etime, $aftersale){
  2150. if($team_id) $query->where('team_id', $team_id);
  2151. if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
  2152. if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
  2153. if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
  2154. if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  2155. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  2156. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->get();
  2157. $count = count($count);
  2158. if ($count > 1) {
  2159. // 总页数
  2160. $pages = ceil($count/$pageSize);
  2161. }else{
  2162. // 总页数
  2163. $pages = 1;
  2164. }
  2165. $teams = DB::table('teams')->lists('name', 'id');
  2166. if($aftersale == 1){
  2167. $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){
  2168. if($team_id) $query->where('team_id', $team_id);
  2169. if($stime) $query->where('aftersale_time', '>=', $stime);
  2170. if($etime) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  2171. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  2172. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('aftersale_time','desc')->offset($offset)->limit($pageSize)->get();
  2173. }else{
  2174. $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){
  2175. if($team_id) $query->where('team_id', $team_id);
  2176. if($stime) $query->where('createTime', '>=', $stime);
  2177. if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
  2178. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  2179. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
  2180. }
  2181. $result = json_decode(json_encode($result), true);
  2182. foreach($result as $k=>&$v){
  2183. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
  2184. $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
  2185. $v['zfm_money'] = $v['receivedAmount'] - $v['wx_money'];
  2186. }
  2187. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  2188. $teamList = json_decode(json_encode($teamList), true);
  2189. return view('/statistics/teamFinanceTotal',[
  2190. 'result' => $result,
  2191. 'team_id' => $team_id,
  2192. 'stime' => $stime,
  2193. 'etime' => $etime,
  2194. 'page' => $page,
  2195. 'count' => $count,
  2196. 'pages' => $pages,
  2197. 'teamlist' => $teamList,
  2198. 'aftersale' => $aftersale,
  2199. ]);
  2200. }
  2201. /**
  2202. * 团队财务明细导出
  2203. */
  2204. public function teamFinanceList_export2(Request $request){
  2205. $team_id = (int)$request->input('team_id');
  2206. $stime = $request->input('stime');
  2207. $etime = $request->input('etime');
  2208. $aftersale = $request->input('aftersale');
  2209. $teams = DB::table('teams')->lists('name', 'id');
  2210. $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){
  2211. if($team_id) $query->where('team_id', $team_id);
  2212. if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
  2213. if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
  2214. if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
  2215. if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  2216. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  2217. })->where('is_del', 0)->orderBy('team_id', 'asc');
  2218. if($aftersale == 1){
  2219. $result = $resultQuery->orderBy('aftersale_time','desc')->get();
  2220. }else{
  2221. $result = $resultQuery->orderBy('createTime','desc')->get();
  2222. }
  2223. $result = json_decode(json_encode($result), true);
  2224. $payArr = [1=>'微信支付', 2=>'付款码支付', 3=>'个体户支付', 4=>'充值卡支付'];
  2225. foreach($result as $k=>&$v){
  2226. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  2227. $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
  2228. $v['payment_type'] = $payArr[$v['payment_type']];
  2229. }
  2230. if($aftersale == 0) $indexKey = ['team_name','createTime','id','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost', 'payment_type'];
  2231. if($aftersale == 1) $indexKey = ['team_name','aftersale_time','id','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost', 'payment_type'];
  2232. $title = ['团队', '时间', '订单编号', '成交金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本', '支付方式'];
  2233. $filename = $aftersale==0 ? 'dingdanmingxi'.date('Y-m-d_H').'.xlsx' : 'shouhoumingxi'.date('Y-m-d_H').'.xlsx';
  2234. return Order::export_excel($result, $filename, $indexKey, $title);
  2235. }
  2236. /**
  2237. * 团队财务汇总导出
  2238. */
  2239. public function teamFinanceTotal_export2(Request $request){
  2240. $team_id = (int)$request->input('team_id');
  2241. $stime = $request->input('stime');
  2242. $etime = $request->input('etime');
  2243. $aftersale = $request->input('aftersale');
  2244. $teams = DB::table('teams')->lists('name', 'id');
  2245. if($aftersale == 1){
  2246. $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){
  2247. if($team_id) $query->where('team_id', $team_id);
  2248. if($stime) $query->where('aftersale_time', '>=', $stime);
  2249. if($etime) $query->where('aftersale_time', '<=', $etime.' 23:59:59');
  2250. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  2251. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('aftersale_time','desc')->get();
  2252. }else{
  2253. $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){
  2254. if($team_id) $query->where('team_id', $team_id);
  2255. if($stime) $query->where('createTime', '>=', $stime);
  2256. if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
  2257. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  2258. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->get();
  2259. }
  2260. $result = json_decode(json_encode($result), true);
  2261. foreach($result as $k=>&$v){
  2262. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
  2263. $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
  2264. $v['zfm_money'] = $v['receivedAmount'] - $v['wx_money'];
  2265. }
  2266. if($aftersale==0){
  2267. $indexKey = ['team_name','ctime','order_count','receivedAmount','wx_money','zfm_money','aftersale_fee','refund_price','true_amount','cost','freight_cost'];
  2268. $title = ['团队', '时间', '总单数', '成交金额', '微信支付金额','付款码支付金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本'];
  2269. }else{
  2270. $indexKey = ['team_name','ctime','order_count','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost'];
  2271. $title = ['团队', '时间', '总单数', '成交金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本'];
  2272. }
  2273. $filename = $aftersale==0 ? 'dingdanhuizong_'.date('Y-m-d_H').'.xlsx' : 'shouhouhuizong_'.date('Y-m-d_H').'.xlsx';
  2274. return Order::export_excel($result, $filename, $indexKey, $title);
  2275. }
  2276. /**
  2277. * 地区汇总roi
  2278. */
  2279. public function districtTotalRoi(Request $request){
  2280. $page = (int)$request->input('page');
  2281. $pageSize = 20;
  2282. if($page<=0){
  2283. $page = 1;
  2284. }
  2285. $offset = ($page-1) * $pageSize;
  2286. $city = $request->input('city');
  2287. if($city !== null){
  2288. $city = str_replace('市', '', $city);
  2289. }
  2290. $ret = AdCost::select('id')->where(function($query) use($city){
  2291. if($city) $query->where('city', 'like', '%'.$city.'%');
  2292. })->groupBy('city')->get();
  2293. $ret = json_decode(json_encode($ret), true);
  2294. $count = count($ret);
  2295. if ($count > 1) {
  2296. // 总页数
  2297. $pages = ceil($count/$pageSize);
  2298. }else{
  2299. // 总页数
  2300. $pages = 1;
  2301. }
  2302. $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city'))->where(function($query) use($city){
  2303. if($city) $query->where('city', 'like', '%'.$city.'%');
  2304. })->groupBy('city')->offset($offset)->limit($pageSize)->get();
  2305. $result = json_decode(json_encode($result), true);
  2306. foreach($result as $k=>&$v){
  2307. //新粉收入
  2308. //加粉
  2309. $city_name = str_replace('市', '', $v['city']);
  2310. $phones = DB::table('customers')->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  2311. #订单总计:
  2312. $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();
  2313. #汇总新粉成单:
  2314. $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();
  2315. // 1.新粉成单数
  2316. $v['new_order_count'] = $new_order->order_count;
  2317. // 2.新粉成交额
  2318. $v['new_order_amount'] = $new_order->order_amount;
  2319. // 3.粉丝总成交额
  2320. $v['order_amount'] = $order->order_amount;
  2321. // 总单数
  2322. $v['order_count'] = $order->order_count;
  2323. //新粉roi
  2324. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  2325. //累计roi
  2326. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  2327. $v['new_fan_count'] = count($phones);
  2328. }
  2329. return view('statistics/districtTotalRoi', ['result' =>$result,
  2330. 'page' =>$page,
  2331. 'count' =>$count,
  2332. 'pages' =>$pages,
  2333. 'city' =>$city,
  2334. ]);
  2335. }
  2336. /**
  2337. * 地域roi7日
  2338. */
  2339. public function districtRoi7day(Request $request){
  2340. $page = (int)$request->input('page');
  2341. $pageSize = 20;
  2342. if($page<=0){
  2343. $page = 1;
  2344. }
  2345. $offset = ($page-1) * $pageSize;
  2346. $stime = $request->input('stime');
  2347. $etime = $request->input('etime');
  2348. $city = $request->input('city');
  2349. if($city !== null){
  2350. $city = str_replace('市', '', $city);
  2351. }
  2352. $count = DistrictRoi7::where(function($query) use($stime, $etime, $city){
  2353. if($stime) $query->where('ad_time', '>=', $stime);
  2354. if($etime) $query->where('ad_time', '<=', $etime);
  2355. if($city) $query->where('city', 'like', '%'.$city.'%');
  2356. })->count();
  2357. if ($count > 1) {
  2358. // 总页数
  2359. $pages = ceil($count/$pageSize);
  2360. }else{
  2361. // 总页数
  2362. $pages = 1;
  2363. }
  2364. $result = DistrictRoi7::where(function($query) use($stime, $etime, $city){
  2365. if($stime) $query->where('ad_time', '>=', $stime);
  2366. if($etime) $query->where('ad_time', '<=', $etime);
  2367. if($city) $query->where('city', 'like', '%'.$city.'%');
  2368. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  2369. $result = json_decode(json_encode($result), true);
  2370. foreach($result as $k=>&$v){
  2371. //新粉roi
  2372. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2373. //累计roi
  2374. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2375. }
  2376. return view('statistics/districtRoi7day', ['result' =>$result,
  2377. 'page' =>$page,
  2378. 'count' =>$count,
  2379. 'pages' =>$pages,
  2380. 'city' =>$city,
  2381. 'stime' =>$stime,
  2382. 'etime' =>$etime,
  2383. ]);
  2384. }
  2385. /**
  2386. * 地域roi7日
  2387. */
  2388. public function districtRoi7day_export(Request $request){
  2389. $stime = $request->input('stime');
  2390. $etime = $request->input('etime');
  2391. $city = $request->input('city');
  2392. if($city !== null){
  2393. $city = str_replace('市', '', $city);
  2394. }
  2395. $result = DistrictRoi7::where(function($query) use($stime, $etime, $city){
  2396. if($stime) $query->where('ad_time', '>=', $stime);
  2397. if($etime) $query->where('ad_time', '<=', $etime);
  2398. if($city) $query->where('city', 'like', '%'.$city.'%');
  2399. })->orderBy('id', 'desc')->get();
  2400. $result = json_decode(json_encode($result), true);
  2401. foreach($result as $k=>&$v){
  2402. //新粉roi
  2403. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2404. //累计roi
  2405. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2406. }
  2407. $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'];
  2408. $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '7日累计成单数', '7日累计收入', '7日累计ROI'];
  2409. $filename = 'districtRoi7day_'.date('Y-m-d_H').'.xlsx';
  2410. return Order::export_excel($result, $filename, $indexKey, $title);
  2411. }
  2412. /**
  2413. * 地域roi7日
  2414. */
  2415. public function districtRoi7dayTotal(Request $request){
  2416. $page = (int)$request->input('page');
  2417. $pageSize = 20;
  2418. if($page<=0){
  2419. $page = 1;
  2420. }
  2421. $offset = ($page-1) * $pageSize;
  2422. $city = $request->input('city');
  2423. if($city !== null){
  2424. $city = str_replace('市', '', $city);
  2425. }
  2426. $count = DistrictRoi7::where(function($query) use($city){
  2427. if($city) $query->where('city', 'like', '%'.$city.'%');
  2428. })->groupBy('city')->get();
  2429. $count = count($count);
  2430. if ($count > 1) {
  2431. // 总页数
  2432. $pages = ceil($count/$pageSize);
  2433. }else{
  2434. // 总页数
  2435. $pages = 1;
  2436. }
  2437. $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){
  2438. if($city) $query->where('city', 'like', '%'.$city.'%');
  2439. })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
  2440. $result = json_decode(json_encode($result), true);
  2441. foreach($result as $k=>&$v){
  2442. //新粉roi
  2443. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2444. //累计roi
  2445. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2446. //数据格式化
  2447. $v['order_amount'] = round($v['sum_order_amount'],2);
  2448. }
  2449. return view('statistics/districtRoi7dayTotal', ['result' =>$result,
  2450. 'page' =>$page,
  2451. 'count' =>$count,
  2452. 'pages' =>$pages,
  2453. 'city' =>$city,
  2454. ]);
  2455. }
  2456. /**
  2457. * 地域roi7日
  2458. */
  2459. public function districtRoi7dayTotal_export(Request $request){
  2460. $city = $request->input('city');
  2461. if($city !== null){
  2462. $city = str_replace('市', '', $city);
  2463. }
  2464. $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){
  2465. if($city) $query->where('city', 'like', '%'.$city.'%');
  2466. })->groupBy('city')->orderBy('roi', 'desc')->get();
  2467. $result = json_decode(json_encode($result), true);
  2468. foreach($result as $k=>&$v){
  2469. //新粉roi
  2470. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2471. //累计roi
  2472. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2473. //数据格式化
  2474. $v['order_amount'] = round($v['sum_order_amount'],2);
  2475. }
  2476. $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
  2477. $title = ['城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '7日累计成单数', '7日累计收入', '7日累计ROI'];
  2478. $filename = 'districtRoi7day_'.date('Y-m-d_H').'.xlsx';
  2479. return Order::export_excel($result, $filename, $indexKey, $title);
  2480. }
  2481. /**
  2482. * 地域roi30日
  2483. */
  2484. public function districtRoi30day(Request $request){
  2485. $page = (int)$request->input('page');
  2486. $pageSize = 20;
  2487. if($page<=0){
  2488. $page = 1;
  2489. }
  2490. $offset = ($page-1) * $pageSize;
  2491. $stime = $request->input('stime');
  2492. $etime = $request->input('etime');
  2493. $city = $request->input('city');
  2494. if($city !== null){
  2495. $city = str_replace('市', '', $city);
  2496. }
  2497. $count = districtRoi30::where(function($query) use($stime, $etime, $city){
  2498. if($stime) $query->where('ad_time', '>=', $stime);
  2499. if($etime) $query->where('ad_time', '<=', $etime);
  2500. if($city) $query->where('city', 'like', '%'.$city.'%');
  2501. })->count();
  2502. if ($count > 1) {
  2503. // 总页数
  2504. $pages = ceil($count/$pageSize);
  2505. }else{
  2506. // 总页数
  2507. $pages = 1;
  2508. }
  2509. $result = districtRoi30::where(function($query) use($stime, $etime, $city){
  2510. if($stime) $query->where('ad_time', '>=', $stime);
  2511. if($etime) $query->where('ad_time', '<=', $etime);
  2512. if($city) $query->where('city', 'like', '%'.$city.'%');
  2513. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  2514. $result = json_decode(json_encode($result), true);
  2515. foreach($result as $k=>&$v){
  2516. //新粉roi
  2517. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2518. //累计roi
  2519. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2520. }
  2521. return view('statistics/districtRoi30day', ['result' =>$result,
  2522. 'page' =>$page,
  2523. 'count' =>$count,
  2524. 'pages' =>$pages,
  2525. 'city' =>$city,
  2526. 'stime' =>$stime,
  2527. 'etime' =>$etime,
  2528. ]);
  2529. }
  2530. /**
  2531. * 地域roi30日
  2532. */
  2533. public function districtRoi30day_export(Request $request){
  2534. $stime = $request->input('stime');
  2535. $etime = $request->input('etime');
  2536. $city = $request->input('city');
  2537. if($city !== null){
  2538. $city = str_replace('市', '', $city);
  2539. }
  2540. $result = districtRoi30::where(function($query) use($stime, $etime, $city){
  2541. if($stime) $query->where('ad_time', '>=', $stime);
  2542. if($etime) $query->where('ad_time', '<=', $etime);
  2543. if($city) $query->where('city', 'like', '%'.$city.'%');
  2544. })->orderBy('id', 'desc')->get();
  2545. $result = json_decode(json_encode($result), true);
  2546. foreach($result as $k=>&$v){
  2547. //新粉roi
  2548. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2549. //累计roi
  2550. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2551. }
  2552. $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'];
  2553. $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  2554. $filename = 'districtRoi30day_'.date('Y-m-d_H').'.xlsx';
  2555. return Order::export_excel($result, $filename, $indexKey, $title);
  2556. }
  2557. /**
  2558. * 地域roi30日汇总
  2559. */
  2560. public function districtRoi30dayTotal(Request $request){
  2561. $page = (int)$request->input('page');
  2562. $pageSize = 20;
  2563. if($page<=0){
  2564. $page = 1;
  2565. }
  2566. $offset = ($page-1) * $pageSize;
  2567. $city = $request->input('city');
  2568. if($city !== null){
  2569. $city = str_replace('市', '', $city);
  2570. }
  2571. $count = districtRoi30::where(function($query) use($city){
  2572. if($city) $query->where('city', 'like', '%'.$city.'%');
  2573. })->groupBy('city')->get();
  2574. $count = count($count);
  2575. if ($count > 1) {
  2576. // 总页数
  2577. $pages = ceil($count/$pageSize);
  2578. }else{
  2579. // 总页数
  2580. $pages = 1;
  2581. }
  2582. $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){
  2583. if($city) $query->where('city', 'like', '%'.$city.'%');
  2584. })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
  2585. $result = json_decode(json_encode($result), true);
  2586. foreach($result as $k=>&$v){
  2587. //新粉roi
  2588. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2589. //累计roi
  2590. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2591. //数据格式化
  2592. $v['order_amount'] = round($v['sum_order_amount'],2);
  2593. }
  2594. return view('statistics/districtRoi30dayTotal', ['result' =>$result,
  2595. 'page' =>$page,
  2596. 'count' =>$count,
  2597. 'pages' =>$pages,
  2598. 'city' =>$city,
  2599. ]);
  2600. }
  2601. /**
  2602. * 地域roi30日
  2603. */
  2604. public function districtRoi30dayTotal_export(Request $request){
  2605. $city = $request->input('city');
  2606. if($city !== null){
  2607. $city = str_replace('市', '', $city);
  2608. }
  2609. $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){
  2610. if($city) $query->where('city', 'like', '%'.$city.'%');
  2611. })->groupBy('city')->orderBy('roi', 'desc')->get();
  2612. $result = json_decode(json_encode($result), true);
  2613. foreach($result as $k=>&$v){
  2614. //新粉roi
  2615. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2616. //累计roi
  2617. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2618. //数据格式化
  2619. $v['order_amount'] = round($v['sum_order_amount'],2);
  2620. }
  2621. $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
  2622. $title = ['城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  2623. $filename = 'districtRoi30day_'.date('Y-m-d_H').'.xlsx';
  2624. return Order::export_excel($result, $filename, $indexKey, $title);
  2625. }
  2626. /**
  2627. * 地域roi15日
  2628. */
  2629. public function districtRoi15day(Request $request){
  2630. $page = (int)$request->input('page');
  2631. $pageSize = 20;
  2632. if($page<=0){
  2633. $page = 1;
  2634. }
  2635. $offset = ($page-1) * $pageSize;
  2636. $stime = $request->input('stime');
  2637. $etime = $request->input('etime');
  2638. $city = $request->input('city');
  2639. if($city !== null){
  2640. $city = str_replace('市', '', $city);
  2641. }
  2642. $count = districtRoi15::where(function($query) use($stime, $etime, $city){
  2643. if($stime) $query->where('ad_time', '>=', $stime);
  2644. if($etime) $query->where('ad_time', '<=', $etime);
  2645. if($city) $query->where('city', 'like', '%'.$city.'%');
  2646. })->count();
  2647. if ($count > 1) {
  2648. // 总页数
  2649. $pages = ceil($count/$pageSize);
  2650. }else{
  2651. // 总页数
  2652. $pages = 1;
  2653. }
  2654. $result = districtRoi15::where(function($query) use($stime, $etime, $city){
  2655. if($stime) $query->where('ad_time', '>=', $stime);
  2656. if($etime) $query->where('ad_time', '<=', $etime);
  2657. if($city) $query->where('city', 'like', '%'.$city.'%');
  2658. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  2659. $result = json_decode(json_encode($result), true);
  2660. foreach($result as $k=>&$v){
  2661. //新粉roi
  2662. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2663. //累计roi
  2664. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2665. }
  2666. return view('statistics/districtRoi15day', ['result' =>$result,
  2667. 'page' =>$page,
  2668. 'count' =>$count,
  2669. 'pages' =>$pages,
  2670. 'city' =>$city,
  2671. 'stime' =>$stime,
  2672. 'etime' =>$etime,
  2673. ]);
  2674. }
  2675. /**
  2676. * 地域roi15日
  2677. */
  2678. public function districtRoi15day_export(Request $request){
  2679. $stime = $request->input('stime');
  2680. $etime = $request->input('etime');
  2681. $city = $request->input('city');
  2682. if($city !== null){
  2683. $city = str_replace('市', '', $city);
  2684. }
  2685. $result = districtRoi15::where(function($query) use($stime, $etime, $city){
  2686. if($stime) $query->where('ad_time', '>=', $stime);
  2687. if($etime) $query->where('ad_time', '<=', $etime);
  2688. if($city) $query->where('city', 'like', '%'.$city.'%');
  2689. })->orderBy('id', 'desc')->get();
  2690. $result = json_decode(json_encode($result), true);
  2691. foreach($result as $k=>&$v){
  2692. //新粉roi
  2693. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2694. //累计roi
  2695. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2696. }
  2697. $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'];
  2698. $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  2699. $filename = 'districtRoi15day_'.date('Y-m-d_H').'.xlsx';
  2700. return Order::export_excel($result, $filename, $indexKey, $title);
  2701. }
  2702. /**
  2703. * 地域roi15日汇总
  2704. */
  2705. public function districtRoi15dayTotal(Request $request){
  2706. $page = (int)$request->input('page');
  2707. $pageSize = 20;
  2708. if($page<=0){
  2709. $page = 1;
  2710. }
  2711. $offset = ($page-1) * $pageSize;
  2712. $city = $request->input('city');
  2713. if($city !== null){
  2714. $city = str_replace('市', '', $city);
  2715. }
  2716. $count = districtRoi15::where(function($query) use($city){
  2717. if($city) $query->where('city', 'like', '%'.$city.'%');
  2718. })->groupBy('city')->get();
  2719. $count = count($count);
  2720. if ($count > 1) {
  2721. // 总页数
  2722. $pages = ceil($count/$pageSize);
  2723. }else{
  2724. // 总页数
  2725. $pages = 1;
  2726. }
  2727. $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){
  2728. if($city) $query->where('city', 'like', '%'.$city.'%');
  2729. })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
  2730. $result = json_decode(json_encode($result), true);
  2731. foreach($result as $k=>&$v){
  2732. //新粉roi
  2733. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2734. //累计roi
  2735. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2736. //数据格式化
  2737. $v['order_amount'] = round($v['sum_order_amount'],2);
  2738. }
  2739. return view('statistics/districtRoi15dayTotal', ['result' =>$result,
  2740. 'page' =>$page,
  2741. 'count' =>$count,
  2742. 'pages' =>$pages,
  2743. 'city' =>$city,
  2744. ]);
  2745. }
  2746. /**
  2747. * 地域roi15日
  2748. */
  2749. public function districtRoi15dayTotal_export(Request $request){
  2750. $city = $request->input('city');
  2751. if($city !== null){
  2752. $city = str_replace('市', '', $city);
  2753. }
  2754. $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){
  2755. if($city) $query->where('city', 'like', '%'.$city.'%');
  2756. })->groupBy('city')->orderBy('roi', 'desc')->get();
  2757. $result = json_decode(json_encode($result), true);
  2758. foreach($result as $k=>&$v){
  2759. //新粉roi
  2760. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2761. //累计roi
  2762. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2763. //数据格式化
  2764. $v['order_amount'] = round($v['sum_order_amount'],2);
  2765. }
  2766. $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
  2767. $title = ['城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  2768. $filename = 'districtRoi15day_'.date('Y-m-d_H').'.xlsx';
  2769. return Order::export_excel($result, $filename, $indexKey, $title);
  2770. }
  2771. /**
  2772. * 地域roi45日
  2773. */
  2774. public function districtRoi45day(Request $request){
  2775. $page = (int)$request->input('page');
  2776. $pageSize = 20;
  2777. if($page<=0){
  2778. $page = 1;
  2779. }
  2780. $offset = ($page-1) * $pageSize;
  2781. $stime = $request->input('stime');
  2782. $etime = $request->input('etime');
  2783. $city = $request->input('city');
  2784. if($city !== null){
  2785. $city = str_replace('市', '', $city);
  2786. }
  2787. $count = districtRoi45::where(function($query) use($stime, $etime, $city){
  2788. if($stime) $query->where('ad_time', '>=', $stime);
  2789. if($etime) $query->where('ad_time', '<=', $etime);
  2790. if($city) $query->where('city', 'like', '%'.$city.'%');
  2791. })->count();
  2792. if ($count > 1) {
  2793. // 总页数
  2794. $pages = ceil($count/$pageSize);
  2795. }else{
  2796. // 总页数
  2797. $pages = 1;
  2798. }
  2799. $result = districtRoi45::where(function($query) use($stime, $etime, $city){
  2800. if($stime) $query->where('ad_time', '>=', $stime);
  2801. if($etime) $query->where('ad_time', '<=', $etime);
  2802. if($city) $query->where('city', 'like', '%'.$city.'%');
  2803. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  2804. $result = json_decode(json_encode($result), true);
  2805. foreach($result as $k=>&$v){
  2806. //新粉roi
  2807. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2808. //累计roi
  2809. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2810. }
  2811. return view('statistics/districtRoi45day', ['result' =>$result,
  2812. 'page' =>$page,
  2813. 'count' =>$count,
  2814. 'pages' =>$pages,
  2815. 'city' =>$city,
  2816. 'stime' =>$stime,
  2817. 'etime' =>$etime,
  2818. ]);
  2819. }
  2820. /**
  2821. * 地域roi45日
  2822. */
  2823. public function districtRoi45day_export(Request $request){
  2824. $stime = $request->input('stime');
  2825. $etime = $request->input('etime');
  2826. $city = $request->input('city');
  2827. if($city !== null){
  2828. $city = str_replace('市', '', $city);
  2829. }
  2830. $result = districtRoi45::where(function($query) use($stime, $etime, $city){
  2831. if($stime) $query->where('ad_time', '>=', $stime);
  2832. if($etime) $query->where('ad_time', '<=', $etime);
  2833. if($city) $query->where('city', 'like', '%'.$city.'%');
  2834. })->orderBy('id', 'desc')->get();
  2835. $result = json_decode(json_encode($result), true);
  2836. foreach($result as $k=>&$v){
  2837. //新粉roi
  2838. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2839. //累计roi
  2840. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2841. }
  2842. $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'];
  2843. $title = ['日期', '城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  2844. $filename = 'districtRoi45day_'.date('Y-m-d_H').'.xlsx';
  2845. return Order::export_excel($result, $filename, $indexKey, $title);
  2846. }
  2847. /**
  2848. * 地域roi45日汇总
  2849. */
  2850. public function districtRoi45dayTotal(Request $request){
  2851. $page = (int)$request->input('page');
  2852. $pageSize = 20;
  2853. if($page<=0){
  2854. $page = 1;
  2855. }
  2856. $offset = ($page-1) * $pageSize;
  2857. $city = $request->input('city');
  2858. if($city !== null){
  2859. $city = str_replace('市', '', $city);
  2860. }
  2861. $count = districtRoi45::where(function($query) use($city){
  2862. if($city) $query->where('city', 'like', '%'.$city.'%');
  2863. })->groupBy('city')->get();
  2864. $count = count($count);
  2865. if ($count > 1) {
  2866. // 总页数
  2867. $pages = ceil($count/$pageSize);
  2868. }else{
  2869. // 总页数
  2870. $pages = 1;
  2871. }
  2872. $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){
  2873. if($city) $query->where('city', 'like', '%'.$city.'%');
  2874. })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
  2875. $result = json_decode(json_encode($result), true);
  2876. foreach($result as $k=>&$v){
  2877. //新粉roi
  2878. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2879. //累计roi
  2880. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2881. //数据格式化
  2882. $v['order_amount'] = round($v['sum_order_amount'],2);
  2883. }
  2884. return view('statistics/districtRoi45dayTotal', ['result' =>$result,
  2885. 'page' =>$page,
  2886. 'count' =>$count,
  2887. 'pages' =>$pages,
  2888. 'city' =>$city,
  2889. ]);
  2890. }
  2891. /**
  2892. * 地域roi45日
  2893. */
  2894. public function districtRoi45dayTotal_export(Request $request){
  2895. $city = $request->input('city');
  2896. if($city !== null){
  2897. $city = str_replace('市', '', $city);
  2898. }
  2899. $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){
  2900. if($city) $query->where('city', 'like', '%'.$city.'%');
  2901. })->groupBy('city')->orderBy('roi', 'desc')->get();
  2902. $result = json_decode(json_encode($result), true);
  2903. foreach($result as $k=>&$v){
  2904. //新粉roi
  2905. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2906. //累计roi
  2907. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2908. //数据格式化
  2909. $v['order_amount'] = round($v['sum_order_amount'],2);
  2910. }
  2911. $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
  2912. $title = ['城市', '投放成本', '加粉数', '下单人数', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  2913. $filename = 'districtRoi45day_'.date('Y-m-d_H').'.xlsx';
  2914. return Order::export_excel($result, $filename, $indexKey, $title);
  2915. }
  2916. /**
  2917. * 销售月度业绩报表
  2918. */
  2919. public function salerBonus(Request $request){
  2920. $team_id = (int)$request->input('team_id');
  2921. $admin_id = (int)$request->input('admin_id');
  2922. $mstime = $request->input('stime');
  2923. $metime = $request->input('etime');
  2924. if($mstime){
  2925. $stime = date('Y-m-01', strtotime($mstime));
  2926. }else{
  2927. $stime = null;
  2928. }
  2929. if($metime){
  2930. $etime = date('Y-m-01', strtotime($metime. ' 1 month'));
  2931. }else{
  2932. $etime = null;
  2933. }
  2934. $self_role = session('role_name');
  2935. if($self_role == '销售' || $self_role == '分销销售'){
  2936. $admin_id = session('admin_id');
  2937. }
  2938. if($self_role == '管理员'){
  2939. //只能看自己团队的
  2940. $self_id = session('admin_id');
  2941. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  2942. }
  2943. //假如有团队筛选,检索销售队员
  2944. $sale_ids = null;
  2945. if($team_id>0 && !$admin_id){
  2946. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  2947. }
  2948. $page = (int)$request->input('page');
  2949. $pageSize = 20;
  2950. if($page<=0){
  2951. $page = 1;
  2952. }
  2953. $offset = ($page-1) * $pageSize;
  2954. $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){
  2955. if($admin_id) $query->where('admin_id', $admin_id);
  2956. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  2957. if(!empty($stime)) $query->where('createTime', '>=', $stime);
  2958. if(!empty($etime)) $query->where('createTime', '<', $etime);
  2959. })->groupBy('month')->groupBy('admin_id')->get();
  2960. $count = count($ret);
  2961. if ($count > 1) {
  2962. // 总页数
  2963. $pages = ceil($count/$pageSize);
  2964. }else{
  2965. // 总页数
  2966. $pages = 1;
  2967. }
  2968. $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){
  2969. if($admin_id) $query->where('admin_id', $admin_id);
  2970. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  2971. if(!empty($stime)) $query->where('createTime', '>=', $stime);
  2972. if(!empty($etime)) $query->where('createTime', '<', $etime);
  2973. })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->offset($offset)->limit($pageSize)->get();
  2974. $result = json_decode(json_encode($result), true);
  2975. foreach($result as $k=>&$v){
  2976. $m_stime = date('Y-m-01', strtotime($v['month']));
  2977. $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
  2978. //加粉数
  2979. $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();
  2980. $v['fan_count'] = $fan_count->fan_count;
  2981. $v['admin_name'] = $fan_count->admin_name;
  2982. if(empty($v['admin_name'])){
  2983. $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
  2984. }
  2985. #计算提成
  2986. $v['bonus'] = $this->getSalerBonus($v['order_amount']);
  2987. }
  2988. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
  2989. if($self_role == '管理员') $query->where('id', $team_id);
  2990. })->where('type', 1)->get();
  2991. $teamList = json_decode(json_encode($teamList), true);
  2992. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
  2993. if($self_role == '管理员') $query->where('team_id', $team_id);
  2994. })->where('id','>', 1)->where('is_use', 1)->get();
  2995. $adminList = json_decode(json_encode($adminList), true);
  2996. return view('statistics/salerBonus', ['result'=>$result,
  2997. 'page' =>$page,
  2998. 'count' =>$count,
  2999. 'pages' =>$pages,
  3000. 'teamlist' =>$teamList,
  3001. 'adminlist' =>$adminList,
  3002. 'team_id' =>$team_id,
  3003. 'admin_id' =>$admin_id,
  3004. 'stime' =>$mstime,
  3005. 'etime' =>$metime,
  3006. 'self_role' =>$self_role,
  3007. ]);
  3008. }
  3009. /**
  3010. * 计算提成
  3011. */
  3012. public function getSalerBonus($amount){
  3013. $result = [
  3014. 0 => 0, //总的
  3015. 1 => 0,
  3016. 2 => 0,
  3017. 3 => 0
  3018. ];
  3019. if($amount>150000){
  3020. $result[1] = round(100000 * 0.03);
  3021. $result[2] = round(50000 * 0.04);
  3022. $result[3] = round( ($amount - 150000) * 0.05 , 2);
  3023. $result[0] = $result[1] + $result[2] + $result[3];
  3024. }elseif($amount>100000){
  3025. $result[1] = round(100000 * 0.03);
  3026. $result[2] = round(($amount - 100000) * 0.04, 2);
  3027. $result[0] = $result[1] + $result[2];
  3028. }else{
  3029. $result[1] = round($amount * 0.03, 2);
  3030. $result[0] = $result[1];
  3031. }
  3032. return $result;
  3033. }
  3034. /**
  3035. * 销售月度业绩报表
  3036. */
  3037. public function salerBonus_export(Request $request){
  3038. $team_id = (int)$request->input('team_id');
  3039. $admin_id = (int)$request->input('admin_id');
  3040. $stime = $request->input('stime');
  3041. $etime = $request->input('etime');
  3042. if($stime){
  3043. $stime = date('Y-m-01', strtotime($stime));
  3044. }
  3045. if($etime){
  3046. $etime = date('Y-m-01', strtotime($etime. ' 1 month'));
  3047. }
  3048. $self_role = session('role_name');
  3049. if($self_role == '销售' || $self_role == '分销销售'){
  3050. $admin_id = session('admin_id');
  3051. }
  3052. if($self_role == '管理员'){
  3053. //只能看自己团队的
  3054. $self_id = session('admin_id');
  3055. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  3056. }
  3057. //假如有团队筛选,检索销售队员
  3058. $sale_ids = null;
  3059. if($team_id>0 && !$admin_id){
  3060. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  3061. }
  3062. $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){
  3063. if($admin_id) $query->where('admin_id', $admin_id);
  3064. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  3065. if(!empty($stime)) $query->where('createTime', '>=', $stime);
  3066. if(!empty($etime)) $query->where('createTime', '<', $etime);
  3067. })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->get();
  3068. $result = json_decode(json_encode($result), true);
  3069. foreach($result as $k=>&$v){
  3070. $m_stime = date('Y-m-01', strtotime($v['month']));
  3071. $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
  3072. //加粉数
  3073. $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();
  3074. $v['fan_count'] = $fan_count->fan_count;
  3075. $v['admin_name'] = $fan_count->admin_name;
  3076. if(empty($v['admin_name'])){
  3077. $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('realname');
  3078. }
  3079. #计算提成
  3080. $bonus = $this->getSalerBonus($v['order_amount']);
  3081. $v['bonus0'] = $bonus[0];
  3082. $v['bonus1'] = $bonus[1];
  3083. $v['bonus2'] = $bonus[2];
  3084. $v['bonus3'] = $bonus[3];
  3085. }
  3086. $indexKey = ['month','admin_name','order_count','order_amount','fan_count','bonus0','bonus1','bonus2','bonus3'];
  3087. $title = ['月份', '销售名', '总单量', '总成交额', '总加粉数', '总提成', '一阶提成', '二阶提成', '三阶提成'];
  3088. $filename = 'xiaoshouyj_'.date('Y-m-d_H').'.xlsx';
  3089. return Order::export_excel($result, $filename, $indexKey, $title);
  3090. }
  3091. /**
  3092. * 团队财务汇总时间格式处理
  3093. */
  3094. public function teamCtimePage($stime, $etime, $team_id, $offset, $pageSize, $sdate=''){
  3095. $result = array();
  3096. if( empty($sdate) ) $sdate = '2019-09-04';
  3097. $edate = date('Y-m-d');
  3098. if($stime && $stime>$sdate){
  3099. $sdate = $stime;
  3100. }
  3101. if($etime && $etime<$edate){
  3102. $edate = $etime;
  3103. }
  3104. $teams = Admin::getTeams();
  3105. if($team_id>0){
  3106. $teams = [$team_id];
  3107. }
  3108. $d = (int)(strtotime($edate) - strtotime($sdate)) / 86400 + 1;
  3109. for($i=$d-1; $i>=0; $i--){
  3110. $dtime = $sdate. '+'.$i.' day';
  3111. $date = date('Y-m-d', strtotime($dtime));
  3112. foreach($teams as $team_id){
  3113. $result[] = ['date'=>$date, 'team_id'=>$team_id];
  3114. }
  3115. }
  3116. $count = count($result);
  3117. $ret['data'] = array_slice($result, $offset, $pageSize);
  3118. $ret['count'] = $count;
  3119. return $ret;
  3120. }
  3121. /**
  3122. * 团队财务汇总时间格式处理
  3123. */
  3124. public function teamCtimePage_export($stime, $etime, $team_id, $sdate=''){
  3125. $result = array();
  3126. if( empty($sdate) ) $sdate = '2019-09-04';
  3127. $edate = date('Y-m-d');
  3128. if($stime && $stime>$sdate){
  3129. $sdate = $stime;
  3130. }
  3131. if($etime && $etime<$edate){
  3132. $edate = $etime;
  3133. }
  3134. $teams = Admin::getTeams();
  3135. if($team_id>0){
  3136. $teams = [$team_id];
  3137. }
  3138. $d = (int)(strtotime($edate) - strtotime($sdate)) / 86400 + 1;
  3139. for($i=$d-1; $i>=0; $i--){
  3140. $dtime = $sdate. '+'.$i.' day';
  3141. $date = date('Y-m-d', strtotime($dtime));
  3142. foreach($teams as $team_id){
  3143. $result[] = ['date'=>$date, 'team_id'=>$team_id];
  3144. }
  3145. }
  3146. return $result;
  3147. }
  3148. /**
  3149. * 投放数据时间格式 分页
  3150. */
  3151. public function throwCtimePage($stime, $etime, $offset='', $pageSize=''){
  3152. $result = array();
  3153. if( empty($sdate) ) $sdate = '2019-07-03';
  3154. $edate = date('Y-m-d', strtotime('-1 day'));
  3155. if($stime && $stime>$sdate){
  3156. $sdate = $stime;
  3157. }
  3158. if($etime && $etime<$edate){
  3159. $edate = $etime;
  3160. }
  3161. $d = (int)(strtotime($edate) - strtotime($sdate)) / 86400 + 1;
  3162. for($i=$d-1; $i>=0; $i--){
  3163. $dtime = $sdate. '+'.$i.' day';
  3164. $date = date('Y-m-d', strtotime($dtime));
  3165. $result[] = ['date'=>$date];
  3166. }
  3167. if( $pageSize>0 ){
  3168. $count = count($result);
  3169. $ret['data'] = array_slice($result, $offset, $pageSize);
  3170. $ret['count'] = $count;
  3171. }else{
  3172. $ret = $result;
  3173. }
  3174. return $ret;
  3175. }
  3176. /**
  3177. * 销售月业绩完成度报表
  3178. */
  3179. public function salerTargetProgress(Request $request){
  3180. $team_id = (int)$request->input('team_id');
  3181. $admin_id = (int)$request->input('admin_id');
  3182. $mstime = $request->input('stime');
  3183. $metime = $request->input('etime');
  3184. $self_role = session('role_name');
  3185. if($self_role == '销售' || $self_role == '分销销售'){
  3186. $admin_id = session('admin_id');
  3187. }
  3188. if($self_role == '管理员'){
  3189. //只能看自己团队的
  3190. $self_id = session('admin_id');
  3191. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  3192. }
  3193. //假如有团队筛选,检索销售队员
  3194. $sale_ids = null;
  3195. if($team_id>0 && !$admin_id){
  3196. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  3197. }
  3198. $page = (int)$request->input('page');
  3199. $pageSize = 20;
  3200. if($page<=0){
  3201. $page = 1;
  3202. }
  3203. $offset = ($page-1) * $pageSize;
  3204. $count = DB::table('saler_targets')->where(function($query) use($admin_id, $sale_ids, $mstime, $metime){
  3205. if($admin_id) $query->where('admin_id', $admin_id);
  3206. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  3207. if(!empty($mstime)) $query->where('month', '>=', $mstime);
  3208. if(!empty($metime)) $query->where('month', '<=', $metime);
  3209. })->count();
  3210. if ($count > 1) {
  3211. // 总页数
  3212. $pages = ceil($count/$pageSize);
  3213. }else{
  3214. // 总页数
  3215. $pages = 1;
  3216. }
  3217. $result = DB::table('saler_targets')->where(function($query) use($admin_id, $sale_ids, $mstime, $metime){
  3218. if($admin_id) $query->where('admin_id', $admin_id);
  3219. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  3220. if(!empty($mstime)) $query->where('month', '>=', $mstime);
  3221. if(!empty($metime)) $query->where('month', '<=', $metime);
  3222. })->orderBy('month', 'desc')->offset($offset)->limit($pageSize)->get();
  3223. if($mstime){
  3224. $stime = date('Y-m-01', strtotime($mstime));
  3225. }else{
  3226. $stime = null;
  3227. }
  3228. if($metime){
  3229. $etime = date('Y-m-01', strtotime($metime. ' 1 month'));
  3230. }else{
  3231. $etime = null;
  3232. }
  3233. //月份天数数组:
  3234. $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];
  3235. if(date('Y') % 4 == 0){
  3236. $m_day[2] = 29;
  3237. }
  3238. $result = json_decode(json_encode($result), true);
  3239. foreach($result as $k=>&$v){
  3240. $m_stime = date('Y-m-01', strtotime($v['month']));
  3241. $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
  3242. //总单数
  3243. $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();
  3244. $v['order_count'] = $order->order_count;
  3245. $v['order_amount'] = $order->order_amount;
  3246. //销售额完成率
  3247. $v['target_rate'] = $v['amount']>0 ? round( $order->order_amount / $v['amount'] , 4) : '';
  3248. //时间进度
  3249. //当前月
  3250. $tmonth = date('Y-m');
  3251. $m = (int)date('m');
  3252. $d = (int)date('d');
  3253. $days = $m_day[$m];
  3254. if( $v['month'] == $tmonth ){
  3255. $v['time_rate'] = round($d / $days, 4);
  3256. //剩余每天任务
  3257. if( $v['target_rate'] >= 1){
  3258. $v['day_remain'] = '';
  3259. }else{
  3260. $v['day_remain'] = round( ($v['amount'] - $order->order_amount) / $days, 4 );
  3261. }
  3262. }elseif( $v['month'] < $tmonth ){
  3263. $v['time_rate'] = 1;
  3264. $v['day_remain'] = '';
  3265. }elseif( $v['month'] > $tmonth ){
  3266. $v['time_rate'] = 0;
  3267. $v['day_remain'] = round( $v['amount'] / $days, 4 );
  3268. }
  3269. //粉数数据
  3270. $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();
  3271. $v['fan_count'] = $fan_count->fan_count;
  3272. $v['new_reply_count'] = $fan_count->new_reply_count;
  3273. $v['new_consult_count'] = $fan_count->new_consult_count;
  3274. $v['admin_name'] = $fan_count->admin_name;
  3275. if(empty($v['admin_name'])){
  3276. $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('username');
  3277. }
  3278. //新粉成单
  3279. $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();
  3280. $v['new_order_count'] = $new_order->order_count;
  3281. $v['new_order_amount'] = $new_order->order_amount;
  3282. //老粉成单
  3283. $v['old_order_count'] = $order->order_count - $new_order->order_count;
  3284. $v['old_order_amount'] = $order->order_amount - $new_order->order_amount;
  3285. //复购单数
  3286. //总单数
  3287. $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();
  3288. $v['fugou_order_count'] = $fugou_order->order_count;
  3289. $v['fugou_order_amount'] = $fugou_order->order_amount;
  3290. //新粉转化
  3291. $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();
  3292. $new_orderfan_count = count($phones);
  3293. $v['new_fan_rate'] = $fan_count->fan_count>0 ? round( $new_orderfan_count / $fan_count->fan_count, 4 ) : '';
  3294. //客单价
  3295. $v['order_amount_avg'] = $v['order_count'] > 0 ? round( $v['order_amount'] / $v['order_count'], 4 ) : '';
  3296. $v['new_orderfan_count'] = $new_orderfan_count;
  3297. }
  3298. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
  3299. if($self_role == '管理员') $query->where('id', $team_id);
  3300. })->where('type', 1)->get();
  3301. $teamList = json_decode(json_encode($teamList), true);
  3302. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
  3303. if($self_role == '管理员') $query->where('team_id', $team_id);
  3304. })->where('id','>', 1)->where('is_use', 1)->get();
  3305. $adminList = json_decode(json_encode($adminList), true);
  3306. return view('statistics/salerTargetProgress', ['result'=>$result,
  3307. 'page' =>$page,
  3308. 'count' =>$count,
  3309. 'pages' =>$pages,
  3310. 'teamlist' =>$teamList,
  3311. 'adminlist' =>$adminList,
  3312. 'team_id' =>$team_id,
  3313. 'admin_id' =>$admin_id,
  3314. 'stime' =>$mstime,
  3315. 'etime' =>$metime,
  3316. 'self_role' =>$self_role,
  3317. ]);
  3318. }
  3319. /**
  3320. * @return \Illuminate\View\View
  3321. */
  3322. public function salerTargetCreate()
  3323. {
  3324. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->get();
  3325. $adminList = json_decode(json_encode($adminList), true);
  3326. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  3327. $teamList = json_decode(json_encode($teamList), true);
  3328. return view('statistics/salerTargetCreate',['adminlist'=>$adminList, 'teamlist'=>$teamList]);
  3329. }
  3330. /**
  3331. * @param Request $request
  3332. * @return \Illuminate\Http\RedirectResponse
  3333. */
  3334. public function salerTargetStore(Request $request)
  3335. {
  3336. $admin_id = intval($request->input('admin_id'));
  3337. $this->validate($request, [
  3338. 'admin_id' => 'required|integer|min:1',
  3339. 'month' => 'required|unique:saler_targets,month,null,month,admin_id,'.$admin_id,
  3340. 'amount' => 'required',
  3341. ], [
  3342. 'admin_id.required' => '销售不能为空',
  3343. 'admin_id.min' => '销售不能为空',
  3344. 'month.required' => '日期不能为空',
  3345. 'month.unique' => '已经添加过',
  3346. 'amount.required' => '销售额不能为空',
  3347. ]);
  3348. $salerTargets = new SalerTargets();
  3349. $salerTargets->admin_id = intval($request->input('admin_id'));
  3350. $salerTargets->month = trim($request->input('month'));
  3351. $salerTargets->amount = trim($request->input('amount'));
  3352. $salerTargets->save();
  3353. return redirect('/admin/statistics/salerTargetProgress')->with('info', '添加成功');
  3354. }
  3355. /**
  3356. * @param $id
  3357. * @return \Illuminate\View\View
  3358. */
  3359. public function salerTargetEdit($id)
  3360. {
  3361. $salerTargets = salerTargetss::findOrFail($id);
  3362. return view('salerTargets/edit', ['salerTargets' => $salerTargets, 'id'=>$id]);
  3363. }
  3364. /**
  3365. * @param Request $request
  3366. * @return \Illuminate\Http\RedirectResponse
  3367. */
  3368. public function salerTargetUpdate(Request $request)
  3369. {
  3370. $id = (int)$request->input('id');
  3371. $this->validate($request, [
  3372. 'url' => 'required|unique:salerTargetss,url,'.$id.',id',
  3373. ], [
  3374. 'url.required' => '模板链接不能为空',
  3375. 'url.unique' => '该模板链接已存在',
  3376. ]);
  3377. $salerTargets = salerTargetss::findOrFail($id);
  3378. $salerTargets->url = trim($request->input('url'));
  3379. $salerTargets->note = trim($request->input('note'));
  3380. //图片上传 阿里云oss
  3381. if ($request->hasFile('img') && $request->file('img')->isValid()) {
  3382. $file = $request->file('img');
  3383. $ossClient=new oss();
  3384. // 上传阿里云
  3385. $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His'));
  3386. $img=$file['oss-request-url'];
  3387. $salerTargets->img=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img);
  3388. }
  3389. $salerTargets->save();
  3390. return redirect('/admin/salerTargets/index')->with('info', '修改模板成功');
  3391. }
  3392. /**
  3393. * 销售月业绩完成度报表导出
  3394. */
  3395. public function salerTargetProgress_export(Request $request){
  3396. $team_id = (int)$request->input('team_id');
  3397. $admin_id = (int)$request->input('admin_id');
  3398. $mstime = $request->input('stime');
  3399. $metime = $request->input('etime');
  3400. $self_role = session('role_name');
  3401. if($self_role == '销售' || $self_role == '分销销售'){
  3402. $admin_id = session('admin_id');
  3403. }
  3404. if($self_role == '管理员'){
  3405. //只能看自己团队的
  3406. $self_id = session('admin_id');
  3407. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  3408. }
  3409. //假如有团队筛选,检索销售队员
  3410. $sale_ids = null;
  3411. if($team_id>0 && !$admin_id){
  3412. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  3413. }
  3414. $result = DB::table('saler_targets')->where(function($query) use($admin_id, $sale_ids, $mstime, $metime){
  3415. if($admin_id) $query->where('admin_id', $admin_id);
  3416. if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  3417. if(!empty($mstime)) $query->where('month', '>=', $mstime);
  3418. if(!empty($metime)) $query->where('month', '<=', $metime);
  3419. })->orderBy('month', 'desc')->get();
  3420. if($mstime){
  3421. $stime = date('Y-m-01', strtotime($mstime));
  3422. }else{
  3423. $stime = null;
  3424. }
  3425. if($metime){
  3426. $etime = date('Y-m-01', strtotime($metime. ' 1 month'));
  3427. }else{
  3428. $etime = null;
  3429. }
  3430. //月份天数数组:
  3431. $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];
  3432. if(date('Y') % 4 == 0){
  3433. $m_day[2] = 29;
  3434. }
  3435. $result = json_decode(json_encode($result), true);
  3436. foreach($result as $k=>&$v){
  3437. $m_stime = date('Y-m-01', strtotime($v['month']));
  3438. $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
  3439. //总单数
  3440. $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();
  3441. $v['order_count'] = $order->order_count;
  3442. $v['order_amount'] = $order->order_amount;
  3443. //销售额完成率
  3444. $v['target_rate'] = $v['amount']>0 ? round( $order->order_amount / $v['amount'] , 4) : '';
  3445. //时间进度
  3446. //当前月
  3447. $tmonth = date('Y-m');
  3448. $m = date('m');
  3449. $d = date('d');
  3450. $days = $m_day[$m];
  3451. if( $v['month'] == $tmonth ){
  3452. $v['time_rate'] = round($d / $days, 4);
  3453. //剩余每天任务
  3454. if( $v['target_rate'] >= 1){
  3455. $v['day_remain'] = '';
  3456. }else{
  3457. $v['day_remain'] = round( ($v['amount'] - $order->order_amount) / $days, 4 );
  3458. }
  3459. }elseif( $v['month'] < $tmonth ){
  3460. $v['time_rate'] = 1;
  3461. $v['day_remain'] = '';
  3462. }elseif( $v['month'] > $tmonth ){
  3463. $v['time_rate'] = 0;
  3464. $v['day_remain'] = round( $v['amount'] / $days, 4 );
  3465. }
  3466. //粉数数据
  3467. $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();
  3468. $v['fan_count'] = $fan_count->fan_count;
  3469. $v['new_reply_count'] = $fan_count->new_reply_count;
  3470. $v['new_consult_count'] = $fan_count->new_consult_count;
  3471. $v['admin_name'] = $fan_count->admin_name;
  3472. if(empty($v['admin_name'])){
  3473. $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('username');
  3474. }
  3475. //新粉成单
  3476. $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();
  3477. $v['new_order_count'] = $new_order->order_count;
  3478. $v['new_order_amount'] = $new_order->order_amount;
  3479. //老粉成单
  3480. $v['old_order_count'] = $order->order_count - $new_order->order_count;
  3481. $v['old_order_amount'] = $order->order_amount - $new_order->order_amount;
  3482. //复购单数
  3483. //总单数
  3484. $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();
  3485. $v['fugou_order_count'] = $fugou_order->order_count;
  3486. $v['fugou_order_amount'] = $fugou_order->order_amount;
  3487. //新粉转化
  3488. $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();
  3489. $new_orderfan_count = count($phones);
  3490. $v['new_fan_rate'] = $fan_count->fan_count>0 ? round( $new_orderfan_count / $fan_count->fan_count, 4 ) : '';
  3491. //客单价
  3492. $v['order_amount_avg'] = $v['order_count'] > 0 ? round( $v['order_amount'] / $v['order_count'], 4 ) : '';
  3493. $v['new_orderfan_count'] = $new_orderfan_count;
  3494. }
  3495. $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'];
  3496. $title = ['销售', '月份', '总订单', '总销售额', '目标销售额','销售额完成率', '时间进度', '剩余每天销售任务', '粉丝数', '初步响应', '单品咨询', '新粉当日成单', '新粉流水', '老粉成单', '老粉流水', '复购成单', '复购流水', '总订单', '总流水', '新粉下单人数', '新粉丝转化', '客单价'];
  3497. $filename = 'xiaoshouyuebao_'.date('Y-m-d_H').'.xlsx';
  3498. return Order::export_excel($result, $filename, $indexKey, $title);
  3499. }
  3500. /**
  3501. * 仓库发货财务报表
  3502. */
  3503. public function warehouseFinanceList(Request $request){
  3504. $warehouse = (int)$request->input('warehouse');
  3505. $stime = $request->input('stime');
  3506. $etime = $request->input('etime');
  3507. $page = (int)$request->input('page');
  3508. $pageSize = 20;
  3509. if($page<=0){
  3510. $page = 1;
  3511. }
  3512. $offset = ($page-1) * $pageSize;
  3513. $count =Order::select('warehouse')->where(function($query) use($stime, $etime, $warehouse){
  3514. if($stime) $query->where('send_time', '>=', $stime);
  3515. if($etime) $query->where('send_time', '<=', $etime);
  3516. if($warehouse>0) $query->where('warehouse', '=', $warehouse);
  3517. })->whereNotNull('send_time')->where('warehouse','>',0)->where('logistics_id','>','')->where('is_del', 0)->groupBy('warehouse')->groupBy('send_time')->get();
  3518. $count = count($count);
  3519. if ($count > 1) {
  3520. // 总页数
  3521. $pages = ceil($count/$pageSize);
  3522. }else{
  3523. // 总页数
  3524. $pages = 1;
  3525. }
  3526. $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){
  3527. if($stime) $query->where('send_time', '>=', $stime);
  3528. if($etime) $query->where('send_time', '<=', $etime);
  3529. if($warehouse>0) $query->where('warehouse', '=', $warehouse);
  3530. })->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();
  3531. $wareArr = $this->warehouse;
  3532. foreach($result as $k=>&$item){
  3533. $item->warehouse = $wareArr[$item->warehouse];
  3534. }
  3535. //汇总
  3536. $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){
  3537. if($stime) $query->where('send_time', '>=', $stime);
  3538. if($etime) $query->where('send_time', '<=', $etime);
  3539. if($warehouse>0) $query->where('warehouse', '=', $warehouse);
  3540. })->whereNotNull('send_time')->where('warehouse','>',0)->where('logistics_id','>','')->where('is_del', 0)->first();
  3541. return view('statistics/warehouseFinanceList', ['result'=>$result,
  3542. 'page' => $page,
  3543. 'count' => $count,
  3544. 'pages' => $pages,
  3545. 'warehouse' => $warehouse,
  3546. 'stime' => $stime,
  3547. 'etime' => $etime,
  3548. 'total' => $total,
  3549. 'warehouseArr' => $this->warehouse,
  3550. ]);
  3551. }
  3552. /**
  3553. * 仓库发货财务报表导出
  3554. */
  3555. public function warehouseFinanceList_export(Request $request){
  3556. $warehouse = (int)$request->input('warehouse');
  3557. $stime = $request->input('stime');
  3558. $etime = $request->input('etime');
  3559. $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){
  3560. if($stime) $query->where('send_time', '>=', $stime);
  3561. if($etime) $query->where('send_time', '<=', $etime);
  3562. if($warehouse>0) $query->where('warehouse', '=', $warehouse);
  3563. })->whereNotNull('send_time')->where('warehouse','>',0)->where('logistics_id','>','')->where('is_del', 0)->groupBy('warehouse')->groupBy('send_time')->orderBy('send_time', 'desc')->get();
  3564. $wareArr = $this->warehouse;
  3565. foreach($result as $k=>&$item){
  3566. $item->warehouse = $wareArr[$item->warehouse];
  3567. }
  3568. $indexKey = ['send_time','warehouse','order_count','goods_cost','freight_cost'];
  3569. $title = ['日期', '仓库', '单数', '货品成本', '物流成本'];
  3570. $filename = 'cangkufahuo_'.date('Y-m-d_H').'.xlsx';
  3571. return Order::export_excel($result, $filename, $indexKey, $title);
  3572. }
  3573. /**
  3574. * 地域roi7 15,30,45,60日汇总
  3575. */
  3576. public function districtRoiAllTotal(Request $request){
  3577. $only_roi = (int)$request->input('only_roi');
  3578. $sort = (int)$request->input('sort'); //排序 0:7日降序 1:15日降序 2:30日降序 3:45日降序 4:60日降序
  3579. $page = (int)$request->input('page');
  3580. $pageSize = 20;
  3581. if($page<=0){
  3582. $page = 1;
  3583. }
  3584. $offset = ($page-1) * $pageSize;
  3585. $city = $request->input('city');
  3586. if($city){
  3587. $city = str_replace('市', '', $city); //单独搜城市不走redis 不用排序
  3588. }else{
  3589. //从redis取数据
  3590. $redis_key = 'seafood.districtRoiAllTotal';
  3591. $redisData = Redis::get($redis_key);
  3592. if(!empty($redisData)){
  3593. $result = json_decode($redisData, true);
  3594. switch ($sort) {
  3595. case '0':
  3596. $sort_c = 'total_roi';
  3597. break;
  3598. case '1':
  3599. $sort_c = 'total_roi15_sort';
  3600. break;
  3601. case '2':
  3602. $sort_c = 'total_roi30_sort';
  3603. break;
  3604. case '3':
  3605. $sort_c = 'total_roi45_sort';
  3606. break;
  3607. case '4':
  3608. $sort_c = 'total_roi60_sort';
  3609. break;
  3610. default:
  3611. $sort_c = 'total_roi';
  3612. break;
  3613. }
  3614. if($sort>0){
  3615. $sortArr=array_column($result, $sort_c);
  3616. array_multisort($sortArr, SORT_DESC, $result);
  3617. }
  3618. $count = count($result);
  3619. $result = array_slice($result, $offset, $pageSize);
  3620. if ($count > 1) {
  3621. // 总页数
  3622. $pages = ceil($count/$pageSize);
  3623. }else{
  3624. // 总页数
  3625. $pages = 1;
  3626. }
  3627. return view('statistics/districtRoiAllTotal', ['result' =>$result,
  3628. 'page' =>$page,
  3629. 'count' =>$count,
  3630. 'pages' =>$pages,
  3631. 'city' =>$city,
  3632. 'only_roi' =>$only_roi,
  3633. 'sort' =>$sort,
  3634. ]);
  3635. }
  3636. }
  3637. $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){
  3638. if($city) $query->where('city', 'like', $city.'%');
  3639. })->groupBy('city')->orderBy('roi', 'desc')->get();
  3640. $result = json_decode(json_encode($result), true);
  3641. foreach($result as $k=>&$v){
  3642. //新粉roi
  3643. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  3644. //累计roi
  3645. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  3646. $v['total_roi_sort'] = rtrim($v['total_roi'], '%') * 100;
  3647. //数据格式化
  3648. $v['order_amount'] = round($v['sum_order_amount'],2);
  3649. //15日
  3650. $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();
  3651. if(isset($data15->cost)){
  3652. $v['cost15'] = $data15->cost;
  3653. $v['order_count15'] = $data15->order_count;
  3654. $v['order_amount15'] = round($data15->sum_order_amount,2);
  3655. $v['total_roi15'] = $v['cost15']>0 ? round($data15->sum_order_amount / $v['cost15'], 4) * 100 .'%' : '';
  3656. $v['total_roi15_sort'] = rtrim($v['total_roi15'], '%') * 100;
  3657. }else{
  3658. $v['cost15'] = '';
  3659. $v['order_count15'] = '';
  3660. $v['order_amount15'] = '';
  3661. $v['total_roi15'] = '';
  3662. $v['total_roi15_sort'] = 0;
  3663. }
  3664. //30日
  3665. $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();
  3666. if(isset($data30->cost)){
  3667. $v['cost30'] = $data30->cost;
  3668. $v['order_count30'] = $data30->order_count;
  3669. $v['order_amount30'] = round($data30->sum_order_amount,2);
  3670. $v['total_roi30'] = $v['cost30']>0 ? round($data30->sum_order_amount / $v['cost30'], 4) * 100 .'%' : '';
  3671. $v['total_roi30_sort'] = rtrim($v['total_roi30'], '%') * 100;
  3672. }else{
  3673. $v['cost30'] = '';
  3674. $v['order_count30'] = '';
  3675. $v['order_amount30'] = '';
  3676. $v['total_roi30'] = '';
  3677. $v['total_roi30_sort'] = 0;
  3678. }
  3679. //45日
  3680. $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();
  3681. if(isset($data45->cost)){
  3682. $v['cost45'] = $data45->cost;
  3683. $v['order_count45'] = $data45->order_count;
  3684. $v['order_amount45'] = round($data45->sum_order_amount,2);
  3685. $v['total_roi45'] = $v['cost45']>0 ? round($data45->sum_order_amount / $v['cost45'], 4) * 100 .'%' : '';
  3686. $v['total_roi45_sort'] = rtrim($v['total_roi45'], '%') * 100;
  3687. }else{
  3688. $v['cost45'] = '';
  3689. $v['order_count45'] = '';
  3690. $v['order_amount45'] = '';
  3691. $v['total_roi45'] = '';
  3692. $v['total_roi45_sort'] = 0;
  3693. }
  3694. //60日
  3695. $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();
  3696. if(isset($data60->cost)){
  3697. $v['cost60'] = $data60->cost;
  3698. $v['order_count60'] = $data60->order_count;
  3699. $v['order_amount60'] = round($data60->sum_order_amount,2);
  3700. $v['total_roi60'] = $v['cost60']>0 ? round($data60->sum_order_amount / $v['cost60'], 4) * 100 .'%' : '';
  3701. $v['total_roi60_sort'] = rtrim($v['total_roi60'], '%') * 100;
  3702. }else{
  3703. $v['cost60'] = '';
  3704. $v['order_count60'] = '';
  3705. $v['order_amount60'] = '';
  3706. $v['total_roi60'] = '';
  3707. $v['total_roi60_sort'] = 0;
  3708. }
  3709. }
  3710. $count = count($result);
  3711. if(!$city){
  3712. Redis::set($redis_key, json_encode($result));
  3713. //计算今日剩余时间
  3714. $_time = strtotime(date('Y-m-d'). '23:59:59') - time();
  3715. Redis::expire($redis_key, $_time);
  3716. switch ($sort) {
  3717. case '0':
  3718. $sort_c = 'total_roi';
  3719. break;
  3720. case '1':
  3721. $sort_c = 'total_roi15_sort';
  3722. break;
  3723. case '2':
  3724. $sort_c = 'total_roi30_sort';
  3725. break;
  3726. case '3':
  3727. $sort_c = 'total_roi45_sort';
  3728. break;
  3729. case '4':
  3730. $sort_c = 'total_roi60_sort';
  3731. break;
  3732. default:
  3733. $sort_c = 'total_roi';
  3734. break;
  3735. }
  3736. if($sort>0){
  3737. $sortArr=array_column($result, $sort_c);
  3738. array_multisort($sortArr, SORT_DESC, $result);
  3739. }
  3740. $result = array_slice($result, $offset, $pageSize);
  3741. }
  3742. if ($count > 1) {
  3743. // 总页数
  3744. $pages = ceil($count/$pageSize);
  3745. }else{
  3746. // 总页数
  3747. $pages = 1;
  3748. }
  3749. return view('statistics/districtRoiAllTotal', ['result' =>$result,
  3750. 'page' =>$page,
  3751. 'count' =>$count,
  3752. 'pages' =>$pages,
  3753. 'city' =>$city,
  3754. 'only_roi' =>$only_roi,
  3755. 'sort' =>$sort,
  3756. ]);
  3757. }
  3758. /**
  3759. * 地域roi汇总导出
  3760. */
  3761. public function districtRoiAllTotal_export(Request $request){
  3762. $only_roi = (int)$request->input('only_roi');
  3763. $sort = (int)$request->input('sort'); //排序 0:7日降序 1:15日降序 2:30日降序 3:45日降序 4:60日降序
  3764. $city = $request->input('city');
  3765. if($city){
  3766. $city = str_replace('市', '', $city); //单独搜城市不走redis 不用排序
  3767. }else{
  3768. //从redis取数据
  3769. $redis_key = 'seafood.districtRoiAllTotal';
  3770. $redisData = Redis::get($redis_key);
  3771. if(!empty($redisData)){
  3772. $result = json_decode($redisData, true);
  3773. switch ($sort) {
  3774. case '0':
  3775. $sort_c = 'total_roi';
  3776. break;
  3777. case '1':
  3778. $sort_c = 'total_roi15_sort';
  3779. break;
  3780. case '2':
  3781. $sort_c = 'total_roi30_sort';
  3782. break;
  3783. case '3':
  3784. $sort_c = 'total_roi45_sort';
  3785. break;
  3786. case '4':
  3787. $sort_c = 'total_roi60_sort';
  3788. break;
  3789. default:
  3790. $sort_c = 'total_roi';
  3791. break;
  3792. }
  3793. if($sort>0){
  3794. $sortArr=array_column($result, $sort_c);
  3795. array_multisort($sortArr, SORT_DESC, $result);
  3796. }
  3797. if($only_roi == 1){
  3798. $indexKey = ['city','total_roi','total_roi15','total_roi30','total_roi45','total_roi60'];
  3799. $title = ['城市', '7日累计ROI', '15日累计ROI', '30日累计ROI', '45日累计ROI', '60日累计ROI'];
  3800. }
  3801. else{
  3802. $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'];
  3803. $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'];
  3804. }
  3805. $filename = 'districtRoiAll_'.date('Y-m-d_H').'.xlsx';
  3806. return Order::export_excel($result, $filename, $indexKey, $title);
  3807. }
  3808. }
  3809. $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){
  3810. if($city) $query->where('city', 'like', $city.'%');
  3811. })->groupBy('city')->orderBy('roi', 'desc')->get();
  3812. $result = json_decode(json_encode($result), true);
  3813. foreach($result as $k=>&$v){
  3814. //新粉roi
  3815. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  3816. //累计roi
  3817. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  3818. $v['total_roi_sort'] = rtrim($v['total_roi'], '%') * 100;
  3819. //数据格式化
  3820. $v['order_amount'] = round($v['sum_order_amount'],2);
  3821. //15日
  3822. $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();
  3823. if(isset($data15->cost)){
  3824. $v['cost15'] = $data15->cost;
  3825. $v['order_count15'] = $data15->order_count;
  3826. $v['order_amount15'] = round($data15->sum_order_amount,2);
  3827. $v['total_roi15'] = $v['cost15']>0 ? round($data15->sum_order_amount / $v['cost15'], 4) * 100 .'%' : '';
  3828. $v['total_roi15_sort'] = rtrim($v['total_roi15'], '%') * 100;
  3829. }else{
  3830. $v['cost15'] = '';
  3831. $v['order_count15'] = '';
  3832. $v['order_amount15'] = '';
  3833. $v['total_roi15'] = '';
  3834. $v['total_roi15_sort'] = 0;
  3835. }
  3836. //30日
  3837. $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();
  3838. if(isset($data30->cost)){
  3839. $v['cost30'] = $data30->cost;
  3840. $v['order_count30'] = $data30->order_count;
  3841. $v['order_amount30'] = round($data30->sum_order_amount,2);
  3842. $v['total_roi30'] = $v['cost30']>0 ? round($data30->sum_order_amount / $v['cost30'], 4) * 100 .'%' : '';
  3843. $v['total_roi30_sort'] = rtrim($v['total_roi30'], '%') * 100;
  3844. }else{
  3845. $v['cost30'] = '';
  3846. $v['order_count30'] = '';
  3847. $v['order_amount30'] = '';
  3848. $v['total_roi30'] = '';
  3849. $v['total_roi30_sort'] = 0;
  3850. }
  3851. //45日
  3852. $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();
  3853. if(isset($data45->cost)){
  3854. $v['cost45'] = $data45->cost;
  3855. $v['order_count45'] = $data45->order_count;
  3856. $v['order_amount45'] = round($data45->sum_order_amount,2);
  3857. $v['total_roi45'] = $v['cost45']>0 ? round($data45->sum_order_amount / $v['cost45'], 4) * 100 .'%' : '';
  3858. $v['total_roi45_sort'] = rtrim($v['total_roi45'], '%') * 100;
  3859. }else{
  3860. $v['cost45'] = '';
  3861. $v['order_count45'] = '';
  3862. $v['order_amount45'] = '';
  3863. $v['total_roi45'] = '';
  3864. $v['total_roi45_sort'] = 0;
  3865. }
  3866. //60日
  3867. $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();
  3868. if(isset($data60->cost)){
  3869. $v['cost60'] = $data60->cost;
  3870. $v['order_count60'] = $data60->order_count;
  3871. $v['order_amount60'] = round($data60->sum_order_amount,2);
  3872. $v['total_roi60'] = $v['cost60']>0 ? round($data60->sum_order_amount / $v['cost60'], 4) * 100 .'%' : '';
  3873. $v['total_roi60_sort'] = rtrim($v['total_roi60'], '%') * 100;
  3874. }else{
  3875. $v['cost60'] = '';
  3876. $v['order_count60'] = '';
  3877. $v['order_amount60'] = '';
  3878. $v['total_roi60'] = '';
  3879. $v['total_roi60_sort'] = 0;
  3880. }
  3881. }
  3882. if(!$city){
  3883. Redis::set($redis_key, json_encode($result));
  3884. //计算今日剩余时间
  3885. $_time = strtotime(date('Y-m-d'). '23:59:59') - time();
  3886. Redis::expire($redis_key, $_time);
  3887. switch ($sort) {
  3888. case '0':
  3889. $sort_c = 'total_roi';
  3890. break;
  3891. case '1':
  3892. $sort_c = 'total_roi15_sort';
  3893. break;
  3894. case '2':
  3895. $sort_c = 'total_roi30_sort';
  3896. break;
  3897. case '3':
  3898. $sort_c = 'total_roi45_sort';
  3899. break;
  3900. case '4':
  3901. $sort_c = 'total_roi60_sort';
  3902. break;
  3903. default:
  3904. $sort_c = 'total_roi';
  3905. break;
  3906. }
  3907. if($sort>0){
  3908. $sortArr=array_column($result, $sort_c);
  3909. array_multisort($sortArr, SORT_DESC, $result);
  3910. }
  3911. }
  3912. if($only_roi == 1){
  3913. $indexKey = ['city','total_roi','total_roi15','total_roi30','total_roi45','total_roi60'];
  3914. $title = ['城市', '7日累计ROI', '15日累计ROI', '30日累计ROI', '45日累计ROI', '60日累计ROI'];
  3915. }
  3916. else{
  3917. $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'];
  3918. $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'];
  3919. }
  3920. $filename = 'districtRoiAll_'.date('Y-m-d_H').'.xlsx';
  3921. return Order::export_excel($result, $filename, $indexKey, $title);
  3922. }
  3923. /**
  3924. * 销售粉丝复购率
  3925. */
  3926. public function salerOrderFugou(Request $request){
  3927. //获取销售
  3928. $saler_id = (int)$request->input('admin_id');
  3929. $team_id = (int)$request->input('team_id');
  3930. $sort = (int)$request->input('sort');
  3931. switch ($sort) {
  3932. case '1':
  3933. $_sort_name = 'cust_fugou_rate_sort';
  3934. break;
  3935. case '2':
  3936. $_sort_name = 'cust_order_rate';
  3937. break;
  3938. default:
  3939. $_sort_name = 'fugou_rate_sort';
  3940. break;
  3941. }
  3942. //假如有团队筛选,检索销售队员
  3943. $saler_ids = null;
  3944. $team_ids = Admin::getTeams();
  3945. if($saler_id>0){
  3946. $saler_ids = [$saler_id];
  3947. }elseif($team_id>0){
  3948. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  3949. }else{
  3950. $saler_ids = DB::table('admin')->whereIn('team_id', $team_ids)->lists('id');
  3951. }
  3952. $page = (int)$request->input('page');
  3953. $pageSize = 20;
  3954. if($page<=0){
  3955. $page = 1;
  3956. }
  3957. $offset = ($page-1) * $pageSize;
  3958. $count = count($saler_ids);
  3959. //销售名字
  3960. $name_arr = DB::table('admin')->whereIn('id', $saler_ids)->lists('realname', 'id');
  3961. $result = array();
  3962. foreach($saler_ids as $k=>$admin_id){
  3963. $result[$k]['admin_id'] = $admin_id;
  3964. //pv
  3965. $temp = TemplatesLog::select(DB::raw('count(1) as tcount, count(if(type=1,true,null)) as pv_count'))->where('admin_id',$admin_id)->first();
  3966. $result[$k]['tcount'] = $temp->tcount;
  3967. $result[$k]['pv_count'] = $temp->pv_count;
  3968. //加粉
  3969. $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add, admin_name'))->where('admin_id', $admin_id)->where('is_del', 0)->first();
  3970. if(!empty($custDetail)){
  3971. $result[$k]['fan_add'] = $custDetail->fan_add;
  3972. }else{
  3973. $result[$k]['fan_add'] = '';
  3974. }
  3975. $result[$k]['admin_name'] = $name_arr[$admin_id];
  3976. $result[$k]['long_count'] = $result[$k]['tcount'] - $result[$k]['pv_count'];
  3977. //点击率
  3978. $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 . '%' : '';
  3979. $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 . '%' : '';
  3980. //下单数据统计
  3981. $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();
  3982. $result[$k]['order_count'] = $order->order_count;
  3983. $result[$k]['order_amount'] = $order->order_amount;
  3984. //下单用户数
  3985. $customer_count = $order->cust_count;
  3986. //复购用户数 = 下单超过一单的用户数
  3987. $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();
  3988. $customer_fugou_count = count($order_cust_fugou);
  3989. //复购单= 总单-用户数
  3990. $fugou_count = $order->order_count - $order->cust_count;
  3991. $no_fugou_count = $order->order_count - $fugou_count;
  3992. //复购率
  3993. $fugou_rate = $no_fugou_count>0 ? round( $fugou_count / $no_fugou_count, 4 ) * 100 . '%' : '';
  3994. $cust_fugou_rate = $customer_count>0 ? round( $customer_fugou_count / $customer_count, 4 ) * 100 . '%' : '';
  3995. $result[$k]['customer_count'] = $customer_count;
  3996. $result[$k]['customer_fugou_count'] = $customer_fugou_count;
  3997. $result[$k]['fugou_rate'] = $fugou_rate;
  3998. $result[$k]['fugou_count'] = $fugou_count;
  3999. $result[$k]['cust_fugou_rate'] = $cust_fugou_rate;
  4000. $result[$k]['fugou_rate_sort'] = $no_fugou_count>0 ? round( $fugou_count / $no_fugou_count, 4 ) * 10000 : 0;
  4001. $result[$k]['cust_fugou_rate_sort'] = $customer_count>0 ? round( $customer_fugou_count / $customer_count, 4 ) * 10000 : 0;
  4002. //用户转化率
  4003. $result[$k]['cust_order_rate'] = $result[$k]['fan_add']>0 ? round( $customer_count / $result[$k]['fan_add'], 4 ) * 100 . '%' : '';
  4004. }
  4005. //按复购率排序
  4006. $fugou_rate_arr = array_column($result, $_sort_name);
  4007. array_multisort($fugou_rate_arr, SORT_DESC, $result);
  4008. $result = array_slice($result, $offset, $pageSize);
  4009. if ($count > 1) {
  4010. // 总页数
  4011. $pages = ceil($count/$pageSize);
  4012. }else{
  4013. // 总页数
  4014. $pages = 1;
  4015. }
  4016. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  4017. $teamList = json_decode(json_encode($teamList), true);
  4018. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where('is_use', 1)->get();
  4019. $adminList = json_decode(json_encode($adminList), true);
  4020. return view('statistics/salerOrderFugou', ['result' =>$result,
  4021. 'page' =>$page,
  4022. 'count' =>$count,
  4023. 'pages' =>$pages,
  4024. 'admin_id' =>$saler_id,
  4025. 'team_id' =>$team_id,
  4026. 'teamlist' =>$teamList,
  4027. 'adminlist' =>$adminList,
  4028. 'sort' =>$sort,
  4029. ]);
  4030. }
  4031. /**
  4032. * 销售粉丝复购率导出
  4033. */
  4034. public function salerOrderFugou_export(Request $request){
  4035. //获取销售
  4036. $saler_id = (int)$request->input('admin_id');
  4037. $team_id = (int)$request->input('team_id');
  4038. $sort = (int)$request->input('sort');
  4039. switch ($sort) {
  4040. case '1':
  4041. $_sort_name = 'cust_fugou_rate_sort';
  4042. break;
  4043. case '2':
  4044. $_sort_name = 'cust_order_rate';
  4045. break;
  4046. default:
  4047. $_sort_name = 'fugou_rate_sort';
  4048. break;
  4049. }
  4050. //假如有团队筛选,检索销售队员
  4051. $saler_ids = null;
  4052. $team_ids = Admin::getTeams();
  4053. if($saler_id>0){
  4054. $saler_ids = [$saler_id];
  4055. }elseif($team_id>0){
  4056. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  4057. }else{
  4058. $saler_ids = DB::table('admin')->whereIn('team_id', $team_ids)->lists('id');
  4059. }
  4060. //销售名字
  4061. $name_arr = DB::table('admin')->whereIn('id', $saler_ids)->lists('realname', 'id');
  4062. $result = array();
  4063. foreach($saler_ids as $k=>$admin_id){
  4064. $result[$k]['admin_id'] = $admin_id;
  4065. //pv
  4066. $temp = TemplatesLog::select(DB::raw('count(1) as tcount, count(if(type=1,true,null)) as pv_count'))->where('admin_id',$admin_id)->first();
  4067. $result[$k]['tcount'] = $temp->tcount;
  4068. $result[$k]['pv_count'] = $temp->pv_count;
  4069. //加粉
  4070. $custDetail = CustDetail::select(DB::raw('sum(fan_add) as fan_add, admin_name'))->where('admin_id', $admin_id)->where('is_del', 0)->first();
  4071. if(!empty($custDetail)){
  4072. $result[$k]['fan_add'] = $custDetail->fan_add;
  4073. }else{
  4074. $result[$k]['fan_add'] = '';
  4075. }
  4076. $result[$k]['admin_name'] = $name_arr[$admin_id];
  4077. $result[$k]['long_count'] = $result[$k]['tcount'] - $result[$k]['pv_count'];
  4078. //点击率
  4079. $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 . '%' : '';
  4080. $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 . '%' : '';
  4081. //下单数据统计
  4082. $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();
  4083. $result[$k]['order_count'] = $order->order_count;
  4084. $result[$k]['order_amount'] = $order->order_amount;
  4085. //下单用户数
  4086. $customer_count = $order->cust_count;
  4087. //复购用户数 = 下单超过一单的用户数
  4088. $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();
  4089. $customer_fugou_count = count($order_cust_fugou);
  4090. //复购单= 总单-用户数
  4091. $fugou_count = $order->order_count - $order->cust_count;
  4092. $no_fugou_count = $order->order_count - $fugou_count;
  4093. //复购率
  4094. $fugou_rate = $no_fugou_count>0 ? round( $fugou_count / $no_fugou_count, 4 ) * 100 . '%' : '';
  4095. $cust_fugou_rate = $customer_count>0 ? round( $customer_fugou_count / $customer_count, 4 ) * 100 . '%' : '';
  4096. $result[$k]['customer_count'] = $customer_count;
  4097. $result[$k]['customer_fugou_count'] = $customer_fugou_count;
  4098. $result[$k]['fugou_rate'] = $fugou_rate;
  4099. $result[$k]['fugou_count'] = $fugou_count;
  4100. $result[$k]['cust_fugou_rate'] = $cust_fugou_rate;
  4101. $result[$k]['fugou_rate_sort'] = $no_fugou_count>0 ? round( $fugou_count / $no_fugou_count, 4 ) * 10000 : 0;
  4102. $result[$k]['cust_fugou_rate_sort'] = $customer_count>0 ? round( $customer_fugou_count / $customer_count, 4 ) * 10000 : 0;
  4103. //用户转化率
  4104. $result[$k]['cust_order_rate'] = $result[$k]['fan_add']>0 ? round( $customer_count / $result[$k]['fan_add'], 4 ) * 100 . '%' : '';
  4105. }
  4106. //按复购率排序
  4107. $fugou_rate_arr = array_column($result, $_sort_name);
  4108. array_multisort($fugou_rate_arr, SORT_DESC, $result);
  4109. //$indexKey = ['admin_name','pv_count','long_count','fan_add','change_rate','customer_count','customer_fugou_count','order_count','order_amount','fugou_rate'];
  4110. $indexKey = ['admin_name','fan_add','customer_count','cust_order_rate','order_count','order_amount', 'fugou_count', 'fugou_rate', 'customer_fugou_count', 'cust_fugou_rate'];
  4111. //$title = ['销售', 'PV量', '累计预估加粉数', '累计加粉数', '加粉转化率', '下单用户数', '复购用户数', '累计下单数', '累计销售额', '复购率'];
  4112. $title = ['销售', '累计加粉数', '下单用户数', '用户转化率', '累计下单数', '累计销售额', '复购订单数', '订单复购率', '复购用户数', '用户复购率'];
  4113. $filename = 'xiaoshoufugou_'.date('Y-m-d_H').'.xlsx';
  4114. return Order::export_excel($result, $filename, $indexKey, $title);
  4115. }
  4116. /**
  4117. * 每日累计汇总数据
  4118. */
  4119. public function dayGrandTotal(Request $request){
  4120. $page = (int)$request->input('page');
  4121. $pageSize = 20;
  4122. if($page<=0){
  4123. $page = 1;
  4124. }
  4125. $_start = '2019-09-04';
  4126. $data = array();
  4127. if($page == 1){
  4128. $pageSize = $pageSize - 1;
  4129. $offset = ($page-1) * $pageSize;
  4130. //今日数据实时计算
  4131. $idate = date('Y-m-d');
  4132. $data['idate'] = $idate;
  4133. //总投入
  4134. $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost');
  4135. //订单信息
  4136. $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();
  4137. $data['goods_cost'] = $order->goods_cost;
  4138. $data['freight_cost'] = $order->freight_cost;
  4139. $data['aftersale_cost'] = $order->aftersale_cost;
  4140. $data['refund_fee'] = $order->refund_fee;
  4141. $data['order_count'] = $order->order_count;
  4142. $data['order_amount'] = $order->order_amount;
  4143. $data['cust_count'] = $order->cust_count;
  4144. //加粉
  4145. $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
  4146. //毛利 = 总销售额-总商品成本-总运费-总售后
  4147. $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
  4148. $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) * 100 .'%' : '';
  4149. //总新粉单数
  4150. $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();
  4151. $data['new_order_count'] = $new_order->order_count;
  4152. //总老粉单数
  4153. $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
  4154. //总复购订单
  4155. $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
  4156. //总复购率
  4157. $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : '';
  4158. //7 - 60 roi汇总
  4159. $data['cost7'] = DB::table('roi_total')->where('type', 1)->sum('cost');
  4160. $data['order_count7'] = DB::table('roi_total')->where('type', 1)->sum('order_count');
  4161. $data['order_amount7'] = DB::table('roi_total')->where('type', 1)->sum('order_amount');
  4162. $data['roi7'] = $data['cost7']>0? round($data['order_amount7'] / $data['cost7'], 4) * 100 .'%' : '';
  4163. $data['cost15'] = DB::table('roi_total')->where('type', 2)->sum('cost');
  4164. $data['order_count15'] = DB::table('roi_total')->where('type', 2)->sum('order_count');
  4165. $data['order_amount15'] = DB::table('roi_total')->where('type', 2)->sum('order_amount');
  4166. $data['roi15'] = $data['cost15']>0? round($data['order_amount15'] / $data['cost15'], 4) * 100 .'%' : '';
  4167. $data['cost30'] = DB::table('roi_total')->where('type', 3)->sum('cost');
  4168. $data['order_count30'] = DB::table('roi_total')->where('type', 3)->sum('order_count');
  4169. $data['order_amount30'] = DB::table('roi_total')->where('type', 3)->sum('order_amount');
  4170. $data['roi30'] = $data['cost30']>0? round($data['order_amount30'] / $data['cost30'], 4) * 100 .'%' : '';
  4171. $data['cost45'] = DB::table('roi_total')->where('type', 4)->sum('cost');
  4172. $data['order_count45'] = DB::table('roi_total')->where('type', 4)->sum('order_count');
  4173. $data['order_amount45'] = DB::table('roi_total')->where('type', 4)->sum('order_amount');
  4174. $data['roi45'] = $data['cost45']>0? round($data['order_amount45'] / $data['cost45'], 4) * 100 .'%' : '';
  4175. $data['cost60'] = DB::table('roi_total')->where('type', 5)->sum('cost');
  4176. $data['order_count60'] = DB::table('roi_total')->where('type', 5)->sum('order_count');
  4177. $data['order_amount60'] = DB::table('roi_total')->where('type', 5)->sum('order_amount');
  4178. $data['roi60'] = $data['cost60']>0? round($data['order_amount60'] / $data['cost60'], 4) * 100 .'%' : '';
  4179. }else{
  4180. $offset = ($page-1) * $pageSize - 1;
  4181. }
  4182. //查询历史数据
  4183. $count = DB::table('day_grand_total')->count();
  4184. $count = $count + 1;
  4185. $result = DB::table('day_grand_total')->orderBy('idate', 'desc')->offset($offset)->limit($pageSize)->get();
  4186. $result = json_decode(json_encode($result), true);
  4187. foreach($result as $k=>&$v){
  4188. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
  4189. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  4190. $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
  4191. $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
  4192. $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
  4193. $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
  4194. $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
  4195. }
  4196. if($page == 1){
  4197. $result = array_merge([$data], $result);
  4198. }
  4199. if ($count > 1) {
  4200. // 总页数
  4201. $pages = ceil($count/20);
  4202. }else{
  4203. // 总页数
  4204. $pages = 1;
  4205. }
  4206. return view('statistics/dayGrandTotal', ['result' =>$result,
  4207. 'page' =>$page,
  4208. 'count' =>$count,
  4209. 'pages' =>$pages,
  4210. ]);
  4211. }
  4212. /**
  4213. * 每日累计汇总数据导出
  4214. */
  4215. public function dayGrandTotal_export(Request $request){
  4216. $_start = '2019-09-04';
  4217. $data = array();
  4218. //今日数据实时计算
  4219. $idate = date('Y-m-d');
  4220. $data['idate'] = $idate;
  4221. //总投入
  4222. $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','>=',$_start)->sum('total_cost');
  4223. //订单信息
  4224. $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();
  4225. $data['goods_cost'] = $order->goods_cost;
  4226. $data['freight_cost'] = $order->freight_cost;
  4227. $data['aftersale_cost'] = $order->aftersale_cost;
  4228. $data['refund_fee'] = $order->refund_fee;
  4229. $data['order_count'] = $order->order_count;
  4230. $data['order_amount'] = $order->order_amount;
  4231. $data['cust_count'] = $order->cust_count;
  4232. //加粉
  4233. $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','>=',$_start)->sum('fan_add');
  4234. //毛利 = 总销售额-总商品成本-总运费-总售后
  4235. $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
  4236. $data['roi'] = $data['throw_cost']>0? round($order->order_amount / $data['throw_cost'], 4) * 100 .'%' : '';
  4237. //总新粉单数
  4238. $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();
  4239. $data['new_order_count'] = $new_order->order_count;
  4240. //总老粉单数
  4241. $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
  4242. //总复购订单
  4243. $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
  4244. //总复购率
  4245. $data['fugou_rate'] = $data['cust_count']>0 ? round($data['fugou_order_count'] / $data['cust_count'], 4) : '';
  4246. //7 - 60 roi汇总
  4247. $data['cost7'] = DB::table('roi_total')->where('type', 1)->sum('cost');
  4248. $data['order_count7'] = DB::table('roi_total')->where('type', 1)->sum('order_count');
  4249. $data['order_amount7'] = DB::table('roi_total')->where('type', 1)->sum('order_amount');
  4250. $data['roi7'] = $data['cost7']>0? round($data['order_amount7'] / $data['cost7'], 4) * 100 .'%' : '';
  4251. $data['cost15'] = DB::table('roi_total')->where('type', 2)->sum('cost');
  4252. $data['order_count15'] = DB::table('roi_total')->where('type', 2)->sum('order_count');
  4253. $data['order_amount15'] = DB::table('roi_total')->where('type', 2)->sum('order_amount');
  4254. $data['roi15'] = $data['cost15']>0? round($data['order_amount15'] / $data['cost15'], 4) * 100 .'%' : '';
  4255. $data['cost30'] = DB::table('roi_total')->where('type', 3)->sum('cost');
  4256. $data['order_count30'] = DB::table('roi_total')->where('type', 3)->sum('order_count');
  4257. $data['order_amount30'] = DB::table('roi_total')->where('type', 3)->sum('order_amount');
  4258. $data['roi30'] = $data['cost30']>0? round($data['order_amount30'] / $data['cost30'], 4) * 100 .'%' : '';
  4259. $data['cost45'] = DB::table('roi_total')->where('type', 4)->sum('cost');
  4260. $data['order_count45'] = DB::table('roi_total')->where('type', 4)->sum('order_count');
  4261. $data['order_amount45'] = DB::table('roi_total')->where('type', 4)->sum('order_amount');
  4262. $data['roi45'] = $data['cost45']>0? round($data['order_amount45'] / $data['cost45'], 4) * 100 .'%' : '';
  4263. $data['cost60'] = DB::table('roi_total')->where('type', 5)->sum('cost');
  4264. $data['order_count60'] = DB::table('roi_total')->where('type', 5)->sum('order_count');
  4265. $data['order_amount60'] = DB::table('roi_total')->where('type', 5)->sum('order_amount');
  4266. $data['roi60'] = $data['cost60']>0? round($data['order_amount60'] / $data['cost60'], 4) * 100 .'%' : '';
  4267. $result = DB::table('day_grand_total')->orderBy('idate', 'desc')->get();
  4268. $result = json_decode(json_encode($result), true);
  4269. foreach($result as $k=>&$v){
  4270. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
  4271. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  4272. $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
  4273. $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
  4274. $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
  4275. $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
  4276. $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
  4277. }
  4278. $result = array_merge([$data], $result);
  4279. $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'];
  4280. $title = ['汇总日期', '总投放', '总客户数', '总下单数', '总销售额', '总货品成本', '总运费成本', '总售后', '总退补', '总毛利', '累计ROI', '总加粉数', '总新粉单数', '总老粉单数', '总复购单数', '总复购率', '累计7日roi', '累计15日roi', '累计30日roi', '累计45日roi', '累计60日roi'];
  4281. $filename = 'huizongshuj_'.date('Y-m-d_H').'.xlsx';
  4282. return Order::export_excel($result, $filename, $indexKey, $title);
  4283. }
  4284. /**
  4285. * 近30天销量排行
  4286. */
  4287. public function volumeRank(Request $request){
  4288. $stime = date('Y-m-d', strtotime('-30 day'));
  4289. $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();
  4290. $result = json_decode(json_encode($result), true);
  4291. foreach($result as $k=>&$v){
  4292. $goods = Goods::where('id', $v['goods_id'])->first();
  4293. $skus = GoodsSkus::select('is_weigh', 'propsName', 'price')->where('id', $v['sku_id'])->first();
  4294. $v['name'] = $goods->name;
  4295. $v['propsName'] = $skus->propsName;
  4296. $v['propsName'] .= !empty($skus) && $skus->is_weigh == 1 ? '【称重商品】' : '';
  4297. $v['price'] = $skus->price;
  4298. if($skus->is_weigh == 0) $v['volume'] = (int)$v['volume'];
  4299. $v['amount'] = round($v['price'] * $v['volume'], 2);
  4300. }
  4301. return view('statistics/volumeRank', ['result' =>$result,
  4302. ]);
  4303. }
  4304. public function volumeRank_export(Request $request){
  4305. $stime = date('Y-m-d', strtotime('-30 day'));
  4306. $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();
  4307. $result = json_decode(json_encode($result), true);
  4308. foreach($result as $k=>&$v){
  4309. $goods = Goods::where('id', $v['goods_id'])->first();
  4310. $skus = GoodsSkus::select('is_weigh', 'propsName', 'price')->where('id', $v['sku_id'])->first();
  4311. $v['name'] = $goods->name;
  4312. $v['propsName'] = $skus->propsName;
  4313. $v['propsName'] .= !empty($skus) && $skus->is_weigh == 1 ? '【称重商品】' : '';
  4314. $v['price'] = $skus->price;
  4315. if($skus->is_weigh == 0) $v['volume'] = (int)$v['volume'];
  4316. $v['amount'] = round($v['price'] * $v['volume'], 2);
  4317. $v['rank_id'] = $k+1;
  4318. }
  4319. $indexKey = ['rank_id','name','propsName','price','counts','volume','amount'];
  4320. $title = ['排名', '商品标题', '商品规格', '价格', '最近30日成单量', '最近30日销量', '近30日销售额'];
  4321. $filename = 'shangpingpaihang_'.date('Y-m-d_H').'.xlsx';
  4322. return Order::export_excel($result, $filename, $indexKey, $title);
  4323. }
  4324. /**
  4325. * 分团队每日roi汇总数据
  4326. */
  4327. public function dayGrandTeamTotal(Request $request){
  4328. $team_id = (int)$request->input('team_id');
  4329. $stime = $request->input('stime');
  4330. $etime = $request->input('etime');
  4331. $page = (int)$request->input('page');
  4332. $pageSize = 20;
  4333. if($page<=0){
  4334. $page = 1;
  4335. }
  4336. $offset = ($page-1) * $pageSize;
  4337. $self_role = session('role_name');
  4338. if($self_role == '管理员'){
  4339. //只能看自己团队的
  4340. $self_id = session('admin_id');
  4341. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  4342. }
  4343. //查询历史数据
  4344. $count = DB::table('day_grand_team_total')->where(function($query) use($team_id, $stime, $etime){
  4345. if($team_id) $query->where('team_id', $team_id);
  4346. if($stime) $query->where('idate', '>=', $stime);
  4347. if($etime) $query->where('idate', '<=', $etime);
  4348. })->count();
  4349. $teamArr = Admin::getTeamsName();
  4350. $result = DB::table('day_grand_team_total')->where(function($query) use($team_id, $stime, $etime){
  4351. if($team_id) $query->where('team_id', $team_id);
  4352. if($stime) $query->where('idate', '>=', $stime);
  4353. if($etime) $query->where('idate', '<=', $etime);
  4354. })->orderBy('idate', 'desc')->offset($offset)->limit($pageSize)->get();
  4355. $result = json_decode(json_encode($result), true);
  4356. foreach($result as $k=>&$v){
  4357. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
  4358. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  4359. $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
  4360. $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
  4361. $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
  4362. $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
  4363. $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
  4364. $v['roi90'] = $v['cost90']>0? round($v['order_amount90'] / $v['cost90'], 4) * 100 .'%' : '';
  4365. $v['team_name'] = $teamArr[$v['team_id']];
  4366. }
  4367. if ($count > 1) {
  4368. // 总页数
  4369. $pages = ceil($count/20);
  4370. }else{
  4371. // 总页数
  4372. $pages = 1;
  4373. }
  4374. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
  4375. if($self_role == '管理员') $query->where('id', $team_id);
  4376. })->where('type', 1)->get();
  4377. $teamList = json_decode(json_encode($teamList), true);
  4378. return view('statistics/dayGrandTeamTotal', ['result' =>$result,
  4379. 'page' =>$page,
  4380. 'count' =>$count,
  4381. 'pages' =>$pages,
  4382. 'teamlist' =>$teamList,
  4383. 'team_id' =>$team_id,
  4384. 'stime' =>$stime,
  4385. 'etime' =>$etime,
  4386. ]);
  4387. }
  4388. /**
  4389. * 每日累计汇总数据导出
  4390. */
  4391. public function dayGrandTeamTotal_export(Request $request){
  4392. $team_id = (int)$request->input('team_id');
  4393. $stime = $request->input('stime');
  4394. $etime = $request->input('etime');
  4395. $self_role = session('role_name');
  4396. if($self_role == '管理员'){
  4397. //只能看自己团队的
  4398. $self_id = session('admin_id');
  4399. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  4400. }
  4401. $teamArr = Admin::getTeamsName();
  4402. $result = DB::table('day_grand_team_total')->where(function($query) use($team_id, $stime, $etime){
  4403. if($team_id) $query->where('team_id', $team_id);
  4404. if($stime) $query->where('idate', '>=', $stime);
  4405. if($etime) $query->where('idate', '<=', $etime);
  4406. })->orderBy('idate', 'desc')->get();
  4407. $result = json_decode(json_encode($result), true);
  4408. foreach($result as $k=>&$v){
  4409. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
  4410. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  4411. $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
  4412. $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
  4413. $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
  4414. $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
  4415. $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
  4416. $v['team_name'] = $teamArr[$v['team_id']];
  4417. }
  4418. $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'];
  4419. $title = ['汇总日期', '团队', '总投放', '总客户数', '总下单数', '总销售额', '总货品成本', '总运费成本', '总售后', '总退补', '总毛利', '累计ROI', '总加粉数', '总新粉单数', '总老粉单数', '总复购单数', '总复购率', '累计7日roi', '累计15日roi', '累计30日roi', '累计45日roi', '累计60日roi'];
  4420. $filename = 'teamRoi_'.date('Y-m-d_H').'.xlsx';
  4421. return Order::export_excel($result, $filename, $indexKey, $title);
  4422. }
  4423. public function customerOrder(Request $request) {
  4424. $page = (int)$request->input('page');
  4425. $pageSize = 20;
  4426. if($page<=0){
  4427. $page = 1;
  4428. }
  4429. $offset = ($page-1) * $pageSize;
  4430. $count = CustomerOrderDay::count();
  4431. $data = CustomerOrderDay::offset($offset)->limit($pageSize)->orderBy('grossProfit','desc')->get();
  4432. if ($count > 1) {
  4433. // 总页数
  4434. $pages = ceil($count/20);
  4435. }else{
  4436. // 总页数
  4437. $pages = 1;
  4438. }
  4439. $result = json_decode(json_encode($data),true);
  4440. $result = array_map(function($value){
  4441. $item = $value;
  4442. $phone = $value['phone'];
  4443. $item['phone'] = is_numeric($phone) ? substr_replace($phone, '****', 3, 4) : $phone;
  4444. return $item;
  4445. },$result);
  4446. return view('statistics/customerOrder', ['result' =>$result,
  4447. 'page' =>$page,
  4448. 'count' =>$count,
  4449. 'pages' =>$pages,
  4450. ]);
  4451. }
  4452. function findNum($str=''){
  4453. $str=trim($str);
  4454. if(empty($str)){return '';}
  4455. $reg='/(\d{3}(\.\d+)?)/is';//匹配数字的正则表达式
  4456. preg_match_all($reg,$str,$result);
  4457. if(is_array($result)&&!empty($result)&&!empty($result[1])&&!empty($result[1][0])){
  4458. return $result[1][0];
  4459. }
  4460. return '';
  4461. }
  4462. /**
  4463. * 分销售每日roi汇总数据
  4464. */
  4465. public function dayGrandSalerTotal(Request $request){
  4466. $team_id = (int)$request->input('team_id');
  4467. $admin_id = (int)$request->input('admin_id');
  4468. $stime = $request->input('stime');
  4469. $etime = $request->input('etime');
  4470. $page = (int)$request->input('page');
  4471. $pageSize = 20;
  4472. if($page<=0){
  4473. $page = 1;
  4474. }
  4475. $offset = ($page-1) * $pageSize;
  4476. $self_role = session('role_name');
  4477. if($self_role == '销售' || $self_role == '分销销售'){
  4478. $admin_id = session('admin_id');
  4479. }
  4480. if($self_role == '管理员'){
  4481. //只能看自己团队的
  4482. $self_id = session('admin_id');
  4483. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  4484. }
  4485. //查询历史数据
  4486. $count = DB::table('day_grand_saler_total')->where(function($query) use($team_id, $stime, $etime, $admin_id){
  4487. if($team_id) $query->where('team_id', $team_id);
  4488. if($admin_id) $query->where('admin_id', $admin_id);
  4489. if($stime) $query->where('idate', '>=', $stime);
  4490. if($etime) $query->where('idate', '<=', $etime);
  4491. })->count();
  4492. $teamArr = DB::table('teams')->lists('name', 'id');
  4493. $adminArr = DB::table('admin')->lists('realname', 'id');
  4494. $result = DB::table('day_grand_saler_total')->where(function($query) use($team_id, $stime, $etime, $admin_id){
  4495. if($team_id) $query->where('team_id', $team_id);
  4496. if($admin_id) $query->where('admin_id', $admin_id);
  4497. if($stime) $query->where('idate', '>=', $stime);
  4498. if($etime) $query->where('idate', '<=', $etime);
  4499. })->orderBy('idate', 'desc')->offset($offset)->limit($pageSize)->get();
  4500. $result = json_decode(json_encode($result), true);
  4501. foreach($result as $k=>&$v){
  4502. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
  4503. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  4504. $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
  4505. $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
  4506. $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
  4507. $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
  4508. $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
  4509. $v['team_name'] = $teamArr[$v['team_id']];
  4510. $v['admin_name'] = $adminArr[$v['admin_id']];
  4511. }
  4512. if ($count > 1) {
  4513. // 总页数
  4514. $pages = ceil($count/20);
  4515. }else{
  4516. // 总页数
  4517. $pages = 1;
  4518. }
  4519. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($team_id, $self_role){
  4520. if($self_role == '管理员') $query->where('id', $team_id);
  4521. })->where('type', 1)->get();
  4522. $teamList = json_decode(json_encode($teamList), true);
  4523. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where(function($query) use($team_id, $self_role){
  4524. if($self_role == '管理员') $query->where('team_id', $team_id);
  4525. })->where('id','>', 1)->where('is_use', 1)->get();
  4526. $adminList = json_decode(json_encode($adminList), true);
  4527. return view('statistics/dayGrandSalerTotal', ['result' =>$result,
  4528. 'page' =>$page,
  4529. 'count' =>$count,
  4530. 'pages' =>$pages,
  4531. 'teamlist' =>$teamList,
  4532. 'adminlist' =>$adminList,
  4533. 'team_id' =>$team_id,
  4534. 'admin_id' =>$admin_id,
  4535. 'stime' =>$stime,
  4536. 'etime' =>$etime,
  4537. 'self_role' =>$self_role,
  4538. ]);
  4539. }
  4540. /**
  4541. * 每日累计汇总数据导出
  4542. */
  4543. public function dayGrandSalerTotal_export(Request $request){
  4544. $team_id = (int)$request->input('team_id');
  4545. $admin_id = (int)$request->input('admin_id');
  4546. $stime = $request->input('stime');
  4547. $etime = $request->input('etime');
  4548. $self_role = session('role_name');
  4549. if($self_role == '销售' || $self_role == '分销销售'){
  4550. $admin_id = session('admin_id');
  4551. }
  4552. if($self_role == '管理员'){
  4553. //只能看自己团队的
  4554. $self_id = session('admin_id');
  4555. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  4556. }
  4557. $teamArr = DB::table('teams')->lists('name', 'id');
  4558. $adminArr = DB::table('admin')->lists('realname', 'id');
  4559. $result = DB::table('day_grand_saler_total')->where(function($query) use($team_id, $stime, $etime, $admin_id){
  4560. if($team_id) $query->where('team_id', $team_id);
  4561. if($admin_id) $query->where('admin_id', $admin_id);
  4562. if($stime) $query->where('idate', '>=', $stime);
  4563. if($etime) $query->where('idate', '<=', $etime);
  4564. })->orderBy('idate', 'desc')->get();
  4565. $result = json_decode(json_encode($result), true);
  4566. foreach($result as $k=>&$v){
  4567. $v['roi'] = $v['throw_cost']>0 ? round($v['order_amount'] / $v['throw_cost'], 4) * 100 .'%' : '';
  4568. $v['fugou_rate'] = $v['cust_count']>0 ? round($v['fugou_order_count'] / $v['cust_count'], 4) : '';
  4569. $v['roi7'] = $v['cost7']>0? round($v['order_amount7'] / $v['cost7'], 4) * 100 .'%' : '';
  4570. $v['roi15'] = $v['cost15']>0? round($v['order_amount15'] / $v['cost15'], 4) * 100 .'%' : '';
  4571. $v['roi30'] = $v['cost30']>0? round($v['order_amount30'] / $v['cost30'], 4) * 100 .'%' : '';
  4572. $v['roi45'] = $v['cost45']>0? round($v['order_amount45'] / $v['cost45'], 4) * 100 .'%' : '';
  4573. $v['roi60'] = $v['cost60']>0? round($v['order_amount60'] / $v['cost60'], 4) * 100 .'%' : '';
  4574. $v['team_name'] = $teamArr[$v['team_id']];
  4575. $v['admin_name'] = $adminArr[$v['admin_id']];
  4576. }
  4577. $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'];
  4578. $title = ['汇总日期','销售', '所属团队', '总投放', '总客户数', '总下单数', '总销售额', '总货品成本', '总运费成本', '总售后', '总退补', '总毛利', '累计ROI', '总加粉数', '总新粉单数', '总老粉单数', '总复购单数', '总复购率', '累计7日roi', '累计15日roi', '累计30日roi', '累计45日roi', '累计60日roi'];
  4579. $filename = 'salerRoi_'.date('Y-m-d_H').'.xlsx';
  4580. return Order::export_excel($result, $filename, $indexKey, $title);
  4581. }
  4582. /**
  4583. * 充值日报表
  4584. */
  4585. public function depositDayReport(Request $request){
  4586. $team_id = (int)$request->input('team_id');
  4587. $stime = $request->input('stime');
  4588. $etime = $request->input('etime');
  4589. $page = (int)$request->input('page');
  4590. $pageSize = 20;
  4591. if($page<=0){
  4592. $page = 1;
  4593. }
  4594. $offset = ($page-1) * $pageSize;
  4595. //获取团队及销售
  4596. $team_ids = Admin::getTeams();
  4597. $team_name_arr = Admin::getTeamsName();
  4598. $saler_ids_arr = Admin::getTeamSalers($team_ids);
  4599. $_data = $this->teamCtimePage($stime, $etime, $team_id, $offset, $pageSize, '2019-10-30');
  4600. $result = $_data['data'];
  4601. foreach($result as $k=>&$v){
  4602. #团队销售
  4603. $saler_ids = $saler_ids_arr[$v['team_id']];
  4604. $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();
  4605. $v['pay_amount'] = $today_deposit->pay_amount;
  4606. $v['discount'] = $today_deposit->discount;
  4607. $v['deposit_amount'] = $today_deposit->deposit_amount;
  4608. $v['gth_amount'] = $today_deposit->gth_amount;
  4609. $v['wx_amount'] = $v['pay_amount'] - $v['gth_amount'];
  4610. //团队名称
  4611. $v['team_name'] = $team_name_arr[$v['team_id']];
  4612. //累计充值金额
  4613. $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();
  4614. $v['pay_amount_total'] = $total_info->pay_amount_total;
  4615. $v['discount_total'] = $total_info->discount_total;
  4616. $v['deposit_amount_total'] = $total_info->deposit_amount_total;
  4617. //当日消费,订单数
  4618. $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();
  4619. $v['order_count'] = $consum->order_count;
  4620. $v['order_amount'] = $consum->order_amount;
  4621. //剩余实的金额
  4622. //$v['day_amount'] = $v['deposit_amount'] - $v['order_amount'];
  4623. }
  4624. $count = $_data['count'];
  4625. if ($count > 1) {
  4626. // 总页数
  4627. $pages = ceil($count/20);
  4628. }else{
  4629. // 总页数
  4630. $pages = 1;
  4631. }
  4632. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  4633. $teamList = json_decode(json_encode($teamList), true);
  4634. return view('statistics/depositDayReport', ['result' =>$result,
  4635. 'page' =>$page,
  4636. 'count' =>$count,
  4637. 'pages' =>$pages,
  4638. 'teamlist' =>$teamList,
  4639. 'team_id' =>$team_id,
  4640. 'stime' =>$stime,
  4641. 'etime' =>$etime,
  4642. ]);
  4643. }
  4644. /**
  4645. * 充值日报表
  4646. */
  4647. public function depositDayReport_export(Request $request){
  4648. $team_id = (int)$request->input('team_id');
  4649. $stime = $request->input('stime');
  4650. $etime = $request->input('etime');
  4651. //获取团队及销售
  4652. $team_ids = Admin::getTeams();
  4653. $team_name_arr = Admin::getTeamsName();
  4654. $saler_ids_arr = Admin::getTeamSalers($team_ids);
  4655. $_data = $this->teamCtimePage_export($stime, $etime, $team_id, '2019-10-30');
  4656. $result = $_data;
  4657. foreach($result as $k=>&$v){
  4658. #团队销售
  4659. $saler_ids = $saler_ids_arr[$v['team_id']];
  4660. $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();
  4661. $v['pay_amount'] = $today_deposit->pay_amount;
  4662. $v['discount'] = $today_deposit->discount;
  4663. $v['deposit_amount'] = $today_deposit->deposit_amount;
  4664. $v['gth_amount'] = $today_deposit->gth_amount;
  4665. $v['wx_amount'] = $v['pay_amount'] - $v['gth_amount'];
  4666. //团队名称
  4667. $v['team_name'] = $team_name_arr[$v['team_id']];
  4668. //累计充值金额
  4669. $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();
  4670. $v['pay_amount_total'] = $total_info->pay_amount_total;
  4671. $v['discount_total'] = $total_info->discount_total;
  4672. $v['deposit_amount_total'] = $total_info->deposit_amount_total;
  4673. //当日消费,订单数
  4674. $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();
  4675. $v['order_count'] = $consum->order_count;
  4676. $v['order_amount'] = $consum->order_amount;
  4677. }
  4678. $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'];
  4679. $title = ['团队', '日期', '当日充值金额', '当日赠送金额', '当日实得金额', '当日微信收款','当日个体户收款', '累计充值金额', '累计赠送金额', '累计实得总金额', '当日消费金额', '当日订单数'];
  4680. $filename = 'chongzhihuizong_'.date('Y-m-d_H').'.xlsx';
  4681. return Order::export_excel($result, $filename, $indexKey, $title);
  4682. }
  4683. /**
  4684. * 充值卡充值明细
  4685. */
  4686. public function depositDetail(Request $request){
  4687. $team_id = (int)$request->input('team_id');
  4688. $stime = $request->input('stime');
  4689. $etime = $request->input('etime');
  4690. $page = (int)$request->input('page');
  4691. $pageSize = 20;
  4692. if($page<=0){
  4693. $page = 1;
  4694. }
  4695. $offset = ($page-1) * $pageSize;
  4696. $saler_ids = null;
  4697. if($team_id>0){
  4698. $saler_ids_arr = Admin::getTeamSalers([$team_id]);
  4699. $saler_ids = $saler_ids_arr[$team_id];
  4700. }
  4701. $count = CustomerDeposit::where('is_del', 0)->where(function($query) use($saler_ids, $stime, $etime){
  4702. if($saler_ids !==null) $query->whereIn('admin_id', $saler_ids);
  4703. if($stime) $query->where('pay_time', '>=', $stime);
  4704. if($etime) $query->where('pay_time', '<=', $etime. ' 23:59:59');
  4705. })->count();
  4706. if ($count > 1) {
  4707. // 总页数
  4708. $pages = ceil($count/20);
  4709. }else{
  4710. // 总页数
  4711. $pages = 1;
  4712. }
  4713. $team_name_arr = Admin::getTeamsName();
  4714. $result = CustomerDeposit::where('is_del', 0)->where(function($query) use($saler_ids, $stime, $etime){
  4715. if($saler_ids !==null) $query->whereIn('admin_id', $saler_ids);
  4716. if($stime) $query->where('pay_time', '>=', $stime);
  4717. if($etime) $query->where('pay_time', '<=', $etime. ' 23:59:59');
  4718. })->orderBy('pay_time', 'desc')->offset($offset)->limit($pageSize)->get();
  4719. foreach($result as $k=>&$v){
  4720. //团队
  4721. $admin = Admin::where('id', $v['admin_id'])->first();
  4722. $v['admin_name'] = $admin->realname;
  4723. $v['team_name'] = $team_name_arr[$admin->team_id];
  4724. //客户名
  4725. $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
  4726. $v['payment_type'] = $v['payment_type'] == 3 ? '个体户' : '微信';
  4727. }
  4728. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  4729. $teamList = json_decode(json_encode($teamList), true);
  4730. return view('statistics/depositDetail', ['result' =>$result,
  4731. 'page' =>$page,
  4732. 'count' =>$count,
  4733. 'pages' =>$pages,
  4734. 'teamlist' =>$teamList,
  4735. 'team_id' =>$team_id,
  4736. 'stime' =>$stime,
  4737. 'etime' =>$etime,
  4738. ]);
  4739. }
  4740. /**
  4741. * 充值卡充值明细
  4742. */
  4743. public function depositDetail_export(Request $request){
  4744. $team_id = (int)$request->input('team_id');
  4745. $stime = $request->input('stime');
  4746. $etime = $request->input('etime');
  4747. $saler_ids = null;
  4748. if($team_id>0){
  4749. $saler_ids_arr = Admin::getTeamSalers([$team_id]);
  4750. $saler_ids = $saler_ids_arr[$team_id];
  4751. }
  4752. $team_name_arr = Admin::getTeamsName();
  4753. $result = CustomerDeposit::where('is_del', 0)->where(function($query) use($saler_ids, $stime, $etime){
  4754. if($saler_ids !==null) $query->whereIn('admin_id', $saler_ids);
  4755. if($stime) $query->where('pay_time', '>=', $stime);
  4756. if($etime) $query->where('pay_time', '<=', $etime. ' 23:59:59');
  4757. })->orderBy('pay_time', 'desc')->get();
  4758. foreach($result as $k=>&$v){
  4759. //团队
  4760. $admin = Admin::where('id', $v['admin_id'])->first();
  4761. $v['admin_name'] = $admin->realname;
  4762. $v['team_name'] = $team_name_arr[$admin->team_id];
  4763. //客户名
  4764. $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
  4765. $v['payment_type'] = $v['payment_type'] == 3 ? '个体户' : '微信';
  4766. }
  4767. $indexKey = ['team_name','pay_time','cust_name','admin_name','pay_amount','discount','deposit_amount','payment_type'];
  4768. $title = ['团队', '充值时间', '客户名称', '销售名称', '充值金额', '赠送金额', '实得金额', '充值方式'];
  4769. $filename = 'chongzhimingxi_'.date('Y-m-d_H').'.xlsx';
  4770. return Order::export_excel($result, $filename, $indexKey, $title);
  4771. }
  4772. /**
  4773. * 充值卡消费明细
  4774. */
  4775. public function consumDetail(Request $request){
  4776. $team_id = (int)$request->input('team_id');
  4777. $stime = $request->input('stime');
  4778. $etime = $request->input('etime');
  4779. $page = (int)$request->input('page');
  4780. $pageSize = 20;
  4781. if($page<=0){
  4782. $page = 1;
  4783. }
  4784. $offset = ($page-1) * $pageSize;
  4785. $count = Order::where('payment_type', 4)->where(function($query) use($stime, $etime, $team_id){
  4786. if($team_id>0) $query->where('team_id', $team_id);
  4787. if($stime) $query->where('createTime', '>=', $stime);
  4788. if($etime) $query->where('createTime', '<=', $etime. ' 23:59:59');
  4789. })->where('is_del', 0)->count();
  4790. if ($count > 1) {
  4791. // 总页数
  4792. $pages = ceil($count/20);
  4793. }else{
  4794. // 总页数
  4795. $pages = 1;
  4796. }
  4797. $result = Order::select('id', 'team_id', 'createTime', 'admin_name', 'receivedAmount')->where('payment_type', 4)->where(function($query) use($stime, $etime, $team_id){
  4798. if($team_id>0) $query->where('team_id', $team_id);
  4799. if($stime) $query->where('createTime', '>=', $stime);
  4800. if($etime) $query->where('createTime', '<=', $etime. ' 23:59:59');
  4801. })->where('is_del', 0)->orderBy('createTime', 'desc')->offset($offset)->limit($pageSize)->get();
  4802. $result = json_decode(json_encode($result), true);
  4803. $team_name_arr = Admin::getTeamsName();
  4804. foreach($result as $k=>&$v){
  4805. $v['team_name'] = $team_name_arr[$v['team_id']];
  4806. //消费记录
  4807. $consum = CustomerConsum::where('order_id', $v['id'])->where('type', 0)->first();
  4808. $v['money'] = $consum->money;
  4809. $v['phone'] = $consum->phone;
  4810. //客户名
  4811. $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
  4812. }
  4813. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  4814. $teamList = json_decode(json_encode($teamList), true);
  4815. return view('statistics/consumDetail', ['result' =>$result,
  4816. 'page' =>$page,
  4817. 'count' =>$count,
  4818. 'pages' =>$pages,
  4819. 'teamlist' =>$teamList,
  4820. 'team_id' =>$team_id,
  4821. 'stime' =>$stime,
  4822. 'etime' =>$etime,
  4823. ]);
  4824. }
  4825. /**
  4826. * 充值卡消费明细
  4827. */
  4828. public function consumDetail_export(Request $request){
  4829. $team_id = (int)$request->input('team_id');
  4830. $stime = $request->input('stime');
  4831. $etime = $request->input('etime');
  4832. $result = Order::select('id', 'team_id', 'createTime', 'admin_name', 'receivedAmount')->where('payment_type', 4)->where(function($query) use($stime, $etime, $team_id){
  4833. if($team_id>0) $query->where('team_id', $team_id);
  4834. if($stime) $query->where('createTime', '>=', $stime);
  4835. if($etime) $query->where('createTime', '<=', $etime. ' 23:59:59');
  4836. })->where('is_del', 0)->orderBy('createTime', 'desc')->get();
  4837. $result = json_decode(json_encode($result), true);
  4838. $team_name_arr = Admin::getTeamsName();
  4839. foreach($result as $k=>&$v){
  4840. $v['team_name'] = $team_name_arr[$v['team_id']];
  4841. //消费记录
  4842. $consum = CustomerConsum::where('order_id', $v['id'])->where('type', 0)->first();
  4843. $v['money'] = $consum->money;
  4844. $v['phone'] = $consum->phone;
  4845. //客户名
  4846. $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
  4847. }
  4848. $indexKey = ['team_name', 'id', 'createTime','cust_name','money','admin_name'];
  4849. $title = ['团队', '订单号', '下单时间', '客户名称', '消费金额', '销售名称'];
  4850. $filename = 'xiaofeimingxi_'.date('Y-m-d_H').'.xlsx';
  4851. return Order::export_excel($result, $filename, $indexKey, $title);
  4852. }
  4853. /**
  4854. * 客户充值月报
  4855. */
  4856. public function customerMonthAccount(Request $request){
  4857. $team_id = (int)$request->input('team_id');
  4858. $stime = $request->input('stime');
  4859. $etime = $request->input('etime');
  4860. $page = (int)$request->input('page');
  4861. $pageSize = 20;
  4862. if($page<=0){
  4863. $page = 1;
  4864. }
  4865. $offset = ($page-1) * $pageSize;
  4866. $_data = $this->depositCtimePage($team_id, $stime, $etime, $offset, $pageSize);
  4867. $count = $_data['count'];
  4868. if ($count > 1) {
  4869. // 总页数
  4870. $pages = ceil($count/20);
  4871. }else{
  4872. // 总页数
  4873. $pages = 1;
  4874. }
  4875. $team_name_arr = Admin::getTeamsName();
  4876. $result = $_data['data'];
  4877. foreach($result as $k=>&$v){
  4878. $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();
  4879. $v['money'] = $m_consum->money;
  4880. $v['month'] = $v['date'];
  4881. //用户
  4882. $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
  4883. //销售 -- 团队
  4884. $admin_id = CustomerInfo::where('phone', $v['phone'])->pluck('admin_id');
  4885. $v['team_id'] = Admin::where('id', $admin_id)->pluck('team_id');
  4886. $v['team_name'] = $team_name_arr[$v['team_id']];
  4887. //累计数据
  4888. $end = date('Y-m-1', strtotime($v['month'].' +1 month'));
  4889. //充值
  4890. $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();
  4891. $v['pay_amount_total'] = $deposit->pay_amount_total;
  4892. $v['discount_total'] = $deposit->discount_total;
  4893. $v['deposit_amount_total'] = $deposit->deposit_amount_total;
  4894. //消费
  4895. $consum = CustomerConsum::select(DB::raw('sum(money) as money'))->where('phone', $v['phone'])->where('is_del', 0)->where('create_time', '<', $end)->first();
  4896. $v['consum_total'] = $consum->money;
  4897. $v['balance'] = $v['deposit_amount_total'] - $v['consum_total'];
  4898. }
  4899. $teamList = DB::table('teams')->select('id', 'name')->where('type', 1)->get();
  4900. $teamList = json_decode(json_encode($teamList), true);
  4901. return view('statistics/customerMonthAccount', ['result' =>$result,
  4902. 'page' =>$page,
  4903. 'count' =>$count,
  4904. 'pages' =>$pages,
  4905. 'team_id' => $team_id,
  4906. 'teamlist' => $teamList,
  4907. 'stime' =>$stime,
  4908. 'etime' =>$etime,
  4909. ]);
  4910. }
  4911. /**
  4912. * 客户充值月报
  4913. */
  4914. public function customerMonthAccount_export(Request $request){
  4915. $team_id = (int)$request->input('team_id');
  4916. $stime = $request->input('stime');
  4917. $etime = $request->input('etime');
  4918. $result = $this->depositCtimePage($team_id,$stime, $etime, 0, 0, 1);
  4919. $team_name_arr = Admin::getTeamsName();
  4920. foreach($result as $k=>&$v){
  4921. $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();
  4922. $v['money'] = $m_consum->money;
  4923. $v['month'] = $v['date'];
  4924. //用户
  4925. $v['cust_name'] = CustomerInfo::where('phone', $v['phone'])->pluck('name');
  4926. //销售 -- 团队
  4927. $admin_id = CustomerInfo::where('phone', $v['phone'])->pluck('admin_id');
  4928. $v['team_id'] = Admin::where('id', $admin_id)->pluck('team_id');
  4929. $v['team_name'] = $team_name_arr[$v['team_id']];
  4930. //累计数据
  4931. $end = date('Y-m-1', strtotime($v['month'].' +1 month'));
  4932. //充值
  4933. $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();
  4934. $v['pay_amount_total'] = $deposit->pay_amount_total;
  4935. $v['discount_total'] = $deposit->discount_total;
  4936. $v['deposit_amount_total'] = $deposit->deposit_amount_total;
  4937. //消费
  4938. $consum = CustomerConsum::select(DB::raw('sum(money) as money'))->where('phone', $v['phone'])->where('is_del', 0)->where('create_time', '<', $end)->first();
  4939. $v['consum_total'] = $consum->money;
  4940. $v['balance'] = $v['deposit_amount_total'] - $v['consum_total'];
  4941. }
  4942. $indexKey = ['month', 'team_name', 'cust_name', 'pay_amount_total','discount_total','deposit_amount_total','money','consum_total','balance'];
  4943. $title = ['月份', '团队', '充值用户', '累计充值', '累计赠送', '累计实得', '当月消费', '累计消费', '剩余实得'];
  4944. $filename = 'chongzhiyuebao_'.date('Y-m-d_H').'.xlsx';
  4945. return Order::export_excel($result, $filename, $indexKey, $title);
  4946. }
  4947. /**
  4948. * 充值客户财务月报时间格式处理
  4949. */
  4950. public function depositCtimePage($team_id, $stime, $etime, $offset, $pageSize, $is_export=0){
  4951. $result = array();
  4952. $sdate = '2019-11';
  4953. $edate = date('Y-m');
  4954. if($stime && $stime>$sdate){
  4955. $sdate = $stime;
  4956. }
  4957. if($etime && $etime<$edate){
  4958. $edate = $etime;
  4959. }
  4960. if($sdate> $edate){
  4961. return ['data'=>[], 'count'=>0];
  4962. }
  4963. $saler_ids = null;
  4964. if($team_id>0){
  4965. $saler_ids = Admin::where('team_id', $team_id)->lists('id');
  4966. }
  4967. /*$custs = CustomerInfo::where(function($query) use($saler_ids){
  4968. if($saler_ids !== null) $query->whereIn('admin_id', $saler_ids);
  4969. })->lists('phone');*/
  4970. for($i=0;$i<36;$i++){
  4971. $d = $sdate. ' +'.$i.' month';
  4972. $date = date('Y-m', strtotime($d));
  4973. $cust_end_time = date('Y-m-01', strtotime($date.' +1 month'));
  4974. #逻辑优化,只统计当月及之前创建的客户
  4975. $custs = CustomerInfo::where(function($query) use($saler_ids){
  4976. if($saler_ids !== null) $query->whereIn('admin_id', $saler_ids);
  4977. })->where('create_time', '<', $cust_end_time)->lists('phone');
  4978. foreach($custs as $phone){
  4979. $result[] = ['date'=>$date, 'phone'=>$phone];
  4980. }
  4981. if($date == $edate){
  4982. break;
  4983. }
  4984. }
  4985. if($is_export == 1){
  4986. return $result;
  4987. }
  4988. $count = count($result);
  4989. $ret['data'] = array_slice($result, $offset, $pageSize);
  4990. $ret['count'] = $count;
  4991. return $ret;
  4992. }
  4993. public function newFanMonthAmountList(Request $request) {
  4994. $stime = $request->input('stime');
  4995. $etime = $request->input('etime');
  4996. $team_id = (int)$request->input('team_id',17);
  4997. $self_role = session('role_name');
  4998. #销售和审核人员只能看自己团队的
  4999. if($self_role == '管理员' || $self_role == '销售'){
  5000. $self_id = session('admin_id');
  5001. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  5002. }
  5003. if($stime == '' && $etime == ''){
  5004. $stime = date("Y-m", strtotime('-13 month'));
  5005. $etime = date("Y-m", strtotime('-1 month'));
  5006. }
  5007. $result = DB::table('cust_month_remain')->where('team_id',$team_id)
  5008. ->where(function($query) use($stime, $etime, $team_id){
  5009. if($stime) $query->where('idate','>=', $stime.'-01');
  5010. if($etime) $query->where('idate','<=', $etime.'-01');
  5011. })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
  5012. #获取列
  5013. $columns = array();
  5014. $i_max = 13;
  5015. for($i=0; $i<$i_max; $i++){
  5016. $columns[] = $i;
  5017. }
  5018. #数据整合
  5019. $data = array();
  5020. foreach($result as $k=>$v){
  5021. $key = $v->idate;
  5022. $amount_data = json_decode($v->amount_data, true);
  5023. $data[$key] = isset($data[$key]) ? array_merge($data[$key], $amount_data) : $amount_data;
  5024. }
  5025. $saler_ids = null;
  5026. if( $team_id>0 ){
  5027. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  5028. }
  5029. #数据格式化
  5030. $new_data = array();
  5031. foreach($data as $k=>$v){
  5032. //当月加粉数
  5033. $k =date('Y-m',strtotime($k));
  5034. $end = $this->getthemonth($k);
  5035. $fan_add = DB::table('cust_day_detail')->where('dtime', '>=', $k)->where('dtime', '<=', $end)->where('is_del',0)->where(function($query) use ($saler_ids) {
  5036. if( $saler_ids ) $query->whereIn('admin_id',$saler_ids);
  5037. })->sum('fan_add');
  5038. $new_data[$k]['fan_add'] = $fan_add;
  5039. foreach($columns as $val){
  5040. $n = $val;
  5041. $new_data[$k]['amount'][] = isset($v[$n]['amount']) ? $v[$n]['amount'] : '';
  5042. }
  5043. }
  5044. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  5045. if($team_id>0 && ($self_role=='管理员' || $self_role =='销售')) $query->where('id', $team_id);
  5046. })->where('type', 1)->get();
  5047. $teamList = json_decode(json_encode($teamList), true);
  5048. return view('statistics/newFanMonthAmountList', [
  5049. 'result' =>$new_data,
  5050. 'columns'=>$columns,
  5051. 'stime'=>$stime,
  5052. 'etime'=>$etime,
  5053. 'team_id'=>$team_id,
  5054. 'teamlist'=>$teamList,
  5055. ]);
  5056. }
  5057. public function getthemonth($date)
  5058. {
  5059. $firstday = date('Y-m-01', strtotime($date));
  5060. $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
  5061. return $lastday;
  5062. }
  5063. }