Merge branch '525-battery-serial-history' into 'master'

Resolve "Battery Serial History"

Closes #525

See merge request jankstudio/resq!609
This commit is contained in:
Kendrick Chan 2020-10-26 09:29:17 +00:00
commit a032c4feb0
3 changed files with 198 additions and 0 deletions

View file

@ -249,3 +249,19 @@ services:
$base_url: "%env(MOTIV_BASE_URL)%"
$sub_key: "%env(MOTIV_KEY)%"
$token: "%env(MOTIV_TOKEN)%"
# entity listener for customer vehicle warranty code history
App\EntityListener\CustomerVehicleSerialListener:
arguments:
$ts: "@security.token_storage"
tags:
- name: doctrine.orm.entity_listener
event: 'preUpdate'
entity: 'App\Entity\CustomerVehicle'
- name: doctrine.orm.entity_listener
event: 'postUpdate'
entity: 'App\Entity\CustomerVehicle'
- name: doctrine.orm.entity_listener
event: 'postPersist'
entity: 'App\Entity\CustomerVehicle'

View file

@ -0,0 +1,94 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use DateTime;
/**
* @ORM\Entity
* @ORM\Table(name="cv_warranty_history")
*/
class CVWarrantyHistory
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// customer vehicle id, loose coupling
/**
* @ORM\Column(type="integer")
*/
protected $cv_id;
/**
* @ORM\Column(type="datetime")
*/
protected $date_create;
// warranty code
/**
* @ORM\Column(type="string", length=20)
*/
protected $warranty_code;
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
public function __construct()
{
$this->date_create = new DateTime();
}
public function getID()
{
return $this->id;
}
public function getDateCreate()
{
return $this->date_create;
}
public function setWarrantyCode($warranty_code)
{
$this->warranty_code = $warranty_code;
return $this;
}
public function getWarrantyCode()
{
return $this->warranty_code;
}
public function setCVID($cv_id)
{
$this->cv_id = $cv_id;
return $this;
}
public function getCVID()
{
return $this->cv_id;
}
public function setUser($user)
{
$this->user = $user;
return $this;
}
public function getUser()
{
return $this->user;
}
}

View file

@ -0,0 +1,88 @@
<?php
namespace App\EntityListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Entity\CVWarrantyHistory;
use App\Entity\CustomerVehicle;
class CustomerVehicleSerialListener
{
protected $ts;
public function __construct(TokenStorageInterface $ts)
{
$this->ts = $ts;
$this->history = null;
}
public function preUpdate(CustomerVehicle $cv, PreUpdateEventArgs $args)
{
// error_log('TRIGGER CV LISTENER - pre update');
// only check if warranty code field has changed
if ($args->hasChangedField('warranty_code'))
{
// error_log('warranty code has changed!');
$em = $args->getEntityManager();
// get user
$user = $this->ts->getToken()->getUser();
// error_log('user - ' . $user->getID());
// add history
$history = new CVWarrantyHistory();
$history->setCVID($cv->getID())
->setWarrantyCode($cv->getWarrantyCode())
->setUser($user);
$em->persist($history);
$this->history = $history;
}
}
public function postUpdate(CustomerVehicle $cv, LifecycleEventArgs $args)
{
if ($this->history != null)
{
$em = $args->getEntityManager();
$em->flush();
}
}
public function postPersist(CustomerVehicle $cv, LifecycleEventArgs $args)
{
// error_log('TRIGGER CV LISTENER - post persist');
$em = $args->getEntityManager();
// get user
$user = $this->ts->getToken()->getUser();
if ($cv->getWarrantyCode() == null)
$warr_code = '';
else
$warr_code = $cv->getWarrantyCode();
// add history
$history = new CVWarrantyHistory();
$history->setCVID($cv->getID())
->setWarrantyCode($warr_code)
->setUser($user);
$em->persist($history);
$em->flush();
}
}