Create command to set branch code for existing warranties. #597

This commit is contained in:
Korina Cordero 2021-07-22 06:39:03 +00:00
parent f2046625d8
commit bc80cc91ea

View file

@ -0,0 +1,72 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Warranty;
use App\Entity\Dealer;
class SetWarrantyBranchCodeCommand extends Command
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->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;
}
}