12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535 |
- <?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 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', '=', $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, 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', $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;
- //新粉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 .'%' : '';
- }
- return view('statistics/districtRoi', ['result' =>$result,
- 'page' =>$page,
- 'count' =>$count,
- 'pages' =>$pages,
- 'stime' =>$stime,
- 'etime' =>$etime,
- 'city' =>$city,
- ]);
- }
- }
|