Brak opisu

StatisticsController.php 105KB

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