diff --git a/config/services.yaml b/config/services.yaml index 29231ef0..dea32a8b 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -284,3 +284,8 @@ services: App\Service\HubFilterLogger: arguments: $em: "@doctrine.orm.entity_manager" + + # warranty api logger + App\Service\WarrantyAPILogger: + arguments: + $em: "@doctrine.orm.entity_manager" diff --git a/src/Command/GenerateWarrantyFromJobOrderCommand.php b/src/Command/GenerateWarrantyFromJobOrderCommand.php index a5c64488..fa2990a0 100644 --- a/src/Command/GenerateWarrantyFromJobOrderCommand.php +++ b/src/Command/GenerateWarrantyFromJobOrderCommand.php @@ -16,20 +16,25 @@ use App\Entity\JobOrder; use App\Entity\Warranty; use App\Entity\SAPBattery; +use App\Service\WarrantyAPILogger; + use App\Ramcar\ServiceType; use App\Ramcar\WarrantyStatus; +use App\Ramcar\WarrantySource; use DoctrineExtensions\Query\Mysql\DateFormat; class GenerateWarrantyFromJobOrderCommand extends Command { protected $em; + protected $logger; protected $sapbatt_hash; protected $warranties_hash; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, WarrantyAPILogger $logger) { $this->em = $em; + $this->logger = $logger; $this->loadSAPBatteries(); @@ -209,7 +214,27 @@ class GenerateWarrantyFromJobOrderCommand extends Command $sql_statement = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,serial,warranty_class,plate_number,status,date_create,date_purchase,date_expire,date_claim,sap_bty_id,claim_id,first_name,last_name,mobile_number,flag_activated,warranty_privacy_policy) VALUES ' . $values . "\n"; - echo $sql_statement; + echo $sql_statement; + + $db = $this->em->getConnection(); + $stmt = $db->prepare($sql_statement); + $stmt->execute(); + + // log warranty creation + $log_data = [ + 'battery_model_id' => $bty_model_id, + 'battery_size_id' => $bty_size_id, + 'warranty_class' => $warranty_class, + 'plate_number' => $cleaned_plate_number, + 'date_create' => $date_create, + 'date_purchase' => $date_purchase, + 'date_expire' => $date_expire, + 'sap_code' => $sap_code, + 'first_name' => $first_name, + 'last_name' => $last_name, + 'mobile_number' => $mobile_number, + ]; + $this->logger->logWarrantyInfo($log_data, '', 'internal', 'create', WarrantySource::COMMAND); } else diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 24aaff76..f7004295 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -30,6 +30,7 @@ use App\Ramcar\AdvanceOrderSlot; use App\Ramcar\AutoAssignStatus; use App\Ramcar\HubCriteria; use App\Ramcar\WillingToWaitContent; +use App\Ramcar\WarrantySource; use App\Service\InvoiceGeneratorInterface; use App\Service\RisingTideGateway; @@ -42,6 +43,7 @@ use App\Service\RiderAssignmentHandlerInterface; use App\Service\HubSelector; use App\Service\HubDistributor; use App\Service\HubFilterLogger; +use App\Service\WarrantyAPILogger; use App\Entity\MobileSession; use App\Entity\Customer; @@ -2977,11 +2979,7 @@ class APIController extends Controller implements LoggedController } $sku = $warr_serial->getSKU(); - - // check if sku is null - $batt = null; - if ($sku != null) - $batt = $em->getRepository(SAPBattery::class)->find($sku); + $batt = $em->getRepository(SAPBattery::class)->find($sku); // TODO: put this in a config file $image_url = $req->getSchemeAndHttpHost() . '/battery/generic.png'; if ($batt != null) @@ -3055,7 +3053,8 @@ class APIController extends Controller implements LoggedController return $serial . '/' . $filename; } - public function warrantyRegister($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans) + public function warrantyRegister($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, + TranslatorInterface $trans, WarrantyAPILogger $logger) { // check required parameters and api key $required_params = [ @@ -3077,22 +3076,37 @@ class APIController extends Controller implements LoggedController $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'; + $source = WarrantySource::MOBILE; + $res = $this->checkParamsAndKey($req, $em, $required_params); if ($res->isError()) + { + $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); return $res->getReturnResponse(); + } // update customer information // $cust = $this->updateCustomerInfo($req, $em); // update warranty - $res = $this->updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename); + $res = $this->updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, + $logger, $log_data, $user_id, $action, $source); $em->flush(); return $res->getReturnResponse(); } - protected function updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null) + protected function updateWarranty($res, $em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, + $logger, $log_data, $user_id, $action, $source) { // get serial $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); @@ -3100,6 +3114,7 @@ class APIController extends Controller implements LoggedController { $res->setError(true) ->setErrorMessage('Invalid warranty serial code.'); + $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); return $res; } @@ -3125,6 +3140,7 @@ class APIController extends Controller implements LoggedController { $res->setError(true) ->setErrorMessage('Warranty registered to a vehicle not in your list of vehicles.'); + $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); return $res; } @@ -3141,7 +3157,16 @@ class APIController extends Controller implements LoggedController $sku = $warr_serial->getSKU(); $sap_bty = null; if ($sku != null) + { $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); + if ($sap_bty == null) + { + $res->setError(true) + ->setErrorMessage('Could not find battery entry for warranty.'); + $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); + return $res; + } + } // default date purchase to today // NOTE: might need to change this later @@ -3153,6 +3178,7 @@ class APIController extends Controller implements LoggedController { $res->setError(true) ->setErrorMessage('Invalid date format for date of purchase.'); + $logger->logWarrantyInfo($log_data, $res->getErrorMessage(), $user_id, $action, $source); return $res; } @@ -3197,6 +3223,8 @@ class APIController extends Controller implements LoggedController // set data to retrun to user $res->setData($data); + $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); + // send sms error_log('sending sms to - ' . $this->session->getPhoneNumber()); $rt->sendSMS($this->session->getPhoneNumber(), 'MOTOLITE', $sms_msg); diff --git a/src/Controller/CAPI/CustomerWarrantyController.php b/src/Controller/CAPI/CustomerWarrantyController.php index 52b305c9..96b0a43c 100644 --- a/src/Controller/CAPI/CustomerWarrantyController.php +++ b/src/Controller/CAPI/CustomerWarrantyController.php @@ -14,6 +14,7 @@ use Catalyst\APIBundle\Controller\APIController; use Catalyst\APIBundle\Response\APIResponse; use App\Service\RisingTideGateway; +use App\Service\WarrantyAPILogger; use App\Entity\WarrantySerial; use App\Entity\Warranty; @@ -32,6 +33,7 @@ use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; use App\Ramcar\FuelType; use App\Ramcar\VehicleStatusCondition; +use App\Ramcar\WarrantySource; use DateTime; @@ -73,13 +75,14 @@ class CustomerWarrantyController extends APIController return $missing; } - protected function checkRequiredParams(Request $req, $params) + 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); } @@ -96,16 +99,32 @@ class CustomerWarrantyController extends APIController return preg_replace('/\s+/', '', strtoupper($plate_num)); } - public function check($serial, EntityManagerInterface $em, Request $req) + public function check($serial, EntityManagerInterface $em, Request $req, WarrantyAPILogger $logger) { + $user_id = $_SERVER['HTTP_X_CATA_API_KEY']; + $log_data = [ + 'serial' => $serial, + ]; + $action = 'check'; + $source = WarrantySource::CAPI; + // check required parameters $required_params = []; - $res = $this->checkRequiredParams($req, $required_params); - if (!$res) - return $res; + $res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action, $source); + if (!$res) + return $res; error_log('check warranty serial'); + //$keys = array_keys($_SERVER); + //foreach ($keys as $key) + //{ + // error_log($key); + //} + + //error_log($_SERVER['HTTP_X_CATA_API_KEY']); + + // 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]); @@ -279,20 +298,40 @@ class CustomerWarrantyController extends APIController } - public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans) + public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans, + WarrantyAPILogger $logger) { error_log('HERE - register'); // check required parameters // TODO: maybe add vmake_id? since warranty cannot be created with no vmake // TODO: maye also add mobile and email since customer creation won't let mobile and email be null + // 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'; + $source = WarrantySource::CAPI; + + error_log('SOURCE: ' . $source); + $required_params = [ 'first_name', 'last_name', 'plate_num' ]; - $res = $this->checkRequiredParams($req, $required_params); - if (!$res) - return $res; + $res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action, $source); + + if (!$res) + return $res; // file uploads $invoice = $req->files->get('invoice'); @@ -305,12 +344,13 @@ class CustomerWarrantyController extends APIController $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); + $res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, + $logger, $log_data, $user_id, $action, $source); // flush to db $em->flush(); - return $res; + return $res; return new APIResponse(true, 'Warranty registered.'); } @@ -346,7 +386,8 @@ class CustomerWarrantyController extends APIController return $serial . '/' . $filename; } - protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null) + protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, + $logger, $log_data, $user_id, $action, $source) { $plate_num = $this->cleanPlateNumber($req->request->get('plate_num')); @@ -355,6 +396,7 @@ class CustomerWarrantyController extends APIController $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.'); } @@ -370,6 +412,7 @@ class CustomerWarrantyController extends APIController 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.'); } @@ -393,10 +436,11 @@ class CustomerWarrantyController extends APIController if ($sku != null) { $sap_bty = $em->getRepository(SAPBattery::class)->find($sku); - //if ($sap_bty == null) - //{ - // return new APIResponse(false, 'Could not find battery entry for warranty.'); - //} + 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 @@ -407,6 +451,7 @@ class CustomerWarrantyController extends APIController $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.'); } } @@ -423,6 +468,7 @@ class CustomerWarrantyController extends APIController $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.'); } } @@ -499,14 +545,15 @@ class CustomerWarrantyController extends APIController $em->persist($warr); + $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); + // TODO: check if we need to do anyting else $data = []; - // send sms confirmation $this->sendSMSConfirmation($rt, $req->request->get('contact_num'), $sms_message); - return new APIResponse(true, 'Warranty registered.', $data); + return new APIResponse(true, 'Warranty registered.', $data); } protected function sendSMSConfirmation($rt, $num, $message) diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index 31dbec58..6e179c09 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -20,12 +20,16 @@ use App\Entity\PrivacyPolicy; use App\Entity\Customer; use App\Entity\CustomerVehicle; use App\Entity\Vehicle; +use App\Entity\WarrantyAPILog; + +use App\Service\WarrantyAPILogger; 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; @@ -142,7 +146,7 @@ class WarrantyController extends APIController return new APIResponse(true, 'Warranties found.', $data); } - public function register(Request $req, EntityManagerInterface $em) + public function register(Request $req, EntityManagerInterface $em, WarrantyAPILogger $logger) { $this->denyAccessUnlessGranted('warranty.register.battery', null, 'No access.'); @@ -159,10 +163,6 @@ class WarrantyController extends APIController 'battery_size_id', */ ]; - $msg = $this->checkRequiredParameters($req, $params); - error_log('msg - ' . $msg); - if ($msg) - return new APIResponse(false, $msg); $serial = $req->request->get('serial'); $date_expire_string = $req->request->get('date_expire'); @@ -175,6 +175,31 @@ class WarrantyController extends APIController $lname = $req->request->get('last_name', null); $mnum = $req->request->get('mobile_number', null); + // set up information for logging + // get user from header + $user_id = $_SERVER['HTTP_X_CATA_API_KEY']; + $log_data = [ + 'serial' => $serial, + 'date_expire' => $date_expire_string, + 'date_pur_string' => $date_pur_string, + 'warranty_class' => $warr_class, + 'plate_number' => $plate, + 'sku' => $sku, + 'first_name' => $fname, + 'last_name' => $lname, + 'mobile_number' => $mnum, + ]; + $action = 'create'; + $source = WarrantySource::CAPI; + + $msg = $this->checkRequiredParameters($req, $params); + error_log('msg - ' . $msg); + if ($msg) + { + $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source); + return new APIResponse(false, $msg); + } + /* $bmodel_id = $req->request->get('battery_model_id'); $bsize_id = $req->request->get('battery_size_id'); @@ -183,21 +208,33 @@ class WarrantyController extends APIController // wrong date expire format $date_expire = DateTime::createFromFormat('Ymd', $date_expire_string); if ($date_expire === false) + { + $logger->logWarrantyInfo($log_data, 'Wrong date format: date_expire.', $user_id, $action, $source); return new APIResponse(false, 'Wrong date format: date_expire.'); + } // wrong date purchase format $date_pur = DateTime::createFromFormat('Ymd', $date_pur_string); if ($date_pur === false) + { + $logger->logWarrantyInfo($log_data, 'Wrong date format: date_purchase', $user_id, $action, $source); return new APIResponse(false, 'Wrong date format: date_purchase.'); + } // valid warranty class if (!WarrantyClass::validate($warr_class)) + { + $logger->logWarrantyInfo($log_data, 'Invalid warranty class.', $user_id, $action, $source); return new APIResponse(false, 'Invalid warranty class.'); + } // plate number $plate = Warranty::cleanPlateNumber($plate); if (!$plate) + { + $logger->logWarrantyInfo($log_data, 'Invalid plate number.', $user_id, $action, $source); return new APIResponse(false, 'Invalid plate number.'); + } // check if sku is blank if ((empty($sku)) || ($sku == null)) @@ -207,7 +244,10 @@ class WarrantyController extends APIController // battery $batt = $em->getRepository(SAPBattery::class)->find($sku); if ($batt == null) + { + $logger->logWarrantyInfo($log_data, 'Invalid battery SKU.', $user_id, $action, $source); return new APIResponse(false, 'Invalid battery SKU.'); + } } /* @@ -245,6 +285,7 @@ class WarrantyController extends APIController } catch (UniqueConstraintViolationException $e) { + $logger->logWarrantyInfo($log_data, 'Duplicate serial encountered.', $user_id, $action, $source); return new APIResponse(false, 'Duplicate serial encountered.'); } @@ -253,10 +294,13 @@ class WarrantyController extends APIController 'warranty' => $this->generateWarrantyData($warr), ]; + // log creation of warranty data + $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); + return new APIResponse(true, 'Warranty registered.', $data); } - public function claim(Request $req, EntityManagerInterface $em, $id) + public function claim(Request $req, EntityManagerInterface $em, $id, WarrantyAPILogger $logger) { $this->denyAccessUnlessGranted('warranty.claim', null, 'No access.'); @@ -264,26 +308,50 @@ class WarrantyController extends APIController $params = [ 'serial', ]; + + $serial = $req->request->get('serial'); + + // set up information for logging + // get user from header + $user_id = $_SERVER['HTTP_X_CATA_API_KEY']; + $log_data = [ + 'serial' => $serial, + 'id' => $id, + ]; + $action = 'claim'; + $source = WarrantySource::CAPI; + $msg = $this->checkRequiredParameters($req, $params); if ($msg) + { + $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source); return new APIResponse(false, $msg); + } // no warranty $warr = $em->getRepository(Warranty::class)->find($id); if ($warr == null) + { + $logger->logWarrantyInfo($log_data, 'No warranty found with that id.', $user_id, $action, $source); return new APIResponse(false, 'No warranty found with that id.', null, 404); + } // warranty is not active if (!$warr->canClaim()) + { + $logger->logWarrantyInfo($log_data, 'Warranty is not active.', $user_id, $action, $source); return new APIResponse(false, 'Warranty is not active.'); + } // check if new serial has been used - $serial = $req->request->get('serial'); $clean_serial = $this->cleanSerial($serial); $check_warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]); if ($check_warr != null) + { + $logger->logWarrantyInfo($log_data, 'Serial for replacement has already been used.', $user_id, $action, $source); return new APIResponse(false, 'Serial for replacement has already been used.'); + } // set status to claim $warr->setStatus(WarrantyStatus::CLAIMED) @@ -309,6 +377,7 @@ class WarrantyController extends APIController $em->flush(); // TODO: claim log + $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); return new APIResponse(true, 'Warranty claimed successfully.'); } @@ -704,5 +773,4 @@ class WarrantyController extends APIController $customers = $em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]); return $customers; } - } diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index 2208ecb1..c078ac18 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -12,8 +12,10 @@ use App\Entity\CustomerVehicle; use App\Ramcar\WarrantyClass; use App\Ramcar\WarrantyStatus; +use App\Ramcar\WarrantySource; use App\Service\WarrantyHandler; +use App\Service\WarrantyAPILogger; use Doctrine\ORM\Query; use Doctrine\ORM\EntityManagerInterface; @@ -149,7 +151,7 @@ class WarrantyController extends Controller return $this->render('warranty/form.html.twig', $params); } - public function addSubmit(Request $req, ValidatorInterface $validator) + public function addSubmit(Request $req, ValidatorInterface $validator, WarrantyAPILogger $logger) { $this->denyAccessUnlessGranted('warranty.add', null, 'No access.'); @@ -255,6 +257,28 @@ class WarrantyController extends Controller $em->persist($obj); $em->flush(); + // log warranty creation + $action = 'create'; + $user_id = $this->getUser()->getUsername(); + $source = WarrantySource::ADMIN_PANEL; + $log_data = [ + 'serial' => $req->request->get('serial'), + 'warranty_class' => $req->request->get('warranty_class'), + 'first_name' => $req->request->get('first_name'), + 'last_name' => $req->request->get('last_name'), + 'mobile_number' => $req->request->get('mobile_number'), + 'date_purchase' => $req->request->get('date_purchase'), + 'claim_from' => $req->request->get('claim_from'), + 'status' => $req->request->get('status'), + 'date_claim' => $req->request->get('date_claim'), + 'date_expire' => $req->request->get('date_expire'), + 'battery_model' => $req->request->get('battery_model'), + 'battery_size' => $req->request->get('battery_size'), + 'sap_battery' => $req->request->get('sap_battery'), + 'plate_number' => $req->request->get('plate_number'), + ]; + $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); + // return successful response return $this->json([ 'success' => 'Changes have been saved!' @@ -640,8 +664,12 @@ class WarrantyController extends Controller } // error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number); + $user_id = $this->getUser()->getUsername(); + $source = WarrantySource::BULK_UPLOAD; + + $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class, + $user_id, $source); - $wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } } @@ -677,4 +705,5 @@ class WarrantyController extends Controller // remove spaces and make upper case return strtoupper(str_replace(' ', '', $plate)); } + } diff --git a/src/Entity/WarrantyAPILog.php b/src/Entity/WarrantyAPILog.php new file mode 100644 index 00000000..595ce3c3 --- /dev/null +++ b/src/Entity/WarrantyAPILog.php @@ -0,0 +1,163 @@ +date_create = new DateTime(); + $this->errors = new ArrayCollection(); + $this->all_data = []; + $this->action = ''; + $this->source = ''; + } + + public function getID() + { + return $this->id; + } + + public function setDateCreate(DateTime $date_create) + { + $this->date_create = $date_create; + return $this; + } + + public function getDateCreate() + { + return $this->date_create; + } + + public function setApiUser($api_user) + { + $this->api_user = $api_user; + return $this; + } + + public function getApiUser() + { + return $this->api_user; + } + + public function addAllData($id, $value) + { + $this->all_data[$id] = $value; + return $this; + } + + public function setAllData($all_data) + { + $this->all_data = $all_data; + return $this; + } + + public function getAllData($id) + { + // return null if we don't have it + if (!isset($this->all_data[$id])) + return null; + + return $this->all_data[$id]; + } + + public function getErrors() + { + return $this->errors; + } + + public function setErrors(array $errors) + { + $this->errors = new ArrayCollection(); + + foreach ($errors as $error) + { + $this->errors->add($error); + } + + return $this; + } + + public function clearErrors() + { + $this->errors = new ArrayCollection(); + return $this; + } + + public function setAction($action) + { + $this->action = $action; + return $this; + } + + public function getAction() + { + return $this->action; + } + + public function setSource($source) + { + $this->source = $source; + return $this; + } + + public function getSource() + { + return $this->source; + } +} diff --git a/src/Ramcar/WarrantySource.php b/src/Ramcar/WarrantySource.php new file mode 100644 index 00000000..1697d9f6 --- /dev/null +++ b/src/Ramcar/WarrantySource.php @@ -0,0 +1,22 @@ + 'Third Party API', + 'rapi' => 'Rider API', + 'admin_panel' => 'Admin Panel', + 'bulk_upload' => 'Bulk Upload', + 'mobile' => 'Mobile API', + 'command' => 'Command', + ]; +} diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index fd1fb2af..6b736bf2 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -41,6 +41,7 @@ use App\Ramcar\CustomerNotWaitReason; use App\Ramcar\NoTradeInReason; use App\Ramcar\WillingToWaitContent; use App\Ramcar\HubCriteria; +use App\Ramcar\WarrantySource; use App\Service\InvoiceGeneratorInterface; use App\Service\JobOrderHandlerInterface; @@ -1025,7 +1026,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface } } - $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); + $user_id = $user->getUsername(); + $source = WarrantySource::ADMIN_PANEL; + $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class, $user_id, $source); } else error_log('Invalid plate number for warranty. Plate number = ' . $obj->getCustomerVehicle()->getPlateNumber()); diff --git a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php index ae6d2199..6f1711c3 100644 --- a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php @@ -13,6 +13,7 @@ use App\Ramcar\JOEventType; use App\Ramcar\InvoiceStatus; use App\Ramcar\ModeOfPayment; use App\Ramcar\InvoiceCriteria; +use App\Ramcar\WarrantySource; use App\Service\RiderAPIHandlerInterface; use App\Service\RedisClientProvider; @@ -598,7 +599,10 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface } } - $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); + // for riders, use rider session id + $user_id = $this->session->getID(); + $source = WarrantySource::RAPI; + $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class, $user_id, $source); } // send mqtt event (fulfilled) diff --git a/src/Service/WarrantyAPILogger.php b/src/Service/WarrantyAPILogger.php new file mode 100644 index 00000000..eb21ad5d --- /dev/null +++ b/src/Service/WarrantyAPILogger.php @@ -0,0 +1,37 @@ +em = $em; + } + + public function logWarrantyInfo($log_data, $error, $user_id, $action, $source) + { + $log_entry = new WarrantyAPILog(); + + $err_aray = []; + $err_array[] = $error; + + $json_data = json_encode($log_data); + + $log_entry->setApiUser($user_id) + ->setErrors($err_array) + ->setAllData($json_data) + ->setAction($action) + ->setSource($source); + + $this->em->persist($log_entry); + $this->em->flush(); + + } +} diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index 6d01eb10..6b86849f 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -11,6 +11,8 @@ use App\Entity\SAPBattery; use App\Entity\BatteryModel; use App\Entity\CustomerVehicle; +use App\Service\WarrantyAPILogger; + use App\Ramcar\WarrantyClass; use DateTime; @@ -19,18 +21,23 @@ use DateInterval; class WarrantyHandler { protected $em; + protected $logger; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, WarrantyAPILogger $logger) { $this->em = $em; + $this->logger = $logger; } public function createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, - $batt_list, DateTime $date_purchase, $warranty_class) + $batt_list, DateTime $date_purchase, $warranty_class, $user_id, $source) { // new warranty $warranty = new Warranty(); + $bmodel_name = ''; + $bsize_name =''; + $sap_batt_id = ''; foreach ($batt_list as $battery) { // get the battery model and battery size @@ -43,10 +50,12 @@ class WarrantyHandler if ($bty_model != null) { $warranty->setBatteryModel($bty_model); + $bmodel_name = $bty_model->getName(); } if ($bty_size != null) { $warranty->setBatterySize($bty_size); + $bsize_name = $bty_size->getName(); } $sap_code = $battery->getSAPCode(); @@ -57,6 +66,7 @@ class WarrantyHandler if ($sap_battery != null) { $warranty->setSAPBattery($sap_battery); + $sap_batt_id = $sap_battery->getID(); } } } @@ -87,6 +97,22 @@ class WarrantyHandler $this->em->persist($warranty); $this->em->flush(); + // log warranty creation + $action = 'create'; + $log_data = [ + 'serial' => $serial, + 'warranty_class' => $warranty_class, + 'first_name' => $first_name, + 'last_name' => $last_name, + 'mobile_number' => $mobile_number, + 'date_purchase' => $date_purchase->format('d-M-y'), + 'date_expire' => $date_expire->format('d-M-y'), + 'battery_model' => $bmodel_name, + 'battery_size' => $bsize_name, + 'sap_battery' => $sap_batt_id, + 'plate_number' => $plate_number, + ]; + $this->logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); // update customer vehicle with warranty info $this->updateCustomerVehicle($serial, $batt_list, $plate_number, $date_expire);