181 lines
6 KiB
PHP
181 lines
6 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\Ramcar\NameValue;
|
|
use App\Ramcar\WarrantyClass;
|
|
use App\Ramcar\WarrantyStatus;
|
|
use DateTime;
|
|
|
|
class WarrantyController extends APIController
|
|
{
|
|
protected function cleanSerial($serial)
|
|
{
|
|
return trim(strtoupper($serial));
|
|
}
|
|
|
|
protected function generateWarrantyData(Warranty $warr)
|
|
{
|
|
$model = $warr->getBatteryModel();
|
|
$size = $warr->getBatterySize();
|
|
$data = [
|
|
'id' => (int) $warr->getID(),
|
|
'serial' => (string) $warr->getSerial(),
|
|
'warranty_class' => (string) $warr->getWarrantyClass(),
|
|
'plate_number' => (string) $warr->getPlateNumber(),
|
|
'battery_model' => [
|
|
(int) ($model == null ? 0 : $model->getID()),
|
|
(string) ($model == null ? '' : $model->getName()),
|
|
],
|
|
'battery_size' => [
|
|
(int) ($size == null ? 0 : $size->getID()),
|
|
(string) ($size == null ? '' : $size->getName()),
|
|
],
|
|
'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)
|
|
{
|
|
$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 register(Request $req, EntityManagerInterface $em)
|
|
{
|
|
// required parameters
|
|
$params = [
|
|
'serial',
|
|
'warranty_class',
|
|
'plate_number',
|
|
'date_expire',
|
|
'date_purchase',
|
|
'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');
|
|
$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 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)
|
|
->setBatteryModel($model)
|
|
->setBatterySize($size)
|
|
->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, $serial)
|
|
{
|
|
$clean_serial = $this->cleanSerial($serial);
|
|
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]);
|
|
|
|
// no warranty
|
|
if ($warr == null)
|
|
return new APIResponse(false, 'No warranty found with that serial number.', null, 404);
|
|
|
|
// warranty is not active
|
|
if (!$warr->canClaim())
|
|
return new APIResponse(false, 'Warranty is not active.');
|
|
|
|
// set status to claim
|
|
$warr->setStatus(WarrantyStatus::CLAIMED)
|
|
->setDateClaim(new DateTime());
|
|
|
|
$em->flush();
|
|
|
|
// TODO: claim log
|
|
|
|
return new APIResponse(true, 'Warranty claimed successfully.');
|
|
}
|
|
}
|