diff --git a/config/acl.yaml b/config/acl.yaml index f48850f6..c6100978 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -252,6 +252,8 @@ access_keys: label: Search - id: warranty.search label: Customer Battery Search + - id: warranty.upload + label: Warranty Upload - id: ticket label: Ticket Access diff --git a/config/menu.yaml b/config/menu.yaml index 92b29c2d..491e51aa 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -151,6 +151,10 @@ main_menu: acl: warranty.list label: Warranty parent: support + - id: warranty_upload + acl: warranty.upload + label: Warranty Upload + parent: support - id: service acl: service.menu diff --git a/config/routes/warranty.yaml b/config/routes/warranty.yaml index 86c3e9b9..3e049e54 100644 --- a/config/routes/warranty.yaml +++ b/config/routes/warranty.yaml @@ -28,3 +28,13 @@ warranty_update_submit: path: /warranties/{id} controller: App\Controller\WarrantyController::updateSubmit methods: [POST] + +warranty_upload: + path: /warranty/upload + controller: App\Controller\WarrantyController::uploadForm + methods: [GET] + +warranty_upload_submit: + path: /warranty/upload + controller: App\Controller\WarrantyController::uploadSubmit + methods: [POST] diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 0dc5a43e..9385d157 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Entity\Warranty; use App\Entity\SAPBattery; +use App\Entity\Battery; use App\Entity\BatteryModel; use App\Entity\BatterySize; @@ -11,8 +12,11 @@ use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; use Doctrine\ORM\Query; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\StreamedResponse; +use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -348,9 +352,209 @@ class WarrantyController extends Controller 'success' => 'Changes have been saved!' ]); } - } + /** + * @Menu(selected="warranty_list") + */ + public function uploadForm() + { + $this->denyAccessUnlessGranted('warranty.upload', null, 'No access.'); + + return $this->render('warranty/upload.form.html.twig'); + } + + /** + * @Menu(selected="warranty_list") + */ + public function uploadSubmit(Request $req, EntityManagerInterface $em) + { + // retrieve temporary info for file + $file = $req->files->get('csv_file'); + + // process the csv file + $inv_entries = $this->processWarrantyFile($file, $em); + + $resp = new StreamedResponse(); + $resp->setCallback(function() use($inv_entries) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Owner First Name', + 'Owner Last Name', + 'Owner Email', + 'Owner Address', + 'Owner Mobile', + 'Owner Telephone', + 'Vehicle Make', + 'Vehicle Model', + 'Vehicle Year', + 'Vehicle Plate Number', + 'Battery Serial Number', + 'Battery Sales Invoice', + 'Battery Date of Purchase', + 'Distributor Name', + 'Distributor Address', + 'Application Type ID', + 'Battery ID', + 'Ownership Type', + ]); + foreach ($inv_entries as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + $filename = 'invalid_warranties' . '.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 processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em) + { + // attempt to open file + try + { + $fh = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // loop through the rows + // 0 - Owner First Name + // 1 - Owner Last Name + // 2 - Owner Email + // 3 - Owner Address + // 4 - Owner Mobile + // 5 - Owner Telephone + // 6 - Vehicle Make + // 7 - Vehicle Model + // 8 - Vehicle Year + // 9 - Vehicle Plate Number + // 10 - Battery Serial Number + // 11 - Battery Sales Invoice + // 12 - Battery Date of Purchase + // 13 - Distributor Name + // 14 - Distributor Address + // 15 - Application Type ID + // 16 - Battery ID + // 17 - Ownership Type + + $row_num = 0; + $invalid_entries = []; + while (($fields = fgetcsv($fh)) !== false) + { + // start processing at row 1, not 0 + if ($row_num < 1) + { + $row_num++; + continue; + } + + // get the data + $first_name = trim($fields[0]); + $last_name = trim($fields[1]); + $mobile_number = trim($fields[4]); + $plate_number = trim($fields[9]); + $serial = trim($fields[10]); + $purchase_date = trim($fields[12]); + $battery_id = trim($fields[16]); + + // check if purchase_date or plate_number or serial is empty or if + // purchase date is valid + $date_purchase = DateTime::createFromFormat('d-M-y', $purchase_date); + + if (empty($purchase_date) || + empty($plate_number) || + empty($serial) || + ($date_purchase == false)) + { + // add to invalid_entries + $invalid_entries[] = [ + 'owner_first_name' => $first_name, + 'owner_last_name' => $last_name, + 'owner_email' => trim($fields[2]), + 'owner_address' => trim($fields[3]), + 'owner_mobile' => $mobile_number, + 'owner_telephone' => trim($fields[5]), + 'vehicle_make' => trim($fields[6]), + 'vehicle_model' => trim($fields[7]), + 'vehicle_year' => trim($fields[8]), + 'vehicle_plate_number' => $plate_number, + 'battery_serial_number' => $serial, + 'battery_sales_invoice' => trim($fields[11]), + 'battery_date_purchase' => $purchase_date, + 'distributor_name' => trim($fields[13]), + 'distributor_address' => trim($fields[14]), + 'application_type_id' => trim($fields[15]), + 'battery_id' => $battery_id, + 'ownership_typ' => trim($fields[17]), + ]; + } + else + { + + // new warranty + $warranty = new Warranty(); + + // get the battery purchased + // check battery first. If not found, check sap_battery + $battery = $em->getRepository(Battery::class)->find($battery_id); + if ($battery != null) + { + // get the battery model and battery size + $model_id = $battery->getModel()->getID(); + $size_id = $battery->getSize()->getID(); + + $bty_model = $em->getRepository(BatteryModel::class)->find($model_id); + $bty_size = $em->getRepository(BatterySize::class)->find($size_id); + + if ($bty_model != null) + { + $warranty->setBatteryModel($bty_model); + } + + if ($bty_size != null) + { + $warranty->setBatterySize($bty_size); + } + } + else + { + // find battery in sap_battery + $battery = $em->getRepository(SAPBattery::class)->find($battery_id); + if ($battery != null) + { + // battery is SAPBattery + $warranty->setSAPBattery($battery); + } + } + + // set and save values + $warranty->setSerial($serial) + ->setPlateNumber($plate_number) + ->setFirstName($first_name) + ->setLastName($last_name) + ->setMobileNumber($mobile_number) + ->setDatePurchase($date_purchase); + + $em->persist($warranty); + $em->flush(); + } + + $row_num++; + } + + return $invalid_entries; + } protected function fillDropdownParameters(&$params) { diff --git a/templates/warranty/upload.form.html.twig b/templates/warranty/upload.form.html.twig new file mode 100644 index 00000000..d778bf47 --- /dev/null +++ b/templates/warranty/upload.form.html.twig @@ -0,0 +1,52 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +