resq/src/Command/WarrantySMSCommand.php
2020-05-05 03:07:08 +00:00

115 lines
3.3 KiB
PHP

<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\RisingTideGateway;
use App\Entity\Warranty;
use DateTime;
class WarrantySMSCommand extends Command
{
protected $gateway;
protected $em;
protected function configure()
{
$this->setName('warranty:sms')
->setDescription('Sends an SMS message to users whose warranty expired one month ago.')
->setHelp('Sends warranty SMS.')
->addArgument('date', InputArgument::OPTIONAL, 'Date to use as basis of expiration. Defaults to current date.');
}
public function __construct(EntityManagerInterface $em, RisingTideGateway $gateway)
{
$this->em = $em;
$this->gateway = $gateway;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$date_string = $input->getArgument('date');
// if date is specified
if (!empty($date_string))
$date = DateTime::createFromFormat('Ymd', $date_string);
else
$date = new DateTime();
// -1 month
$date->modify('-1 month');
$warrs = $this->em->getRepository(Warranty::class)->findBy(['date_expire' => $date]);
$valid_numbers = [];
foreach ($warrs as $w)
{
error_log($w->getID() . ' - ' . $w->getMobileNumber());
// check valid number
$num = trim($w->getMobileNumber());
// should be 10 digits
if (strlen($num) != 10)
continue;
// should start with '9'
if ($num[0] != '9')
continue;
// should be numeric
if (!is_numeric($num))
continue;
// should not be 9900000000
if ($num == '9900000000')
continue;
// add 63 prefix
$num = '63' . $num;
// get battery model and size
$bsize = $w->getBatterySize();
$bmodel = $w->getBatteryModel();
if ($bsize == null || $bmodel == null)
$batt = '';
else
$batt = $bsize->getName() . ' ' . $bmodel->getName();
$valid_numbers[] = [
'number' => $num,
'name' => $w->getFirstName(),
'plate' => $w->getPlateNumber(),
'batt' => $batt,
];
}
error_log(print_r($valid_numbers, true));
foreach ($valid_numbers as $wdata)
{
$msg = 'Hi ' . $wdata['name'] . ', the warranty for the ' . $wdata['batt'] . ' installed in your car(' . $wdata['plate'] . ') has expired already. Please call MOTOLITE EXPRESS HATID at 8370-6686 to have the status of your battery checked to avoid any inconvenience. Thank you for choosing Motolite.';
error_log($wdata['number'] . ' - sending ' . $msg);
$this->gateway->sendSMS($wdata['number'], 'MOTOLITE', $msg);
}
/*
error_log('sending sms to ' . $number);
$msg = 'This is a test.';
$this->gateway->sendSMS($number, 'MOTOLITE', $msg);
return 0;
*/
}
}