Resolve "Make standard warranty adding" #1142
7 changed files with 419 additions and 126 deletions
|
|
@ -91,6 +91,10 @@ services:
|
||||||
arguments:
|
arguments:
|
||||||
$geofence_flag: "%env(GEOFENCE_ENABLE)%"
|
$geofence_flag: "%env(GEOFENCE_ENABLE)%"
|
||||||
|
|
||||||
|
App\Service\WarrantyHandler:
|
||||||
|
arguments:
|
||||||
|
$em: "@doctrine.orm.entity_manager"
|
||||||
|
|
||||||
App\Command\SetCustomerPrivacyPolicyCommand:
|
App\Command\SetCustomerPrivacyPolicyCommand:
|
||||||
arguments:
|
arguments:
|
||||||
$policy_promo: "%env(POLICY_PROMO)%"
|
$policy_promo: "%env(POLICY_PROMO)%"
|
||||||
|
|
|
||||||
75
src/Command/ComputeWarrantyExpiryDateCommand.php
Normal file
75
src/Command/ComputeWarrantyExpiryDateCommand.php
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Command;
|
||||||
|
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
|
use App\Entity\Battery;
|
||||||
|
|
||||||
|
use App\Service\WarrantyHandler;
|
||||||
|
|
||||||
|
class ComputeWarrantyExpiryDateCommand extends Command
|
||||||
|
{
|
||||||
|
protected $em;
|
||||||
|
protected $wh;
|
||||||
|
|
||||||
|
public function __construct(ObjectManager $em, WarrantyHandler $wh)
|
||||||
|
{
|
||||||
|
$this->em = $em;
|
||||||
|
$this->wh = $wh;
|
||||||
|
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure()
|
||||||
|
{
|
||||||
|
$this->setName('warranty:computeexpirydate')
|
||||||
|
->setDescription('Compute expiry date for existing warranties.')
|
||||||
|
->setHelp('Compute expiry date for existing warranties.');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
{
|
||||||
|
$warr_q = $this->em->createQuery('select w from App\Entity\Warranty w where w.date_expire is null');
|
||||||
|
$warranties = $warr_q->iterate();
|
||||||
|
|
||||||
|
foreach($warranties as $row)
|
||||||
|
{
|
||||||
|
$warr = $row[0];
|
||||||
|
|
||||||
|
error_log('Processing warranty for ' . $warr->getID());
|
||||||
|
|
||||||
|
$date_purchase = $warr->getDatePurchase();
|
||||||
|
|
||||||
|
$batteries = $this->wh->getBatteriesForWarrantyPeriod($warr);
|
||||||
|
if (!empty($batteries))
|
||||||
|
{
|
||||||
|
$warranty_class = $warr->getWarrantyClass();
|
||||||
|
|
||||||
|
$warr_period = $this->wh->getWarrantyPeriod($batteries, $warranty_class);
|
||||||
|
|
||||||
|
if ($warr_period != null)
|
||||||
|
{
|
||||||
|
$expiry_date = $this->wh->computeDateExpire($date_purchase, $warr_period);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$expiry_date = $date_purchase;
|
||||||
|
}
|
||||||
|
|
||||||
|
// save expiry date
|
||||||
|
$warr->setDateExpire($expiry_date);
|
||||||
|
|
||||||
|
$this->em->persist($warr);
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->em->clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,7 +7,6 @@ use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Dotenv\Dotenv;
|
|
||||||
|
|
||||||
use Doctrine\Common\Persistence\ObjectManager;
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,13 @@ use App\Entity\SAPBattery;
|
||||||
use App\Entity\Battery;
|
use App\Entity\Battery;
|
||||||
use App\Entity\BatteryModel;
|
use App\Entity\BatteryModel;
|
||||||
use App\Entity\BatterySize;
|
use App\Entity\BatterySize;
|
||||||
|
use App\Entity\Invoice;
|
||||||
|
|
||||||
use App\Ramcar\WarrantyClass;
|
use App\Ramcar\WarrantyClass;
|
||||||
use App\Ramcar\WarrantyStatus;
|
use App\Ramcar\WarrantyStatus;
|
||||||
|
|
||||||
|
use App\Service\WarrantyHandler;
|
||||||
|
|
||||||
use Doctrine\ORM\Query;
|
use Doctrine\ORM\Query;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
@ -372,13 +375,14 @@ class WarrantyController extends Controller
|
||||||
/**
|
/**
|
||||||
* @Menu(selected="warranty_list")
|
* @Menu(selected="warranty_list")
|
||||||
*/
|
*/
|
||||||
public function uploadSubmit(Request $req, EntityManagerInterface $em)
|
public function uploadSubmit(Request $req, EntityManagerInterface $em,
|
||||||
|
WarrantyHandler $wh)
|
||||||
{
|
{
|
||||||
// retrieve temporary info for file
|
// retrieve temporary info for file
|
||||||
$file = $req->files->get('csv_file');
|
$file = $req->files->get('csv_file');
|
||||||
|
|
||||||
// process the csv file
|
// process the csv file
|
||||||
$inv_entries = $this->processWarrantyFile($file, $em);
|
$inv_entries = $this->processWarrantyFile($file, $em, $wh);
|
||||||
|
|
||||||
$resp = new StreamedResponse();
|
$resp = new StreamedResponse();
|
||||||
$resp->setCallback(function() use($inv_entries) {
|
$resp->setCallback(function() use($inv_entries) {
|
||||||
|
|
@ -422,7 +426,8 @@ class WarrantyController extends Controller
|
||||||
return $resp;
|
return $resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em)
|
protected function processWarrantyFile(UploadedFile $csv_file, EntityManagerInterface $em,
|
||||||
|
WarrantyHandler $wh)
|
||||||
{
|
{
|
||||||
// attempt to open file
|
// attempt to open file
|
||||||
try
|
try
|
||||||
|
|
@ -473,8 +478,9 @@ class WarrantyController extends Controller
|
||||||
$serial = trim($fields[10]);
|
$serial = trim($fields[10]);
|
||||||
$purchase_date = trim($fields[12]);
|
$purchase_date = trim($fields[12]);
|
||||||
$battery_id = trim($fields[16]);
|
$battery_id = trim($fields[16]);
|
||||||
|
$batt_invoice = trim($fields[11]);
|
||||||
|
|
||||||
$plate_number = $this->cleanPlateNumber($plate);
|
$plate_number = $wh->cleanPlateNumber($plate);
|
||||||
|
|
||||||
// check if purchase_date or plate_number or serial is empty or if
|
// check if purchase_date or plate_number or serial is empty or if
|
||||||
// purchase date is valid
|
// purchase date is valid
|
||||||
|
|
@ -511,7 +517,7 @@ class WarrantyController extends Controller
|
||||||
'vehicle_year' => trim($fields[8]),
|
'vehicle_year' => trim($fields[8]),
|
||||||
'vehicle_plate_number' => $plate_number,
|
'vehicle_plate_number' => $plate_number,
|
||||||
'battery_serial_number' => $serial,
|
'battery_serial_number' => $serial,
|
||||||
'battery_sales_invoice' => trim($fields[11]),
|
'battery_sales_invoice' => $batt_invoice,
|
||||||
'battery_date_purchase' => $purchase_date,
|
'battery_date_purchase' => $purchase_date,
|
||||||
'distributor_name' => trim($fields[13]),
|
'distributor_name' => trim($fields[13]),
|
||||||
'distributor_address' => trim($fields[14]),
|
'distributor_address' => trim($fields[14]),
|
||||||
|
|
@ -526,81 +532,42 @@ class WarrantyController extends Controller
|
||||||
// additional validation
|
// additional validation
|
||||||
// check if serial number and plate number already exists
|
// check if serial number and plate number already exists
|
||||||
$warr_results = $em->getRepository(Warranty::class)->findBy(['serial' => $serial, 'plate_number' => $plate_number]);
|
$warr_results = $em->getRepository(Warranty::class)->findBy(['serial' => $serial, 'plate_number' => $plate_number]);
|
||||||
|
|
||||||
|
// get battery via the invoice because battery_id doesn't match what's in the live data
|
||||||
|
// get job order via invoice to get the warranty class
|
||||||
|
$warranty_class = '';
|
||||||
|
$batt_list = array();
|
||||||
|
|
||||||
|
// find invoice
|
||||||
|
$invoice = $em->getRepository(Invoice::class)->find($batt_invoice);
|
||||||
|
if (!empty($invoice))
|
||||||
|
{
|
||||||
|
// get job order
|
||||||
|
$jo = $invoice->getJobOrder();
|
||||||
|
|
||||||
|
// get warranty class
|
||||||
|
$warranty_class = $jo->getWarrantyClass();
|
||||||
|
|
||||||
|
// get battery
|
||||||
|
$invoice_items = $invoice->getItems();
|
||||||
|
foreach ($invoice_items as $item)
|
||||||
|
{
|
||||||
|
$battery = $item->getBattery();
|
||||||
|
if ($battery != null)
|
||||||
|
{
|
||||||
|
$batt_list[] = $item->getBattery();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($warr_results))
|
if (!empty($warr_results))
|
||||||
{
|
{
|
||||||
foreach($warr_results as $warr)
|
foreach($warr_results as $warr)
|
||||||
{
|
{
|
||||||
// check if details are complete
|
// call service to check if warranty details is incomplete and then update warranty
|
||||||
//error_log('Updating warranty with serial number ' . $serial . ' and plate number ' . $plate_number);
|
// using details from csv file
|
||||||
if (empty($warr->getFirstName()))
|
//error_log('Updating warranty for ' . $warr->getID());
|
||||||
{
|
$wh->updateWarranty($warr, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase);
|
||||||
if (!empty($first_name))
|
|
||||||
{
|
|
||||||
$warr->setFirstName($first_name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (empty($warr->getLastName()))
|
|
||||||
{
|
|
||||||
if (!empty($last_name))
|
|
||||||
{
|
|
||||||
$warr->setLastName($last_name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (empty($warr->getMobileNumber()))
|
|
||||||
{
|
|
||||||
if (!empty($mobile_number))
|
|
||||||
{
|
|
||||||
$warr->setMobileNumber($mobile_number);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ((empty($warr->getBatteryModel())) ||
|
|
||||||
(empty($warr->getBatterySize())))
|
|
||||||
{
|
|
||||||
if (!empty($battery_id))
|
|
||||||
{
|
|
||||||
// find battery
|
|
||||||
$battery = $em->getRepository(Battery::class)->find($battery_id);
|
|
||||||
if (!empty($battery))
|
|
||||||
{
|
|
||||||
// get the battery model and battery size
|
|
||||||
$model_id = $battery->getModel()->getID();
|
|
||||||
$size_id = $battery->getSize()->getID();
|
|
||||||
|
|
||||||
$bty_model = $em->getRepository(BatteryModel::class)->find($model_id);
|
|
||||||
$bty_size = $em->getRepository(BatterySize::class)->find($size_id);
|
|
||||||
|
|
||||||
if ($bty_model != null)
|
|
||||||
{
|
|
||||||
$warr->setBatteryModel($bty_model);
|
|
||||||
}
|
|
||||||
if ($bty_size != null)
|
|
||||||
{
|
|
||||||
$warr->setBatterySize($bty_size);
|
|
||||||
}
|
|
||||||
$sap_code = $battery->getSAPCode();
|
|
||||||
if (!empty($sap_code))
|
|
||||||
{
|
|
||||||
// find sap battery
|
|
||||||
$sap_batt = $em->getRepository(SAPBattery::class)->find($sap_code);
|
|
||||||
if (!empty($sap_batt))
|
|
||||||
{
|
|
||||||
$warr->setSAPBattery($sap_batt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (empty($warr->getDatePurchase()))
|
|
||||||
{
|
|
||||||
if (!empty($date_purchase))
|
|
||||||
{
|
|
||||||
$warr->setDatePurchase($date_purchase);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: compute expiry date
|
|
||||||
$em->persist($warr);
|
|
||||||
$em->flush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -614,57 +581,10 @@ class WarrantyController extends Controller
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//error_log('Adding warranty with serial number ' . $serial . ' and plate number ' . $plate_number);
|
//error_log('Creating warranty for serial ' . $serial . ' and plate number ' . $plate_number);
|
||||||
// new warranty
|
|
||||||
$warranty = new Warranty();
|
|
||||||
|
|
||||||
// get the battery purchased
|
$wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class);
|
||||||
// check battery first. If not found, check sap_battery
|
|
||||||
$battery = $em->getRepository(Battery::class)->find($battery_id);
|
|
||||||
if ($battery != null)
|
|
||||||
{
|
|
||||||
// get the battery model and battery size
|
|
||||||
$model_id = $battery->getModel()->getID();
|
|
||||||
$size_id = $battery->getSize()->getID();
|
|
||||||
|
|
||||||
$bty_model = $em->getRepository(BatteryModel::class)->find($model_id);
|
|
||||||
$bty_size = $em->getRepository(BatterySize::class)->find($size_id);
|
|
||||||
|
|
||||||
if ($bty_model != null)
|
|
||||||
{
|
|
||||||
$warranty->setBatteryModel($bty_model);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($bty_size != null)
|
|
||||||
{
|
|
||||||
$warranty->setBatterySize($bty_size);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// find battery in sap_battery
|
|
||||||
$battery = $em->getRepository(SAPBattery::class)->find($battery_id);
|
|
||||||
if ($battery != null)
|
|
||||||
{
|
|
||||||
// battery is SAPBattery
|
|
||||||
$warranty->setSAPBattery($battery);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: compute expiry date
|
|
||||||
|
|
||||||
// set and save values
|
|
||||||
$warranty->setSerial($serial)
|
|
||||||
->setPlateNumber($plate_number)
|
|
||||||
->setFirstName($first_name)
|
|
||||||
->setLastName($last_name)
|
|
||||||
->setMobileNumber($mobile_number)
|
|
||||||
->setDatePurchase($date_purchase);
|
|
||||||
|
|
||||||
$em->persist($warranty);
|
|
||||||
$em->flush();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$row_num++;
|
$row_num++;
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,7 @@ class BatteryModel
|
||||||
|
|
||||||
public function getBatteries()
|
public function getBatteries()
|
||||||
{
|
{
|
||||||
|
// TODO: fix this to be a proper getter function
|
||||||
// has to return set of strings because symfony is trying to move away from role objects
|
// has to return set of strings because symfony is trying to move away from role objects
|
||||||
$str_batteries = [];
|
$str_batteries = [];
|
||||||
foreach ($this->batteries as $battery)
|
foreach ($this->batteries as $battery)
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,7 @@ class BatterySize
|
||||||
|
|
||||||
public function getBatteries()
|
public function getBatteries()
|
||||||
{
|
{
|
||||||
|
// TODO: fix this to be a proper getter function
|
||||||
// has to return set of strings because symfony is trying to move away from role objects
|
// has to return set of strings because symfony is trying to move away from role objects
|
||||||
$str_batteries = [];
|
$str_batteries = [];
|
||||||
foreach ($this->batteries as $battery)
|
foreach ($this->batteries as $battery)
|
||||||
|
|
|
||||||
293
src/Service/WarrantyHandler.php
Normal file
293
src/Service/WarrantyHandler.php
Normal file
|
|
@ -0,0 +1,293 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
use App\Entity\Warranty;
|
||||||
|
use App\Entity\Battery;
|
||||||
|
use App\Entity\BatterySize;
|
||||||
|
use App\Entity\SAPBattery;
|
||||||
|
use App\Entity\BatteryModel;
|
||||||
|
|
||||||
|
use App\Ramcar\WarrantyClass;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
use DateInterval;
|
||||||
|
|
||||||
|
class WarrantyHandler
|
||||||
|
{
|
||||||
|
protected $em;
|
||||||
|
|
||||||
|
public function __construct(EntityManagerInterface $em)
|
||||||
|
{
|
||||||
|
$this->em = $em;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number,
|
||||||
|
$batt_list, DateTime $date_purchase, $warranty_class)
|
||||||
|
{
|
||||||
|
// new warranty
|
||||||
|
$warranty = new Warranty();
|
||||||
|
|
||||||
|
foreach ($batt_list as $battery)
|
||||||
|
{
|
||||||
|
// get the battery model and battery size
|
||||||
|
$model_id = $battery->getModel()->getID();
|
||||||
|
$size_id = $battery->getSize()->getID();
|
||||||
|
|
||||||
|
$bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id);
|
||||||
|
$bty_size = $this->em->getRepository(BatterySize::class)->find($size_id);
|
||||||
|
|
||||||
|
if ($bty_model != null)
|
||||||
|
{
|
||||||
|
$warranty->setBatteryModel($bty_model);
|
||||||
|
}
|
||||||
|
if ($bty_size != null)
|
||||||
|
{
|
||||||
|
$warranty->setBatterySize($bty_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sap_code = $battery->getSAPCode();
|
||||||
|
if (!empty($sap_code))
|
||||||
|
{
|
||||||
|
// find sap battery
|
||||||
|
$sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code);
|
||||||
|
if ($sap_battery != null)
|
||||||
|
{
|
||||||
|
$warranty->setSAPBattery($sap_battery);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// compute expiry date
|
||||||
|
if ((!empty($warranty_class)) &&
|
||||||
|
(count($batt_list) != 0))
|
||||||
|
{
|
||||||
|
$period = $this->getWarrantyPeriod($batt_list, $warranty_class);
|
||||||
|
$date_expire = $this->computeDateExpire($date_purchase, $period);
|
||||||
|
|
||||||
|
$warranty->setDateExpire($date_expire);
|
||||||
|
}
|
||||||
|
|
||||||
|
// set and save values
|
||||||
|
$warranty->setSerial($serial)
|
||||||
|
->setPlateNumber($plate_number)
|
||||||
|
->setFirstName($first_name)
|
||||||
|
->setLastName($last_name)
|
||||||
|
->setMobileNumber($mobile_number)
|
||||||
|
->setDatePurchase($date_purchase);
|
||||||
|
|
||||||
|
$this->em->persist($warranty);
|
||||||
|
$this->em->flush();
|
||||||
|
$this->em->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateWarranty(Warranty $warr, $first_name, $last_name, $mobile_number, $batt_list, DateTime $date_purchase)
|
||||||
|
{
|
||||||
|
// TODO: add serial and plate number to update
|
||||||
|
// TODO: check if data from existing warranty matches the new data
|
||||||
|
// check if details are complete
|
||||||
|
if (empty($warr->getFirstName()))
|
||||||
|
{
|
||||||
|
if (!empty($first_name))
|
||||||
|
{
|
||||||
|
$warr->setFirstName($first_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (empty($warr->getLastName()))
|
||||||
|
{
|
||||||
|
if (!empty($last_name))
|
||||||
|
{
|
||||||
|
$warr->setLastName($last_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (empty($warr->getMobileNumber()))
|
||||||
|
{
|
||||||
|
if (!empty($mobile_number))
|
||||||
|
{
|
||||||
|
$warr->setMobileNumber($mobile_number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((empty($warr->getBatteryModel())) ||
|
||||||
|
(empty($warr->getBatterySize())))
|
||||||
|
{
|
||||||
|
if (count($batt_list) != 0)
|
||||||
|
{
|
||||||
|
foreach ($batt_list as $battery)
|
||||||
|
{
|
||||||
|
// get the battery model and battery size
|
||||||
|
$model_id = $battery->getModel()->getID();
|
||||||
|
$size_id = $battery->getSize()->getID();
|
||||||
|
|
||||||
|
$bty_model = $this->em->getRepository(BatteryModel::class)->find($model_id);
|
||||||
|
$bty_size = $this->em->getRepository(BatterySize::class)->find($size_id);
|
||||||
|
|
||||||
|
if ($bty_model != null)
|
||||||
|
{
|
||||||
|
$warranty->setBatteryModel($bty_model);
|
||||||
|
}
|
||||||
|
if ($bty_size != null)
|
||||||
|
{
|
||||||
|
$warranty->setBatterySize($bty_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sap_code = $battery->getSAPCode();
|
||||||
|
if (!empty($sap_code))
|
||||||
|
{
|
||||||
|
// find sap battery
|
||||||
|
$sap_battery = $this->em->getRepository(SAPBattery::class)->find($sap_code);
|
||||||
|
if ($sap_battery != null)
|
||||||
|
{
|
||||||
|
$warranty->setSAPBattery($sap_battery);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$purchase_date = $warr->getDatePurchase();
|
||||||
|
if (empty($purchase_date))
|
||||||
|
{
|
||||||
|
if (!empty($date_purchase))
|
||||||
|
{
|
||||||
|
$warr->setDatePurchase($date_purchase);
|
||||||
|
}
|
||||||
|
$purchase_date = $date_purchase;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($warr->getDateExpire()))
|
||||||
|
{
|
||||||
|
$batteries = [];
|
||||||
|
if (count($batt_list) == 0)
|
||||||
|
{
|
||||||
|
$batteries = $this->getBatteriesForWarrantyPeriod($warr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$batteries = $batt_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($batteries))
|
||||||
|
{
|
||||||
|
$period = $this->getWarrantyPeriod($batteries, $warr->getWarrantyClass());
|
||||||
|
if (!empty($purchase_date))
|
||||||
|
{
|
||||||
|
$expire_date = $this->computeDateExpire($purchase_date, $period);
|
||||||
|
$warr->setDateExpire($expire_date);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->em->persist($warr);
|
||||||
|
$this->em->flush();
|
||||||
|
$this->em->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function computeDateExpire($date_create, $warranty_period)
|
||||||
|
{
|
||||||
|
$expire_date = clone $date_create;
|
||||||
|
$expire_date->add(new DateInterval('P'.$warranty_period.'M'));
|
||||||
|
return $expire_date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getWarrantyPeriod($batteries, $warranty_class)
|
||||||
|
{
|
||||||
|
// set to -1 to show that we haven't set a warranty period yet
|
||||||
|
// cannot set initial value to 0 because warranty tnv can be 0
|
||||||
|
$least_warranty = -1;
|
||||||
|
$warr_period = 0;
|
||||||
|
foreach ($batteries as $battery)
|
||||||
|
{
|
||||||
|
// if multiple batteries, get the smallest warranty period
|
||||||
|
// check warranty class to get warranty period
|
||||||
|
if ($warranty_class == WarrantyClass::WTY_PRIVATE)
|
||||||
|
{
|
||||||
|
$warr_period = $battery->getWarrantyPrivate();
|
||||||
|
//error_log('Warranty Period for Private: ' . $warr_period);
|
||||||
|
}
|
||||||
|
if ($warranty_class == WarrantyClass::WTY_COMMERCIAL)
|
||||||
|
{
|
||||||
|
$warr_period = $battery->getWarrantyCommercial();
|
||||||
|
//error_log('Warranty Period for Commercial: ' . $warr_period);
|
||||||
|
}
|
||||||
|
if ($warranty_class == WarrantyClass::WTY_TNV)
|
||||||
|
{
|
||||||
|
$warr_period = $battery->getWarrantyTnv();
|
||||||
|
//error_log('Warranty Period for TNV: ' . $warr_period);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($least_warranty < 0)
|
||||||
|
{
|
||||||
|
// set least warranty to the first obtained warranty period
|
||||||
|
$least_warranty = $warr_period;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($least_warranty > $warr_period)
|
||||||
|
{
|
||||||
|
$least_warranty = $warr_period;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$warranty_period = $least_warranty;
|
||||||
|
|
||||||
|
return $warranty_period;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBatteriesForWarrantyPeriod($warr)
|
||||||
|
{
|
||||||
|
// find battery via sku/sap battery first
|
||||||
|
// if sku is null, use battery model and battery size to find battery
|
||||||
|
// if all three are null, do nothing
|
||||||
|
$batteries = null;
|
||||||
|
|
||||||
|
$sap_battery = $warr->getSAPBattery();
|
||||||
|
$batt_model = $warr->getBatteryModel();
|
||||||
|
$batt_size = $warr->getBatterySize();
|
||||||
|
$warranty_class = $warr->getWarrantyClass();
|
||||||
|
|
||||||
|
if (empty($warranty_class))
|
||||||
|
{
|
||||||
|
error_log('Warranty class is empty for warranty id ' . $warr->getID());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($sap_battery != null)
|
||||||
|
{
|
||||||
|
// get the battery linked to SAP Battery using sap_battery id
|
||||||
|
$batteries = $this->em->getRepository(Battery::class)->findBy(['sap_code' => $sap_battery->getID()]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ($batt_model == null)
|
||||||
|
{
|
||||||
|
error_log('Battery model is null for warranty id ' . $warr->getID());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ($batt_size == null)
|
||||||
|
{
|
||||||
|
error_log('Battery size is null for warranty id ' . $warr->getID());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// find battery using battery model and battery size
|
||||||
|
$batteries = $this->em->getRepository(Battery::class)->findBy(['model' => $batt_model, 'size' => $batt_size]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($batteries))
|
||||||
|
{
|
||||||
|
error_log('Battery not found for warranty id ' . $warr->getID());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $batteries;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function cleanPlateNumber($plate)
|
||||||
|
{
|
||||||
|
// remove spaces and make upper case
|
||||||
|
return strtoupper(str_replace(' ', '', $plate));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue