Nenhuma Descrição

StatisticsController.php 115KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  1. <?php
  2. /**
  3. * Created by Sublime.
  4. * User: hao
  5. * Date: 19/08/30
  6. * Time: 上午11:20
  7. */
  8. namespace App\Http\Controllers\Admin;
  9. use App\Http\Controllers\Controller;
  10. use App\Logs;
  11. use App\CustTotal;
  12. use App\CustDetail;
  13. use App\AdCost;
  14. use App\Order;
  15. use App\DistrictRoi7;
  16. use App\DistrictRoi30;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\DB;
  19. class StatisticsController extends Controller
  20. {
  21. /**
  22. * 当日数据统计
  23. */
  24. public function fanDay(Request $request){
  25. $page = (int)$request->input('page');
  26. $pageSize = 20;
  27. if($page<=0){
  28. $page = 1;
  29. }
  30. $offset = ($page-1) * $pageSize;
  31. $stime = $request->input('stime');
  32. $etime = $request->input('etime');
  33. $team_id = (int)$request->input('team_id');
  34. $admin_id = (int)$request->input('admin_id');
  35. $self_role = session('role_name');
  36. $team_id = $request->input('team_id');
  37. #只能看自己团队的
  38. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  39. $self_id = session('admin_id');
  40. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  41. }
  42. if($admin_id>0 && !$team_id){
  43. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  44. }
  45. //假如有团队筛选,检索销售队员
  46. $sale_ids = null;
  47. if($team_id>0){
  48. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  49. }
  50. $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime){
  51. if($team_id) $query->where('team_id', '=', $team_id);
  52. if($stime) $query->where('dtime', '>=', $stime);
  53. if($etime) $query->where('dtime', '<=', $etime);
  54. })->where('is_del',0)->first();
  55. $count = $count->total;
  56. if ($count > 1) {
  57. // 总页数
  58. $pages = ceil($count/$pageSize);
  59. }else{
  60. // 总页数
  61. $pages = 1;
  62. }
  63. $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){
  64. if($team_id) $query->where('team_id', '=', $team_id);
  65. if($stime) $query->where('dtime', '>=', $stime);
  66. if($etime) $query->where('dtime', '<=', $etime);
  67. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  68. $result = json_decode(json_encode($result),true);
  69. foreach($result as $k=>&$v){
  70. #进粉成本
  71. $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  72. #当日微信粉
  73. $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){
  74. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  75. if($admin_id>0) $query->where('admin_id', $admin_id);
  76. })->first();
  77. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  78. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  79. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  80. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  81. #当日微信粉成本
  82. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  83. #当日订单数、销售额
  84. $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){
  85. if($team_id>0) $query->where('team_id', $team_id);
  86. if($admin_id>0) $query->where('admin_id', $admin_id);
  87. })->first();
  88. $v['order_count'] = $order->order_count;
  89. $v['order_amount'] = $order->order_amount;
  90. #物流成本
  91. $v['freight_cost'] = $order->freight_cost;
  92. #货品成本
  93. $v['goods_cost'] = $order->goods_cost;
  94. #新加逻辑 -- 销售筛选
  95. if($admin_id>0){
  96. //预估当日投放 当日公众进粉
  97. # 1. 当前团队销售加粉占比
  98. $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();
  99. $salerfanRate = $teamFanAdd->team_fan_add>0 ? round($v['wx_fan_add'] / $teamFanAdd->team_fan_add, 3) * 1000 : 0;
  100. #预估该销售投放金额
  101. $v['total_cost'] = round( $salerfanRate * $v['total_cost'] / 1000, 2);
  102. #预估进粉
  103. $v['total_fan_add'] = round( $salerfanRate * $v['total_fan_add'] / 1000);
  104. #当日微信粉成本
  105. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  106. }
  107. }
  108. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  109. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  110. })->get();
  111. $teamList = json_decode(json_encode($teamList), true);
  112. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where(function($query) use($self_role, $team_id){
  113. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('team_id', $team_id);
  114. })->get();
  115. $adminList = json_decode(json_encode($adminList), true);
  116. return view('statistics/fanDay', ['result' =>$result,
  117. 'page' =>$page,
  118. 'count' =>$count,
  119. 'pages' =>$pages,
  120. 'teamlist' =>$teamList,
  121. 'adminlist' =>$adminList,
  122. 'stime' =>$stime,
  123. 'etime' =>$etime,
  124. 'team_id' =>$team_id,
  125. 'admin_id' =>$admin_id,
  126. ]);
  127. }
  128. public function fanDay_export(Request $request){
  129. $stime = $request->input('stime');
  130. $etime = $request->input('etime');
  131. $team_id = (int)$request->input('team_id');
  132. $self_role = session('role_name');
  133. #只能看自己团队的
  134. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  135. $self_id = session('admin_id');
  136. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  137. }
  138. //假如有团队筛选,检索销售队员
  139. $sale_ids = null;
  140. if($team_id>0){
  141. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  142. }
  143. $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){
  144. if($team_id) $query->where('team_id', '=', $team_id);
  145. if($stime) $query->where('dtime', '>=', $stime);
  146. if($etime) $query->where('dtime', '<=', $etime);
  147. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  148. $result = json_decode(json_encode($result),true);
  149. $filename="当日数据统计.xls";
  150. header("Content-type:application/vnd.ms-excel");
  151. Header("Accept-Ranges:bytes");
  152. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  153. header("Pragma: no-cache");
  154. header("Expires: 0");
  155. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  156. xmlns:x="urn:schemas-microsoft-com:office:excel"
  157. xmlns="http://www.w3.org/TR/REC-html40">
  158. <head>
  159. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  160. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  161. <!--[if gte mso 9]><xml>
  162. <x:ExcelWorkbook>
  163. <x:ExcelWorksheets>
  164. <x:ExcelWorksheet>
  165. <x:Name></x:Name>
  166. <x:WorksheetOptions>
  167. <x:DisplayGridlines/>
  168. </x:WorksheetOptions>
  169. </x:ExcelWorksheet>
  170. </x:ExcelWorksheets>
  171. </x:ExcelWorkbook>
  172. </xml><![endif]-->
  173. </head>';
  174. $data_str .= "
  175. <table>
  176. <tr>
  177. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  178. <th>".iconv("UTF-8", "GB2312//IGNORE","当日投放金额")."</th>
  179. <th>".iconv("UTF-8", "GB2312//IGNORE","当日公众号进粉")."</th>
  180. <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉成本")."</th>
  181. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉")."</th>
  182. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉成本")."</th>
  183. <th>".iconv("UTF-8", "GB2312//IGNORE","当日订单数")."</th>
  184. <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
  185. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复量")."</th>
  186. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉回复率")."</th>
  187. <th>".iconv("UTF-8", "GB2312//IGNORE","当日老粉主动咨询量")."</th>
  188. </tr>";
  189. foreach ($result as $k => $v)
  190. {
  191. #进粉成本
  192. $v['cost_fan'] = $v['total_fan_add']>0 ? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  193. #当日微信粉
  194. $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){
  195. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  196. })->first();
  197. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  198. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  199. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  200. $v['new_reply_rate'] = $v['wx_fan_add']>0 ? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  201. #当日微信粉成本
  202. $v['cost_wx_fan'] = $v['wx_fan_add']>0 ? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  203. #当日订单数、销售额
  204. $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){
  205. if($team_id>0) $query->where('team_id', $team_id);
  206. })->first();
  207. $v['order_count'] = $order->order_count;
  208. $v['order_amount'] = $order->order_amount;
  209. $data_str .= "<tr>";
  210. $data_str .= "<td>".$v['dtime']."</td>";
  211. $data_str .= "<td>".$v['total_cost']."</td>";
  212. $data_str .= "<td>".$v['total_fan_add']."</td>";
  213. $data_str .= "<td>".$v['cost_fan']."</td>";
  214. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  215. $data_str .= "<td>".$v['cost_wx_fan']."</td>";
  216. $data_str .= "<td>".$v['order_count']."</td>";
  217. $data_str .= "<td>".$v['order_amount']."</td>";
  218. $data_str .= "<td>".$v['total_new_reply']."</td>";
  219. $data_str .= "<td>".$v['new_reply_rate']."</td>";
  220. $data_str .= "<td>".$v['total_old_consult']."</td>";
  221. $data_str .= "</tr>";
  222. }
  223. $data_str .= "</table>";
  224. echo $data_str;
  225. exit;
  226. }
  227. /**
  228. * 当日数据统计
  229. */
  230. public function orderDay(Request $request){
  231. $page = (int)$request->input('page');
  232. $pageSize = 20;
  233. if($page<=0){
  234. $page = 1;
  235. }
  236. $offset = ($page-1) * $pageSize;
  237. $stime = $request->input('stime');
  238. $etime = $request->input('etime');
  239. $team_id = (int)$request->input('team_id');
  240. $self_role = session('role_name');
  241. #只能看自己团队的
  242. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  243. $self_id = session('admin_id');
  244. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  245. }
  246. //假如有团队筛选,检索销售队员
  247. $sale_ids = null;
  248. if($team_id>0){
  249. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  250. }
  251. //规定只统计前天及以前的数据
  252. $end_time = date('Y-m-d');
  253. $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime, $end_time){
  254. if($team_id) $query->where('team_id', '=', $team_id);
  255. if($stime) $query->where('dtime', '>=', $stime);
  256. if($etime) $query->where('dtime', '<=', $etime);
  257. $query->where('dtime','<', $end_time);
  258. })->where('is_del',0)->first();
  259. $count = $count->total;
  260. if ($count > 1) {
  261. // 总页数
  262. $pages = ceil($count/$pageSize);
  263. }else{
  264. // 总页数
  265. $pages = 1;
  266. }
  267. $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){
  268. if($team_id) $query->where('team_id', '=', $team_id);
  269. if($stime) $query->where('dtime', '>=', $stime);
  270. if($etime) $query->where('dtime', '<=', $etime);
  271. $query->where('dtime','<', $end_time);
  272. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  273. $result = json_decode(json_encode($result),true);
  274. foreach($result as $k=>&$v){
  275. #当日微信好友数量
  276. $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){
  277. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  278. })->first();
  279. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  280. //当日加粉
  281. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  282. #当日加粉订单总计:
  283. $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){
  284. if($team_id>0) $query->where('team_id', $team_id);
  285. })->first();
  286. #当日新粉成单:
  287. $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){
  288. if($team_id>0) $query->where('team_id', $team_id);
  289. })->first();
  290. // 1.当日新粉成单数
  291. $v['new_order_count'] = $new_order->order_count;
  292. // 2.当日新粉成交额
  293. $v['new_order_amount'] = $new_order->order_amount;
  294. $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){
  295. if($team_id>0) $query->where('team_id', $team_id);
  296. })->first();
  297. // 3.老粉成单数
  298. $v['old_order_count'] = $old_order->order_count;
  299. // 4.老粉成交额
  300. $v['old_order_amount'] = $old_order->order_amount;
  301. // 5.总成交额
  302. $v['order_amount'] = $order->order_amount;
  303. #复购单数、复购成交额
  304. $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){
  305. if($team_id>0) $query->where('team_id', $team_id);
  306. })->first();
  307. $v['fugou_order_count'] = $fugou->order_count;
  308. $v['fugou_order_amount'] = $fugou->order_amount;
  309. #货品成本
  310. $v['order_cost'] = $order->order_cost;
  311. //加物流成本
  312. $v['freight_cost'] = $order->freight_cost;
  313. #毛利
  314. $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'] - $v['freight_cost'];
  315. //综合成单率 综合成单率=订单数/微信好友数
  316. $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  317. //新粉成单率
  318. $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  319. //复购率
  320. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  321. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  322. //加roi
  323. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  324. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  325. }
  326. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  327. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  328. })->get();
  329. $teamList = json_decode(json_encode($teamList), true);
  330. return view('statistics/orderDay', ['result' =>$result,
  331. 'page' =>$page,
  332. 'count' =>$count,
  333. 'pages' =>$pages,
  334. 'teamlist' =>$teamList,
  335. 'stime' =>$stime,
  336. 'etime' =>$etime,
  337. 'team_id' =>$team_id,
  338. ]);
  339. }
  340. public function orderDay_export(Request $request){
  341. $stime = $request->input('stime');
  342. $etime = $request->input('etime');
  343. $team_id = (int)$request->input('team_id');
  344. $self_role = session('role_name');
  345. #只能看自己团队的
  346. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  347. $self_id = session('admin_id');
  348. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  349. }
  350. //假如有团队筛选,检索销售队员
  351. $sale_ids = null;
  352. if($team_id>0){
  353. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  354. }
  355. //规定只统计前天及以前的数据
  356. $end_time = date('Y-m-d');
  357. $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){
  358. if($team_id) $query->where('team_id', '=', $team_id);
  359. if($stime) $query->where('dtime', '>=', $stime);
  360. if($etime) $query->where('dtime', '<=', $etime);
  361. $query->where('dtime','<', $end_time);
  362. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  363. $result = json_decode(json_encode($result),true);
  364. $filename="分片数据统计.xls";
  365. header("Content-type:application/vnd.ms-excel");
  366. Header("Accept-Ranges:bytes");
  367. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  368. header("Pragma: no-cache");
  369. header("Expires: 0");
  370. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  371. xmlns:x="urn:schemas-microsoft-com:office:excel"
  372. xmlns="http://www.w3.org/TR/REC-html40">
  373. <head>
  374. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  375. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  376. <!--[if gte mso 9]><xml>
  377. <x:ExcelWorkbook>
  378. <x:ExcelWorksheets>
  379. <x:ExcelWorksheet>
  380. <x:Name></x:Name>
  381. <x:WorksheetOptions>
  382. <x:DisplayGridlines/>
  383. </x:WorksheetOptions>
  384. </x:ExcelWorksheet>
  385. </x:ExcelWorksheets>
  386. </x:ExcelWorkbook>
  387. </xml><![endif]-->
  388. </head>';
  389. $data_str .= "
  390. <table>
  391. <tr>
  392. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  393. <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉数量")."</th>
  394. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
  395. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
  396. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
  397. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
  398. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  399. <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
  400. <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
  401. <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
  402. <th>".iconv("UTF-8", "GB2312//IGNORE","总投放成本")."</th>
  403. <th>".iconv("UTF-8", "GB2312//IGNORE","货品成本")."</th>
  404. <th>".iconv("UTF-8", "GB2312//IGNORE","毛利")."</th>
  405. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
  406. <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
  407. </tr>";
  408. foreach ($result as $k => $v)
  409. {
  410. #当日微信好友数量
  411. $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){
  412. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  413. })->first();
  414. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  415. //当日加粉
  416. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  417. #当日加粉订单总计:
  418. $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){
  419. if($team_id>0) $query->where('team_id', $team_id);
  420. })->first();
  421. #当日新粉成单:
  422. $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){
  423. if($team_id>0) $query->where('team_id', $team_id);
  424. })->first();
  425. // 1.当日新粉成单数
  426. $v['new_order_count'] = $new_order->order_count;
  427. // 2.当日新粉成交额
  428. $v['new_order_amount'] = $new_order->order_amount;
  429. $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){
  430. if($team_id>0) $query->where('team_id', $team_id);
  431. })->first();
  432. // 3.老粉成单数
  433. $v['old_order_count'] = $old_order->order_count;
  434. // 4.老粉成交额
  435. $v['old_order_amount'] = $old_order->order_amount;
  436. // 5.总成交额
  437. $v['order_amount'] = $order->order_amount;
  438. #复购单数、复购成交额
  439. $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){
  440. if($team_id>0) $query->where('team_id', $team_id);
  441. })->first();
  442. $v['fugou_order_count'] = $fugou->order_count;
  443. $v['fugou_order_amount'] = $fugou->order_amount;
  444. #货品成本
  445. $v['order_cost'] = $order->order_cost;
  446. #毛利
  447. $v['profit'] = $v['order_amount'] - $v['order_cost'] - $v['total_cost'];
  448. //综合成单率 综合成单率=订单数/微信好友数
  449. $v['order_rate'] = $custDetail->wx_fan_add>0 ? round($order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  450. //新粉成单率
  451. $v['new_order_rate'] = $custDetail->wx_fan_add>0 ? round($new_order->order_count/$custDetail->wx_fan_add, 4) * 100 .'%' : '';
  452. //复购率
  453. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  454. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  455. $data_str .= "<tr>";
  456. $data_str .= "<td>".$v['dtime']."</td>";
  457. $data_str .= "<td>".$v['total_fan_add']."</td>";
  458. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  459. $data_str .= "<td>".$v['new_order_count']."</td>";
  460. $data_str .= "<td>".$v['new_order_amount']."</td>";
  461. $data_str .= "<td>".$v['old_order_count']."</td>";
  462. $data_str .= "<td>".$v['old_order_amount']."</td>";
  463. $data_str .= "<td>".$v['fugou_order_count']."</td>";
  464. $data_str .= "<td>".$v['fugou_order_amount']."</td>";
  465. $data_str .= "<td>".$v['order_amount']."</td>";
  466. $data_str .= "<td>".$v['total_cost']."</td>";
  467. $data_str .= "<td>".$v['order_cost']."</td>";
  468. $data_str .= "<td>".$v['profit']."</td>";
  469. $data_str .= "<td>".$v['new_order_rate']."</td>";
  470. $data_str .= "<td>".$v['order_rate']."</td>";
  471. $data_str .= "</tr>";
  472. }
  473. $data_str .= "</table>";
  474. echo $data_str;
  475. exit;
  476. }
  477. /**
  478. * 当日数据统计(投放)
  479. */
  480. public function throwDay(Request $request){
  481. $page = (int)$request->input('page');
  482. $pageSize = 20;
  483. if($page<=0){
  484. $page = 1;
  485. }
  486. $offset = ($page-1) * $pageSize;
  487. $stime = $request->input('stime');
  488. $etime = $request->input('etime');
  489. $team_id = (int)$request->input('team_id');
  490. //假如有团队筛选,检索销售队员
  491. $sale_ids = null;
  492. if($team_id>0){
  493. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  494. }
  495. $count = CustTotal::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($team_id, $stime, $etime){
  496. if($team_id) $query->where('team_id', '=', $team_id);
  497. if($stime) $query->where('dtime', '>=', $stime);
  498. if($etime) $query->where('dtime', '<=', $etime);
  499. })->where('is_del',0)->first();
  500. $count = $count->total;
  501. if ($count > 1) {
  502. // 总页数
  503. $pages = ceil($count/$pageSize);
  504. }else{
  505. // 总页数
  506. $pages = 1;
  507. }
  508. $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){
  509. if($team_id) $query->where('team_id', '=', $team_id);
  510. if($stime) $query->where('dtime', '>=', $stime);
  511. if($etime) $query->where('dtime', '<=', $etime);
  512. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  513. $result = json_decode(json_encode($result),true);
  514. foreach($result as $k=>&$v){
  515. #进粉成本
  516. $v['cost_fan'] = $v['total_fan_add']>0? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  517. #当日微信粉
  518. $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){
  519. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  520. })->first();
  521. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  522. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  523. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  524. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  525. #当日微信粉成本
  526. $v['cost_wx_fan'] = $v['wx_fan_add']>0? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  527. #当日订单数、销售额
  528. $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){
  529. if($team_id>0) $query->where('team_id', $team_id);
  530. })->first();
  531. $v['order_count'] = $order->order_count;
  532. $v['order_amount'] = $order->order_amount;
  533. }
  534. $teamList = DB::table('teams')->select('id', 'name')->get();
  535. $teamList = json_decode(json_encode($teamList), true);
  536. return view('statistics/throwDay', ['result' =>$result,
  537. 'page' =>$page,
  538. 'count' =>$count,
  539. 'pages' =>$pages,
  540. 'teamlist' =>$teamList,
  541. 'stime' =>$stime,
  542. 'etime' =>$etime,
  543. 'team_id' =>$team_id,
  544. ]);
  545. }
  546. public function throwDay_export(Request $request){
  547. $stime = $request->input('stime');
  548. $etime = $request->input('etime');
  549. $team_id = (int)$request->input('team_id');
  550. //假如有团队筛选,检索销售队员
  551. $sale_ids = null;
  552. if($team_id>0){
  553. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  554. }
  555. $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){
  556. if($team_id) $query->where('team_id', '=', $team_id);
  557. if($stime) $query->where('dtime', '>=', $stime);
  558. if($etime) $query->where('dtime', '<=', $etime);
  559. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  560. $result = json_decode(json_encode($result),true);
  561. $filename="当日数据统计.xls";
  562. header("Content-type:application/vnd.ms-excel");
  563. Header("Accept-Ranges:bytes");
  564. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  565. header("Pragma: no-cache");
  566. header("Expires: 0");
  567. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  568. xmlns:x="urn:schemas-microsoft-com:office:excel"
  569. xmlns="http://www.w3.org/TR/REC-html40">
  570. <head>
  571. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  572. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  573. <!--[if gte mso 9]><xml>
  574. <x:ExcelWorkbook>
  575. <x:ExcelWorksheets>
  576. <x:ExcelWorksheet>
  577. <x:Name></x:Name>
  578. <x:WorksheetOptions>
  579. <x:DisplayGridlines/>
  580. </x:WorksheetOptions>
  581. </x:ExcelWorksheet>
  582. </x:ExcelWorksheets>
  583. </x:ExcelWorkbook>
  584. </xml><![endif]-->
  585. </head>';
  586. $data_str .= "
  587. <table>
  588. <tr>
  589. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  590. <th>".iconv("UTF-8", "GB2312//IGNORE","当日投放金额")."</th>
  591. <th>".iconv("UTF-8", "GB2312//IGNORE","当日公众号进粉")."</th>
  592. <th>".iconv("UTF-8", "GB2312//IGNORE","当日进粉成本")."</th>
  593. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉")."</th>
  594. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信粉成本")."</th>
  595. </tr>";
  596. foreach ($result as $k => $v)
  597. {
  598. #进粉成本
  599. $v['cost_fan'] = $v['total_fan_add']>0 ? round($v['total_cost'] / $v['total_fan_add'], 2) : '';
  600. #当日微信粉
  601. $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){
  602. if($team_id>0 && isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  603. })->first();
  604. $v['wx_fan_add'] = $custDetail->wx_fan_add; //当日微信粉数
  605. $v['total_new_reply'] = $custDetail->total_new_reply; //当日微信新粉回复
  606. $v['total_old_consult'] = $custDetail->total_old_consult; //当日微信老粉询价
  607. $v['new_reply_rate'] = $v['wx_fan_add']>0 ? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  608. #当日微信粉成本
  609. $v['cost_wx_fan'] = $v['wx_fan_add']>0 ? round($v['total_cost'] / $v['wx_fan_add'], 2) : '';
  610. #当日订单数、销售额
  611. $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){
  612. if($team_id>0) $query->where('team_id', $team_id);
  613. })->first();
  614. $v['order_count'] = $order->order_count;
  615. $v['order_amount'] = $order->order_amount;
  616. $data_str .= "<tr>";
  617. $data_str .= "<td>".$v['dtime']."</td>";
  618. $data_str .= "<td>".$v['total_cost']."</td>";
  619. $data_str .= "<td>".$v['total_fan_add']."</td>";
  620. $data_str .= "<td>".$v['cost_fan']."</td>";
  621. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  622. $data_str .= "<td>".$v['cost_wx_fan']."</td>";
  623. $data_str .= "</tr>";
  624. }
  625. $data_str .= "</table>";
  626. echo $data_str;
  627. exit;
  628. }
  629. /**
  630. * 销售可看当日数据表
  631. *
  632. */
  633. public function fanDaySaler(Request $request){
  634. $page = (int)$request->input('page');
  635. $pageSize = 20;
  636. if($page<=0){
  637. $page = 1;
  638. }
  639. $offset = ($page-1) * $pageSize;
  640. $stime = $request->input('stime');
  641. $etime = $request->input('etime');
  642. $team_id = (int)$request->input('team_id');
  643. $admin_id = (int)$request->input('admin_id');
  644. $is_self_team = (int)$request->input('is_self_team');
  645. //规定只统计前天及以前的数据
  646. $end_time = date('Y-m-d');
  647. $self_role = session('role_name');
  648. if($self_role == '销售'){
  649. $admin_id = session('admin_id');
  650. #假如查看团队的
  651. if($is_self_team == 1){
  652. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  653. $admin_id = 0;
  654. }else{
  655. $team_id = null;
  656. }
  657. }
  658. //假如有团队筛选,检索销售队员
  659. $sale_ids = null;
  660. if($team_id>0){
  661. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  662. }
  663. $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
  664. if($admin_id) $query->where('admin_id', '=', $admin_id);
  665. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  666. if($stime) $query->where('dtime', '>=', $stime);
  667. if($etime) $query->where('dtime', '<=', $etime);
  668. if($end_time) $query->where('dtime', '<', $end_time);
  669. })->where('is_del',0)->first();
  670. $count = $count->total;
  671. if ($count > 1) {
  672. // 总页数
  673. $pages = ceil($count/$pageSize);
  674. }else{
  675. // 总页数
  676. $pages = 1;
  677. }
  678. $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){
  679. if($admin_id) $query->where('admin_id', '=', $admin_id);
  680. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  681. if($stime) $query->where('dtime', '>=', $stime);
  682. if($etime) $query->where('dtime', '<=', $etime);
  683. if($end_time) $query->where('dtime', '<', $end_time);
  684. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  685. $result = json_decode(json_encode($result),true);
  686. foreach($result as $k=>&$v){
  687. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  688. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  689. //当日加粉
  690. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  691. #当日新粉成单:
  692. $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){
  693. if($team_id>0) $query->where('team_id', $team_id);
  694. if($admin_id>0) $query->where('admin_id', $admin_id);
  695. })->first();
  696. // 1.当日新粉成单数
  697. $v['new_order_count'] = $new_order->order_count;
  698. // 2.当日新粉成交额
  699. $v['new_order_amount'] = $new_order->order_amount;
  700. // 3.新粉成交转化率
  701. $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
  702. //累计老粉量
  703. $dtime = $v['dtime'];
  704. $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
  705. if($admin_id) $query->where('admin_id', '=', $admin_id);
  706. if($sale_ids) $query->whereIn('admin_id', $sale_ids);
  707. if($dtime) $query->where('dtime', '<', $dtime);
  708. })->where('is_del',0)->sum('fan_add');
  709. $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
  710. #当日订单数、销售额
  711. $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){
  712. if($team_id>0) $query->where('team_id', $team_id);
  713. if($admin_id>0) $query->where('admin_id', $admin_id);
  714. })->first();
  715. $v['order_count'] = $order->order_count;
  716. $v['order_amount'] = $order->order_amount;
  717. // 3.老粉成单数
  718. $v['old_order_count'] = $order->order_count - $v['new_order_count'];
  719. // 4.老粉成交额
  720. $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
  721. // 5.老粉成交转化率
  722. $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
  723. }
  724. $teamList = DB::table('teams')->select('id', 'name')->get();
  725. $teamList = json_decode(json_encode($teamList), true);
  726. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
  727. $adminList = json_decode(json_encode($adminList), true);
  728. return view('statistics/fanDaySaler', ['result' =>$result,
  729. 'page' =>$page,
  730. 'count' =>$count,
  731. 'pages' =>$pages,
  732. 'teamlist' =>$teamList,
  733. 'adminlist' =>$adminList,
  734. 'stime' =>$stime,
  735. 'etime' =>$etime,
  736. 'team_id' =>$team_id,
  737. 'admin_id' =>$admin_id,
  738. 'is_self_team' =>$is_self_team,
  739. 'self_role' =>$self_role,
  740. ]);
  741. }
  742. public function fanDaySaler_export(Request $request){
  743. $stime = $request->input('stime');
  744. $etime = $request->input('etime');
  745. $team_id = (int)$request->input('team_id');
  746. $admin_id = (int)$request->input('admin_id');
  747. $is_self_team = (int)$request->input('is_self_team');
  748. //规定只统计前天及以前的数据
  749. $end_time = date('Y-m-d');
  750. $self_role = session('role_name');
  751. if($self_role == '销售'){
  752. $admin_id = session('admin_id');
  753. #假如查看团队的
  754. if($is_self_team == 1){
  755. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  756. $admin_id = 0;
  757. }else{
  758. $team_id = null;
  759. }
  760. }
  761. //假如有团队筛选,检索销售队员
  762. $sale_ids = null;
  763. if($team_id>0){
  764. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  765. }
  766. $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){
  767. if($admin_id) $query->where('admin_id', '=', $admin_id);
  768. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  769. if($stime) $query->where('dtime', '>=', $stime);
  770. if($etime) $query->where('dtime', '<=', $etime);
  771. if($end_time) $query->where('dtime', '<', $end_time);
  772. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  773. $result = json_decode(json_encode($result),true);
  774. $filename="当日数据统计(销售).xls";
  775. header("Content-type:application/vnd.ms-excel");
  776. Header("Accept-Ranges:bytes");
  777. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  778. header("Pragma: no-cache");
  779. header("Expires: 0");
  780. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  781. xmlns:x="urn:schemas-microsoft-com:office:excel"
  782. xmlns="http://www.w3.org/TR/REC-html40">
  783. <head>
  784. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  785. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  786. <!--[if gte mso 9]><xml>
  787. <x:ExcelWorkbook>
  788. <x:ExcelWorksheets>
  789. <x:ExcelWorksheet>
  790. <x:Name></x:Name>
  791. <x:WorksheetOptions>
  792. <x:DisplayGridlines/>
  793. </x:WorksheetOptions>
  794. </x:ExcelWorksheet>
  795. </x:ExcelWorksheets>
  796. </x:ExcelWorkbook>
  797. </xml><![endif]-->
  798. </head>';
  799. $data_str .= "
  800. <table>
  801. <tr>
  802. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  803. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉增加量")."</th>
  804. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复")."</th>
  805. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉回复率")."</th>
  806. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价量")."</th>
  807. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉询价率")."</th>
  808. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交单量")."</th>
  809. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交额")."</th>
  810. <th>".iconv("UTF-8", "GB2312//IGNORE","新粉成交转化率")."</th>
  811. <th>".iconv("UTF-8", "GB2312//IGNORE","累计老粉量")."</th>
  812. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价量")."</th>
  813. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉询价率")."</th>
  814. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交单量")."</th>
  815. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  816. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交转化率")."</th>
  817. <th>".iconv("UTF-8", "GB2312//IGNORE","当日单量")."</th>
  818. <th>".iconv("UTF-8", "GB2312//IGNORE","当日销售额")."</th>
  819. </tr>";
  820. foreach ($result as $k => $v)
  821. {
  822. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  823. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  824. //当日加粉
  825. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  826. #当日新粉成单:
  827. $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){
  828. if($team_id>0) $query->where('team_id', $team_id);
  829. if($admin_id>0) $query->where('admin_id', $admin_id);
  830. })->first();
  831. // 1.当日新粉成单数
  832. $v['new_order_count'] = $new_order->order_count;
  833. // 2.当日新粉成交额
  834. $v['new_order_amount'] = $new_order->order_amount;
  835. // 3.新粉成交转化率
  836. $v['new_order_rate'] = $v['total_new_consult']>0 ? round($v['new_order_count'] / $v['total_new_consult'], 4) * 100 .'%' : '';
  837. //累计老粉量
  838. $dtime = $v['dtime'];
  839. $v['wx_old_fan'] = CustDetail::where(function($query) use($sale_ids, $dtime, $admin_id){
  840. if($admin_id) $query->where('admin_id', '=', $admin_id);
  841. if($sale_ids) $query->whereIn('admin_id', $sale_ids);
  842. if($dtime) $query->where('dtime', '<', $dtime);
  843. })->where('is_del',0)->sum('fan_add');
  844. $v['old_consult_rate'] = $v['wx_old_fan']>0? round($v['total_old_consult'] / $v['wx_old_fan'], 4) * 100 .'%' : ''; //当日老粉询价率
  845. #当日订单数、销售额
  846. $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){
  847. if($team_id>0) $query->where('team_id', $team_id);
  848. if($admin_id>0) $query->where('admin_id', $admin_id);
  849. })->first();
  850. $v['order_count'] = $order->order_count;
  851. $v['order_amount'] = $order->order_amount;
  852. // 3.老粉成单数
  853. $v['old_order_count'] = $order->order_count - $v['new_order_count'];
  854. // 4.老粉成交额
  855. $v['old_order_amount'] = $order->order_amount - $v['new_order_amount'];
  856. // 5.老粉成交转化率
  857. $v['old_order_rate'] = $v['total_old_consult']>0 ? round($v['old_order_count'] / $v['total_old_consult'], 4) * 100 .'%' : '';
  858. $data_str .= "<tr>";
  859. $data_str .= "<td>".$v['dtime']."</td>";
  860. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  861. $data_str .= "<td>".$v['total_new_reply']."</td>";
  862. $data_str .= "<td>".$v['new_reply_rate']."</td>";
  863. $data_str .= "<td>".$v['total_new_consult']."</td>";
  864. $data_str .= "<td>".$v['new_consult_rate']."</td>";
  865. $data_str .= "<td>".$v['new_order_count']."</td>";
  866. $data_str .= "<td>".$v['new_order_amount']."</td>";
  867. $data_str .= "<td>".$v['new_order_rate']."</td>";
  868. $data_str .= "<td>".$v['wx_old_fan']."</td>";
  869. $data_str .= "<td>".$v['total_old_consult']."</td>";
  870. $data_str .= "<td>".$v['old_consult_rate']."</td>";
  871. $data_str .= "<td>".$v['old_order_count']."</td>";
  872. $data_str .= "<td>".$v['old_order_amount']."</td>";
  873. $data_str .= "<td>".$v['old_order_rate']."</td>";
  874. $data_str .= "<td>".$v['order_count']."</td>";
  875. $data_str .= "<td>".$v['order_amount']."</td>";
  876. $data_str .= "</tr>";
  877. }
  878. $data_str .= "</table>";
  879. echo $data_str;
  880. exit;
  881. }
  882. /**
  883. * 销售可看当日分片数据
  884. *
  885. */
  886. public function orderDaySaler(Request $request){
  887. $page = (int)$request->input('page');
  888. $pageSize = 20;
  889. if($page<=0){
  890. $page = 1;
  891. }
  892. $offset = ($page-1) * $pageSize;
  893. $stime = $request->input('stime');
  894. $etime = $request->input('etime');
  895. $team_id = (int)$request->input('team_id');
  896. $admin_id = (int)$request->input('admin_id');
  897. $is_self_team = (int)$request->input('is_self_team');
  898. //规定只统计前天及以前的数据
  899. $end_time = date('Y-m-d');
  900. $self_role = session('role_name');
  901. if($self_role == '销售'){
  902. $admin_id = session('admin_id');
  903. #假如查看团队的
  904. if($is_self_team == 1){
  905. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  906. $admin_id = 0;
  907. }else{
  908. $team_id = null;
  909. }
  910. }
  911. //假如有团队筛选,检索销售队员
  912. $sale_ids = null;
  913. if($team_id>0){
  914. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  915. }
  916. $count = CustDetail::select(DB::raw('count(distinct dtime) as total'))->where(function($query) use($sale_ids, $stime, $etime, $admin_id, $end_time){
  917. if($admin_id) $query->where('admin_id', '=', $admin_id);
  918. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  919. if($stime) $query->where('dtime', '>=', $stime);
  920. if($etime) $query->where('dtime', '<=', $etime);
  921. $query->where('dtime', '<', $end_time);
  922. })->where('is_del',0)->first();
  923. $count = $count->total;
  924. if ($count > 1) {
  925. // 总页数
  926. $pages = ceil($count/$pageSize);
  927. }else{
  928. // 总页数
  929. $pages = 1;
  930. }
  931. $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){
  932. if($admin_id) $query->where('admin_id', '=', $admin_id);
  933. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  934. if($stime) $query->where('dtime', '>=', $stime);
  935. if($etime) $query->where('dtime', '<=', $etime);
  936. $query->where('dtime', '<', $end_time);
  937. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->offset($offset)->limit($pageSize)->get();
  938. $result = json_decode(json_encode($result),true);
  939. foreach($result as $k=>&$v){
  940. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  941. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  942. //当日加粉
  943. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  944. #当日新粉成单:
  945. $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){
  946. if($team_id>0) $query->where('team_id', $team_id);
  947. if($admin_id>0) $query->where('admin_id', $admin_id);
  948. })->first();
  949. // 1.当日新粉成单数
  950. $v['new_order_count'] = $new_order->order_count;
  951. // 2.当日新粉成交额
  952. $v['new_order_amount'] = $new_order->order_amount;
  953. #当日老粉成单
  954. $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){
  955. if($team_id>0) $query->where('team_id', $team_id);
  956. if($admin_id>0) $query->where('admin_id', $admin_id);
  957. })->first();
  958. // 1.当日老粉成单数
  959. $v['old_order_count'] = $old_order->order_count;
  960. // 2.当日老粉成交额
  961. $v['old_order_amount'] = $old_order->order_amount;
  962. #复购单数、复购成交额
  963. $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){
  964. if($team_id>0) $query->where('team_id', $team_id);
  965. if($admin_id>0) $query->where('admin_id', $admin_id);
  966. })->first();
  967. $v['fugou_order_count'] = $fugou->order_count;
  968. $v['fugou_order_amount'] = $fugou->order_amount;
  969. #当日加粉订单总计:
  970. $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){
  971. if($team_id>0) $query->where('team_id', $team_id);
  972. if($admin_id>0) $query->where('admin_id', $admin_id);
  973. })->first();
  974. //总成交额
  975. $v['order_amount'] = $order->order_amount;
  976. //综合成单率 综合成单率=订单数/微信好友数
  977. $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
  978. //新粉成单率
  979. $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
  980. //复购率
  981. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  982. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  983. }
  984. $teamList = DB::table('teams')->select('id', 'name')->get();
  985. $teamList = json_decode(json_encode($teamList), true);
  986. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
  987. $adminList = json_decode(json_encode($adminList), true);
  988. return view('statistics/orderDaySaler', ['result' =>$result,
  989. 'page' =>$page,
  990. 'count' =>$count,
  991. 'pages' =>$pages,
  992. 'teamlist' =>$teamList,
  993. 'adminlist' =>$adminList,
  994. 'stime' =>$stime,
  995. 'etime' =>$etime,
  996. 'team_id' =>$team_id,
  997. 'admin_id' =>$admin_id,
  998. 'is_self_team' =>$is_self_team,
  999. 'self_role' =>$self_role,
  1000. ]);
  1001. }
  1002. public function orderDaySaler_export(Request $request){
  1003. $stime = $request->input('stime');
  1004. $etime = $request->input('etime');
  1005. $team_id = (int)$request->input('team_id');
  1006. $admin_id = (int)$request->input('admin_id');
  1007. $is_self_team = (int)$request->input('is_self_team');
  1008. //规定只统计前天及以前的数据
  1009. $end_time = date('Y-m-d');
  1010. $self_role = session('role_name');
  1011. if($self_role == '销售'){
  1012. $admin_id = session('admin_id');
  1013. #假如查看团队的
  1014. if($is_self_team == 1){
  1015. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  1016. $admin_id = 0;
  1017. }else{
  1018. $team_id = null;
  1019. }
  1020. }
  1021. //假如有团队筛选,检索销售队员
  1022. $sale_ids = null;
  1023. if($team_id>0){
  1024. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  1025. }
  1026. $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){
  1027. if($admin_id) $query->where('admin_id', '=', $admin_id);
  1028. if(isset($sale_ids)) $query->whereIn('admin_id', $sale_ids);
  1029. if($stime) $query->where('dtime', '>=', $stime);
  1030. if($etime) $query->where('dtime', '<=', $etime);
  1031. $query->where('dtime', '<', $end_time);
  1032. })->where('is_del',0)->groupBy('dtime')->orderBy('dtime', 'desc')->get();
  1033. $result = json_decode(json_encode($result),true);
  1034. $filename="当日数据统计(销售).xls";
  1035. header("Content-type:application/vnd.ms-excel");
  1036. Header("Accept-Ranges:bytes");
  1037. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  1038. header("Pragma: no-cache");
  1039. header("Expires: 0");
  1040. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  1041. xmlns:x="urn:schemas-microsoft-com:office:excel"
  1042. xmlns="http://www.w3.org/TR/REC-html40">
  1043. <head>
  1044. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  1045. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  1046. <!--[if gte mso 9]><xml>
  1047. <x:ExcelWorkbook>
  1048. <x:ExcelWorksheets>
  1049. <x:ExcelWorksheet>
  1050. <x:Name></x:Name>
  1051. <x:WorksheetOptions>
  1052. <x:DisplayGridlines/>
  1053. </x:WorksheetOptions>
  1054. </x:ExcelWorksheet>
  1055. </x:ExcelWorksheets>
  1056. </x:ExcelWorkbook>
  1057. </xml><![endif]-->
  1058. </head>';
  1059. $data_str .= "
  1060. <table>
  1061. <tr>
  1062. <th>".iconv("UTF-8", "GB2312//IGNORE","日期")."</th>
  1063. <th>".iconv("UTF-8", "GB2312//IGNORE","当日微信好友数量")."</th>
  1064. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单数")."</th>
  1065. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成交额")."</th>
  1066. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成单数")."</th>
  1067. <th>".iconv("UTF-8", "GB2312//IGNORE","老粉成交额")."</th>
  1068. <th>".iconv("UTF-8", "GB2312//IGNORE","复购单数")."</th>
  1069. <th>".iconv("UTF-8", "GB2312//IGNORE","复购成交额")."</th>
  1070. <th>".iconv("UTF-8", "GB2312//IGNORE","总成交额")."</th>
  1071. <th>".iconv("UTF-8", "GB2312//IGNORE","当日新粉成单率")."</th>
  1072. <th>".iconv("UTF-8", "GB2312//IGNORE","复购率")."</th>
  1073. <th>".iconv("UTF-8", "GB2312//IGNORE","综合成单率")."</th>
  1074. </tr>";
  1075. foreach ($result as $k => $v)
  1076. {
  1077. $v['new_reply_rate'] = $v['wx_fan_add']>0? round($v['total_new_reply'] / $v['wx_fan_add'], 4) * 100 .'%' : ''; //当日新粉回复率
  1078. $v['new_consult_rate'] = $v['total_new_reply']>0? round($v['total_new_consult'] / $v['total_new_reply'], 4) * 100 .'%' : ''; //当日新粉询价率=
  1079. //当日加粉
  1080. $phones = DB::table('customers')->where('fanTime', $v['dtime'])->lists('phone');
  1081. #当日新粉成单:
  1082. $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){
  1083. if($team_id>0) $query->where('team_id', $team_id);
  1084. if($admin_id>0) $query->where('admin_id', $admin_id);
  1085. })->first();
  1086. // 1.当日新粉成单数
  1087. $v['new_order_count'] = $new_order->order_count;
  1088. // 2.当日新粉成交额
  1089. $v['new_order_amount'] = $new_order->order_amount;
  1090. #当日老粉成单
  1091. $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){
  1092. if($team_id>0) $query->where('team_id', $team_id);
  1093. if($admin_id>0) $query->where('admin_id', $admin_id);
  1094. })->first();
  1095. // 1.当日老粉成单数
  1096. $v['old_order_count'] = $old_order->order_count;
  1097. // 2.当日老粉成交额
  1098. $v['old_order_amount'] = $old_order->order_amount;
  1099. #复购单数、复购成交额
  1100. $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){
  1101. if($team_id>0) $query->where('team_id', $team_id);
  1102. if($admin_id>0) $query->where('admin_id', $admin_id);
  1103. })->first();
  1104. $v['fugou_order_count'] = $fugou->order_count;
  1105. $v['fugou_order_amount'] = $fugou->order_amount;
  1106. #当日加粉订单总计:
  1107. $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){
  1108. if($team_id>0) $query->where('team_id', $team_id);
  1109. if($admin_id>0) $query->where('admin_id', $admin_id);
  1110. })->first();
  1111. //总成交额
  1112. $v['order_amount'] = $order->order_amount;
  1113. //综合成单率 综合成单率=订单数/微信好友数
  1114. $v['order_rate'] = $v['wx_fan_add']>0 ? round($order->order_count / $v['wx_fan_add'], 4) * 100 .'%' : '';
  1115. //新粉成单率
  1116. $v['new_order_rate'] = $v['wx_fan_add']>0 ? round($v['new_order_count'] / $v['wx_fan_add'], 4) * 100 .'%' : '';
  1117. //复购率
  1118. $fugou_order_count_no = $order->order_count - $fugou->order_count;
  1119. $v['fugou_rate'] = $fugou_order_count_no>0 ? round($fugou->order_count / $fugou_order_count_no, 4) * 100 .'%' : '';
  1120. $data_str .= "<tr>";
  1121. $data_str .= "<td>".$v['dtime']."</td>";
  1122. $data_str .= "<td>".$v['wx_fan_add']."</td>";
  1123. $data_str .= "<td>".$v['new_order_count']."</td>";
  1124. $data_str .= "<td>".$v['new_order_amount']."</td>";
  1125. $data_str .= "<td>".$v['old_order_count']."</td>";
  1126. $data_str .= "<td>".$v['old_order_amount']."</td>";
  1127. $data_str .= "<td>".$v['fugou_order_count']."</td>";
  1128. $data_str .= "<td>".$v['fugou_order_amount']."</td>";
  1129. $data_str .= "<td>".$v['order_amount']."</td>";
  1130. $data_str .= "<td>".$v['new_order_rate']."</td>";
  1131. $data_str .= "<td>".$v['fugou_rate']."</td>";
  1132. $data_str .= "<td>".$v['order_rate']."</td>";
  1133. $data_str .= "</tr>";
  1134. }
  1135. $data_str .= "</table>";
  1136. echo $data_str;
  1137. exit;
  1138. }
  1139. /**
  1140. * 成单率梯形图
  1141. */
  1142. public function orderRateList(Request $request){
  1143. $stime = $request->input('stime');
  1144. $etime = $request->input('etime');
  1145. $self_role = session('role_name');
  1146. $team_id = $request->input('team_id');
  1147. #只能看自己团队的
  1148. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  1149. $self_id = session('admin_id');
  1150. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  1151. }
  1152. if($team_id>0){
  1153. $result = $this->orderTeamRateList($stime, $etime, $team_id);
  1154. return view('statistics/orderRateList', $result);
  1155. }
  1156. $_day = 30;
  1157. if($stime == '' && $etime == ''){
  1158. $stime = date("Y-m-d", strtotime('-32 day'));
  1159. $etime = date("Y-m-d", strtotime('-2 day'));
  1160. }else{
  1161. if($stime){
  1162. $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
  1163. }
  1164. }
  1165. #统计天数
  1166. $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
  1167. $day_count = DB::table('cust_day_remain')->where('month_time', $month)->where(function($query) use($stime, $etime){
  1168. if($stime) $query->where('idate','>=', $stime);
  1169. if($etime) $query->where('idate','<=', $etime);
  1170. })->count();
  1171. $result = DB::table('cust_day_remain')->where(function($query) use($stime, $etime){
  1172. if($stime) $query->where('idate','>=', $stime);
  1173. if($etime) $query->where('idate','<=', $etime);
  1174. })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
  1175. if($stime)
  1176. #获取列
  1177. $columns = array();
  1178. $i_max = 31;
  1179. for($i=0; $i<$i_max; $i++){
  1180. $columns[] = $i;
  1181. }
  1182. if($_day >= 45){
  1183. $columns[] = 45;
  1184. }
  1185. if($_day >= 60){
  1186. $columns[] = 60;
  1187. }
  1188. if($_day >= 75){
  1189. $columns[] = 75;
  1190. }
  1191. if($_day >= 90){
  1192. $columns[] = 90;
  1193. }
  1194. #数据整合
  1195. $data = array();
  1196. foreach($result as $k=>$v){
  1197. $key = $v->idate;
  1198. $rate_data = json_decode($v->order_rate_data, true);
  1199. $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
  1200. }
  1201. #数据格式化
  1202. $new_data = array();
  1203. foreach($data as $k=>$v){
  1204. //当日加粉数
  1205. $fan_add = DB::table('cust_day_detail')->where('dtime', $k)->where('is_del',0)->sum('fan_add');
  1206. $new_data[$k]['fan_add'] = $fan_add;
  1207. foreach($columns as $val){
  1208. $n = $val;
  1209. $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
  1210. }
  1211. }
  1212. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  1213. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  1214. })->get();
  1215. $teamList = json_decode(json_encode($teamList), true);
  1216. //echo '<pre>';
  1217. //print_r($new_data);print_r($columns);exit;
  1218. return view('statistics/orderRateList', ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList]);
  1219. }
  1220. /**
  1221. * 团队成单率梯形图
  1222. */
  1223. public function orderTeamRateList($stime, $etime, $team_id){
  1224. $_day = 30;
  1225. if($stime == '' && $etime == ''){
  1226. $stime = date("Y-m-d", strtotime('-32 day'));
  1227. $etime = date("Y-m-d", strtotime('-2 day'));
  1228. }else{
  1229. if($stime){
  1230. $_day = floor( (time() - strtotime($stime)) / 86400) - 2; //距昨天天数
  1231. }
  1232. }
  1233. #统计天数
  1234. $month = date('d')<=2 ? date('Y-m', strtotime('-1 month')) : date('Y-m'); //当前月
  1235. $day_count = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where('month_time', $month)->where(function($query) use($stime, $etime){
  1236. if($stime) $query->where('idate','>=', $stime);
  1237. if($etime) $query->where('idate','<=', $etime);
  1238. })->count();
  1239. $result = DB::table('team_cust_day_remain')->where('team_id', $team_id)->where(function($query) use($stime, $etime){
  1240. if($stime) $query->where('idate','>=', $stime);
  1241. if($etime) $query->where('idate','<=', $etime);
  1242. })->orderBy('idate', 'asc')->orderBy('month_time', 'asc')->get();
  1243. if($stime)
  1244. #获取列
  1245. $columns = array();
  1246. $i_max = 31;
  1247. for($i=0; $i<$i_max; $i++){
  1248. $columns[] = $i;
  1249. }
  1250. if($_day >= 45){
  1251. $columns[] = 45;
  1252. }
  1253. if($_day >= 60){
  1254. $columns[] = 60;
  1255. }
  1256. if($_day >= 75){
  1257. $columns[] = 75;
  1258. }
  1259. if($_day >= 90){
  1260. $columns[] = 90;
  1261. }
  1262. #数据整合
  1263. $data = array();
  1264. foreach($result as $k=>$v){
  1265. $key = $v->idate;
  1266. $rate_data = json_decode($v->order_rate_data, true);
  1267. $data[$key] = isset($data[$key]) ? array_merge($data[$key], $rate_data) : $rate_data;
  1268. }
  1269. #团队成员:
  1270. $saler_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  1271. #数据格式化
  1272. $new_data = array();
  1273. foreach($data as $k=>$v){
  1274. //当日加粉数
  1275. $fan_add = DB::table('cust_day_detail')->whereIn('admin_id', $saler_ids)->where('dtime', $k)->where('is_del',0)->sum('fan_add');
  1276. $new_data[$k]['fan_add'] = $fan_add;
  1277. foreach($columns as $val){
  1278. $n = $val;
  1279. $new_data[$k]['rate'][] = isset($v[$n]['rate']) ? $v[$n]['rate'].'%' : '';
  1280. }
  1281. }
  1282. //echo '<pre>';
  1283. //print_r($new_data);print_r($columns);exit;
  1284. $self_role = session('role_name');
  1285. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  1286. if($self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  1287. })->get();
  1288. $teamList = json_decode(json_encode($teamList), true);
  1289. return ['result' =>$new_data, 'columns'=>$columns, 'stime'=>$stime, 'etime'=>$etime, 'team_id'=>$team_id, 'teamlist'=>$teamList];
  1290. }
  1291. /**
  1292. * orderSaleRank 实时销售数据展榜
  1293. */
  1294. public function orderSaleRank(Request $request){
  1295. header('Location:https://datav.aliyuncs.com/share/c936e72d66c7b5900238cf51cbd31884');
  1296. exit;
  1297. }
  1298. /**
  1299. * orderDistrict 实时订单地域分布
  1300. */
  1301. public function orderDistrict(Request $request){
  1302. header('Location:https://datav.aliyuncs.com/share/423b408234ea48d49a2a4ee7f33fa4e4');
  1303. exit;
  1304. }
  1305. /**
  1306. * 地区roi
  1307. */
  1308. public function districtRoi(Request $request){
  1309. $page = (int)$request->input('page');
  1310. $pageSize = 20;
  1311. if($page<=0){
  1312. $page = 1;
  1313. }
  1314. $offset = ($page-1) * $pageSize;
  1315. $stime = $request->input('stime');
  1316. $etime = $request->input('etime');
  1317. $city = $request->input('city');
  1318. if($city !== null){
  1319. $city = str_replace('市', '', $city);
  1320. }
  1321. $ret = AdCost::select('id')->where(function($query) use($stime, $etime, $city){
  1322. if($stime) $query->where('ad_time', '>=', $stime);
  1323. if($etime) $query->where('ad_time', '<=', $etime);
  1324. if($city) $query->where('city', 'like', '%'.$city.'%');
  1325. })->groupBy('ad_time')->groupBy('city')->get();
  1326. $ret = json_decode(json_encode($ret), true);
  1327. $count = count($ret);
  1328. if ($count > 1) {
  1329. // 总页数
  1330. $pages = ceil($count/$pageSize);
  1331. }else{
  1332. // 总页数
  1333. $pages = 1;
  1334. }
  1335. $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city, ad_time'))->where(function($query) use($stime, $etime, $city){
  1336. if($stime) $query->where('ad_time', '>=', $stime);
  1337. if($etime) $query->where('ad_time', '<=', $etime);
  1338. if($city) $query->where('city', 'like', '%'.$city.'%');
  1339. })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->offset($offset)->limit($pageSize)->get();
  1340. $result = json_decode(json_encode($result), true);
  1341. foreach($result as $k=>&$v){
  1342. //新粉收入
  1343. //当日加粉
  1344. $city_name = str_replace('市', '', $v['city']);
  1345. $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  1346. #当日加粉订单总计:
  1347. $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();
  1348. #当日新粉成单:
  1349. $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();
  1350. // 1.当日新粉成单数
  1351. $v['new_order_count'] = $new_order->order_count;
  1352. // 2.当日新粉成交额
  1353. $v['new_order_amount'] = $new_order->order_amount;
  1354. // 3.当日粉丝总成交额
  1355. $v['order_amount'] = $order->order_amount;
  1356. // 总单数
  1357. $v['order_count'] = $order->order_count;
  1358. //新粉roi
  1359. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1360. //累计roi
  1361. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1362. $v['new_fan_count'] = count($phones);
  1363. }
  1364. //总计
  1365. $total_data = [
  1366. 'cost' => 0,
  1367. 'fan_count' => 0,
  1368. 'order_count' => 0,
  1369. 'order_amount' => 0,
  1370. 'roi' => 0,
  1371. 'conversion_times' => 0,
  1372. 'new_order_count' => 0,
  1373. 'new_order_amount' => 0,
  1374. 'new_roi' => 0,
  1375. ];
  1376. //投入
  1377. $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
  1378. if($stime) $query->where('ad_time', '>=', $stime);
  1379. if($etime) $query->where('ad_time', '<=', $etime);
  1380. if($city) $query->where('city', 'like', '%'.$city.'%');
  1381. })->first();
  1382. $total_data['cost'] = $adcost->total_cost;
  1383. $total_data['conversion_times'] = $adcost->conversion_times;
  1384. //总新粉
  1385. $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
  1386. if($stime) $query->where('fanTime', '>=', $stime);
  1387. if($etime) $query->where('fanTime', '<=', $etime);
  1388. if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
  1389. })->lists('phone');
  1390. $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
  1391. //总收入
  1392. $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();
  1393. $total_data['order_count'] = $order->order_count;
  1394. $total_data['order_amount'] = $order->order_amount;
  1395. $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1396. #总新粉成单:
  1397. $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
  1398. $total_data['new_order_count'] = $new_order->order_count;
  1399. $total_data['new_order_amount'] = $new_order->order_amount;
  1400. $total_data['new_roi'] = $total_data['cost']>0 ? round($total_data['new_order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1401. return view('statistics/districtRoi', ['result' =>$result,
  1402. 'page' =>$page,
  1403. 'count' =>$count,
  1404. 'pages' =>$pages,
  1405. 'stime' =>$stime,
  1406. 'etime' =>$etime,
  1407. 'city' =>$city,
  1408. 'total_data' =>$total_data,
  1409. ]);
  1410. }
  1411. /**
  1412. * 导出地域roi报表
  1413. */
  1414. public function districtRoi_export(Request $request){
  1415. $stime = $request->input('stime');
  1416. $etime = $request->input('etime');
  1417. $city = $request->input('city');
  1418. if($city !== null){
  1419. $city = str_replace('市', '', $city);
  1420. }
  1421. $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city, ad_time'))->where(function($query) use($stime, $etime, $city){
  1422. if($stime) $query->where('ad_time', '>=', $stime);
  1423. if($etime) $query->where('ad_time', '<=', $etime);
  1424. if($city) $query->where('city', 'like', '%'.$city.'%');
  1425. })->groupBy('ad_time')->groupBy('city')->orderBy('ad_time', 'desc')->get();
  1426. $result = json_decode(json_encode($result), true);
  1427. foreach($result as $k=>&$v){
  1428. //新粉收入
  1429. //当日加粉
  1430. $city_name = str_replace('市', '', $v['city']);
  1431. $phones = DB::table('customers')->where('fanTime', $v['ad_time'])->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  1432. #当日加粉订单总计:
  1433. $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();
  1434. #当日新粉成单:
  1435. $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();
  1436. // 1.当日新粉成单数
  1437. $v['new_order_count'] = $new_order->order_count;
  1438. // 2.当日新粉成交额
  1439. $v['new_order_amount'] = $new_order->order_amount;
  1440. // 3.当日粉丝总成交额
  1441. $v['order_amount'] = $order->order_amount;
  1442. // 总单数
  1443. $v['order_count'] = $order->order_count;
  1444. //新粉roi
  1445. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1446. //累计roi
  1447. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1448. $v['new_fan_count'] = count($phones);
  1449. }
  1450. //总计
  1451. $total_data = [
  1452. 'cost' => 0,
  1453. 'fan_count' => 0,
  1454. 'order_count' => 0,
  1455. 'order_amount' => 0,
  1456. 'roi' => 0,
  1457. 'conversion_times' => 0,
  1458. 'new_order_count' => 0,
  1459. 'new_order_amount' => 0,
  1460. 'new_roi' => 0,
  1461. ];
  1462. //投入
  1463. $adcost = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times'))->where(function($query) use($stime, $etime, $city){
  1464. if($stime) $query->where('ad_time', '>=', $stime);
  1465. if($etime) $query->where('ad_time', '<=', $etime);
  1466. if($city) $query->where('city', 'like', '%'.$city.'%');
  1467. })->first();
  1468. $total_data['cost'] = $adcost->total_cost;
  1469. $total_data['conversion_times'] = $adcost->conversion_times;
  1470. //总新粉
  1471. $phones = DB::table('customers')->where('fanTime','>=', '2019-09-01')->where(function($query) use($stime, $etime, $city){
  1472. if($stime) $query->where('fanTime', '>=', $stime);
  1473. if($etime) $query->where('fanTime', '<=', $etime);
  1474. if($city) $query->where('receiverCity', 'like', '%'.$city.'%');
  1475. })->lists('phone');
  1476. $total_data['fan_count'] = !empty($phones) ? count($phones) : 0;
  1477. //总收入
  1478. $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();
  1479. $total_data['order_count'] = $order->order_count;
  1480. $total_data['order_amount'] = $order->order_amount;
  1481. $total_data['roi'] = $total_data['cost']>0 ? round($total_data['order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1482. #总新粉成单:
  1483. $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
  1484. $total_data['new_order_count'] = $new_order->order_count;
  1485. $total_data['new_order_amount'] = $new_order->order_amount;
  1486. $total_data['new_roi'] = $total_data['cost']>0 ? round($total_data['new_order_amount'] / $total_data['cost'], 4) * 100 .'%' : '';
  1487. $indexKey = ['ad_time','city','total_cost','conversion_times','new_fan_count','new_order_count','new_order_amount','new_roi','order_count','order_amount','total_roi'];
  1488. $title = ['日期', '城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  1489. $filename = 'district_roi_'.date('Y-m-d_H').'.xlsx';
  1490. return Order::export_excel($result, $filename, $indexKey, $title);
  1491. }
  1492. /**
  1493. * 团队订单财务明细
  1494. */
  1495. public function teamFinanceList(Request $request){
  1496. $team_id = (int)$request->input('team_id');
  1497. $stime = $request->input('stime');
  1498. $etime = $request->input('etime');
  1499. $aftersale = (int)$request->input('aftersale'); //售后
  1500. $page = (int)$request->input('page');
  1501. $pageSize = 20;
  1502. if($page<=0){
  1503. $page = 1;
  1504. }
  1505. $offset = ($page-1) * $pageSize;
  1506. $count = Order::where(function($query) use($team_id, $stime, $etime, $aftersale){
  1507. if($team_id) $query->where('team_id', $team_id);
  1508. if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
  1509. if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
  1510. if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
  1511. if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  1512. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  1513. })->where('is_del', 0)->count();
  1514. if ($count > 1) {
  1515. // 总页数
  1516. $pages = ceil($count/$pageSize);
  1517. }else{
  1518. // 总页数
  1519. $pages = 1;
  1520. }
  1521. $teams = DB::table('teams')->lists('name', 'id');
  1522. $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time')->where(function($query) use($team_id, $stime, $etime, $aftersale){
  1523. if($team_id) $query->where('team_id', $team_id);
  1524. if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
  1525. if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
  1526. if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
  1527. if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  1528. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  1529. })->where('is_del', 0)->orderBy('team_id', 'asc');
  1530. if($aftersale == 1){
  1531. $result = $resultQuery->orderBy('aftersale_time','desc')->offset($offset)->limit($pageSize)->get();
  1532. }else{
  1533. $result = $resultQuery->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
  1534. }
  1535. $result = json_decode(json_encode($result), true);
  1536. $payArr = [1=>'微信支付', 2=>'付款码支付'];
  1537. foreach($result as $k=>&$v){
  1538. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  1539. $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
  1540. $v['payment_type'] = $payArr[$v['payment_type']];
  1541. $v['ctime'] = $aftersale == 1 ? $v['aftersale_time'] : $v['createTime'];
  1542. }
  1543. $teamList = DB::table('teams')->select('id', 'name')->get();
  1544. $teamList = json_decode(json_encode($teamList), true);
  1545. return view('/statistics/teamFinanceList',[
  1546. 'result' => $result,
  1547. 'team_id' => $team_id,
  1548. 'stime' => $stime,
  1549. 'etime' => $etime,
  1550. 'page' => $page,
  1551. 'count' => $count,
  1552. 'pages' => $pages,
  1553. 'teamlist' => $teamList,
  1554. 'aftersale' => $aftersale,
  1555. ]);
  1556. }
  1557. /**
  1558. * 团队订单财务汇总列表
  1559. */
  1560. public function teamFinanceTotal(Request $request){
  1561. $team_id = (int)$request->input('team_id');
  1562. $stime = $request->input('stime');
  1563. $etime = $request->input('etime');
  1564. $aftersale = (int)$request->input('aftersale');
  1565. $page = (int)$request->input('page');
  1566. $pageSize = 20;
  1567. if($page<=0){
  1568. $page = 1;
  1569. }
  1570. $offset = ($page-1) * $pageSize;
  1571. if($aftersale == 1){
  1572. $countQuery = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id'));
  1573. }else{
  1574. $countQuery = Order::select(DB::raw('left(createTime, 10) as ctime, team_id'));
  1575. }
  1576. $count = $countQuery->where(function($query) use($team_id, $stime, $etime, $aftersale){
  1577. if($team_id) $query->where('team_id', $team_id);
  1578. if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
  1579. if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
  1580. if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
  1581. if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  1582. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  1583. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->get();
  1584. $count = count($count);
  1585. if ($count > 1) {
  1586. // 总页数
  1587. $pages = ceil($count/$pageSize);
  1588. }else{
  1589. // 总页数
  1590. $pages = 1;
  1591. }
  1592. $teams = DB::table('teams')->lists('name', 'id');
  1593. if($aftersale == 1){
  1594. $result = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
  1595. if($team_id) $query->where('team_id', $team_id);
  1596. if($stime) $query->where('aftersale_time', '>=', $stime);
  1597. if($etime) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  1598. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  1599. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('aftersale_time','desc')->offset($offset)->limit($pageSize)->get();
  1600. }else{
  1601. $result = Order::select(DB::raw('left(createTime, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
  1602. if($team_id) $query->where('team_id', $team_id);
  1603. if($stime) $query->where('createTime', '>=', $stime);
  1604. if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
  1605. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  1606. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->offset($offset)->limit($pageSize)->get();
  1607. }
  1608. $result = json_decode(json_encode($result), true);
  1609. foreach($result as $k=>&$v){
  1610. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
  1611. $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
  1612. }
  1613. $teamList = DB::table('teams')->select('id', 'name')->get();
  1614. $teamList = json_decode(json_encode($teamList), true);
  1615. return view('/statistics/teamFinanceTotal',[
  1616. 'result' => $result,
  1617. 'team_id' => $team_id,
  1618. 'stime' => $stime,
  1619. 'etime' => $etime,
  1620. 'page' => $page,
  1621. 'count' => $count,
  1622. 'pages' => $pages,
  1623. 'teamlist' => $teamList,
  1624. 'aftersale' => $aftersale,
  1625. ]);
  1626. }
  1627. /**
  1628. * 团队财务明细导出
  1629. */
  1630. public function teamFinanceList_export(Request $request){
  1631. $team_id = (int)$request->input('team_id');
  1632. $stime = $request->input('stime');
  1633. $etime = $request->input('etime');
  1634. $aftersale = $request->input('aftersale');
  1635. $teams = DB::table('teams')->lists('name', 'id');
  1636. $resultQuery = Order::select('team_id', 'createTime', 'id', 'receivedAmount', 'aftersale_fee', 'refund_price', 'cost', 'freight_cost', 'payment_type', 'aftersale_time')->where(function($query) use($team_id, $stime, $etime, $aftersale){
  1637. if($team_id) $query->where('team_id', $team_id);
  1638. if($stime && $aftersale == 0) $query->where('createTime', '>=', $stime);
  1639. if($etime && $aftersale == 0) $query->where('createTime', '<=', $etime. ' 23:59:59');
  1640. if($stime && $aftersale == 1) $query->where('aftersale_time', '>=', $stime);
  1641. if($etime && $aftersale == 1) $query->where('aftersale_time', '<=', $etime. ' 23:59:59');
  1642. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  1643. })->where('is_del', 0)->orderBy('team_id', 'asc');
  1644. if($aftersale == 1){
  1645. $result = $resultQuery->orderBy('aftersale_time','desc')->get();
  1646. }else{
  1647. $result = $resultQuery->orderBy('createTime','desc')->get();
  1648. }
  1649. $result = json_decode(json_encode($result), true);
  1650. $payArr = [1=>'微信支付', 2=>'付款码支付'];
  1651. foreach($result as $k=>&$v){
  1652. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';;
  1653. $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
  1654. $v['payment_type'] = $payArr[$v['payment_type']];
  1655. }
  1656. if($aftersale == 0) $indexKey = ['team_name','createTime','id','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost', 'payment_type'];
  1657. if($aftersale == 1) $indexKey = ['team_name','aftersale_time','id','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost', 'payment_type'];
  1658. $title = ['团队', '时间', '订单编号', '成交金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本', '支付方式'];
  1659. $filename = 'teamFinanceList_'.date('Y-m-d_H').'.xlsx';
  1660. return Order::export_excel($result, $filename, $indexKey, $title);
  1661. }
  1662. /**
  1663. * 团队财务汇总导出
  1664. */
  1665. public function teamFinanceTotal_export(Request $request){
  1666. $team_id = (int)$request->input('team_id');
  1667. $stime = $request->input('stime');
  1668. $etime = $request->input('etime');
  1669. $aftersale = $request->input('aftersale');
  1670. $teams = DB::table('teams')->lists('name', 'id');
  1671. if($aftersale == 1){
  1672. $result = Order::select(DB::raw('left(aftersale_time, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
  1673. if($team_id) $query->where('team_id', $team_id);
  1674. if($stime) $query->where('aftersale_time', '>=', $stime);
  1675. if($etime) $query->where('aftersale_time', '<=', $etime.' 23:59:59');
  1676. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  1677. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('aftersale_time','desc')->get();
  1678. }else{
  1679. $result = Order::select(DB::raw('left(createTime, 10) as ctime, team_id, count(1) as order_count, sum(receivedAmount) as receivedAmount, sum(aftersale_fee) as aftersale_fee, sum(refund_price) as refund_price, sum(cost) as cost, sum(freight_cost) as freight_cost'))->where(function($query) use($team_id, $stime, $etime, $aftersale){
  1680. if($team_id) $query->where('team_id', $team_id);
  1681. if($stime) $query->where('createTime', '>=', $stime);
  1682. if($etime) $query->where('createTime', '<=', $etime.' 23:59:59');
  1683. if($aftersale == 1) $query->where('aftersale_fee', '>', 0);
  1684. })->where('is_del', 0)->groupBy('team_id')->groupBy('ctime')->orderBy('team_id', 'asc')->orderBy('createTime','desc')->get();
  1685. }
  1686. $result = json_decode(json_encode($result), true);
  1687. foreach($result as $k=>&$v){
  1688. $v['team_name'] = $v['team_id']>0 ? $teams[$v['team_id']] : '';
  1689. $v['true_amount'] = $v['receivedAmount'] - $v['refund_price'] - $v['aftersale_fee'];
  1690. }
  1691. $indexKey = ['team_name','ctime','order_count','receivedAmount','aftersale_fee','refund_price','true_amount','cost','freight_cost'];
  1692. $title = ['团队', '时间', '总单数', '成交金额', '售后', '退补差价', '实际金额', '供应商成本', '物流成本'];
  1693. $filename = 'teamFinanceTotal_'.date('Y-m-d_H').'.xlsx';
  1694. return Order::export_excel($result, $filename, $indexKey, $title);
  1695. }
  1696. /**
  1697. * 地区汇总roi
  1698. */
  1699. public function districtTotalRoi(Request $request){
  1700. $page = (int)$request->input('page');
  1701. $pageSize = 20;
  1702. if($page<=0){
  1703. $page = 1;
  1704. }
  1705. $offset = ($page-1) * $pageSize;
  1706. $city = $request->input('city');
  1707. if($city !== null){
  1708. $city = str_replace('市', '', $city);
  1709. }
  1710. $ret = AdCost::select('id')->where(function($query) use($city){
  1711. if($city) $query->where('city', 'like', '%'.$city.'%');
  1712. })->groupBy('city')->get();
  1713. $ret = json_decode(json_encode($ret), true);
  1714. $count = count($ret);
  1715. if ($count > 1) {
  1716. // 总页数
  1717. $pages = ceil($count/$pageSize);
  1718. }else{
  1719. // 总页数
  1720. $pages = 1;
  1721. }
  1722. $result = AdCost::select(DB::raw('sum(cost) as total_cost, sum(conversion_times) as conversion_times, city'))->where(function($query) use($city){
  1723. if($city) $query->where('city', 'like', '%'.$city.'%');
  1724. })->groupBy('city')->offset($offset)->limit($pageSize)->get();
  1725. $result = json_decode(json_encode($result), true);
  1726. foreach($result as $k=>&$v){
  1727. //新粉收入
  1728. //加粉
  1729. $city_name = str_replace('市', '', $v['city']);
  1730. $phones = DB::table('customers')->where('receiverCity','like', '%'.$city_name.'%')->lists('phone');
  1731. #订单总计:
  1732. $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();
  1733. #汇总新粉成单:
  1734. $new_order = DB::table('order')->select(DB::raw('sum(receivedAmount) as order_amount, count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->whereIn('receiverMobile', $phones)->first();
  1735. // 1.新粉成单数
  1736. $v['new_order_count'] = $new_order->order_count;
  1737. // 2.新粉成交额
  1738. $v['new_order_amount'] = $new_order->order_amount;
  1739. // 3.粉丝总成交额
  1740. $v['order_amount'] = $order->order_amount;
  1741. // 总单数
  1742. $v['order_count'] = $order->order_count;
  1743. //新粉roi
  1744. $v['new_roi'] = $v['total_cost']>0 ? round($v['new_order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1745. //累计roi
  1746. $v['total_roi'] = $v['total_cost']>0 ? round($v['order_amount'] / $v['total_cost'], 4) * 100 .'%' : '';
  1747. $v['new_fan_count'] = count($phones);
  1748. }
  1749. return view('statistics/districtTotalRoi', ['result' =>$result,
  1750. 'page' =>$page,
  1751. 'count' =>$count,
  1752. 'pages' =>$pages,
  1753. 'city' =>$city,
  1754. ]);
  1755. }
  1756. /**
  1757. * 地域roi7日
  1758. */
  1759. public function districtRoi7day(Request $request){
  1760. $page = (int)$request->input('page');
  1761. $pageSize = 20;
  1762. if($page<=0){
  1763. $page = 1;
  1764. }
  1765. $offset = ($page-1) * $pageSize;
  1766. $stime = $request->input('stime');
  1767. $etime = $request->input('etime');
  1768. $city = $request->input('city');
  1769. if($city !== null){
  1770. $city = str_replace('市', '', $city);
  1771. }
  1772. $count = DistrictRoi7::where(function($query) use($stime, $etime, $city){
  1773. if($stime) $query->where('ad_time', '>=', $stime);
  1774. if($etime) $query->where('ad_time', '<=', $etime);
  1775. if($city) $query->where('city', 'like', '%'.$city.'%');
  1776. })->count();
  1777. if ($count > 1) {
  1778. // 总页数
  1779. $pages = ceil($count/$pageSize);
  1780. }else{
  1781. // 总页数
  1782. $pages = 1;
  1783. }
  1784. $result = DistrictRoi7::where(function($query) use($stime, $etime, $city){
  1785. if($stime) $query->where('ad_time', '>=', $stime);
  1786. if($etime) $query->where('ad_time', '<=', $etime);
  1787. if($city) $query->where('city', 'like', '%'.$city.'%');
  1788. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  1789. $result = json_decode(json_encode($result), true);
  1790. foreach($result as $k=>&$v){
  1791. //新粉roi
  1792. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1793. //累计roi
  1794. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1795. }
  1796. return view('statistics/districtRoi7day', ['result' =>$result,
  1797. 'page' =>$page,
  1798. 'count' =>$count,
  1799. 'pages' =>$pages,
  1800. 'city' =>$city,
  1801. 'stime' =>$stime,
  1802. 'etime' =>$etime,
  1803. ]);
  1804. }
  1805. /**
  1806. * 地域roi7日
  1807. */
  1808. public function districtRoi7day_export(Request $request){
  1809. $stime = $request->input('stime');
  1810. $etime = $request->input('etime');
  1811. $city = $request->input('city');
  1812. if($city !== null){
  1813. $city = str_replace('市', '', $city);
  1814. }
  1815. $result = DistrictRoi7::where(function($query) use($stime, $etime, $city){
  1816. if($stime) $query->where('ad_time', '>=', $stime);
  1817. if($etime) $query->where('ad_time', '<=', $etime);
  1818. if($city) $query->where('city', 'like', '%'.$city.'%');
  1819. })->orderBy('id', 'desc')->get();
  1820. $result = json_decode(json_encode($result), true);
  1821. foreach($result as $k=>&$v){
  1822. //新粉roi
  1823. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1824. //累计roi
  1825. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1826. }
  1827. $indexKey = ['ad_time','city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
  1828. $title = ['日期', '城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '7日累计成单数', '7日累计收入', '7日累计ROI'];
  1829. $filename = 'districtRoi7day_'.date('Y-m-d_H').'.xlsx';
  1830. return Order::export_excel($result, $filename, $indexKey, $title);
  1831. }
  1832. /**
  1833. * 地域roi7日
  1834. */
  1835. public function districtRoi7dayTotal(Request $request){
  1836. $page = (int)$request->input('page');
  1837. $pageSize = 20;
  1838. if($page<=0){
  1839. $page = 1;
  1840. }
  1841. $offset = ($page-1) * $pageSize;
  1842. $city = $request->input('city');
  1843. if($city !== null){
  1844. $city = str_replace('市', '', $city);
  1845. }
  1846. $count = DistrictRoi7::where(function($query) use($city){
  1847. if($city) $query->where('city', 'like', '%'.$city.'%');
  1848. })->groupBy('city')->get();
  1849. $count = count($count);
  1850. if ($count > 1) {
  1851. // 总页数
  1852. $pages = ceil($count/$pageSize);
  1853. }else{
  1854. // 总页数
  1855. $pages = 1;
  1856. }
  1857. $result = DistrictRoi7::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
  1858. if($city) $query->where('city', 'like', '%'.$city.'%');
  1859. })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
  1860. $result = json_decode(json_encode($result), true);
  1861. foreach($result as $k=>&$v){
  1862. //新粉roi
  1863. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1864. //累计roi
  1865. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1866. //数据格式化
  1867. $v['order_amount'] = round($v['sum_order_amount'],2);
  1868. }
  1869. return view('statistics/districtRoi7dayTotal', ['result' =>$result,
  1870. 'page' =>$page,
  1871. 'count' =>$count,
  1872. 'pages' =>$pages,
  1873. 'city' =>$city,
  1874. ]);
  1875. }
  1876. /**
  1877. * 地域roi7日
  1878. */
  1879. public function districtRoi7dayTotal_export(Request $request){
  1880. $city = $request->input('city');
  1881. if($city !== null){
  1882. $city = str_replace('市', '', $city);
  1883. }
  1884. $result = DistrictRoi7::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount,sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
  1885. if($city) $query->where('city', 'like', '%'.$city.'%');
  1886. })->groupBy('city')->orderBy('roi', 'desc')->get();
  1887. $result = json_decode(json_encode($result), true);
  1888. foreach($result as $k=>&$v){
  1889. //新粉roi
  1890. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1891. //累计roi
  1892. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1893. //数据格式化
  1894. $v['order_amount'] = round($v['sum_order_amount'],2);
  1895. }
  1896. $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
  1897. $title = ['城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '7日累计成单数', '7日累计收入', '7日累计ROI'];
  1898. $filename = 'districtRoi7day_'.date('Y-m-d_H').'.xlsx';
  1899. return Order::export_excel($result, $filename, $indexKey, $title);
  1900. }
  1901. /**
  1902. * 地域roi30日
  1903. */
  1904. public function districtRoi30day(Request $request){
  1905. $page = (int)$request->input('page');
  1906. $pageSize = 20;
  1907. if($page<=0){
  1908. $page = 1;
  1909. }
  1910. $offset = ($page-1) * $pageSize;
  1911. $stime = $request->input('stime');
  1912. $etime = $request->input('etime');
  1913. $city = $request->input('city');
  1914. if($city !== null){
  1915. $city = str_replace('市', '', $city);
  1916. }
  1917. $count = districtRoi30::where(function($query) use($stime, $etime, $city){
  1918. if($stime) $query->where('ad_time', '>=', $stime);
  1919. if($etime) $query->where('ad_time', '<=', $etime);
  1920. if($city) $query->where('city', 'like', '%'.$city.'%');
  1921. })->count();
  1922. if ($count > 1) {
  1923. // 总页数
  1924. $pages = ceil($count/$pageSize);
  1925. }else{
  1926. // 总页数
  1927. $pages = 1;
  1928. }
  1929. $result = districtRoi30::where(function($query) use($stime, $etime, $city){
  1930. if($stime) $query->where('ad_time', '>=', $stime);
  1931. if($etime) $query->where('ad_time', '<=', $etime);
  1932. if($city) $query->where('city', 'like', '%'.$city.'%');
  1933. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  1934. $result = json_decode(json_encode($result), true);
  1935. foreach($result as $k=>&$v){
  1936. //新粉roi
  1937. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1938. //累计roi
  1939. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1940. }
  1941. return view('statistics/districtRoi30day', ['result' =>$result,
  1942. 'page' =>$page,
  1943. 'count' =>$count,
  1944. 'pages' =>$pages,
  1945. 'city' =>$city,
  1946. 'stime' =>$stime,
  1947. 'etime' =>$etime,
  1948. ]);
  1949. }
  1950. /**
  1951. * 地域roi30日
  1952. */
  1953. public function districtRoi30day_export(Request $request){
  1954. $stime = $request->input('stime');
  1955. $etime = $request->input('etime');
  1956. $city = $request->input('city');
  1957. if($city !== null){
  1958. $city = str_replace('市', '', $city);
  1959. }
  1960. $result = districtRoi30::where(function($query) use($stime, $etime, $city){
  1961. if($stime) $query->where('ad_time', '>=', $stime);
  1962. if($etime) $query->where('ad_time', '<=', $etime);
  1963. if($city) $query->where('city', 'like', '%'.$city.'%');
  1964. })->orderBy('id', 'desc')->get();
  1965. $result = json_decode(json_encode($result), true);
  1966. foreach($result as $k=>&$v){
  1967. //新粉roi
  1968. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1969. //累计roi
  1970. $v['total_roi'] = $v['cost']>0 ? round($v['order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  1971. }
  1972. $indexKey = ['ad_time','city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
  1973. $title = ['日期', '城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  1974. $filename = 'districtRoi30day_'.date('Y-m-d_H').'.xlsx';
  1975. return Order::export_excel($result, $filename, $indexKey, $title);
  1976. }
  1977. /**
  1978. * 地域roi30日汇总
  1979. */
  1980. public function districtRoi30dayTotal(Request $request){
  1981. $page = (int)$request->input('page');
  1982. $pageSize = 20;
  1983. if($page<=0){
  1984. $page = 1;
  1985. }
  1986. $offset = ($page-1) * $pageSize;
  1987. $city = $request->input('city');
  1988. if($city !== null){
  1989. $city = str_replace('市', '', $city);
  1990. }
  1991. $count = districtRoi30::where(function($query) use($city){
  1992. if($city) $query->where('city', 'like', '%'.$city.'%');
  1993. })->groupBy('city')->get();
  1994. $count = count($count);
  1995. if ($count > 1) {
  1996. // 总页数
  1997. $pages = ceil($count/$pageSize);
  1998. }else{
  1999. // 总页数
  2000. $pages = 1;
  2001. }
  2002. $result = districtRoi30::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount, sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
  2003. if($city) $query->where('city', 'like', '%'.$city.'%');
  2004. })->groupBy('city')->orderBy('roi', 'desc')->offset($offset)->limit($pageSize)->get();
  2005. $result = json_decode(json_encode($result), true);
  2006. foreach($result as $k=>&$v){
  2007. //新粉roi
  2008. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2009. //累计roi
  2010. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2011. //数据格式化
  2012. $v['order_amount'] = round($v['sum_order_amount'],2);
  2013. }
  2014. return view('statistics/districtRoi30dayTotal', ['result' =>$result,
  2015. 'page' =>$page,
  2016. 'count' =>$count,
  2017. 'pages' =>$pages,
  2018. 'city' =>$city,
  2019. ]);
  2020. }
  2021. /**
  2022. * 地域roi30日
  2023. */
  2024. public function districtRoi30dayTotal_export(Request $request){
  2025. $city = $request->input('city');
  2026. if($city !== null){
  2027. $city = str_replace('市', '', $city);
  2028. }
  2029. $result = districtRoi30::select(DB::raw('city,sum(cost) as cost,sum(gzh_count) as gzh_count,sum(fan_count) as fan_count,sum(new_fan_order_count) as new_fan_order_count,sum(new_fan_order_amount) as new_fan_order_amount,sum(order_count) as order_count,sum(order_amount) as sum_order_amount,sum(order_amount)/sum(cost) as roi'))->where(function($query) use($city){
  2030. if($city) $query->where('city', 'like', '%'.$city.'%');
  2031. })->groupBy('city')->orderBy('roi', 'desc')->get();
  2032. $result = json_decode(json_encode($result), true);
  2033. foreach($result as $k=>&$v){
  2034. //新粉roi
  2035. $v['new_roi'] = $v['cost']>0 ? round($v['new_fan_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2036. //累计roi
  2037. $v['total_roi'] = $v['cost']>0 ? round($v['sum_order_amount'] / $v['cost'], 4) * 100 .'%' : '';
  2038. //数据格式化
  2039. $v['order_amount'] = round($v['sum_order_amount'],2);
  2040. }
  2041. $indexKey = ['city','cost','gzh_count','fan_count','new_fan_order_count','new_fan_order_amount','new_roi','order_count','order_amount','total_roi'];
  2042. $title = ['城市', '投放成本', '加粉数', '新粉数量', '新粉成单数', '新粉收入', '新粉ROI', '累计成单数', '累计收入', '累计ROI'];
  2043. $filename = 'districtRoi30day_'.date('Y-m-d_H').'.xlsx';
  2044. return Order::export_excel($result, $filename, $indexKey, $title);
  2045. }
  2046. }