diff --git a/config/services.yaml b/config/services.yaml index 43fb74df..c2388e79 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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' + diff --git a/src/Entity/CVWarrantyHistory.php b/src/Entity/CVWarrantyHistory.php new file mode 100644 index 00000000..88ccc35a --- /dev/null +++ b/src/Entity/CVWarrantyHistory.php @@ -0,0 +1,94 @@ +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; + } +} diff --git a/src/EntityListener/CustomerVehicleSerialListener.php b/src/EntityListener/CustomerVehicleSerialListener.php new file mode 100644 index 00000000..b19316e4 --- /dev/null +++ b/src/EntityListener/CustomerVehicleSerialListener.php @@ -0,0 +1,88 @@ +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(); + } +} +