Brak opisu

CustReportController.php 33KB

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