From bc80cc91ea252e1836ea13a3f175c4801d52f8e7 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 22 Jul 2021 06:39:03 +0000 Subject: [PATCH] Create command to set branch code for existing warranties. #597 --- src/Command/SetWarrantyBranchCodeCommand.php | 72 ++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/Command/SetWarrantyBranchCodeCommand.php diff --git a/src/Command/SetWarrantyBranchCodeCommand.php b/src/Command/SetWarrantyBranchCodeCommand.php new file mode 100644 index 00000000..c6478e49 --- /dev/null +++ b/src/Command/SetWarrantyBranchCodeCommand.php @@ -0,0 +1,72 @@ +em = $em; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('warranty:setbranchcode') + ->setDescription('Set branch code for warranty, if any.') + ->setHelp('Set branch code for warranty, if any.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $em = $this->em; + + // get all warranties with dealer_name + $query = $em->createQuery('select w from App\Entity\Warranty w where w.dealer_name is not null and w.dealer_name != \'\''); + + $result = $query->iterate(); + + foreach ($result as $row) + { + $warranty = $row[0]; + + // get dealer name, uppercase it since dealer name is saved in uppercase in db + $dealer_name = strtoupper($warranty->getDealerName()); + // error_log('warranty dealer name ' . $dealer_name); + + // find dealer using name + $dealer_results = $em->getRepository(Dealer::class)->findBy(['name' => $dealer_name]); + foreach ($dealer_results as $dealer) + { + error_log('Setting branch code for warranty with dealer name ' . $dealer->getName()); + // get branch code + $branch_code = $dealer->getBranchCode(); + + // set warranty branch code + $warranty->setDealerBranchCode($branch_code); + + $em->flush(); + } + + $em->detach($row[0]); + } + + $em->clear(); + + return 0; + } +}