Ei kuvausta

StatisticsController.php 245KB

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