Просмотр исходного кода

gestionale - modificata esportazione Excel

ferrari 3 месяцев назад
Родитель
Сommit
85f2306282
1 измененных файлов с 142 добавлено и 51 удалено
  1. 142 51
      app/Http/Livewire/RecordINOUT.php

+ 142 - 51
app/Http/Livewire/RecordINOUT.php

@@ -6,6 +6,8 @@ use Livewire\Component;
 
 use PhpOffice\PhpSpreadsheet\Spreadsheet;
 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
+use PhpOffice\PhpSpreadsheet\Cell\DataType;
 use Illuminate\Support\Facades\Log;
 use SimpleXMLElement;
 use Illuminate\Support\Facades\Auth;
@@ -56,7 +58,6 @@ class RecordINOUT extends Component
     public function boot()
     {
         app(TenantMiddleware::class)->setupTenantConnection();
-
     }
 
 
@@ -403,6 +404,7 @@ class RecordINOUT extends Component
     {
 
         $rows_in = array();
+        $rows_out = array();
 
         if ($this->filterCausalsIn != null && sizeof($this->filterCausalsIn) > 0) {
             foreach ($this->rows_in as $r) {
@@ -413,7 +415,18 @@ class RecordINOUT extends Component
         } else {
             $rows_in = $this->rows_in;
         }
-        $result = $this->generateExcel($this->columns, $rows_in, $this->records_in, $this->rows_out, $this->records_out, false);
+
+        if ($this->filterCausalsOut != null && sizeof($this->filterCausalsOut) > 0) {
+            foreach ($this->rows_out as $r) {
+                if (in_array($r["id"], $this->filterCausalsOut) || in_array($r["parent_id"], $this->filterCausalsOut) || in_array($r["first_parent_id"], $this->filterCausalsOut)) {
+                    $rows_out[] = $r;
+                }
+            }
+        } else {
+            $rows_out = $this->rows_out;
+        }
+
+        $result = $this->generateExcel($this->columns, $rows_in, $this->records_in, $rows_out, $this->records_out, false);
 
         if ($result['storage_type'] === 's3') {
             try {
@@ -439,12 +452,10 @@ class RecordINOUT extends Component
         $records_in = [];
         $records_out = [];
         $datas = [];
-        if (env('FISCAL_YEAR_MONTH_FROM', 1) > 1)
-        {
+        if (env('FISCAL_YEAR_MONTH_FROM', 1) > 1) {
             if (date("m") < env('FISCAL_YEAR_MONTH_FROM', 1))
                 $year -= 1;
-            for($m=env('FISCAL_YEAR_MONTH_FROM', 1);$m<=12;$m++)
-            {
+            for ($m = env('FISCAL_YEAR_MONTH_FROM', 1); $m <= 12; $m++) {
                 $datas[] = $m . "-" . $year;
             }
             for ($m = 1; $m <= env('FISCAL_YEAR_MONTH_TO', 12); $m++) {
@@ -533,100 +544,180 @@ class RecordINOUT extends Component
 
     public function generateExcel($columns, $rows_in, $records_in, $rows_out, $records_out, $isYearExport)
     {
-        $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
+        $writeNumber = function ($sheet, string $cell, $value, string $format = '#,##0.00') {
+            $sheet->setCellValueExplicit($cell, (float)$value, DataType::TYPE_NUMERIC);
+            $sheet->getStyle($cell)->getNumberFormat()->setFormatCode($format);
+        };
+
+        $shouldCountFn = function (array $row, array $presentParentIds): bool {
+            $level = $row['level'] ?? 0;
+            if ($level == 0) return true; // padre sempre incluso
+
+            $pid = $row['parent_id'] ?? null;
+            // se non c'è il padre includi il figlio
+            return !in_array($pid, $presentParentIds, true);
+        };
 
         $spreadsheet = new Spreadsheet();
         $activeWorksheet = $spreadsheet->getActiveSheet();
 
-        $activeWorksheet->setCellValue('A1', 'Entrate');
+        // Mappa colonne dinamica
+        $monthCount   = count($columns);
+        $nameColIdx   = 1;
+        $firstDataIdx = 2;
+        $lastDataIdx  = 1 + $monthCount;
+        $rowTotalIdx  = $lastDataIdx + 1;
+
+        $colA = Coordinate::stringFromColumnIndex($nameColIdx);
+        $colStart = Coordinate::stringFromColumnIndex($firstDataIdx);
+        $colEnd = Coordinate::stringFromColumnIndex($lastDataIdx);
+        $colRowTot = Coordinate::stringFromColumnIndex($rowTotalIdx);
+
+        // ========== ENTRATE ==========
+        // Header
+        $activeWorksheet->setCellValue($colA . '1', 'Entrate');
         foreach ($columns as $idx => $column) {
-            $activeWorksheet->setCellValue($letters[$idx + 1] . '1', $this->getMonth($column));
+            $col = Coordinate::stringFromColumnIndex($firstDataIdx + $idx);
+            $activeWorksheet->setCellValue($col . '1', $this->getMonth($column));
         }
+        $activeWorksheet->setCellValue($colRowTot . '1', 'Totale');
 
-        $activeWorksheet->getStyle('A1:N1')->getFont()->setBold(true);
-        $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
+        $activeWorksheet->getStyle($colA . '1:' . $colRowTot . '1')->getFont()->setBold(true);
+        $activeWorksheet->getStyle($colA . '1:' . $colRowTot . '1')
+            ->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
+            ->getStartColor()->setARGB('C6E0B4'); // verde chiaro
 
         $count = 2;
 
-        $totals = [];
+        $parentIdsIn = array_column(array_filter($rows_in, function ($r) {
+            return (int)($r['level'] ?? 0) === 0;
+        }), 'id');
+
+        $totalsIn = [];
 
         foreach ($rows_in as $in) {
-            $activeWorksheet->setCellValue('A' . $count, str_repeat("  ", $in["level"]) . $in["name"]);
+            $activeWorksheet->setCellValue($colA . $count, str_repeat("  ", (int)$in["level"]) . $in["name"]);
+
+            $rowSum = 0.0;
 
             foreach ($columns as $idx => $column) {
+                $col = Coordinate::stringFromColumnIndex($firstDataIdx + $idx);
+
                 if (isset($records_in[$column][$in["id"]])) {
-                    $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_in[$column][$in["id"]]));
-                    if ($in["level"] == 0) {
-                        if (isset($totals[$idx]))
-                            $totals[$idx] += $records_in[$column][$in["id"]];
-                        else
-                            $totals[$idx] = $records_in[$column][$in["id"]];
+                    $val = (float)$records_in[$column][$in["id"]];
+                    $writeNumber($activeWorksheet, $col . $count, $val);
+
+                    if ($shouldCountFn($in, $parentIdsIn)) {
+                        $totalsIn[$idx] = ($totalsIn[$idx] ?? 0) + $val;
                     }
+
+                    $rowSum += $val;
                 }
             }
 
-            if ($in["level"] == 0) {
-                $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
+            $writeNumber($activeWorksheet, $colRowTot . $count, $rowSum);
+
+            if ((int)$in["level"] === 0) {
+                $activeWorksheet->getStyle($colA . $count . ':' . $colRowTot . $count)->getFont()->setBold(true);
             }
 
-            $count += 1;
+            $count++;
         }
 
-        $activeWorksheet->setCellValue('A' . $count, 'Totale');
-        foreach ($totals as $idx => $total) {
-            $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
-            $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
-            $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('C6E0B4'); // Lighter green
+        // Riga "Totale" ENTRATE
+        $activeWorksheet->setCellValue($colA . $count, 'Totale');
+
+        $grandTotalIn = 0.0;
+        foreach ($totalsIn as $idx => $total) {
+            $col = Coordinate::stringFromColumnIndex($firstDataIdx + $idx);
+            $writeNumber($activeWorksheet, $col . $count, (float)$total);
+            $grandTotalIn += (float)$total;
         }
+        // Totale complessivo nella colonna "Totale"
+        $writeNumber($activeWorksheet, $colRowTot . $count, $grandTotalIn);
 
+        $activeWorksheet->getStyle($colA . $count . ':' . $colRowTot . $count)->getFont()->setBold(true);
+        $activeWorksheet->getStyle($colA . $count . ':' . $colRowTot . $count)
+            ->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
+            ->getStartColor()->setARGB('C6E0B4');
+
+        // ========== USCITE ==========
         $count += 2;
-        $activeWorksheet->setCellValue('A' . $count, "Uscite");
+
+        // Header
+        $activeWorksheet->setCellValue($colA . $count, 'Uscite');
         foreach ($columns as $idx => $column) {
-            $activeWorksheet->setCellValue($letters[$idx + 1] . $count, $this->getMonth($column));
+            $col = Coordinate::stringFromColumnIndex($firstDataIdx + $idx);
+            $activeWorksheet->setCellValue($col . $count, $this->getMonth($column));
         }
+        $activeWorksheet->setCellValue($colRowTot . '1', 'Totale'); // già impostato sopra, qui lasciamo l’header a riga corrente
+        $activeWorksheet->setCellValue($colRowTot . $count, 'Totale');
+
+        $activeWorksheet->getStyle($colA . $count . ':' . $colRowTot . $count)->getFont()->setBold(true);
+        $activeWorksheet->getStyle($colA . $count . ':' . $colRowTot . $count)
+            ->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
+            ->getStartColor()->setARGB('F8CBAD'); // rosso chiaro
 
-        $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
-        $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
+        $count++;
 
-        $count += 1;
+        $parentIdsOut = array_column(array_filter($rows_out, function ($r) {
+            return (int)($r['level'] ?? 0) === 0;
+        }), 'id');
 
-        $totals = [];
+        $totalsOut = [];
 
         foreach ($rows_out as $out) {
-            $activeWorksheet->setCellValue('A' . $count, str_repeat("  ", $out["level"]) . $out["name"]);
+            $activeWorksheet->setCellValue($colA . $count, str_repeat("  ", (int)$out["level"]) . $out["name"]);
+
+            $rowSum = 0.0;
 
             foreach ($columns as $idx => $column) {
+                $col = Coordinate::stringFromColumnIndex($firstDataIdx + $idx);
+
                 if (isset($records_out[$column][$out["id"]])) {
-                    $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($records_out[$column][$out["id"]]));
-                    if ($out["level"] == 0) {
-                        if (isset($totals[$idx]))
-                            $totals[$idx] += $records_out[$column][$out["id"]];
-                        else
-                            $totals[$idx] = $records_out[$column][$out["id"]];
+                    $val = (float)$records_out[$column][$out["id"]];
+                    $writeNumber($activeWorksheet, $col . $count, $val);
+
+                    if ($shouldCountFn($out, $parentIdsOut)) {
+                        $totalsOut[$idx] = ($totalsOut[$idx] ?? 0) + $val;
                     }
+
+                    $rowSum += $val;
                 }
             }
 
-            if ($out["level"] == 0) {
-                $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
+            $writeNumber($activeWorksheet, $colRowTot . $count, $rowSum);
+
+            if ((int)$out["level"] === 0) {
+                $activeWorksheet->getStyle($colA . $count . ':' . $colRowTot . $count)->getFont()->setBold(true);
             }
 
-            $count += 1;
+            $count++;
         }
 
-        $activeWorksheet->setCellValue('A' . $count, 'Totale');
-        foreach ($totals as $idx => $total) {
-            $activeWorksheet->setCellValue($letters[$idx + 1] . $count, formatPrice($total));
-            $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFont()->setBold(true);
-            $activeWorksheet->getStyle('A' . $count . ':N' . $count)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('F8CBAD'); // Lighter red
+        // Riga "Totale" USCITE
+        $activeWorksheet->setCellValue($colA . $count, 'Totale');
+
+        $grandTotalOut = 0.0;
+        foreach ($totalsOut as $idx => $total) {
+            $col = Coordinate::stringFromColumnIndex($firstDataIdx + $idx);
+            $writeNumber($activeWorksheet, $col . $count, (float)$total);
+            $grandTotalOut += (float)$total;
         }
+        $writeNumber($activeWorksheet, $colRowTot . $count, $grandTotalOut);
 
-        $activeWorksheet->getColumnDimension('A')->setWidth(35);
+        $activeWorksheet->getStyle($colA . $count . ':' . $colRowTot . $count)->getFont()->setBold(true);
+        $activeWorksheet->getStyle($colA . $count . ':' . $colRowTot . $count)
+            ->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
+            ->getStartColor()->setARGB('F8CBAD');
 
-        for ($i = 1; $i < count($letters); $i++) {
-            $activeWorksheet->getColumnDimension($letters[$i])->setWidth(20);
+        // ========== Larghezze colonne ==========
+        $activeWorksheet->getColumnDimension($colA)->setWidth(35);
+        for ($i = $firstDataIdx; $i <= $rowTotalIdx; $i++) {
+            $activeWorksheet->getColumnDimension(Coordinate::stringFromColumnIndex($i))->setWidth(20);
         }
 
+        // ========== Salvataggio ==========
         $fileSuffix = $isYearExport ? 'AnnoFiscale' : 'Selezione';
         $filename = date("Ymd") . '_Gestionale_' . $fileSuffix . '.xlsx';