Add new customer app API logic #730
This commit is contained in:
parent
2a0ca3b9ea
commit
b3301fdbac
14 changed files with 4012 additions and 0 deletions
157
src/Controller/CustomerAppAPI/ApiController.php
Normal file
157
src/Controller/CustomerAppAPI/ApiController.php
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Dotenv\Dotenv;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Catalyst\ApiBundle\Controller\ApiController as BaseApiController;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Entity\MobileSession;
|
||||
use App\Entity\Warranty;
|
||||
use App\Entity\JobOrder;
|
||||
|
||||
class ApiController extends BaseApiController
|
||||
{
|
||||
protected $em;
|
||||
protected $session;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, KernelInterface $kernel)
|
||||
{
|
||||
$this->session = new MobileSession; // NOTE: original was null
|
||||
$this->em = $em;
|
||||
|
||||
// load env file
|
||||
$dotenv = new Dotenv();
|
||||
$dotenv->loadEnv($kernel->getProjectDir() . '.env');
|
||||
}
|
||||
|
||||
protected function debugRequest(Request $req)
|
||||
{
|
||||
$all = $req->request->all();
|
||||
error_log(print_r($all, true));
|
||||
}
|
||||
|
||||
protected function validateParams(Request $req, $params = [])
|
||||
{
|
||||
$missing = $this->checkRequiredParameters($req, $params);
|
||||
if ($missing) {
|
||||
return new ApiResponse(false, $missing, []);
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateSession($api_key)
|
||||
{
|
||||
// check if the session exists
|
||||
$session = $this->em->getRepository(MobileSession::class)->find($api_key);
|
||||
if ($session === null) {
|
||||
return new ApiResponse(false, 'Invalid API Key.');
|
||||
}
|
||||
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
protected function validateRequest(Request $req, $params = [])
|
||||
{
|
||||
$this->validateParams($req, $params);
|
||||
$this->validateSession($req->query->get('api_key'));
|
||||
}
|
||||
|
||||
protected function findWarranty($plate_number)
|
||||
{
|
||||
// NOTE: Modify the search for the latest warranty. This seems hacky.
|
||||
// get latest warranty using plate number
|
||||
$warranty_results = $this->em->getRepository(Warranty::class)->findBy(
|
||||
['plate_number' => $plate_number],
|
||||
['date_create' => 'desc']
|
||||
);
|
||||
|
||||
$warr = [];
|
||||
|
||||
// check if warranty_results is empty
|
||||
if (empty($warranty_results)) {
|
||||
/*
|
||||
$res->setError(true)
|
||||
->setErrorMessage('No warranty found for plate number');
|
||||
return $res->getReturnResponse();
|
||||
*/
|
||||
|
||||
return $warr;
|
||||
}
|
||||
|
||||
// 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");
|
||||
}
|
||||
if (!(is_null($warranty->getDateExpire()))) {
|
||||
$expiry_date = $warranty->getDateExpire()->format("d M Y");
|
||||
}
|
||||
|
||||
$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"),
|
||||
'date_expire' => $expiry_date,
|
||||
'date_claim' => $claim_date,
|
||||
'claim_from' => $warranty->getClaimedFrom(),
|
||||
'is_activated' => $warranty->isActivated() ? 1 : 0,
|
||||
];
|
||||
|
||||
return $warr;
|
||||
}
|
||||
|
||||
protected function getBatteryImageURL($req, $batt)
|
||||
{
|
||||
// TODO: workaround for now, we get static image of battery based on model name
|
||||
$filename = trim(strtolower($batt->getModel()->getName())) . '_mobile.jpg';
|
||||
$file_path = $req->getSchemeAndHttpHost() . $this->generateUrl('static_battery_image') . '/' . $filename;
|
||||
|
||||
return $file_path;
|
||||
}
|
||||
|
||||
protected function getOngoingJobOrders($cust)
|
||||
{
|
||||
$ongoing_jos = $this->em->getRepository(JobOrder::class)->findBy([
|
||||
'customer' => $cust,
|
||||
'status' => [JOStatus::PENDING, JOStatus::RIDER_ASSIGN, JOStatus::IN_TRANSIT, JOStatus::ASSIGNED, JOStatus::IN_PROGRESS],
|
||||
], ['date_schedule' => 'desc']);
|
||||
|
||||
return $ongoing_jos;
|
||||
}
|
||||
|
||||
protected function getGeoErrorMessage()
|
||||
{
|
||||
return 'Oops! Our service is limited to some areas in Metro Manila, Laguna, Cavite, Pampanga and Baguio only. We will update you as soon as we are able to cover your area';
|
||||
}
|
||||
}
|
||||
45
src/Controller/CustomerAppAPI/AppController.php
Normal file
45
src/Controller/CustomerAppAPI/AppController.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
class AppController extends ApiController
|
||||
{
|
||||
public function versionCheck(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateParams($req, [
|
||||
'version',
|
||||
]);
|
||||
|
||||
$need_update = false;
|
||||
$msg = 'Version is up to date.';
|
||||
|
||||
$api_version = $this->getParameter('api_version');
|
||||
|
||||
$app_version = $req->query->get('version');
|
||||
// putting this in for the future, in case we have diverging versions
|
||||
$os = $req->query->get('os');
|
||||
$platform = $req->query->get('platform');
|
||||
|
||||
$api_v = explode('.', $api_version);
|
||||
$app_v = explode('.', $app_version);
|
||||
|
||||
if ($api_v[0] < $app_v[0]) {
|
||||
return new ApiResponse(false, 'Invalid application version: ' . $app_version);
|
||||
}
|
||||
|
||||
if ($api_v[0] > $app_v[0]) {
|
||||
$need_update = true;
|
||||
$msg = 'Your version is outdated and needs an update to use the latest features RES-Q has to offer.';
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'need_update' => $need_update,
|
||||
'message' => $msg,
|
||||
]);
|
||||
}
|
||||
}
|
||||
229
src/Controller/CustomerAppAPI/AuthController.php
Normal file
229
src/Controller/CustomerAppAPI/AuthController.php
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use App\Entity\MobileSession;
|
||||
use App\Service\RisingTideGateway;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class AuthController extends ApiController
|
||||
{
|
||||
public function register(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateParams($req, [
|
||||
'phone_model',
|
||||
'os_type',
|
||||
'os_version',
|
||||
'phone_id',
|
||||
]);
|
||||
|
||||
// retry until we get a unique id
|
||||
while (true) {
|
||||
try {
|
||||
// instantiate session
|
||||
$sess = new MobileSession();
|
||||
$sess->setPhoneModel($req->request->get('phone_model'))
|
||||
->setOSType($req->request->get('os_type'))
|
||||
->setOSVersion($req->request->get('os_version'))
|
||||
->setPhoneID($req->request->get('phone_id'));
|
||||
|
||||
// reopen in case we get an exception
|
||||
if (!$this->em->isOpen()) {
|
||||
$this->em = $this->em->create(
|
||||
$this->em->getConnection(),
|
||||
$this->em->getConfiguration()
|
||||
);
|
||||
}
|
||||
|
||||
// save
|
||||
$this->em->persist($sess);
|
||||
$this->em->flush();
|
||||
} catch (DBALException $e) {
|
||||
error_log($e->getMessage());
|
||||
// delay one second and try again
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// return data
|
||||
return new ApiResponse(true, '', [
|
||||
'session_id' => $sess->getID(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function confirmNumber(RisingTideGateway $rt, Request $req, TranslatorInterface $translator)
|
||||
{
|
||||
// validate request
|
||||
$this->validateRequest($req, [
|
||||
'phone_number'
|
||||
]);
|
||||
|
||||
// phone number
|
||||
$phone_number = $req->request->get('phone_number');
|
||||
|
||||
// get otp_mode from .env
|
||||
$otp_mode = $_ENV['OTP_MODE'];
|
||||
|
||||
// check for hardcoded phone number for app store testing
|
||||
if ($phone_number == '639221111111') {
|
||||
$code = '123456';
|
||||
$this->session->setConfirmCode($code)
|
||||
->setPhoneNumber($phone_number);
|
||||
$this->em->flush();
|
||||
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
// check if otp_mode is test
|
||||
if ($otp_mode == 'test') {
|
||||
$code = '123456';
|
||||
$this->session->setConfirmCode($code)
|
||||
->setPhoneNumber($phone_number);
|
||||
$this->em->flush();
|
||||
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
// TODO: spam protection
|
||||
|
||||
// TODO: validate phone number
|
||||
|
||||
// generate code and save
|
||||
$code = $this->generateConfirmCode();
|
||||
$this->session->setConfirmCode($code)
|
||||
->setPhoneNumber($phone_number);
|
||||
$this->em->flush();
|
||||
|
||||
if ($otp_mode != 'test') {
|
||||
// send sms to number
|
||||
$this->sendConfirmationCode($rt, $phone_number, $code, $translator);
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
public function validateCode(Request $req)
|
||||
{
|
||||
// validate request
|
||||
$this->validateRequest($req, [
|
||||
'code'
|
||||
]);
|
||||
|
||||
// code is wrong
|
||||
$code = $req->request->get('code');
|
||||
if ($this->session->getConfirmCode() != $code) {
|
||||
return new ApiResponse(false, 'Wrong confirm code.');
|
||||
}
|
||||
|
||||
// set confirm date
|
||||
$date = new DateTime();
|
||||
$this->session->setDateConfirmed($date)
|
||||
->setConfirmed();
|
||||
|
||||
|
||||
// TODO: check if we have the number registered before and merge
|
||||
$dupe_sess = $this->findNumberSession($this->session->getPhoneNumber());
|
||||
if ($dupe_sess != null) {
|
||||
$dupe_cust = $dupe_sess->getCustomer();
|
||||
$this->session->setCustomer($dupe_cust);
|
||||
}
|
||||
|
||||
// TODO: check if mobile matches mobile of customer
|
||||
$customer = $this->findCustomerByNumber($this->session->getPhoneNumber());
|
||||
if ($customer != null) {
|
||||
// TODO: if there is a dupe_sess, do we need to check if
|
||||
// dupe_cust is the same as the customer we found?
|
||||
$this->session->setCustomer($customer);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
public function resendCode(Request $req, RisingTideGateway $rt, TranslatorInterface $translator)
|
||||
{
|
||||
// validate request
|
||||
$this->validateRequest($req);
|
||||
|
||||
// already confirmed
|
||||
if ($this->session->isConfirmed()) {
|
||||
return new ApiResponse(false, 'User is already confirmed.');
|
||||
}
|
||||
|
||||
// have sent code before
|
||||
if ($this->session->getDateCodeSent() != null) {
|
||||
return new ApiResponse(false, 'Can only send confirm code every 5 mins.');
|
||||
}
|
||||
|
||||
// TODO: send via sms
|
||||
$phone_number = $this->session->getPhoneNumber();
|
||||
$code = $this->session->getConfirmCode();
|
||||
$this->sendConfirmationCode($rt, $phone_number, $code, $translator);
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
protected function generateConfirmCode()
|
||||
{
|
||||
return sprintf("%06d", mt_rand(100000, 999999));
|
||||
}
|
||||
|
||||
protected function sendConfirmationCode(RisingTideGateway $rt, $phone_number, $code, TranslatorInterface $translator)
|
||||
{
|
||||
// send sms to number
|
||||
$message = $translator->trans('message.confirmation_code') . ' ' . $code;
|
||||
$rt->sendSMS($phone_number, $translator->trans('message.battery_brand_allcaps'), $message);
|
||||
}
|
||||
|
||||
// TODO: find session customer by phone number
|
||||
protected function findNumberSession($number)
|
||||
{
|
||||
$query = $this->em->getRepository(MobileSession::class)->createQueryBuilder('s')
|
||||
->where('s.phone_number = :number')
|
||||
->andWhere('s.customer is not null')
|
||||
->andWhere('s.confirm_flag = 1')
|
||||
->setParameter('number', $number)
|
||||
->setMaxResults(1)
|
||||
->getQuery();
|
||||
|
||||
// we just need one
|
||||
$res = $query->getOneOrNullResult();
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
protected function findCustomerByNumber($number)
|
||||
{
|
||||
$customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);
|
||||
|
||||
// find the customer with the most number of cars
|
||||
$car_count = 0;
|
||||
$cust = null;
|
||||
|
||||
foreach ($customers as $customer) {
|
||||
$vehicles = $customer->getVehicles();
|
||||
if (count($vehicles) > $car_count) {
|
||||
$car_count = count($vehicles);
|
||||
|
||||
// "save" customer object
|
||||
$cust = $customer;
|
||||
}
|
||||
}
|
||||
|
||||
return $cust;
|
||||
}
|
||||
}
|
||||
140
src/Controller/CustomerAppAPI/CustomerController.php
Normal file
140
src/Controller/CustomerAppAPI/CustomerController.php
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use App\Ramcar\CustomerSource;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\PrivacyPolicy;
|
||||
use App\Service\HashGenerator;
|
||||
|
||||
class CustomerController extends ApiController
|
||||
{
|
||||
public function getInfo(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// if no customer found
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(true, '', [
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'priv_third_party' => (bool) false,
|
||||
'priv_promo' => (bool) false,
|
||||
]);
|
||||
}
|
||||
|
||||
// send back customer details
|
||||
return new ApiResponse(true, '', [
|
||||
'first_name' => $cust->getFirstName(),
|
||||
'last_name' => $cust->getLastName(),
|
||||
'priv_third_party' => (bool) $cust->getPrivacyThirdParty(),
|
||||
'priv_promo' => (bool) $cust->getPrivacyPromo(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateInfo(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'first_name',
|
||||
'last_name',
|
||||
]);
|
||||
|
||||
$cust = $this->updateCustomerInfo($req);
|
||||
|
||||
$policy_mobile_id = $_ENV['POLICY_MOBILE'];
|
||||
$mobile_policy = $this->em->getRepository(PrivacyPolicy::class)->find($policy_mobile_id);
|
||||
|
||||
// set policy id
|
||||
if ($mobile_policy != null) {
|
||||
$cust->setPrivacyPolicyMobile($mobile_policy);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
public function getStatus(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// set data
|
||||
$data = [];
|
||||
if ($this->session->isConfirmed()) {
|
||||
$data['status'] = 'confirmed';
|
||||
} else {
|
||||
$data['status'] = 'unconfirmed';
|
||||
}
|
||||
|
||||
return new ApiResponse(true, '', $data);
|
||||
}
|
||||
|
||||
public function updateDeviceID(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'device_id',
|
||||
]);
|
||||
|
||||
$device_id = $req->request->get('device_id');
|
||||
$this->session->setDevicePushID($device_id);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
protected function updateCustomerInfo(Request $req)
|
||||
{
|
||||
// create new customer if it's not there
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
$cust = new Customer();
|
||||
|
||||
// set customer source
|
||||
$cust->setCreateSource(CustomerSource::MOBILE);
|
||||
$this->em->persist($cust);
|
||||
|
||||
$this->session->setCustomer($cust);
|
||||
}
|
||||
|
||||
$cust->setFirstName($req->request->get('first_name'))
|
||||
->setLastName($req->request->get('last_name'))
|
||||
->setEmail($req->request->get('email', ''))
|
||||
->setConfirmed($this->session->isConfirmed());
|
||||
|
||||
// update mobile phone of customer
|
||||
$cust->setPhoneMobile(substr($this->session->getPhoneNumber(), 2));
|
||||
|
||||
return $cust;
|
||||
}
|
||||
|
||||
public function getCustomerHash(Request $req, HashGenerator $hash)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// hash customer id
|
||||
$hashed_id = $hash->getHash($cust->getID());
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'cust_hash' => $hashed_id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
1374
src/Controller/CustomerAppAPI/JobOrderController.php
Normal file
1374
src/Controller/CustomerAppAPI/JobOrderController.php
Normal file
File diff suppressed because it is too large
Load diff
590
src/Controller/CustomerAppAPI/LocationController.php
Normal file
590
src/Controller/CustomerAppAPI/LocationController.php
Normal file
|
|
@ -0,0 +1,590 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Service\GeofenceTracker;
|
||||
use App\Service\InventoryManager;
|
||||
use App\Service\MapTools;
|
||||
use App\Entity\Hub;
|
||||
use App\Entity\Battery;
|
||||
use App\Entity\CustomerMetadata;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
|
||||
class LocationController extends ApiController
|
||||
{
|
||||
public function locationSupport(Request $req, GeofenceTracker $geo)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'longitude',
|
||||
'latitude',
|
||||
]);
|
||||
|
||||
$long = $req->query->get('longitude');
|
||||
$lat = $req->query->get('latitude');
|
||||
|
||||
// NOTE: had to add this for promo tag
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
$is_covered = false;
|
||||
// check if customer still has promo
|
||||
if (($cust->getCustomerTag('TAG_CAR_CLUB_OFFICER_PROMO')) ||
|
||||
($cust->getCustomerTag('TAG_CAR_CLUB_MEMBER_PROMO'))
|
||||
) {
|
||||
// if has customer tag, customer has not availed of promo
|
||||
$is_covered = true;
|
||||
} else {
|
||||
// geofence
|
||||
$is_covered = $geo->isCovered($long, $lat);
|
||||
}
|
||||
|
||||
// geofence
|
||||
// $is_covered = $geo->isCovered($long, $lat);
|
||||
|
||||
$data = [
|
||||
'longitude' => $long,
|
||||
'latitude' => $lat,
|
||||
'supported' => $is_covered,
|
||||
];
|
||||
|
||||
// check if is_covered is false. If so, we need to set the error part in the response
|
||||
if (!$is_covered) {
|
||||
return new ApiResponse(false, $this->getGeoErrorMessage(), $data);
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', $data);
|
||||
}
|
||||
|
||||
public function getNearestHubAndSlots(
|
||||
Request $req,
|
||||
MapTools $map_tools
|
||||
) {
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'longitude',
|
||||
'latitude',
|
||||
]);
|
||||
|
||||
$coordinates = new Point($req->query->get('longitude'), $req->query->get('latitude'));
|
||||
|
||||
// add checking if customer has a pre-registered hub
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// check if customer has customer tag promo
|
||||
if (($cust->getCustomerTag('TAG_CAR_CLUB_OFFICER_PROMO')) ||
|
||||
($cust->getCustomerTag('TAG_CAR_CLUB_MEMBER_PROMO'))
|
||||
) {
|
||||
// if has customer tag, customer has not availed of promo, get the hub where customer is pre-registered
|
||||
$car_club_cust_hub = $cust->getCarClubCustomerHub();
|
||||
if ($car_club_cust_hub != null) {
|
||||
// need to get the rider slots for the pre-registered hub
|
||||
$hub = $car_club_cust_hub->getHub();
|
||||
$nearest_hub_slots = $this->findAdvanceNearestHubAndSlots($coordinates, $map_tools, $hub);
|
||||
} else {
|
||||
$nearest_hub_slots = $this->findAdvanceNearestHubAndSlots($coordinates, $map_tools);
|
||||
|
||||
if (empty($nearest_hub_slots['hub'])) {
|
||||
return new ApiResponse(false, 'Thank you for reaching out to us. Please expect a call from us and we will assist you with your request. Thank you and stay safe!');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$nearest_hub_slots = $this->findAdvanceNearestHubAndSlots($coordinates, $map_tools);
|
||||
|
||||
if (empty($nearest_hub_slots['hub'])) {
|
||||
return new ApiResponse(false, 'Thank you for reaching out to us. Please expect a call from us and we will assist you with your request. Thank you and stay safe!');
|
||||
}
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'hub_id' => $nearest_hub_slots['hub']->getID(),
|
||||
'hub_slots' => $nearest_hub_slots['slots'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function addLocation(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'name',
|
||||
'address',
|
||||
'longitude',
|
||||
'latitude',
|
||||
'landmark',
|
||||
]);
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// get the information
|
||||
$name = $req->request->get('name');
|
||||
$address = $req->request->get('address');
|
||||
$lng = $req->request->get('longitude');
|
||||
$lat = $req->request->get('latitude');
|
||||
$landmark = $req->request->get('landmark');
|
||||
|
||||
$loc_info = [
|
||||
'address' => $address,
|
||||
'longitude' => $lng,
|
||||
'latitude' => $lat,
|
||||
'landmark' => $landmark,
|
||||
];
|
||||
|
||||
// check if customer already has existing metadata
|
||||
$c_meta = $this->em->getRepository(CustomerMetadata::class)->findOneBy(['customer' => $cust]);
|
||||
if ($c_meta == null) {
|
||||
// create new customer meta
|
||||
$cust_meta = new CustomerMetadata();
|
||||
$cust_meta->setCustomer($cust);
|
||||
$cust_meta->addMetaInfo($name, $loc_info);
|
||||
|
||||
$this->em->persist($cust_meta);
|
||||
} else {
|
||||
// limit locations to 6. If more than 6, pop the first one out
|
||||
// add location to existing customer meta
|
||||
$meta_count = count($c_meta->getAllMetaInfo());
|
||||
|
||||
if ($meta_count >= 6)
|
||||
$c_meta->popMetaInfo();
|
||||
|
||||
$c_meta->addMetaInfo($name, $loc_info);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
public function getLocations(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// get the customer meta for customer
|
||||
$locations = [];
|
||||
$cust_meta = $this->em->getRepository(CustomerMetadata::class)->findOneBy(['customer' => $cust]);
|
||||
if ($cust_meta != null) {
|
||||
$locations[] = $cust_meta->getAllMetaInfo();
|
||||
}
|
||||
|
||||
$data = [
|
||||
'locations' => $locations,
|
||||
];
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
protected function findAdvanceNearestHubAndSlots(Point $coordinates, MapTools $map_tools, $hub = null)
|
||||
{
|
||||
$hub_data = [];
|
||||
|
||||
if ($hub != null) {
|
||||
// get the slots of hub
|
||||
$hub_slots = $this->getHubRiderSlots($hub);
|
||||
|
||||
$slots = $hub_slots['slot_data'];
|
||||
|
||||
$hub_data = [
|
||||
'hub' => $hub,
|
||||
'slots' => $slots,
|
||||
];
|
||||
return $hub_data;
|
||||
}
|
||||
|
||||
// get the nearest 10 hubs
|
||||
$nearest_hubs_with_distance = [];
|
||||
$hubs = $map_tools->getClosestOpenHubs($coordinates, 10);
|
||||
|
||||
foreach ($hubs as $hub) {
|
||||
$nearest_hubs_with_distance[] = $hub;
|
||||
// TODO: insert checking for branch code here when inventory manager is up
|
||||
}
|
||||
|
||||
$nearest = null;
|
||||
$hub_slots = [];
|
||||
$slot_found = false;
|
||||
// find the nearest hub
|
||||
if (!empty($nearest_hubs_with_distance)) {
|
||||
// get slots of nearest hub right after getting nearest hub.
|
||||
// then check if hub has available slots. If not, get next nearest hub.
|
||||
foreach ($nearest_hubs_with_distance as $nhd) {
|
||||
if (empty($nearest)) {
|
||||
// get the slots for the hub to check if hub is available for assignment
|
||||
$hub_slots = $this->getHubRiderSlots($nhd['hub']);
|
||||
|
||||
$flag_hub_available = $hub_slots['flag_hub_available'];
|
||||
if ($flag_hub_available == true) {
|
||||
$nearest = $nhd;
|
||||
}
|
||||
} else {
|
||||
if ($nhd['distance'] < $nearest['distance']) {
|
||||
// get the slots for nearest which is nhd right now
|
||||
$hub_slots = $this->getHubRiderSlots($nhd['hub']);
|
||||
|
||||
$flag_hub_available = $hub_slots['flag_hub_available'];
|
||||
|
||||
// if hub is available, set hub to nearest
|
||||
if ($flag_hub_available == true) {
|
||||
$nearest = $nhd;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($nearest != null) {
|
||||
// set hub data to what is in nearest
|
||||
$hub_data = [
|
||||
'hub' => $nearest['hub'],
|
||||
'slots' => $hub_slots['slot_data'],
|
||||
];
|
||||
}
|
||||
|
||||
return $hub_data;
|
||||
}
|
||||
|
||||
protected function getHubRiderSlots(Hub $hub)
|
||||
{
|
||||
// check hub's advance orders for the day
|
||||
|
||||
/*
|
||||
// get number of advance orders for the next day if request came in before midnight
|
||||
// or for current day if request came in after midnight
|
||||
// check request_time
|
||||
$request_time = time();
|
||||
$midnight = strtotime('00:00');
|
||||
*/
|
||||
$start_date = new DateTime();
|
||||
$end_date = new DateTime();
|
||||
|
||||
// to keep things simple, just start on next day regardless of midnight timer
|
||||
$start_date->add(new DateInterval('P1D'));
|
||||
$end_date->add(new DateInterval('P3D'));
|
||||
|
||||
/*
|
||||
if ($request_time < $midnight)
|
||||
{
|
||||
// add +1 to start date to get the next day
|
||||
// add +3 to date to end date to get the advance orders for the next three days
|
||||
$start_date->add(new DateInterval('P1D'));
|
||||
$end_date->add(new DateInterval('P1D'));
|
||||
}
|
||||
$end_date->add(new DateInterval('P2D'));
|
||||
*/
|
||||
|
||||
// set time bounds for the start and end date
|
||||
$start_date->setTime(0, 1);
|
||||
$end_date->setTime(23, 59);
|
||||
|
||||
// NOTE: get advance orders via query
|
||||
// get JOs assigned to hub that are advance orders and scheduled for the next three days with
|
||||
// for hub assignment status
|
||||
$query = $this->em->createQuery('select jo from App\Entity\JobOrder jo where jo.hub = :hub and jo.flag_advance = true and
|
||||
jo.date_schedule >= :date_start and jo.date_schedule <= :date_end and jo.status != :status_cancelled
|
||||
and jo.status != :status_fulfilled');
|
||||
$jos_advance_orders = $query->setParameters([
|
||||
'hub' => $hub,
|
||||
'date_start' => $start_date,
|
||||
'date_end' => $end_date,
|
||||
'status_cancelled' => JOStatus::CANCELLED,
|
||||
'status_fulfilled' => JOStatus::FULFILLED,
|
||||
])
|
||||
->getResult();
|
||||
// check request_time
|
||||
|
||||
// define slots
|
||||
$slots = [
|
||||
'08_09' => '8:00 AM',
|
||||
'09_10' => '9:00 AM',
|
||||
'10_11' => '10:00 AM',
|
||||
'11_12' => '11:00 AM',
|
||||
'12_13' => '12:00 PM',
|
||||
'13_14' => '1:00 PM',
|
||||
'14_15' => '2:00 PM',
|
||||
'15_16' => '3:00 PM',
|
||||
'16_17' => '4:00 PM',
|
||||
];
|
||||
|
||||
|
||||
// get the dates for the next three days
|
||||
$first_date = $start_date->format('Y-m-d');
|
||||
$second_date = $start_date->add(new DateInterval('P1D'));
|
||||
$sec_date = $second_date->format('Y-m-d');
|
||||
$third_date = $end_date->format('Y-m-d');
|
||||
|
||||
// define days
|
||||
$days = [
|
||||
$first_date => $first_date,
|
||||
$sec_date => $sec_date,
|
||||
$third_date => $third_date,
|
||||
];
|
||||
|
||||
// initialize hub rider slots
|
||||
$hub_rider_slots = [];
|
||||
foreach ($days as $day) {
|
||||
foreach ($slots as $slot_key => $slot) {
|
||||
$hub_rider_slots[$day][$slot_key] = $hub->getRiderSlots();
|
||||
}
|
||||
}
|
||||
|
||||
// check each JO's date_schedule, decrement rider_slots if date schedule falls in that slot
|
||||
foreach ($jos_advance_orders as $jo) {
|
||||
// get date key
|
||||
$date_sched = $jo->getDateSchedule();
|
||||
$date_string = $date_sched->format('Y-m-d');
|
||||
$hour = $date_sched->format('H');
|
||||
$slot_id = sprintf('%02d_%02d', $hour, $hour + 1);
|
||||
|
||||
// error_log("SLOT - $date_string - $slot_id");
|
||||
|
||||
// decrement rider slot
|
||||
if (isset($hub_rider_slots[$date_string][$slot_id]))
|
||||
$hub_rider_slots[$date_string][$slot_id]--;
|
||||
|
||||
// check if it goes through next slot (10 min allowance)
|
||||
$mins = $date_sched->format('i');
|
||||
if ($mins > 10) {
|
||||
$next_slot_id = sprintf('%02d_%02d', $hour + 1, $hour + 2);
|
||||
// error_log("NEXT SLOT - $date_string - $next_slot_id");
|
||||
// decrement rider slot
|
||||
if (isset($hub_rider_slots[$date_string][$next_slot_id]))
|
||||
$hub_rider_slots[$date_string][$next_slot_id]--;
|
||||
}
|
||||
}
|
||||
|
||||
// error_log(print_r($hub_rider_slots, true));
|
||||
|
||||
$hub_slots = $this->generateHubSlots($hub_rider_slots, $slots);
|
||||
|
||||
// error_log(print_r($hub_slots, true));
|
||||
|
||||
return $hub_slots;
|
||||
}
|
||||
|
||||
protected function generateHubSlots($rider_slots, $slots)
|
||||
{
|
||||
$data = [];
|
||||
$total_rslots = 0;
|
||||
$total_unavailable_rslots = 0;
|
||||
foreach ($rider_slots as $day_id => $rslot) {
|
||||
$data[$day_id] = [];
|
||||
|
||||
foreach ($rslot as $slot_id => $avail_slots) {
|
||||
// increment total rider slots
|
||||
$total_rslots++;
|
||||
|
||||
$slot_data = [
|
||||
'id' => $slot_id,
|
||||
'label' => $slots[$slot_id],
|
||||
'available' => true,
|
||||
];
|
||||
|
||||
// mark unavailable ones
|
||||
if ($avail_slots <= 0) { // increment total number of unavailable slots
|
||||
$total_unavailable_rslots++;
|
||||
$slot_data['available'] = false;
|
||||
}
|
||||
|
||||
// add to day data
|
||||
$data[$day_id][] = $slot_data;
|
||||
}
|
||||
}
|
||||
|
||||
// check if hub has available slots
|
||||
$hub_available = true;
|
||||
// error_log('total rider slots ' . $total_rslots);
|
||||
// error_log('total unavailable slots ' . $total_unavailable_rslots);
|
||||
if ($total_rslots == $total_unavailable_rslots) {
|
||||
// error_log('hub has no available slots');
|
||||
$hub_available = false;
|
||||
}
|
||||
|
||||
$hs_data = [
|
||||
'flag_hub_available' => $hub_available,
|
||||
'slot_data' => $data,
|
||||
];
|
||||
|
||||
return $hs_data;
|
||||
}
|
||||
|
||||
protected function findNearestHub($jo, MapTools $map_tools)
|
||||
{
|
||||
// get the nearest 10 hubs
|
||||
$selected_hub = null;
|
||||
$hubs = $map_tools->getClosestOpenHubs($jo->getCoordinates(), 10, date("H:i:s"));
|
||||
|
||||
$nearest_hubs_with_distance = [];
|
||||
$nearest_branch_codes = [];
|
||||
foreach ($hubs as $hub) {
|
||||
$nearest_hubs_with_distance[] = $hub;
|
||||
//if (!empty($hub['hub']->getBranchCode()))
|
||||
// $nearest_branch_codes[] = $hub['hub']->getBranchCode();
|
||||
}
|
||||
|
||||
// check if nearest hubs have branch codes
|
||||
//if (count($nearest_branch_codes) == 0)
|
||||
// return $selected_hub;
|
||||
|
||||
// assume all 10 have stock
|
||||
// find the nearest hub with available riders
|
||||
$nearest = null;
|
||||
foreach ($nearest_hubs_with_distance as $nhd) {
|
||||
// get number of available riders
|
||||
$count_riders = count($nhd['hub']->getAvailableRiders());
|
||||
|
||||
// get number of advance orders in the next 3 hours
|
||||
$time_now = new DateTime();
|
||||
$date_end = new DateTime();
|
||||
$date_end->add(new DateInterval('PT2H'));
|
||||
|
||||
// NOTE: get advance orders via query
|
||||
// get JOs assigned to hub that are advance orders and scheduled within X hours with
|
||||
// for rider assignment status
|
||||
$query = $this->em->createQuery('select count(jo) from App\Entity\JobOrder jo where jo.hub = :hub and jo.flag_advance = true and jo.date_schedule <= :date_end and jo.status = :status');
|
||||
$count_advance_orders = $query->setParameters([
|
||||
'hub' => $nhd['hub'],
|
||||
'date_end' => $date_end,
|
||||
'status' => JOStatus::RIDER_ASSIGN,
|
||||
])
|
||||
->setMaxResults(1)
|
||||
->getSingleScalarResult();
|
||||
|
||||
// error_log('HUB - ' . $nhd['hub']->getID());
|
||||
// error_log('RIDER COUNT - ' . $count_riders);
|
||||
// error_log('ADVANCE ORDER COUNT - ' . $count_advance_orders);
|
||||
|
||||
// if (count($nhd['hub']->getAvailableRiders()) > 0)
|
||||
// if we have more riders than we have advance orders
|
||||
if ($count_riders - $count_advance_orders > 0) {
|
||||
if (empty($nearest))
|
||||
$nearest = $nhd;
|
||||
else {
|
||||
if ($nhd['distance'] < $nearest['distance'])
|
||||
$nearest = $nhd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$selected_hub = $nearest['hub'];
|
||||
|
||||
return $selected_hub;
|
||||
}
|
||||
|
||||
protected function findNearestHubWithInventory(
|
||||
$jo,
|
||||
Battery $batt,
|
||||
MapTools $map_tools,
|
||||
InventoryManager $im
|
||||
) {
|
||||
// get the nearest 10 hubs
|
||||
$selected_hub = null;
|
||||
$hubs = $map_tools->getClosestOpenHubs($jo->getCoordinates(), 10, date("H:i:s"));
|
||||
|
||||
$nearest_hubs_with_distance = [];
|
||||
$nearest_branch_codes = [];
|
||||
foreach ($hubs as $hub) {
|
||||
$nearest_hubs_with_distance[] = $hub;
|
||||
//if (!empty($hub['hub']->getBranchCode()))
|
||||
// $nearest_branch_codes[] = $hub['hub']->getBranchCode();
|
||||
}
|
||||
|
||||
// check if nearest hubs have branch codes
|
||||
//if (count($nearest_branch_codes) == 0)
|
||||
// return $selected_hub;
|
||||
|
||||
// assume all 10 have stock
|
||||
// find the nearest hub with available riders
|
||||
$nearest = null;
|
||||
foreach ($nearest_hubs_with_distance as $nhd) {
|
||||
if (count($nhd['hub']->getAvailableRiders()) > 0) {
|
||||
if (empty($nearest))
|
||||
$nearest = $nhd;
|
||||
else {
|
||||
if ($nhd['distance'] < $nearest['distance'])
|
||||
$nearest = $nhd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$selected_hub = $nearest['hub'];
|
||||
|
||||
// uncomment this snippet when inventory check becomes active
|
||||
// get battery sku
|
||||
/*
|
||||
if ($batt != null)
|
||||
{
|
||||
$skus[] = $batt->getSAPCode();
|
||||
|
||||
// api call to check inventory
|
||||
// pass the list of branch codes of nearest hubs and the skus
|
||||
// go through returned list of branch codes
|
||||
// bypass inventory check for now
|
||||
// $hubs_with_inventory = $im->getBranchesInventory($nearest_branch_codes, $skus);
|
||||
if (!empty($hubs_with_inventory))
|
||||
{
|
||||
$nearest = [];
|
||||
$flag_hub_found = false;
|
||||
foreach ($hubs_with_inventory as $hub_with_inventory)
|
||||
{
|
||||
// find hub according to branch code
|
||||
$found_hub = $this->em->getRepository(Hub::class)->findOneBy(['branch_code' => $hub_with_inventory['BranchCode']]);
|
||||
if ($found_hub != null)
|
||||
{
|
||||
// check rider availability
|
||||
if (count($found_hub->getAvailableRiders()) > 0)
|
||||
{
|
||||
// check against nearest hubs with distance
|
||||
foreach ($nearest_hubs_with_distance as $nhd)
|
||||
{
|
||||
// get distance of hub from location, compare with $nearest. if less, replace nearest
|
||||
if ($found_hub->getID() == $nhd['hub']->getID())
|
||||
{
|
||||
if (empty($nearest))
|
||||
{
|
||||
$nearest = $nhd;
|
||||
$flag_hub_found = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($nhd['distance'] < $nearest['distance'])
|
||||
{
|
||||
$nearest = $nhd;
|
||||
$flag_hub_found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$selected_hub = $nearest['hub'];
|
||||
}
|
||||
} */
|
||||
return $selected_hub;
|
||||
}
|
||||
}
|
||||
152
src/Controller/CustomerAppAPI/PartnerController.php
Normal file
152
src/Controller/CustomerAppAPI/PartnerController.php
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use App\Entity\Partner;
|
||||
use App\Entity\Review;
|
||||
|
||||
class PartnerController extends ApiController
|
||||
{
|
||||
public function getPartnerInformation(Request $req, $pid)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// get partner
|
||||
$partner = $this->em->getRepository(Partner::class)->findOneBy(['id' => $pid]);
|
||||
if ($partner == null) {
|
||||
return new ApiResponse(false, 'No partner found.');
|
||||
}
|
||||
|
||||
// get reviews for partner
|
||||
$reviews = $this->em->getRepository(Review::class)->findBy(['partner' => $partner]);
|
||||
|
||||
// get average rating for all reviews
|
||||
$average_rating = 0;
|
||||
if (!empty($reviews)) {
|
||||
$rating = 0;
|
||||
foreach ($reviews as $review) {
|
||||
$rating = $rating + $review->getRating();
|
||||
}
|
||||
|
||||
$average_rating = $rating / sizeof($reviews);
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$data['partner'] = [
|
||||
'id' => $partner->getID(),
|
||||
'name' => $partner->getName(),
|
||||
'branch' => $partner->getBranch(),
|
||||
'address' => $partner->getAddress(),
|
||||
'contact_nums' => $partner->getContactNumbers(),
|
||||
'time_open' => $partner->getTimeOpen()->format("g:i A"),
|
||||
'time_close' => $partner->getTimeClose()->format("g:i A"),
|
||||
'longitude' => $partner->getCoordinates()->getLongitude(),
|
||||
'latitude' => $partner->getCoordinates()->getLatitude(),
|
||||
'average_rating' => $average_rating,
|
||||
];
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', $data);
|
||||
}
|
||||
|
||||
public function getClosestPartners(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'longitude',
|
||||
'latitude',
|
||||
'service_id',
|
||||
'limit',
|
||||
]);
|
||||
|
||||
$long = $req->query->get('longitude');
|
||||
$lat = $req->query->get('latitude');
|
||||
$service_id = $req->query->get('service_id');
|
||||
$limit = $req->query->get('limit');
|
||||
|
||||
// get partners within range
|
||||
$query = $this->em->createQuery('SELECT p, st_distance(p.coordinates, point(:lng, :lat)) as dist FROM App\Entity\Partner p
|
||||
JOIN App\Entity\Service s where s.id = :service_id ORDER BY dist')
|
||||
->setParameter('lat', $lat)
|
||||
->setParameter('lng', $long)
|
||||
->setParameter('service_id', $service_id);
|
||||
|
||||
$query->setMaxResults($limit);
|
||||
$result = $query->getResult();
|
||||
|
||||
$data = [];
|
||||
$partners = [];
|
||||
foreach ($result as $row) {
|
||||
// get all the reviews for each partner and average the ratings
|
||||
$partner_id = $row[0]->getID();
|
||||
$partner = $this->em->getRepository(Partner::class)->find($partner_id);
|
||||
$partner_reviews = $this->em->getRepository(Review::class)->findBy(['partner' => $partner]);
|
||||
|
||||
$average_rating = 0;
|
||||
if (count($partner_reviews) > 0) {
|
||||
$rating = 0;
|
||||
foreach ($partner_reviews as $review) {
|
||||
$rating = $rating + $review->getRating();
|
||||
}
|
||||
|
||||
$average_rating = $rating / sizeof($partner_reviews);
|
||||
}
|
||||
|
||||
$partners[] = [
|
||||
'id' => $row[0]->getID(),
|
||||
'name' => $row[0]->getName(),
|
||||
'branch' => $row[0]->getBranch(),
|
||||
'address' => $row[0]->getAddress(),
|
||||
'contact_nums' => $row[0]->getContactNumbers(),
|
||||
'time_open' => $row[0]->getTimeOpen()->format("g:i A"),
|
||||
'time_close' => $row[0]->getTimeClose()->format("g:i A"),
|
||||
'longitude' => $row[0]->getCoordinates()->getLongitude(),
|
||||
'latitude' => $row[0]->getCoordinates()->getLatitude(),
|
||||
'db_distance' => $row['dist'],
|
||||
'rating' => $average_rating,
|
||||
];
|
||||
}
|
||||
|
||||
$data['partners'] = $partners;
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', $data);
|
||||
}
|
||||
|
||||
public function reviewPartner($pid, Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'rating',
|
||||
'message',
|
||||
]);
|
||||
|
||||
$rating = $req->request->get('rating');
|
||||
$msg = $req->request->get('message');
|
||||
|
||||
// TODO: check rating if 1 - 5
|
||||
|
||||
// check if partner exists
|
||||
$partner = $this->em->getRepository(Partner::class)->find($pid);
|
||||
if ($partner == null) {
|
||||
return new ApiResponse(false, 'No partner found.');
|
||||
}
|
||||
|
||||
$rev = new Review();
|
||||
$rev->setRating($rating)
|
||||
->setMessage($msg)
|
||||
->setPartner($partner)
|
||||
->setMobileSession($this->session);
|
||||
|
||||
// save to db
|
||||
$this->em->persist($rev);
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
}
|
||||
63
src/Controller/CustomerAppAPI/PrivacyController.php
Normal file
63
src/Controller/CustomerAppAPI/PrivacyController.php
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use App\Entity\PrivacyPolicy;
|
||||
|
||||
class PrivacyController extends ApiController
|
||||
{
|
||||
public function privacySettings(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'priv_third_party',
|
||||
// 'priv_promo',
|
||||
]);
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// set privacy settings
|
||||
$priv_promo = $req->request->get('priv_promo', false);
|
||||
$priv_third_party = $req->request->get('priv_third_party');
|
||||
$cust->setPrivacyThirdParty($priv_third_party)
|
||||
->setPrivacyPromo($priv_promo);
|
||||
|
||||
// get the policy ids from .env
|
||||
$policy_promo_id = $_ENV['POLICY_PROMO'];
|
||||
$policy_third_party_id = $_ENV['POLICY_THIRD_PARTY'];
|
||||
|
||||
// check if privacy settings are true
|
||||
// if true, set the private policy for the customer
|
||||
if ($priv_promo) {
|
||||
// find the promo policy
|
||||
$policy = $this->em->getRepository(PrivacyPolicy::class)->find($policy_promo_id);
|
||||
|
||||
// set policy id
|
||||
if ($policy != null) {
|
||||
$cust->setPrivacyPolicyPromo($policy);
|
||||
}
|
||||
}
|
||||
|
||||
if ($priv_third_party) {
|
||||
// find the third party policy
|
||||
$policy = $this->em->getRepository(PrivacyPolicy::class)->find($policy_third_party_id);
|
||||
|
||||
// set policy id
|
||||
if ($policy != null) {
|
||||
$cust->setPrivacyPolicyThirdParty($policy);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
}
|
||||
18
src/Controller/CustomerAppAPI/PromoController.php
Normal file
18
src/Controller/CustomerAppAPI/PromoController.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
class PromoController extends ApiController
|
||||
{
|
||||
public function listPromos(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
}
|
||||
229
src/Controller/CustomerAppAPI/RiderController.php
Normal file
229
src/Controller/CustomerAppAPI/RiderController.php
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Ramcar\APIRiderStatus;
|
||||
use App\Entity\RiderRating;
|
||||
use App\Service\RiderTracker;
|
||||
|
||||
use Exception;
|
||||
|
||||
class RiderController extends ApiController
|
||||
{
|
||||
public function getRiderStatus(Request $req, RiderTracker $rt)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// check if we have an ongoing job order
|
||||
/*
|
||||
$ongoing_jos = $em->getRepository(JobOrder::class)->findBy([
|
||||
'customer' => $cust,
|
||||
'status' => [JOStatus::PENDING, JOStatus::RIDER_ASSIGN, JOStatus::IN_TRANSIT, JOStatus::ASSIGNED, JOStatus::IN_PROGRESS],
|
||||
]);
|
||||
*/
|
||||
$ongoing_jos = $this->getOngoingJobOrders($cust);
|
||||
|
||||
// $res->setData(['here' => count($ongoing_jos)]);
|
||||
// return $res->getReturnResponse();
|
||||
if (count($ongoing_jos) <= 0) {
|
||||
try {
|
||||
// check if the latest fulfilled jo they have needs rider rating
|
||||
$query = $this->em->createQuery('select jo from App\Entity\JobOrder jo where jo.customer = :cust and jo.status = :status order by jo.date_fulfill desc');
|
||||
$fulfill_jo = $query->setParameters([
|
||||
'cust' => $cust,
|
||||
'status' => JOStatus::FULFILLED,
|
||||
])
|
||||
->setMaxResults(1)
|
||||
->getSingleResult();
|
||||
} catch (Exception $e) {
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'status' => APIRiderStatus::NO_PENDING_JO,
|
||||
]);
|
||||
}
|
||||
|
||||
// we got a recently fulfilled job order
|
||||
if ($fulfill_jo) {
|
||||
// check if the rider has been rated
|
||||
if (!$fulfill_jo->hasRiderRating()) {
|
||||
$dest = $fulfill_jo->getCoordinates();
|
||||
|
||||
$data = [
|
||||
'jo_id' => $fulfill_jo->getID(),
|
||||
'service_type' => $fulfill_jo->getServiceType(),
|
||||
'destination' => [
|
||||
'long' => $dest->getLongitude(),
|
||||
'lat' => $dest->getLatitude(),
|
||||
],
|
||||
'delivery_address' => $fulfill_jo->getDeliveryAddress(),
|
||||
'delivery_instructions' => $fulfill_jo->getDeliveryInstructions(),
|
||||
];
|
||||
|
||||
$rider = $fulfill_jo->getRider();
|
||||
|
||||
// default image url
|
||||
$url_prefix = $req->getSchemeAndHttpHost();
|
||||
$image_url = $url_prefix . '/assets/images/user.gif';
|
||||
if ($rider->getImageFile() != null)
|
||||
$image_url = $url_prefix . '/uploads/' . $rider->getImageFile();
|
||||
|
||||
$data['status'] = APIRiderStatus::RIDER_RATING;
|
||||
// default rider location to hub
|
||||
$data['rider'] = [
|
||||
'id' => $rider->getID(),
|
||||
'name' => $rider->getFullName(),
|
||||
'plate_num' => $rider->getPlateNumber(),
|
||||
'contact_num' => $rider->getContactNumber(),
|
||||
'image_url' => $image_url,
|
||||
];
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', $data);
|
||||
}
|
||||
}
|
||||
|
||||
// response, no pending
|
||||
return new ApiResponse(true, '', [
|
||||
'status' => APIRiderStatus::NO_PENDING_JO,
|
||||
]);
|
||||
}
|
||||
|
||||
// get first jo that's pending
|
||||
$jo = $ongoing_jos[0];
|
||||
$dest = $jo->getCoordinates();
|
||||
|
||||
$data = [
|
||||
'jo_id' => $jo->getID(),
|
||||
'service_type' => $jo->getServiceType(),
|
||||
'destination' => [
|
||||
'long' => $dest->getLongitude(),
|
||||
'lat' => $dest->getLatitude(),
|
||||
],
|
||||
'delivery_address' => $jo->getDeliveryAddress(),
|
||||
'delivery_instructions' => $jo->getDeliveryInstructions(),
|
||||
];
|
||||
|
||||
switch ($jo->getStatus()) {
|
||||
case JOStatus::PENDING:
|
||||
$data['status'] = APIRiderStatus::OUTLET_ASSIGN;
|
||||
break;
|
||||
case JOStatus::RIDER_ASSIGN:
|
||||
$data['status'] = APIRiderStatus::RIDER_ASSIGN;
|
||||
break;
|
||||
case JOStatus::ASSIGNED:
|
||||
case JOStatus::IN_TRANSIT:
|
||||
case JOStatus::IN_PROGRESS:
|
||||
$rider = $jo->getRider();
|
||||
// get rider coordinates from redis
|
||||
$coord = $rt->getRiderLocation($rider->getID());
|
||||
|
||||
// default image url
|
||||
$url_prefix = $req->getSchemeAndHttpHost();
|
||||
$image_url = $url_prefix . '/assets/images/user.gif';
|
||||
if ($rider->getImageFile() != null)
|
||||
$image_url = $url_prefix . '/uploads/' . $rider->getImageFile();
|
||||
|
||||
$data['status'] = APIRiderStatus::RIDER_PICK_UP;
|
||||
// TODO: fix this to actual location of rider
|
||||
// default rider location to hub
|
||||
$data['rider'] = [
|
||||
'id' => $rider->getID(),
|
||||
'name' => $rider->getFullName(),
|
||||
'plate_num' => $rider->getPlateNumber(),
|
||||
'contact_num' => $rider->getContactNumber(),
|
||||
'image_url' => $image_url,
|
||||
'location' => [
|
||||
'long' => $coord->getLongitude(),
|
||||
'lat' => $coord->getLatitude()
|
||||
]
|
||||
];
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', $data);
|
||||
}
|
||||
|
||||
public function addRiderRating(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'jo_id',
|
||||
'rating',
|
||||
]);
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// get job order
|
||||
$jo_id = $req->request->get('jo_id');
|
||||
$jo = $this->em->getRepository(JobOrder::class)->find($jo_id);
|
||||
if ($jo == null) {
|
||||
return new ApiResponse(false, 'No job order found.');
|
||||
}
|
||||
|
||||
// get rider
|
||||
$rider = $jo->getRider();
|
||||
if ($rider == null) {
|
||||
return new ApiResponse(false, 'No rider found.');
|
||||
}
|
||||
|
||||
// check that the customer owns the job order
|
||||
$jo_cust = $jo->getCustomer();
|
||||
if ($jo_cust->getID() != $cust->getID()) {
|
||||
return new ApiResponse(false, 'Job order was not initiated by customer.');
|
||||
}
|
||||
|
||||
// TODO: check job order status, if it's complete
|
||||
|
||||
// add rider rating
|
||||
$rating_num = $req->request->get('rating', -1);
|
||||
|
||||
// if rating is -1
|
||||
if ($rating_num == -1) {
|
||||
$jo->setHasRiderRating();
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
|
||||
$rating = new RiderRating();
|
||||
$rating->setRider($rider)
|
||||
->setCustomer($cust)
|
||||
->setJobOrder($jo)
|
||||
->setRating($rating_num);
|
||||
|
||||
// rider rating comment
|
||||
$comment = $req->request->get('comment');
|
||||
if (!empty($comment))
|
||||
$rating->setComment($comment);
|
||||
|
||||
// mark jo as rider rated already
|
||||
$jo->setHasRiderRating();
|
||||
|
||||
$this->em->persist($rating);
|
||||
$this->em->flush();
|
||||
|
||||
// TODO: set average rating in rider entity
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
}
|
||||
57
src/Controller/CustomerAppAPI/ScheduleController.php
Normal file
57
src/Controller/CustomerAppAPI/ScheduleController.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class ScheduleController extends ApiController
|
||||
{
|
||||
public function scheduleOptionStatus(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
$schedule_choice = true;
|
||||
|
||||
// remove the time check after ECQ. This will then always return true
|
||||
// get current time
|
||||
$current_datetime = new DateTime();
|
||||
//$current_datetime = DateTime::createFromFormat('Y-m-d H:i', '2020-04-30 17:01');
|
||||
|
||||
// get the hour
|
||||
$hour = $current_datetime->format('G');
|
||||
|
||||
// commenting out the time check since we can now book 24/7
|
||||
// this will get uncommented out if and when ECQ will kick in
|
||||
//if (($hour < 8) || ($hour > 16))
|
||||
// $schedule_choice = false;
|
||||
|
||||
// add checking if customer has a pre-registered hub
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
// check if customer has customer tag promo
|
||||
if (($cust->getCustomerTag('TAG_CAR_CLUB_OFFICER_PROMO')) ||
|
||||
($cust->getCustomerTag('TAG_CAR_CLUB_MEMBER_PROMO'))
|
||||
) {
|
||||
// if has customer tag, customer has not availed of promo, get the hub where customer is pre-registered
|
||||
$car_club_hub = $cust->getCarClubCustomerHub();
|
||||
if ($car_club_hub != null) {
|
||||
$schedule_choice = false;
|
||||
}
|
||||
}
|
||||
|
||||
// schedule_choice will always be true aka customer can opt to
|
||||
// Book Now or Schedule Order EXCEPT if customer has customer tag promo
|
||||
// or ECQ comes back
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'display_schedule_choice' => $schedule_choice,
|
||||
]);
|
||||
}
|
||||
}
|
||||
53
src/Controller/CustomerAppAPI/ServiceController.php
Normal file
53
src/Controller/CustomerAppAPI/ServiceController.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
class ServiceController extends ApiController
|
||||
{
|
||||
public function listServices(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// services
|
||||
$results = $this->em->getRepository(Service::class)->findAll();
|
||||
if (empty($results)) {
|
||||
return new ApiResponse(false, 'No services available.');
|
||||
}
|
||||
|
||||
$services = [];
|
||||
foreach ($results as $result) {
|
||||
/*
|
||||
// get partners
|
||||
$partners = [];
|
||||
$service_partners = $result->getPartners();
|
||||
foreach($service_partners as $sp)
|
||||
{
|
||||
$partners[] = [
|
||||
'id' => $sp->getID(),
|
||||
'name' => $sp->getName(),
|
||||
'branch' => $sp->getBranch(),
|
||||
'address' => $sp->getAddress(),
|
||||
'contact_nums' => $sp->getContactNumbers(),
|
||||
'time_open' => $sp->getTimeOpen()->format("g:i A"),
|
||||
'time_close' => $sp->getTimeClose()->format("g:i A"),
|
||||
];
|
||||
}
|
||||
*/
|
||||
|
||||
$services[] = [
|
||||
'id' => $result->getID(),
|
||||
'name' => $result->getName(),
|
||||
// 'partners' => $partners,
|
||||
];
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'services' => $services,
|
||||
]);
|
||||
}
|
||||
}
|
||||
323
src/Controller/CustomerAppAPI/VehicleController.php
Normal file
323
src/Controller/CustomerAppAPI/VehicleController.php
Normal file
|
|
@ -0,0 +1,323 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use App\Entity\CustomerVehicle;
|
||||
use App\Entity\VehicleManufacturer;
|
||||
use App\Entity\Vehicle;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class VehicleController extends ApiController
|
||||
{
|
||||
public function listVehicleManufacturers(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// get manufacturer list
|
||||
$mfgs = $this->em->getRepository(VehicleManufacturer::class)->findBy(['flag_mobile' => true], ['name' => 'asc']);
|
||||
$mfg_list = [];
|
||||
|
||||
foreach ($mfgs as $mfg) {
|
||||
$mfg_list[] = [
|
||||
'id' => $mfg->getID(),
|
||||
'name' => $mfg->getName(),
|
||||
];
|
||||
}
|
||||
|
||||
return new ApiResponse(true, '', [
|
||||
'manufacturers' => $mfg_list,
|
||||
]);
|
||||
}
|
||||
|
||||
public function listVehicleMakes(Request $req, $mfg_id)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// get manufacturer
|
||||
$mfg = $this->em->getRepository(VehicleManufacturer::class)->find($mfg_id);
|
||||
if ($mfg == null) {
|
||||
// response
|
||||
return new ApiResponse(false, 'Invalid vehicle manufacturer id.');
|
||||
}
|
||||
|
||||
// get makes
|
||||
$vehicles = $this->em->getRepository(Vehicle::class)->findBy(
|
||||
[
|
||||
'flag_mobile' => true,
|
||||
'manufacturer' => $mfg_id,
|
||||
],
|
||||
['make' => 'asc']
|
||||
);
|
||||
// $vehicles = $mfg->getVehicles();
|
||||
$vlist = [];
|
||||
foreach ($vehicles as $v) {
|
||||
$vlist[] = [
|
||||
'id' => $v->getID(),
|
||||
'make' => trim($v->getMake() . ' ' . $v->getModelYearFormatted(false)),
|
||||
// 'make' => $v->getMake() . ' ' . $v->getModelYearFrom() . '-' . $v->getModelYearTo(),
|
||||
];
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'manufacturer' => [
|
||||
'id' => $mfg->getID(),
|
||||
'name' => $mfg->getName(),
|
||||
],
|
||||
'makes' => $vlist,
|
||||
]);
|
||||
}
|
||||
|
||||
public function addVehicle(Request $req)
|
||||
{
|
||||
// check requirements
|
||||
$this->checkVehicleRequirements($req);
|
||||
|
||||
// customer vehicle
|
||||
$cv = new CustomerVehicle();
|
||||
|
||||
// set object and return
|
||||
$this->setCustomerVehicleObject($req, $cv);
|
||||
}
|
||||
|
||||
public function updateVehicle(Request $req, $id)
|
||||
{
|
||||
// check requirements
|
||||
$this->checkVehicleRequirements($req);
|
||||
|
||||
// get customer vehicle
|
||||
$cv = $this->em->getRepository(CustomerVehicle::class)->find($id);
|
||||
|
||||
// check if it exists
|
||||
if ($cv == null) {
|
||||
return new ApiResponse(false, 'Vehicle does not exist.');
|
||||
}
|
||||
|
||||
// check if it's owned by customer
|
||||
if ($cv->getCustomer()->getID() != $this->session->getCustomer()->getID()) {
|
||||
return new ApiResponse(false, 'Invalid vehicle.');
|
||||
}
|
||||
|
||||
// set object and return
|
||||
$this->setCustomerVehicleObject($req, $cv);
|
||||
}
|
||||
|
||||
public function listVehicles(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// vehicles
|
||||
$cv_list = [];
|
||||
// $cvs = $cust->getVehicles();
|
||||
// only get the customer's vehicles whose flag_active is true
|
||||
$cvs = $this->em->getRepository(CustomerVehicle::class)->findBy(['flag_active' => true, 'customer' => $cust]);
|
||||
foreach ($cvs as $cv) {
|
||||
$battery_id = null;
|
||||
if ($cv->getCurrentBattery() != null)
|
||||
$battery_id = $cv->getCurrentBattery()->getID();
|
||||
|
||||
$wty_ex = null;
|
||||
if ($cv->getWarrantyExpiration() != null)
|
||||
$wty_ex = $cv->getWarrantyExpiration()->format('Y-m-d');
|
||||
|
||||
$warranty = $this->findWarranty($cv->getPlateNumber());
|
||||
|
||||
$cv_name = '';
|
||||
if ($cv->getName() != null)
|
||||
$cv_name = $cv->getName();
|
||||
|
||||
$cv_list[] = [
|
||||
'cv_id' => $cv->getID(),
|
||||
'mfg_id' => $cv->getVehicle()->getManufacturer()->getID(),
|
||||
'make_id' => $cv->getVehicle()->getID(),
|
||||
'name' => $cv_name,
|
||||
'plate_num' => $cv->getPlateNumber(),
|
||||
'model_year' => $cv->getModelYear(),
|
||||
'color' => $cv->getColor(),
|
||||
'condition' => $cv->getStatusCondition(),
|
||||
'fuel_type' => $cv->getFuelType(),
|
||||
'wty_code' => $cv->getWarrantyCode(),
|
||||
'wty_expire' => $wty_ex,
|
||||
'curr_batt_id' => $battery_id,
|
||||
'is_motolite' => $cv->hasMotoliteBattery() ? 1 : 0,
|
||||
'is_active' => $cv->isActive() ? 1 : 0,
|
||||
'warranty' => $warranty,
|
||||
];
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'vehicles' => $cv_list,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getCompatibleBatteries(Request $req, $vid)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// get vehicle
|
||||
$vehicle = $this->em->getRepository(Vehicle::class)->find($vid);
|
||||
if ($vehicle == null) {
|
||||
return new ApiResponse(false, 'Invalid vehicle.');
|
||||
}
|
||||
|
||||
// batteries
|
||||
$batt_list = [];
|
||||
$batts = $vehicle->getBatteries();
|
||||
foreach ($batts as $batt) {
|
||||
// TODO: Add warranty_tnv to battery information
|
||||
$batt_list[] = [
|
||||
'id' => $batt->getID(),
|
||||
'mfg_id' => $batt->getManufacturer()->getID(),
|
||||
'mfg_name' => $batt->getManufacturer()->getName(),
|
||||
'model_id' => $batt->getModel()->getID(),
|
||||
'model_name' => $batt->getModel()->getName(),
|
||||
'size_id' => $batt->getSize()->getID(),
|
||||
'size_name' => $batt->getSize()->getName(),
|
||||
'price' => $batt->getSellingPrice(),
|
||||
'wty_private' => $batt->getWarrantyPrivate(),
|
||||
'wty_commercial' => $batt->getWarrantyCommercial(),
|
||||
'image_url' => $this->getBatteryImageURL($req, $batt),
|
||||
];
|
||||
}
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'vehicle' => [
|
||||
'id' => $vehicle->getID(),
|
||||
'mfg_id' => $vehicle->getManufacturer()->getID(),
|
||||
'mfg_name' => $vehicle->getManufacturer()->getName(),
|
||||
'make' => $vehicle->getMake(),
|
||||
'model_year_from' => $vehicle->getModelYearFrom(),
|
||||
'model_year_to' => $vehicle->getModelYearTo(),
|
||||
],
|
||||
'batteries' => $batt_list,
|
||||
]);
|
||||
}
|
||||
|
||||
public function removeVehicle($id, Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// get customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// find customer vehicle
|
||||
$cv = $this->em->getRepository(CustomerVehicle::class)->find($id);
|
||||
if ($cv == null) {
|
||||
return new ApiResponse(false, 'Invalid customer vehicle id.');
|
||||
}
|
||||
|
||||
// confirm that customer vehicle belongs to customer
|
||||
if ($cv->getCustomer()->getID() != $cust->getID()) {
|
||||
return new ApiResponse(false, 'Vehicle does not belong to customer.');
|
||||
}
|
||||
|
||||
// we cannot remove a vehicle from customer if customer vehicle has already has JOs for it.
|
||||
// instead we set the customer vehicle's flag_active to false
|
||||
$cv->setActive(false);
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
protected function checkVehicleRequirements(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'make_id',
|
||||
'name',
|
||||
'plate_num',
|
||||
'model_year',
|
||||
'color',
|
||||
'condition',
|
||||
'fuel_type',
|
||||
]);
|
||||
|
||||
// TODO: check valid plate number
|
||||
// TODO: check valid fuel type (gas / diesel)
|
||||
// TODO: check current battery id
|
||||
// TODO: check condition (brand new / second-hand)
|
||||
// TODO: check is_motolite and is_active (1 or 0)
|
||||
// TODO: check warranty expiration date (YYYYMMDD)
|
||||
// TODO: check model year coverage if it fits in between
|
||||
}
|
||||
|
||||
protected function setCustomerVehicleObject(Request $req, CustomerVehicle $cv)
|
||||
{
|
||||
// check customer
|
||||
$cust = $this->session->getCustomer();
|
||||
if ($cust == null) {
|
||||
return new ApiResponse(false, 'No customer information found.');
|
||||
}
|
||||
|
||||
// get vehicle
|
||||
$vehicle = $this->em->getRepository(Vehicle::class)->find($req->request->get('make_id'));
|
||||
if ($vehicle == null) {
|
||||
return new ApiResponse(false, 'Invalid vehicle make id.');
|
||||
}
|
||||
|
||||
$cv->setCustomer($cust)
|
||||
->setVehicle($vehicle)
|
||||
->setName($req->request->get('name'))
|
||||
->setPlateNumber($req->request->get('plate_num'))
|
||||
->setModelYear($req->request->get('model_year'))
|
||||
->setColor($req->request->get('color'))
|
||||
->setFuelType($this->normalizeString($req->request->get('fuel_type')))
|
||||
->setStatusCondition($this->normalizeString($req->request->get('condition')));
|
||||
|
||||
// set warranty code and expiration
|
||||
// TODO: check warranty requirements
|
||||
if (!empty($req->request->get('wty_code')))
|
||||
$cv->setWarrantyCode($req->request->get('wty_code'));
|
||||
if (!empty($req->request->get('wty_expire')))
|
||||
$cv->setWarrantyExpiration(new DateTime($req->request->get('wty_expire')));
|
||||
|
||||
// TODO: get current battery
|
||||
|
||||
// is motolite
|
||||
if ($req->request->get('is_motolite') == 0)
|
||||
$cv->setHasMotoliteBattery(false);
|
||||
else
|
||||
$cv->setHasMotoliteBattery(true);
|
||||
|
||||
// is active
|
||||
if ($req->request->get('is_active') == 0)
|
||||
$cv->setActive(false);
|
||||
else
|
||||
$cv->setActive(true);
|
||||
|
||||
// save
|
||||
$this->em->persist($cv);
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'cv_id' => $cv->getID(),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function normalizeString($string)
|
||||
{
|
||||
return trim(strtolower($string));
|
||||
}
|
||||
}
|
||||
582
src/Controller/CustomerAppAPI/WarrantyController.php
Normal file
582
src/Controller/CustomerAppAPI/WarrantyController.php
Normal file
|
|
@ -0,0 +1,582 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller\CustomerAppAPI;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Catalyst\ApiBundle\Component\Response as ApiResponse;
|
||||
|
||||
use App\Entity\Warranty;
|
||||
use App\Service\WarrantyRaffleLogger;
|
||||
use App\Service\WarrantyAPILogger;
|
||||
use App\Service\RisingTideGateway;
|
||||
use App\Ramcar\WarrantySource;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class WarrantyController extends ApiController
|
||||
{
|
||||
public function activateWarranty(Request $req)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'plate_number',
|
||||
]);
|
||||
|
||||
$plate_number = $req->request->get('plate_number');
|
||||
|
||||
// find warranty using plate number
|
||||
$warranty_results = $this->em->getRepository(Warranty::class)->findBy(
|
||||
['plate_number' => $plate_number],
|
||||
['date_create' => 'desc']
|
||||
);
|
||||
|
||||
// check if warranty_results is empty
|
||||
if (empty($warranty_results)) {
|
||||
return new ApiResponse(false, 'No warranty found for plate number.');
|
||||
}
|
||||
|
||||
// activate all entries
|
||||
foreach ($warranty_results as $warranty) {
|
||||
$warranty->setActivated();
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
public function warrantyCheck($serial, Request $req, WarrantyRaffleLogger $raffle_logger)
|
||||
{
|
||||
// validate params
|
||||
$this->validateRequest($req);
|
||||
|
||||
// check if warranty serial is there
|
||||
$serial = $this->cleanSerial($serial);
|
||||
$warr_serial = $this->em->getRepository(WarrantySerial::class)->find($serial);
|
||||
$warr = $this->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();
|
||||
|
||||
$user_id = $req->query->get('api_key');
|
||||
$raffle_data = [
|
||||
'user_id' => $user_id,
|
||||
'serial' => $serial,
|
||||
'warranty_id' => null,
|
||||
'action' => '',
|
||||
'bmodel_name' => '',
|
||||
'bsize_name' => '',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'plate_number' => '',
|
||||
'contact_num' => '',
|
||||
'email' => '',
|
||||
'address' => '',
|
||||
];
|
||||
|
||||
$data_sent = [];
|
||||
|
||||
// if we have a warranty entry for the serial already
|
||||
if ($warr != null) {
|
||||
$warr_plate = $warr->getPlateNumber();
|
||||
$is_registered = true;
|
||||
$is_customer_warranty = false;
|
||||
|
||||
// check if the warranty is registered to a car owned by the customer
|
||||
$cust = $this->session->getCustomer();
|
||||
$is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust);
|
||||
|
||||
// null mobile number should be blank string instead
|
||||
if ($warr->getMobileNumber() == null)
|
||||
$mobile_num = '';
|
||||
else
|
||||
$mobile_num = $warr->getMobileNumber();
|
||||
|
||||
$can_edit = $is_customer_warranty;
|
||||
|
||||
// if customer plate number matches the one registered on the warranty
|
||||
if ($is_customer_warranty) {
|
||||
// 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 = '';
|
||||
|
||||
$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() ?? '',
|
||||
];
|
||||
$other_data = [
|
||||
'odometer' => (int) $warr->getOdometer() ?? 0,
|
||||
'date_purchase' => $date_purchase_cust,
|
||||
'invoice' => $invoice_url,
|
||||
'warr_card' => $warr_card_url,
|
||||
'dealer_name' => $warr->getDealerName() ?? '',
|
||||
'dealer_address' => $warr->getDealerAddress() ?? '',
|
||||
'branch_code' => $warr->getDealerBranchCode() ?? '',
|
||||
];
|
||||
|
||||
// set customer info and action for raffle log
|
||||
$raffle_data['action'] = 'serial_check_customer';
|
||||
$raffle_data['first_name'] = $customer['first_name'];
|
||||
$raffle_data['last_name'] = $customer['last_name'];
|
||||
$raffle_data['plate_number'] = $customer['plate_number'];
|
||||
$raffle_data['email'] = $customer['email'];
|
||||
$raffle_data['contact_num'] = $customer['contact_num'];
|
||||
$raffle_data['address'] = $customer['address'];
|
||||
$raffle_data['warranty_id'] = $warr->getID();
|
||||
} else {
|
||||
// hide customer information if customer is not the one registered
|
||||
$customer = [
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'mobile_number' => '',
|
||||
'plate_number' => '',
|
||||
'email' => '',
|
||||
'contact_num' => '',
|
||||
'address' => '',
|
||||
];
|
||||
$other_data = [
|
||||
'odometer' => 0,
|
||||
'date_purchase' => $today->format('Y-m-d'),
|
||||
'invoice' => '',
|
||||
'warr_card' => '',
|
||||
'dealer_name' => '',
|
||||
'dealer_address' => '',
|
||||
'branch_code' => '',
|
||||
];
|
||||
|
||||
// set action for raffle log
|
||||
$raffle_data['action'] = 'serial_check_not_customer';
|
||||
}
|
||||
} else {
|
||||
$can_edit = true;
|
||||
$customer = [
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'mobile_number' => '',
|
||||
'plate_number' => '',
|
||||
'email' => '',
|
||||
'contact_num' => '',
|
||||
'address' => '',
|
||||
];
|
||||
$other_data = [
|
||||
'odometer' => 0,
|
||||
'date_purchase' => $today->format('Y-m-d'),
|
||||
'invoice' => '',
|
||||
'warr_card' => '',
|
||||
'dealer_name' => '',
|
||||
'dealer_address' => '',
|
||||
'branch_code' => '',
|
||||
];
|
||||
|
||||
// set action for raffle log
|
||||
$raffle_data['action'] = 'serial_check_customer';
|
||||
}
|
||||
|
||||
$sku = $warr_serial->getSKU();
|
||||
$batt = null;
|
||||
$cat_name = '';
|
||||
if ($sku != null)
|
||||
$batt = $this->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' => '',
|
||||
];
|
||||
}
|
||||
|
||||
// set the rest of the raffle log entry
|
||||
$raffle_data['bmodel_name'] = $battery['brand'];
|
||||
$raffle_data['bsize_name'] = $battery['size'];
|
||||
|
||||
// log the raffle log
|
||||
$raffle_logger->logRaffleInfo($data_sent, $raffle_data);
|
||||
|
||||
// response
|
||||
return new ApiResponse(true, '', [
|
||||
'is_valid' => true,
|
||||
'is_registered' => $is_registered,
|
||||
'can_edit' => $can_edit,
|
||||
'customer' => $customer,
|
||||
'battery' => $battery,
|
||||
'odometer' => $other_data['odometer'],
|
||||
'invoice' => $other_data['invoice'],
|
||||
'warr_card' => $other_data['warr_card'],
|
||||
'date_purchase' => $other_data['date_purchase'],
|
||||
'dealer_name' => $other_data['dealer_name'],
|
||||
'dealer_address' => $other_data['dealer_address'],
|
||||
'branch_code' => $other_data['branch_code'],
|
||||
'message' => [
|
||||
'register_error' => 'Warranty serial code has already been registered.',
|
||||
'edit_error' => 'Sorry, warranty is registered under another vehicle not in your list of vehicles.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function warrantyRegister(
|
||||
$serial,
|
||||
Request $req,
|
||||
KernelInterface $kernel,
|
||||
RisingTideGateway $rt,
|
||||
TranslatorInterface $trans,
|
||||
WarrantyRaffleLogger $raffle_logger,
|
||||
WarrantyAPILogger $logger
|
||||
) {
|
||||
// validate params
|
||||
$this->validateRequest($req, [
|
||||
'first_name',
|
||||
'last_name',
|
||||
'plate_number',
|
||||
'date_purchase',
|
||||
]);
|
||||
|
||||
// handle file uploads
|
||||
$invoice = $req->files->get('invoice');
|
||||
$warr_card = $req->files->get('warr_card');
|
||||
|
||||
// normalize serial
|
||||
$serial = $this->cleanSerial($serial);
|
||||
// $serial = trim(strtoupper($serial));
|
||||
|
||||
// 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');
|
||||
|
||||
$user_id = $req->query->get('api_key');
|
||||
$log_data = [
|
||||
'plate_number' => $req->request->get('plate_num'),
|
||||
'first_name' => $req->request->get('first_name'),
|
||||
'last_name' => $req->request->get('last_name'),
|
||||
'date_purchase' => $req->request->get('date_purchase'),
|
||||
];
|
||||
$action = 'create/update';
|
||||
$source = WarrantySource::MOBILE;
|
||||
|
||||
// update customer information
|
||||
// $cust = $this->updateCustomerInfo($req, $em);
|
||||
|
||||
// update warranty
|
||||
$res = $this->updateWarranty(
|
||||
$rt,
|
||||
$trans,
|
||||
$req,
|
||||
$serial,
|
||||
$inv_filename,
|
||||
$wcard_filename,
|
||||
$logger,
|
||||
$log_data,
|
||||
$user_id,
|
||||
$action,
|
||||
$source,
|
||||
$raffle_logger
|
||||
);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
protected function handlePictureUpload($file, $target_dir, $serial, $name)
|
||||
{
|
||||
// error_log("handling $name upload");
|
||||
// no file sent
|
||||
if ($file == null) {
|
||||
error_log("handling $name upload - 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;
|
||||
}
|
||||
|
||||
// TODO: put this in a service
|
||||
protected function cleanSerial($serial)
|
||||
{
|
||||
// trim and make everything upper case
|
||||
$clean_serial = trim(strtoupper($serial));
|
||||
|
||||
|
||||
// remove QR prefix if it exists
|
||||
// $prefix = substr($clean_serial, 0, 2);
|
||||
// if ($prefix == 'QR')
|
||||
// $clean_serial = substr($clean_serial, 2);
|
||||
|
||||
return $clean_serial;
|
||||
}
|
||||
|
||||
protected function checkCustomerPlateNumber($plate_number, $cust)
|
||||
{
|
||||
// strip spaces and make all caps
|
||||
$plate_number = preg_replace('/\s+/', '', strtoupper($plate_number));
|
||||
|
||||
// if there's no customer linked to session
|
||||
if ($cust != null) {
|
||||
// check all the customer vehicles
|
||||
$cvs = $cust->getVehicles();
|
||||
foreach ($cvs as $cv) {
|
||||
$cv_plate = preg_replace('/\s+/', '', strtoupper($cv->getPlateNumber()));
|
||||
|
||||
// did we find a match?
|
||||
if ($cv_plate == $plate_number) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function updateWarranty($rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, $logger, $log_data, $user_id, $action, $source, $raffle_logger)
|
||||
{
|
||||
// prepare raffle log entry
|
||||
$raffle_data = [
|
||||
'user_id' => $user_id,
|
||||
'serial' => $serial,
|
||||
'warranty_id' => null,
|
||||
'action' => '',
|
||||
'bmodel_name' => '',
|
||||
'bsize_name' => '',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'plate_number' => '',
|
||||
'contact_num' => '',
|
||||
'email' => '',
|
||||
'address' => '',
|
||||
];
|
||||
|
||||
// get serial
|
||||
$warr_serial = $this->em->getRepository(WarrantySerial::class)->find($serial);
|
||||
if ($warr_serial == null) {
|
||||
return new ApiResponse(false, 'Invalid warranty serial code.');
|
||||
}
|
||||
|
||||
// check if warranty exists already
|
||||
$warr = $this->em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
|
||||
|
||||
// skip warranty if it already exists
|
||||
if ($warr != null) {
|
||||
/*
|
||||
// NOTE: we could not update in the old version
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Warranty registration entry already exists.');
|
||||
return $res;
|
||||
*/
|
||||
|
||||
// check if warranty is registered to a serial owned by customer
|
||||
$warr_plate = $warr->getPlateNumber();
|
||||
$cust = $this->session->getCustomer();
|
||||
$is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust);
|
||||
|
||||
if (!$is_customer_warranty) {
|
||||
$error_msg = 'Warranty registered to a vehicle not in your list of vehicles.';
|
||||
|
||||
// set action to update
|
||||
$action = 'update';
|
||||
$logger->logWarrantyInfo($log_data, $error_msg, $user_id, $action, $source);
|
||||
|
||||
// response
|
||||
return new ApiResponse(false, $error_msg);
|
||||
}
|
||||
|
||||
$sms_msg = $trans->trans('warranty_update_confirm');
|
||||
|
||||
// update raffle data action
|
||||
$raffle_data['action'] = 'warranty_update';
|
||||
} else {
|
||||
$warr = new Warranty();
|
||||
$sms_msg = $trans->trans('warranty_register_confirm');
|
||||
|
||||
// set warranty source
|
||||
$warr->setCreateSource($source);
|
||||
|
||||
// update raffle data action
|
||||
$raffle_data['action'] = 'warranty_create';
|
||||
}
|
||||
|
||||
// get sap battery
|
||||
$sku = $warr_serial->getSKU();
|
||||
$sap_bty = null;
|
||||
if ($sku != null) {
|
||||
$sap_bty = $this->em->getRepository(SAPBattery::class)->find($sku);
|
||||
if ($sap_bty == null) {
|
||||
$error_msg = 'Could not find battery entry for warranty.';
|
||||
$logger->logWarrantyInfo($log_data, $error_msg, $user_id, $action, $source);
|
||||
|
||||
// response
|
||||
return new ApiResponse(false, $error_msg);
|
||||
}
|
||||
}
|
||||
|
||||
// default date purchase to today
|
||||
// NOTE: might need to change this later
|
||||
$date_pur = new DateTime();
|
||||
|
||||
// get date purchase specified by customer
|
||||
$date_pur_cust = DateTime::createFromFormat('Y-m-d', $req->request->get('date_purchase'));
|
||||
if (!$date_pur_cust) {
|
||||
$error_msg = 'Invalid date format for date of purchase.';
|
||||
$logger->logWarrantyInfo($log_data, $error_msg, $user_id, $action, $source);
|
||||
|
||||
// response
|
||||
return new ApiResponse(false, $error_msg);
|
||||
}
|
||||
|
||||
$customer = $this->session->getCustomer();
|
||||
if ($customer != null) {
|
||||
$warr->setCustomer($customer);
|
||||
// get customer vehicles
|
||||
|
||||
$vehicle = $this->findCustomerVehicle($customer, $req->request->get('plate_number'));
|
||||
if ($vehicle != null)
|
||||
$warr->setVehicle($vehicle);
|
||||
}
|
||||
|
||||
// TODO: make a standard clean plate number service
|
||||
// clean plate number
|
||||
$plate = $req->request->get('plate_number');
|
||||
// upper case and remove spaces
|
||||
$plate = strtoupper(str_replace(' ', '', $plate));
|
||||
// remove special characters
|
||||
$plate = preg_replace('/[^A-Za-z0-9. -]/', '', $plate);
|
||||
|
||||
// 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($plate)
|
||||
// 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'))
|
||||
->setDealerName($req->request->get('dealer_name'))
|
||||
->setDealerAddress($req->request->get('dealer_address'))
|
||||
->setDealerBranchCode($req->request->get('branch_code'))
|
||||
->setValidated(false);
|
||||
|
||||
// TODO: check for date purchase and date expire
|
||||
|
||||
$this->em->persist($warr);
|
||||
|
||||
// TODO: check if we need to do anyting else
|
||||
$logger->logWarrantyInfo($log_data, '', $user_id, $action, $source);
|
||||
|
||||
// send sms
|
||||
// error_log('sending sms to - ' . $this->session->getPhoneNumber());
|
||||
$rt->sendSMS($this->session->getPhoneNumber(), $trans->trans('message.battery_brand_allcaps'), $sms_msg);
|
||||
|
||||
// prepare the rest of the raffle log entry
|
||||
$raffle_data['warranty_id'] = $warr->getID();
|
||||
$raffle_data['bmodel_name'] = $sap_bty->getBrand()->getName();
|
||||
$raffle_data['bsize_name'] = $sap_bty->getSize()->getName();
|
||||
$raffle_data['first_name'] = $req->request->get('first_name', '');
|
||||
$raffle_data['last_name'] = $req->request->get('last_name', '');
|
||||
$raffle_data['plate_number'] = $plate;
|
||||
$raffle_data['contact_num'] = $req->request->get('contact_num', '');
|
||||
$raffle_data['email'] = $req->request->get('email', '');
|
||||
$raffle_data['address'] = $req->request->get('cust_address', '');
|
||||
|
||||
$data_sent = [
|
||||
'plate_number' => $req->request->get('plate_number'),
|
||||
'first_name' => $req->request->get('first_name'),
|
||||
'last_name' => $req->request->get('last_name'),
|
||||
'date_purchase' => $req->request->get('date_purchase'),
|
||||
'address' => $req->request->get('cust_address', ''),
|
||||
'email' => $req->request->get('email', ''),
|
||||
'contact_num' => $req->request->get('contact_num', ''),
|
||||
];
|
||||
|
||||
// log raffle data
|
||||
$raffle_logger->logRaffleInfo($data_sent, $raffle_data);
|
||||
|
||||
// response
|
||||
return new ApiResponse();
|
||||
}
|
||||
|
||||
protected function findCustomerVehicle($customer, $plate_number)
|
||||
{
|
||||
$clean_plate = Warranty::cleanPlateNumber($plate_number);
|
||||
if ($clean_plate) {
|
||||
// find the customer vehicle and get the vehicle
|
||||
$cv = $this->em->getRepository(CustomerVehicle::class)->findOneBy(['plate_number' => $clean_plate, 'customer' => $customer]);
|
||||
if ($cv != null) {
|
||||
$vehicle = $cv->getVehicle();
|
||||
return $vehicle;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue