Nenhuma Descrição

StatisticsController.php 84KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. <?php
  2. /**
  3. * Created by Sublime.
  4. * User: hao
  5. * Date: 19/08/30
  6. * Time: 上午11:20
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\Http\Controllers\Controller;
  10. use App\Logs;
  11. use App\CustTotal;
  12. use App\CustDetail;
  13. use App\AdCost;
  14. use App\Order;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\DB;
  17. class StatisticsController extends Controller
  18. {
  19. /**
  20. * 当日数据统计
  21. */
  22. public function fanDay(Request $request){
  23. $page = (int)$request->input('page');
  24. $pageSize = 20;
  25. if($page<=0){
  26. $page = 1;
  27. }
  28. $offset = ($page-1) * $pageSize;
  29. $stime = $request->input('stime');
  30. $etime = $request->input('etime');
  31. $team_id = (int)$request->input('team_id');
  32. $admin_id = (int)$request->input('admin_id');
  33. $self_role = session('role_name');
  34. $team_id = $request->input('team_id');
  35. #只能看自己团队的
  36. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  37. $self_id = session('admin_id');
  38. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  39. }
  40. if($admin_id>0 && !$team_id){
  41. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  42. }
  43. //假如有团队筛选,检索销售队员
  44. $sale_ids = null;
  45. if($team_id>0){
  46. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  47. }
  48. $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime){
  49. if($team_id) $query->where('team_id', '=', $team_id);
  50. if($stime) $query->where('dtime', '>=', $stime);
  51. if($etime) $query->where('dtime', '<=', $etime);
  52. })->where('is_del',0)->first();
  53. $count = $count->total;
  54. if ($count > 1) {
  55. // 总页数
  56. $pages = ceil($count/$pageSize);
  57. }else{
  58. // 总页数
  59. $pages = 1;
  60. }
  61. $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){
  62. if($team_id) $query->where('team_id', '=', $team_id);
  63. if($stime) $query->where('dtime', '>=', $stime);
  64. if($etime) $query->where('dtime', '<=', $etime);
  65. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  66. $result = json_decode(json_encode($result),true);
  67. foreach($result as $k=>&$v){
  68. #进粉成本
  69. $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  70. #当日微信粉
  71. $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){
  72. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  73. if($admin_id>0) $query->where('admin_id', $admin_id);
  74. })->first();
  75. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  76. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  77. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  78. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  79. #当日微信粉成本
  80. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  81. #当日订单数、销售额
  82. $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){
  83. if($team_id>0) $query->where('team_id', $team_id);
  84. if($admin_id>0) $query->where('admin_id', $admin_id);
  85. })->first();
  86. $v['order_count'] = $order->order_count;
  87. $v['order_amount'] = $order->order_amount;
  88. #物流成本
  89. $v['freight_cost'] = $order->freight_cost;
  90. #货品成本
  91. $v['goods_cost'] = $order->goods_cost;
  92. #新加逻辑 -- 销售筛选
  93. if($admin_id>0){
  94. //预估当日投放 当日公众进粉
  95. # 1. 当前团队销售加粉占比
  96. $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();
  97. $salerfanRate = $teamFanAdd->team_fan_add>0 ? round($v['wx_fan_add'] / $teamFanAdd->team_fan_add, 3) * 1000 : 0;
  98. #预估该销售投放金额
  99. $v['total_cost'] = round( $salerfanRate * $v['total_cost'] / 1000, 2);
  100. #预估进粉
  101. $v['total_fan_add'] = round( $salerfanRate * $v['total_fan_add'] / 1000);
  102. #当日微信粉成本
  103. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  104. }
  105. }
  106. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  107. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  108. })->get();
  109. $teamList = json_decode(json_encode($teamList), true);
  110. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where(function($query) use($self_role, $team_id){
  111. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('team_id', $team_id);
  112. })->get();
  113. $adminList = json_decode(json_encode($adminList), true);
  114. return view('statistics/fanDay', ['result' =>$result,
  115. 'page' =>$page,
  116. 'count' =>$count,
  117. 'pages' =>$pages,
  118. 'teamlist' =>$teamList,
  119. 'adminlist' =>$adminList,
  120. 'stime' =>$stime,
  121. 'etime' =>$etime,
  122. 'team_id' =>$team_id,
  123. 'admin_id' =>$admin_id,
  124. ]);
  125. }
  126. public function fanDay_export(Request $request){
  127. $stime = $request->input('stime');
  128. $etime = $request->input('etime');
  129. $team_id = (int)$request->input('team_id');
  130. $self_role = session('role_name');
  131. #只能看自己团队的
  132. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  133. $self_id = session('admin_id');
  134. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  135. }
  136. //假如有团队筛选,检索销售队员
  137. $sale_ids = null;
  138. if($team_id>0){
  139. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  140. }
  141. $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){
  142. if($team_id) $query->where('team_id', '=', $team_id);
  143. if($stime) $query->where('dtime', '>=', $stime);
  144. if($etime) $query->where('dtime', '<=', $etime);
  145. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  146. $result = json_decode(json_encode($result),true);
  147. $filename="当日数据统计.xls";
  148. header("Content-type:application/vnd.ms-excel");
  149. Header("Accept-Ranges:bytes");
  150. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  151. header("Pragma: no-cache");
  152. header("Expires: 0");
  153. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  154. xmlns:x="urn:schemas-microsoft-com:office:excel"
  155. xmlns="http://www.w3.org/TR/REC-html40">
  156. <head>
  157. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  158. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  159. <!--[if gte mso 9]><xml>
  160. <x:ExcelWorkbook>
  161. <x:ExcelWorksheets>
  162. <x:ExcelWorksheet>
  163. <x:Name></x:Name>
  164. <x:WorksheetOptions>
  165. <x:DisplayGridlines/>
  166. </x:WorksheetOptions>
  167. </x:ExcelWorksheet>
  168. </x:ExcelWorksheets>
  169. </x:ExcelWorkbook>
  170. </xml><![endif]-->
  171. </head>';
  172. $data_str .= "
  173. <table>
  174. <tr>
  175. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  176. <th>".iconv("UTF-8", "GB2312//IGNORE","当日投放金额")."</th>
  177. <th>".iconv("UTF-8", "GB2312//IGNORE","当日公众号进粉")."</th>
  178. <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉成本")."</th>
  179. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉")."</th>
  180. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉成本")."</th>
  181. <th>".iconv("UTF-8", "GB2312//IGNORE","当日订单数")."</th>
  182. <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
  183. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复量")."</th>
  184. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复率")."</th>
  185. <th>".iconv("UTF-8", "GB2312//IGNORE","当日老粉主动咨询量")."</th>
  186. </tr>";
  187. foreach ($result as $k => $v)
  188. {
  189. #进粉成本
  190. $v['cost_fan'] = $v['total_fan_add']>0 ? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  191. #当日微信粉
  192. $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){
  193. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  194. })->first();
  195. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  196. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  197. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  198. $v['new_reply_rate'] = $v['wx_fan_add']>0 ? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  199. #当日微信粉成本
  200. $v['cost_wx_fan'] = $v['wx_fan_add']>0 ? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  201. #当日订单数、销售额
  202. $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){
  203. if($team_id>0) $query->where('team_id', $team_id);
  204. })->first();
  205. $v['order_count'] = $order->order_count;
  206. $v['order_amount'] = $order->order_amount;
  207. $data_str .= "<tr>";
  208. $data_str .= "<td>".$v['dtime']."</td>";
  209. $data_str .= "<td>".$v['total_cost']."</td>";
  210. $data_str .= "<td>".$v['total_fan_add']."</td>";
  211. $data_str .= "<td>".$v['cost_fan']."</td>";
  212. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  213. $data_str .= "<td>".$v['cost_wx_fan']."</td>";
  214. $data_str .= "<td>".$v['order_count']."</td>";
  215. $data_str .= "<td>".$v['order_amount']."</td>";
  216. $data_str .= "<td>".$v['total_new_reply']."</td>";
  217. $data_str .= "<td>".$v['new_reply_rate']."</td>";
  218. $data_str .= "<td>".$v['total_old_consult']."</td>";
  219. $data_str .= "</tr>";
  220. }
  221. $data_str .= "</table>";
  222. echo $data_str;
  223. exit;
  224. }
  225. /**
  226. * 当日数据统计
  227. */
  228. public function orderDay(Request $request){
  229. $page = (int)$request->input('page');
  230. $pageSize = 20;
  231. if($page<=0){
  232. $page = 1;
  233. }
  234. $offset = ($page-1) * $pageSize;
  235. $stime = $request->input('stime');
  236. $etime = $request->input('etime');
  237. $team_id = (int)$request->input('team_id');
  238. $self_role = session('role_name');
  239. #只能看自己团队的
  240. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  241. $self_id = session('admin_id');
  242. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  243. }
  244. //假如有团队筛选,检索销售队员
  245. $sale_ids = null;
  246. if($team_id>0){
  247. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  248. }
  249. //规定只统计前天及以前的数据
  250. $end_time = date('Y-m-d');
  251. $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime, $end_time){
  252. if($team_id) $query->where('team_id', '=', $team_id);
  253. if($stime) $query->where('dtime', '>=', $stime);
  254. if($etime) $query->where('dtime', '<=', $etime);
  255. $query->where('dtime','<', $end_time);
  256. })->where('is_del',0)->first();
  257. $count = $count->total;
  258. if ($count > 1) {
  259. // 总页数
  260. $pages = ceil($count/$pageSize);
  261. }else{
  262. // 总页数
  263. $pages = 1;
  264. }
  265. $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){
  266. if($team_id) $query->where('team_id', '=', $team_id);
  267. if($stime) $query->where('dtime', '>=', $stime);
  268. if($etime) $query->where('dtime', '<=', $etime);
  269. $query->where('dtime','<', $end_time);
  270. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  271. $result = json_decode(json_encode($result),true);
  272. foreach($result as $k=>&$v){
  273. #当日微信好友数量
  274. $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){
  275. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  276. })->first();
  277. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  278. //当日加粉
  279. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  280. #当日加粉订单总计:
  281. $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){
  282. if($team_id>0) $query->where('team_id', $team_id);
  283. })->first();
  284. #当日新粉成单:
  285. $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){
  286. if($team_id>0) $query->where('team_id', $team_id);
  287. })->first();
  288. // 1.当日新粉成单数
  289. $v['new_order_count'] = $new_order->order_count;
  290. // 2.当日新粉成交额
  291. $v['new_order_amount'] = $new_order->order_amount;
  292. $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){
  293. if($team_id>0) $query->where('team_id', $team_id);
  294. })->first();
  295. // 3.老粉成单数
  296. $v['old_order_count'] = $old_order->order_count;
  297. // 4.老粉成交额
  298. $v['old_order_amount'] = $old_order->order_amount;
  299. // 5.总成交额
  300. $v['order_amount'] = $order->order_amount;
  301. #复购单数、复购成交额
  302. $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){
  303. if($team_id>0) $query->where('team_id', $team_id);
  304. })->first();
  305. $v['fugou_order_count'] = $fugou->order_count;
  306. $v['fugou_order_amount'] = $fugou->order_amount;
  307. #货品成本
  308. $v['order_cost'] = $order->order_cost;
  309. //加物流成本
  310. $v['freight_cost'] = $order->freight_cost;
  311. #毛利
  312. $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'] - $v['freight_cost'];
  313. //综合成单率 综合成单率=订单数/微信好友数
  314. $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  315. //新粉成单率
  316. $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  317. //复购率
  318. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  319. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  320. //加roi
  321. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  322. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  323. }
  324. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  325. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  326. })->get();
  327. $teamList = json_decode(json_encode($teamList), true);
  328. return view('statistics/orderDay', ['result' =>$result,
  329. 'page' =>$page,
  330. 'count' =>$count,
  331. 'pages' =>$pages,
  332. 'teamlist' =>$teamList,
  333. 'stime' =>$stime,
  334. 'etime' =>$etime,
  335. 'team_id' =>$team_id,
  336. ]);
  337. }
  338. public function orderDay_export(Request $request){
  339. $stime = $request->input('stime');
  340. $etime = $request->input('etime');
  341. $team_id = (int)$request->input('team_id');
  342. $self_role = session('role_name');
  343. #只能看自己团队的
  344. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  345. $self_id = session('admin_id');
  346. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  347. }
  348. //假如有团队筛选,检索销售队员
  349. $sale_ids = null;
  350. if($team_id>0){
  351. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  352. }
  353. //规定只统计前天及以前的数据
  354. $end_time = date('Y-m-d');
  355. $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){
  356. if($team_id) $query->where('team_id', '=', $team_id);
  357. if($stime) $query->where('dtime', '>=', $stime);
  358. if($etime) $query->where('dtime', '<=', $etime);
  359. $query->where('dtime','<', $end_time);
  360. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  361. $result = json_decode(json_encode($result),true);
  362. $filename="分片数据统计.xls";
  363. header("Content-type:application/vnd.ms-excel");
  364. Header("Accept-Ranges:bytes");
  365. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  366. header("Pragma: no-cache");
  367. header("Expires: 0");
  368. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  369. xmlns:x="urn:schemas-microsoft-com:office:excel"
  370. xmlns="http://www.w3.org/TR/REC-html40">
  371. <head>
  372. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  373. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  374. <!--[if gte mso 9]><xml>
  375. <x:ExcelWorkbook>
  376. <x:ExcelWorksheets>
  377. <x:ExcelWorksheet>
  378. <x:Name></x:Name>
  379. <x:WorksheetOptions>
  380. <x:DisplayGridlines/>
  381. </x:WorksheetOptions>
  382. </x:ExcelWorksheet>
  383. </x:ExcelWorksheets>
  384. </x:ExcelWorkbook>
  385. </xml><![endif]-->
  386. </head>';
  387. $data_str .= "
  388. <table>
  389. <tr>
  390. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  391. <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉数量")."</th>
  392. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
  393. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
  394. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
  395. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
  396. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  397. <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
  398. <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
  399. <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
  400. <th>".iconv("UTF-8", "GB2312//IGNORE","总投放成本")."</th>
  401. <th>".iconv("UTF-8", "GB2312//IGNORE","货品成本")."</th>
  402. <th>".iconv("UTF-8", "GB2312//IGNORE","毛利")."</th>
  403. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
  404. <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
  405. </tr>";
  406. foreach ($result as $k => $v)
  407. {
  408. #当日微信好友数量
  409. $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){
  410. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  411. })->first();
  412. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  413. //当日加粉
  414. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  415. #当日加粉订单总计:
  416. $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){
  417. if($team_id>0) $query->where('team_id', $team_id);
  418. })->first();
  419. #当日新粉成单:
  420. $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){
  421. if($team_id>0) $query->where('team_id', $team_id);
  422. })->first();
  423. // 1.当日新粉成单数
  424. $v['new_order_count'] = $new_order->order_count;
  425. // 2.当日新粉成交额
  426. $v['new_order_amount'] = $new_order->order_amount;
  427. $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){
  428. if($team_id>0) $query->where('team_id', $team_id);
  429. })->first();
  430. // 3.老粉成单数
  431. $v['old_order_count'] = $old_order->order_count;
  432. // 4.老粉成交额
  433. $v['old_order_amount'] = $old_order->order_amount;
  434. // 5.总成交额
  435. $v['order_amount'] = $order->order_amount;
  436. #复购单数、复购成交额
  437. $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){
  438. if($team_id>0) $query->where('team_id', $team_id);
  439. })->first();
  440. $v['fugou_order_count'] = $fugou->order_count;
  441. $v['fugou_order_amount'] = $fugou->order_amount;
  442. #货品成本
  443. $v['order_cost'] = $order->order_cost;
  444. #毛利
  445. $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'];
  446. //综合成单率 综合成单率=订单数/微信好友数
  447. $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  448. //新粉成单率
  449. $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  450. //复购率
  451. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  452. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  453. $data_str .= "<tr>";
  454. $data_str .= "<td>".$v['dtime']."</td>";
  455. $data_str .= "<td>".$v['total_fan_add']."</td>";
  456. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  457. $data_str .= "<td>".$v['new_order_count']."</td>";
  458. $data_str .= "<td>".$v['new_order_amount']."</td>";
  459. $data_str .= "<td>".$v['old_order_count']."</td>";
  460. $data_str .= "<td>".$v['old_order_amount']."</td>";
  461. $data_str .= "<td>".$v['fugou_order_count']."</td>";
  462. $data_str .= "<td>".$v['fugou_order_amount']."</td>";
  463. $data_str .= "<td>".$v['order_amount']."</td>";
  464. $data_str .= "<td>".$v['total_cost']."</td>";
  465. $data_str .= "<td>".$v['order_cost']."</td>";
  466. $data_str .= "<td>".$v['profit']."</td>";
  467. $data_str .= "<td>".$v['new_order_rate']."</td>";
  468. $data_str .= "<td>".$v['order_rate']."</td>";
  469. $data_str .= "</tr>";
  470. }
  471. $data_str .= "</table>";
  472. echo $data_str;
  473. exit;
  474. }
  475. /**
  476. * 当日数据统计(投放)
  477. */
  478. public function throwDay(Request $request){
  479. $page = (int)$request->input('page');
  480. $pageSize = 20;
  481. if($page<=0){
  482. $page = 1;
  483. }
  484. $offset = ($page-1) * $pageSize;
  485. $stime = $request->input('stime');
  486. $etime = $request->input('etime');
  487. $team_id = (int)$request->input('team_id');
  488. //假如有团队筛选,检索销售队员
  489. $sale_ids = null;
  490. if($team_id>0){
  491. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  492. }
  493. $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime){
  494. if($team_id) $query->where('team_id', '=', $team_id);
  495. if($stime) $query->where('dtime', '>=', $stime);
  496. if($etime) $query->where('dtime', '<=', $etime);
  497. })->where('is_del',0)->first();
  498. $count = $count->total;
  499. if ($count > 1) {
  500. // 总页数
  501. $pages = ceil($count/$pageSize);
  502. }else{
  503. // 总页数
  504. $pages = 1;
  505. }
  506. $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){
  507. if($team_id) $query->where('team_id', '=', $team_id);
  508. if($stime) $query->where('dtime', '>=', $stime);
  509. if($etime) $query->where('dtime', '<=', $etime);
  510. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  511. $result = json_decode(json_encode($result),true);
  512. foreach($result as $k=>&$v){
  513. #进粉成本
  514. $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  515. #当日微信粉
  516. $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){
  517. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  518. })->first();
  519. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  520. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  521. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  522. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  523. #当日微信粉成本
  524. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  525. #当日订单数、销售额
  526. $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){
  527. if($team_id>0) $query->where('team_id', $team_id);
  528. })->first();
  529. $v['order_count'] = $order->order_count;
  530. $v['order_amount'] = $order->order_amount;
  531. }
  532. $teamList = DB::table('teams')->select('id', 'name')->get();
  533. $teamList = json_decode(json_encode($teamList), true);
  534. return view('statistics/throwDay', ['result' =>$result,
  535. 'page' =>$page,
  536. 'count' =>$count,
  537. 'pages' =>$pages,
  538. 'teamlist' =>$teamList,
  539. 'stime' =>$stime,
  540. 'etime' =>$etime,
  541. 'team_id' =>$team_id,
  542. ]);
  543. }
  544. public function throwDay_export(Request $request){
  545. $stime = $request->input('stime');
  546. $etime = $request->input('etime');
  547. $team_id = (int)$request->input('team_id');
  548. //假如有团队筛选,检索销售队员
  549. $sale_ids = null;
  550. if($team_id>0){
  551. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  552. }
  553. $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){
  554. if($team_id) $query->where('team_id', '=', $team_id);
  555. if($stime) $query->where('dtime', '>=', $stime);
  556. if($etime) $query->where('dtime', '<=', $etime);
  557. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  558. $result = json_decode(json_encode($result),true);
  559. $filename="当日数据统计.xls";
  560. header("Content-type:application/vnd.ms-excel");
  561. Header("Accept-Ranges:bytes");
  562. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  563. header("Pragma: no-cache");
  564. header("Expires: 0");
  565. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  566. xmlns:x="urn:schemas-microsoft-com:office:excel"
  567. xmlns="http://www.w3.org/TR/REC-html40">
  568. <head>
  569. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  570. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  571. <!--[if gte mso 9]><xml>
  572. <x:ExcelWorkbook>
  573. <x:ExcelWorksheets>
  574. <x:ExcelWorksheet>
  575. <x:Name></x:Name>
  576. <x:WorksheetOptions>
  577. <x:DisplayGridlines/>
  578. </x:WorksheetOptions>
  579. </x:ExcelWorksheet>
  580. </x:ExcelWorksheets>
  581. </x:ExcelWorkbook>
  582. </xml><![endif]-->
  583. </head>';
  584. $data_str .= "
  585. <table>
  586. <tr>
  587. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  588. <th>".iconv("UTF-8", "GB2312//IGNORE","当日投放金额")."</th>
  589. <th>".iconv("UTF-8", "GB2312//IGNORE","当日公众号进粉")."</th>
  590. <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉成本")."</th>
  591. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉")."</th>
  592. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉成本")."</th>
  593. </tr>";
  594. foreach ($result as $k => $v)
  595. {
  596. #进粉成本
  597. $v['cost_fan'] = $v['total_fan_add']>0 ? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  598. #当日微信粉
  599. $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){
  600. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  601. })->first();
  602. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  603. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  604. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  605. $v['new_reply_rate'] = $v['wx_fan_add']>0 ? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  606. #当日微信粉成本
  607. $v['cost_wx_fan'] = $v['wx_fan_add']>0 ? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  608. #当日订单数、销售额
  609. $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){
  610. if($team_id>0) $query->where('team_id', $team_id);
  611. })->first();
  612. $v['order_count'] = $order->order_count;
  613. $v['order_amount'] = $order->order_amount;
  614. $data_str .= "<tr>";
  615. $data_str .= "<td>".$v['dtime']."</td>";
  616. $data_str .= "<td>".$v['total_cost']."</td>";
  617. $data_str .= "<td>".$v['total_fan_add']."</td>";
  618. $data_str .= "<td>".$v['cost_fan']."</td>";
  619. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  620. $data_str .= "<td>".$v['cost_wx_fan']."</td>";
  621. $data_str .= "</tr>";
  622. }
  623. $data_str .= "</table>";
  624. echo $data_str;
  625. exit;
  626. }
  627. /**
  628. * 销售可看当日数据表
  629. *
  630. */
  631. public function fanDaySaler(Request $request){
  632. $page = (int)$request->input('page');
  633. $pageSize = 20;
  634. if($page<=0){
  635. $page = 1;
  636. }
  637. $offset = ($page-1) * $pageSize;
  638. $stime = $request->input('stime');
  639. $etime = $request->input('etime');
  640. $team_id = (int)$request->input('team_id');
  641. $admin_id = (int)$request->input('admin_id');
  642. $is_self_team = (int)$request->input('is_self_team');
  643. //规定只统计前天及以前的数据
  644. $end_time = date('Y-m-d');
  645. $self_role = session('role_name');
  646. if($self_role == '销售'){
  647. $admin_id = session('admin_id');
  648. #假如查看团队的
  649. if($is_self_team == 1){
  650. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  651. $admin_id = 0;
  652. }else{
  653. $team_id = null;
  654. }
  655. }
  656. //假如有团队筛选,检索销售队员
  657. $sale_ids = null;
  658. if($team_id>0){
  659. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  660. }
  661. $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
  662. if($admin_id) $query->where('admin_id', '=', $admin_id);
  663. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  664. if($stime) $query->where('dtime', '>=', $stime);
  665. if($etime) $query->where('dtime', '<=', $etime);
  666. if($end_time) $query->where('dtime', '<', $end_time);
  667. })->where('is_del',0)->first();
  668. $count = $count->total;
  669. if ($count > 1) {
  670. // 总页数
  671. $pages = ceil($count/$pageSize);
  672. }else{
  673. // 总页数
  674. $pages = 1;
  675. }
  676. $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){
  677. if($admin_id) $query->where('admin_id', '=', $admin_id);
  678. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  679. if($stime) $query->where('dtime', '>=', $stime);
  680. if($etime) $query->where('dtime', '<=', $etime);
  681. if($end_time) $query->where('dtime', '<', $end_time);
  682. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  683. $result = json_decode(json_encode($result),true);
  684. foreach($result as $k=>&$v){
  685. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  686. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  687. //当日加粉
  688. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  689. #当日新粉成单:
  690. $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){
  691. if($team_id>0) $query->where('team_id', $team_id);
  692. if($admin_id>0) $query->where('admin_id', $admin_id);
  693. })->first();
  694. // 1.当日新粉成单数
  695. $v['new_order_count'] = $new_order->order_count;
  696. // 2.当日新粉成交额
  697. $v['new_order_amount'] = $new_order->order_amount;
  698. // 3.新粉成交转化率
  699. $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
  700. //累计老粉量
  701. $dtime = $v['dtime'];
  702. $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
  703. if($admin_id) $query->where('admin_id', '=', $admin_id);
  704. if($sale_ids) $query->whereIn('admin_id', $sale_ids);
  705. if($dtime) $query->where('dtime', '<', $dtime);
  706. })->where('is_del',0)->sum('fan_add');
  707. $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
  708. #当日订单数、销售额
  709. $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){
  710. if($team_id>0) $query->where('team_id', $team_id);
  711. if($admin_id>0) $query->where('admin_id', $admin_id);
  712. })->first();
  713. $v['order_count'] = $order->order_count;
  714. $v['order_amount'] = $order->order_amount;
  715. // 3.老粉成单数
  716. $v['old_order_count'] = $order->order_count - $v['new_order_count'];
  717. // 4.老粉成交额
  718. $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
  719. // 5.老粉成交转化率
  720. $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
  721. }
  722. $teamList = DB::table('teams')->select('id', 'name')->get();
  723. $teamList = json_decode(json_encode($teamList), true);
  724. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
  725. $adminList = json_decode(json_encode($adminList), true);
  726. return view('statistics/fanDaySaler', ['result' =>$result,
  727. 'page' =>$page,
  728. 'count' =>$count,
  729. 'pages' =>$pages,
  730. 'teamlist' =>$teamList,
  731. 'adminlist' =>$adminList,
  732. 'stime' =>$stime,
  733. 'etime' =>$etime,
  734. 'team_id' =>$team_id,
  735. 'admin_id' =>$admin_id,
  736. 'is_self_team' =>$is_self_team,
  737. 'self_role' =>$self_role,
  738. ]);
  739. }
  740. public function fanDaySaler_export(Request $request){
  741. $stime = $request->input('stime');
  742. $etime = $request->input('etime');
  743. $team_id = (int)$request->input('team_id');
  744. $admin_id = (int)$request->input('admin_id');
  745. $is_self_team = (int)$request->input('is_self_team');
  746. //规定只统计前天及以前的数据
  747. $end_time = date('Y-m-d');
  748. $self_role = session('role_name');
  749. if($self_role == '销售'){
  750. $admin_id = session('admin_id');
  751. #假如查看团队的
  752. if($is_self_team == 1){
  753. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  754. $admin_id = 0;
  755. }else{
  756. $team_id = null;
  757. }
  758. }
  759. //假如有团队筛选,检索销售队员
  760. $sale_ids = null;
  761. if($team_id>0){
  762. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  763. }
  764. $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){
  765. if($admin_id) $query->where('admin_id', '=', $admin_id);
  766. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  767. if($stime) $query->where('dtime', '>=', $stime);
  768. if($etime) $query->where('dtime', '<=', $etime);
  769. if($end_time) $query->where('dtime', '<', $end_time);
  770. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  771. $result = json_decode(json_encode($result),true);
  772. $filename="当日数据统计(销售).xls";
  773. header("Content-type:application/vnd.ms-excel");
  774. Header("Accept-Ranges:bytes");
  775. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  776. header("Pragma: no-cache");
  777. header("Expires: 0");
  778. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  779. xmlns:x="urn:schemas-microsoft-com:office:excel"
  780. xmlns="http://www.w3.org/TR/REC-html40">
  781. <head>
  782. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  783. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  784. <!--[if gte mso 9]><xml>
  785. <x:ExcelWorkbook>
  786. <x:ExcelWorksheets>
  787. <x:ExcelWorksheet>
  788. <x:Name></x:Name>
  789. <x:WorksheetOptions>
  790. <x:DisplayGridlines/>
  791. </x:WorksheetOptions>
  792. </x:ExcelWorksheet>
  793. </x:ExcelWorksheets>
  794. </x:ExcelWorkbook>
  795. </xml><![endif]-->
  796. </head>';
  797. $data_str .= "
  798. <table>
  799. <tr>
  800. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  801. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉增加量")."</th>
  802. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复")."</th>
  803. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复率")."</th>
  804. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价量")."</th>
  805. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价率")."</th>
  806. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交单量")."</th>
  807. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交额")."</th>
  808. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交转化率")."</th>
  809. <th>".iconv("UTF-8", "GB2312//IGNORE","累计老粉量")."</th>
  810. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价量")."</th>
  811. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价率")."</th>
  812. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交单量")."</th>
  813. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  814. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交转化率")."</th>
  815. <th>".iconv("UTF-8", "GB2312//IGNORE","当日单量")."</th>
  816. <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
  817. </tr>";
  818. foreach ($result as $k => $v)
  819. {
  820. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  821. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  822. //当日加粉
  823. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  824. #当日新粉成单:
  825. $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){
  826. if($team_id>0) $query->where('team_id', $team_id);
  827. if($admin_id>0) $query->where('admin_id', $admin_id);
  828. })->first();
  829. // 1.当日新粉成单数
  830. $v['new_order_count'] = $new_order->order_count;
  831. // 2.当日新粉成交额
  832. $v['new_order_amount'] = $new_order->order_amount;
  833. // 3.新粉成交转化率
  834. $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
  835. //累计老粉量
  836. $dtime = $v['dtime'];
  837. $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
  838. if($admin_id) $query->where('admin_id', '=', $admin_id);
  839. if($sale_ids) $query->whereIn('admin_id', $sale_ids);
  840. if($dtime) $query->where('dtime', '<', $dtime);
  841. })->where('is_del',0)->sum('fan_add');
  842. $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
  843. #当日订单数、销售额
  844. $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){
  845. if($team_id>0) $query->where('team_id', $team_id);
  846. if($admin_id>0) $query->where('admin_id', $admin_id);
  847. })->first();
  848. $v['order_count'] = $order->order_count;
  849. $v['order_amount'] = $order->order_amount;
  850. // 3.老粉成单数
  851. $v['old_order_count'] = $order->order_count - $v['new_order_count'];
  852. // 4.老粉成交额
  853. $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
  854. // 5.老粉成交转化率
  855. $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
  856. $data_str .= "<tr>";
  857. $data_str .= "<td>".$v['dtime']."</td>";
  858. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  859. $data_str .= "<td>".$v['total_new_reply']."</td>";
  860. $data_str .= "<td>".$v['new_reply_rate']."</td>";
  861. $data_str .= "<td>".$v['total_new_consult']."</td>";
  862. $data_str .= "<td>".$v['new_consult_rate']."</td>";
  863. $data_str .= "<td>".$v['new_order_count']."</td>";
  864. $data_str .= "<td>".$v['new_order_amount']."</td>";
  865. $data_str .= "<td>".$v['new_order_rate']."</td>";
  866. $data_str .= "<td>".$v['wx_old_fan']."</td>";
  867. $data_str .= "<td>".$v['total_old_consult']."</td>";
  868. $data_str .= "<td>".$v['old_consult_rate']."</td>";
  869. $data_str .= "<td>".$v['old_order_count']."</td>";
  870. $data_str .= "<td>".$v['old_order_amount']."</td>";
  871. $data_str .= "<td>".$v['old_order_rate']."</td>";
  872. $data_str .= "<td>".$v['order_count']."</td>";
  873. $data_str .= "<td>".$v['order_amount']."</td>";
  874. $data_str .= "</tr>";
  875. }
  876. $data_str .= "</table>";
  877. echo $data_str;
  878. exit;
  879. }
  880. /**
  881. * 销售可看当日分片数据
  882. *
  883. */
  884. public function orderDaySaler(Request $request){
  885. $page = (int)$request->input('page');
  886. $pageSize = 20;
  887. if($page<=0){
  888. $page = 1;
  889. }
  890. $offset = ($page-1) * $pageSize;
  891. $stime = $request->input('stime');
  892. $etime = $request->input('etime');
  893. $team_id = (int)$request->input('team_id');
  894. $admin_id = (int)$request->input('admin_id');
  895. $is_self_team = (int)$request->input('is_self_team');
  896. //规定只统计前天及以前的数据
  897. $end_time = date('Y-m-d');
  898. $self_role = session('role_name');
  899. if($self_role == '销售'){
  900. $admin_id = session('admin_id');
  901. #假如查看团队的
  902. if($is_self_team == 1){
  903. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  904. $admin_id = 0;
  905. }else{
  906. $team_id = null;
  907. }
  908. }
  909. //假如有团队筛选,检索销售队员
  910. $sale_ids = null;
  911. if($team_id>0){
  912. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  913. }
  914. $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
  915. if($admin_id) $query->where('admin_id', '=', $admin_id);
  916. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  917. if($stime) $query->where('dtime', '>=', $stime);
  918. if($etime) $query->where('dtime', '<=', $etime);
  919. $query->where('dtime', '<', $end_time);
  920. })->where('is_del',0)->first();
  921. $count = $count->total;
  922. if ($count > 1) {
  923. // 总页数
  924. $pages = ceil($count/$pageSize);
  925. }else{
  926. // 总页数
  927. $pages = 1;
  928. }
  929. $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){
  930. if($admin_id) $query->where('admin_id', '=', $admin_id);
  931. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  932. if($stime) $query->where('dtime', '>=', $stime);
  933. if($etime) $query->where('dtime', '<=', $etime);
  934. $query->where('dtime', '<', $end_time);
  935. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  936. $result = json_decode(json_encode($result),true);
  937. foreach($result as $k=>&$v){
  938. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  939. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  940. //当日加粉
  941. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  942. #当日新粉成单:
  943. $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){
  944. if($team_id>0) $query->where('team_id', $team_id);
  945. if($admin_id>0) $query->where('admin_id', $admin_id);
  946. })->first();
  947. // 1.当日新粉成单数
  948. $v['new_order_count'] = $new_order->order_count;
  949. // 2.当日新粉成交额
  950. $v['new_order_amount'] = $new_order->order_amount;
  951. #当日老粉成单
  952. $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){
  953. if($team_id>0) $query->where('team_id', $team_id);
  954. if($admin_id>0) $query->where('admin_id', $admin_id);
  955. })->first();
  956. // 1.当日老粉成单数
  957. $v['old_order_count'] = $old_order->order_count;
  958. // 2.当日老粉成交额
  959. $v['old_order_amount'] = $old_order->order_amount;
  960. #复购单数、复购成交额
  961. $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){
  962. if($team_id>0) $query->where('team_id', $team_id);
  963. if($admin_id>0) $query->where('admin_id', $admin_id);
  964. })->first();
  965. $v['fugou_order_count'] = $fugou->order_count;
  966. $v['fugou_order_amount'] = $fugou->order_amount;
  967. #当日加粉订单总计:
  968. $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){
  969. if($team_id>0) $query->where('team_id', $team_id);
  970. if($admin_id>0) $query->where('admin_id', $admin_id);
  971. })->first();
  972. //总成交额
  973. $v['order_amount'] = $order->order_amount;
  974. //综合成单率 综合成单率=订单数/微信好友数
  975. $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
  976. //新粉成单率
  977. $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
  978. //复购率
  979. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  980. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  981. }
  982. $teamList = DB::table('teams')->select('id', 'name')->get();
  983. $teamList = json_decode(json_encode($teamList), true);
  984. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
  985. $adminList = json_decode(json_encode($adminList), true);
  986. return view('statistics/orderDaySaler', ['result' =>$result,
  987. 'page' =>$page,
  988. 'count' =>$count,
  989. 'pages' =>$pages,
  990. 'teamlist' =>$teamList,
  991. 'adminlist' =>$adminList,
  992. 'stime' =>$stime,
  993. 'etime' =>$etime,
  994. 'team_id' =>$team_id,
  995. 'admin_id' =>$admin_id,
  996. 'is_self_team' =>$is_self_team,
  997. 'self_role' =>$self_role,
  998. ]);
  999. }
  1000. public function orderDaySaler_export(Request $request){
  1001. $stime = $request->input('stime');
  1002. $etime = $request->input('etime');
  1003. $team_id = (int)$request->input('team_id');
  1004. $admin_id = (int)$request->input('admin_id');
  1005. $is_self_team = (int)$request->input('is_self_team');
  1006. //规定只统计前天及以前的数据
  1007. $end_time = date('Y-m-d');
  1008. $self_role = session('role_name');
  1009. if($self_role == '销售'){
  1010. $admin_id = session('admin_id');
  1011. #假如查看团队的
  1012. if($is_self_team == 1){
  1013. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  1014. $admin_id = 0;
  1015. }else{
  1016. $team_id = null;
  1017. }
  1018. }
  1019. //假如有团队筛选,检索销售队员
  1020. $sale_ids = null;
  1021. if($team_id>0){
  1022. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  1023. }
  1024. $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){
  1025. if($admin_id) $query->where('admin_id', '=', $admin_id);
  1026. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  1027. if($stime) $query->where('dtime', '>=', $stime);
  1028. if($etime) $query->where('dtime', '<=', $etime);
  1029. $query->where('dtime', '<', $end_time);
  1030. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  1031. $result = json_decode(json_encode($result),true);
  1032. $filename="当日数据统计(销售).xls";
  1033. header("Content-type:application/vnd.ms-excel");
  1034. Header("Accept-Ranges:bytes");
  1035. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  1036. header("Pragma: no-cache");
  1037. header("Expires: 0");
  1038. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  1039. xmlns:x="urn:schemas-microsoft-com:office:excel"
  1040. xmlns="http://www.w3.org/TR/REC-html40">
  1041. <head>
  1042. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  1043. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  1044. <!--[if gte mso 9]><xml>
  1045. <x:ExcelWorkbook>
  1046. <x:ExcelWorksheets>
  1047. <x:ExcelWorksheet>
  1048. <x:Name></x:Name>
  1049. <x:WorksheetOptions>
  1050. <x:DisplayGridlines/>
  1051. </x:WorksheetOptions>
  1052. </x:ExcelWorksheet>
  1053. </x:ExcelWorksheets>
  1054. </x:ExcelWorkbook>
  1055. </xml><![endif]-->
  1056. </head>';
  1057. $data_str .= "
  1058. <table>
  1059. <tr>
  1060. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  1061. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
  1062. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
  1063. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
  1064. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
  1065. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  1066. <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
  1067. <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
  1068. <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
  1069. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
  1070. <th>".iconv("UTF-8", "GB2312//IGNORE","复购率")."</th>
  1071. <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
  1072. </tr>";
  1073. foreach ($result as $k => $v)
  1074. {
  1075. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  1076. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  1077. //当日加粉
  1078. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  1079. #当日新粉成单:
  1080. $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){
  1081. if($team_id>0) $query->where('team_id', $team_id);
  1082. if($admin_id>0) $query->where('admin_id', $admin_id);
  1083. })->first();
  1084. // 1.当日新粉成单数
  1085. $v['new_order_count'] = $new_order->order_count;
  1086. // 2.当日新粉成交额
  1087. $v['new_order_amount'] = $new_order->order_amount;
  1088. #当日老粉成单
  1089. $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){
  1090. if($team_id>0) $query->where('team_id', $team_id);
  1091. if($admin_id>0) $query->where('admin_id', $admin_id);
  1092. })->first();
  1093. // 1.当日老粉成单数
  1094. $v['old_order_count'] = $old_order->order_count;
  1095. // 2.当日老粉成交额
  1096. $v['old_order_amount'] = $old_order->order_amount;
  1097. #复购单数、复购成交额
  1098. $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){
  1099. if($team_id>0) $query->where('team_id', $team_id);
  1100. if($admin_id>0) $query->where('admin_id', $admin_id);
  1101. })->first();
  1102. $v['fugou_order_count'] = $fugou->order_count;
  1103. $v['fugou_order_amount'] = $fugou->order_amount;
  1104. #当日加粉订单总计:
  1105. $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){
  1106. if($team_id>0) $query->where('team_id', $team_id);
  1107. if($admin_id>0) $query->where('admin_id', $admin_id);
  1108. })->first();
  1109. //总成交额
  1110. $v['order_amount'] = $order->order_amount;
  1111. //综合成单率 综合成单率=订单数/微信好友数
  1112. $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
  1113. //新粉成单率
  1114. $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
  1115. //复购率
  1116. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  1117. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  1118. $data_str .= "<tr>";
  1119. $data_str .= "<td>".$v['dtime']."</td>";
  1120. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  1121. $data_str .= "<td>".$v['new_order_count']."</td>";
  1122. $data_str .= "<td>".$v['new_order_amount']."</td>";
  1123. $data_str .= "<td>".$v['old_order_count']."</td>";
  1124. $data_str .= "<td>".$v['old_order_amount']."</td>";
  1125. $data_str .= "<td>".$v['fugou_order_count']."</td>";
  1126. $data_str .= "<td>".$v['fugou_order_amount']."</td>";
  1127. $data_str .= "<td>".$v['order_amount']."</td>";
  1128. $data_str .= "<td>".$v['new_order_rate']."</td>";
  1129. $data_str .= "<td>".$v['fugou_rate']."</td>";
  1130. $data_str .= "<td>".$v['order_rate']."</td>";
  1131. $data_str .= "</tr>";
  1132. }
  1133. $data_str .= "</table>";
  1134. echo $data_str;
  1135. exit;
  1136. }
  1137. /**
  1138. * 成单率梯形图
  1139. */
  1140. public function orderRateList(Request $request){
  1141. $stime = $request->input('stime');
  1142. $etime = $request->input('etime');
  1143. $self_role = session('role_name');
  1144. $team_id = $request->input('team_id');
  1145. #只能看自己团队的
  1146. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  1147. $self_id = session('admin_id');
  1148. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  1149. }
  1150. if($team_id>0){
  1151. $result = $this->orderTeamRateList($stime, $etime, $team_id);
  1152. return view('statistics/orderRateList', $result);
  1153. }
  1154. $_day = 30;
  1155. if($stime == '' && $etime == ''){
  1156. $stime = date("Y-m-d", strtotime('-32 day'));
  1157. $etime = date("Y-m-d", strtotime('-2 day'));
  1158. }else{
  1159. if($stime){
  1160. $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
  1161. }
  1162. }
  1163. #统计天数
  1164. $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
  1165. $day_count = DB::table('cust_day_remain')->where('month_time', $month)->where(function($query) use($stime, $etime){
  1166. if($stime) $query->where('idate','>=', $stime);
  1167. if($etime) $query->where('idate','<=', $etime);
  1168. })->count();
  1169. $result = DB::table('cust_day_remain')->where(function($query) use($stime, $etime){
  1170. if($stime) $query->where('idate','>=', $stime);
  1171. if($etime) $query->where('idate','<=', $etime);
  1172. })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
  1173. if($stime)
  1174. #获取列
  1175. $columns = array();
  1176. $i_max = 31;
  1177. for($i=0; $i<$i_max; $i++){
  1178. $columns[] = $i;
  1179. }
  1180. if($_day >= 45){
  1181. $columns[] = 45;
  1182. }
  1183. if($_day >= 60){
  1184. $columns[] = 60;
  1185. }
  1186. if($_day >= 75){
  1187. $columns[] = 75;
  1188. }
  1189. if($_day >= 90){
  1190. $columns[] = 90;
  1191. }
  1192. #数据整合
  1193. $data = array();
  1194. foreach($result as $k=>$v){
  1195. $key = $v->idate;
  1196. $rate_data = json_decode($v->order_rate_data, true);
  1197. $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
  1198. }
  1199. #数据格式化
  1200. $new_data = array();
  1201. foreach($data as $k=>$v){
  1202. //当日加粉数
  1203. $fan_add = DB::table('cust_day_detail')->where('dtime', $k)->where('is_del',0)->sum('fan_add');
  1204. $new_data[$k]['fan_add'] = $fan_add;
  1205. foreach($columns as $val){
  1206. $n = $val;
  1207. $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
  1208. }
  1209. }
  1210. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  1211. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  1212. })->get();
  1213. $teamList = json_decode(json_encode($teamList), true);
  1214. //echo '<pre>';
  1215. //print_r($new_data);print_r($columns);exit;
  1216. return view('statistics/orderRateList', ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList]);
  1217. }
  1218. /**
  1219. * 团队成单率梯形图
  1220. */
  1221. public function orderTeamRateList($stime, $etime, $team_id){
  1222. $_day = 30;
  1223. if($stime == '' && $etime == ''){
  1224. $stime = date("Y-m-d", strtotime('-32 day'));
  1225. $etime = date("Y-m-d", strtotime('-2 day'));
  1226. }else{
  1227. if($stime){
  1228. $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
  1229. }
  1230. }
  1231. #统计天数
  1232. $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
  1233. $day_count = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('month_time', $month)->where(function($query) use($stime, $etime){
  1234. if($stime) $query->where('idate','>=', $stime);
  1235. if($etime) $query->where('idate','<=', $etime);
  1236. })->count();
  1237. $result = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where(function($query) use($stime, $etime){
  1238. if($stime) $query->where('idate','>=', $stime);
  1239. if($etime) $query->where('idate','<=', $etime);
  1240. })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
  1241. if($stime)
  1242. #获取列
  1243. $columns = array();
  1244. $i_max = 31;
  1245. for($i=0; $i<$i_max; $i++){
  1246. $columns[] = $i;
  1247. }
  1248. if($_day >= 45){
  1249. $columns[] = 45;
  1250. }
  1251. if($_day >= 60){
  1252. $columns[] = 60;
  1253. }
  1254. if($_day >= 75){
  1255. $columns[] = 75;
  1256. }
  1257. if($_day >= 90){
  1258. $columns[] = 90;
  1259. }
  1260. #数据整合
  1261. $data = array();
  1262. foreach($result as $k=>$v){
  1263. $key = $v->idate;
  1264. $rate_data = json_decode($v->order_rate_data, true);
  1265. $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
  1266. }
  1267. #团队成员:
  1268. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  1269. #数据格式化
  1270. $new_data = array();
  1271. foreach($data as $k=>$v){
  1272. //当日加粉数
  1273. $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('dtime', $k)->where('is_del',0)->sum('fan_add');
  1274. $new_data[$k]['fan_add'] = $fan_add;
  1275. foreach($columns as $val){
  1276. $n = $val;
  1277. $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
  1278. }
  1279. }
  1280. //echo '<pre>';
  1281. //print_r($new_data);print_r($columns);exit;
  1282. $self_role = session('role_name');
  1283. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  1284. if($self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  1285. })->get();
  1286. $teamList = json_decode(json_encode($teamList), true);
  1287. return ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList];
  1288. }
  1289. /**
  1290. * orderSaleRank 实时销售数据展榜
  1291. */
  1292. public function orderSaleRank(Request $request){
  1293. header('Location:https://datav.aliyuncs.com/share/c936e72d66c7b5900238cf51cbd31884');
  1294. exit;
  1295. }
  1296. /**
  1297. * orderDistrict 实时订单地域分布
  1298. */
  1299. public function orderDistrict(Request $request){
  1300. header('Location:https://datav.aliyuncs.com/share/423b408234ea48d49a2a4ee7f33fa4e4');
  1301. exit;
  1302. }
  1303. /**
  1304. * 地区roi
  1305. */
  1306. public function districtRoi(Request $request){
  1307. $page = (int)$request->input('page');
  1308. $pageSize = 20;
  1309. if($page<=0){
  1310. $page = 1;
  1311. }
  1312. $offset = ($page-1) * $pageSize;
  1313. $stime = $request->input('stime');
  1314. $etime = $request->input('etime');
  1315. $city = $request->input('city');
  1316. if($city !== null){
  1317. $city = str_replace('市', '', $city);
  1318. }
  1319. $ret = AdCost::select('id')->where(function($query) use($stime, $etime, $city){
  1320. if($stime) $query->where('ad_time', '>=', $stime);
  1321. if($etime) $query->where('ad_time', '<=', $etime);
  1322. if($city) $query->where('city', '=', $city);
  1323. })->groupBy('ad_time')->groupBy('city')->get();
  1324. $ret = json_decode(json_encode($ret), true);
  1325. $count = count($ret);
  1326. if ($count > 1) {
  1327. // 总页数
  1328. $pages = ceil($count/$pageSize);
  1329. }else{
  1330. // 总页数
  1331. $pages = 1;
  1332. }
  1333. $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){
  1334. if($stime) $query->where('ad_time', '>=', $stime);
  1335. if($etime) $query->where('ad_time', '<=', $etime);
  1336. if($city) $query->where('city', 'like', '%'.$city.'%');
  1337. })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->offset($offset)->limit($pageSize)->get();
  1338. $result = json_decode(json_encode($result), true);
  1339. foreach($result as $k=>&$v){
  1340. //新粉收入
  1341. //当日加粉
  1342. $city_name = str_replace('市', '', $v['city']);
  1343. $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  1344. #当日加粉订单总计:
  1345. $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();
  1346. #当日新粉成单:
  1347. $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();
  1348. // 1.当日新粉成单数
  1349. $v['new_order_count'] = $new_order->order_count;
  1350. // 2.当日新粉成交额
  1351. $v['new_order_amount'] = $new_order->order_amount;
  1352. // 3.当日粉丝总成交额
  1353. $v['order_amount'] = $order->order_amount;
  1354. // 总单数
  1355. $v['order_count'] = $order->order_count;
  1356. //新粉roi
  1357. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1358. //累计roi
  1359. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1360. $v['new_fan_count'] = count($phones);
  1361. }
  1362. //总计
  1363. $total_data = [
  1364. 'cost' => 0,
  1365. 'fan_count' => 0,
  1366. 'order_count' => 0,
  1367. 'order_amount' => 0,
  1368. 'roi' => 0,
  1369. 'conversion_times' => 0,
  1370. ];
  1371. //投入
  1372. $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
  1373. if($stime) $query->where('ad_time', '>=', $stime);
  1374. if($etime) $query->where('ad_time', '<=', $etime);
  1375. if($city) $query->where('city', 'like', '%'.$city.'%');
  1376. })->first();
  1377. $total_data['cost'] = $adcost->total_cost;
  1378. $total_data['conversion_times'] = $adcost->conversion_times;
  1379. //总新粉
  1380. $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
  1381. if($stime) $query->where('fanTime', '>=', $stime);
  1382. if($etime) $query->where('fanTime', '<=', $etime);
  1383. if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
  1384. })->lists('phone');
  1385. $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
  1386. //总收入
  1387. $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();
  1388. $total_data['order_count'] = $order->order_count;
  1389. $total_data['order_amount'] = $order->order_amount;
  1390. $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1391. return view('statistics/districtRoi', ['result' =>$result,
  1392. 'page' =>$page,
  1393. 'count' =>$count,
  1394. 'pages' =>$pages,
  1395. 'stime' =>$stime,
  1396. 'etime' =>$etime,
  1397. 'city' =>$city,
  1398. 'total_data' =>$total_data,
  1399. ]);
  1400. }
  1401. /**
  1402. * 导出地域roi报表
  1403. */
  1404. public function districtRoi_export(Request $request){
  1405. $stime = $request->input('stime');
  1406. $etime = $request->input('etime');
  1407. $city = $request->input('city');
  1408. if($city !== null){
  1409. $city = str_replace('市', '', $city);
  1410. }
  1411. $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){
  1412. if($stime) $query->where('ad_time', '>=', $stime);
  1413. if($etime) $query->where('ad_time', '<=', $etime);
  1414. if($city) $query->where('city', 'like', '%'.$city.'%');
  1415. })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->get();
  1416. $result = json_decode(json_encode($result), true);
  1417. foreach($result as $k=>&$v){
  1418. //新粉收入
  1419. //当日加粉
  1420. $city_name = str_replace('市', '', $v['city']);
  1421. $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  1422. #当日加粉订单总计:
  1423. $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();
  1424. #当日新粉成单:
  1425. $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();
  1426. // 1.当日新粉成单数
  1427. $v['new_order_count'] = $new_order->order_count;
  1428. // 2.当日新粉成交额
  1429. $v['new_order_amount'] = $new_order->order_amount;
  1430. // 3.当日粉丝总成交额
  1431. $v['order_amount'] = $order->order_amount;
  1432. // 总单数
  1433. $v['order_count'] = $order->order_count;
  1434. //新粉roi
  1435. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1436. //累计roi
  1437. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1438. $v['new_fan_count'] = count($phones);
  1439. }
  1440. //总计
  1441. $total_data = [
  1442. 'cost' => 0,
  1443. 'fan_count' => 0,
  1444. 'order_count' => 0,
  1445. 'order_amount' => 0,
  1446. 'roi' => 0,
  1447. 'conversion_times' => 0,
  1448. ];
  1449. //投入
  1450. $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
  1451. if($stime) $query->where('ad_time', '>=', $stime);
  1452. if($etime) $query->where('ad_time', '<=', $etime);
  1453. if($city) $query->where('city', 'like', '%'.$city.'%');
  1454. })->first();
  1455. $total_data['cost'] = $adcost->total_cost;
  1456. $total_data['conversion_times'] = $adcost->conversion_times;
  1457. //总新粉
  1458. $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
  1459. if($stime) $query->where('fanTime', '>=', $stime);
  1460. if($etime) $query->where('fanTime', '<=', $etime);
  1461. if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
  1462. })->lists('phone');
  1463. $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
  1464. //总收入
  1465. $order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->whereIn('receiverMobile', $phones)->where('is_del', 0)->first();
  1466. $total_data['order_count'] = $order->order_count;
  1467. $total_data['order_amount'] = $order->order_amount;
  1468. $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1469. $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'];
  1470. $title = ['日期', '城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  1471. $filename = 'district_roi_'.date('Y-m-d_H').'.xlsx';
  1472. return Order::export_excel($result, $filename, $indexKey, $title);
  1473. }
  1474. }