Нет описания

StatisticsController.php 74KB

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