Merge branch 'master' of gitlab.com:jankstudio/resq into 229-create-test-scripts

This commit is contained in:
Korina Cordero 2019-08-27 07:53:16 +00:00
commit becfad3075
9 changed files with 340 additions and 68 deletions

View file

@ -65,6 +65,21 @@ class TestAPICommand extends Command
// warranty find // warranty find
$api->get('/capi/warranties/' . $serial); $api->get('/capi/warranties/' . $serial);
// warranty update
$id = 86811;
$params = [
'serial' => $serial,
'plate_number' => $plate_num,
'warranty_class' => 'private',
'sku' => 'WMEB24CB-CPN00-LX',
'date_purchase' => '20181001',
'date_expire' => '20191001',
'first_name' => 'First',
'last_name' => 'Last',
'mobile_number' => '123456789111',
];
$api->post('/capi/warranties/'. $id, $params);
// warranty claim // warranty claim
$id = 86811; $id = 86811;
$serial = 'AJ34LJADR12134LKJL5'; $serial = 'AJ34LJADR12134LKJL5';
@ -73,9 +88,17 @@ class TestAPICommand extends Command
]; ];
$api->post('/capi/warranties/' . $id . '/claim', $params); $api->post('/capi/warranties/' . $id . '/claim', $params);
// warranty cancel
$id = 86811;
$api->get('/capi/warranties/' . $id . '/cancel');
// plate warranty // plate warranty
$api->get('/capi/plates/' . $plate_num . '/warranties'); $api->get('/capi/plates/' . $plate_num . '/warranties');
// warranty delete
$id = 86811;
$api->post('/capi/warranties/' . $id . '/delete');
// battery // battery
$api->get('/capi/battery_brands'); $api->get('/capi/battery_brands');
$api->get('/capi/battery_sizes'); $api->get('/capi/battery_sizes');

View file

@ -12,6 +12,12 @@ access_keys:
label: Register Battery label: Register Battery
- id: warranty.claim - id: warranty.claim
label: Claim label: Claim
- id: warranty.update
label: Update
- id: warranty.cancel
label: Cancel
- id: warranty.delete
label: Delete
- id: batterybrand - id: batterybrand
label: Battery Brand Access label: Battery Brand Access
acls: acls:

View file

@ -84,7 +84,23 @@ capi_warranty_get_all:
controller: App\Controller\CAPI\WarrantyController::getAll controller: App\Controller\CAPI\WarrantyController::getAll
methods: [GET] methods: [GET]
# edit warranty
capi_warranty_update:
path: /capi/warranties/{id}
controller: App\Controller\CAPI\WarrantyController::update
methods: [POST]
# cancel warranty
capi_warranty_cancel:
path: /capi/warranties/{id}/cancel
controller: App\Controller\CAPI\WarrantyController::cancel
methods: [GET]
# delete warranty
capi_warranty_delete:
path: /capi/warranties/{id}/delete
controller: App\Controller\CAPI\WarrantyController::delete
methods: [POST]
# customer vehicle api # customer vehicle api

View file

@ -33,7 +33,7 @@ rep_popapp_comp_form:
controller: App\Controller\ReportController::popappComparisonForm controller: App\Controller\ReportController::popappComparisonForm
methods: [GET] methods: [GET]
rep_popapp_comp_submit: rep_popapp_export_csv:
path: /report/popapp_comparison path: /report/popapp_export
controller: App\Controller\ReportController::popappComparisonSubmit controller: App\Controller\ReportController::popappExportCSV
methods: [POST] methods: [POST]

View file

