Add customer and customer vehicle when warranty is registered via API. #285
This commit is contained in:
parent
ec5b8951da
commit
ced7292413
3 changed files with 166 additions and 3 deletions
|
|
@ -53,12 +53,18 @@ class TestAPICommand extends Command
|
||||||
'date_expire' => '20191001',
|
'date_expire' => '20191001',
|
||||||
'first_name' => 'First',
|
'first_name' => 'First',
|
||||||
'last_name' => 'Last',
|
'last_name' => 'Last',
|
||||||
'mobile_number' => '12345678910',
|
'mobile_number' => '09231234567',
|
||||||
];
|
];
|
||||||
$api->post('/capi/warranties', $params);
|
$api->post('/capi/warranties', $params);
|
||||||
|
|
||||||
// get all warranties
|
// get all warranties
|
||||||
$api->get('/capi/warranties');
|
$params = [
|
||||||
|
'order' => 'DESC',
|
||||||
|
'limit' => '5',
|
||||||
|
'start' => '1',
|
||||||
|
];
|
||||||
|
|
||||||
|
$api->get('/capi/warranties', $params);
|
||||||
|
|
||||||
|
|
||||||
// warranty find
|
// warranty find
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ parameters:
|
||||||
api_access_key: 'api_access_keys'
|
api_access_key: 'api_access_keys'
|
||||||
app_acl_file: 'acl.yaml'
|
app_acl_file: 'acl.yaml'
|
||||||
app_access_key: 'access_keys'
|
app_access_key: 'access_keys'
|
||||||
|
cvu_brand_id: "%env(CVU_BRAND_ID)%"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# default configuration for services in *this* file
|
# default configuration for services in *this* file
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,16 @@ use App\Entity\SAPBattery;
|
||||||
use App\Entity\SAPBatterySize;
|
use App\Entity\SAPBatterySize;
|
||||||
use App\Entity\SAPBatteryBrand;
|
use App\Entity\SAPBatteryBrand;
|
||||||
use App\Entity\PrivacyPolicy;
|
use App\Entity\PrivacyPolicy;
|
||||||
|
use App\Entity\Customer;
|
||||||
|
use App\Entity\CustomerVehicle;
|
||||||
|
use App\Entity\Vehicle;
|
||||||
|
|
||||||
use App\Ramcar\NameValue;
|
use App\Ramcar\NameValue;
|
||||||
use App\Ramcar\WarrantyClass;
|
use App\Ramcar\WarrantyClass;
|
||||||
use App\Ramcar\WarrantyStatus;
|
use App\Ramcar\WarrantyStatus;
|
||||||
|
use App\Ramcar\FuelType;
|
||||||
|
use App\Ramcar\VehicleStatusCondition;
|
||||||
|
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||||
|
|
@ -226,6 +232,9 @@ class WarrantyController extends APIController
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
$em->persist($warr);
|
$em->persist($warr);
|
||||||
|
|
||||||
|
$this->getCustomerFromMobile($em, $warr);
|
||||||
|
|
||||||
$em->flush();
|
$em->flush();
|
||||||
}
|
}
|
||||||
catch (UniqueConstraintViolationException $e)
|
catch (UniqueConstraintViolationException $e)
|
||||||
|
|
@ -488,9 +497,156 @@ class WarrantyController extends APIController
|
||||||
}
|
}
|
||||||
|
|
||||||
$em->persist($warr);
|
$em->persist($warr);
|
||||||
$em->flush();
|
|
||||||
|
|
||||||
return new APIResponse(true, 'Privacy policy for warranty set successfully.');
|
return new APIResponse(true, 'Privacy policy for warranty set successfully.');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getCustomerFromMobile($em, $warranty)
|
||||||
|
{
|
||||||
|
$w_mobile = $warranty->getMobileNumber();
|
||||||
|
if (empty($w_mobile))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set values for new customer vehicle
|
||||||
|
$w_plate_number = $this->cleanPlateNumber($warranty->getPlateNumber());
|
||||||
|
|
||||||
|
$cust_found = false;
|
||||||
|
|
||||||
|
$w_mobile_num = trim($w_mobile);
|
||||||
|
|
||||||
|
// does it fit our 09XXXXXXXXX pattern?
|
||||||
|
if (preg_match('/^09[0-9]{9}$/', $w_mobile_num))
|
||||||
|
{
|
||||||
|
// remove first '0'
|
||||||
|
$w_mobile_num = substr($w_mobile_num, 1);
|
||||||
|
error_log("CONVERTED TO $w_mobile_num");
|
||||||
|
}
|
||||||
|
|
||||||
|
// does it fit our 9XXXXXXXXX pattern?
|
||||||
|
if (!preg_match('/^9[0-9]{9}$/', $w_mobile_num))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
/*
|
||||||
|
// min length 2
|
||||||
|
// TODO: we need to check proper phone number format
|
||||||
|
// format should be '9XXXXXXXXX'
|
||||||
|
// TODO: if format doesn't fit and there's a 0 or 63 prefix, we should be able to detect and convert
|
||||||
|
if (strlen($w_mobile_num <= 2))
|
||||||
|
continue;
|
||||||
|
*/
|
||||||
|
|
||||||
|
$customers = $this->findCustomerByNumber($em, $w_mobile_num);
|
||||||
|
|
||||||
|
if (!empty($customers))
|
||||||
|
{
|
||||||
|
error_log('found customer for ' . $w_mobile_num);
|
||||||
|
foreach ($customers as $customer)
|
||||||
|
{
|
||||||
|
// get customer vehicles for customer
|
||||||
|
$c_vehicles = $customer->getVehicles();
|
||||||
|
|
||||||
|
$cv_found = false;
|
||||||
|
if (!empty($c_vehicles))
|
||||||
|
{
|
||||||
|
// check if plate number of customer vehicle matches warranty plate number
|
||||||
|
foreach ($c_vehicles as $c_vehicle)
|
||||||
|
{
|
||||||
|
$clean_cv_plate = $this->cleanPlateNumber($c_vehicle->getPlateNumber());
|
||||||
|
|
||||||
|
// check if it's already there
|
||||||
|
if ($clean_cv_plate == $w_plate_number)
|
||||||
|
{
|
||||||
|
// customer and customer vehicle already exists
|
||||||
|
$cv_found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// if there was a customer vehicle matched
|
||||||
|
if ($cv_found)
|
||||||
|
{
|
||||||
|
// vehicle found, do nothing.
|
||||||
|
error_log('vehicle found - ' . $w_plate_number);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// customer exists but not customer vehicle
|
||||||
|
// add customer vehicle to existing customer with unknown manufacturer and make
|
||||||
|
error_log('new vehicle - ' . $w_plate_number);
|
||||||
|
$this->createCustomerVehicle($em, $customer, $this->getDefaultVehicle($em), $w_plate_number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// customer not found
|
||||||
|
else
|
||||||
|
{
|
||||||
|
error_log('NEW customer and vehicle - ' . $w_plate_number);
|
||||||
|
// customer not found, add customer and customer vehicle
|
||||||
|
// get warranty first name, last name
|
||||||
|
$w_first_name = $warranty->getFirstName();
|
||||||
|
$w_last_name = $warranty->getLastName();
|
||||||
|
|
||||||
|
$new_cust = new Customer();
|
||||||
|
$new_cust->setFirstName($w_first_name)
|
||||||
|
->setLastName($w_last_name)
|
||||||
|
->setPhoneMobile($w_mobile_num);
|
||||||
|
|
||||||
|
$em->persist($new_cust);
|
||||||
|
|
||||||
|
$this->createCustomerVehicle($em, $new_cust, $this->getDefaultVehicle($em), $w_plate_number);
|
||||||
|
}
|
||||||
|
|
||||||
|
$em->flush();
|
||||||
|
$em->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getDefaultVehicle($em)
|
||||||
|
{
|
||||||
|
// get default vehicle
|
||||||
|
$cvu_brand_id = $this->getParameter('cvu_brand_id');
|
||||||
|
$default_vehicle = $em->getRepository(Vehicle::class)->find($cvu_brand_id);
|
||||||
|
if ($default_vehicle == null)
|
||||||
|
{
|
||||||
|
$output->writeln("Need to add vehicle with default values.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $default_vehicle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function cleanPlateNumber($plate)
|
||||||
|
{
|
||||||
|
// remove spaces and make upper case
|
||||||
|
return strtoupper(str_replace(' ', '', $plate));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function createCustomerVehicle($em, Customer $cust, $vehicle, $plate_number)
|
||||||
|
{
|
||||||
|
$new_cv = new CustomerVehicle();
|
||||||
|
|
||||||
|
$new_cv->setCustomer($cust)
|
||||||
|
->setPlateNumber($plate_number)
|
||||||
|
->setStatusCondition(VehicleStatusCondition::BRAND_NEW)
|
||||||
|
->setModelYear('')
|
||||||
|
->setColor('')
|
||||||
|
->setFuelType(FuelType::GAS)
|
||||||
|
->setHasMotoliteBattery(true)
|
||||||
|
->setVehicle($vehicle);
|
||||||
|
|
||||||
|
$em->persist($new_cv);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function findCustomerByNumber($em, $number)
|
||||||
|
{
|
||||||
|
$customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);
|
||||||
|
return $customers;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue