Merge branch '539-customer-source-report' into 'master'

Resolve "Customer source report"

Closes #539

See merge request jankstudio/resq!630
This commit is contained in:
Kendrick Chan 2021-03-02 02:38:45 +00:00
commit fc8de9101f
2 changed files with 149 additions and 0 deletions

View file

@ -0,0 +1,140 @@
<?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');
// 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';
$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);
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'];
}
return $cust_ids;
}
}

View file

@ -0,0 +1,9 @@
#!/bin/sh
/var/www/resq/bin/console report:customer_source 2020 09 /tmp/customer_source_report_202009.csv
/var/www/resq/bin/console report:customer_source 2020 10 /tmp/customer_source_report_202010.csv
/var/www/resq/bin/console report:customer_source 2020 11 /tmp/customer_source_report_202011.csv
/var/www/resq/bin/console report:customer_source 2020 12 /tmp/customer_source_report_202012.csv
/var/www/resq/bin/console report:customer_source 2021 01 /tmp/customer_source_report_202101.csv
/var/www/resq/bin/console report:customer_source 2021 02 /tmp/customer_source_report_202102.csv
/var/www/resq/bin/console report:customer_source 2021 03 /tmp/customer_source_report_202103.csv