DataController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php
  2. namespace App\Http\Controllers\Api\mini;
  3. use App\Models\Device;
  4. use App\Models\DeviceName;
  5. use App\Models\Order;
  6. use App\Models\OrderDevice;
  7. use App\Models\OrderOverviewModel;
  8. use App\Models\Project;
  9. use App\Models\RentType;
  10. use App\Models\Spec;
  11. use Carbon\Carbon;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Log;
  14. use phpDocumentor\Reflection\DocBlock;
  15. class DataController extends BaseController
  16. {
  17. protected $order;
  18. protected $order_device;
  19. protected $order_ids = [];
  20. protected $project_ids = [];
  21. protected $start_at;
  22. protected $end_at;
  23. public function __construct()
  24. {
  25. $this->order = new Order();
  26. $this->order_device = new OrderDevice();
  27. }
  28. public function getYearsAndMonths(Request $request)
  29. {
  30. $years = [];
  31. $this_year = Carbon::now()->year;
  32. for($i = 5; $i >= 0; $i--) {
  33. array_push($years, [
  34. 'name' => $this_year - $i,
  35. 'id' => $this_year - $i
  36. ]);
  37. }
  38. array_push($years, ['name' => '所有年', 'id' => '']);
  39. $months = [];
  40. for($i = 1; $i < 13; ++$i) {
  41. array_push($months, [
  42. 'name' => $i . '月',
  43. 'id' => $i
  44. ]);
  45. }
  46. $month = Carbon::now()->month;
  47. return $this->success(['data' => [
  48. 'array' => [$years, $months],
  49. 'index' => [count($years) - 2, $month - 1]
  50. ]]);
  51. }
  52. public function getDateInfo()
  53. {
  54. $max_date = Carbon::now()->toDateString();
  55. // $order = $this->order->orderBy('created_at', 'desc')->first();
  56. // $min_date = $order ? substr($order->created_at, 0, 10) : Carbon::now()->subYear()->toDateString();
  57. $min_date = Carbon::now()->subYears(10)->toDateString();
  58. $date = substr($max_date, 0, 7);
  59. $start_date = $max_date;
  60. $end_date = Carbon::now()->addMonth()->toDateString();
  61. $start_date = substr($start_date, 0, 7);
  62. $end_date = substr($end_date, 0, 7);
  63. return $this->success(['data' => compact('date', 'min_date', 'max_date', 'start_date', 'end_date')]);
  64. }
  65. public function getStartAt($date)
  66. {
  67. return strlen($date) <= 7 ? $date . '-01 00:00:00' : $date . ' 00:00:00';
  68. }
  69. public function getEndAt($date)
  70. {
  71. return strlen($date) <= 7 ? Carbon::createFromTimeString($date . '-01 00:00:00')->addMonth(1)->toDateTimeString() : Carbon::createFromTimeString($date . ' 00:00:00')->addDay(1)->toDateTimeString();
  72. }
  73. public function getOrders(Request $request)
  74. {
  75. $order_ids = $this->getOrderIds($request);
  76. $start_at = $this->getStartAt($request->input('start_date'));
  77. $end_at = $this->getEndAt($request->input('end_date'));
  78. return $this->order->whereIn('id', $order_ids)->where([
  79. ['type', '=', 1],
  80. ['created_at', '>=', $start_at],
  81. ['created_at', '<', $end_at]
  82. ])->get();
  83. }
  84. public function getOrderIds(Request $request)
  85. {
  86. $ids = $request->input('project_ids');
  87. if(!$ids || count($ids) < 0) return false;
  88. $this->project_ids = $ids;
  89. $order_devices = $this->order_device->whereIn('project_id', $ids);
  90. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  91. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  92. foreach ($in_items as $key => $item) {
  93. if($request->input($item)) {
  94. $item_ids = collect($request->input($item))->filter(function($id) {
  95. return $id;
  96. });
  97. if($item_ids && $item_ids->count() > 0) {
  98. $order_devices = $order_devices->whereIn($key_items[$key], $item_ids);
  99. }
  100. }
  101. }
  102. return $order_devices->pluck('order_id')->unique();
  103. }
  104. public function getStat(Request $request)
  105. {
  106. $info = [];
  107. $device_arr = $request->input('device_ids');
  108. $device_name_arr = $request->input('device_name_ids');
  109. $spec_arr = $request->input('spec_ids');
  110. if (array_sum($device_arr) != 0)
  111. {
  112. array_push($info,Device::whereIn('id',$device_arr)->pluck('name')->toArray()) ;
  113. array_push($info,DeviceName::whereIn('id',$device_name_arr)->pluck('name')->toArray()) ;
  114. array_push($info,Spec::whereIn('device_id',$device_arr)
  115. ->whereIn('device_name_id',$device_name_arr)
  116. ->whereIn('id',$spec_arr)
  117. ->pluck('name')->toArray());
  118. array_push($info,RentType::where('id',$request->input('rent_type_ids'))->value('name'));
  119. }
  120. if(!$request->input('project_ids')) return $this->error(['msg' => '']);
  121. if($request->input('chart_type') == 'pie') return $this->getPieData($request);
  122. else if($request->input('chart_type') == 'radar') return $this->getRadarData($request);
  123. $orders = $this->getOrders($request);
  124. $names = [];
  125. $values = [];
  126. // year|month
  127. $type = $request->input('type') ? $request->input('type') : 'year';
  128. $cnt = 0;
  129. $projects = Project::whereIn('id', $this->project_ids)->where('id','!=',1)->get();
  130. if(empty($orders->toArray())){
  131. foreach ($projects as $p){
  132. $values[] = 0;
  133. $names[] = $p->name;
  134. }
  135. }
  136. foreach($orders as $item) {
  137. if($this->inDate($names, $item->created_at, $type)) {
  138. foreach ($projects as $key => $project) {
  139. $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + ($item->money / 100) : $values[$key][$cnt-1];
  140. }
  141. } else {
  142. $names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  143. foreach ($projects as $key => $project) {
  144. $values[$key][$cnt] = $project->id == $item->project_id ? ($item->money / 100) : 0;
  145. }
  146. $cnt = $cnt + 1;
  147. }
  148. }
  149. $legends = $projects->pluck('name');
  150. return $this->success(['data' => compact('values', 'names', 'legends','info')]);
  151. }
  152. public function getRadarData(Request $request)
  153. {
  154. $orders = $this->getOrders($request);
  155. if(!$orders) return $this->error(['msg' => '']);
  156. $data = [];
  157. $projects = Project::whereIn('id', $request->input('project_ids'))->get();
  158. $indicator = [
  159. ['name' => '累计租赁花费', 'max' => 0],
  160. ['name' => '累计租赁数量', 'max' => 0],
  161. ['name' => '平均月消费', 'max' => 0],
  162. ['name' => '租赁次数', 'max' => 0],
  163. ['name' => '租赁天数', 'max' => 0]
  164. ];
  165. $legends = [];
  166. foreach($projects as $key => $project) {
  167. $tmp_orders = $orders->where('project_id', $project->id);
  168. $total_money = $tmp_orders->sum('money') / 100;
  169. $total_num = OrderDevice::whereIn('order_id', $orders->where('project_id', $project->id)->pluck('id'))->count();
  170. $months = $this->getMonths($tmp_orders);
  171. $mean_month = $total_money / $months;
  172. $rent_times = $tmp_orders->count();
  173. $rent_days = $this->getRentDays($tmp_orders);
  174. $indicator[0]['max'] = max($indicator[0]['max'], $total_money);
  175. $indicator[1]['max'] = max($indicator[1]['max'], $total_num);
  176. $indicator[2]['max'] = max($indicator[2]['max'], $mean_month);
  177. $indicator[3]['max'] = max($indicator[3]['max'], $rent_times);
  178. $indicator[4]['max'] = max($indicator[4]['max'], $rent_days);
  179. array_push($data, [
  180. 'name' => $project->name,
  181. 'value' => [$total_money, $total_num, $mean_month, $rent_times, $rent_days]
  182. ]);
  183. array_push($legends, $project->name);
  184. }
  185. return $this->success(['data' => compact('data', 'legends', 'indicator')]);
  186. }
  187. public function getMonths($orders)
  188. {
  189. $min_at = $orders->min('created_at');
  190. $max_at = $orders->max('updated_at');
  191. if($max_at && $max_at) $months = $min_at->diffInMonths($max_at);
  192. else $months = 1;
  193. return $months ? $months : 1;
  194. }
  195. public function getRentDays($orders)
  196. {
  197. $order_ids = $orders->pluck('id');
  198. $order_devices = OrderDevice::whereId('order_id', $order_ids)->get();
  199. $days = 0;
  200. foreach($order_devices as $order_device) {
  201. if($order_device->start_date && $order_device->end_date) {
  202. $days = $days + Carbon::createFromTimeString($order_device->start_date . ' 00:00:00')->diffInDays($order_device->end_date);
  203. }
  204. }
  205. return $days;
  206. }
  207. public function getPieData(Request $request)
  208. {
  209. $order_ids = $this->getOrderIds($request);
  210. if(!$order_ids) return $this->error(['msg' => '']);
  211. $total = OrderDevice::whereIn('order_id', $order_ids)->count();
  212. $devices = Device::all();
  213. $data = [];
  214. foreach($devices as $device) {
  215. $tmp = OrderDevice::whereIn('order_id', $order_ids)->where('device_id', $device->id)->count();
  216. $percent = $total > 0 ? round(($tmp / $total * 10000)) / 100 : '0';
  217. array_push($data, ['name' => $device->name, 'value' => $percent]);
  218. }
  219. $legends = $devices->pluck('name');
  220. return $this->success(['data' => compact('data', 'legends')]);
  221. }
  222. public function getDetailData(Request $request)
  223. {
  224. // $order_ids = $this->getOrderIds($request);
  225. // if(!$order_ids) return $this->error(['msg' => '']);
  226. // $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
  227. // $devices = Device::all();
  228. //
  229. // $dates = [];
  230. // // year|month
  231. // $type = $request->input('type') ? $request->input('type') : 'year';
  232. // $columns = [];
  233. // $date = '';
  234. // $values = [
  235. // ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
  236. // ];
  237. // foreach($order_devices as $item) {
  238. // if($this->inDate($dates, $item->created_at, $type)) {
  239. // foreach($devices as $key => $device) {
  240. // $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  241. // $columns[$key] = $columns[$key] + $money;
  242. // }
  243. // } else {
  244. // $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  245. // array_push($dates, $date);
  246. // if(count($columns) > 0) {
  247. // array_push($values, [
  248. // 'date' => $date,
  249. // 'total' => collect($columns)->sum() / 100,
  250. // 'data' => $columns
  251. // ]);
  252. // }
  253. // $columns = [];
  254. // foreach($devices as $device) {
  255. // $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
  256. // array_push($columns, $money);
  257. // }
  258. // }
  259. // }
  260. // if(count($columns) > 0) {
  261. // array_push($values, [
  262. // 'date' => $date,
  263. // 'total' => collect($columns)->sum(),
  264. // 'data' => $columns
  265. // ]);
  266. // }
  267. // return $this->success(['data' => $values]);
  268. $date = $this->getStartAt($request->input('date'));
  269. $year = date('Y',strtotime($date));
  270. $project_id = $request->input('project_ids');
  271. $project_id = $project_id[0];
  272. $devices = Device::all();
  273. $data = [
  274. ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')->toArray()]
  275. ];
  276. $items = OrderOverviewModel::whereYear('date',$year)->where('project_id',$project_id)->get()->toArray();
  277. foreach ($items as &$item)
  278. {
  279. $arr = [
  280. 'data'=>[],
  281. ];
  282. $arr_data = $devices->pluck('id')->toArray();
  283. $arr['total'] = $item['total_price']/100;
  284. $arr['date'] = date('Y-m',strtotime($item['date']));
  285. //获取到月份的开始时间和结束时间
  286. $start_at = $item['date'];
  287. $end_at = $this->getEndAt(date('Y-m',strtotime($item['date'])));
  288. //获取到订单数组
  289. $order_arr = Order::where('project_id',$project_id)
  290. ->where('status',3)
  291. ->where('type',1)
  292. ->whereBetween('updated_at',[$start_at,$end_at])
  293. ->pluck('id')->toArray();
  294. $order_details = OrderDevice::whereIn('order_id',$order_arr)->get(['quantity','price','device_id'])->groupBy('device_id')->toArray();
  295. foreach ($order_details as $key=>&$detail)
  296. {
  297. $total_nums = 0;
  298. foreach ($detail as $detail_value)
  299. {
  300. $total_nums += ($detail_value['price']*$detail_value['quantity'])/100;
  301. }
  302. $device_data = $arr_data;
  303. //重置键值数组
  304. foreach ($device_data as $k=>&$device_datum)
  305. {
  306. $device_datum = 0;
  307. }
  308. $device_data[$key-1] = $total_nums;
  309. $arr['data']=$device_data;
  310. }
  311. array_push($data,$arr);
  312. }
  313. return $this->success(['data' => $data]);
  314. }
  315. public function getTotalInfo(Request $request)
  316. {
  317. if (!$request->input('project_ids'))
  318. {
  319. return $this->error(['msg' => '暂未选择项目']);
  320. }
  321. $start_at = $this->getStartAt($request->input('date'));
  322. $end_at = $this->getEndAt($request->input('date'));
  323. $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
  324. $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
  325. ['created_at', '>=', $start_at],
  326. ['created_at', '<', $end_at]
  327. ])->sum('money') / 100;
  328. return $this->success(['data' => compact('total_money', 'month_money')]);
  329. }
  330. public function inDate($dates, $created_at, $type)
  331. {
  332. $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10);
  333. return in_array($date, $dates);
  334. }
  335. public function parseDate($date)
  336. {
  337. $now = Carbon::now();
  338. if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day];
  339. $items = explode('-', $date);
  340. $year = $now->year;
  341. $month = $now->month;
  342. $day = $now->day;
  343. if(isset($items[0])) {
  344. $year = (int)$items[0];
  345. $year = $year > 2010 && $year < 2050 ? $year : $now->year;
  346. }
  347. if(isset($items[1])) {
  348. $month = (int)$items[1];
  349. $month = $month > 0 && $month < 13 ? $month : $now->month;
  350. }
  351. if(isset($item[2])) {
  352. $day = (int)$items[2];
  353. $day = $day > 0 && $day < 32 ? $date : 1;
  354. }
  355. return compact('year', 'month', 'day');
  356. }
  357. public function getYearAndMonthMoney(Request $request)
  358. {
  359. $date = $request->input('date');
  360. if(!$date) {
  361. $year_money = Order::where('type', 1)->sum('money') / 100;
  362. $month_money = $year_money;
  363. } else {
  364. $start_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->toDateTimeString();
  365. $end_year = Carbon::createFromTimeString(substr($date, 0, 4) . '-01-01 00:00:00')->addYear()->toDateTimeString();
  366. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  367. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  368. $year_money = Order::where([
  369. ['type', 1],
  370. ['created_at', '>=', $start_year],
  371. ['created_at', '<', $end_year]
  372. ])->sum('money') / 100;
  373. $month_money = Order::where([
  374. ['type', 1],
  375. ['created_at', '>=', $start_month],
  376. ['created_at', '<', $end_month]
  377. ])->sum('money') / 100;
  378. }
  379. return $this->success(['data' => compact('year_money', 'month_money')]);
  380. }
  381. public function getProjectYears()
  382. {
  383. $start=2020;
  384. $now = date('Y',time());
  385. $years_arr = [];
  386. for ($i = 0;$i<=($now-$start);$i++)
  387. {
  388. array_push($years_arr,($start+$i).'年');
  389. }
  390. return $this->success(['msg' => '操作成功', 'data' => $years_arr]);
  391. }
  392. public function getSingleStat(Request $request)
  393. {
  394. //如果初始进来不传筛选的年份,那就默认选择当前的年份
  395. $project_year = $request->input('project_year')? $request->input('project_year'):date('Y',time());
  396. //如果有项目id,那就用项目id,如果没有就默认选择第一个项目
  397. if ($request->input('project_id')!=-1)
  398. {
  399. $all_project = 0;
  400. $project_id = $request->input('project_id') ? $request->input('project_id') : Project::where('id','>',1)->first()->id;
  401. $project = Project::find($project_id);
  402. }else
  403. {
  404. $all_project = 1;
  405. }
  406. $sort_type = $request->input('sort_type') == 'year' ? 'year' : 'month';
  407. $values = [];
  408. $names = [];
  409. $name = $sort_type == 'year' ? '每年租赁金额' : '每月租赁金额';
  410. if($sort_type == 'month') {
  411. $start_at = $project_year.'-01-01 00:00:00';
  412. $end_at = ($project_year+1).'-01-01 00:00:00';
  413. for($i = 1; $i < 13; $i++) {
  414. if ($i>9&&$i!=12)
  415. {
  416. $start_month_at = $project_year.'-'.$i.'-01 00:00:00';
  417. }else
  418. {
  419. $start_month_at = $project_year.'-0'.$i.'-01 00:00:00';
  420. }
  421. if ($i>=9&&$i!=12)
  422. {
  423. $end_month_at = $project_year.'-'.($i+1).'-01 00:00:00';
  424. }else
  425. {
  426. $end_month_at = $project_year.'-0'.($i+1).'-01 00:00:00';
  427. }
  428. if ($i==12)
  429. {
  430. $start_month_at = $project_year.'-'.$i.'-01 00:00:00';
  431. $end_month_at = ($project_year+1).'-01-01 00:00:00';
  432. }
  433. if ($all_project == 0)
  434. {
  435. $value = Order::where('type',1)
  436. ->where('project_id', $project->id)
  437. ->whereBetween('created_at',[$start_at,$end_at])
  438. ->whereBetween('created_at',[$start_month_at,$end_month_at])
  439. ->sum('money');
  440. $value = $value/100;
  441. }else
  442. {
  443. $value = Order::where('type',1)
  444. ->whereBetween('created_at',[$start_at,$end_at])
  445. ->whereBetween('created_at',[$start_month_at,$end_month_at])
  446. ->sum('money');
  447. $value = $value/100;
  448. }
  449. array_push($values, $value);
  450. array_push($names, $i . '月');
  451. }
  452. } else {
  453. $start=2020;
  454. $now = date('Y',time());
  455. $year = [];
  456. for ($i = 0;$i<=($now-$start);$i++)
  457. {
  458. array_push($year,($start+$i));
  459. array_push($names,($start+$i).'年');
  460. }
  461. foreach ($year as $v)
  462. {
  463. $start_year_at = $v.'-01-01 00:00:00';
  464. $end_year_at = ($v+1).'-01-01 00:00:00';
  465. if ($all_project == 0)
  466. {
  467. $value = Order::where('type',1)
  468. ->where('project_id', $project->id)
  469. ->whereBetween('created_at',[$start_year_at,$end_year_at])
  470. ->sum('money');
  471. $value = $value/100;
  472. }else{
  473. $value = Order::where('type',1)
  474. ->whereBetween('created_at',[$start_year_at,$end_year_at])
  475. ->sum('money');
  476. $value = $value/100;
  477. }
  478. array_push($values, $value);
  479. }
  480. }
  481. if ($all_project == 0)
  482. {
  483. return $this->success(['data' => compact('values', 'names', 'project', 'name')]);
  484. }else
  485. {
  486. return $this->success(['data' => compact('values', 'names', 'name')]);
  487. }
  488. }
  489. public function getMaxStat(Request $request)
  490. {
  491. $info = [];
  492. if ($request->input('devices')!=null)
  493. {
  494. array_push($info,Device::where('id',$request->input('device_ids'))->value('name')) ;
  495. array_push($info,DeviceName::where('id',$request->input('device_name_ids'))->value('name')) ;
  496. array_push($info,Spec::where('device_id',$request->input('device_ids'))->where('device_name_id',$request->input('device_name_ids'))->where('id',$request->input('spec_ids'))->value('name'));
  497. array_push($info,RentType::where('id',$request->input('rent_type_ids'))->value('name'));
  498. }
  499. $date = $request->input('date');
  500. $date = $date ? $date : Carbon::now()->toDateString();
  501. $start_at = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  502. $end_at = Carbon::now()->toDateTimeString();
  503. $items = OrderDevice::where([
  504. ['created_at', '>=', $start_at],
  505. ['created_at', '<', $end_at]
  506. ]);
  507. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  508. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  509. foreach ($in_items as $key => $item) {
  510. if($request->input($item)) {
  511. $item_ids = collect($request->input($item))->filter(function($id) {
  512. return $id;
  513. });
  514. if($item_ids && $item_ids->count() > 0) {
  515. $items = $items->whereIn($key_items[$key], $item_ids);
  516. }
  517. }
  518. }
  519. $items = $items->get();
  520. $projects = Project::where('id','!=',1)->get();
  521. foreach ($projects as $project) {
  522. $project->max_price = $items->where('project_id', $project->id)->max('price') / 100;
  523. }
  524. $orderBy = $request->input('orderBy');
  525. if($orderBy == 'asc') $projects = $projects->sortBy('max_price');
  526. else $projects = $projects->sortByDesc('max_price');
  527. if ($info == null)
  528. {
  529. $values = $projects->pluck('max_price')->toArray();
  530. foreach ($values as &$value)
  531. {
  532. $value = 0;
  533. }
  534. $values = 0;
  535. }else
  536. {
  537. $values = $projects->pluck('max_price');
  538. }
  539. $names = $projects->pluck('name');
  540. return $this->success(['data' => compact('values', 'names','info')]);
  541. }
  542. public function projectStat(Request $request)
  543. {
  544. if($request->input('chartIndex') == 0) {
  545. return $this->getSingleStat($request);
  546. } else if($request->input('chartIndex') == 2) {
  547. return $this->getMaxStat($request);
  548. }
  549. $items = Order::where('type', 1);
  550. $date = $request->input('date');
  551. if($date) {
  552. $start_month = Carbon::createFromTimeString($date . ' 00:00:00')->toDateTimeString();
  553. $end_month = Carbon::createFromTimeString($date . ' 00:00:00')->addMonth()->toDateTimeString();
  554. $items= $items->where([
  555. ['created_at', '>=', $start_month],
  556. ['created_at', '<', $end_month]
  557. ]);
  558. }
  559. $orderBy = $request->input('orderBy');
  560. $items = $items->groupBy('project_id')->selectRaw('sum(money) as sum, project_id')->pluck('sum', 'project_id');
  561. $projects = Project::where('id','!=',1)->get();
  562. foreach ($projects as $project) {
  563. $project->money = 0;
  564. foreach ($items as $key => $val) {
  565. if($key == $project->id) {
  566. $project->money = $val / 100;
  567. break;
  568. }
  569. }
  570. }
  571. if($orderBy == 'asc') $projects = $projects->sortBy('money');
  572. else $projects = $projects->sortByDesc('money');
  573. $values = $projects->pluck('money');
  574. $names = $projects->pluck('name');
  575. return $this->success(['data' => compact('values', 'names')]);
  576. }
  577. }