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(); } } } } }