From 29468c629b730641aa25ee4366ae0bfd066e6981 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 14 Nov 2022 08:11:49 +0000 Subject: [PATCH] Add warranty raffle report. #717 --- config/acl.yaml | 2 + config/routes/report.yaml | 10 ++ src/Controller/ReportController.php | 141 ++++++++++++++++++ templates/base.html.twig | 8 + .../report/warranty-raffle/form.html.twig | 83 +++++++++++ 5 files changed, 244 insertions(+) create mode 100644 templates/report/warranty-raffle/form.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index 6da5b558..115d9f63 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -348,6 +348,8 @@ access_keys: label: Customer Source Report - id: report.hub.filter label: Hub Filter Report + - id: report.warranty.raffle + label: Warranty Raffle Report - id: service label: Other Services diff --git a/config/routes/report.yaml b/config/routes/report.yaml index bb9096b9..f6bd07b8 100644 --- a/config/routes/report.yaml +++ b/config/routes/report.yaml @@ -147,3 +147,13 @@ rep_hub_filter_submit: path: /report/hub_filter_report controller: App\Controller\ReportController::hubFilterSubmit methods: [POST] + +rep_warranty_raffle_form: + path: /report/warranty_raffle_report + controller: App\Controller\ReportController::warrantyRaffleForm + methods: [GET] + +rep_warranty_raffle_submit: + path: /report/warranty_raffle_report + controller: App\Controller\ReportController::warrantyRaffleSubmit + methods: [POST] diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 51a874de..2d311090 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -1222,6 +1222,64 @@ class ReportController extends Controller } + /** + * @Menu(selected="outlet_list") + */ + public function warrantyRaffleForm() + { + $this->denyAccessUnlessGranted('report.warranty.raffle', null, 'No access.'); + + return $this->render('report/warranty-raffle/form.html.twig'); + } + + public function warrantyRaffleSubmit(Request $req, EntityManagerInterface $em) + { + // 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); + + $data = $this->getWarrantyRaffleData($req, $em, $raw_date_start, $raw_date_end); + + $resp = new StreamedResponse(); + $resp->setCallback(function() use ($data) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Raffle Number', + 'Serial Number', + 'Product Name', + 'Transaction Number', // Warranty ID + 'Date', + 'Outlet', + 'Plate Number', + 'Warranty Class', + 'First Name', + 'Last Name', + 'Contact Number', + 'Address', + 'Email', + ]); + + foreach ($data as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'warranty_raffle_' . $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; + } + protected function processPopappFile(UploadedFile $csv_file, EntityManagerInterface $em) { // attempt to open file @@ -2598,4 +2656,87 @@ class ReportController extends Controller return $cust_ids; } + protected function getWarrantyRaffleData($req, $em, $raw_date_start, $raw_date_end) + { + $date_start = DateTime::createFromFormat('m/d/Y', $raw_date_start); + $date_end = DateTime::createFromFormat('m/d/Y', $raw_date_end); + + // change to this format: Y-m-d H:i:s + $new_date_start = $date_start->format('Y-m-d H:i:s') . ' 00:00:00'; + $new_date_end = $date_end->format('Y-m-d H:i:s') . ' 23:59:59'; + + $db = $em->getConnection(); + + // get the data from warranty_raffle_log + $wrl_sql = 'SELECT wrl.serial AS serial, wrl.warranty_id AS warranty_id, + wrl.batt_model_name AS model_name, wrl.batt_size_name AS size_name, + wrl.date_create AS date_create, wrl.plate_number AS plate_number, + wrl.first_name AS first_name, wrl.last_name AS last_name, + wrl.contact_num AS contact_num, wrl.address AS address, wrl.email AS email + FROM warranty_raffle_log wrl + WHERE wrl.date_create >= :date_start + AND wrl.date_create <= :date_end'; + + $wrl_stmt = $db->prepare($wrl_sql); + $wrl_stmt->bindValue('date_start', $new_date_start); + $wrl_stmt->bindValue('date_end', $new_date_end); + + $wrl_result = $wrl_stmt->executeQuery(); + + $wrl_data = []; + // go through rows + while($row = $wrl_result->fetchAssociative()) + { + // check if entry has a warranty id + $w_id = $row['warranty_id']; + $warranty_id = ''; + $warranty_class = ''; + if ($w_id != null) + { + $warranty_id = $w_id; + + // find the warranty to get the warranty class + $w_sql = 'SELECT w.warranty_class AS warranty_class + FROM warranty w + WHERE w.id = :warranty_id'; + + $w_stmt = $db->prepare($w_sql); + $w_stmt->bindValue('warranty_id' , $w_id); + + $w_result = $w_stmt->executeQuery(); + + while ($w_row = $w_result->fetchAssociative()) + { + $warranty_class = $w_row['warranty_class']; + } + } + + $raffle_number = ''; + $hub = ''; + $battery_name = $row['model_name'] . ' / ' . $row['size_name']; + + // get the date from the date schedule + $date_array = explode(' ' , $row['date_create']); + $date_create = $date_array[0]; + + $wrl_data[] = [ + 'raffle_num' => $raffle_number, + 'serial' => $row['serial'], + 'product_name' => $battery_name, + 'warranty_id' => $warranty_id, + 'date' => $date_create, + 'hub' => $hub, + 'plate_number' => $row['plate_number'], + 'warranty_class' => $warranty_class, + 'first_name' => $row['first_name'], + 'last_name' => $row['last_name'], + 'contact_number' => $row['contact_num'], + 'address' => $row['address'], + 'email' => $row['email'] + ]; + } + + return $wrl_data; + } + } diff --git a/templates/base.html.twig b/templates/base.html.twig index ab5dfb77..c289737d 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -257,6 +257,14 @@ Hub Filter Report + + + + + + Warranty Raffle Report + + diff --git a/templates/report/warranty-raffle/form.html.twig b/templates/report/warranty-raffle/form.html.twig new file mode 100644 index 00000000..f752ec81 --- /dev/null +++ b/templates/report/warranty-raffle/form.html.twig @@ -0,0 +1,83 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Warranty Raffle Report

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

+ Select a date range +

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