Change customer source report generation to use raw sql #539

This commit is contained in:
Kendrick Chan 2021-03-01 23:16:29 +08:00
parent 59a77c945c
commit fd8845118e

View file

@ -16,6 +16,7 @@ use App\Entity\VehicleManufacturer;
use App\Entity\Vehicle;
use DateTime;
use PDO;
class GenerateCustomerSourceReportCommand extends Command
{
@ -34,7 +35,8 @@ class GenerateCustomerSourceReportCommand extends Command
->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('month', InputArgument::REQUIRED, 'Month to process customer data')
->addArgument('csv_file', InputArgument::REQUIRED, 'Output CSV file');
}
protected function execute(InputInterface $input, OutputInterface $output)
@ -44,6 +46,7 @@ class GenerateCustomerSourceReportCommand extends Command
// 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');
@ -56,7 +59,58 @@ class GenerateCustomerSourceReportCommand extends Command
//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,
]);
// 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');
@ -64,13 +118,16 @@ class GenerateCustomerSourceReportCommand extends Command
->setParameter('end_date', $end_date);
$customer_results = $cust_query->iterate();
*/
/*
$customers = [];
foreach ($customer_results as $row)
{
$cust = $row[0];
//error_log($cust->getID());
error_log($cust->getID());
// get the ff fields: first name, last name, landline
// mobile, fax, office, email address, vehicle brand,
// vehicle model, plate number
@ -79,9 +136,75 @@ class GenerateCustomerSourceReportCommand extends Command
// 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;
}
}