Add upload of csv file to ReportController. Add templates for upload. #248

This commit is contained in:
Korina Cordero 2019-08-08 08:33:20 +00:00
parent c695a1f9b2
commit a5e6a632f4
3 changed files with 137 additions and 0 deletions

View file

@ -31,4 +31,9 @@ rep_battery_conflict_submit:
rep_popapp_comp_form:
path: /report/popapp_comparison
controller: App\Controller\ReportController::popappComparisonForm
methods: [GET]
rep_popapp_comp_submit:
path: /report/popapp_comparison
controller: App\Controller\ReportController::popappComparisonSubmit
methods: [POST]

View file

@ -9,12 +9,16 @@ use App\Ramcar\JOStatus;
use App\Entity\JORejection;
use App\Entity\Battery;
use App\Entity\JobOrder;
use App\Entity\Warranty;
use App\Entity\CustomerVehicle;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\DBAL\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
@ -399,4 +403,77 @@ class ReportController extends Controller
return $resp;
}
/**
* @Menu(selected="outlet_list")
*/
public function popappComparisonForm()
{
$this->denyAccessUnlessGranted('report.popapp.comparison', null, 'No access.');
return $this->render('report/popapp/form.html.twig');
}
public function popappComparisonSubmit(Request $req)
{
// retrieve temporary info for file
$file = $req->files->get('csv_file');
// process the csv file
$this->processPopappFile($file);
// return response
return $this->json([
'success' => true,
]);
}
protected function processPopappFile(UploadedFile $csv_file)
{
// 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
$serial_numbers = [];
$row_num = 0;
while(($fields = fgetcsv($fh)) !== false)
{
if ($row_num <= 2)
{
$row_num++;
continue;
}
// get the serial numbers
$serial_numbers[] = trim($fields[2]);
}
// get the warranty for serial
$qb = $this->getDoctrine()
->getRepository(Warranty::class)
->createQueryBuilder('q');
$warranty_query = $qb->select('q')
->where('q.serial IN(:serials)')
->setParameter('serials', $serial_numbers, Connection::PARAM_STR_ARRAY);
$warr_results = $warranty_query->getQuery()->getResult();
// get the plate numbers
$plate_numbers = [];
foreach ($warr_results as $warranty)
{
$plate_numbers[] = $warranty->getPlateNumber();
// what if plate number is 0000 or NONE?
}
// search in customer vehicle using the plate numbers
}
}

View file

@ -0,0 +1,55 @@
{% 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">Popapp Comparison Report</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 Popapp 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('rep_popapp_comp_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">Submit</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script>
</script>
{% endblock %}