No Description

StatisticsController.php 271KB

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