diff --git a/config/acl.yaml b/config/acl.yaml index 6da5b558..64540ecf 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -348,6 +348,10 @@ access_keys: label: Customer Source Report - id: report.hub.filter label: Hub Filter Report + - id: report.warranty.raffle + label: Warranty Raffle Report + - id: report.jo.raffle + label: JO Raffle Report - id: service label: Other Services diff --git a/config/routes/report.yaml b/config/routes/report.yaml index bb9096b9..0d969f76 100644 --- a/config/routes/report.yaml +++ b/config/routes/report.yaml @@ -147,3 +147,23 @@ 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] + +rep_jo_raffle_form: + path: /report/jo_raffle_report + controller: App\Controller\ReportController::joRaffleForm + methods: [GET] + +rep_jo_raffle_submit: + path: /report/jo_raffle_report + controller: App\Controller\ReportController::joRaffleSubmit + methods: [POST] diff --git a/config/services.yaml b/config/services.yaml index c130b986..961ac7b1 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -277,6 +277,11 @@ services: arguments: $em: "@doctrine.orm.entity_manager" + # warranty logger for raffle + App\Service\WarrantyRaffleLogger: + arguments: + $em: "@doctrine.orm.entity_manager" + # promo logger App\Service\PromoLogger: arguments: diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 3669b52b..6e4d9ed2 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -41,6 +41,7 @@ use App\Service\RiderTracker; use App\Service\MapTools; use App\Service\InventoryManager; use App\Service\RiderAssignmentHandlerInterface; +use App\Service\WarrantyRaffleLogger; use App\Service\WarrantyAPILogger; use App\Service\PromoLogger; use App\Service\HubSelector; @@ -3362,7 +3363,7 @@ class APIController extends Controller implements LoggedController return $clean_serial; } - public function warrantyCheck($serial, EntityManagerInterface $em, Request $req) + public function warrantyCheck($serial, EntityManagerInterface $em, Request $req, WarrantyRaffleLogger $raffle_logger) { // check required parameters and api key $required_params = []; @@ -3371,7 +3372,7 @@ class APIController extends Controller implements LoggedController return $res->getReturnResponse(); // check if warranty serial is there - $serial = $this->cleanSerial($serial); + $serial = $this->cleanSerial($serial); $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); $warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]); $batt = null; @@ -3386,6 +3387,24 @@ class APIController extends Controller implements LoggedController $today = new DateTime(); + $user_id = $req->query->get('api_key'); + $raffle_data = [ + 'user_id' => $user_id, + 'serial' => $serial, + 'warranty_id' => null, + 'action' => '', + 'bmodel_name' => '', + 'bsize_name' => '', + 'first_name' => '', + 'last_name' => '', + 'plate_number' => '', + 'contact_num' => '', + 'email' => '', + 'address' => '', + ]; + + $data_sent = []; + // if we have a warranty entry for the serial already if ($warr != null) { @@ -3445,6 +3464,16 @@ class APIController extends Controller implements LoggedController 'dealer_address' => $warr->getDealerAddress() ?? '', 'branch_code' => $warr->getDealerBranchCode() ?? '', ]; + + // set customer info and action for raffle log + $raffle_data['action'] = 'serial_check_customer'; + $raffle_data['first_name'] = $customer['first_name']; + $raffle_data['last_name'] = $customer['last_name']; + $raffle_data['plate_number'] = $customer['plate_number']; + $raffle_data['email'] = $customer['email']; + $raffle_data['contact_num'] = $customer['contact_num']; + $raffle_data['address'] = $customer['address']; + $raffle_data['warranty_id'] = $warr->getID(); } else { @@ -3467,6 +3496,9 @@ class APIController extends Controller implements LoggedController 'dealer_address' => '', 'branch_code' => '', ]; + + // set action for raffle log + $raffle_data['action'] = 'serial_check_not_customer'; } } else @@ -3490,6 +3522,9 @@ class APIController extends Controller implements LoggedController 'dealer_address' => '', 'branch_code' => '', ]; + + // set action for raffle log + $raffle_data['action'] = 'serial_check_customer'; } $sku = $warr_serial->getSKU(); @@ -3544,6 +3579,13 @@ class APIController extends Controller implements LoggedController $res->setData($data); + // set the rest of the raffle log entry + $raffle_data['bmodel_name'] = $battery['brand']; + $raffle_data['bsize_name'] = $battery['size']; + + // log the raffle log + $raffle_logger->logRaffleInfo($data_sent, $raffle_data); + return $res->getReturnResponse(); } @@ -3578,7 +3620,7 @@ class APIController extends Controller implements LoggedController } public function warrantyRegister($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, - TranslatorInterface $trans, WarrantyAPILogger $logger) + TranslatorInterface $trans, WarrantyRaffleLogger $raffle_logger, WarrantyAPILogger $logger) { // check required parameters and api key $required_params = [ @@ -3593,7 +3635,7 @@ class APIController extends Controller implements LoggedController $warr_card = $req->files->get('warr_card'); // normalize serial - $serial = $this->cleanSerial($serial); + $serial = $this->cleanSerial($serial); // $serial = trim(strtoupper($serial)); // process picture uploads @@ -3608,7 +3650,7 @@ class APIController extends Controller implements LoggedController 'last_name' => $req->request->get('last_name'), 'date_purchase' => $req->request->get('date_purchase'), ]; - $action = 'create'; + $action = 'create/update'; $source = WarrantySource::MOBILE; $res = $this->checkParamsAndKey($req, $em, $required_params); @@ -3623,7 +3665,7 @@ class APIController extends Controller implements LoggedController // update warranty $res = $this->updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, - $logger, $log_data, $user_id, $action, $source); + $logger, $log_data, $user_id, $action, $source, $raffle_logger); $em->flush(); @@ -3943,9 +3985,24 @@ class APIController extends Controller implements LoggedController } */ - protected function updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, - $logger, $log_data, $user_id, $action, $source) + protected function updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, $logger, $log_data, $user_id, $action, $source, $raffle_logger) { + // prepare raffle log entry + $raffle_data = [ + 'user_id' => $user_id, + 'serial' => $serial, + 'warranty_id' => null, + 'action' => '', + 'bmodel_name' => '', + 'bsize_name' => '', + 'first_name' => '', + 'last_name' => '', + 'plate_number' => '', + 'contact_num' => '', + 'email' => '', + 'address' => '', + ]; + // get serial $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); if ($warr_serial == null) @@ -3978,11 +4035,16 @@ class APIController extends Controller implements LoggedController { $res->setError(true) ->setErrorMessage('Warranty registered to a vehicle not in your list of vehicles.'); + // set action to update + $action = 'update'; $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); return $res; } $sms_msg = $trans->trans('warranty_update_confirm'); + + // update raffle data action + $raffle_data['action'] = 'warranty_update'; } else { @@ -3991,6 +4053,9 @@ class APIController extends Controller implements LoggedController // set warranty source $warr->setCreateSource($source); + + // update raffle data action + $raffle_data['action'] = 'warranty_create'; } // get sap battery @@ -4088,6 +4153,30 @@ class APIController extends Controller implements LoggedController // error_log('sending sms to - ' . $this->session->getPhoneNumber()); $rt->sendSMS($this->session->getPhoneNumber(), $trans->trans('message.battery_brand_allcaps'), $sms_msg); + // prepare the rest of the raffle log entry + $raffle_data['warranty_id'] = $warr->getID(); + $raffle_data['bmodel_name'] = $sap_bty->getBrand()->getName(); + $raffle_data['bsize_name'] = $sap_bty->getSize()->getName(); + $raffle_data['first_name'] = $req->request->get('first_name', ''); + $raffle_data['last_name'] = $req->request->get('last_name', ''); + $raffle_data['plate_number'] = $plate; + $raffle_data['contact_num'] = $req->request->get('contact_num', ''); + $raffle_data['email'] = $req->request->get('email', ''); + $raffle_data['address'] = $req->request->get('cust_address', ''); + + $data_sent = [ + 'plate_number' => $req->request->get('plate_number'), + 'first_name' => $req->request->get('first_name'), + 'last_name' => $req->request->get('last_name'), + 'date_purchase' => $req->request->get('date_purchase'), + 'address' => $req->request->get('cust_address', ''), + 'email' => $req->request->get('email', ''), + 'contact_num' => $req->request->get('contact_num', ''), + ]; + + // log raffle data + $raffle_logger->logRaffleInfo($data_sent, $raffle_data); + return $res; } diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index 51a874de..f3335a14 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -1222,6 +1222,121 @@ 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'); + } + + /** + * @Menu(selected="outlet_list") + */ + public function joRaffleForm() + { + $this->denyAccessUnlessGranted('report.jo.raffle', null, 'No access.'); + + return $this->render('report/jo-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; + } + + + public function joRaffleSubmit(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->getJORaffleData($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', // JO ID + 'Date', + 'Outlet', // Hub + 'Plate Number', + 'Warranty Class', + 'First Name', + 'Last Name', + 'Contact Number', + 'Address', + 'Email', + ]); + + foreach ($data as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'jo_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 +2713,174 @@ 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; + } + + protected function getJORaffleData($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 JOs that have been scheduled within the date range and are fulfilled + // and service type is battery sales + $jo_sql = 'SELECT jo.id AS jo_id, cv.warranty_code AS serial, jo.date_schedule AS date_schedule, + h.name AS hub_name, cv.plate_number AS plate_number, jo.warranty_class AS warranty_class, + c.first_name AS first_name, c.last_name AS last_name, c.phone_mobile AS mobile_number, + c.email AS email + FROM job_order jo, customer_vehicle cv, hub h, customer c + WHERE jo.cvehicle_id = cv.id + AND jo.customer_id = c.id + AND jo.hub_id = h.id + AND jo.status = :fulfilled + AND jo.service_type = :battery_sales + AND jo.date_schedule >= :date_start AND jo.date_schedule <= :date_end'; + $jo_stmt = $db->prepare($jo_sql); + $jo_stmt->bindValue('fulfilled', JOStatus::FULFILLED); + $jo_stmt->bindValue('battery_sales', ServiceType::BATTERY_REPLACEMENT_NEW); + $jo_stmt->bindValue('date_start', $new_date_start); + $jo_stmt->bindValue('date_end', $new_date_end); + + $jo_result = $jo_stmt->executeQuery(); + + $jo_data = []; + // go through rows + while($row = $jo_result->fetchAssociative()) + { + // need to get the battery ordered + $jo_id = $row['jo_id']; + + $b_sql = 'SELECT bmodel.name AS model_name, bsize.name AS size_name + FROM battery_model bmodel, battery_size bsize, battery b, + invoice i, invoice_item ii + WHERE b.model_id = bmodel.id + AND b.size_id = bsize.id + AND ii.invoice_id = i.id + AND ii.battery_id = b.id + AND i.job_order_id = :jo_id'; + + $b_stmt = $db->prepare($b_sql); + $b_stmt->bindValue('jo_id', $jo_id); + + $b_result = $b_stmt->executeQuery(); + + $b_data = []; + + $battery_name = ''; + while ($b_row = $b_result->fetchAssociative()) + { + $battery_name = $b_row['model_name'] . ' / ' . $b_row['size_name']; + } + + // get the date from the date schedule + $date_array = explode(' ' , $row['date_schedule']); + $date_schedule = $date_array[0]; + + $raffle_number = ''; + $address = ''; + + $jo_data[] = [ + 'raffle_num' => $raffle_number, + 'serial' => $row['serial'], + 'product_name' => $battery_name, + 'jo_id' => $jo_id, + 'date' => $date_schedule, + 'hub' => $row['hub_name'], + 'plate_number' => $row['plate_number'], + 'warranty_class' => $row['warranty_class'], + 'first_name' => $row['first_name'], + 'last_name' => $row['last_name'], + 'contact_number' => $row['mobile_number'], + 'address' => $address, + 'email' => $row['email'] + ]; + } + + return $jo_data; + } } diff --git a/src/Entity/WarrantyRaffleLog.php b/src/Entity/WarrantyRaffleLog.php new file mode 100644 index 00000000..00aba6dc --- /dev/null +++ b/src/Entity/WarrantyRaffleLog.php @@ -0,0 +1,295 @@ +date_create = new DateTime(); + $this->data_sent = []; + $this->serial = ''; + $this->api_user = ''; + $this->action = ''; + $this->batt_model_name = ''; + $this->batt_size_name = ''; + $this->first_name = ''; + $this->last_name = ''; + $this->plate_number = ''; + $this->contact_num = ''; + $this->email = ''; + $this->address = ''; + } + + public function getID() + { + return $this->id; + } + + public function setDateCreate(DateTime $date_create) + { + $this->date_create = $date_create; + return $this; + } + + public function getDateCreate() + { + return $this->date_create; + } + + public function setApiUser($api_user) + { + $this->api_user = $api_user; + return $this; + } + + public function getApiUser() + { + return $this->api_user; + } + + public function addDataSent($id, $value) + { + $this->data_sent[$id] = $value; + return $this; + } + + public function setDataSent($data_sent) + { + $this->data_sent = $data_sent; + return $this; + } + + public function getDataSent($id) + { + // return null if we don't have it + if (!isset($this->data_sent[$id])) + return null; + + return $this->data_sent[$id]; + } + + public function setSerial($serial) + { + $this->serial = $serial; + return $this; + } + + public function getSerial() + { + return $this->serial; + } + + public function setWarrantyID($warranty_id) + { + $this->warranty_id = $warranty_id; + return $this; + } + + public function getWarrantyID() + { + return $this->warranty_id; + } + + public function setAction($action) + { + $this->action = $action; + return $this; + } + + public function getAction() + { + return $this->action; + } + + public function setBattModelName($batt_model_name) + { + $this->batt_model_name = $batt_model_name; + return $this; + } + + public function getBattModelName() + { + return $this->batt_model_name; + } + + public function setBattSizeName($batt_size_name) + { + $this->batt_size_name = $batt_size_name; + return $this; + } + + public function getBattSizeName() + { + return $this->batt_size_name; + } + + public function setFirstName($first_name) + { + $this->first_name = $first_name; + return $this; + } + + public function getFirstName() + { + return $this->first_name; + } + + public function setLastName($last_name) + { + $this->last_name = $last_name; + return $this; + } + + public function getLastName() + { + return $this->last_name; + } + + public function setPlateNumber($plate_number) + { + $this->plate_number = $plate_number; + return $this; + } + + public function getPlateNumber() + { + return $this->plate_number; + } + + public function setContactNumber($contact_num) + { + $this->contact_num = $contact_num; + return $this; + } + + public function getContactNumber() + { + return $this->contact_num; + } + + public function setEmail($email) + { + $this->email = $email; + return $this; + } + + public function getEmail() + { + return $this->email; + } + + public function setAddress($address) + { + $this->cust_address = $address; + return $this; + } + + public function getAddress() + { + return $this->cust_address; + } +} + diff --git a/src/Service/WarrantyRaffleLogger.php b/src/Service/WarrantyRaffleLogger.php new file mode 100644 index 00000000..2ea7237c --- /dev/null +++ b/src/Service/WarrantyRaffleLogger.php @@ -0,0 +1,41 @@ +em = $em; + } + + public function logRaffleInfo($data_sent, $raffle_log_data) + { + $log_entry = new WarrantyRaffleLog(); + + $log_entry->setApiUser($raffle_log_data['user_id']) + ->setDataSent($data_sent) + ->setSerial($raffle_log_data['serial']) + ->setWarrantyID($raffle_log_data['warranty_id']) + ->setAction($raffle_log_data['action']) + ->setBattModelName($raffle_log_data['bmodel_name']) + ->setBattSizename($raffle_log_data['bsize_name']) + ->setFirstName($raffle_log_data['first_name']) + ->setLastName($raffle_log_data['last_name']) + ->setPlateNumber($raffle_log_data['plate_number']) + ->setContactNumber($raffle_log_data['contact_num']) + ->setEmail($raffle_log_data['email']) + ->setAddress($raffle_log_data['address']); + + $this->em->persist($log_entry); + $this->em->flush(); + + } +} + diff --git a/templates/base.html.twig b/templates/base.html.twig index ab5dfb77..50e72b20 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -182,6 +182,16 @@ +