Merge branch '255-retrieve-only-one-customer-vehicle' into 'master'
Resolve "Retrieve only one customer vehicle" Closes #255 See merge request jankstudio/resq!301
This commit is contained in:
commit
041a7cf29b
1 changed files with 94 additions and 70 deletions
|
|
@ -16,7 +16,9 @@ use App\Entity\MobileSession;
|
||||||
|
|
||||||
use Doctrine\ORM\Query;
|
use Doctrine\ORM\Query;
|
||||||
use Doctrine\ORM\QueryBuilder;
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Doctrine\DBAL\Connection;
|
use Doctrine\DBAL\Connection;
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||||
|
|
@ -442,13 +444,13 @@ class ReportController extends Controller
|
||||||
/**
|
/**
|
||||||
* @Menu(selected="outlet_list")
|
* @Menu(selected="outlet_list")
|
||||||
*/
|
*/
|
||||||
public function popappExportCSV(Request $req)
|
public function popappExportCSV(Request $req, EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
// retrieve temporary info for file
|
// retrieve temporary info for file
|
||||||
$file = $req->files->get('csv_file');
|
$file = $req->files->get('csv_file');
|
||||||
|
|
||||||
// process the csv file
|
// process the csv file
|
||||||
$data = $this->processPopappFile($file);
|
$data = $this->processPopappFile($file, $em);
|
||||||
|
|
||||||
$resp = new StreamedResponse();
|
$resp = new StreamedResponse();
|
||||||
$resp->setCallback(function() use ($data) {
|
$resp->setCallback(function() use ($data) {
|
||||||
|
|
@ -492,7 +494,7 @@ class ReportController extends Controller
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function processPopappFile(UploadedFile $csv_file)
|
protected function processPopappFile(UploadedFile $csv_file, EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
// attempt to open file
|
// attempt to open file
|
||||||
try
|
try
|
||||||
|
|
@ -507,90 +509,112 @@ class ReportController extends Controller
|
||||||
// loop through the rows
|
// loop through the rows
|
||||||
$row_num = 0;
|
$row_num = 0;
|
||||||
$results = [];
|
$results = [];
|
||||||
|
|
||||||
while(($fields = fgetcsv($fh)) !== false)
|
while(($fields = fgetcsv($fh)) !== false)
|
||||||
{
|
{
|
||||||
$has_warranty = false;
|
//error_log($row_num . ' ' . trim($fields[2]));
|
||||||
if ($row_num <= 2)
|
if ($row_num < 1)
|
||||||
{
|
{
|
||||||
$row_num++;
|
$row_num++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the data
|
// pre-populate the result array
|
||||||
$serial = trim($fields[2]);
|
$results[] = [
|
||||||
|
'model_size' => trim($fields[0]),
|
||||||
|
'sku' => trim($fields[1]),
|
||||||
|
'serial' => trim($fields[2]),
|
||||||
|
'created_date' => trim($fields[3]),
|
||||||
|
'branch_name' => trim($fields[4]),
|
||||||
|
'branch_code' => trim($fields[5]),
|
||||||
|
'cust_id' => '',
|
||||||
|
'cust_lastname' => '',
|
||||||
|
'cust_firstname' => '',
|
||||||
|
'cust_mobile_number' => '',
|
||||||
|
'warr_lastname' => '',
|
||||||
|
'warr_firstname' => '',
|
||||||
|
'plate_num' => '',
|
||||||
|
'warr_date_create' => '',
|
||||||
|
'warr_activation_status' => '',
|
||||||
|
'has_mobile' => '',
|
||||||
|
'date_mobile' => '',
|
||||||
|
'mobile_number' => '',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
// get the warranty for serial
|
foreach($results as $key => $data)
|
||||||
$warr_qb = $this->getDoctrine()
|
{
|
||||||
->getRepository(Warranty::class)
|
//error_log($results[$key]['model_size']);
|
||||||
->createQueryBuilder('q');
|
|
||||||
$warranty_query = $warr_qb->select('q')
|
// get the serial
|
||||||
|
$serial = $results[$key]['serial'];
|
||||||
|
|
||||||
|
// if serial is empty, move to next element
|
||||||
|
if (!empty($serial))
|
||||||
|
{
|
||||||
|
// get the warranty for serial
|
||||||
|
$warr_qb = $this->getDoctrine()
|
||||||
|
->getRepository(Warranty::class)
|
||||||
|
->createQueryBuilder('q');
|
||||||
|
$warranty_query = $warr_qb->select('q')
|
||||||
->where('q.serial = :serial')
|
->where('q.serial = :serial')
|
||||||
->setParameter('serial', $serial);
|
->setParameter('serial', $serial);
|
||||||
$warranty = $warranty_query->getQuery()->getOneOrNullResult();
|
$warranty = $warranty_query->getQuery()->getOneOrNullResult();
|
||||||
|
|
||||||
if ($warranty != null)
|
if ($warranty != null)
|
||||||
{
|
|
||||||
$isValid = InvalidPlateNumber::isInvalid($warranty->getPlateNumber());
|
|
||||||
if ($isValid)
|
|
||||||
{
|
{
|
||||||
// get customer vehicle using plate number
|
$isValid = InvalidPlateNumber::isInvalid($warranty->getPlateNumber());
|
||||||
$em = $this->getDoctrine()->getManager();
|
if ($isValid)
|
||||||
$cust_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $warranty->getPlateNumber()]);
|
|
||||||
foreach ($cust_vehicles as $cv)
|
|
||||||
{
|
{
|
||||||
// get customer info
|
// get customer vehicles using plate number
|
||||||
// get mobile session of customer
|
$customer_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $warranty->getPlateNumber()]);
|
||||||
//error_log($cv->getCustomer()->getLastName() . ' ' . $cv->getCustomer()->getFirstName());
|
|
||||||
$has_mobile = false;
|
// check if customer vehicle is empty
|
||||||
$mobile_date = '';
|
if (count($customer_vehicles) != 0)
|
||||||
$mobile_number = '';
|
|
||||||
$mobile_session = $em->getRepository(MobileSession::class)
|
|
||||||
->findOneBy(['customer' => $cv->getCustomer()->getID()], ['date_generated' => 'ASC']);
|
|
||||||
if ($mobile_session != null)
|
|
||||||
{
|
{
|
||||||
// get mobile data
|
$has_mobile = false;
|
||||||
$has_mobile = true;
|
$mobile_date = '';
|
||||||
$mobile_date = $mobile_session->getDateGenerated()->format("d M Y");
|
$mobile_number = '';
|
||||||
$mobile_number = $mobile_session->getPhoneNumber();
|
|
||||||
|
// get the first customer vehicle, store as best_cv until we find one with a mobile session
|
||||||
|
$best_cv = current($customer_vehicles);
|
||||||
|
|
||||||
|
foreach($customer_vehicles as $cv)
|
||||||
|
{
|
||||||
|
// get mobile session of customer
|
||||||
|
//error_log($cv->getCustomer()->getLastName() . ' ' . $cv->getCustomer()->getFirstName());
|
||||||
|
$mobile_session = $em->getRepository(MobileSession::class)
|
||||||
|
->findOneBy(['customer' => $cv->getCustomer()->getID()], ['date_generated' => 'ASC']);
|
||||||
|
if ($mobile_session != null)
|
||||||
|
{
|
||||||
|
// get mobile data
|
||||||
|
$has_mobile = true;
|
||||||
|
$mobile_date = $mobile_session->getDateGenerated()->format("d M Y");
|
||||||
|
$mobile_number = $mobile_session->getPhoneNumber();
|
||||||
|
|
||||||
|
// set best_cv to this customer vehicle with mobile session
|
||||||
|
$best_cv = $cv;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the customer data in results
|
||||||
|
$results[$key]['cust_id'] = $best_cv->getCustomer()->getID();
|
||||||
|
$results[$key]['cust_lastname'] = $best_cv->getCustomer()->getLastName();
|
||||||
|
$results[$key]['cust_firstname'] = $best_cv->getCustomer()->getFirstName();
|
||||||
|
$results[$key]['cust_mobile_number'] = $best_cv->getCustomer()->getPhoneMobile();
|
||||||
|
$results[$key]['plate_num'] = $best_cv->getPlateNumber();
|
||||||
|
$results[$key]['has_mobile'] = ($has_mobile ? 'Yes' : 'No');
|
||||||
|
$results[$key]['date_mobile'] = $mobile_date;
|
||||||
|
$results[$key]['mobile_number'] = $mobile_number;
|
||||||
}
|
}
|
||||||
$has_warranty = true;
|
}
|
||||||
$results[] = [
|
// set the warranty data in results
|
||||||
'model_size' => trim($fields[0]),
|
$results[$key]['warr_lastname'] = $warranty->getLastName();
|
||||||
'sku' => trim($fields[1]),
|
$results[$key]['warr_firstname'] = $warranty->getFirstName();
|
||||||
'serial' => $serial,
|
$results[$key]['warr_date_create'] = $warranty->getDateCreate()->format("d M Y");
|
||||||
'created_date' => trim($fields[3]),
|
$results[$key]['warr_activation_status'] = ($warranty->isActivated() ? 'Active' : 'Inactive');
|
||||||
'branch_name' => trim($fields[4]),
|
|
||||||
'branch_code' => trim($fields[5]),
|
|
||||||
'cust_id' => $cv->getCustomer()->getID(),
|
|
||||||
'cust_lastname' => $cv->getCustomer()->getLastName(),
|
|
||||||
'cust_firstname' => $cv->getCustomer()->getFirstName(),
|
|
||||||
'cust_mobile_number' => $cv->getCustomer()->getPhoneMobile(),
|
|
||||||
'warr_lastname' => $warranty->getLastName(),
|
|
||||||
'warr_firstname' => $warranty->getFirstName(),
|
|
||||||
'plate_num' => $cv->getPlateNumber(),
|
|
||||||
'warr_date_create' => $warranty->getDateCreate()->format("d M Y"),
|
|
||||||
'warr_activation_status' => ($warranty->isActivated() ? 'Active' : 'Inactive'),
|
|
||||||
'has_mobile' => ($has_mobile ? 'Yes' : 'No'),
|
|
||||||
'date_mobile' => $mobile_date,
|
|
||||||
'mobile_number' => $mobile_number,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($has_warranty == false)
|
|
||||||
{
|
|
||||||
$results[] = [
|
|
||||||
'model_size' => trim($fields[0]),
|
|
||||||
'sku' => trim($fields[1]),
|
|
||||||
'serial' => $serial,
|
|
||||||
'created_date' => trim($fields[3]),
|
|
||||||
'branch_name' => trim($fields[4]),
|
|
||||||
'branch_code' => trim($fields[5]),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$row_num++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue