From 0fb09f2e6dd705b546d03633f93fdd468d88fc1f Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Fri, 11 Jun 2021 00:53:02 +0800 Subject: [PATCH] Add recommended extra battery inventory feature #582 --- src/Controller/AnalyticsController.php | 69 ++++++++++++++++++- templates/analytics/forecast_submit.html.twig | 4 ++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/Controller/AnalyticsController.php b/src/Controller/AnalyticsController.php index d18b10bb..308ef2e6 100644 --- a/src/Controller/AnalyticsController.php +++ b/src/Controller/AnalyticsController.php @@ -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; } diff --git a/templates/analytics/forecast_submit.html.twig b/templates/analytics/forecast_submit.html.twig index e9370e76..14fb5197 100644 --- a/templates/analytics/forecast_submit.html.twig +++ b/templates/analytics/forecast_submit.html.twig @@ -98,6 +98,10 @@
+
+ Recommended Extra Battery Inventory - {{ hub.battery.mfg }} - {{ hub.battery.model }} - {{ hub.battery.size }} +
+