No Description

StatisticsController.php 270KB

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