Merge branch '283-warranty-class-report' into 'master'

Resolve "Warranty Class Report"

Closes #283

See merge request jankstudio/resq!328
This commit is contained in:
Kendrick Chan 2019-11-28 05:45:37 +00:00
commit fbd96d1296
6 changed files with 262 additions and 1 deletions

View file

@ -296,6 +296,8 @@ access_keys:
label: Popapp Comparison Report
- id: report.meh.customer
label: RESQ MEH Customer Report
- id: report.warranty.class
label: Warranty Class Report
- id: service
label: Other Services

View file

@ -47,3 +47,13 @@ rep_resq_meh_export_csv:
path: /report/meh_customer_export
controller: App\Controller\ReportController::mehCustomerExportCSV
methods: [POST]
rep_warranty_class_form:
path: /report/warranty_class_report
controller: App\Controller\ReportController::warrantyClassForm
methods: [GET]
rep_warranty_class_export_csv:
path: /report/warranty_class_report
controller: App\Controller\ReportController::warrantyClassExportCSV
methods: [POST]

View file

@ -36,6 +36,8 @@ use DateTime;
class ReportController extends Controller
{
const PREREGISTER_PREFIX = '9';
/**
* @Menu(selected="outlet_list")
*/
@ -547,6 +549,71 @@ class ReportController extends Controller
return $resp;
}
/**
* @Menu(selected="outlet_list")
*/
public function warrantyClassForm()
{
$this->denyAccessUnlessGranted('report.warranty.class', null, 'No access.');
$params['mode'] = 'form';
return $this->render('report/warranty-class/form.html.twig', $params);
}
/**
* @Menu(selected="outlet_list")
*/
public function warrantyClassExportCSV(Request $req, EntityManagerInterface $em)
{
$data = $this->getWarrantyClassData($em);
$resp = new StreamedResponse();
$resp->setCallback(function() use ($data) {
// csv output
$csv_handle = fopen('php://output', 'w+');
fputcsv($csv_handle, [
'Customer Last Name',
'Customer First Name',
'Vehicle Manufacturer',
'Vehicle Make',
'Model Year',
'Vehicle Color',
'Warranty Serial',
'Warranty Class',
'Plate Number',
'Warranty Last Name',
'Warranty First Name',
'Warranty Mobile Number',
'Warranty Battery Model',
'Warranty Battery Size',
'Warranty SAP Battery',
'Warranty Status',
'Warranty Created',
'Warranty Purchased',
'Warranty Expiry Date',
'Warranty Claim Date',
'Warranty Claimed From',
'Warranty Privacy Policy',
'Is Warranty Activated?',
]);
foreach ($data as $row)
{
fputcsv($csv_handle, $row);
}
fclose($csv_handle);
});
$filename = 'warranty_class_report' . '.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 processPopappFile(UploadedFile $csv_file, EntityManagerInterface $em)
{
// attempt to open file
@ -723,6 +790,129 @@ class ReportController extends Controller
}
return $results;
}
protected function getWarrantyClassData(EntityManagerInterface $em)
{
$results = [];
// query preregistered ustomers using search term '%9'
$cust_query = $em->createQuery('select c from App\Entity\Customer c where c.phone_mobile like :search_mobile')
->setParameter('search_mobile', "%" . self::PREREGISTER_PREFIX);
$customers = $cust_query->iterate();
foreach($customers as $crow)
{
$cust = $crow[0];
//error_log('Processing customer ' . $cust->getID());
// get list of customer vehicles
$c_vehicles = $cust->getVehicles();
foreach($c_vehicles as $cv)
{
if (!empty($c_vehicles))
{
// find warranty for plate number
$clean_cv_plate = $this->cleanPlateNumber($cv->getPlateNumber());
$warranties = $em->getRepository(Warranty::class)->findBy(['plate_number' => $clean_cv_plate]);
foreach ($warranties as $warr)
{
//error_log('Found warranty for plate number ' . $warr->getPlateNumber());
// form the result row
$results[] = $this->formWarrantyClassResult($cust, $cv, $warr);
}
}
}
$em->clear();
}
return $results;
}
protected function cleanPlateNumber($plate)
{
// remove spaces and make upper case
return strtoupper(str_replace(' ', '', $plate));
}
protected function formWarrantyClassResult($cust, $cv, $warr)
{
$batt_model = '';
$batt_size = '';
$sap_batt = '';
$policy = '';
$date_purchased = '';
$date_expire = '';
$date_claim = '';
$create_date = $warr->getDateCreate();
$date_create = $create_date->format('d/M/y');
if ($warr->getDatePurchase() != null)
{
$p_date = $warr->getDatePurchase();
$date_purchased = $p_date->format('d/M/y');
}
if ($warr->getDateClaim() != null)
{
$c_date = $warr->getDateClaim();
$date_claim = $c_date->format('d/M/y');
}
if ($warr->getDateExpire() != null)
{
$e_date = $warr->getDateExpire();
$date_expire = $e_date->format('d/M/y');
}
if ($warr->getBatteryModel() != null)
{
$batt_model = $warr->getBatteryModel()->getName();
}
if ($warr->getBatterySize() != null)
{
$batt_size = $warr->getBatterySize()->getName();
}
if ($warr->getSAPBattery() != null)
{
$sap_batt = $warr->getSAPBattery()->getBrand()->getName();
}
if ($warr->getPrivacyPolicy() != null)
{
$policy = $warr->getPrivacyPolicy()->getName();
}
$data = [
'c_last_name' => $cust->getLastName(),
'c_first_name' => $cust->getFirstName(),
'manufacturer' => $cv->getVehicle()->getManufacturer()->getName(),
'make' => $cv->getVehicle()->getMake(),
'model_year' => $cv->getModelYear(),
'color' => $cv->getColor(),
'serial' => $warr->getSerial(),
'class' => $warr->getWarrantyClass(),
'plate_number' => $warr->getPlateNumber(),
'w_last_name' => $warr->getLastName(),
'w_first_name' => $warr->getFirstName(),
'w_mobile_num' => $warr->getMobileNumber(),
'w_batt_model' => $batt_model,
'w_batt_size' => $batt_size,
'w_sap_batt' => $sap_batt,
'w_status' => $warr->getStatus(),
'w_date_create' => $date_create,
'w_date_purchase' => $date_purchased,
'w_date_expire' => $date_expire,
'w_date_claim' => $date_claim,
'w_claimed_from' => $warr->getClaimedFrom(),
'w_privacy_policy' => $policy,
'w_activated' => ($warr->isActivated() ? 'Active' : 'Inactive'),
];
return $data;
}
}

View file

@ -14,7 +14,8 @@ use Exception;
* name="warranty",
* uniqueConstraints={
* @ORM\UniqueConstraint(columns={"serial"})
* }
* },
* indexes={@ORM\Index(name="plate_number_idx", columns={"plate_number"})})
* )
*/
class Warranty

View file

@ -165,6 +165,14 @@
RESQ MEH Customer Report
</span>
</a>
<a href="{{ url('rep_warranty_class_form') }}" class="m-menu__link">
<i class="m-menu__link-bullet m-menu__link-bullet--dot">
<span></span>
</i>
<span class="m-menu__link-text">
Warranty Class Report
</span>
</a>
</li>
</ul>
</li>

View file

@ -0,0 +1,50 @@
{% 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 Class 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">
Generate Warranty Class 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_warranty_class_export_csv') }}" enctype="multipart/form-data">
<div class="m-portlet__body">
<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">Export to CSV</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}