From 479ff96fdc86fb01a7142cde3f9f326b35ef1248 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 28 Aug 2020 09:36:11 +0000 Subject: [PATCH] Modified contents of the csv file when no invalid warranties are found. #476 --- src/Controller/WarrantyController.php | 77 ++++++++++++++++----------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/src/Controller/WarrantyController.php b/src/Controller/WarrantyController.php index ae6da43f..b1f2442e 100644 --- a/src/Controller/WarrantyController.php +++ b/src/Controller/WarrantyController.php @@ -386,43 +386,58 @@ class WarrantyController extends Controller $inv_entries = $this->processWarrantyFile($file, $em, $wh); $resp = new StreamedResponse(); - $resp->setCallback(function() use($inv_entries) { - // csv output - $csv_handle = fopen('php://output', 'w+'); - fputcsv($csv_handle, [ - 'Owner First Name', - 'Owner Last Name', - 'Owner Email', - 'Owner Address', - 'Owner Mobile', - 'Owner Telephone', - 'Vehicle Make', - 'Vehicle Model', - 'Vehicle Year', - 'Vehicle Plate Number', - 'Battery Serial Number', - 'Battery Sales Invoice', - 'Battery Date of Purchase', - 'Distributor Name', - 'Distributor Address', - 'Application Type ID', - 'Battery ID', - 'Ownership Type', - 'Reason Warranty Not Added', - ]); - foreach ($inv_entries as $row) - { - fputcsv($csv_handle, $row); - } - fclose($csv_handle); - }); + if (count($inv_entries) > 0) + { + $resp->setCallback(function() use($inv_entries) { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, [ + 'Owner First Name', + 'Owner Last Name', + 'Owner Email', + 'Owner Address', + 'Owner Mobile', + 'Owner Telephone', + 'Vehicle Make', + 'Vehicle Model', + 'Vehicle Year', + 'Vehicle Plate Number', + 'Battery Serial Number', + 'Battery Sales Invoice', + 'Battery Date of Purchase', + 'Distributor Name', + 'Distributor Address', + 'Application Type ID', + 'Battery ID', + 'Ownership Type', + 'Reason Warranty Not Added', + ]); + foreach ($inv_entries as $row) + { + fputcsv($csv_handle, $row); + } + + fclose($csv_handle); + }); + + } + else + { + $resp->setCallback(function() { + // csv output + $csv_handle = fopen('php://output', 'w+'); + fputcsv($csv_handle, ['No Invalid Warranties']); + + fclose($csv_handle); + }); + } $filename = 'invalid_warranties' . '.csv'; - $resp->setStatusCode(200); $resp->headers->set('Content-Type', 'text/csv; charset=utf-8'); $resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); + $resp->setStatusCode(200); return $resp; }