From 00c65d1e567746df5abe506c01d76ea1c4fc7325 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 19 Apr 2021 10:30:56 +0000 Subject: [PATCH] Add checking for empty sku. #551 --- src/Controller/APIController.php | 17 +-- .../CAPI/CustomerWarrantyController.php | 12 ++- src/Controller/CAPI/WarrantyController.php | 28 +++-- src/Controller/WarrantyController.php | 102 ++++++++++++------ templates/warranty/form.html.twig | 2 + 5 files changed, 112 insertions(+), 49 deletions(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index b03f057e..2d3a662e 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -2905,7 +2905,11 @@ class APIController extends Controller implements LoggedController } $sku = $warr_serial->getSKU(); - $batt = $em->getRepository(SAPBattery::class)->find($sku); + + // check if sku is null + $batt = null; + if ($sku != null) + $batt = $em->getRepository(SAPBattery::class)->find($sku); // TODO: put this in a config file $image_url = $req->getSchemeAndHttpHost() . '/battery/generic.png'; if ($batt != null) @@ -3060,15 +3064,12 @@ class APIController extends Controller implements LoggedController $sms_msg = $trans->trans('warranty_register_confirm'); } + // check if sku is null // get sap battery $sku = $warr_serial->getSKU(); - $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); - if ($sap_bty == null) - { - $res->setError(true) - ->setErrorMessage('Could not find battery entry for warranty.'); - return $res; - } + $sap_bty = null; + if ($sku != null) + $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); // default date purchase to today // NOTE: might need to change this later diff --git a/src/Controller/CAPI/CustomerWarrantyController.php b/src/Controller/CAPI/CustomerWarrantyController.php index 0ebc2c60..bd9c7be9 100644 --- a/src/Controller/CAPI/CustomerWarrantyController.php +++ b/src/Controller/CAPI/CustomerWarrantyController.php @@ -381,10 +381,16 @@ class CustomerWarrantyController extends APIController error_log('sap battery check'); // get sap battery $sku = $warr_serial->getSKU(); - $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); - if ($sap_bty == null) + $sap_bty = null; + + // check if sku is null + if ($sku != null) { - return new APIResponse(false, 'Could not find battery entry for warranty.'); + $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); + //if ($sap_bty == null) + //{ + // return new APIResponse(false, 'Could not find battery entry for warranty.'); + //} } // vehicle fetch diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index c444370c..d49e0374 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -199,10 +199,16 @@ class WarrantyController extends APIController 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.'); + // check if sku is blank + if ((empty($sku)) || ($sku == null)) + $batt = null; + else + { + // battery + $batt = $em->getRepository(SAPBattery::class)->find($sku); + if ($batt == null) + return new APIResponse(false, 'Invalid battery SKU.'); + } /* // battery model @@ -382,10 +388,16 @@ class WarrantyController extends APIController 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.'); + // check if sku is blank + if ((empty($sku)) || ($sku == null)) + $batt = null; + else + { + // battery + $batt = $em->getRepository(SAPBattery::class)->find($sku); + if ($batt == null) + return new APIResponse(false, 'Invalid battery SKU.'); + } $warr->setSerial($serial) ->setWarrantyClass($warr_class) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index b1f2442e..bca3290e 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -181,31 +181,52 @@ class WarrantyController extends Controller } // custom validation for battery model - $model = $em->getRepository(BatteryModel::class) - ->find($req->request->get('battery_model')); + // check if battery model is blank + $bmodel = $req->request->get('battery_model'); + if (!empty($bmodel)) + { + $model = $em->getRepository(BatteryModel::class) + ->find($req->request->get('battery_model')); - if (empty($model)) - $error_array['battery_model'] = 'Invalid model selected.'; + if (empty($model)) + $error_array['battery_model'] = 'Invalid model selected.'; + else + $obj->setBatteryModel($model); + } else - $obj->setBatteryModel($model); + $obj->setBatteryModel(null); // custom validation for battery size - $size = $em->getRepository(BatterySize::class) - ->find($req->request->get('battery_size')); + // check if battery size is blank + $bsize = $req->request->get('battery_size'); + if (!empty($bsize)) + { + $size = $em->getRepository(BatterySize::class) + ->find($req->request->get('battery_size')); - if (empty($size)) - $error_array['battery_size'] = 'Invalid size selected.'; + if (empty($size)) + $error_array['battery_size'] = 'Invalid size selected.'; + else + $obj->setBatterySize($size); + } else - $obj->setBatterySize($size); + $obj->setBatterySize(null); // custom validation for SAP battery - $sap = $em->getRepository(SAPBattery::class) - ->find($req->request->get('sap_battery')); + // check if sap battery is blank + $sap_battery = $req->request->get('sap_battery'); + if (!empty($sap_battery)) + { + $sap = $em->getRepository(SAPBattery::class) + ->find($req->request->get('sap_battery')); - if (empty($sap)) - $error_array['sap_battery'] = 'Invalid SAP battery selected.'; + if (empty($sap)) + $error_array['sap_battery'] = 'Invalid SAP battery selected.'; + else + $obj->setSAPBattery($sap); + } else - $obj->setSAPBattery($sap); + $obj->setSAPBattery(null); // validate $errors = $validator->validate($obj); @@ -303,31 +324,52 @@ class WarrantyController extends Controller } // custom validation for battery model - $model = $em->getRepository(BatteryModel::class) - ->find($req->request->get('battery_model')); + // check if battery model is blank + $bmodel = $req->request->get('battery_model'); + if (!empty($bmodel)) + { + $model = $em->getRepository(BatteryModel::class) + ->find($req->request->get('battery_model')); - if (empty($model)) - $error_array['battery_model'] = 'Invalid model selected.'; + if (empty($model)) + $error_array['battery_model'] = 'Invalid model selected.'; + else + $obj->setBatteryModel($model); + } else - $obj->setBatteryModel($model); + $obj->setBatteryModel(null); // custom validation for battery size - $size = $em->getRepository(BatterySize::class) - ->find($req->request->get('battery_size')); + // check if battery size is blank + $bsize = $req->request->get('battery_size'); + if (!empty($bsize)) + { + $size = $em->getRepository(BatterySize::class) + ->find($req->request->get('battery_size')); - if (empty($size)) - $error_array['battery_size'] = 'Invalid size selected.'; + if (empty($size)) + $error_array['battery_size'] = 'Invalid size selected.'; + else + $obj->setBatterySize($size); + } else - $obj->setBatterySize($size); + $obj->setBatterySize(null); // custom validation for SAP battery - $sap = $em->getRepository(SAPBattery::class) - ->find($req->request->get('sap_battery')); + // check if sap battery is blank + $sap_battery = $req->request->get('sap_battery'); + if (!empty($sap_battery)) + { + $sap = $em->getRepository(SAPBattery::class) + ->find($req->request->get('sap_battery')); - if (empty($sap)) - $error_array['sap_battery'] = 'Invalid SAP battery selected.'; + if (empty($sap)) + $error_array['sap_battery'] = 'Invalid SAP battery selected.'; + else + $obj->setSAPBattery($sap); + } else - $obj->setSAPBattery($sap); + $obj->setSAPBattery(null); // validate $errors = $validator->validate($obj); diff --git a/templates/warranty/form.html.twig b/templates/warranty/form.html.twig index 4af98004..a1210ca9 100644 --- a/templates/warranty/form.html.twig +++ b/templates/warranty/form.html.twig @@ -98,6 +98,7 @@ SAP Battery + {% for model in batt_models %} {% endfor %}