Skip to content

Instantly share code, notes, and snippets.

@tesarwijaya
Created July 31, 2017 14:52
Show Gist options
  • Save tesarwijaya/721d68503f97821cc03c5efc1306d42a to your computer and use it in GitHub Desktop.
Save tesarwijaya/721d68503f97821cc03c5efc1306d42a to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Locations;
use App\History;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use DateTime;
use DateInterval;
use DatePeriod;
/**
*
*/
class Statistics extends Controller
{
/**
* get statistic of single location today
* @param Request->manager_id required
* @param Request->location, all or location id
* @param Request->date, now or valid date format
* @param Request->date_from, null or valid date format
* @return Json Object
*/
public function locationStatistics(Request $request)
{
$this->validate($request, [
'manager_id' => 'required',
]);
$manager_id = $request->input('manager_id');
$locations = $request->input('location', 'all');
$date = $request->input('date', 'now');
$date_from = $request->input('date_from', null);
$histories = $this->collection($manager_id,
$locations == 'all' ? null : $locations,
$date == 'now' ? date('Y-m-d') : date('Y-m-d', strtotime($date)),
$date_from
)
->get();
$income = $histories->sum('biaya');
$bikes = $histories->where('tires', '<=', 3)->count();
$cars = $histories->where('tires', '>', 3)->count();
return response([
'date' => $date,
'date_from' => $date_from,
'income' => $income,
'bikes' => $bikes,
'cars' => $cars,
'row_selected' => $histories->count(),
'data' => $histories
]);
}
public function vehicleStatistics(Request $request)
{
$this->validate($request, [
'manager_id' => 'required',
]);
$manager_id = $request->input('manager_id');
$locations = $request->input('location', 'all');
$date = $request->input('date', 'now');
$date_from = $request->input('date_from', null);
if (is_null($date_from)) {
$histories = $this->collection($manager_id,
$locations == 'all' ? null : $locations,
$date == 'now' ? date('Y-m-d') : date('Y-m-d', strtotime($date))
)->get();
$parking = $histories->where('ended_at', null)->count();
$bikes = $histories->where('tires', '<=', '3')->count();
$cars = $histories->where('tires', '>', '3')->count();
$day = [['Motor', $bikes],
['Mobil', $cars]];
return response(['Date' => $date, 'total' => $histories->count(), 'parking' => $parking, 'day' => $day]);
} else {
$begin = new DateTime(date('Y-m-d', strtotime($date_from)));
if ($date == 'now') {
$end = new DateTime(date('Y-m-d'));
} else {
$end = new DateTime(date('Y-m-d', strtotime($date)));
}
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
$i = 0;
$bikesTotal = 0;
$carsTotal = 0;
$days = array();
foreach ($period as $date) {
$histories = $this->collection($manager_id,
$locations == 'all' ? null : $locations,
$date->format("Y-m-d")
)
->get();
$bikes = $histories->where('tires', '<=', '3')->count();
$cars = $histories->where('tires', '>', '3')->count();
$days[$i] = [$date->format('d D'), $bikes, $cars ];
$bikesTotal = $bikesTotal + $bikes;
$carsTotal = $carsTotal + $cars;
$i++;
}
return response(['begin' => $begin, 'end' => $end, 'bikes' => $bikesTotal, 'cars' => $carsTotal, 'days' => $days]);
}
}
private function collection($manager_id, $location_id = null, $date = null, $date_from = null)
{
$collections = DB::table('history')->select('history.*', 'vehicles_type.tires')
->join('locations', 'locations.id', '=', 'history.location_id')
->join('vehicles', 'history.vehicle_id', '=', 'vehicles.id')
->join('vehicles_type', 'vehicles.vehicle_type_id', '=', 'vehicles_type.id')
->where('locations.manager_id', $manager_id);
if (!is_null($location_id))
{
$collections->where('locations.id', $location_id);
}
if (!is_null($date_from) && !is_null($date))
{
$collections->whereDate('started_at', '>=', $date_from);
$collections->whereDate('started_at', '<=', $date);
}
if (!is_null($date)) {
$collections->whereDate('started_at', date('Y-m-d', strtotime($date)));
}
return $collections;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment