| xqd
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Http\Controllers\Api\mini;
|
|
|
|
|
|
+use App\Models\Device;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderDevice;
|
|
|
use App\Models\Project;
|
| xqd
@@ -15,6 +16,14 @@ class DataController extends BaseController
|
|
|
|
|
|
protected $order_device;
|
|
|
|
|
|
+ protected $order_ids = [];
|
|
|
+
|
|
|
+ protected $project_ids = [];
|
|
|
+
|
|
|
+ protected $start_at;
|
|
|
+
|
|
|
+ protected $end_at;
|
|
|
+
|
|
|
public function __construct()
|
|
|
{
|
|
|
$this->order = new Order();
|
| xqd
@@ -51,18 +60,46 @@ class DataController extends BaseController
|
|
|
|
|
|
public function getDateInfo()
|
|
|
{
|
|
|
- $now = Carbon::now();
|
|
|
- $date = $now->year . '-' . $now->month;
|
|
|
- $min_date = Carbon::now()->subYears(1)->getTimestamp() * 1000;
|
|
|
- return $this->success(['data' => compact('date', 'min_date')]);
|
|
|
+ $max_date = Carbon::now()->toDateString();
|
|
|
+// $order = $this->order->orderBy('created_at', 'desc')->first();
|
|
|
+// $min_date = $order ? substr($order->created_at, 0, 10) : Carbon::now()->subYear()->toDateString();
|
|
|
+ $min_date = Carbon::now()->subYears(10)->toDateString();
|
|
|
+ $date = substr($max_date, 0, 7);
|
|
|
+ $start_date = $max_date;
|
|
|
+ $end_date = Carbon::now()->addMonth()->toDateString();
|
|
|
+ $start_date = substr($start_date, 0, 7);
|
|
|
+ $end_date = substr($end_date, 0, 7);
|
|
|
+ return $this->success(['data' => compact('date', 'min_date', 'max_date', 'start_date', 'end_date')]);
|
|
|
}
|
|
|
|
|
|
- public function getStat(Request $request)
|
|
|
+ public function getStartAt($date)
|
|
|
+ {
|
|
|
+ return strlen($date) <= 7 ? $date . '-01 00:00:00' : $date . ' 00:00:00';
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getEndAt($date)
|
|
|
+ {
|
|
|
+ return strlen($date) <= 7 ? Carbon::createFromTimeString($date . '-01 00:00:00')->addMonth(1)->toDateTimeString() : Carbon::createFromTimeString($date . ' 00:00:00')->addDay(1)->toDateTimeString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getOrders(Request $request)
|
|
|
+ {
|
|
|
+ $order_ids = $this->getOrderIds($request);
|
|
|
+
|
|
|
+ $start_at = $this->getStartAt($request->input('start_date'));
|
|
|
+ $end_at = $this->getEndAt($request->input('end_date'));
|
|
|
+ return $this->order->whereIn('id', $order_ids)->where([
|
|
|
+ ['type', '=', 1],
|
|
|
+ ['created_at', '>=', $start_at],
|
|
|
+ ['created_at', '<', $end_at]
|
|
|
+ ])->get();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getOrderIds(Request $request)
|
|
|
{
|
|
|
- $date = $request->input('date') ? $request->input('date') : '2020-1';
|
|
|
- $date_data = $this->parseDate($date);
|
|
|
- $date = Carbon::createFromDate($date_data['year'], $date_data['month'], $date_data['day'])->toDateTimeString();
|
|
|
$ids = $request->input('project_ids');
|
|
|
+ if(!$ids || count($ids) < 0) return false;
|
|
|
+ $this->project_ids = $ids;
|
|
|
$order_devices = $this->order_device->whereIn('project_id', $ids);
|
|
|
|
|
|
$in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
|
| xqd
@@ -72,46 +109,123 @@ class DataController extends BaseController
|
|
|
$item_ids = collect($request->input($item))->filter(function($id) {
|
|
|
return $id;
|
|
|
});
|
|
|
- if($item_ids->count() > 0) {
|
|
|
+ if($item_ids && $item_ids->count() > 0) {
|
|
|
$order_devices = $order_devices->whereIn($key_items[$key], $item_ids);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $order_ids = $order_devices->pluck('order_id')->unique();
|
|
|
- $orders = $this->order->whereIn('id', $order_ids)->get();
|
|
|
+ return $order_devices->pluck('order_id')->unique();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getStat(Request $request)
|
|
|
+ {
|
|
|
+ if(!$request->input('project_ids')) return $this->error(['msg' => '']);
|
|
|
+ if($request->input('chart_type') == 'pie') return $this->getPieData($request);
|
|
|
+ $orders = $this->getOrders($request);
|
|
|
+ if(!$orders) return $this->error(['msg' => '']);
|
|
|
$names = [];
|
|
|
$values = [];
|
|
|
// year|month
|
|
|
$type = $request->input('type') ? $request->input('type') : 'year';
|
|
|
$cnt = 0;
|
|
|
- $projects = Project::whereIn('id', $ids)->get();
|
|
|
+ $projects = Project::whereIn('id', $this->project_ids)->get();
|
|
|
|
|
|
foreach($orders as $item) {
|
|
|
if($this->inDate($names, $item->created_at, $type)) {
|
|
|
foreach ($projects as $key => $project) {
|
|
|
- $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + 1 : $values[$key][$cnt-1];
|
|
|
+ $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + ($item->money / 100) : $values[$key][$cnt-1];
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
$names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
|
|
|
foreach ($projects as $key => $project) {
|
|
|
- $values[$key][$cnt] = $project->id == $item->project_id ? 1 : 0;
|
|
|
+ $values[$key][$cnt] = $project->id == $item->project_id ? ($item->money / 100) : 0;
|
|
|
}
|
|
|
$cnt = $cnt + 1;
|
|
|
}
|
|
|
}
|
|
|
$legends = $projects->pluck('name');
|
|
|
+ return $this->success(['data' => compact('values', 'names', 'legends')]);
|
|
|
+ }
|
|
|
|
|
|
- $total_project = $orders->sum('money') / 100;
|
|
|
- $tmp = Carbon::createFromTimeString($date);
|
|
|
- if($tmp->month == 12) {
|
|
|
- $next_month = Carbon::createFromDate($tmp->year + 1, 1, 1)->toDateTimeString();
|
|
|
- } else {
|
|
|
- $next_month = Carbon::createFromDate($tmp->year, $tmp->month + 1, 1)->toDateTimeString();
|
|
|
+ public function getPieData(Request $request)
|
|
|
+ {
|
|
|
+ $order_ids = $this->getOrderIds($request);
|
|
|
+ if(!$order_ids) return $this->error(['msg' => '']);
|
|
|
+
|
|
|
+ $total = OrderDevice::whereIn('order_id', $order_ids)->count();
|
|
|
+ $devices = Device::all();
|
|
|
+ $data = [];
|
|
|
+ foreach($devices as $device) {
|
|
|
+ $tmp = OrderDevice::whereIn('order_id', $order_ids)->where('device_id', $device->id)->count();
|
|
|
+ $percent = $total > 0 ? round(($tmp / $total * 10000)) / 100 : '0';
|
|
|
+ array_push($data, ['name' => $device->name, 'value' => $percent]);
|
|
|
}
|
|
|
- $total_month = $orders->where('created_at', '<', $next_month)->sum('money') / 100;
|
|
|
- return $this->success(['data' => compact('values', 'names', 'legends', 'total_project', 'total_month')]);
|
|
|
+
|
|
|
+ $legends = $devices->pluck('name');
|
|
|
+ return $this->success(['data' => compact('data', 'legends')]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getDetailData(Request $request)
|
|
|
+ {
|
|
|
+ $order_ids = $this->getOrderIds($request);
|
|
|
+ if(!$order_ids) return $this->error(['msg' => '']);
|
|
|
+
|
|
|
+ $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
|
|
|
+ $devices = Device::all();
|
|
|
+
|
|
|
+ $dates = [];
|
|
|
+ // year|month
|
|
|
+ $type = $request->input('type') ? $request->input('type') : 'year';
|
|
|
+ $columns = [];
|
|
|
+ $date = '';
|
|
|
+ $values = [
|
|
|
+ ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
|
|
|
+ ];
|
|
|
+ foreach($order_devices as $item) {
|
|
|
+ if($this->inDate($dates, $item->created_at, $type)) {
|
|
|
+ foreach($devices as $key => $device) {
|
|
|
+ $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
|
|
|
+ $columns[$key] = $columns[$key] + $money;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
|
|
|
+ array_push($dates, $date);
|
|
|
+ if(count($columns) > 0) {
|
|
|
+ array_push($values, [
|
|
|
+ 'date' => $date,
|
|
|
+ 'total' => collect($columns)->sum() / 100,
|
|
|
+ 'data' => $columns
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ $columns = [];
|
|
|
+ foreach($devices as $device) {
|
|
|
+ $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
|
|
|
+ array_push($columns, $money);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(count($columns) > 0) {
|
|
|
+ array_push($values, [
|
|
|
+ 'date' => $date,
|
|
|
+ 'total' => collect($columns)->sum(),
|
|
|
+ 'data' => $columns
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ return $this->success(['data' => $values]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getTotalInfo(Request $request)
|
|
|
+ {
|
|
|
+ $start_at = $this->getStartAt($request->input('date'));
|
|
|
+ $end_at = $this->getStartAt($request->input('date'));
|
|
|
+ $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
|
|
|
+ $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
|
|
|
+ ['created_at', '>=', $start_at],
|
|
|
+ ['created_at', '<', $end_at]
|
|
|
+ ])->sum('money') / 100;
|
|
|
+ return $this->success(['data' => compact('total_money', 'month_money')]);
|
|
|
}
|
|
|
|
|
|
public function inDate($dates, $created_at, $type)
|