diff --git a/catalyst/api-bundle/Command/TestAPICommand.php b/catalyst/api-bundle/Command/TestAPICommand.php index 1eb46713..ee0b8992 100644 --- a/catalyst/api-bundle/Command/TestAPICommand.php +++ b/catalyst/api-bundle/Command/TestAPICommand.php @@ -88,9 +88,17 @@ class TestAPICommand extends Command ]; $api->post('/capi/warranties/' . $id . '/claim', $params); + // warranty cancel + $id = 86811; + $api->get('/capi/warranties/' . $id . '/cancel'); + // plate warranty $api->get('/capi/plates/' . $plate_num . '/warranties'); + // warranty delete + $id = 86811; + $api->post('/capi/warranties/' . $id . '/delete'); + // battery $api->get('/capi/battery_brands'); $api->get('/capi/battery_sizes'); diff --git a/config/api_acl.yaml b/config/api_acl.yaml index cbf1d4f4..b677f99d 100644 --- a/config/api_acl.yaml +++ b/config/api_acl.yaml @@ -14,6 +14,10 @@ access_keys: label: Claim - id: warranty.update label: Update + - id: warranty.cancel + label: Cancel + - id: warranty.delete + label: Delete - id: batterybrand label: Battery Brand Access acls: diff --git a/config/routes/capi.yaml b/config/routes/capi.yaml index 633ea6a3..d99fb182 100644 --- a/config/routes/capi.yaml +++ b/config/routes/capi.yaml @@ -90,6 +90,18 @@ capi_warranty_update: 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 # find customer vehicle by id diff --git a/config/routes/report.yaml b/config/routes/report.yaml index 40bba3f8..274a1268 100644 --- a/config/routes/report.yaml +++ b/config/routes/report.yaml @@ -33,7 +33,7 @@ rep_popapp_comp_form: controller: App\Controller\ReportController::popappComparisonForm methods: [GET] -rep_popapp_comp_submit: - path: /report/popapp_comparison - controller: App\Controller\ReportController::popappComparisonSubmit +rep_popapp_export_csv: + path: /report/popapp_export + controller: App\Controller\ReportController::popappExportCSV methods: [POST] diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index fde9d266..710ea886 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -404,4 +404,45 @@ class WarrantyController extends APIController 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.'); + } } diff --git a/src/Controller/ReportController.php b/src/Controller/ReportController.php index c92b071b..0c1ea33a 100644 --- a/src/Controller/ReportController.php +++ b/src/Controller/ReportController.php @@ -16,7 +16,9 @@ use App\Entity\MobileSession; use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; +use Doctrine\ORM\EntityManagerInterface; use Doctrine\DBAL\Connection; +use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -420,7 +422,7 @@ class ReportController extends Controller /** * @Menu(selected="outlet_list") */ - public function popappComparisonSubmit(Request $req) + /* public function popappComparisonSubmit(Request $req) { // retrieve temporary info for file $file = $req->files->get('csv_file'); @@ -437,9 +439,62 @@ class ReportController extends Controller $params['data'] = $data; 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 try @@ -452,69 +507,117 @@ class ReportController extends Controller } // loop through the rows - $serial_numbers = []; $row_num = 0; + $results = []; + while(($fields = fgetcsv($fh)) !== false) { - if ($row_num <= 2) + //error_log($row_num . ' ' . trim($fields[2])); + if ($row_num < 1) { $row_num++; continue; } - // get the serial numbers - $serial_numbers[] = trim($fields[2]); + // pre-populate the result array + $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 - $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) + foreach($results as $key => $data) { - //error_log($warranty->getPlateNumber()); - // 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(), - ]; + //error_log($results[$key]['model_size']); + // 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; } + } diff --git a/src/Controller/WarrantySearchController.php b/src/Controller/WarrantySearchController.php index f73d6267..17b3268f 100644 --- a/src/Controller/WarrantySearchController.php +++ b/src/Controller/WarrantySearchController.php @@ -34,7 +34,6 @@ class WarrantySearchController extends Controller $this->denyAccessUnlessGranted('warranty.search', null, 'No access.'); $serial = $req->query->get('battery_serial'); - $name = $req->query->get('owner_name'); $plate_num = $req->query->get('plate_num'); // find the warranty @@ -63,7 +62,6 @@ class WarrantySearchController extends Controller } $params['data'] = $res; $params['battery_serial'] = $serial; - $params['owner_name'] = $name; $params['plate_num'] = $plate_num; $params['mode'] = "results"; diff --git a/templates/report/popapp/form.html.twig b/templates/report/popapp/form.html.twig index f48ada3d..82384d08 100644 --- a/templates/report/popapp/form.html.twig +++ b/templates/report/popapp/form.html.twig @@ -29,7 +29,7 @@ -
+
@@ -38,7 +38,7 @@
- +
@@ -96,8 +96,8 @@ {{ result.plate_num|default('') }} {{ result.serial|default('') }} {{ result.warr_date_create|default('') }} - {{ result.warr_activation_status ? 'Active' : 'Inactive' }} - {{ result.has_mobile ? 'Yes' : 'No' }} + {{ result.warr_activation_status }} + {{ result.has_mobile }} {% endfor %} diff --git a/templates/warranty-search/form.html.twig b/templates/warranty-search/form.html.twig index e643d3a4..2e29b5de 100644 --- a/templates/warranty-search/form.html.twig +++ b/templates/warranty-search/form.html.twig @@ -35,19 +35,10 @@
-
- -
-
- - -