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