diff --git a/config/cmb.services.yaml b/config/cmb.services.yaml index d12b2acf..3ec1e341 100644 --- a/config/cmb.services.yaml +++ b/config/cmb.services.yaml @@ -227,3 +227,14 @@ services: $redis_prov: "@App\\Service\\RedisClientProvider" $loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%" $status_key: "%env(STATUS_RIDER_KEY)%" + + App\EventListener\JobOrderStatusListener: + arguments: + $wh: "@App\\Service\\WarrantyHandler" + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postUpdate' + entity: 'App\Entity\JobOrder' + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\JobOrder' diff --git a/config/resq.services.yaml b/config/resq.services.yaml index 1756cf98..a8143011 100644 --- a/config/resq.services.yaml +++ b/config/resq.services.yaml @@ -226,3 +226,15 @@ services: $redis_prov: "@App\\Service\\RedisClientProvider" $loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%" $status_key: "%env(STATUS_RIDER_KEY)%" + + App\EventListener\JobOrderStatusListener: + arguments: + $wh: "@App\\Service\\WarrantyHandler" + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postUpdate' + entity: 'App\Entity\JobOrder' + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\JobOrder' + diff --git a/config/services.yaml b/config/services.yaml index 8fe325e4..01465e88 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -247,3 +247,14 @@ services: $redis_prov: "@App\\Service\\RedisClientProvider" $loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%" $status_key: "%env(STATUS_RIDER_KEY)%" + + App\EventListener\JobOrderStatusListener: + arguments: + $wh: "@App\\Service\\WarrantyHandler" + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postUpdate' + entity: 'App\Entity\JobOrder' + - name: 'doctrine.orm.entity_listener' + event: 'postPersist' + entity: 'App\Entity\JobOrder' diff --git a/src/EventListener/JobOrderStatusListener.php b/src/EventListener/JobOrderStatusListener.php new file mode 100644 index 00000000..8c3c9f45 --- /dev/null +++ b/src/EventListener/JobOrderStatusListener.php @@ -0,0 +1,91 @@ +wh = $wh; + $this->jo_handler = $jo_handler; + } + + // when a JO is updated + public function postUpdate(JobOrder $jo, LifecycleEventArgs $args) + { + $status = $jo->getStatus(); + + if ($status == JOStatus::FULFILLED) + { + $this->createWarrantyFromJO($jo); + } + } + + // when a new job order comes in and status is already fulfilled + public function postPersist(JobOrder $jo, LifecycleEventArgs $args) + { + $status = $jo->getStatus(); + + if ($status == JOStatus::FULFILLED) + { + $this->createWarrantyFromJO($jo); + } + } + + protected function createWarrantyFromJO(JobOrder $jo) + { + // create warranty + if ($this->jo_handler->checkIfNewBattery($jo)) + { + $serial = null; + $warranty_class = $jo->getWarrantyClass(); + $first_name = $jo->getCustomer()->getFirstName(); + $last_name = $jo->getCustomer()->getLastName(); + $mobile_number = $jo->getCustomer()->getPhoneMobile(); + + // check if date fulfilled is null + if ($jo->getDateFulfill() == null) + $date_purchase = $jo->getDateCreate(); + else + $date_purchase = $jo->getDateFulfill(); + + // validate plate number + // $plate_number = $this->wh->cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); + $plate_number = Warranty::cleanPlateNumber($jo->getCustomerVehicle()->getPlateNumber()); + if ($plate_number != false) + { + $batt_list = array(); + $invoice = $jo->getInvoice(); + if (!empty($invoice)) + { + // get battery + $invoice_items = $invoice->getItems(); + foreach ($invoice_items as $item) + { + $battery = $item->getBattery(); + if ($battery != null) + { + $batt_list[] = $item->getBattery(); + } + } + } + + $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); + } + } + } + +} diff --git a/src/Service/JobOrderHandler/CMBJobOrderHandler.php b/src/Service/JobOrderHandler/CMBJobOrderHandler.php index 06f1fb72..0229a5cc 100644 --- a/src/Service/JobOrderHandler/CMBJobOrderHandler.php +++ b/src/Service/JobOrderHandler/CMBJobOrderHandler.php @@ -24,7 +24,6 @@ use App\Entity\Hub; use App\Entity\Promo; use App\Entity\Rider; use App\Entity\JORejection; -use App\Entity\Warranty; use App\Entity\Customer; use App\Entity\ServiceCharge; @@ -44,7 +43,6 @@ use App\Service\InvoiceGeneratorInterface; use App\Service\JobOrderHandlerInterface; use App\Service\RiderAssignmentHandlerInterface; use App\Service\CustomerHandlerInterface; -use App\Service\WarrantyHandler; use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\MapTools; @@ -67,7 +65,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface protected $translator; protected $rah; protected $country_code; - protected $wh; protected $cust_handler; protected $template_hash; @@ -75,8 +72,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, - string $country_code, WarrantyHandler $wh, - CustomerHandlerInterface $cust_handler) + string $country_code, CustomerHandlerInterface $cust_handler) { $this->em = $em; $this->ic = $ic; @@ -85,7 +81,6 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $this->translator = $translator; $this->rah = $rah; $this->country_code = $country_code; - $this->wh = $wh; $this->cust_handler = $cust_handler; $this->loadTemplates(); @@ -937,6 +932,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface // call rider assignment handler's fulfillJobOrder $this->rah->fulfillJobOrder($obj, $image_url, $rider); + /* // create the warranty if new battery only if ($this->checkIfNewBattery($obj)) { @@ -979,7 +975,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } + } */ // validated! save the entity $em->flush(); @@ -2605,7 +2601,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $cust_vehicle->setWarrantyCode($req->request->get('warranty_code')); $em->persist($cust_vehicle); - + /* // create the warranty if new battery only if ($this->checkIfNewBattery($jo)) { @@ -2648,7 +2644,7 @@ class CMBJobOrderHandler implements JobOrderHandlerInterface $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } + } */ $em->flush(); } diff --git a/src/Service/JobOrderHandler/ResqJobOrderHandler.php b/src/Service/JobOrderHandler/ResqJobOrderHandler.php index c6a6fa01..50908933 100644 --- a/src/Service/JobOrderHandler/ResqJobOrderHandler.php +++ b/src/Service/JobOrderHandler/ResqJobOrderHandler.php @@ -23,7 +23,6 @@ use App\Entity\Hub; use App\Entity\Promo; use App\Entity\Rider; use App\Entity\JORejection; -use App\Entity\Warranty; use App\Entity\Customer; use App\Ramcar\InvoiceCriteria; @@ -41,7 +40,6 @@ use App\Ramcar\JORejectionReason; use App\Service\InvoiceGeneratorInterface; use App\Service\JobOrderHandlerInterface; use App\Service\RiderAssignmentHandlerInterface; -use App\Service\WarrantyHandler; use App\Service\MQTTClient; use App\Service\APNSClient; use App\Service\MapTools; @@ -64,14 +62,13 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface protected $translator; protected $rah; protected $country_code; - protected $wh; protected $template_hash; public function __construct(Security $security, EntityManagerInterface $em, InvoiceGeneratorInterface $ic, ValidatorInterface $validator, TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah, - string $country_code, WarrantyHandler $wh) + string $country_code) { $this->em = $em; $this->ic = $ic; @@ -80,7 +77,6 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $this->translator = $translator; $this->rah = $rah; $this->country_code = $country_code; - $this->wh = $wh; $this->loadTemplates(); } @@ -715,6 +711,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface // call rider assignment handler's fulfillJobOrder $this->rah->fulfillJobOrder($obj, $image_url, $rider); + /* // create the warranty if new battery only if ($this->checkIfNewBattery($obj)) { @@ -753,7 +750,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } + } */ } } diff --git a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php index 3c0236e1..32514afd 100644 --- a/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/CMBRiderAPIHandler.php @@ -18,7 +18,6 @@ use App\Service\RiderAPIHandlerInterface; use App\Service\RedisClientProvider; use App\Service\RiderCache; use App\Service\MQTTClient; -use App\Service\WarrantyHandler; use App\Service\JobOrderHandlerInterface; use App\Service\InvoiceGeneratorInterface; @@ -30,7 +29,6 @@ use App\Entity\Promo; use App\Entity\Battery; use App\Entity\BatteryModel; use App\Entity\BatterySize; -use App\Entity\Warranty; use DateTime; @@ -42,7 +40,6 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface protected $rcache; protected $country_code; protected $mclient; - protected $wh; protected $jo_handler; protected $ic; protected $session; @@ -50,8 +47,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, EncoderFactoryInterface $ef, RiderCache $rcache, string $country_code, MQTTClient $mclient, - WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler, - InvoiceGeneratorInterface $ic) + JobOrderHandlerInterface $jo_handler, InvoiceGeneratorInterface $ic) { $this->em = $em; $this->redis = $redis; @@ -59,7 +55,6 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface $this->rcache = $rcache; $this->country_code = $country_code; $this->mclient = $mclient; - $this->wh = $wh; $this->jo_handler = $jo_handler; $this->ic = $ic; @@ -523,6 +518,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface $this->em->flush(); + /* // create warranty if($this->jo_handler->checkIfNewBattery($jo)) { @@ -561,7 +557,7 @@ class CMBRiderAPIHandler implements RiderAPIHandlerInterface $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); } - } + } */ // send mqtt event (fulfilled) $rider = $this->session->getRider(); diff --git a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php index b5bed63b..4a3ea62a 100644 --- a/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php +++ b/src/Service/RiderAPIHandler/ResqRiderAPIHandler.php @@ -18,7 +18,6 @@ use App\Service\RiderAPIHandlerInterface; use App\Service\RedisClientProvider; use App\Service\RiderCache; use App\Service\MQTTClient; -use App\Service\WarrantyHandler; use App\Service\JobOrderHandlerInterface; use App\Service\InvoiceGeneratorInterface; @@ -41,7 +40,6 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface protected $rcache; protected $country_code; protected $mclient; - protected $wh; protected $jo_handler; protected $ic; protected $session; @@ -49,8 +47,7 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface public function __construct(EntityManagerInterface $em, RedisClientProvider $redis, EncoderFactoryInterface $ef, RiderCache $rcache, string $country_code, MQTTClient $mclient, - WarrantyHandler $wh, JobOrderHandlerInterface $jo_handler, - InvoiceGeneratorInterface $ic) + JobOrderHandlerInterface $jo_handler, InvoiceGeneratorInterface $ic) { $this->em = $em; $this->redis = $redis; @@ -58,7 +55,6 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface $this->rcache = $rcache; $this->country_code = $country_code; $this->mclient = $mclient; - $this->wh = $wh; $this->jo_handler = $jo_handler; $this->ic = $ic; @@ -522,6 +518,7 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface $this->em->flush(); + /* // create warranty if($this->jo_handler->checkIfNewBattery($jo)) { @@ -556,7 +553,7 @@ class ResqRiderAPIHandler implements RiderAPIHandlerInterface } $this->wh->createWarranty($serial, $plate_number, $first_name, $last_name, $mobile_number, $batt_list, $date_purchase, $warranty_class); - } + } */ // send mqtt event (fulfilled) $rider = $this->session->getRider();