293 lines
6.1 KiB
PHP
293 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Entity;
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use App\Ramcar\WarrantyClass;
|
|
use App\Ramcar\WarrantyStatus;
|
|
use DateTime;
|
|
use Exception;
|
|
|
|
/**
|
|
* @ORM\Entity
|
|
* @ORM\Table(
|
|
* name="warranty",
|
|
* uniqueConstraints={
|
|
* @ORM\UniqueConstraint(columns={"serial"})
|
|
* }
|
|
* )
|
|
*/
|
|
class Warranty
|
|
{
|
|
// unique id
|
|
/**
|
|
* @ORM\Id
|
|
* @ORM\Column(type="integer")
|
|
* @ORM\GeneratedValue(strategy="AUTO")
|
|
*/
|
|
protected $id;
|
|
|
|
// serial number
|
|
/**
|
|
* @ORM\Column(type="string", length=50, nullable=true)
|
|
*/
|
|
protected $serial;
|
|
|
|
// warranty class
|
|
/**
|
|
* @ORM\Column(type="string", length=25)
|
|
*/
|
|
protected $warranty_class;
|
|
|
|
// plate
|
|
/**
|
|
* @ORM\Column(type="string", length=20)
|
|
*/
|
|
protected $plate_number;
|
|
|
|
// battery model
|
|
/**
|
|
* @ORM\ManyToOne(targetEntity="BatteryModel", inversedBy="warranties")
|
|
* @ORM\JoinColumn(name="bty_model_id", referencedColumnName="id")
|
|
*/
|
|
protected $bty_model;
|
|
|
|
// battery size
|
|
/**
|
|
* @ORM\ManyToOne(targetEntity="BatterySize", inversedBy="warranties")
|
|
* @ORM\JoinColumn(name="bty_size_id", referencedColumnName="id")
|
|
*/
|
|
protected $bty_size;
|
|
|
|
// sap battery
|
|
/**
|
|
* @ORM\ManyToOne(targetEntity="SAPBattery", inversedBy="warranties")
|
|
* @ORM\JoinColumn(name="sap_bty_id", referencedColumnName="id", nullable=true)
|
|
*/
|
|
protected $sap_bty;
|
|
|
|
// status
|
|
/**
|
|
* @ORM\Column(type="string", length=25)
|
|
*/
|
|
protected $status;
|
|
|
|
// date created
|
|
/**
|
|
* @ORM\Column(type="datetime")
|
|
*/
|
|
protected $date_create;
|
|
|
|
// date purchased
|
|
/**
|
|
* @ORM\Column(type="date")
|
|
*/
|
|
protected $date_purchase;
|
|
|
|
// date expires
|
|
/**
|
|
* @ORM\Column(type="date", nullable=true)
|
|
*/
|
|
protected $date_expire;
|
|
|
|
// date claimed
|
|
/**
|
|
* @ORM\Column(type="date", nullable=true)
|
|
*/
|
|
protected $date_claim;
|
|
|
|
// claimed from
|
|
/**
|
|
* @ORM\OneToOne(targetEntity="Warranty")
|
|
* @ORM\JoinColumn(name="claim_id", referencedColumnName="id", nullable=true)
|
|
*/
|
|
protected $claim_from;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->date_create = new DateTime();
|
|
$this->warranty_class = WarrantyClass::WTY_PRIVATE;
|
|
$this->status = WarrantyStatus::ACTIVE;
|
|
$this->date_claim = null;
|
|
}
|
|
|
|
public function getID()
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function setSerial($serial)
|
|
{
|
|
$this->serial = $serial;
|
|
return $this;
|
|
}
|
|
|
|
public function getSerial()
|
|
{
|
|
return $this->serial;
|
|
}
|
|
|
|
public function setWarrantyClass($class)
|
|
{
|
|
$this->warranty_class = $class;
|
|
return $this;
|
|
}
|
|
|
|
public function getWarrantyClass()
|
|
{
|
|
return $this->warranty_class;
|
|
}
|
|
|
|
// TODO: use a service to handle plate number filtering
|
|
public static function cleanPlateNumber($plate)
|
|
{
|
|
// trim and make upper case
|
|
$clean_plate = strtoupper(trim($plate));
|
|
|
|
// remove invalid characters
|
|
$clean_plate = preg_replace("/[^A-Z0-9]/", '', $clean_plate);
|
|
|
|
// check for 4 to 5 digit diplomatic plate
|
|
$res = preg_match("/^[0-9]{4,5}$/", $clean_plate);
|
|
if ($res)
|
|
return $clean_plate;
|
|
|
|
// ABC-1234 or ABC-123 or ABC-12 format
|
|
$res = preg_match("/^[A-Z]{3}[0-9]{2,4}$/", $clean_plate);
|
|
if ($res)
|
|
return $clean_plate;
|
|
|
|
// AB-123 or AB-12345 or AB-1234 format (motorcycles)
|
|
$res = preg_match("/^[A-Z]{2}[0-9]{3,5}$/", $clean_plate);
|
|
if ($res)
|
|
return $clean_plate;
|
|
|
|
// 1234-AB format (motorcycles)
|
|
$res = preg_match("/^[0-9]{4}[A-Z]{2}$/", $clean_plate);
|
|
if ($res)
|
|
return $clean_plate;
|
|
|
|
return false;
|
|
}
|
|
|
|
public function setPlateNumber($plate)
|
|
{
|
|
// TODO: custom exception
|
|
$plate_number = $this->cleanPlateNumber($plate);
|
|
if (!$plate_number)
|
|
throw new Exception('Invalid plate number.');
|
|
|
|
$this->plate_number = $plate_number;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getPlateNumber()
|
|
{
|
|
return $this->plate_number;
|
|
}
|
|
|
|
public function setBatteryModel(BatteryModel $model)
|
|
{
|
|
$this->bty_model = $model;
|
|
return $this;
|
|
}
|
|
|
|
public function getBatteryModel()
|
|
{
|
|
return $this->bty_model;
|
|
}
|
|
|
|
public function setBatterySize(BatterySize $size)
|
|
{
|
|
$this->bty_size = $size;
|
|
return $this;
|
|
}
|
|
|
|
public function getBatterySize()
|
|
{
|
|
return $this->bty_size;
|
|
}
|
|
|
|
public function setSAPBattery(SAPBattery $sap_bty)
|
|
{
|
|
$this->sap_bty = $sap_bty;
|
|
return $this;
|
|
}
|
|
|
|
public function getSAPBattery()
|
|
{
|
|
return $this->sap_bty;
|
|
}
|
|
|
|
public function setStatus($status)
|
|
{
|
|
$this->status = $status;
|
|
return $this;
|
|
}
|
|
|
|
public function getStatus()
|
|
{
|
|
return $this->status;
|
|
}
|
|
|
|
public function getDateCreate()
|
|
{
|
|
return $this->date_create;
|
|
}
|
|
|
|
public function setDatePurchase(DateTime $date)
|
|
{
|
|
$this->date_purchase = $date;
|
|
return $this;
|
|
}
|
|
|
|
public function getDatePurchase()
|
|
{
|
|
return $this->date_purchase;
|
|
}
|
|
|
|
public function setDateExpire(DateTime $date)
|
|
{
|
|
$this->date_expire = $date;
|
|
return $this;
|
|
}
|
|
|
|
public function getDateExpire()
|
|
{
|
|
return $this->date_expire;
|
|
}
|
|
|
|
public function setDateClaim(DateTime $date = null)
|
|
{
|
|
$this->date_claim = $date;
|
|
return $this;
|
|
}
|
|
|
|
public function getDateClaim()
|
|
{
|
|
return $this->date_claim;
|
|
}
|
|
|
|
public function canClaim()
|
|
{
|
|
if ($this->status == WarrantyStatus::ACTIVE)
|
|
return true;
|
|
|
|
if ($this->status == WarrantyStatus::CLAIMED)
|
|
return false;
|
|
|
|
return false;
|
|
}
|
|
|
|
public function setClaimedFrom($claim_from)
|
|
{
|
|
$this->claim_form = $claim_from;
|
|
return $this;
|
|
}
|
|
|
|
public function getClaimedFrom()
|
|
{
|
|
return $this->claim_from;
|
|
}
|
|
}
|