diff --git a/config/routes/api.yaml b/config/routes/api.yaml index 6c66429d..5e04aea3 100644 --- a/config/routes/api.yaml +++ b/config/routes/api.yaml @@ -125,6 +125,11 @@ api_location_support: controller: App\Controller\APIController:locationSupport methods: [GET] +api_activate_warranty: + path: /api/activate_warranty + controller: App\Controller\APIController:activateWarranty + methods: [POST] + api_service_list: path: /api/services controller: App\Controller\APIController:listServices diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index d1bad313..a89e359f 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -38,6 +38,7 @@ use App\Entity\Promo; use App\Entity\Battery; use App\Entity\RiderRating; use App\Entity\JOEvent; +use App\Entity\Warranty; use App\Entity\Service; use App\Entity\Partner; use App\Entity\Review; @@ -676,6 +677,8 @@ class APIController extends Controller if ($cv->getWarrantyExpiration() != null) $wty_ex = $cv->getWarrantyExpiration()->format('Y-m-d'); + $warranty = $this->findWarranty($cv->getPlateNumber()); + $cv_list[] = [ 'cv_id' => $cv->getID(), 'mfg_id' => $cv->getVehicle()->getManufacturer()->getID(), @@ -691,6 +694,7 @@ class APIController extends Controller 'curr_batt_id' => $battery_id, 'is_motolite' => $cv->hasMotoliteBattery() ? 1 : 0, 'is_active' => $cv->isActive() ? 1 : 0, + 'warranty' => $warranty, ]; } @@ -738,6 +742,7 @@ class APIController extends Controller $batts = $vehicle->getBatteries(); foreach ($batts as $batt) { + // TODO: Add warranty_tnv to battery information $batt_list[] = [ 'id' => $batt->getID(), 'mfg_id' => $batt->getManufacturer()->getID(), @@ -1641,11 +1646,16 @@ class APIController extends Controller 'status' => $status, ]; - // customer vehicle + // customer vehicle and warranty $cv = $jo->getCustomerVehicle(); + + // get latest warranty using plate number + $warranty = $this->findWarranty($cv->getPlateNumber()); + $jo_data['customer_vehicle'] = [ 'id' => $cv->getID(), 'plate_number' => $cv->getPlateNumber(), + 'warranty' => $warranty, ]; // rider @@ -1806,6 +1816,110 @@ class APIController extends Controller return $res->getReturnResponse(); } + public function activateWarranty(Request $req) + { + $required_params = ['plate_number']; + $em = $this->getDoctrine()->getManager(); + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + $plate_number = $req->request->get('plate_number'); + + // find warranty using plate number + $warranty_results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $plate_number], + ['date_create' => 'desc']); + + // check if warranty_results is empty + if (empty($warranty_results)) + { + $res->setError(true) + ->setErrorMessage('No warranty found for plate number'); + return $res->getReturnResponse(); + } + + // get first entry + $warranty = current($warranty_results); + if ($warranty->isActivated()) + { + $res->setError(true) + ->setErrorMessage('Warranty already activated'); + return $res->getReturnResponse(); + } + + $warranty->setActivated(); + + $em->flush(); + + return $res->getReturnResponse(); + } + + protected function findWarranty($plate_number) + { + $em = $this->getDoctrine()->getManager(); + // NOTE: Modify the search for the latest warranty. This seems hacky. + // get latest warranty using plate number + $warranty_results = $em->getRepository(Warranty::class)->findBy(['plate_number' => $plate_number], + ['date_create' => 'desc']); + + // check if warranty_results is empty + if (empty($warranty_results)) + { + $res->setError(true) + ->setErrorMessage('No warranty found for plate number'); + return $res->getReturnResponse(); + } + + // get first entry + $warranty = current($warranty_results); + + // check for null values for battery and date claim and date expire + $batt_model = ''; + $batt_size = ''; + $sap_batt = ''; + $claim_date = ''; + $expiry_date = ''; + + if (!(is_null($warranty->getBatteryModel()))) { + $batt_model = $warranty->getBatteryModel()->getName(); + } + if (!(is_null($warranty->getBatterySize()))) { + $batt_size = $warranty->getBatterySize()->getName(); + } + if (!(is_null($warranty->getSAPBattery()))) { + $sap_batt = $warranty->getSAPBattery()->getID(); + } + if (!(is_null($warranty->getDateClaim()))) { + $claim_date = $warranty->getDateClaim()->format("d M Y g:i A"); + } + if (!(is_null($warranty->getDateExpire()))) { + $expiry_date = $warranty->getDateExpire()->format("d M Y g:i A"); + } + + $warr = []; + $warr[] = [ + 'id' => $warranty->getID(), + 'serial' => $warranty->getSerial(), + 'warranty_class' => $warranty->getWarrantyClass(), + 'plate_number' => $warranty->getPlateNumber(), + 'first_name' => $warranty->getFirstName(), + 'last_name' => $warranty->getLastName(), + 'mobile_number' => $warranty->getMobileNumber(), + 'battery_model' => $batt_model, + 'battery_size' => $batt_size, + 'sap_battery' => $sap_batt, + 'status' => $warranty->getStatus(), + 'date_create' => $warranty->getDateCreate()->format("d M Y g:i A"), + 'date_purchase' => $warranty->getDatePurchase()->format("d M Y g:i A"), + 'date_expire' => $expiry_date, + 'date_claim' => $claim_date, + 'claim_from' => $warranty->getClaimedFrom(), + 'is_activated' => $warranty->isActivated() ? 1 : 0, + ]; + + return $warr; + } + public function listServices(Request $req) { $required_params = []; @@ -1957,6 +2071,5 @@ class APIController extends Controller $res->setData($data); return $res->getReturnResponse(); - } } diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index 1c56a837..081201ea 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -61,6 +61,7 @@ class WarrantyController extends APIController 'date_create' => (string) $warr->getDateCreate()->format('YmdHis'), 'date_purchase' => (string) $warr->getDatePurchase()->format('Ymd'), 'date_expire' => (string) $warr->getDateExpire()->format('Ymd'), + 'flag_activated' => (boolean) $warr->isActivated(), ]; $date_claim = $warr->getDateClaim(); diff --git a/src/Entity/Warranty.php b/src/Entity/Warranty.php index c1e16d32..1cfa242c 100644 --- a/src/Entity/Warranty.php +++ b/src/Entity/Warranty.php @@ -121,12 +121,19 @@ class Warranty */ protected $claim_from; + // warranty activated + /** + * @ORM\Column(type="boolean") + */ + protected $flag_activated; + public function __construct() { $this->date_create = new DateTime(); $this->warranty_class = WarrantyClass::WTY_PRIVATE; $this->status = WarrantyStatus::ACTIVE; $this->date_claim = null; + $this->flag_activated = false; } public function getID() @@ -348,4 +355,15 @@ class Warranty { return $this->claim_from; } + + public function setActivated($flag_activated = true) + { + $this->flag_activated = $flag_activated; + return $this; + } + + public function isActivated() + { + return $this->flag_activated; + } } diff --git a/templates/search/form.html.twig b/templates/search/form.html.twig index 892ca1ed..e1231721 100644 --- a/templates/search/form.html.twig +++ b/templates/search/form.html.twig @@ -197,6 +197,7 @@ Expiry Date Serial Battery + Status @@ -210,6 +211,13 @@ {{ result.getDateExpire|default("")|date('d M Y') }} {{ result.getSerial|default("") }} {{ result.getSAPBattery.getID|default("") }} + + {% if result.isActivated == true %} + Activated + {% else %} + Not Activated + {% endif %} + {% endfor %} diff --git a/templates/warranty-search/form.html.twig b/templates/warranty-search/form.html.twig index 371ee1ea..e643d3a4 100644 --- a/templates/warranty-search/form.html.twig +++ b/templates/warranty-search/form.html.twig @@ -125,6 +125,16 @@ End of Warranty {{ result.getDateExpire|date("d M Y") }} + + Status + + {% if result.isActivated == true %} + Activated + {% else %} + Not Activated + {% endif %} + + {% endfor %}