From 59a77c945ca0f87b188a5f2f0dd7d4ee630b0077 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 1 Mar 2021 11:09:43 +0000 Subject: [PATCH 1/6] Add command to get all customers for report. #539 --- .../GenerateCustomerSourceReportCommand.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/Command/GenerateCustomerSourceReportCommand.php diff --git a/src/Command/GenerateCustomerSourceReportCommand.php b/src/Command/GenerateCustomerSourceReportCommand.php new file mode 100644 index 00000000..5a178cd1 --- /dev/null +++ b/src/Command/GenerateCustomerSourceReportCommand.php @@ -0,0 +1,87 @@ +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'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $em = $this->em; + + // get year and month + $year = $input->getArgument('year'); + $month = $input->getArgument('month'); + + $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 customers + $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 + + $em->detach($row[0]); + } + + return 0; + } +} From fd8845118e7c9bfaedc711966ac7321692294cad Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Mon, 1 Mar 2021 23:16:29 +0800 Subject: [PATCH 2/6] Change customer source report generation to use raw sql #539 --- .../GenerateCustomerSourceReportCommand.php | 127 +++++++++++++++++- 1 file changed, 125 insertions(+), 2 deletions(-) diff --git a/src/Command/GenerateCustomerSourceReportCommand.php b/src/Command/GenerateCustomerSourceReportCommand.php index 5a178cd1..a3f85c4a 100644 --- a/src/Command/GenerateCustomerSourceReportCommand.php +++ b/src/Command/GenerateCustomerSourceReportCommand.php @@ -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; + } } From 8f839674b553f387710d6509f3f6a9c16d4ed1b9 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 2 Mar 2021 01:49:03 +0000 Subject: [PATCH 3/6] Add headers to csv file. #539 --- .../GenerateCustomerSourceReportCommand.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Command/GenerateCustomerSourceReportCommand.php b/src/Command/GenerateCustomerSourceReportCommand.php index a3f85c4a..893fe6ed 100644 --- a/src/Command/GenerateCustomerSourceReportCommand.php +++ b/src/Command/GenerateCustomerSourceReportCommand.php @@ -77,6 +77,23 @@ class GenerateCustomerSourceReportCommand extends Command '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)) { From cd48c5e032194a2badbc3ec15d640a9230a9ca37 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 2 Mar 2021 02:19:37 +0000 Subject: [PATCH 4/6] Add script to generate the customer source report. #539 --- .../generateCustomerSourceReport.sh | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 utils/customer_source_report/generateCustomerSourceReport.sh diff --git a/utils/customer_source_report/generateCustomerSourceReport.sh b/utils/customer_source_report/generateCustomerSourceReport.sh new file mode 100755 index 00000000..5d0c3700 --- /dev/null +++ b/utils/customer_source_report/generateCustomerSourceReport.sh @@ -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 From 643bc61bf90dfb91ee8c82208547dbf2c735e7e4 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 2 Mar 2021 02:31:11 +0000 Subject: [PATCH 5/6] Remove commented out code. #539 --- .../GenerateCustomerSourceReportCommand.php | 77 ------------------- 1 file changed, 77 deletions(-) diff --git a/src/Command/GenerateCustomerSourceReportCommand.php b/src/Command/GenerateCustomerSourceReportCommand.php index 893fe6ed..68339d5e 100644 --- a/src/Command/GenerateCustomerSourceReportCommand.php +++ b/src/Command/GenerateCustomerSourceReportCommand.php @@ -56,8 +56,6 @@ class GenerateCustomerSourceReportCommand extends Command $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(); @@ -105,7 +103,6 @@ class GenerateCustomerSourceReportCommand extends Command else $source = 'crm / owr'; - // error_log(print_r($row, true)); $data = [ $row[0], // id $row[1], // first name @@ -127,79 +124,6 @@ class GenerateCustomerSourceReportCommand extends Command 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; } @@ -221,7 +145,6 @@ class GenerateCustomerSourceReportCommand extends Command $cust_ids[$cust['customer_id']] = $cust['customer_id']; } - // error_log(print_r($cust_ids)); return $cust_ids; } } From b7c722d8e926c7bb784e6a6562c9af65a560986c Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 2 Mar 2021 02:37:17 +0000 Subject: [PATCH 6/6] Remove the unused date code. #539 --- src/Command/GenerateCustomerSourceReportCommand.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Command/GenerateCustomerSourceReportCommand.php b/src/Command/GenerateCustomerSourceReportCommand.php index 68339d5e..7b326963 100644 --- a/src/Command/GenerateCustomerSourceReportCommand.php +++ b/src/Command/GenerateCustomerSourceReportCommand.php @@ -48,19 +48,9 @@ class GenerateCustomerSourceReportCommand extends Command $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'); - // get all mobile user customer id hash $resq_cust_ids = $this->getRESQCustomerIDs(); - // TODO: error checking for file $fp = fopen($csv_file, 'w');