Fix query to get customers for report. #267

This commit is contained in:
Korina Cordero 2019-09-17 03:32:37 +00:00
parent 4a50729a41
commit bee8a0d9fe
2 changed files with 32 additions and 169 deletions

View file

@ -1,115 +0,0 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Query\ResultSetMapping;
use App\Entity\JobOrder;
use App\Entity\CustomerVehicle;
use App\Entity\MobileSession;
use App\Entity\Customer;
class GenerateMEHCustomerReportCommand extends Command
{
private $em;
public function __construct(ObjectManager $om)
{
$this->em = $om;
parent::__construct();
}
protected function configure()
{
$this->setName('customer:generateMEHReport')
->setDescription('Generate MEH Customer Report.')
->setHelp('Generate MEH Customer Report.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$customer_outfile = fopen('/tmp/meh_customer_report.csv', 'w');
$data = $this->getMEHCustomerData($output);
foreach($data as $row)
{
$line = $row['last_name'] . ',' . $row['first_name'] . ',' .
$row['cust_mobile_number'] . ',' . $row['landline_number'] . ',' .
$row['office_number'] . ',' . $row['fax_number'] . ',' .
$row['date_mobile'] . ',' . $row['mobile_number'];
$output->writeln("Writing " . $line);
fwrite($customer_outfile, $line . "\n");
}
fclose($customer_outfile);
}
protected function getMEHCustomerData($output)
{
$em = $this->em;
$results = [];
$output->writeln("about to query");
$rsm = new ResultSetMapping();
$query = $em->createNativeQuery('SELECT c.id, c.last_name, c.first_name, c.phone_mobile, c.phone_landline,
c.phone_office, c.phone_fax, jo.source FROM job_order jo,
customer_vehicle cv, customer c
WHERE c.id = cv.customer_id
AND jo.cvehicle_id = cv.id
AND c.policy_mobile_app_id IS NOT NULL
AND jo.source != \'mobile\'', $rsm);
$results = $query->getResult();
$output->writeln("about to iterate");
foreach($results as $row)
{
// check if the source of the job order is not 'mobile' (meaning JO originated from MEH)
$output->writeln("checking job order source");
//if ($row[0]->getSource() != 'mobile')
//{
// $mobile_date = '';
// $mobile_number = '';
// $mobile_session = $em->getRepository(MobileSession::class)
// ->findOneBy(['customer' => $row[1]->getID()], ['date_generated' => 'ASC']);
// if ($mobile_session != null)
// {
// $output->writeln("mobile session not null");
// $mobile_date = $mobile_session->getDateGenerated()->format("d M Y");
// $mobile_number = $mobile_session->getPhoneNumber();
// }
// $results[] = [
// 'last_name' => $row[1]->getLastName(),
// 'first_name' => $row[1]->getFirstName(),
// 'cust_mobile_number' => $row[1]->getPhoneMobile(),
// 'landline_number' => $row[1]->getPhoneLandline(),
// 'office_number' => $row[1]->getPhoneOffice(),
// 'fax_number' => $row[1]->getPhoneFax(),
// 'date_mobile' => $mobile_date,
// 'mobile_number' => $mobile_number,
// ];
//}
//$this->em->detach($cv[0]);
//$this->em->detach($cv[1]);
}
return $results;
}
}

View file

@ -675,69 +675,47 @@ class ReportController extends Controller
{
$results = [];
//get the policy id for the mobile privacy policy from env
$dotenv = new Dotenv();
$dotenv->loadEnv(__DIR__.'/../../.env');
$conn = $em->getConnection();
$sql = 'SELECT c.id, c.last_name, c.first_name, c.phone_mobile, c.phone_landline,
c.phone_office, c.phone_fax, jo.source FROM job_order jo,
customer_vehicle cv, customer c
WHERE c.id = cv.customer_id
AND jo.cvehicle_id = cv.id
AND c.policy_mobile_app_id IS NOT NULL
AND jo.source != :source';
$policy_mobile_id = $_ENV['POLICY_MOBILE'];
$stmt = $conn->prepare($sql);
$stmt->execute(array('source' => 'mobile_app'));
// get all the customers with mobile privacy policy id
$customers = $em->getRepository(Customer::class)->findBy(['privpol_mobile_app' => $policy_mobile_id]);
$query_results = $stmt->fetchAll();
foreach ($customers as $customer)
foreach($query_results as $row)
{
// get plate numbers
$plate_numbers = $customer->getPlateNumberList();
$mobile_date = '';
$mobile_number = '';
// using plate number, get all customer vehicles with the plate numbers
//$cust_vehicles = $em->createQuery('SELECT cv from App\Entity\CustomerVehicle cv
// WHERE cv.plate_number in (:plate_numbers)')
// ->setParameter('plate_numbers', $plate_numbers)
// ->getResult();
foreach ($plate_numbers as $plate_number)
$mobile_session = $em->getRepository(MobileSession::class)
->findOneBy(['customer' => $row['id']], ['date_generated' => 'ASC']);
if ($mobile_session != null)
{
$cust_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $plate_number]);
if ($cust_vehicles != null)
{
foreach($cust_vehicles as $cv)
{
// get the job orders for each cv
$job_orders = $cv->getJobOrders();
foreach($job_orders as $jo)
{
// check if the source of the job order is not 'mobile' (meaning JO originated from MEH)
if ($jo->getSource() != 'mobile')
{
$mobile_date = '';
$mobile_number = '';
$mobile_session = $em->getRepository(MobileSession::class)
->findOneBy(['customer' => $customer->getID()], ['date_generated' => 'ASC']);
if ($mobile_session != null)
{
$mobile_date = $mobile_session->getDateGenerated()->format("d M Y");
$mobile_number = $mobile_session->getPhoneNumber();
}
$results[] = [
'last_name' => $customer->getLastName(),
'first_name' => $customer->getFirstName(),
'cust_mobile_number' => $customer->getPhoneMobile(),
'landline_number' => $customer->getPhoneLandline(),
'office_number' => $customer->getPhoneOffice(),
'fax_number' => $customer->getPhoneFax(),
'date_mobile' => $mobile_date,
'mobile_number' => $mobile_number,
];
}
}
}
}
$mobile_date = $mobile_session->getDateGenerated()->format("d M Y");
$mobile_number = $mobile_session->getPhoneNumber();
}
$results[] = [
'last_name' => $row['last_name'],
'first_name' => $row['first_name'],
'cust_mobile_number' => $row['phone_mobile'],
'landline_number' => $row['phone_landline'],
'office_number' => $row['phone_office'],
'fax_number' => $row['phone_fax'],
'date_mobile' => $mobile_date,
'mobile_number' => $mobile_number,
];
}
return $results;
}
}