123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496 |
- <?php
- /**
- * Created by Sublime.
- * User: hao
- * Date: 19/08/30
- * Time: 上午11:20
- */
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use App\Logs;
- use App\CustTotal;
- use App\CustDetail;
- use App\AdCost;
- use App\Order;
- use App\DistrictRoi7;
- use App\DistrictRoi30;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- class StatisticsController extends Controller
- {
- /**
- * 当日数据统计
- */
- public function fanDay(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $self_role = session('role_name');
- $team_id = $request->input('team_id');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- if($admin_id>0 && !$team_id){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- #进粉成本
- $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
- #当日微信粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids, $admin_id){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
- $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(freight_cost) as freight_cost, sum(cost) as goods_cost'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- #物流成本
- $v['freight_cost'] = $order->freight_cost;
- #货品成本
- $v['goods_cost'] = $order->goods_cost;
- #新加逻辑 -- 销售筛选
- if($admin_id>0){
- //预估当日投放 当日公众进粉
- # 1. 当前团队销售加粉占比
- $teamFanAdd = CustDetail::select(DB::raw('sum(fan_add) as team_fan_add'))->whereIn('admin_id', $sale_ids)->where('dtime', $v['dtime'])->where('is_del', 0)->first();
- $salerfanRate = $teamFanAdd->team_fan_add>0 ? round($v['wx_fan_add'] / $teamFanAdd->team_fan_add, 3) * 1000 : 0;
- #预估该销售投放金额
- $v['total_cost'] = round( $salerfanRate * $v['total_cost'] / 1000, 2);
- #预估进粉
- $v['total_fan_add'] = round( $salerfanRate * $v['total_fan_add'] / 1000);
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- }
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
- })->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where(function($query) use($self_role, $team_id){
- if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('team_id', $team_id);
- })->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/fanDay', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- ]);
- }
- public function fanDay_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $self_role = session('role_name');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="当日数据统计.xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日投放金额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日公众号进粉")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日订单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日老粉主动咨询量")."</th>
- </tr>";
- foreach ($result as $k => $v)
- {
- #进粉成本
- $v['cost_fan'] = $v['total_fan_add']>0 ? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
- #当日微信粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
- $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
- $v['new_reply_rate'] = $v['wx_fan_add']>0 ? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0 ? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['total_cost']."</td>";
- $data_str .= "<td>".$v['total_fan_add']."</td>";
- $data_str .= "<td>".$v['cost_fan']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['cost_wx_fan']."</td>";
- $data_str .= "<td>".$v['order_count']."</td>";
- $data_str .= "<td>".$v['order_amount']."</td>";
- $data_str .= "<td>".$v['total_new_reply']."</td>";
- $data_str .= "<td>".$v['new_reply_rate']."</td>";
- $data_str .= "<td>".$v['total_old_consult']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 当日数据统计
- */
- public function orderDay(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $self_role = session('role_name');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
- $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime, $end_time){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime','<', $end_time);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime, $end_time){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime','<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- #当日微信好友数量
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(cost) as order_cost, sum(freight_cost) as freight_cost'))->whereIn('receiverMobile', $phones)->where('createTime','<',$end_time)->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- $old_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>', $v['dtime'].' 23:59:59')->where('createTime','<', $end_time)->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 3.老粉成单数
- $v['old_order_count'] = $old_order->order_count;
- // 4.老粉成交额
- $v['old_order_amount'] = $old_order->order_amount;
- // 5.总成交额
- $v['order_amount'] = $order->order_amount;
- #复购单数、复购成交额
- $fugou = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where('is_fugou', 1)->where('createTime','<', $end_time)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['fugou_order_count'] = $fugou->order_count;
- $v['fugou_order_amount'] = $fugou->order_amount;
- #货品成本
- $v['order_cost'] = $order->order_cost;
- //加物流成本
- $v['freight_cost'] = $order->freight_cost;
- #毛利
- $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'] - $v['freight_cost'];
- //综合成单率 综合成单率=订单数/微信好友数
- $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
- //新粉成单率
- $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
- //复购率
- $fugou_order_count_no = $order->order_count - $fugou->order_count;
- $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
- //加roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
- })->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('statistics/orderDay', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- ]);
- }
- public function orderDay_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $self_role = session('role_name');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime, $end_time){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime','<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="分片数据统计.xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉数量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","总投放成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","货品成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","毛利")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
- </tr>";
- foreach ($result as $k => $v)
- {
- #当日微信好友数量
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count, sum(cost) as order_cost'))->whereIn('receiverMobile', $phones)->where('createTime', '<', $end_time)->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- $old_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>', $v['dtime'].' 23:59:59')->where('createTime','<', $end_time)->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- // 3.老粉成单数
- $v['old_order_count'] = $old_order->order_count;
- // 4.老粉成交额
- $v['old_order_amount'] = $old_order->order_amount;
- // 5.总成交额
- $v['order_amount'] = $order->order_amount;
- #复购单数、复购成交额
- $fugou = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where('is_fugou', 1)->where('createTime','<', $end_time)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['fugou_order_count'] = $fugou->order_count;
- $v['fugou_order_amount'] = $fugou->order_amount;
- #货品成本
- $v['order_cost'] = $order->order_cost;
- #毛利
- $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'];
- //综合成单率 综合成单率=订单数/微信好友数
- $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
- //新粉成单率
- $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
- //复购率
- $fugou_order_count_no = $order->order_count - $fugou->order_count;
- $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['total_fan_add']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['new_order_count']."</td>";
- $data_str .= "<td>".$v['new_order_amount']."</td>";
- $data_str .= "<td>".$v['old_order_count']."</td>";
- $data_str .= "<td>".$v['old_order_amount']."</td>";
- $data_str .= "<td>".$v['fugou_order_count']."</td>";
- $data_str .= "<td>".$v['fugou_order_amount']."</td>";
- $data_str .= "<td>".$v['order_amount']."</td>";
- $data_str .= "<td>".$v['total_cost']."</td>";
- $data_str .= "<td>".$v['order_cost']."</td>";
- $data_str .= "<td>".$v['profit']."</td>";
- $data_str .= "<td>".$v['new_order_rate']."</td>";
- $data_str .= "<td>".$v['order_rate']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 当日数据统计(投放)
- */
- public function throwDay(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- #进粉成本
- $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
- #当日微信粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
- $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('statistics/throwDay', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- ]);
- }
- public function throwDay_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = CustTotal::select(DB::raw('sum(total_cost) as total_cost, sum(total_fan_add) as total_fan_add, dtime'))->where(function($query) use($team_id, $stime, $etime){
- if($team_id) $query->where('team_id', '=', $team_id);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="当日数据统计.xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日投放金额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日公众号进粉")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉成本")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉成本")."</th>
- </tr>";
- foreach ($result as $k => $v)
- {
- #进粉成本
- $v['cost_fan'] = $v['total_fan_add']>0 ? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
- #当日微信粉
- $custDetail = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult'))->where('dtime', $v['dtime'])->where('is_del', 0)->where(function($query) use ($team_id, $sale_ids){
- if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- })->first();
- $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
- $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
- $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
- $v['new_reply_rate'] = $v['wx_fan_add']>0 ? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- #当日微信粉成本
- $v['cost_wx_fan'] = $v['wx_fan_add']>0 ? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id){
- if($team_id>0) $query->where('team_id', $team_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['total_cost']."</td>";
- $data_str .= "<td>".$v['total_fan_add']."</td>";
- $data_str .= "<td>".$v['cost_fan']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['cost_wx_fan']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 销售可看当日数据表
- *
- */
- public function fanDaySaler(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $is_self_team = (int)$request->input('is_self_team');
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
-
- $self_role = session('role_name');
- if($self_role == '销售'){
- $admin_id = session('admin_id');
- #假如查看团队的
- if($is_self_team == 1){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- $admin_id = 0;
- }else{
- $team_id = null;
- }
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- if($end_time) $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult, sum(new_consult) as total_new_consult, dtime'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- if($end_time) $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.新粉成交转化率
- $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
- //累计老粉量
- $dtime = $v['dtime'];
- $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if($sale_ids) $query->whereIn('admin_id', $sale_ids);
- if($dtime) $query->where('dtime', '<', $dtime);
- })->where('is_del',0)->sum('fan_add');
- $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- // 3.老粉成单数
- $v['old_order_count'] = $order->order_count - $v['new_order_count'];
- // 4.老粉成交额
- $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
- // 5.老粉成交转化率
- $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/fanDaySaler', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'is_self_team' =>$is_self_team,
- 'self_role' =>$self_role,
- ]);
- }
- public function fanDaySaler_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $is_self_team = (int)$request->input('is_self_team');
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
-
- $self_role = session('role_name');
- if($self_role == '销售'){
- $admin_id = session('admin_id');
- #假如查看团队的
- if($is_self_team == 1){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- $admin_id = 0;
- }else{
- $team_id = null;
- }
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult, sum(new_consult) as total_new_consult, dtime'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- if($end_time) $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="当日数据统计(销售).xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
-
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉增加量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交单量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交转化率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","累计老粉量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交单量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交转化率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日单量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
- </tr>";
- foreach ($result as $k => $v)
- {
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.新粉成交转化率
- $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
- //累计老粉量
- $dtime = $v['dtime'];
- $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if($sale_ids) $query->whereIn('admin_id', $sale_ids);
- if($dtime) $query->where('dtime', '<', $dtime);
- })->where('is_del',0)->sum('fan_add');
- $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
- #当日订单数、销售额
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['order_count'] = $order->order_count;
- $v['order_amount'] = $order->order_amount;
- // 3.老粉成单数
- $v['old_order_count'] = $order->order_count - $v['new_order_count'];
- // 4.老粉成交额
- $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
- // 5.老粉成交转化率
- $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['total_new_reply']."</td>";
- $data_str .= "<td>".$v['new_reply_rate']."</td>";
- $data_str .= "<td>".$v['total_new_consult']."</td>";
- $data_str .= "<td>".$v['new_consult_rate']."</td>";
- $data_str .= "<td>".$v['new_order_count']."</td>";
- $data_str .= "<td>".$v['new_order_amount']."</td>";
- $data_str .= "<td>".$v['new_order_rate']."</td>";
- $data_str .= "<td>".$v['wx_old_fan']."</td>";
- $data_str .= "<td>".$v['total_old_consult']."</td>";
- $data_str .= "<td>".$v['old_consult_rate']."</td>";
- $data_str .= "<td>".$v['old_order_count']."</td>";
- $data_str .= "<td>".$v['old_order_amount']."</td>";
- $data_str .= "<td>".$v['old_order_rate']."</td>";
- $data_str .= "<td>".$v['order_count']."</td>";
- $data_str .= "<td>".$v['order_amount']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 销售可看当日分片数据
- *
- */
- public function orderDaySaler(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $is_self_team = (int)$request->input('is_self_team');
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
-
- $self_role = session('role_name');
- if($self_role == '销售'){
- $admin_id = session('admin_id');
- #假如查看团队的
- if($is_self_team == 1){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- $admin_id = 0;
- }else{
- $team_id = null;
- }
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->first();
- $count = $count->total;
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult, sum(new_consult) as total_new_consult, dtime'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result),true);
- foreach($result as $k=>&$v){
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- #当日老粉成单
- $old_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>', $v['dtime'].' 23:59:59')->where('createTime','<', $end_time)->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日老粉成单数
- $v['old_order_count'] = $old_order->order_count;
- // 2.当日老粉成交额
- $v['old_order_amount'] = $old_order->order_amount;
- #复购单数、复购成交额
- $fugou = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where('is_fugou', 1)->where('createTime','<', $end_time)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['fugou_order_count'] = $fugou->order_count;
- $v['fugou_order_amount'] = $fugou->order_amount;
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('createTime','<',$end_time)->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- //总成交额
- $v['order_amount'] = $order->order_amount;
- //综合成单率 综合成单率=订单数/微信好友数
- $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
- //新粉成单率
- $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
- //复购率
- $fugou_order_count_no = $order->order_count - $fugou->order_count;
- $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
-
- }
- $teamList = DB::table('teams')->select('id', 'name')->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/orderDaySaler', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'is_self_team' =>$is_self_team,
- 'self_role' =>$self_role,
- ]);
- }
- public function orderDaySaler_export(Request $request){
-
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $is_self_team = (int)$request->input('is_self_team');
- //规定只统计前天及以前的数据
- $end_time = date('Y-m-d');
-
- $self_role = session('role_name');
- if($self_role == '销售'){
- $admin_id = session('admin_id');
- #假如查看团队的
- if($is_self_team == 1){
- $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
- $admin_id = 0;
- }else{
- $team_id = null;
- }
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = CustDetail::select(DB::raw('sum(fan_add) as wx_fan_add, sum(new_reply) as total_new_reply, sum(old_consult) as total_old_consult, sum(new_consult) as total_new_consult, dtime'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
- if($admin_id) $query->where('admin_id', '=', $admin_id);
- if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if($stime) $query->where('dtime', '>=', $stime);
- if($etime) $query->where('dtime', '<=', $etime);
- $query->where('dtime', '<', $end_time);
- })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
- $result = json_decode(json_encode($result),true);
- $filename="当日数据统计(销售).xls";
- header("Content-type:application/vnd.ms-excel");
- Header("Accept-Ranges:bytes");
- Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
- header("Pragma: no-cache");
- header("Expires: 0");
- $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
- xmlns:x="urn:schemas-microsoft-com:office:excel"
- xmlns="http://www.w3.org/TR/REC-html40">
- <head>
- <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
- <meta http-equiv=Content-Type content="text/html; charset=gb2312">
- <!--[if gte mso 9]><xml>
- <x:ExcelWorkbook>
- <x:ExcelWorksheets>
- <x:ExcelWorksheet>
- <x:Name></x:Name>
- <x:WorksheetOptions>
- <x:DisplayGridlines/>
- </x:WorksheetOptions>
- </x:ExcelWorksheet>
- </x:ExcelWorksheets>
- </x:ExcelWorkbook>
- </xml><![endif]-->
- </head>';
-
- $data_str .= "
- <table>
- <tr>
- <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","复购率")."</th>
- <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
-
- </tr>";
- foreach ($result as $k => $v)
- {
- $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
- $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
- //当日加粉
- $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['dtime'])->where('createTime','<', $v['dtime'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- #当日老粉成单
- $old_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>', $v['dtime'].' 23:59:59')->where('createTime','<', $end_time)->where('is_del', 0)->whereIn('receiverMobile', $phones)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- // 1.当日老粉成单数
- $v['old_order_count'] = $old_order->order_count;
- // 2.当日老粉成交额
- $v['old_order_amount'] = $old_order->order_amount;
- #复购单数、复购成交额
- $fugou = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->where('is_fugou', 1)->where('createTime','<', $end_time)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- $v['fugou_order_count'] = $fugou->order_count;
- $v['fugou_order_amount'] = $fugou->order_amount;
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('createTime','<',$end_time)->where('is_del', 0)->where(function($query) use($team_id, $admin_id){
- if($team_id>0) $query->where('team_id', $team_id);
- if($admin_id>0) $query->where('admin_id', $admin_id);
- })->first();
- //总成交额
- $v['order_amount'] = $order->order_amount;
- //综合成单率 综合成单率=订单数/微信好友数
- $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
- //新粉成单率
- $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
- //复购率
- $fugou_order_count_no = $order->order_count - $fugou->order_count;
- $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
- $data_str .= "<tr>";
- $data_str .= "<td>".$v['dtime']."</td>";
- $data_str .= "<td>".$v['wx_fan_add']."</td>";
- $data_str .= "<td>".$v['new_order_count']."</td>";
- $data_str .= "<td>".$v['new_order_amount']."</td>";
- $data_str .= "<td>".$v['old_order_count']."</td>";
- $data_str .= "<td>".$v['old_order_amount']."</td>";
- $data_str .= "<td>".$v['fugou_order_count']."</td>";
- $data_str .= "<td>".$v['fugou_order_amount']."</td>";
- $data_str .= "<td>".$v['order_amount']."</td>";
- $data_str .= "<td>".$v['new_order_rate']."</td>";
- $data_str .= "<td>".$v['fugou_rate']."</td>";
- $data_str .= "<td>".$v['order_rate']."</td>";
- $data_str .= "</tr>";
- }
- $data_str .= "</table>";
- echo $data_str;
- exit;
- }
- /**
- * 成单率梯形图
- */
- public function orderRateList(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $self_role = session('role_name');
- $team_id = $request->input('team_id');
- #只能看自己团队的
- if($self_role != '超级管理员' && $self_role != '售后管理员'){
- $self_id = session('admin_id');
- $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
- }
- if($team_id>0){
- $result = $this->orderTeamRateList($stime, $etime, $team_id);
- return view('statistics/orderRateList', $result);
- }
- $_day = 30;
- if($stime == '' && $etime == ''){
- $stime = date("Y-m-d", strtotime('-32 day'));
- $etime = date("Y-m-d", strtotime('-2 day'));
- }else{
- if($stime){
- $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
- }
- }
- #统计天数
- $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
- $day_count = DB::table('cust_day_remain')->where('month_time', $month)->where(function($query) use($stime, $etime){
- if($stime) $query->where('idate','>=', $stime);
- if($etime) $query->where('idate','<=', $etime);
- })->count();
- $result = DB::table('cust_day_remain')->where(function($query) use($stime, $etime){
- if($stime) $query->where('idate','>=', $stime);
- if($etime) $query->where('idate','<=', $etime);
- })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
- if($stime)
- #获取列
- $columns = array();
- $i_max = 31;
- for($i=0; $i<$i_max; $i++){
- $columns[] = $i;
- }
- if($_day >= 45){
- $columns[] = 45;
- }
- if($_day >= 60){
- $columns[] = 60;
- }
- if($_day >= 75){
- $columns[] = 75;
- }
- if($_day >= 90){
- $columns[] = 90;
- }
- #数据整合
- $data = array();
- foreach($result as $k=>$v){
- $key = $v->idate;
- $rate_data = json_decode($v->order_rate_data, true);
- $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
- }
- #数据格式化
- $new_data = array();
- foreach($data as $k=>$v){
- //当日加粉数
- $fan_add = DB::table('cust_day_detail')->where('dtime', $k)->where('is_del',0)->sum('fan_add');
- $new_data[$k]['fan_add'] = $fan_add;
- foreach($columns as $val){
- $n = $val;
- $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
- }
- }
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
- })->get();
- $teamList = json_decode(json_encode($teamList), true);
- //echo '<pre>';
- //print_r($new_data);print_r($columns);exit;
- return view('statistics/orderRateList', ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList]);
- }
- /**
- * 团队成单率梯形图
- */
- public function orderTeamRateList($stime, $etime, $team_id){
- $_day = 30;
- if($stime == '' && $etime == ''){
- $stime = date("Y-m-d", strtotime('-32 day'));
- $etime = date("Y-m-d", strtotime('-2 day'));
- }else{
- if($stime){
- $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
- }
- }
- #统计天数
- $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
- $day_count = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('month_time', $month)->where(function($query) use($stime, $etime){
- if($stime) $query->where('idate','>=', $stime);
- if($etime) $query->where('idate','<=', $etime);
- })->count();
- $result = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where(function($query) use($stime, $etime){
- if($stime) $query->where('idate','>=', $stime);
- if($etime) $query->where('idate','<=', $etime);
- })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
- if($stime)
- #获取列
- $columns = array();
- $i_max = 31;
- for($i=0; $i<$i_max; $i++){
- $columns[] = $i;
- }
- if($_day >= 45){
- $columns[] = 45;
- }
- if($_day >= 60){
- $columns[] = 60;
- }
- if($_day >= 75){
- $columns[] = 75;
- }
- if($_day >= 90){
- $columns[] = 90;
- }
- #数据整合
- $data = array();
- foreach($result as $k=>$v){
- $key = $v->idate;
- $rate_data = json_decode($v->order_rate_data, true);
- $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
- }
- #团队成员:
- $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- #数据格式化
- $new_data = array();
- foreach($data as $k=>$v){
- //当日加粉数
- $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('dtime', $k)->where('is_del',0)->sum('fan_add');
- $new_data[$k]['fan_add'] = $fan_add;
- foreach($columns as $val){
- $n = $val;
- $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
- }
- }
- //echo '<pre>';
- //print_r($new_data);print_r($columns);exit;
- $self_role = session('role_name');
- $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
- if($self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
- })->get();
- $teamList = json_decode(json_encode($teamList), true);
- return ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList];
- }
- /**
- * orderSaleRank 实时销售数据展榜
- */
- public function orderSaleRank(Request $request){
- header('Location:https://datav.aliyuncs.com/share/c936e72d66c7b5900238cf51cbd31884');
- exit;
- }
- /**
- * orderDistrict 实时订单地域分布
- */
- public function orderDistrict(Request $request){
- header('Location:https://datav.aliyuncs.com/share/423b408234ea48d49a2a4ee7f33fa4e4');
- exit;
- }
- /**
- * 地区roi
- */
- public function districtRoi(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $ret = AdCost::select('id')->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('ad_time')->groupBy('city')->get();
- $ret = json_decode(json_encode($ret), true);
- $count = count($ret);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city, ad_time'))->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉收入
- //当日加粉
- $city_name = str_replace('市', '', $v['city']);
- $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['ad_time'])->where('createTime','<=', $v['ad_time'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.当日粉丝总成交额
- $v['order_amount'] = $order->order_amount;
- // 总单数
- $v['order_count'] = $order->order_count;
- //新粉roi
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_fan_count'] = count($phones);
- }
- //总计
- $total_data = [
- 'cost' => 0,
- 'fan_count' => 0,
- 'order_count' => 0,
- 'order_amount' => 0,
- 'roi' => 0,
- 'conversion_times' => 0,
- 'new_order_count' => 0,
- 'new_order_amount' => 0,
- 'new_roi' => 0,
- ];
- //投入
- $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->first();
- $total_data['cost'] = $adcost->total_cost;
- $total_data['conversion_times'] = $adcost->conversion_times;
- //总新粉
- $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('fanTime', '>=', $stime);
- if($etime) $query->where('fanTime', '<=', $etime);
- if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
- })->lists('phone');
- $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
- //总收入
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
- $total_data['order_count'] = $order->order_count;
- $total_data['order_amount'] = $order->order_amount;
- $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
- #总新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
- $total_data['new_order_count'] = $new_order->order_count;
- $total_data['new_order_amount'] = $new_order->order_amount;
- $total_data['new_roi'] = $total_data['cost']>0 ? round($total_data['new_order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
- return view('statistics/districtRoi', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'city' =>$city,
- 'total_data' =>$total_data,
- ]);
- }
- /**
- * 导出地域roi报表
- */
- public function districtRoi_export(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city, ad_time'))->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉收入
- //当日加粉
- $city_name = str_replace('市', '', $v['city']);
- $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
- #当日加粉订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
- #当日新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->where('createTime','>=', $v['ad_time'])->where('createTime','<=', $v['ad_time'].' 23:59:59')->where('is_del', 0)->whereIn('receiverMobile', $phones)->first();
- // 1.当日新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.当日新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.当日粉丝总成交额
- $v['order_amount'] = $order->order_amount;
- // 总单数
- $v['order_count'] = $order->order_count;
- //新粉roi
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_fan_count'] = count($phones);
- }
- //总计
- $total_data = [
- 'cost' => 0,
- 'fan_count' => 0,
- 'order_count' => 0,
- 'order_amount' => 0,
- 'roi' => 0,
- 'conversion_times' => 0,
- 'new_order_count' => 0,
- 'new_order_amount' => 0,
- 'new_roi' => 0,
- ];
- //投入
- $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->first();
- $total_data['cost'] = $adcost->total_cost;
- $total_data['conversion_times'] = $adcost->conversion_times;
- //总新粉
- $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('fanTime', '>=', $stime);
- if($etime) $query->where('fanTime', '<=', $etime);
- if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
- })->lists('phone');
- $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
- //总收入
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
- $total_data['order_count'] = $order->order_count;
- $total_data['order_amount'] = $order->order_amount;
- $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
- #总新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
- $total_data['new_order_count'] = $new_order->order_count;
- $total_data['new_order_amount'] = $new_order->order_amount;
- $total_data['new_roi'] = $total_data['cost']>0 ? round($total_data['new_order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
- $indexKey = ['ad_time','city','total_cost','conversion_times','new_fan_count','new_order_count','new_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['日期', '城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'district_roi_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 团队订单财务明细
- */
- public function teamFinanceList(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $aftersale = (int)$request->input('aftersale'); //售后
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $count = Order::where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
- if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
- if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
- if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time')->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
- if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
- if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
- if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->orderBy('team_id', 'asc');
- if($aftersale == 1){
- $result = $resultQuery->orderBy('aftersale_time','desc')->offset($offset)->limit($pageSize)->get();
- }else{
- $result = $resultQuery->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
- }
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付'];
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
- $v['payment_type'] = $payArr[$v['payment_type']];
- $v['ctime'] = $aftersale == 1 ? $v['aftersale_time'] : $v['createTime'];
- }
- $teamList = DB::table('teams')->select('id', 'name')->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('/statistics/teamFinanceList',[
- 'result' => $result,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'teamlist' => $teamList,
- 'aftersale' => $aftersale,
- ]);
- }
- /**
- * 团队订单财务汇总列表
- */
- public function teamFinanceTotal(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $aftersale = (int)$request->input('aftersale');
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- if($aftersale == 1){
- $countQuery = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id'));
- }else{
- $countQuery = Order::select(DB::raw('left(createTime, 10) as ctime, team_id'));
- }
- $count = $countQuery->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
- if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
- if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
- if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $teams = DB::table('teams')->lists('name', 'id');
- if($aftersale == 1){
- $result = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('aftersale_time', '>=', $stime);
- if($etime) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('aftersale_time','desc')->offset($offset)->limit($pageSize)->get();
- }else{
- $result = Order::select(DB::raw('left(createTime, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
- }
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
- $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
- $v['zfm_money'] = $v['receivedAmount'] - $v['wx_money'];
- }
- $teamList = DB::table('teams')->select('id', 'name')->get();
- $teamList = json_decode(json_encode($teamList), true);
- return view('/statistics/teamFinanceTotal',[
- 'result' => $result,
- 'team_id' => $team_id,
- 'stime' => $stime,
- 'etime' => $etime,
- 'page' => $page,
- 'count' => $count,
- 'pages' => $pages,
- 'teamlist' => $teamList,
- 'aftersale' => $aftersale,
- ]);
- }
- /**
- * 团队财务明细导出
- */
- public function teamFinanceList_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $aftersale = $request->input('aftersale');
-
- $teams = DB::table('teams')->lists('name', 'id');
- $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time')->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
- if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
- if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
- if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->orderBy('team_id', 'asc');
- if($aftersale == 1){
- $result = $resultQuery->orderBy('aftersale_time','desc')->get();
- }else{
- $result = $resultQuery->orderBy('createTime','desc')->get();
- }
- $result = json_decode(json_encode($result), true);
- $payArr = [1=>'微信支付', 2=>'付款码支付'];
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
- $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
- $v['payment_type'] = $payArr[$v['payment_type']];
- }
- if($aftersale == 0) $indexKey = ['team_name','createTime','id','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost', 'payment_type'];
- if($aftersale == 1) $indexKey = ['team_name','aftersale_time','id','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost', 'payment_type'];
- $title = ['团队', '时间', '订单编号', '成交金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本', '支付方式'];
- $filename = $aftersale==0 ? 'dingdanmingxi'.date('Y-m-d_H').'.xlsx' : 'shouhoumingxi'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 团队财务汇总导出
- */
- public function teamFinanceTotal_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $aftersale = $request->input('aftersale');
-
- $teams = DB::table('teams')->lists('name', 'id');
- if($aftersale == 1){
- $result = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('aftersale_time', '>=', $stime);
- if($etime) $query->where('aftersale_time', '<=', $etime.' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('aftersale_time','desc')->get();
- }else{
- $result = Order::select(DB::raw('left(createTime, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost, sum(if(payment_type=1,receivedAmount,null)) as wx_money'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
- if($team_id) $query->where('team_id', $team_id);
- if($stime) $query->where('createTime', '>=', $stime);
- if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
- if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
- })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->get();
- }
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
- $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
- $v['zfm_money'] = $v['receivedAmount'] - $v['wx_money'];
- }
- if($aftersale==0){
- $indexKey = ['team_name','ctime','order_count','receivedAmount','wx_money','zfm_money','aftersale_fee','refund_price','true_amount','cost','freight_cost'];
- $title = ['团队', '时间', '总单数', '成交金额', '微信支付金额','付款码支付金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本'];
- }else{
- $indexKey = ['team_name','ctime','order_count','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost'];
- $title = ['团队', '时间', '总单数', '成交金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本'];
- }
- $filename = $aftersale==0 ? 'dingdanhuizong_'.date('Y-m-d_H').'.xlsx' : 'shouhouhuizong_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地区汇总roi
- */
- public function districtTotalRoi(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $ret = AdCost::select('id')->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->get();
- $ret = json_decode(json_encode($ret), true);
- $count = count($ret);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉收入
- //加粉
- $city_name = str_replace('市', '', $v['city']);
- $phones = DB::table('customers')->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
- #订单总计:
- $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
-
- #汇总新粉成单:
- $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
- // 1.新粉成单数
- $v['new_order_count'] = $new_order->order_count;
- // 2.新粉成交额
- $v['new_order_amount'] = $new_order->order_amount;
- // 3.粉丝总成交额
- $v['order_amount'] = $order->order_amount;
- // 总单数
- $v['order_count'] = $order->order_count;
- //新粉roi
- $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
- $v['new_fan_count'] = count($phones);
- }
- return view('statistics/districtTotalRoi', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- ]);
- }
- /**
- * 地域roi7日
- */
- public function districtRoi7day(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = DistrictRoi7::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = DistrictRoi7::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- return view('statistics/districtRoi7day', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 地域roi7日
- */
- public function districtRoi7day_export(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = DistrictRoi7::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- $indexKey = ['ad_time','city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['日期', '城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '7日累计成单数', '7日累计收入', '7日累计ROI'];
- $filename = 'districtRoi7day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi7日
- */
- public function districtRoi7dayTotal(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = DistrictRoi7::where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = DistrictRoi7::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- return view('statistics/districtRoi7dayTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- ]);
- }
- /**
- * 地域roi7日
- */
- public function districtRoi7dayTotal_export(Request $request){
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = DistrictRoi7::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount,sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '7日累计成单数', '7日累计收入', '7日累计ROI'];
- $filename = 'districtRoi7day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi30日
- */
- public function districtRoi30day(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = districtRoi30::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->count();
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = districtRoi30::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- return view('statistics/districtRoi30day', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- 'stime' =>$stime,
- 'etime' =>$etime,
- ]);
- }
- /**
- * 地域roi30日
- */
- public function districtRoi30day_export(Request $request){
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = districtRoi30::where(function($query) use($stime, $etime, $city){
- if($stime) $query->where('ad_time', '>=', $stime);
- if($etime) $query->where('ad_time', '<=', $etime);
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->orderBy('id', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- }
- $indexKey = ['ad_time','city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['日期', '城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'districtRoi30day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 地域roi30日汇总
- */
- public function districtRoi30dayTotal(Request $request){
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $count = districtRoi30::where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->get();
- $count = count($count);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = districtRoi30::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- return view('statistics/districtRoi30dayTotal', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'city' =>$city,
- ]);
- }
- /**
- * 地域roi30日
- */
- public function districtRoi30dayTotal_export(Request $request){
- $city = $request->input('city');
- if($city !== null){
- $city = str_replace('市', '', $city);
- }
- $result = districtRoi30::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount,sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
- if($city) $query->where('city', 'like', '%'.$city.'%');
- })->groupBy('city')->orderBy('roi', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- //新粉roi
- $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //累计roi
- $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
- //数据格式化
- $v['order_amount'] = round($v['sum_order_amount'],2);
- }
- $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
- $title = ['城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
- $filename = 'districtRoi30day_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- /**
- * 销售月度业绩报表
- */
- public function salerBonus(Request $request){
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $mstime = $request->input('stime');
- $metime = $request->input('etime');
- if($mstime){
- $stime = date('Y-m-01', strtotime($mstime));
- }else{
- $stime = null;
- }
- if($metime){
- $etime = date('Y-m-01', strtotime($metime. ' 1 month'));
- }else{
- $etime = null;
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0 && !$admin_id){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $page = (int)$request->input('page');
- $pageSize = 20;
- if($page<=0){
- $page = 1;
- }
- $offset = ($page-1) * $pageSize;
- $ret = Order::select(DB::raw('left(createTime, 7) as month, admin_id'))->where('is_del', 0)->where('status', '>=', 2)->where(function($query) use($admin_id, $sale_ids, $stime, $etime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($stime)) $query->where('createTime', '>=', $stime);
- if(!empty($etime)) $query->where('createTime', '<', $etime);
- })->groupBy('month')->groupBy('admin_id')->get();
- $count = count($ret);
- if ($count > 1) {
- // 总页数
- $pages = ceil($count/$pageSize);
- }else{
- // 总页数
- $pages = 1;
- }
- $result = Order::select(DB::raw('left(createTime, 7) as month, admin_id, count(1) as order_count, sum(receivedAmount) as order_amount'))->where('is_del', 0)->where('status', '>=', 2)->where(function($query) use($admin_id, $sale_ids, $stime, $etime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($stime)) $query->where('createTime', '>=', $stime);
- if(!empty($etime)) $query->where('createTime', '<', $etime);
- })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->offset($offset)->limit($pageSize)->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $m_stime = date('Y-m-01', strtotime($v['month']));
- $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
- //加粉数
- $fan_count = DB::table('cust_day_detail')->select(DB::raw('sum(fan_add) as fan_count, admin_name'))->where('dtime', '>=', $m_stime)->where('dtime', '<', $m_etime)->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- $v['fan_count'] = $fan_count->fan_count;
- $v['admin_name'] = $fan_count->admin_name;
- if(empty($v['admin_name'])){
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('name');
- }
- #计算提成
- $v['bonus'] = $this->getSalerBonus($v['order_amount']);
- }
- $teamList = DB::table('teams')->select('id', 'name')->get();
- $teamList = json_decode(json_encode($teamList), true);
- $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
- $adminList = json_decode(json_encode($adminList), true);
- return view('statistics/salerBonus', ['result'=>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'teamlist' =>$teamList,
- 'adminlist' =>$adminList,
- 'team_id' =>$team_id,
- 'admin_id' =>$admin_id,
- 'stime' =>$mstime,
- 'etime' =>$metime,
- ]);
- }
- /**
- * 计算提成
- */
- public function getSalerBonus($amount){
- $result = [
- 0 => 0, //总的
- 1 => 0,
- 2 => 0,
- 3 => 0
- ];
- if($amount>150000){
- $result[1] = round(100000 * 0.03);
- $result[2] = round(50000 * 0.04);
- $result[3] = round( ($amount - 150000) * 0.05 , 2);
- $result[0] = $result[1] + $result[2] + $result[3];
- }elseif($amount>100000){
- $result[1] = round(100000 * 0.03);
- $result[2] = round(($amount - 100000) * 0.04, 2);
- $result[0] = $result[1] + $result[2];
- }else{
- $result[1] = round($amount * 0.03, 2);
- $result[0] = $result[1];
- }
- return $result;
- }
- /**
- * 销售月度业绩报表
- */
- public function salerBonus_export(Request $request){
- $team_id = (int)$request->input('team_id');
- $admin_id = (int)$request->input('admin_id');
- $stime = $request->input('stime');
- $etime = $request->input('etime');
- if($stime){
- $stime = date('Y-m-01', strtotime($stime));
- }
- if($etime){
- $etime = date('Y-m-01', strtotime($etime. ' 1 month'));
- }
- //假如有团队筛选,检索销售队员
- $sale_ids = null;
- if($team_id>0 && !$admin_id){
- $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
- }
- $result = Order::select(DB::raw('left(createTime, 7) as month, admin_id, count(1) as order_count, sum(receivedAmount) as order_amount'))->where('is_del', 0)->where('status', '>=', 2)->where(function($query) use($admin_id, $sale_ids, $stime, $etime){
- if($admin_id) $query->where('admin_id', $admin_id);
- if(!empty($sale_ids)) $query->whereIn('admin_id', $sale_ids);
- if(!empty($stime)) $query->where('createTime', '>=', $stime);
- if(!empty($etime)) $query->where('createTime', '<', $etime);
- })->groupBy('month')->groupBy('admin_id')->orderBy('month', 'desc')->get();
- $result = json_decode(json_encode($result), true);
- foreach($result as $k=>&$v){
- $m_stime = date('Y-m-01', strtotime($v['month']));
- $m_etime = date('Y-m-01', strtotime($m_stime. ' 1 month'));
- //加粉数
- $fan_count = DB::table('cust_day_detail')->select(DB::raw('sum(fan_add) as fan_count, admin_name'))->where('dtime', '>=', $m_stime)->where('dtime', '<', $m_etime)->where('admin_id', $v['admin_id'])->where('is_del', 0)->first();
- $v['fan_count'] = $fan_count->fan_count;
- $v['admin_name'] = $fan_count->admin_name;
- if(empty($v['admin_name'])){
- $v['admin_name'] = DB::table('admin')->where('id', $v['admin_id'])->pluck('name');
- }
- #计算提成
- $bonus = $this->getSalerBonus($v['order_amount']);
- $v['bonus0'] = $bonus[0];
- $v['bonus1'] = $bonus[1];
- $v['bonus2'] = $bonus[2];
- $v['bonus3'] = $bonus[3];
- }
- $indexKey = ['month','admin_name','order_count','order_amount','fan_count','bonus0','bonus1','bonus2','bonus3'];
- $title = ['月份', '销售名', '总单量', '总成交额', '总加粉数', '总提成', '一阶提成', '二阶提成', '三阶提成'];
- $filename = 'xiaoshouyj_'.date('Y-m-d_H').'.xlsx';
- return Order::export_excel($result, $filename, $indexKey, $title);
- }
- }
-
-
|