Redo the processing of the popapp csv file. #255
This commit is contained in:
parent
b4c8b83742
commit
ce83ed689b
1 changed files with 93 additions and 69 deletions
|
|
@ -16,7 +16,9 @@ use App\Entity\MobileSession;
|
|||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
|
@ -442,13 +444,13 @@ class ReportController extends Controller
|
|||
/**
|
||||
* @Menu(selected="outlet_list")
|
||||
*/
|
||||
public function popappExportCSV(Request $req)
|
||||
public function popappExportCSV(Request $req, EntityManagerInterface $em)
|
||||
{
|
||||
// retrieve temporary info for file
|
||||
$file = $req->files->get('csv_file');
|
||||
|
||||
// process the csv file
|
||||
$data = $this->processPopappFile($file);
|
||||
$data = $this->processPopappFile($file, $em);
|
||||
|
||||
$resp = new StreamedResponse();
|
||||
$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, $em)
|
||||
{
|
||||
// attempt to open file
|
||||
try
|
||||
|
|
@ -507,9 +509,10 @@ class ReportController extends Controller
|
|||
// loop through the rows
|
||||
$row_num = 0;
|
||||
$results = [];
|
||||
|
||||
// TODO: first loop populate the result array
|
||||
while(($fields = fgetcsv($fh)) !== false)
|
||||
{
|
||||
$has_warranty = false;
|
||||
//error_log($row_num . ' ' . trim($fields[2]));
|
||||
if ($row_num < 1)
|
||||
{
|
||||
|
|
@ -517,81 +520,102 @@ class ReportController extends Controller
|
|||
continue;
|
||||
}
|
||||
|
||||
// get the data
|
||||
$serial = trim($fields[2]);
|
||||
// pre-populate the result array
|
||||
$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
|
||||
$warr_qb = $this->getDoctrine()
|
||||
->getRepository(Warranty::class)
|
||||
->createQueryBuilder('q');
|
||||
$warranty_query = $warr_qb->select('q')
|
||||
foreach($results as $key => $data)
|
||||
{
|
||||
//error_log($results[$key]['model_size']);
|
||||
|
||||
// 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')
|
||||
->setParameter('serial', $serial);
|
||||
$warranty = $warranty_query->getQuery()->getOneOrNullResult();
|
||||
$warranty = $warranty_query->getQuery()->getOneOrNullResult();
|
||||
|
||||
if ($warranty != null)
|
||||
{
|
||||
$isValid = InvalidPlateNumber::isInvalid($warranty->getPlateNumber());
|
||||
if ($isValid)
|
||||
if ($warranty != null)
|
||||
{
|
||||
// get customer vehicle using plate number
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$cv = $em->getRepository(CustomerVehicle::class)->findOneBy(['plate_number' => $warranty->getPlateNumber()]);
|
||||
if ($cv != null)
|
||||
$isValid = InvalidPlateNumber::isInvalid($warranty->getPlateNumber());
|
||||
if ($isValid)
|
||||
{
|
||||
// get customer info
|
||||
// get mobile session of customer
|
||||
//error_log($cv->getCustomer()->getLastName() . ' ' . $cv->getCustomer()->getFirstName());
|
||||
$has_mobile = false;
|
||||
$mobile_date = '';
|
||||
$mobile_number = '';
|
||||
$mobile_session = $em->getRepository(MobileSession::class)
|
||||
->findOneBy(['customer' => $cv->getCustomer()->getID()], ['date_generated' => 'ASC']);
|
||||
if ($mobile_session != null)
|
||||
// get customer vehicles using plate number
|
||||
$customer_vehicles = $em->getRepository(CustomerVehicle::class)->findBy(['plate_number' => $warranty->getPlateNumber()]);
|
||||
|
||||
// check if customer vehicle is empty
|
||||
if (count($customer_vehicles) != 0)
|
||||
{
|
||||
// get mobile data
|
||||
$has_mobile = true;
|
||||
$mobile_date = $mobile_session->getDateGenerated()->format("d M Y");
|
||||
$mobile_number = $mobile_session->getPhoneNumber();
|
||||
$has_mobile = false;
|
||||
$mobile_date = '';
|
||||
$mobile_number = '';
|
||||
|
||||
// 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[] = [
|
||||
'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]),
|
||||
'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,
|
||||
];
|
||||
}
|
||||
}
|
||||
// set the warranty data in results
|
||||
$results[$key]['warr_lastname'] = $warranty->getLastName();
|
||||
$results[$key]['warr_firstname'] = $warranty->getFirstName();
|
||||
$results[$key]['warr_date_create'] = $warranty->getDateCreate()->format("d M Y");
|
||||
$results[$key]['warr_activation_status'] = ($warranty->isActivated() ? 'Active' : 'Inactive');
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue