227 lines
6.9 KiB
PHP
227 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Command;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use App\Entity\Customer;
|
|
use App\Entity\CustomerVehicle;
|
|
use App\Entity\VehicleManufacturer;
|
|
use App\Entity\Vehicle;
|
|
|
|
use DateTime;
|
|
use PDO;
|
|
|
|
class GenerateCustomerSourceReportCommand extends Command
|
|
{
|
|
protected $em;
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure()
|
|
{
|
|
$this->setName('report:customer_source')
|
|
->setDescription('Create customer source reports')
|
|
->setHelp('Creates customer source reports.')
|
|
->addArgument('year', InputArgument::REQUIRED, 'Year to process customer data')
|
|
->addArgument('month', InputArgument::REQUIRED, 'Month to process customer data')
|
|
->addArgument('csv_file', InputArgument::REQUIRED, 'Output CSV file');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
{
|
|
$em = $this->em;
|
|
|
|
// get year and month
|
|
$year = $input->getArgument('year');
|
|
$month = $input->getArgument('month');
|
|
$csv_file = $input->getArgument('csv_file');
|
|
|
|
$start_date = DateTime::createFromFormat('Y-m-d', $year . '-' . $month . '-01');
|
|
$end_date = DateTime::createFromFormat('Y-m-d', $year . '-' . $month . '-01');
|
|
$start_date->setTime(0, 0);
|
|
$str_start_date = $start_date->format('Y-m-d H:i:s');
|
|
|
|
$end_date->modify('last day of this month');
|
|
$end_date->setTime(23, 59);
|
|
$str_end_date = $end_date->format('Y-m-d H:i:s');
|
|
//error_log($start_date->format('Y-m-d H:i:s'));
|
|
//error_log($str_end_date);
|
|
|
|
// get all mobile user customer id hash
|
|
$resq_cust_ids = $this->getRESQCustomerIDs();
|
|
|
|
|
|
// TODO: error checking for file
|
|
$fp = fopen($csv_file, 'w');
|
|
|
|
// pdo connection
|
|
$db = $em->getConnection();
|
|
|
|
// get all customers
|
|
$sql = 'select c.id, c.first_name, c.last_name, c.phone_mobile, c.phone_landline, c.phone_office, c.phone_fax, c.email, c.flag_mobile_app, vm.name, v.make, cv.model_year, cv.plate_number from customer c, customer_vehicle cv, vehicle v, vehicle_manufacturer vm where year(c.date_create) = :year and month(c.date_create) = :month and c.id = cv.customer_id and cv.vehicle_id = v.id and v.manufacturer_id = vm.id';
|
|
$stmt = $db->prepare($sql);
|
|
$stmt->execute([
|
|
'month' => $month,
|
|
'year' => $year,
|
|
]);
|
|
|
|
// add header rows
|
|
fputcsv($fp, [
|
|
'Customer ID',
|
|
'First Name',
|
|
'Last Name',
|
|
'Mobile Phone',
|
|
'Landline',
|
|
'Office Phone',
|
|
'Fax',
|
|
'Email Address',
|
|
'Vehicle Manufacturer',
|
|
'Vehicle Make',
|
|
'Model Year',
|
|
'Plate Number',
|
|
'Source',
|
|
]);
|
|
|
|
// go through rows
|
|
while ($row = $stmt->fetch(PDO::FETCH_NUM))
|
|
{
|
|
// TODO: find customer source
|
|
// NOTE: flag_mobile_app is 0 for some reason
|
|
// customer source
|
|
if (isset($resq_cust_ids[$row[0]]))
|
|
$source = 'resq';
|
|
else
|
|
$source = 'crm / owr';
|
|
|
|
// error_log(print_r($row, true));
|
|
$data = [
|
|
$row[0], // id
|
|
$row[1], // first name
|
|
$row[2], // last name
|
|
$row[3], // phone - mobile
|
|
$row[4], // phone - landline
|
|
$row[5], // phone - office
|
|
$row[6], // phone - fax
|
|
$row[7], // email
|
|
$row[9], // vehicle manufacturer
|
|
$row[10], // vehicle make
|
|
$row[11], // vehicle model
|
|
$row[12], // plate number
|
|
$source // customer source
|
|
];
|
|
|
|
fputcsv($fp, $data);
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
/*
|
|
$cust_query = $em->createQuery('select c from App\Entity\Customer c
|
|
where c.date_create >= :start_date and c.date_create <= :end_date
|
|
order by c.date_create asc');
|
|
$cust_query->setParameter('start_date', $start_date)
|
|
->setParameter('end_date', $end_date);
|
|
|
|
$customer_results = $cust_query->iterate();
|
|
*/
|
|
|
|
|
|
/*
|
|
$customers = [];
|
|
foreach ($customer_results as $row)
|
|
{
|
|
$cust = $row[0];
|
|
|
|
error_log($cust->getID());
|
|
// get the ff fields: first name, last name, landline
|
|
// mobile, fax, office, email address, vehicle brand,
|
|
// vehicle model, plate number
|
|
|
|
// check if flag_confirmed == true and date_confirmed is not null
|
|
// if so, source is Resq app
|
|
// if not, source is OWR/CRM
|
|
|
|
// TODO: figure out the source
|
|
if ($cust->hasMobileApp())
|
|
$source = 'resq';
|
|
else
|
|
$source = 'crm / owr';
|
|
|
|
|
|
// NOTE: this does not eliminate duplicate vehicles
|
|
$vehicles = $cust->getVehicles();
|
|
|
|
foreach ($vehicles as $cv)
|
|
{
|
|
$v = $cv->getVehicle();
|
|
$vmfg = $v->getManufacturer();
|
|
|
|
$data = [
|
|
$cust->getID(),
|
|
$cust->getFirstName(),
|
|
$cust->getLastName(),
|
|
$cust->getPhoneMobile(),
|
|
$cust->getPhoneLandline(),
|
|
$cust->getPhoneOffice(),
|
|
$cust->getPhoneFax(),
|
|
$cust->getEmail(),
|
|
$vmfg->getName(),
|
|
$v->getMake(),
|
|
$v->getModelYearFormatted(),
|
|
$cv->getPlateNumber(),
|
|
$cv->getColor(),
|
|
$source,
|
|
];
|
|
|
|
// detach associations
|
|
// NOTE: we may need to detach vehicle manufacturer eventually
|
|
$em->detach($cv);
|
|
$em->detach($v);
|
|
$em->detach($vmfg);
|
|
|
|
fputcsv($fp, $data);
|
|
}
|
|
|
|
$em->detach($row[0]);
|
|
}
|
|
|
|
fclose($fp);
|
|
*/
|
|
|
|
return 0;
|
|
}
|
|
|
|
protected function getRESQCustomerIDs()
|
|
{
|
|
// pdo connection
|
|
$db = $this->em->getConnection();
|
|
|
|
// get all customer ids of all mobile sessions
|
|
$sql = 'select distinct customer_id from mobile_session';
|
|
$stmt = $db->prepare($sql);
|
|
$stmt->execute();
|
|
$res = $stmt->fetchAll();
|
|
|
|
$cust_ids = [];
|
|
|
|
foreach ($res as $cust)
|
|
{
|
|
$cust_ids[$cust['customer_id']] = $cust['customer_id'];
|
|
}
|
|
|
|
// error_log(print_r($cust_ids));
|
|
return $cust_ids;
|
|
}
|
|
}
|