Merge branch '249-add-export-to-csv-button-for-popapp-report' into 'master'

Resolve "Add export to csv button for popapp report"

Closes #249

See merge request jankstudio/resq!294
This commit is contained in:
Kendrick Chan 2019-08-16 02:27:09 +00:00
commit 4d0c0345fb
3 changed files with 54 additions and 10 deletions

View file

@ -33,7 +33,7 @@ rep_popapp_comp_form:
controller: App\Controller\ReportController::popappComparisonForm controller: App\Controller\ReportController::popappComparisonForm
methods: [GET] methods: [GET]
rep_popapp_comp_submit: rep_popapp_export_csv:
path: /report/popapp_comparison path: /report/popapp_export
controller: App\Controller\ReportController::popappComparisonSubmit controller: App\Controller\ReportController::popappExportCSV
methods: [POST] methods: [POST]

View file

@ -420,7 +420,7 @@ class ReportController extends Controller
/** /**
* @Menu(selected="outlet_list") * @Menu(selected="outlet_list")
*/ */
public function popappComparisonSubmit(Request $req) /* public function popappComparisonSubmit(Request $req)
{ {
// retrieve temporary info for file // retrieve temporary info for file
$file = $req->files->get('csv_file'); $file = $req->files->get('csv_file');
@ -437,6 +437,49 @@ class ReportController extends Controller
$params['data'] = $data; $params['data'] = $data;
return $this->render('report/popapp/form.html.twig', $params); return $this->render('report/popapp/form.html.twig', $params);
} */
/**
* @Menu(selected="outlet_list")
*/
public function popappExportCSV(Request $req)
{
// retrieve temporary info for file
$file = $req->files->get('csv_file');
// process the csv file
$data = $this->processPopappFile($file);
$resp = new StreamedResponse();
$resp->setCallback(function() use ($data) {
// csv output
$csv_handle = fopen('php://output', 'w+');
fputcsv($csv_handle, [
'Customer ID',
'Last Name',
'First Name',
'Plate Number',
'Serial',
'Warranty Create Date',
'Activation Status',
'Has Mobile App?',
]);
foreach ($data as $row)
{
fputcsv($csv_handle, $row);
}
fclose($csv_handle);
});
$filename = 'popapp_comparison_report' . '.csv';
$resp->setStatusCode(200);
$resp->headers->set('Content-Type', 'text/csv; charset=utf-8');
$resp->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
return $resp;
} }
protected function processPopappFile(UploadedFile $csv_file) protected function processPopappFile(UploadedFile $csv_file)
@ -507,8 +550,8 @@ class ReportController extends Controller
'plate_num' => $cv->getPlateNumber(), 'plate_num' => $cv->getPlateNumber(),
'serial' => $warranty->getSerial(), 'serial' => $warranty->getSerial(),
'warr_date_create' => $warranty->getDateCreate()->format("d M Y"), 'warr_date_create' => $warranty->getDateCreate()->format("d M Y"),
'has_mobile' => $has_mobile, 'warr_activation_status' => ($warranty->isActivated() ? 'Active' : 'Inactive'),
'warr_activation_status' => $warranty->isActivated(), 'has_mobile' => ($has_mobile ? 'Yes' : 'No'),
]; ];
} }
@ -517,4 +560,5 @@ class ReportController extends Controller
return $results; return $results;
} }
} }

View file

@ -29,7 +29,7 @@
</div> </div>
</div> </div>
</div> </div>
<form id="upload_form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ url('rep_popapp_comp_submit') }}" enctype="multipart/form-data"> <form id="upload_form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ url('rep_popapp_export_csv') }}" enctype="multipart/form-data">
<div class="m-portlet__body"> <div class="m-portlet__body">
<div class="form-group m-form__group row"> <div class="form-group m-form__group row">
<input type="file" id="csv_file" name="csv_file" > <input type="file" id="csv_file" name="csv_file" >
@ -38,7 +38,7 @@
<div class="m-form__actions m-form__actions--solid m-form__actions--right"> <div class="m-form__actions m-form__actions--solid m-form__actions--right">
<div class="row"> <div class="row">
<div class="col-lg-12"> <div class="col-lg-12">
<button type="submit" class="btn btn-success">Submit</button> <button type="submit" class="btn btn-success">Export to CSV</button>
</div> </div>
</div> </div>
</div> </div>
@ -96,8 +96,8 @@
<td>{{ result.plate_num|default('') }}</td> <td>{{ result.plate_num|default('') }}</td>
<td>{{ result.serial|default('') }}</td> <td>{{ result.serial|default('') }}</td>
<td>{{ result.warr_date_create|default('') }}</td> <td>{{ result.warr_date_create|default('') }}</td>
<td>{{ result.warr_activation_status ? 'Active' : 'Inactive' }}</td> <td>{{ result.warr_activation_status }}</td>
<td>{{ result.has_mobile ? 'Yes' : 'No' }} </td> <td>{{ result.has_mobile }} </td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>