Ei kuvausta

StatisticsController.php 77KB

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