diff --git a/config/acl.yaml b/config/acl.yaml index 64b53ad1..04fac4bb 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..369c6632 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,10 @@ 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\File\UploadedFile; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; @@ -348,9 +351,147 @@ 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 + $this->processWarrantyFile($file, $em); + + return $this->render('warranty/upload.form.html.twig'); + } + + 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; + 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]); + + // new warranty + $warranty = new Warranty(); + + // clean the plate number + $cleaned_plate_number = Warranty::cleanPlateNumber($plate_number); + if ($cleaned_plate_number) + { + error_log("mogol plate number " . $cleaned_plate_number); + // continue processing + // check if purchase_date is empty. If so, ignore entry + $date_purchase = DateTime::createFromFormat('d-M-y', $purchase_date); + if ($date_purchase != false) + { + error_log ("mogol date purchase " . $purchase_date); + // 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 + error_log("mogol serial " . $serial); + $warranty->setSerial($serial) + ->setPlateNumber($cleaned_plate_number) + ->setFirstName($first_name) + ->setLastName($last_name) + ->setMobileNumber($mobile_number) + ->setDatePurchase($date_purchase); + + $em->persist($warranty); + $em->flush(); + } + } + + $row_num++; + } + } 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 %} + +
+
+
+

+ Warranty Upload +

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

+ Upload Warranty CSV File +

+
+
+
+
+
+
+ +
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+{% endblock %}