resq/src/Command/SetWarrantyBranchCodeCommand.php
2021-07-23 04:06:48 +00:00

68 lines
2 KiB
PHP

<?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 with findOneBy
$dealer = $em->getRepository(Dealer::class)->findOneBy(['name' => $dealer_name]);
if ($dealer != null)
{
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();
return 0;
}
}