Commit 1d5f79d2 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

FIX : Format de donnée monnétaire dans l'export Excel

parent 787cee32
Loading
Loading
Loading
Loading
Loading
+4 −3
Changes for module/Oscar/src/Oscar/Controller/ProjectGrantController.php: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -1008,6 +1008,8 @@ class ProjectGrantController extends AbstractOscarController implements UseNotif
        // Format
        $format = $this->params()->fromPost('format', 'csv');

        $delimiter = "\t";

        // Récupération des IDS
        if ($request->isPost()) {
            $paramID = $this->params()->fromPost('ids', '');
@@ -1017,15 +1019,14 @@ class ProjectGrantController extends AbstractOscarController implements UseNotif

        $datas = new ExportDatas($this->getProjectGrantService(), $this->getOscarUserContextService());
        $dt = $datas->output($paramID, $fields, $perimeter);

        $csv = uniqid('oscar_export_activities_') . '.csv';
        $csvPath = sprintf('/tmp/%s', $csv);
        $handler = fopen($csvPath, 'w');

        fputcsv($handler, $dt['headers']);
        fputcsv($handler, $dt['headers'], $delimiter);

        foreach ($dt['datas'] as $data) {
            fputcsv($handler, $data);
            fputcsv($handler, $data, $delimiter);
        }

        $downloader = new CSVDownloader();
+19 −11
Changes for module/Oscar/src/Oscar/Formatter/CSVDownloader.php: 19 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -9,10 +9,13 @@
namespace Oscar\Formatter;


use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class CSVDownloader
{

    public function downloadCSVToExcel($csvPath){
    public function downloadCSVToExcel($csvPath)
    {
        $xlsPath = $csvPath . '.xls';

        $doc = new \PHPExcel();
@@ -25,26 +28,29 @@ class CSVDownloader
        $row = 1;
        if (($handle = fopen($csvPath, "r")) !== FALSE) {
            $cell = 1;
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {

                $num = count($data);
                for ($c = 0; $c < $num; $c++) {
                    $value = $data[$c];
                    if(preg_match('/[0-9]*,[0-9]*/', $value)){
                       $sheet->setCellValueExplicitByColumnAndRow($c, $row, $value, \PHPExcel_Cell_DataType::TYPE_NUMERIC);
                    }

                    elseif (preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $value)) {
                    if (preg_match('/([0-9 ]*),([0-9]{2})/', $value)) {
                        //echo "Value reçue : $value\n";
                        // Il faut convertir en "vrai" nombre
                        $value = str_replace(',', '.', $value);
                        //echo "Convertie : $value\n";
                        $cell = $sheet->setCellValueExplicitByColumnAndRow($c, $row, $value, \PHPExcel_Cell_DataType::TYPE_NUMERIC, true);
                        $cell->getStyle()->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_CURRENCY_EUR);
                    } elseif (preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $value)) {
                        $sheet->setCellValueExplicitByColumnAndRow($c, $row, $value, \PHPExcel_Cell_DataType::TYPE_STRING);
                    }

                    else {
                    } else {
                        $sheet->setCellValueExplicitByColumnAndRow($c, $row, $value, \PHPExcel_Cell_DataType::TYPE_STRING);
                    }
                }
                $row++;
            }
            fclose($handle);
            //die();
        }
        $writer = \PHPExcel_IOFactory::createWriter($doc, 'Excel5');
        $writer->save($xlsPath);
@@ -56,12 +62,14 @@ class CSVDownloader
        return $xlsPath;
    }

    public function downloadCSV( $csvPath ){
    public function downloadCSV($csvPath)
    {
        $this->download($csvPath, 'csv', 'text/csv');
        @unlink($csvPath);
    }

    private function download($path, $extension, $mime){
    private function download($path, $extension, $mime)
    {
        header('Content-Disposition: attachment; filename=oscar-export.' . $extension);
        header('Content-Length: ' . filesize($path));
        header('Content-type: ' . $mime);
+2 −2

File changed.

Contains only whitespace changes.