@ -60,7 +60,6 @@ class WarrantyController extends APIController
'status' => (string) $warr->getStatus(), 'status' => (string) $warr->getStatus(),
'date_create' => (string) $warr->getDateCreate()->format('YmdHis'), 'date_create' => (string) $warr->getDateCreate()->format('YmdHis'),
'date_purchase' => (string) $warr->getDatePurchase()->format('Ymd'), 'date_purchase' => (string) $warr->getDatePurchase()->format('Ymd'),
'date_expire' => (string) $warr->getDateExpire()->format('Ymd'),
'flag_activated' => (boolean) $warr->isActivated(), 'flag_activated' => (boolean) $warr->isActivated(),
]; ];
@ -70,6 +69,12 @@ class WarrantyController extends APIController
else else
$data['date_claim'] = (string) $warr->getDateClaim()->format('Ymd'); $data['date_claim'] = (string) $warr->getDateClaim()->format('Ymd');
$date_expire = $warr->getDateExpire();
if ($date_expire == null)
$data['date_expire'] = null;
else
$data['date_expire'] = (string) $warr->getDateExpire()->format('Ymd');
return $data; return $data;
} }
@ -310,4 +315,134 @@ class WarrantyController extends APIController
return new APIResponse(true, 'Warranties loaded.', $data); return new APIResponse(true, 'Warranties loaded.', $data);
} }
public function update(Request $req, EntityManagerInterface $em, $id)
{
$this->denyAccessUnlessGranted('warranty.update', null, 'No access.');
// find warranty
$warr = $em->getRepository(Warranty::class)->find($id);
if ($warr == null)
return new APIResponse(false, 'No warranty found with that id.', null, 404);
// required parameters
$params = [
'serial',
'warranty_class',
'plate_number',
'date_expire',
'date_purchase',
'sku',
];
$msg = $this->checkRequiredParameters($req, $params);
error_log('msg - ' . $msg);
if ($msg)
return new APIResponse(false, $msg);
// TODO: refactor this since this snippet is the same for register
$serial = $req->request->get('serial');
$date_expire_string = $req->request->get('date_expire');
$date_pur_string = $req->request->get('date_purchase');
$warr_class = $req->request->get('warranty_class');
$plate = $req->request->get('plate_number');
$sku = $req->request->get('sku');
$fname = $req->request->get('first_name', null);
$lname = $req->request->get('last_name', null);
$mnum = $req->request->get('mobile_number', null);
// wrong date expire format
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
if ($date_expire === false)
return new APIResponse(false, 'Wrong date format: date_expire.');
// wrong date purchase format
$date_pur = DateTime::createFromFormat('Ymd', $date_pur_string);
if ($date_pur === false)
return new APIResponse(false, 'Wrong date format: date_purchase.');
// valid warranty class
if (!WarrantyClass::validate($warr_class))
return new APIResponse(false, 'Invalid warranty class.');
// plate number
$plate = Warranty::cleanPlateNumber($plate);
if (!$plate)
return new APIResponse(false, 'Invalid plate number.');
// battery
$batt = $em->getRepository(SAPBattery::class)->find($sku);
if ($batt == null)
return new APIResponse(false, 'Invalid battery SKU.');
$warr->setSerial($serial)
->setWarrantyClass($warr_class)
->setPlateNumber($plate)
->setFirstName($fname)
->setLastName($lname)
->setMobileNumber($mnum)
->setSAPBattery($batt)
->setDatePurchase($date_pur)
->setDateClaim(null)
->setDateExpire($date_expire);
try
{
$em->persist($warr);
$em->flush();
}
catch (UniqueConstraintViolationException $e)
{
return new APIResponse(false, 'Duplicate serial encountered.');
}
// data
$data = [
'warranty' => $this->generateWarrantyData($warr),
];
return new APIResponse(true, 'Warranty updated.', $data);
}
public function cancel(Request $req, EntityManagerInterface $em, $id)
{
$this->denyAccessUnlessGranted('warranty.cancel', null, 'No access.');
// find warranty
$warr = $em->getRepository(Warranty::class)->find($id);
if ($warr == null)
return new APIResponse(false, 'No warranty found with that id.', null, 404);
if ($warr->getStatus() == WarrantyStatus::CANCELLED)
{
return new APIResponse(false, 'Warranty already cancelled.');
}
// set status to cancelled
$warr->setStatus(WarrantyStatus::CANCELLED);
$em->persist($warr);
$em->flush();
return new APIResponse(true, 'Warranty cancelled successfully.');
}
public function delete(EntityManagerInterface $em, $id)
{
$this->denyAccessUnlessGranted('warranty.delete', null, 'No access.');
// find warranty
$warr = $em->getRepository(Warranty::class)->find($id);
if ($warr == null)
{
return new APIResponse(false, 'No warranty found with that id.', null, 404);
}
// delete the warranty
$em->remove($warr);
$em->flush();
return new APIResponse(true, 'Warranty deleted successfully.');
}
} }

