Merge branch '258-upload-warranty-csv' into 'master'

Resolve "Upload warranty csv"

Closes #258

See merge request jankstudio/resq!305
This commit is contained in:
Korina Cordero 2019-11-18 03:10:07 +00:00
commit 11b60c8655
5 changed files with 273 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,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)
{

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 %}