diff --git a/src/Command/UpdateCustomerVehicleBatteryCommand.php b/src/Command/UpdateCustomerVehicleBatteryCommand.php new file mode 100644 index 00000000..f7e09e30 --- /dev/null +++ b/src/Command/UpdateCustomerVehicleBatteryCommand.php @@ -0,0 +1,67 @@ +em = $om; + $this->custvehicle_hash = []; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('joborder:updatebattery') + ->setDescription('Update customer vehicle battery information.') + ->setHelp('Updates the customer vehicle battery based on the latest battery purchased. '); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // get all job orders with fulfilled status + $jos = $this->em->getRepository(JobOrder::class)->findBy(['status' => JOStatus::FULFILLED], ['date_create' => 'DESC']); + foreach ($jos as $jo) + { + if ($jo->getCustomerVehicle() != null) + { + $custvehicle_id = $jo->getCustomerVehicle()->getID(); + // check if custvehicle is in hash. This would mean that the battery has already been set + if(!isset($this->custvehicle_hash[$custvehicle_id])) + { + // get battery purchased from job order + $items = $jo->getInvoice()->getItems(); + foreach($items as $item) + { + if ($item->getBattery() != null) + { + // set battery of vehicle to the latest battery purchased + $new_battery = $item->getBattery(); + $jo->getCustomerVehicle()->setCurrentBattery($new_battery); + + // add customer vehicle to hash + $this->custvehicle_hash[$custvehicle_id] = true; + } + } + $this->em->flush(); + } + } + } + } +}