View file

@ -16,7 +16,9 @@ use App\Entity\MobileSession;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpFoundation\StreamedResponse;
@ -420,7 +422,7 @@ class ReportController extends Controller
/** /**
* @Menu(selected="outlet_list") * @Menu(selected="outlet_list")
*/ */
public function popappComparisonSubmit(Request $req) /* public function popappComparisonSubmit(Request $req)
{ {
// retrieve temporary info for file // retrieve temporary info for file
$file = $req->files->get('csv_file'); $file = $req->files->get('csv_file');
@ -437,9 +439,62 @@ class ReportController extends Controller
$params['data'] = $data; $params['data'] = $data;
return $this->render('report/popapp/form.html.twig', $params); return $this->render('report/popapp/form.html.twig', $params);
} */
/**
* @Menu(selected="outlet_list")
*/
public function popappExportCSV(Request $req, EntityManagerInterface $em)
{
// retrieve temporary info for file
$file = $req->files->get('csv_file');
// process the csv file
$data = $this->processPopappFile($file, $em);
$resp = new StreamedResponse();
$resp->setCallback(function() use ($data) {
// csv output
$csv_handle = fopen('php://output', 'w+');
fputcsv($csv_handle, [
'Size/Model',
'SKU',
'Serial Number',
'Created Date',
'Branch Name',
'Branch Code',
'Customer ID',
'Customer Last Name',
'Customer First Name',
'Customer Mobile Number',
'Warranty Last Name',
'Warranty First Name',
'Plate Number',
'Warranty Create Date',
'Activation Status',
'Has Mobile App?',
'Date Mobile App Downloaded',
'Mobile Number Using Mobile App',
]);
foreach ($data as $row)
{
fputcsv($csv_handle, $row);
}
fclose($csv_handle);
});
$filename = 'popapp_comparison_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) protected function processPopappFile(UploadedFile $csv_file, EntityManagerInterface $em)
{ {
// attempt to open file // attempt to open file
try try
@ -452,69 +507,117 @@ class ReportController extends Controller
} }
// loop through the rows // loop through the rows
$serial_numbers = [];
$row_num = 0; $row_num = 0;
$results = [];
while(($fields = fgetcsv($fh)) !== false) while(($fields = fgetcsv($fh)) !== false)
{ {
if ($row_num <= 2) //error_log($row_num . ' ' . trim($fields[2]));
if ($row_num < 1)
{ {
$row_num++; $row_num++;
continue; continue;
} }
// get the serial numbers // pre-populate the result array
$serial_numbers[] = trim($fields[2]); $results[] = [
'model_size' => trim($fields[0]),
'sku' => trim($fields[1]),
'serial' => trim($fields[2]),
'created_date' => trim($fields[3]),
'branch_name' => trim($fields[4]),
'branch_code' => trim($fields[5]),
'cust_id' => '',
'cust_lastname' => '',
'cust_firstname' => '',
'cust_mobile_number' => '',
'warr_lastname' => '',
'warr_firstname' => '',
'plate_num' => '',
'warr_date_create' => '',
'warr_activation_status' => '',
'has_mobile' => '',
'date_mobile' => '',
'mobile_number' => '',
];
} }
// get the warranty for serial foreach($results as $key => $data)
$warr_qb = $this->getDoctrine()
->getRepository(Warranty::class)
->createQueryBuilder('q');
$warranty_query = $warr_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
$results = [];
foreach ($warr_results as $warranty)
{ {
//error_log($warranty->getPlateNumber()); //error_log($results[$key]['model_size']);
// validate the plate number
$isValid = InvalidPlateNumber::isInvalid($warranty->getPlateNumber());
if ($isValid)
{
// get customer vehicle using plate number
$em = $this->getDoctrine()->getManager();
$cust_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $warranty->getPlateNumber()]);
foreach ($cust_vehicles as $cv)
{
// get customer info
// get mobile session of customer
//error_log($cv->getCustomer()->getLastName() . ' ' . $cv->getCustomer()->getFirstName());
$has_mobile = false;
$mobile_session = $em->getRepository(MobileSession::class)->findBy(['customer' => $cv->getCustomer()->getID()]);
foreach ($mobile_session as $mobile)
{
error_log($mobile->getID());
$has_mobile = true;
}
$results[] = [
'cust_id' => $cv->getCustomer()->getID(),
'cust_lastname' => $cv->getCustomer()->getLastName(),
'cust_firstname' => $cv->getCustomer()->getFirstName(),
'plate_num' => $cv->getPlateNumber(),
'serial' => $warranty->getSerial(),
'warr_date_create' => $warranty->getDateCreate()->format("d M Y"),
'has_mobile' => $has_mobile,
'warr_activation_status' => $warranty->isActivated(),
];
// get the serial
$serial = $results[$key]['serial'];
// if serial is empty, move to next element
if (!empty($serial))
{
// get the warranty for serial
$warr_qb = $this->getDoctrine()
->getRepository(Warranty::class)
->createQueryBuilder('q');
$warranty_query = $warr_qb->select('q')
->where('q.serial = :serial')
->setParameter('serial', $serial);
$warranty = $warranty_query->getQuery()->getOneOrNullResult();
if ($warranty != null)
{
$isValid = InvalidPlateNumber::isInvalid($warranty->getPlateNumber());
if ($isValid)
{
// get customer vehicles using plate number
$customer_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $warranty->getPlateNumber()]);
// check if customer vehicle is empty
if (count($customer_vehicles) != 0)
{
$has_mobile = false;
$mobile_date = '';
$mobile_number = '';
// get the first customer vehicle, store as best_cv until we find one with a mobile session
$best_cv = current($customer_vehicles);
foreach($customer_vehicles as $cv)
{
// get mobile session of customer
//error_log($cv->getCustomer()->getLastName() . ' ' . $cv->getCustomer()->getFirstName());
$mobile_session = $em->getRepository(MobileSession::class)
->findOneBy(['customer' => $cv->getCustomer()->getID()], ['date_generated' => 'ASC']);
if ($mobile_session != null)
{
// get mobile data
$has_mobile = true;
$mobile_date = $mobile_session->getDateGenerated()->format("d M Y");
$mobile_number = $mobile_session->getPhoneNumber();
// set best_cv to this customer vehicle with mobile session
$best_cv = $cv;
}
}
// set the customer data in results
$results[$key]['cust_id'] = $best_cv->getCustomer()->getID();
$results[$key]['cust_lastname'] = $best_cv->getCustomer()->getLastName();
$results[$key]['cust_firstname'] = $best_cv->getCustomer()->getFirstName();
$results[$key]['cust_mobile_number'] = $best_cv->getCustomer()->getPhoneMobile();
$results[$key]['plate_num'] = $best_cv->getPlateNumber();
$results[$key]['has_mobile'] = ($has_mobile ? 'Yes' : 'No');
$results[$key]['date_mobile'] = $mobile_date;
$results[$key]['mobile_number'] = $mobile_number;
}
}
// set the warranty data in results
$results[$key]['warr_lastname'] = $warranty->getLastName();
$results[$key]['warr_firstname'] = $warranty->getFirstName();
$results[$key]['warr_date_create'] = $warranty->getDateCreate()->format("d M Y");
$results[$key]['warr_activation_status'] = ($warranty->isActivated() ? 'Active' : 'Inactive');
} }
} }
} }
return $results; return $results;
} }
} }

