Ei kuvausta

CustReportController.php 32KB

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