Add route and access for warranty upload. Add method to WarrantyController to handle the upload. Add template for warranty upload. #258

This commit is contained in:
Korina Cordero 2019-08-29 07:26:11 +00:00
parent c204fccc81
commit 33e0e6b41b
5 changed files with 210 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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]

View file

@ -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)
{

View file

@ -0,0 +1,52 @@
{% extends 'base.html.twig' %}
{% block body %}
<!-- BEGIN: Subheader -->
<div class="m-subheader">
<div class="d-flex align-items-center">
<div class="mr-auto">
<h3 class="m-subheader__title">
Warranty Upload
</h3>
</div>
</div>
</div>
<!-- END: Subheader -->
<div class="m-content">
<!--Begin::Section-->
<div class="row">
<div class="col-xl-6">
<div class="m-portlet m-portlet--mobile">
<div class="m-portlet__head">
<div class="m-portlet__head-caption">
<div class="m-portlet__head-title">
<span class="m-portlet__head-icon">
<i class="fa fa-upload"></i>
</span>
<h3 class="m-portlet__head-text">
Upload Warranty CSV File
</h3>
</div>
</div>
</div>
<form id="upload_form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ url('warranty_upload_submit') }}" enctype="multipart/form-data">
<div class="m-portlet__body">
<div class="form-group m-form__group row">
<input type="file" id="csv_file" name="csv_file" >
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}