View file

@ -34,7 +34,6 @@ class WarrantySearchController extends Controller
$this->denyAccessUnlessGranted('warranty.search', null, 'No access.'); $this->denyAccessUnlessGranted('warranty.search', null, 'No access.');
$serial = $req->query->get('battery_serial'); $serial = $req->query->get('battery_serial');
$name = $req->query->get('owner_name');
$plate_num = $req->query->get('plate_num'); $plate_num = $req->query->get('plate_num');
// find the warranty // find the warranty
@ -63,7 +62,6 @@ class WarrantySearchController extends Controller
} }
$params['data'] = $res; $params['data'] = $res;
$params['battery_serial'] = $serial; $params['battery_serial'] = $serial;
$params['owner_name'] = $name;
$params['plate_num'] = $plate_num; $params['plate_num'] = $plate_num;
$params['mode'] = "results"; $params['mode'] = "results";

View file

@ -29,7 +29,7 @@
</div> </div>
</div> </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"> <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_export_csv') }}" enctype="multipart/form-data">
<div class="m-portlet__body"> <div class="m-portlet__body">
<div class="form-group m-form__group row"> <div class="form-group m-form__group row">
<input type="file" id="csv_file" name="csv_file" > <input type="file" id="csv_file" name="csv_file" >
@ -38,7 +38,7 @@
<div class="m-form__actions m-form__actions--solid m-form__actions--right"> <div class="m-form__actions m-form__actions--solid m-form__actions--right">
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<button type="submit" class="btn btn-success">Submit</button> <button type="submit" class="btn btn-success">Export to CSV</button>
</div> </div>
</div> </div>
</div> </div>
@ -96,8 +96,8 @@
<td>{{ result.plate_num|default('') }}</td> <td>{{ result.plate_num|default('') }}</td>
<td>{{ result.serial|default('') }}</td> <td>{{ result.serial|default('') }}</td>
<td>{{ result.warr_date_create|default('') }}</td> <td>{{ result.warr_date_create|default('') }}</td>
<td>{{ result.warr_activation_status ? 'Active' : 'Inactive' }}</td> <td>{{ result.warr_activation_status }}</td>
<td>{{ result.has_mobile ? 'Yes' : 'No' }} </td> <td>{{ result.has_mobile }} </td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>

