diff --git a/config/acl.yaml b/config/acl.yaml index a1152234..6655e53f 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -248,3 +248,5 @@ access_keys: label: Menu - id: report.reject label: Rejection Report + - id: report.battery.conflict + label: Battery Conflict Report diff --git a/config/routes/report.yaml b/config/routes/report.yaml index 483d7254..c8560798 100644 --- a/config/routes/report.yaml +++ b/config/routes/report.yaml @@ -17,3 +17,13 @@ rep_reject_detail_submit: path: /report/rejection_detail controller: App\Controller\ReportController::rejectDetailSubmit methods: [POST] + +rep_battery_conflict_form: + path: /report/battery_conflict + controller: App\Controller\ReportController::batteryConflictForm + methods: [GET] + +rep_battery_conflict_submit: + path: /report/battery_conflict + controller: App\Controller\ReportController::batteryConflictSubmit + methods: [POST] diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 7d302694..800474d7 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -5,8 +5,11 @@ namespace App\Controller; use App\Ramcar\BaseController; use App\Ramcar\JORejectionReason; use App\Ramcar\ServiceType; +use App\Ramcar\JOStatus; use App\Entity\JORejection; +use App\Entity\Battery; +use App\Entity\JobOrder; use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; @@ -260,4 +263,139 @@ class ReportController extends BaseController ]); */ } + + public function batteryConflictForm() + { + $this->denyAccessUnlessGranted('report.battery.conflict', null, 'No access.'); + + $params = $this->initParameters('outlet_list'); + + return $this->render('report/battery/batt_conflict_form.html.twig', $params); + } + + public function batteryConflictSubmit(Request $req) + { + $this->denyAccessUnlessGranted('report.battery.conflict', null, 'No access.'); + + // get job order query builder + $job_qb = $this->getDoctrine() + ->getRepository(JobOrder::class) + ->createQueryBuilder('jo'); + + $em = $this->getDoctrine()->getManager(); + + // get dates + $raw_date_start = $req->request->get('date_start'); + $raw_date_end = $req->request->get('date_end'); + + $date_start = DateTime::createFromFormat('m/d/Y', $raw_date_start); + $date_end = DateTime::createFromFormat('m/d/Y', $raw_date_end); + + // build query for job order + $jo_query = $job_qb->where('jo.date_create >= :start') + ->andWhere('jo.date_create <= :end') + ->andWhere('jo.status != :status') + ->setParameter('start', $date_start->format('Y-m-d') . ' 00:00:00') + ->setParameter('end', $date_end->format('Y-m-d') . ' 23:59:59') + ->setParameter('status', JOStatus::CANCELLED) + ->getQuery(); + + + // run queries + $jos = $jo_query->getResult(); + + $batteries = $em->getRepository(Battery::class)->findAll(); + + // create compatibility matrix for battery and vehicle + $comp_matrix = []; + foreach ($batteries as $batt) + { + $vehicles = $batt->getVehicles(); + foreach ($vehicles as $vehicle) + { + $comp_matrix[$batt->getID()][$vehicle->getID()] = true; + } + } + + // go through the job orders to find the conflicts + $results = []; + foreach ($jos as $jo) + { + $invoice_items = $jo->getInvoice()->getItems(); + foreach ($invoice_items as $item) + { + // check if the item is a battery + if ($item->getBattery() != null) + { + $batt_id = $item->getBattery()->getID(); + $vehicle_id = $jo->getCustomerVehicle()->getVehicle()->getID(); + if (isset($comp_matrix[$batt_id][$vehicle_id]) && $comp_matrix[$batt_id][$vehicle_id]) + { + continue; + } + else + { + // get the compatible batteries for the customer vehicle + $batteries = []; + foreach($jo->getCustomerVehicle()->getVehicle()->getBatteries() as $comp_batt) + { + //$batteries[] = [ + // 'mfg_name' => $comp_batt->getManufacturer()->getName(), + // 'model_name' => $comp_batt->getModel()->getName(), + // 'size_name' => $comp_batt->getSize()->getName(), + //]; + $batteries[] = $comp_batt->getManufacturer()->getName() . ' ' . + $comp_batt->getModel()->getName() . ' ' . + $comp_batt->getSize()->getName(); + } + + $results[] = [ + 'jo_id' => $jo->getID(), + 'jo_date_create' => $jo->getDateCreate()->format('m/d/Y H:i'), + 'cus_vehicle_manufacturer' => $jo->getCustomerVehicle()->getVehicle()->getManufacturer()->getName(), + 'cus_vehicle_make' => $jo->getCustomerVehicle()->getVehicle()->getMake(), + 'cus_vehicle_model' => $jo->getCustomerVehicle()->getModelYear(), + 'battery_model_ordered' => $item->getBattery()->getModel()->getName(), + 'battery_size_ordered' => $item->getBattery()->getSize()->getName(), + 'compatible_batt' => implode(', ', $batteries), + ]; + } + } + } + } + + $resp = new StreamedResponse(); + $resp->setCallback(function() use ($results) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Order #', + 'Order Date and Time', + 'Manufacturer', + 'Make', + 'Year', + 'Battery Model', + 'Battery Size', + 'Compatible Batteries' + ]); + foreach ($results as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'battery_conflict_' . $date_start->format('Ymd') . '_' . $date_end->format('Ymd') . '.csv'; + + $resp->setStatusCode(200); + $resp->headers->set('Content-Type', 'text/csv; charset=utf-8'); + $resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); + + return $resp; + + //return $this->json([ + // 'result' => $results, + //]); + } } diff --git a/templates/base.html.twig b/templates/base.html.twig index b6464785..e61acf78 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -128,6 +128,16 @@ + +
+
+
+

Battery Conflict Report

+
+
+
+ +
+ +
+
+
+
+
+
+ + + +

+ Select a date range +

+
+
+
+
+
+
+
+ +
+ +
+ +
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %}