No Description

StatisticsController.php 124KB

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