Няма описание

CustReportController.php 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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\Oplog;
  15. use App\Order;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\DB;
  18. use PHPExcel_Reader_Excel2007;
  19. use PHPExcel_Reader_Excel5;
  20. use PHPExcel_Reader_CSV;
  21. class custReportController extends Controller
  22. {
  23. public function totalindex(Request $request){
  24. $page = (int)$request->input('page');
  25. $pageSize = 20;
  26. if($page<=0){
  27. $page = 1;
  28. }
  29. $offset = ($page-1) * $pageSize;
  30. $team_id = (int)$request->input('team_id');
  31. $stime = $request->input('stime');
  32. $etime = $request->input('etime');
  33. $count = CustTotal::where(function($query) use($team_id, $stime, $etime){
  34. if($team_id) $query->where('team_id', $team_id);
  35. if($stime) $query->where('dtime', '>=', $stime);
  36. if($etime) $query->where('dtime', '<=', $etime);
  37. })->where('is_del',0)->count();
  38. if ($count > 1) {
  39. // 总页数
  40. $pages = ceil($count/$pageSize);
  41. }else{
  42. // 总页数
  43. $pages = 1;
  44. }
  45. $result = CustTotal::where(function($query) use($team_id, $stime, $etime){
  46. if($team_id) $query->where('team_id', $team_id);
  47. if($stime) $query->where('dtime', '>=', $stime);
  48. if($etime) $query->where('dtime', '<=', $etime);
  49. })->where('is_del',0)->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  50. $result = json_decode(json_encode($result),true);
  51. foreach($result as $k=>&$v){
  52. if($v['team_id']>0){
  53. $team = DB::table('teams')->select('name')->where('id', $v['team_id'])->first();
  54. $v['team_name'] = isset($team->name) ? $team->name : '';
  55. }else{
  56. $v['team_name'] = '';
  57. }
  58. }
  59. $teamList = DB::table('teams')->select('id', 'name')->get();
  60. $teamList = json_decode(json_encode($teamList), true);
  61. return view('custreport/totallist', ['result' =>$result,
  62. 'page' =>$page,
  63. 'count' =>$count,
  64. 'pages' =>$pages,
  65. 'teamlist' =>$teamList,
  66. 'stime' =>$stime,
  67. 'etime' =>$etime,
  68. 'team_id' =>$team_id,
  69. ]);
  70. }
  71. /**
  72. * 添加订单
  73. * @return \Illuminate\View\View
  74. */
  75. public function totalcreate(Request $request)
  76. {
  77. $teamList = DB::table('teams')->select('id', 'name')->get();
  78. $teamList = json_decode(json_encode($teamList), true);
  79. return view('custreport/totalcreate',['teamlist'=>$teamList]);
  80. }
  81. /**
  82. * 分组管理-进行添加操作
  83. * @param Request $request
  84. * @return \Illuminate\Http\RedirectResponse
  85. */
  86. public function totalstore(Request $request)
  87. {
  88. $team_id = (int)$request->input('team_id');
  89. $this->validate($request, [
  90. 'total_cost' => 'required',
  91. 'total_fan_add' => 'required',
  92. 'team_id' => 'required',
  93. 'dtime' => 'required|unique:cust_day_total,dtime,1,is_del,team_id,'.$team_id,
  94. ], [
  95. 'total_cost.required' => '总成本不能为空',
  96. 'total_fan_add.required' => '总加粉数不能为空',
  97. 'dtime.required' => '日期不能为空',
  98. 'team_id.required' => '团队不能为空',
  99. 'dtime.unique' => '指定日期已经添加过',
  100. ]);
  101. //数据库-新增数据
  102. $custreport = array();
  103. $custreport['total_cost'] = $request->input('total_cost');
  104. $custreport['total_fan_add'] = $request->input('total_fan_add');
  105. $custreport['dtime'] = $request->input('dtime');
  106. $custreport['team_id'] = $team_id;
  107. $res = DB::table('cust_day_total')->insertGetId($custreport);
  108. if($res){
  109. #记录操作日志
  110. $self_id = session('admin_id');
  111. $self_name = session('real_name');
  112. $context = "运营上报投放数据";
  113. $type = 0;
  114. $tables = 'cust_day_total';
  115. $data_id = $res;
  116. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  117. }
  118. return redirect('/admin/custreport/totalindex')->with('info', '添加成功');
  119. }
  120. /**
  121. * 分组管理-编辑分组界面
  122. * @param $id
  123. * @return \Illuminate\View\View
  124. */
  125. public function totaledit($id,Request $request)
  126. {
  127. $data = CustTotal::where('id', $id)->first();
  128. $teamList = DB::table('teams')->select('id', 'name')->get();
  129. $teamList = json_decode(json_encode($teamList), true);
  130. return view('custreport/totaledit', [
  131. 'custreport' => $data,
  132. 'teamlist' => $teamList,
  133. ]);
  134. }
  135. /**
  136. * 分组管理-进行编辑操作
  137. * @param Request $request
  138. * @return \Illuminate\Http\RedirectResponse
  139. */
  140. public function totalupdate(Request $request)
  141. {
  142. $team_id = (int)$request->input('team_id');
  143. $id = (int)$request->input('id');
  144. $this->validate($request, [
  145. 'id' => 'required',
  146. 'total_cost' => 'required',
  147. 'total_fan_add' => 'required',
  148. 'team_id' => 'required',
  149. 'dtime' => 'required|unique:cust_day_total,dtime,'.$id.',id,team_id,'.$team_id,
  150. ], [
  151. 'id.required' => 'id不能为空',
  152. 'total_cost.required' => '总成本不能为空',
  153. 'total_fan_add.required' => '总加粉数不能为空',
  154. 'team_id.required' => '团队不能为空',
  155. 'dtime.required' => '日期不能为空',
  156. 'dtime.unique' => '指定日期已存在',
  157. ]);
  158. $custreport = array();
  159. $custreport['total_cost'] = $request->input('total_cost');
  160. $custreport['total_fan_add'] = $request->input('total_fan_add');
  161. $custreport['dtime'] = $request->input('dtime');
  162. $custreport['team_id'] = $team_id;
  163. $res = DB::table('cust_day_total')->where('id', $id)->update($custreport);
  164. if($res){
  165. #记录操作日志
  166. $self_id = session('admin_id');
  167. $self_name = session('real_name');
  168. $context = "修改运营上报数据";
  169. $type = 0;
  170. $tables = 'cust_day_total';
  171. $data_id = $id;
  172. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  173. }
  174. return redirect('/admin/custreport/totalindex')->with('info', '更新成功');
  175. }
  176. /**
  177. * 分组管理-进行删除操作
  178. * @param Request $request
  179. * @return \Illuminate\Http\RedirectResponse
  180. */
  181. public function totaldelete($id)
  182. {
  183. $custreport = CustTotal::find($id);
  184. $custreport->is_del = 1;
  185. if ($custreport ->save()){
  186. #记录操作日志
  187. $self_id = session('admin_id');
  188. $self_name = session('real_name');
  189. $context = "删除运营上报数据";
  190. $type = 0;
  191. $tables = 'cust_day_total';
  192. $data_id = $id;
  193. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  194. return redirect('/admin/custreport/totalindex')->with('info', '删除成功');
  195. }
  196. }
  197. public function total_export(Request $request){
  198. $self_role = session('role_name');
  199. if($self_role == '超级管理员' || $self_role == '团队主管' || $self_role == '售后管理员'){
  200. $admin_id = $request->input('admin_id');
  201. }else{
  202. $admin_id = session('admin_id');
  203. }
  204. $stime = $request->input('stime');
  205. $etime = $request->input('etime');
  206. $result = custreport::where(function($query) use($admin_id, $stime, $etime){
  207. if($admin_id) $query->where('admin_id', $admin_id);
  208. if($stime) $query->where('createTime', '>=', $stime);
  209. if($etime) $query->where('createTime', '<=', $etime);
  210. })->where('is_del',0)->custreportBy('id', 'desc')->get();
  211. $result = json_decode(json_encode($result),true);
  212. $filename="订单数据.xls";
  213. header("Content-type:application/vnd.ms-excel");
  214. Header("Accept-Ranges:bytes");
  215. Header("Content-Disposition:attachment;filename=".$filename); //$filename导出的文件名
  216. header("Pragma: no-cache");
  217. header("Expires: 0");
  218. $data_str = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
  219. xmlns:x="urn:schemas-microsoft-com:office:excel"
  220. xmlns="http://www.w3.org/TR/REC-html40">
  221. <head>
  222. <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
  223. <meta http-equiv=Content-Type content="text/html; charset=gb2312">
  224. <!--[if gte mso 9]><xml>
  225. <x:ExcelWorkbook>
  226. <x:ExcelWorksheets>
  227. <x:ExcelWorksheet>
  228. <x:Name></x:Name>
  229. <x:WorksheetOptions>
  230. <x:DisplayGridlines/>
  231. </x:WorksheetOptions>
  232. </x:ExcelWorksheet>
  233. </x:ExcelWorksheets>
  234. </x:ExcelWorkbook>
  235. </xml><![endif]-->
  236. </head>';
  237. $data_str .= "
  238. <table>
  239. <tr>
  240. <th>".iconv("UTF-8", "GB2312//IGNORE","销售微信号")."</th>
  241. <th>".iconv("UTF-8", "GB2312//IGNORE","销售团队")."</th>
  242. <th>".iconv("UTF-8", "GB2312//IGNORE","销售")."</th>
  243. <th>".iconv("UTF-8", "GB2312//IGNORE","订单时间")."</th>
  244. <th>".iconv("UTF-8", "GB2312//IGNORE","加粉时间")."</th>
  245. <th>".iconv("UTF-8", "GB2312//IGNORE","是否复购")."</th>
  246. <th>".iconv("UTF-8", "GB2312//IGNORE","订单金额")."</th>
  247. <th>".iconv("UTF-8", "GB2312//IGNORE","订单商品")."</th>
  248. <th>".iconv("UTF-8", "GB2312//IGNORE","是否退补单")."</th>
  249. <th>".iconv("UTF-8", "GB2312//IGNORE","配送地址")."</th>
  250. <th>".iconv("UTF-8", "GB2312//IGNORE","配送姓名")."</th>
  251. <th>".iconv("UTF-8", "GB2312//IGNORE","配送电话")."</th>
  252. <th>".iconv("UTF-8", "GB2312//IGNORE","供应商成本")."</th>
  253. <th>".iconv("UTF-8", "GB2312//IGNORE","顺丰单号")."</th>
  254. </tr>";
  255. foreach ($result as $k => $v)
  256. {
  257. #销售
  258. $admin = DB::table('admin')->where('id', $v['admin_id'])->first();
  259. $v['wx_id'] = isset($admin->wx_id) ? $admin->wx_id : '';
  260. #team
  261. $team = DB::table('teams')->where('id', $v['team_id'])->first();
  262. $v['team_name'] = isset($team->name) ? $team->name : '';
  263. #加粉时间
  264. $customr = DB::table('customers')->where('phone', $v['receiverMobile'])->first();
  265. $v['fanTime'] = isset($customr->fanTime) ? $customr->fanTime : '';
  266. $v['receiverMobile'] = substr($v['receiverMobile'], 0, 3).'****'.substr($v['receiverMobile'], 7);
  267. $fugou = $v['is_fugou']==1? '是' : '否';
  268. $is_refund = $v['is_refund']==1? '是' : '否';
  269. $address = $v['receiverState'].$v['receiverCity'].$v['receiverDistrict'].$v['receiverAddress'];
  270. $data_str .= "<tr>";
  271. $data_str .= "<td>".$v['wx_id']."</td>";
  272. $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["team_name"])."</td>";
  273. $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["admin_name"])."</td>";
  274. $data_str .= "<td>".$v['createTime']."</td>";
  275. $data_str .= "<td>".$v['fanTime']."</td>";
  276. $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $fugou)."</td>";
  277. $data_str .= "<td>".$v['receivedAmount']."</td>";
  278. $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["goods_note"])."</td>";
  279. $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $is_refund)."</td>";
  280. $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $address)."</td>";
  281. $data_str .= "<td>".iconv("UTF-8", "GB2312//IGNORE", $v["receiverName"])."</td>";
  282. $data_str .= "<td>".$v['receiverMobile']."</td>";
  283. $data_str .= "<td>".$v['cost']."</td>";
  284. $data_str .= "<td style='vnd.ms-excel.numberformat:@'>".$v["logistics_id"]."</td>";
  285. $data_str .= "</tr>";
  286. }
  287. $data_str .= "</table>";
  288. echo $data_str;
  289. exit;
  290. }
  291. public function detailindex(Request $request){
  292. $page = (int)$request->input('page');
  293. $pageSize = 20;
  294. if($page<=0){
  295. $page = 1;
  296. }
  297. $offset = ($page-1) * $pageSize;
  298. $team_id = (int)$request->input('team_id');
  299. $admin_id = (int)$request->input('admin_id');
  300. $search_admin = 1;
  301. #只能看自己团队的
  302. $self_role = session('role_name');
  303. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  304. $self_id = session('admin_id');
  305. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  306. }
  307. if($admin_id>0 && !$team_id){
  308. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  309. }
  310. if($self_role == '销售'){
  311. $search_admin = 0;
  312. $admin_id = $self_id;
  313. }
  314. //假如有团队筛选,检索销售队员
  315. $sale_ids = null;
  316. if($team_id>0 && !$admin_id){
  317. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  318. }
  319. $stime = $request->input('stime');
  320. $etime = $request->input('etime');
  321. $count = CustDetail::where(function($query) use($admin_id, $stime, $etime, $sale_ids){
  322. if($admin_id) $query->where('admin_id', $admin_id);
  323. if($stime) $query->where('dtime', '>=', $stime);
  324. if($etime) $query->where('dtime', '<=', $etime);
  325. if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
  326. })->where('is_del',0)->count();
  327. if ($count > 1) {
  328. // 总页数
  329. $pages = ceil($count/$pageSize);
  330. }else{
  331. // 总页数
  332. $pages = 1;
  333. }
  334. $result = CustDetail::where(function($query) use($admin_id, $stime, $etime, $sale_ids){
  335. if($admin_id) $query->where('admin_id', $admin_id);
  336. if($stime) $query->where('dtime', '>=', $stime);
  337. if($etime) $query->where('dtime', '<=', $etime);
  338. if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
  339. })->where('is_del',0)->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  340. $result = json_decode(json_encode($result),true);
  341. $teamList = DB::table('teams')->select('id', 'name')->where(function($query) use($self_role, $team_id){
  342. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('id', $team_id);
  343. })->get();
  344. $teamList = json_decode(json_encode($teamList), true);
  345. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->where(function($query) use($self_role, $team_id){
  346. if($team_id>0 && $self_role != '超级管理员' && $self_role != '售后管理员') $query->where('team_id', $team_id);
  347. })->get();
  348. $adminList = json_decode(json_encode($adminList), true);
  349. //未上报数据销售
  350. $noReportSaler = $this->noReportSaler();
  351. $no_height = '100';
  352. if($no_num = count($noReportSaler)) $no_height += round($no_num * 37);
  353. return view('custreport/detaillist', ['result' =>$result,
  354. 'page' =>$page,
  355. 'count' =>$count,
  356. 'pages' =>$pages,
  357. 'stime' =>$stime,
  358. 'etime' =>$etime,
  359. 'admin_id' =>$admin_id,
  360. 'team_id' =>$team_id,
  361. 'search_admin' =>$search_admin,
  362. 'adminlist' =>$adminList,
  363. 'teamlist' =>$teamList,
  364. 'noReportSaler' =>$noReportSaler,
  365. 'no_height' =>$no_height,
  366. ]);
  367. }
  368. /**
  369. * 添加订单
  370. * @return \Illuminate\View\View
  371. */
  372. public function detailcreate(Request $request)
  373. {
  374. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
  375. $adminList = json_decode(json_encode($adminList), true);
  376. $teamList = DB::table('teams')->select('id', 'name')->get();
  377. $teamList = json_decode(json_encode($teamList), true);
  378. $self_role = session('role_name');
  379. return view('custreport/detailcreate',['adminlist'=>$adminList, 'teamlist'=>$teamList, 'self_role'=>$self_role]);
  380. }
  381. /**
  382. * 分组管理-进行添加操作
  383. * @param Request $request
  384. * @return \Illuminate\Http\RedirectResponse
  385. */
  386. public function detailstore(Request $request)
  387. {
  388. $admin_id = (int)$request->input('admin_id');
  389. if(!$admin_id){
  390. $admin_id = session('admin_id');
  391. }
  392. $this->validate($request, [
  393. 'old_consult' => 'required',
  394. 'new_consult' => 'required',
  395. 'fan_add' => 'required',
  396. 'dtime' => 'required|unique:cust_day_detail,dtime,1,is_del,admin_id,'.$admin_id,
  397. ], [
  398. 'old_consult.required' => '老粉咨询不能为空',
  399. 'new_consult.required' => '新粉咨询数不能为空',
  400. 'fan_add.required' => '加粉数不能为空',
  401. 'dtime.required' => '日期不能为空',
  402. 'dtime.unique' => '指定日期已经添加过',
  403. ]);
  404. //数据库-新增数据
  405. $custreport = array();
  406. $custreport['old_consult'] = $request->input('old_consult');
  407. $custreport['new_consult'] = $request->input('new_consult');
  408. $custreport['fan_add'] = $request->input('fan_add');
  409. $custreport['new_reply'] = $request->input('new_reply');
  410. $custreport['dtime'] = $request->input('dtime');
  411. if($admin_id>0){
  412. $admin_info = DB::table('admin')->select('realname', 'team_id')->where('id', $admin_id)->first();
  413. $custreport['admin_id'] = $admin_id;
  414. $custreport['admin_name'] = $admin_info->realname;
  415. }else{
  416. $custreport['admin_id'] = session('admin_id');
  417. $custreport['admin_name'] = session('real_name');
  418. }
  419. $custreport['reporter_id'] = session('admin_id');
  420. $res = DB::table('cust_day_detail')->insertGetId($custreport);
  421. if($res){
  422. #记录操作日志
  423. $self_id = session('admin_id');
  424. $self_name = session('real_name');
  425. $context = "销售上报加粉数据";
  426. $type = 0;
  427. $tables = 'cust_day_detail';
  428. $data_id = $res;
  429. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  430. }
  431. return redirect('/admin/custreport/detailindex')->with('info', '添加成功');
  432. }
  433. /**
  434. * 分组管理-编辑分组界面
  435. * @param $id
  436. * @return \Illuminate\View\View
  437. */
  438. public function detailedit($id,Request $request)
  439. {
  440. $data = CustDetail::where('id', $id)->first();
  441. $adminList = DB::table('admin')->select('id', 'realname', 'username')->where('id','>', 1)->get();
  442. $adminList = json_decode(json_encode($adminList), true);
  443. $teamList = DB::table('teams')->select('id', 'name')->get();
  444. $teamList = json_decode(json_encode($teamList), true);
  445. $self_role = session('role_name');
  446. return view('custreport/detailedit', [
  447. 'custreport' => $data,
  448. 'adminlist' => $adminList,
  449. 'teamlist' => $teamList,
  450. 'self_role' => $self_role,
  451. ]);
  452. }
  453. /**
  454. * 分组管理-进行编辑操作
  455. * @param Request $request
  456. * @return \Illuminate\Http\RedirectResponse
  457. */
  458. public function detailupdate(Request $request)
  459. {
  460. $id = (int)$request->input('id');
  461. $admin_id = (int)$request->input('admin_id');
  462. if(!$admin_id){
  463. $admin_id = session('admin_id');
  464. }
  465. $this->validate($request, [
  466. 'id' => 'required',
  467. 'old_consult' => 'required',
  468. 'new_consult' => 'required',
  469. 'fan_add' => 'required',
  470. 'dtime' => 'required|unique:cust_day_detail,dtime,'.$id.',id,admin_id,'.$admin_id,
  471. ], [
  472. 'id.required' => 'id不能为空',
  473. 'old_consult.required' => '老粉咨询不能为空',
  474. 'new_consult.required' => '新粉咨询数不能为空',
  475. 'fan_add.required' => '加粉数不能为空',
  476. 'dtime.required' => '日期不能为空',
  477. 'dtime.unique' => '指定日期已经存在',
  478. ]);
  479. //数据库-新增数据
  480. $custreport = array();
  481. $custreport['old_consult'] = $request->input('old_consult');
  482. $custreport['new_consult'] = $request->input('new_consult');
  483. $custreport['fan_add'] = $request->input('fan_add');
  484. $custreport['new_reply'] = $request->input('new_reply');
  485. $custreport['dtime'] = $request->input('dtime');
  486. $admin_id = (int)$request->input('admin_id');
  487. if($admin_id>0){
  488. $admin_info = DB::table('admin')->select('realname', 'team_id')->where('id', $admin_id)->first();
  489. $custreport['admin_id'] = $admin_id;
  490. $custreport['admin_name'] = $admin_info->realname;
  491. }
  492. $res = DB::table('cust_day_detail')->where('id', $id)->update($custreport);
  493. if($res){
  494. #记录操作日志
  495. $self_id = session('admin_id');
  496. $self_name = session('real_name');
  497. $context = "修改销售上报数据";
  498. $type = 0;
  499. $tables = 'cust_day_detail';
  500. $data_id = $id;
  501. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  502. }
  503. return redirect('/admin/custreport/detailindex')->with('info', '更新成功');
  504. }
  505. /**
  506. * 分组管理-进行删除操作
  507. * @param Request $request
  508. * @return \Illuminate\Http\RedirectResponse
  509. */
  510. public function detaildelete($id)
  511. {
  512. $custreport = CustDetail::find($id);
  513. $custreport->is_del = 1;
  514. if ($custreport ->save()){
  515. #记录操作日志
  516. $self_id = session('admin_id');
  517. $self_name = session('real_name');
  518. $context = "删除销售上报数据";
  519. $type = 0;
  520. $tables = 'cust_day_detail';
  521. $data_id = $id;
  522. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  523. return redirect('/admin/custreport/detailindex')->with('info', '删除成功');
  524. }
  525. }
  526. public function detail_export(Request $request){
  527. $team_id = (int)$request->input('team_id');
  528. $admin_id = (int)$request->input('admin_id');
  529. $search_admin = 1;
  530. #只能看自己团队的
  531. $self_role = session('role_name');
  532. if($self_role != '超级管理员' && $self_role != '售后管理员'){
  533. $self_id = session('admin_id');
  534. $team_id = DB::table('admin')->where('id', $self_id)->pluck('team_id');
  535. }
  536. if($admin_id>0 && !$team_id){
  537. $team_id = DB::table('admin')->where('id', $admin_id)->pluck('team_id');
  538. }
  539. if($self_role == '销售'){
  540. $search_admin = 0;
  541. $admin_id = $self_id;
  542. }
  543. //假如有团队筛选,检索销售队员
  544. $sale_ids = null;
  545. if($team_id>0 && !$admin_id){
  546. $sale_ids = DB::table('admin')->where('team_id', $team_id)->lists('id');
  547. }
  548. $stime = $request->input('stime');
  549. $etime = $request->input('etime');
  550. $result = CustDetail::where(function($query) use($admin_id, $stime, $etime, $sale_ids){
  551. if($admin_id) $query->where('admin_id', $admin_id);
  552. if($stime) $query->where('dtime', '>=', $stime);
  553. if($etime) $query->where('dtime', '<=', $etime);
  554. if($sale_ids !== null) $query->whereIn('admin_id', $sale_ids);
  555. })->where('is_del',0)->orderBy('id', 'desc')->get();
  556. $result = json_decode(json_encode($result),true);
  557. $indexKey = ['dtime','fan_add','new_reply','old_consult','new_consult','admin_name'];
  558. $title = ['日期', '加粉数', '新粉回复数', '老粉询价数', '新粉询价数','所属销售'];
  559. $filename = 'xiaoshoushangbao_'.date('Y-m-d_H').'.xlsx';
  560. return Order::export_excel($result, $filename, $indexKey, $title);
  561. }
  562. /**
  563. * 地域投入数据
  564. */
  565. public function disCostList(Request $request){
  566. $page = (int)$request->input('page');
  567. $pageSize = 20;
  568. if($page<=0){
  569. $page = 1;
  570. }
  571. $offset = ($page-1) * $pageSize;
  572. $stime = $request->input('stime');
  573. $etime = $request->input('etime');
  574. $count = AdCost::where(function($query) use($stime, $etime){
  575. if($stime) $query->where('ad_time', '>=', $stime);
  576. if($etime) $query->where('ad_time', '<=', $etime);
  577. })->count();
  578. if ($count > 1) {
  579. // 总页数
  580. $pages = ceil($count/$pageSize);
  581. }else{
  582. // 总页数
  583. $pages = 1;
  584. }
  585. $result = AdCost::where(function($query) use($stime, $etime){
  586. if($stime) $query->where('ad_time', '>=', $stime);
  587. if($etime) $query->where('ad_time', '<=', $etime);
  588. })->orderBy('id', 'desc')->offset($offset)->limit($pageSize)->get();
  589. $result = json_decode(json_encode($result),true);
  590. $today = date('Y-m-d');
  591. return view('custreport/discostlist', ['result' =>$result,
  592. 'page' =>$page,
  593. 'count' =>$count,
  594. 'pages' =>$pages,
  595. 'stime' =>$stime,
  596. 'etime' =>$etime,
  597. 'today' =>$today,
  598. ]);
  599. }
  600. /**
  601. * excel导入每日地域投入信息
  602. * @param Request $request
  603. */
  604. public function importDisCost(Request $request) {
  605. $wexin_appid = trim($request->input('wexin_appid'));
  606. $ad_time = $request->input('ad_time');
  607. $excelFile = $request->file('dataFile');
  608. //实例化Excel读取类
  609. $objReader = new PHPExcel_Reader_Excel2007();
  610. if(!$objReader->canRead($excelFile)){
  611. $objReader = new PHPExcel_Reader_Excel5();
  612. if(!$objReader->canRead($excelFile)){
  613. $objReader = new PHPExcel_Reader_CSV();
  614. $objReader->setInputEncoding('UTF-8');
  615. $objReader->setDelimiter(',');
  616. if(!$objReader->canRead($excelFile)){
  617. echo "\n".'无法识别的Excel文件!';
  618. return false;
  619. }
  620. }
  621. }
  622. $objPHPExcel=$objReader->load($excelFile);
  623. $worksheet=$objPHPExcel->getSheet(0);//获取第一个工作表
  624. $highestRow=$worksheet->getHighestRow();//取得总行数
  625. $highestColumn=$worksheet->getHighestColumn(); //取得总列数
  626. $lines = $highestRow - 1;
  627. if ($lines <= 0) {
  628. //'Excel表格中没有数据';
  629. return false;
  630. }
  631. $errorArr=[];
  632. $okStr = '';
  633. for ($row = 2; $row <= $highestRow; ++$row) {
  634. $params = array();
  635. $params['wexin_appid'] = $wexin_appid;
  636. $params['ad_time'] = $ad_time;
  637. $params['city'] = trim($worksheet->getCellByColumnAndRow(0, $row)->getValue());
  638. $params['cost'] = trim($worksheet->getCellByColumnAndRow(1, $row)->getValue());
  639. $params['exposure_times'] = intval($worksheet->getCellByColumnAndRow(2, $row)->getValue());
  640. $params['click_times'] = intval($worksheet->getCellByColumnAndRow(3, $row)->getValue());
  641. $params['click_rate'] = trim($worksheet->getCellByColumnAndRow(4, $row)->getValue());
  642. $params['conversion_times'] = intval($worksheet->getCellByColumnAndRow(5, $row)->getValue());
  643. $params['conversion_cost'] = trim($worksheet->getCellByColumnAndRow(6, $row)->getValue());
  644. //判断是否已存在
  645. $if_data = AdCost::where('wexin_appid', $params['wexin_appid'])->where('ad_time', $params['ad_time'])->where('city', $params['city'])->first();
  646. if(isset($if_data->id)){
  647. $result = AdCost::where('id', $if_data->id)->update($params);
  648. }else{
  649. $result = AdCost::insert($params);
  650. }
  651. }
  652. return redirect('admin/custreport/disCostList')->with('info','导入成功');
  653. }
  654. /**
  655. * 未上报数据销售
  656. */
  657. public function noReportSaler(){
  658. $today = date('Y-m-d');
  659. //销售列表
  660. $ids = DB::table('admin_role')->where('user_id','!=',3)->where('role_name', '销售')->lists('user_id');
  661. $ids = DB::table('admin')->whereIn('id', $ids)->where('is_use', 1)->lists('id');
  662. //销售名字
  663. $salers = DB::table('admin')->whereIn('id', $ids)->lists('realname', 'id');
  664. //查每日上报数据销售
  665. $result = DB::table('cust_day_detail')->select('admin_id', 'dtime')->where('dtime','<',$today)->where('dtime','>=','2019-09-04')->where('is_del', 0)->orderBy('dtime', 'desc')->get();
  666. $result = json_decode(json_encode($result), true);
  667. $data = array();
  668. foreach($result as $k=>$v){
  669. $data[$v['dtime']][] = $v['admin_id'];
  670. }
  671. $ret = array();
  672. foreach($data as $k=>$v){
  673. $diff = array_diff($ids, $v);
  674. if(!empty($diff)){
  675. //名字带入
  676. foreach($diff as &$nid){
  677. $nid = $salers[$nid];
  678. }
  679. $ret[$k] = implode(',', $diff);
  680. }
  681. }
  682. return $ret;
  683. }
  684. }