607 lines
21 KiB
PHP
607 lines
21 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\CAPI;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
use Doctrine\ORM\Query;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
|
|
|
use Catalyst\APIBundle\Controller\APIController;
|
|
use Catalyst\APIBundle\Response\APIResponse;
|
|
|
|
use App\Service\RisingTideGateway;
|
|
use App\Service\WarrantyAPILogger;
|
|
use App\Service\CustomerGeneratedIdService;
|
|
|
|
use App\Entity\WarrantySerial;
|
|
use App\Entity\Warranty;
|
|
use App\Entity\BatteryModel;
|
|
use App\Entity\BatterySize;
|
|
use App\Entity\SAPBattery;
|
|
use App\Entity\SAPBatterySize;
|
|
use App\Entity\SAPBatteryBrand;
|
|
use App\Entity\PrivacyPolicy;
|
|
use App\Entity\Customer;
|
|
use App\Entity\CustomerVehicle;
|
|
use App\Entity\Vehicle;
|
|
|
|
use App\Ramcar\NameValue;
|
|
use App\Ramcar\WarrantyClass;
|
|
use App\Ramcar\WarrantyStatus;
|
|
use App\Ramcar\FuelType;
|
|
use App\Ramcar\VehicleStatusCondition;
|
|
use App\Ramcar\WarrantySource;
|
|
|
|
use DateTime;
|
|
|
|
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
|
|
|
// third party API
|
|
class CustomerWarrantyController extends APIController
|
|
{
|
|
protected $acl_gen;
|
|
|
|
public function __construct(ACLGenerator $acl_gen)
|
|
{
|
|
$this->acl_gen = $acl_gen;
|
|
}
|
|
|
|
protected function checkMissingParameters(Request $req, $params = [])
|
|
{
|
|
$missing = [];
|
|
|
|
// check if parameters are there
|
|
foreach ($params as $param)
|
|
{
|
|
if ($req->getMethod() == 'GET')
|
|
{
|
|
$check = $req->query->get($param);
|
|
if (empty($check))
|
|
$missing[] = $param;
|
|
}
|
|
else if ($req->getMethod() == 'POST')
|
|
{
|
|
$check = $req->request->get($param);
|
|
if (empty($check))
|
|
$missing[] = $param;
|
|
}
|
|
else
|
|
return $params;
|
|
}
|
|
|
|
return $missing;
|
|
}
|
|
|
|
protected function checkRequiredParams(Request $req, $params, $logger, $log_data, $user_id, $action, $source)
|
|
{
|
|
// check required parameters
|
|
$missing = $this->checkMissingParameters($req, $params);
|
|
if (count($missing) > 0)
|
|
{
|
|
$miss_string = implode(', ', $missing);
|
|
$logger->logWarrantyInfo($log_data, 'Missing parameter(s): ' . $miss_string, $user_id, $action, $source);
|
|
return new APIResponse(false, 'Missing parameter(s): ' . $miss_string);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
protected function cleanSerial($serial)
|
|
{
|
|
return trim(strtoupper($serial));
|
|
}
|
|
|
|
protected function cleanPlateNumber($plate_num)
|
|
{
|
|
return preg_replace('/\s+/', '', strtoupper($plate_num));
|
|
}
|
|
|
|
public function check($serial, EntityManagerInterface $em, Request $req, WarrantyAPILogger $logger)
|
|
{
|
|
$user_id = $_SERVER['HTTP_X_CATA_API_KEY'];
|
|
$log_data = [
|
|
'serial' => $serial,
|
|
];
|
|
$action = 'check';
|
|
// TODO: we need to modify this later.
|
|
$source = WarrantySource::CAPI;
|
|
|
|
// check required parameters
|
|
$required_params = [];
|
|
$res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action, $source);
|
|
if (!$res)
|
|
return $res;
|
|
|
|
error_log('check warranty serial');
|
|
|
|
// TODO: add logging for the other scenarios
|
|
// check if warranty serial is there
|
|
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
|
|
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
|
|
$batt = null;
|
|
$is_registered = false;
|
|
|
|
if ($warr_serial == null)
|
|
{
|
|
return new APIResponse(false, 'Invalid warranty serial code.');
|
|
}
|
|
|
|
$today = new DateTime();
|
|
|
|
// if we have a warranty entry for the serial already
|
|
if ($warr != null)
|
|
{
|
|
error_log('already have warranty.');
|
|
|
|
$warr_plate = $warr->getPlateNumber();
|
|
$is_registered = true;
|
|
|
|
// null mobile number should be blank string instead
|
|
if ($warr->getMobileNumber() == null)
|
|
$mobile_num = '';
|
|
else
|
|
$mobile_num = $warr->getMobileNumber();
|
|
|
|
// purchase date of customer
|
|
if ($warr->getDatePurchaseCustomer() != null)
|
|
$date_purchase_cust = $warr->getDatePurchaseCustomer()->format('Y-m-d');
|
|
else
|
|
$date_purchase_cust = $today->format('Y-m-d');
|
|
|
|
|
|
// invoice
|
|
if ($warr->getFileInvoice() != null)
|
|
$invoice_url = $req->getSchemeAndHttpHost() . '/warranty_uploads/' . $warr->getFileInvoice();
|
|
else
|
|
$invoice_url = '';
|
|
|
|
// warranty card
|
|
if ($warr->getFileWarrantyCard() != null)
|
|
$warr_card_url = $req->getSchemeAndHttpHost() . '/warranty_uploads/' . $warr->getFileWarrantyCard();
|
|
else
|
|
$warr_card_url = '';
|
|
|
|
// vehicle
|
|
if ($warr->getVehicle() != null)
|
|
{
|
|
$v = $warr->getVehicle();
|
|
$vmfg_id = $v->getManufacturer()->getID();
|
|
$vmake_id = $v->getID();
|
|
}
|
|
else
|
|
{
|
|
$vmfg_id = null;
|
|
$vmake_id = null;
|
|
}
|
|
|
|
// customer
|
|
$cust = $warr->getCustomer();
|
|
if ($cust != null)
|
|
{
|
|
$cust_exists = true;
|
|
$priv_promo = $cust->getPrivacyPromo();
|
|
}
|
|
else
|
|
{
|
|
$cust_exists = false;
|
|
$priv_promo = false;
|
|
}
|
|
|
|
$customer = [
|
|
'first_name' => $warr->getFirstName(),
|
|
'last_name' => $warr->getLastName(),
|
|
'mobile_number' => $mobile_num,
|
|
'plate_number' => $warr_plate,
|
|
'email' => $warr->getEmail(),
|
|
'contact_num' => $warr->getContactNumber(),
|
|
'address' => $warr->getCustomerAddress(),
|
|
'priv_promo' => $priv_promo,
|
|
'exists' => $cust_exists,
|
|
];
|
|
$other_data = [
|
|
'odometer' => $warr->getOdometer(),
|
|
'date_purchase' => $date_purchase_cust,
|
|
'invoice' => $invoice_url,
|
|
'warr_card' => $warr_card_url,
|
|
'vmfg_id' => $vmfg_id,
|
|
'vmake_id' => $vmake_id,
|
|
'vmodel' => $warr->getVehicleModelYear(),
|
|
'dealer_name' => $warr->getDealerName(),
|
|
'dealer_address' => $warr->getDealerAddress(),
|
|
'branch_code' => $warr->getDealerBranchCode(),
|
|
'province_id' => $warr->getProvinceID(),
|
|
'municipality_id' => $warr->getMunicipalityID(),
|
|
];
|
|
}
|
|
else
|
|
{
|
|
$customer = [
|
|
'first_name' => '',
|
|
'last_name' => '',
|
|
'mobile_number' => '',
|
|
'plate_number' => '',
|
|
'email' => '',
|
|
'contact_num' => '',
|
|
'address' => '',
|
|
'priv_promo' => false,
|
|
'exists' => false,
|
|
];
|
|
$other_data = [
|
|
'odometer' => 0,
|
|
'date_purchase' => $today->format('Y-m-d'),
|
|
'invoice' => '',
|
|
'warr_card' => '',
|
|
'vmfg_id' => null,
|
|
'vmake_id' => null,
|
|
'vmodel' => '',
|
|
'dealer_name' => '',
|
|
'dealer_address' => '',
|
|
'branch_code' => '',
|
|
'province_id' => '',
|
|
'municipality_id' => '',
|
|
];
|
|
}
|
|
|
|
$sku = $warr_serial->getSKU();
|
|
// check if sku is null
|
|
$batt = null;
|
|
$cat_name = '';
|
|
if ($sku != null)
|
|
$batt = $em->getRepository(SAPBattery::class)->find($sku);
|
|
else
|
|
{
|
|
// get the category name of the serial
|
|
$cat_name = $warr_serial->getMetaInfo('category_name');
|
|
}
|
|
|
|
// TODO: put this in a config file
|
|
$image_url = $req->getSchemeAndHttpHost() . '/battery/generic.png';
|
|
if ($batt != null)
|
|
{
|
|
$battery = [
|
|
'brand' => $batt->getBrand()->getName(),
|
|
'size' => $batt->getSize()->getName(),
|
|
'image_url' => $image_url,
|
|
];
|
|
}
|
|
else
|
|
{
|
|
$battery = [
|
|
'brand' => $cat_name,
|
|
'size' => '',
|
|
'image_url' => '',
|
|
];
|
|
}
|
|
|
|
// populate data
|
|
$data = [
|
|
'is_valid' => true,
|
|
'is_registered' => $is_registered,
|
|
'customer' => $customer,
|
|
'battery' => $battery,
|
|
'odometer' => $other_data['odometer'],
|
|
'invoice' => $other_data['invoice'],
|
|
'warr_card' => $other_data['warr_card'],
|
|
'date_purchase' => $other_data['date_purchase'],
|
|
'vmfg_id' => $other_data['vmfg_id'],
|
|
'vehicle_id' => $other_data['vmake_id'],
|
|
'vmodel' => $other_data['vmodel'],
|
|
'dealer_name' => $other_data['dealer_name'],
|
|
'dealer_address' => $other_data['dealer_address'],
|
|
'branch_code' => $other_data['branch_code'],
|
|
'province_id' => $other_data['province_id'],
|
|
'municipality_id' => $other_data['municipality_id'],
|
|
];
|
|
|
|
return new APIResponse(true, 'Warranty found.', $data);
|
|
}
|
|
|
|
|
|
public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans,
|
|
WarrantyAPILogger $logger, CustomerGeneratedIdService $cust_gen_id)
|
|
{
|
|
error_log('HERE - register');
|
|
|
|
// set up information for logging
|
|
// get user from header
|
|
$user_id = $_SERVER['HTTP_X_CATA_API_KEY'];
|
|
$log_data = [
|
|
'serial' => $serial,
|
|
'plate_number' => $req->request->get('plate_num'),
|
|
'first_name' => $req->request->get('first_name'),
|
|
'last_name' => $req->request->get('last_name'),
|
|
'vmake_id' => $req->request->get('vmake_id'),
|
|
'contact_number' => $req->request->get('contact_num'),
|
|
'email' => $req->request->get('email'),
|
|
'invoice' => $req->request->get('invoice'),
|
|
];
|
|
$action = 'create/update';
|
|
|
|
// get the api_user that made the call so that it gets added to the source
|
|
// source becomes CAPI_USER_<insert name of api user here>
|
|
$username = $this->getUser()->getName();
|
|
$source = 'CAPI_USER_' . $username;
|
|
|
|
// error_log('SOURCE: ' . $source);
|
|
|
|
// TODO: maybe add vmake_id? since warranty cannot be created with no vmake
|
|
// TODO: maybe also add mobile and email since customer creation won't let mobile and email be null
|
|
// check required parameters
|
|
$required_params = [
|
|
'first_name',
|
|
'last_name',
|
|
'plate_num'
|
|
];
|
|
|
|
$res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action, $source);
|
|
|
|
if (!$res)
|
|
return $res;
|
|
|
|
// file uploads
|
|
$invoice = $req->files->get('invoice');
|
|
$warr_card = $req->files->get('warr_card');
|
|
|
|
error_log('handling file uploads');
|
|
// process picture uploads
|
|
$upload_dir = $kernel->getProjectDir() . '/public/warranty_uploads';
|
|
$inv_filename = $this->handlePictureUpload($invoice, $upload_dir, $serial, 'invoice');
|
|
$wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard');
|
|
|
|
// do actual registering
|
|
$res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename,
|
|
$logger, $log_data, $user_id, $action, $source, $cust_gen_id);
|
|
|
|
// flush to db
|
|
$em->flush();
|
|
|
|
return $res;
|
|
|
|
return new APIResponse(true, 'Warranty registered.');
|
|
}
|
|
|
|
// TODO: move this to a service, since it's shared by all warranty updaters
|
|
protected function handlePictureUpload($file, $target_dir, $serial, $name)
|
|
{
|
|
error_log("handling $name upload");
|
|
// no file sent
|
|
if ($file == null)
|
|
{
|
|
error_log('no file');
|
|
return null;
|
|
}
|
|
|
|
// create target dir if it doesn't exist
|
|
if (!file_exists($target_dir))
|
|
{
|
|
if (!mkdir($target_dir, 0744, true))
|
|
{
|
|
error_log('failed to create folder for warranty pictures');
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// move file
|
|
$filename = $name . '.' . $file->getClientOriginalExtension();
|
|
$file->move($target_dir . '/' . $serial, $filename);
|
|
|
|
error_log("filename - $filename");
|
|
error_log($target_dir . '/' . $serial . '/' . $filename);
|
|
|
|
return $serial . '/' . $filename;
|
|
}
|
|
|
|
protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null,
|
|
$logger, $log_data, $user_id, $action, $source, $cust_gen_id)
|
|
{
|
|
$plate_num = $this->cleanPlateNumber($req->request->get('plate_num'));
|
|
|
|
error_log('warranty serial check');
|
|
// get serial
|
|
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
|
|
if ($warr_serial == null)
|
|
{
|
|
$logger->logWarrantyInfo($log_data, 'Invalid warranty serial code..', $user_id, $action, $source);
|
|
return new APIResponse(false, 'Invalid warranty serial code.');
|
|
}
|
|
|
|
// check if warranty exists already
|
|
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
|
|
|
|
// skip warranty if it already exists
|
|
$cust = null;
|
|
$sms_message = '';
|
|
if ($warr != null)
|
|
{
|
|
$warr_plate_num = $this->cleanPlateNumber($warr->getPlateNumber());
|
|
|
|
if ($plate_num != $warr_plate_num)
|
|
{
|
|
$logger->logWarrantyInfo($log_data, 'Plate number does not match vehicle registered to warranty.', $user_id, $action, $source);
|
|
return new APIResponse(false, 'Plate number does not match vehicle registered to warranty.');
|
|
}
|
|
|
|
// get customer
|
|
$cust = $warr->getCustomer();
|
|
|
|
$sms_message = $trans->trans('warranty_update_confirm');
|
|
}
|
|
else
|
|
{
|
|
$warr = new Warranty();
|
|
$sms_message = $trans->trans('warranty_register_confirm');
|
|
|
|
// set warranty's create source
|
|
$warr->setCreateSource($source);
|
|
}
|
|
|
|
error_log('sap battery check');
|
|
// get sap battery
|
|
$sku = $warr_serial->getSKU();
|
|
$sap_bty = null;
|
|
|
|
// check if sku is null
|
|
if ($sku != null)
|
|
{
|
|
$sap_bty = $em->getRepository(SAPBattery::class)->find($sku);
|
|
if ($sap_bty == null)
|
|
{
|
|
$logger->logWarrantyInfo($log_data, 'Cound not find battery entry for warranty.', $user_id, $action, $source);
|
|
return new APIResponse(false, 'Could not find battery entry for warranty.');
|
|
}
|
|
}
|
|
|
|
// vehicle fetch
|
|
$vmake_id = $req->request->get('vmake_id');
|
|
$vehicle = null;
|
|
if ($vmake_id != null)
|
|
{
|
|
$vehicle = $em->getRepository(Vehicle::class)->find($vmake_id);
|
|
if ($vehicle == null)
|
|
{
|
|
$logger->logWarrantyInfo($log_data, 'Could not find vehicle specified for warranty.', $user_id, $action, $source);
|
|
return new APIResponse(false, 'Could not find vehicle specified for warranty.');
|
|
}
|
|
}
|
|
|
|
error_log('date check');
|
|
// default date purchase to today
|
|
// NOTE: might need to change this later
|
|
$date_pur = new DateTime();
|
|
$date_pur_cust = new DateTime();
|
|
|
|
// get date purchase specified by customer
|
|
if (!empty($req->request->get('date_purchase')))
|
|
{
|
|
$date_pur_cust = DateTime::createFromFormat('Y-m-d', $req->request->get('date_purchase'));
|
|
if (!$date_pur_cust)
|
|
{
|
|
$logger->logWarrantyInfo($log_data, 'Invalid date format for date of purchase.', $user_id, $action, $source);
|
|
return new APIResponse(false, 'Invalid date format for date of purchase.');
|
|
}
|
|
}
|
|
|
|
// customer check
|
|
$priv_promo = $req->request->get('priv_promo', false);
|
|
if ($cust == null)
|
|
{
|
|
// if no customer yet, create one and fill in fields
|
|
$cust = new Customer();
|
|
$cust->setFirstName($req->request->get('first_name'))
|
|
->setLastName($req->request->get('last_name'))
|
|
->setEmail($req->request->get('email'))
|
|
->setCreateSource('web_warranty')
|
|
->setPrivacyPromo($priv_promo)
|
|
->setPhoneMobile($req->request->get('contact_num'))
|
|
->setCreateSource($source);
|
|
|
|
$em->persist($cust);
|
|
}
|
|
else
|
|
{
|
|
// NOTE: behaviour has been changed. we now save customer details too
|
|
$cust->setFirstName($req->request->get('first_name'))
|
|
->setLastName($req->request->get('last_name'))
|
|
->setEmail($req->request->get('email'))
|
|
->setPrivacyPromo($priv_promo)
|
|
->setPhoneMobile($req->request->get('contact_num'));
|
|
// only update privacy promo
|
|
$cust->setPrivacyPromo($priv_promo);
|
|
}
|
|
|
|
error_log('update entity / database');
|
|
// create or update warranty entry
|
|
$warr->setSerial($serial)
|
|
->setFirstName($req->request->get('first_name'))
|
|
->setLastName($req->request->get('last_name'))
|
|
->setEmail($req->request->get('email'))
|
|
->setPlateNumber($req->request->get('plate_num'))
|
|
// TODO: figure out how to compute date of purchase
|
|
->setDatePurchase($date_pur)
|
|
// TODO: set status
|
|
// ->setStatus()
|
|
// TODO: set battery model and size id
|
|
// ->setBatterySize()
|
|
// ->setBatteryModel()
|
|
->setSAPBattery($sap_bty)
|
|
// ->setMobileNumber(substr($this->session->getPhoneNumber(), 2))
|
|
->setActivated(true)
|
|
|
|
// files
|
|
->setFileInvoice($inv_filename)
|
|
->setFileWarrantyCard($wcard_filename)
|
|
|
|
// new fields
|
|
->setOdometer($req->request->get('odometer', 0))
|
|
->setDatePurchaseCustomer($date_pur_cust)
|
|
->setContactNumber($req->request->get('contact_num'))
|
|
->setCustomerAddress($req->request->get('cust_address'))
|
|
->setVehicle($vehicle)
|
|
->setVehicleModelYear($req->request->get('vmodel'))
|
|
->setDealerName($req->request->get('dealer_name'))
|
|
->setDealerAddress($req->request->get('dealer_address'))
|
|
->setDealerBranchCode($req->request->get('branch_code'))
|
|
->setCustomer($cust)
|
|
->setValidated(false)
|
|
|
|
// and more new fields
|
|
->setProvinceID($req->request->get('province_id'))
|
|
->setMunicipalityID($req->request->get('municipality_id'));
|
|
|
|
// TODO: check for date purchase and date expire
|
|
|
|
$em->persist($warr);
|
|
|
|
// need to check if it's a new customer or updated existing customer
|
|
// a new customer would not have a generated id while an existing one
|
|
// already have one.
|
|
if ($cust->getGeneratedId() == null)
|
|
{
|
|
// TODO: temporary fix on how to save customer with a generated id
|
|
// since we need to keep generating an id until we are sure that there
|
|
// are no duplicates for generated id
|
|
// when saving the customer. This is an additional check.
|
|
// This will keep generating an id until a unique id is generated
|
|
// and the customer entity can then be inserted
|
|
$cust_gen_id->saveCustomerWithGeneratedId($cust);
|
|
}
|
|
|
|
// TODO: check if we need to do anything else
|
|
$data = [];
|
|
|
|
// send sms confirmation
|
|
$this->sendSMSConfirmation($rt, $req->request->get('contact_num'), $sms_message);
|
|
|
|
$logger->logWarrantyInfo($log_data, '', $user_id, $action, $source);
|
|
|
|
$em->flush();
|
|
|
|
return new APIResponse(true, 'Warranty registered.', $data);
|
|
}
|
|
|
|
protected function sendSMSConfirmation($rt, $num, $message)
|
|
{
|
|
$clean_num = trim($num);
|
|
|
|
// check if number is valid
|
|
// number should have 11 to 12 characters
|
|
if (strlen($clean_num) < 11 || strlen($clean_num) > 12)
|
|
return false;
|
|
|
|
// check if numeric
|
|
if (!is_numeric($clean_num))
|
|
return false;
|
|
|
|
// number should begin with 0 or 6
|
|
if ($clean_num[0] != '0' && $clean_num[0] != '6')
|
|
return false;
|
|
|
|
error_log('sending sms to - ' . $clean_num);
|
|
|
|
$rt->sendSMS($clean_num, 'MOTOLITE', $message);
|
|
}
|
|
}
|