169 lines
4.8 KiB
PHP
169 lines
4.8 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 CrEOF\Spatial\PHP\Types\Geometry\Point;
|
|
use DateTime;
|
|
|
|
use App\Entity\Partner;
|
|
use App\Entity\Service;
|
|
|
|
class ImportPartnersCommand extends Command
|
|
{
|
|
// field index in csv file for needed info
|
|
// other indices ignore
|
|
const F_DEALER = 2;
|
|
const F_OFFICE_NUM = 5;
|
|
const F_ADDRESS = 6;
|
|
const F_MONDAY = 7;
|
|
const F_CONTACT_NUM = 15;
|
|
const F_LAT = 16;
|
|
const F_LONG = 17;
|
|
const F_TIRE = 18;
|
|
const F_AIRCON = 19;
|
|
const F_REPAIR = 20;
|
|
const F_BODY = 21;
|
|
const F_PARTS = 22;
|
|
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName('partner:import')
|
|
->setDescription('Import a CSV file with partners.')
|
|
->setHelp('Creates partners based on imported CSV.')
|
|
->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$csv_file = $input->getArgument('file');
|
|
|
|
// attempt to open file
|
|
try
|
|
{
|
|
$fh = fopen($csv_file, "r");
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception('The file "' . $csv_file . '" could be read.');
|
|
}
|
|
|
|
// get entity manager
|
|
$em = $this->em;
|
|
|
|
// loop through the rows
|
|
$row_num = 0;
|
|
while (($fields = fgetcsv($fh)) !== false)
|
|
{
|
|
if ($row_num < 2)
|
|
{
|
|
$row_num++;
|
|
continue;
|
|
}
|
|
|
|
$output->writeln("Parsing row " . $row_num . "...");
|
|
|
|
// parse
|
|
$name = trim($fields[self::F_DEALER]);
|
|
$office_num = trim($fields[self::F_OFFICE_NUM]);
|
|
$address = trim($fields[self::F_ADDRESS]);
|
|
$monday = trim($fields[self::F_MONDAY]);
|
|
$contact_num = trim($fields[self::F_CONTACT_NUM]);
|
|
$lat = trim($fields[self::F_LAT]);
|
|
$long = trim($fields[self::F_LONG]);
|
|
|
|
// parse time
|
|
if (!empty($monday))
|
|
{
|
|
$format = 'g:i A';
|
|
$times = explode("-", $monday);
|
|
$time_open = DateTime::createFromFormat($format, $times[0]);
|
|
$time_close = DateTime::createFromFormat($format, $times[1]);
|
|
}
|
|
|
|
// coordinates
|
|
if ((!empty($lat))&&(!empty($long)))
|
|
{
|
|
$point = new Point($long, $lat);
|
|
}
|
|
|
|
// consolidate contact numbers
|
|
$phone_numbers = "";
|
|
if ((!empty($contact_num))&&(!empty($office_num)))
|
|
{
|
|
$phone_numbers = $office_num . ' / ' . $contact_num;
|
|
}
|
|
else {
|
|
if (!empty($office_num))
|
|
{
|
|
$phone_numbers = $office_num;
|
|
}
|
|
if (!empty($contact_num))
|
|
{
|
|
$phone_numbers = $contact_num;
|
|
}
|
|
}
|
|
|
|
// create partner
|
|
$partner = new Partner();
|
|
|
|
$partner->setName($name)
|
|
->setAddress($address)
|
|
->setCoordinates($point)
|
|
->setContactNumbers($phone_numbers)
|
|
->setTimeOpen($time_open)
|
|
->setTimeClose($time_close)
|
|
->setBranch("");
|
|
|
|
// process the services
|
|
if ($fields[self::F_TIRE] == 'YES')
|
|
{
|
|
$service = $em->getRepository(Service::class)->find('1');
|
|
$partner->addService($service);
|
|
}
|
|
if ($fields[self::F_AIRCON] == 'YES')
|
|
{
|
|
$service = $em->getRepository(Service::class)->find('2');
|
|
$partner->addService($service);
|
|
}
|
|
if ($fields[self::F_REPAIR] == 'YES')
|
|
{
|
|
$service = $em->getRepository(Service::class)->find('3');
|
|
$partner->addService($service);
|
|
}
|
|
if ($fields[self::F_BODY] == 'YES')
|
|
{
|
|
$service = $em->getRepository(Service::class)->find('4');
|
|
$partner->addService($service);
|
|
}
|
|
if ($fields[self::F_PARTS] == 'YES')
|
|
{
|
|
$service = $em->getRepository(Service::class)->find('5');
|
|
$partner->addService($service);
|
|
}
|
|
|
|
$em->persist($partner);
|
|
$em->flush();
|
|
|
|
$row_num++;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
}
|