View file

@ -35,19 +35,10 @@
</div> </div>
<form id="warranty-search-form" autocomplete="off" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="get" action="{{ url('search_warranty') }}"> <form id="warranty-search-form" autocomplete="off" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="get" action="{{ url('search_warranty') }}">
<div class="m-portlet__body"> <div class="m-portlet__body">
<div class="form-group m-form__group row">
<label>
Please fill up at least two (2) of the fields below to view battery's warranty status.
</label>
</div>
<div class="form-group m-form__group row"> <div class="form-group m-form__group row">
<label data-field="battery_serial">Battery Serial Number</label> <label data-field="battery_serial">Battery Serial Number</label>
<input name="battery_serial" type="search"class="form-control" placeholder="xxxxxxxxxxxx" value="{{ battery_serial|default("") }}"> <input name="battery_serial" type="search"class="form-control" placeholder="xxxxxxxxxxxx" value="{{ battery_serial|default("") }}">
</div> </div>
<div class="form-group m-form__group row">
<label data-field="owner_name">Owner Name</label>
<input name="owner_name" type="search" class="form-control" placeholder="i.e. Juan Dela Cruz" value="{{ owner_name|default("") }}">
</div>
<div class="form-group m-form__group row"> <div class="form-group m-form__group row">
<label data-field="plate_num">Vehicle Plate Number</label> <label data-field="plate_num">Vehicle Plate Number</label>
<input name="plate_num" type="search" class="form-control" placeholder="i.e. ABC 123" value="{{ plate_num|default("") }}"> <input name="plate_num" type="search" class="form-control" placeholder="i.e. ABC 123" value="{{ plate_num|default("") }}">