312 lines
9.9 KiB
PHP
312 lines
9.9 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\CAPI;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Doctrine\ORM\Query;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
|
use Catalyst\APIBundle\Controller\APIController;
|
|
use Catalyst\APIBundle\Response\APIResponse;
|
|
|
|
use App\Entity\Warranty;
|
|
use App\Entity\BatteryModel;
|
|
use App\Entity\BatterySize;
|
|
use App\Entity\SAPBattery;
|
|
use App\Entity\SAPBatterySize;
|
|
use App\Entity\SAPBatteryBrand;
|
|
|
|
use App\Ramcar\NameValue;
|
|
use App\Ramcar\WarrantyClass;
|
|
use App\Ramcar\WarrantyStatus;
|
|
use DateTime;
|
|
|
|
use Catalyst\AuthBundle\Access\Generator as ACLGenerator;
|
|
|
|
class WarrantyController extends APIController
|
|
{
|
|
protected $acl_gen;
|
|
|
|
public function __construct(ACLGenerator $acl_gen)
|
|
{
|
|
$this->acl_gen = $acl_gen;
|
|
}
|
|
|
|
protected function cleanSerial($serial)
|
|
{
|
|
return trim(strtoupper($serial));
|
|
}
|
|
|
|
protected function generateWarrantyData(Warranty $warr)
|
|
{
|
|
$batt = $warr->getSAPBattery();
|
|
|
|
$data = [
|
|
'id' => (int) $warr->getID(),
|
|
'serial' => (string) $warr->getSerial(),
|
|
'warranty_class' => (string) $warr->getWarrantyClass(),
|
|
'plate_number' => (string) $warr->getPlateNumber(),
|
|
'battery' => [
|
|
'sku' => (string) ($batt == null ? '' : $batt->getID()),
|
|
'brand' => (int) ($batt == null ? 0 : $batt->getBrand()->getID()),
|
|
'size' => (int) ($batt == null ? 0 : $batt->getSize()->getID()),
|
|
],
|
|
'customer' => [
|
|
'first_name' => (string) $warr->getFirstName() ?? '',
|
|
'last_name' => (string) $warr->getLastName() ?? '',
|
|
'mobile_number' => (string) $warr->getMobileNumber() ?? '',
|
|
],
|
|
'status' => (string) $warr->getStatus(),
|
|
'date_create' => (string) $warr->getDateCreate()->format('YmdHis'),
|
|
'date_purchase' => (string) $warr->getDatePurchase()->format('Ymd'),
|
|
'date_expire' => (string) $warr->getDateExpire()->format('Ymd'),
|
|
];
|
|
|
|
$date_claim = $warr->getDateClaim();
|
|
if ($date_claim == null)
|
|
$data['date_claim'] = null;
|
|
else
|
|
$data['date_claim'] = (string) $warr->getDateClaim()->format('Ymd');
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function find($serial, EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('warranty.find.serial', null, 'No access.');
|
|
|
|
$clean_serial = $this->cleanSerial($serial);
|
|
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]);
|
|
|
|
if ($warr == null)
|
|
return new APIResponse(false, 'No warranty found with that serial number.', null, 404);
|
|
|
|
|
|
$data = [
|
|
'warranty' => $this->generateWarrantyData($warr),
|
|
];
|
|
|
|
return new APIResponse(true, 'Warranty found.', $data);
|
|
}
|
|
|
|
public function getAll(Request $req, EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('warranty.list', null, 'No access.');
|
|
|
|
$order = $req->query->get('order');
|
|
if ($order == null)
|
|
$order = 'ASC';
|
|
|
|
$max = $req->query->get('limit');
|
|
if ($max == null)
|
|
$max = 20;
|
|
|
|
$start = $req->query->get('start');
|
|
if ($start == null)
|
|
$start = 0;
|
|
|
|
$qb = $em->createQueryBuilder();
|
|
|
|
$query = $qb->select('w')
|
|
->from('App\\Entity\\Warranty', 'w')
|
|
->orderBy('w.serial', $order)
|
|
->setFirstResult($start)
|
|
->setMaxResults($max)
|
|
->getQuery();
|
|
|
|
$warrs = $query->getResult();
|
|
|
|
$warr_data = [];
|
|
foreach ($warrs as $warr)
|
|
$warr_data[] = $this->generateWarrantyData($warr);
|
|
|
|
$data = [
|
|
'warranties' => $warr_data,
|
|
];
|
|
|
|
return new APIResponse(true, 'Warranties found.', $data);
|
|
}
|
|
|
|
public function register(Request $req, EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('warranty.register.battery', null, 'No access.');
|
|
|
|
// required parameters
|
|
$params = [
|
|
'serial',
|
|
'warranty_class',
|
|
'plate_number',
|
|
'date_expire',
|
|
'date_purchase',
|
|
'sku',
|
|
/*
|
|
'battery_model_id',
|
|
'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');
|
|
$date_pur_string = $req->request->get('date_purchase');
|
|
$warr_class = $req->request->get('warranty_class');
|
|
$plate = $req->request->get('plate_number');
|
|
$sku = $req->request->get('sku');
|
|
|
|
$fname = $req->request->get('first_name', null);
|
|
$lname = $req->request->get('last_name', null);
|
|
$mnum = $req->request->get('mobile_number', null);
|
|
|
|
/*
|
|
$bmodel_id = $req->request->get('battery_model_id');
|
|
$bsize_id = $req->request->get('battery_size_id');
|
|
*/
|
|
|
|
// wrong date expire format
|
|
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
|
|
if ($date_expire === false)
|
|
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)
|
|
return new APIResponse(false, 'Wrong date format: date_purchase.');
|
|
|
|
// valid warranty class
|
|
if (!WarrantyClass::validate($warr_class))
|
|
return new APIResponse(false, 'Invalid warranty class.');
|
|
|
|
// plate number
|
|
$plate = Warranty::cleanPlateNumber($plate);
|
|
if (!$plate)
|
|
return new APIResponse(false, 'Invalid plate number.');
|
|
|
|
// battery
|
|
$batt = $em->getRepository(SAPBattery::class)->find($sku);
|
|
if ($batt == null)
|
|
return new APIResponse(false, 'Invalid battery SKU.');
|
|
|
|
/*
|
|
// battery model
|
|
$model = $em->getRepository(BatteryModel::class)->find($bmodel_id);
|
|
if ($model == null)
|
|
return new APIResponse(false, 'Invalid battery model id.');
|
|
|
|
// battery size
|
|
$size = $em->getRepository(BatterySize::class)->find($bsize_id);
|
|
if ($size == null)
|
|
return new APIResponse(false, 'Invalid battery size id.');
|
|
*/
|
|
|
|
// warranty
|
|
$warr = new Warranty();
|
|
$warr->setSerial($serial)
|
|
->setWarrantyClass($warr_class)
|
|
->setPlateNumber($plate)
|
|
->setFirstName($fname)
|
|
->setLastName($lname)
|
|
->setMobileNumber($mnum)
|
|
->setSAPBattery($batt)
|
|
->setDatePurchase($date_pur)
|
|
->setDateClaim(null)
|
|
->setDateExpire($date_expire);
|
|
|
|
try
|
|
{
|
|
$em->persist($warr);
|
|
$em->flush();
|
|
}
|
|
catch (UniqueConstraintViolationException $e)
|
|
{
|
|
return new APIResponse(false, 'Duplicate serial encountered.');
|
|
}
|
|
|
|
// data
|
|
$data = [
|
|
'warranty' => $this->generateWarrantyData($warr),
|
|
];
|
|
|
|
return new APIResponse(true, 'Warranty registered.', $data);
|
|
}
|
|
|
|
public function claim(Request $req, EntityManagerInterface $em, $id)
|
|
{
|
|
$this->denyAccessUnlessGranted('warranty.claim', null, 'No access.');
|
|
|
|
// required parameters
|
|
$params = [
|
|
'serial',
|
|
];
|
|
$msg = $this->checkRequiredParameters($req, $params);
|
|
if ($msg)
|
|
return new APIResponse(false, $msg);
|
|
|
|
// no warranty
|
|
$warr = $em->getRepository(Warranty::class)->find($id);
|
|
if ($warr == null)
|
|
return new APIResponse(false, 'No warranty found with that id.', null, 404);
|
|
|
|
// warranty is not active
|
|
if (!$warr->canClaim())
|
|
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)
|
|
return new APIResponse(false, 'Serial for replacement has already been used.');
|
|
|
|
// set status to claim
|
|
$warr->setStatus(WarrantyStatus::CLAIMED)
|
|
->setDateClaim(new DateTime());
|
|
|
|
// make replacement warranty
|
|
$new_warr = new Warranty();
|
|
$new_warr->setSerial($clean_serial)
|
|
->setWarrantyClass($warr->getWarrantyClass())
|
|
->setPlateNumber($warr->getPlateNumber())
|
|
->setFirstName($warr->getFirstName())
|
|
->setLastName($warr->getLastName())
|
|
->setMobileNumber($warr->getMobileNumber())
|
|
->setSAPBattery($warr->getSAPBattery())
|
|
->setDatePurchase($warr->getDatePurchase())
|
|
->setDateClaim(null)
|
|
->setDateExpire($warr->getDateExpire())
|
|
->setClaimedFrom($warr);
|
|
|
|
$em->persist($new_warr);
|
|
|
|
|
|
$em->flush();
|
|
|
|
// TODO: claim log
|
|
|
|
return new APIResponse(true, 'Warranty claimed successfully.');
|
|
}
|
|
|
|
public function getPlateWarranties($plate_number, EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('warranty.find.platenumber', null, 'No access.');
|
|
|
|
$warranties = $em->getRepository(Warranty::class)
|
|
->findBy(['plate_number' => $plate_number], ['date_purchase' => 'DESC']);
|
|
|
|
$warr_data = [];
|
|
foreach ($warranties as $warr)
|
|
{
|
|
$warr_data[] = $this->generateWarrantyData($warr);
|
|
}
|
|
|
|
$data = [
|
|
'warranties' => $warr_data,
|
|
];
|
|
|
|
return new APIResponse(true, 'Warranties loaded.', $data);
|
|
}
|
|
}
|