Merge branch '242-migration-script-for-other-services-providers-2' into 'master'
Resolve "Migration script for other services providers" Closes #242 See merge request jankstudio/resq!285
This commit is contained in:
commit
a1ad5457ac
2 changed files with 168 additions and 1 deletions
168
src/Command/ImportPartnersCommand.php
Normal file
168
src/Command/ImportPartnersCommand.php
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
<?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\Common\Persistence\ObjectManager;
|
||||
|
||||
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(Objectmanager $om)
|
||||
{
|
||||
$this->em = $om;
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -28,7 +28,6 @@ trait Location
|
|||
// branch name
|
||||
/**
|
||||
* @ORM\Column(type="string", length=80)
|
||||
* @Assert\NotBlank()
|
||||
*/
|
||||
protected $branch;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue