Add recommended extra battery inventory feature #582

This commit is contained in:
Kendrick Chan 2021-06-11 00:53:02 +08:00
parent da6b344bef
commit 0fb09f2e6d
2 changed files with 71 additions and 2 deletions

View file

@ -21,6 +21,7 @@ use DateInterval;
use App\Entity\JobOrder;
use App\Entity\Hub;
use App\Entity\Battery;
use App\Ramcar\ShiftSchedule;
@ -125,7 +126,7 @@ class AnalyticsController extends Controller
// TODO: populate the hour_shift array, depending on the shift selected
$hour_shifts = $this->populateHourShift($shift);
error_log(print_r($hour_shifts, true));
// error_log(print_r($hour_shifts, true));
// error_log(print_r($hub_list, true));
// $hub_list = [ 6, 4, 36, 7, 8, 126, 127, 18, 12, 9, 60, 10, 21, 135 ];
@ -195,7 +196,7 @@ class AnalyticsController extends Controller
// error_log(print_r($chart_all_weekdays, true));
error_log(print_r($sched_res, true));
// error_log(print_r($sched_res, true));
// agggregate weekday data
$i = 0;
@ -367,6 +368,22 @@ class AnalyticsController extends Controller
// get job order data (job orders within coverage area)
$jos = $this->generateJobOrderData($conn, $hub, $distance_limit, $date_start, $date_end, $time_start, $time_end);
// get most bought battery from these JOs
$batt_id = $this->getHubBattery($conn, $jos);
$batt = $em->getRepository(Battery::class)->find($batt_id);
if ($batt == null)
$batt_data = [
'mfg' => 'None',
'model' => 'None',
'size' => 'None',
];
else
$batt_data = [
'mfg' => $batt->getManufacturer()->getName(),
'model' => $batt->getModel()->getName(),
'size' => $batt->getSize()->getName(),
];
// initialize counters
$c_weekday = [];
$c_day = [];
@ -433,12 +450,57 @@ class AnalyticsController extends Controller
'data_year' => $chart_year,
// 'data_weekday' => $chart_weekday,
'c_weekday' => $c_weekday, // sending raw weekday data because we need to process overlaps
'battery' => $batt_data,
// TODO: refactor this pls
];
return $params;
}
protected function getHubBattery($conn, $jos)
{
// collect ids
$ids = [];
foreach ($jos as $jo)
{
$ids[] = $jo['id'];
}
// ideally we encode the ids, but right now we're assuming they'll be int
$in_text = implode(',', $ids);
// get all the batteries ordered in these JOs
$sql = 'select battery_id, count(*) as total from invoice i,invoice_item item where i.id = item.invoice_id and i.job_order_id in (' . $in_text . ') group by battery_id';
$stmt = $conn->prepare($sql);
$stmt->execute();
$batteries = $stmt->fetchAll();
// error_log(print_r($batteries, true));
// get the most ordered, skipping the null battery
$best_batt_id = 0;
$best_batt_count = 0;
foreach ($batteries as $batt)
{
if ($batt['battery_id'] == null)
continue;
// if current battery is better than our best
if ($best_batt_count < $batt['total'])
{
$best_batt_id = $batt['battery_id'];
$best_batt_count = $batt['total'];
}
}
error_log('BEST - ' . $best_batt_id . ' - ' . $best_batt_count);
return $best_batt_id;
}
protected function generateJobOrderData($conn, $hub, $distance_limit, DateTime $date_start, DateTime $date_end, $time_start, $time_end)
{
$hub_coord = $hub->getCoordinates();
@ -471,7 +533,10 @@ class AnalyticsController extends Controller
$stmt->execute();
$jos = $stmt->fetchAll();
/*
error_log(count($jos));
error_log(print_r($jos, true));
*/
return $jos;
}

View file

@ -98,6 +98,10 @@
<div id="month-all-weekday-chart-{{ hub.id }}" style="height: 400px;">
</div>
<div class="shift-table">
<b>Recommended Extra Battery Inventory</b> - {{ hub.battery.mfg }} - {{ hub.battery.model }} - {{ hub.battery.size }}
</div>
<table class="shift-table">
<thead>
<tr>