Jelajahi Sumber

Colonna Stato Export prima nota

FabioFratini 8 bulan lalu
induk
melakukan
24f7b98a4c
1 mengubah file dengan 98 tambahan dan 134 penghapusan
  1. 98 134
      app/Http/Livewire/Record.php

+ 98 - 134
app/Http/Livewire/Record.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace App\Http\Livewire;
+
 use Livewire\Component;
 use DateInterval;
 use DatePeriod;
@@ -47,16 +48,14 @@ class Record extends Component
 
         $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
 
-        $this->payments = \App\Models\PaymentMethod::select('id', 'name','type')->where('enabled', true)->where('money', false)->get();
-
+        $this->payments = \App\Models\PaymentMethod::select('id', 'name', 'type')->where('enabled', true)->where('money', false)->get();
     }
 
     public function getCausals($records, $indentation)
     {
-        foreach($records as $record)
-        {
+        foreach ($records as $record) {
             $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
-            if(count($record->childs))
+            if (count($record->childs))
                 $this->getCausals($record->childs, $indentation + 1);
         }
     }
@@ -119,68 +118,57 @@ class Record extends Component
 
         $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
         $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
-                    ->select('records.*', 'records_rows.*') // Ensure all columns are selected
-                    ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
-                    ->whereBetween('date', [$this->fromDate, $this->toDate])
-                    ->where(function ($query)  {
-                        $query->where('type', 'OUT')
-                        ->orWhere(function ($query)  {
-                            $query->where('records.corrispettivo_fiscale', true)
-                                  ->orWhere('records.commercial', false);
-                        });
-                    })
-                    ->where(function ($query) use ($exclude_from_records) {
-                        $query->where('type', 'OUT')
-                              ->orWhere(function ($subquery) use ($exclude_from_records) {
-                                  $subquery->whereNotIn('member_id', $exclude_from_records);
-                              });
+            ->select('records.*', 'records_rows.*') // Ensure all columns are selected
+            ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
+            ->whereBetween('date', [$this->fromDate, $this->toDate])
+            ->where(function ($query) {
+                $query->where('type', 'OUT')
+                    ->orWhere(function ($query) {
+                        $query->where('records.corrispettivo_fiscale', true)
+                            ->orWhere('records.commercial', false);
                     });
-                    if ($this->filterCausals != null && sizeof($this->filterCausals) > 0)
-                    {
-                        $causals = array();
-                        foreach($this->filterCausals as $z)
-                        {
-                            $causals[] = $z;
-                            $childs = \App\Models\Causal::where('parent_id', $z)->get();
-                            foreach($childs as $c)
-                            {
-                                $causals[] = $c->id;
-                                $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
-                                foreach($childsX as $cX)
-                                {
-                                    $causals[] = $cX->id;
-
-                                }
-                            }
-                        }
-                        $datas->whereIn('causal_id', $causals);
-                    }
-                    if ($this->filterMember != null && $this->filterMember > 0)
-                    {
-                        $datas->where('member_id', $this->filterMember);
+            })
+            ->where(function ($query) use ($exclude_from_records) {
+                $query->where('type', 'OUT')
+                    ->orWhere(function ($subquery) use ($exclude_from_records) {
+                        $subquery->whereNotIn('member_id', $exclude_from_records);
+                    });
+            });
+        if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
+            $causals = array();
+            foreach ($this->filterCausals as $z) {
+                $causals[] = $z;
+                $childs = \App\Models\Causal::where('parent_id', $z)->get();
+                foreach ($childs as $c) {
+                    $causals[] = $c->id;
+                    $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
+                    foreach ($childsX as $cX) {
+                        $causals[] = $cX->id;
                     }
-                    $datas = $datas->orderBy('date', 'ASC')
-                    ->orderBy('records.created_at', 'ASC')
-                    ->orderBy('records_rows.id', 'ASC') // Important to maintain row order
-                    ->get();
+                }
+            }
+            $datas->whereIn('causal_id', $causals);
+        }
+        if ($this->filterMember != null && $this->filterMember > 0) {
+            $datas->where('member_id', $this->filterMember);
+        }
+        $datas = $datas->orderBy('date', 'ASC')
+            ->orderBy('records.created_at', 'ASC')
+            ->orderBy('records_rows.id', 'ASC') // Important to maintain row order
+            ->get();
 
-        foreach($datas as $idx => $data)
-        {
+        foreach ($datas as $idx => $data) {
 
             $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
 
             $paymentCheck = $data->payment_method->money;
 
-            if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first))
-            {
+            if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
 
-                if (!$data->deleted)
-                {
+                if (!$data->deleted) {
                     $amount = $data->amount;
                     $amount += getVatValue($amount, $data->vat_id);
-                }
-                else
-                {
+                } else {
                     $amount = $data->amount;
                 }
 
@@ -191,9 +179,9 @@ class Record extends Component
 
                 // aggiungere il nome * * *
                 $causal = $prefix . $data->date . "§" . $causalCheck->getTree() . "§" .
-                ($data->type == "IN" ? ($data->member ? ($data->member->last_name . " " . $data->member->first_name) : "") :
-                $data->supplier->name ?? "") . "§" . $data->note . "§" . ($data->deleted ? 'DELETED' : '') .
-                "§" . $data->numero_linea;
+                    ($data->type == "IN" ? ($data->member ? ($data->member->last_name . " " . $data->member->first_name) : "") :
+                        $data->supplier->name ?? "") . "§" . $data->note . "§" . ($data->deleted ? 'DELETED' : '') .
+                    "§" . $data->numero_linea;
 
                 if (!isset($this->records[$causal][$data->payment_method->name][$data->type])) {
                     $this->records[$causal][$data->payment_method->name][$data->type] = 0;
@@ -210,11 +198,9 @@ class Record extends Component
 
                 // Update totals if not deleted
                 if (!$data->deleted)
-                    $this->totals[$data->payment_method->name][$data->type] += $amount;// $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
+                    $this->totals[$data->payment_method->name][$data->type] += $amount; // $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
 
             }
-
-
         }
 
         return view('livewire.records');
@@ -228,8 +214,7 @@ class Record extends Component
 
         $interval = DateInterval::createFromDateString('1 day');
         $date_range = new DatePeriod($begin, $interval, $end);
-        foreach ($date_range as $date)
-        {
+        foreach ($date_range as $date) {
             $labels[] = $date->format('d/M');
         }
 
@@ -245,15 +230,11 @@ class Record extends Component
         $interval = DateInterval::createFromDateString('1 day');
         $date_range = new DatePeriod($begin, $interval, $end);
 
-        foreach ($date_range as $date)
-        {
-            if ($type == 'IN')
-            {
+        foreach ($date_range as $date) {
+            if ($type == 'IN') {
                 $found = false;
-                foreach($this->in as $in)
-                {
-                    if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d'))
-                    {
+                foreach ($this->in as $in) {
+                    if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d')) {
                         $data[] = number_format($in->total, 0, "", "");
                         $found = true;
                     }
@@ -261,13 +242,10 @@ class Record extends Component
                 if (!$found)
                     $data[] = 0;
             }
-            if ($type == 'OUT')
-            {
+            if ($type == 'OUT') {
                 $found = false;
-                foreach($this->out as $out)
-                {
-                    if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d'))
-                    {
+                foreach ($this->out as $out) {
+                    if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d')) {
                         $data[] = number_format($out->total, 0, "", "");
                         $found = true;
                     }
@@ -279,10 +257,9 @@ class Record extends Component
 
         return $data;
     }
-
     public function export()
     {
-        $letters = array('E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
+        $letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
 
         $spreadsheet = new Spreadsheet();
         $activeWorksheet = $spreadsheet->getActiveSheet();
@@ -292,9 +269,10 @@ class Record extends Component
         $activeWorksheet->setCellValue('B1', "Causale");
         $activeWorksheet->setCellValue('C1', "Dettaglio");
         $activeWorksheet->setCellValue('D1', "Nominativo");
+        $activeWorksheet->setCellValue('E1', "Stato");
+
         $idx = 0;
-        foreach($this->payments as $p)
-        {
+        foreach ($this->payments as $p) {
             if ($idx >= count($letters)) {
                 break;
             }
@@ -312,9 +290,10 @@ class Record extends Component
         $activeWorksheet->setCellValue('B2', "");
         $activeWorksheet->setCellValue('C2', "");
         $activeWorksheet->setCellValue('D2', "");
-        foreach($this->payments as $p)
-        {
-            if($p->type == 'ALL'){
+        $activeWorksheet->setCellValue('E2', "");
+
+        foreach ($this->payments as $p) {
+            if ($p->type == 'ALL') {
                 if ($idx >= count($letters)) {
                     break;
                 }
@@ -323,8 +302,7 @@ class Record extends Component
                 $idx++;
                 $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
                 $idx++;
-            }
-            elseif($p->type == 'IN'){
+            } elseif ($p->type == 'IN') {
                 if ($idx >= count($letters)) {
                     break;
                 }
@@ -333,8 +311,7 @@ class Record extends Component
                 $idx++;
                 $activeWorksheet->setCellValue($letters[$idx] . '2', "");
                 $idx++;
-            }
-            elseif($p->type == 'OUT'){
+            } elseif ($p->type == 'OUT') {
                 if ($idx >= count($letters)) {
                     break;
                 }
@@ -346,53 +323,48 @@ class Record extends Component
             }
         }
 
-        $activeWorksheet->getStyle('A1:P1')->getFont()->setBold(true);
-        $activeWorksheet->getStyle('A2:P2')->getFont()->setBold(true);
+        $activeWorksheet->getStyle('A1:Q1')->getFont()->setBold(true);
+        $activeWorksheet->getStyle('A2:Q2')->getFont()->setBold(true);
         // $activeWorksheet->getStyle('A1:N1')->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB('00ff00');
 
         $count = 3;
 
         $totals = [];
 
-        foreach($this->records as $causal => $record)
-        {
+        foreach ($this->records as $causal => $record) {
 
             $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
-            list($d, $c, $j, $k) = explode("§", $check);
+            list($d, $c, $j, $k, $deleted, $numeroLinea) = explode("§", $check);
 
             $activeWorksheet->setCellValue('A' . $count, date("d/m/Y", strtotime($d)));
             $activeWorksheet->setCellValue('B' . $count, $c);
             $activeWorksheet->setCellValue('C' . $count, $k);
             $activeWorksheet->setCellValue('D' . $count, $j);
 
+            $stato = ($deleted === 'DELETED') ? 'ANNULLATA' : '';
+            $activeWorksheet->setCellValue('E' . $count, $stato);
+
+            if ($stato === 'ANNULLATA') {
+                $activeWorksheet->getStyle('E' . $count)->getFont()->getColor()->setARGB('FFFF0000');
+            }
+
             $idx = 0;
-            foreach($this->payments as $p)
-            {
-                if(isset($record[$p->name]))
-                {
+            foreach ($this->payments as $p) {
+                if (isset($record[$p->name])) {
 
-                    if(isset($record[$p->name]["IN"]))
-                    {
+                    if (isset($record[$p->name]["IN"])) {
                         $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["IN"]));
-                    }
-                    else
-                    {
+                    } else {
                         $activeWorksheet->setCellValue($letters[$idx] . $count, "");
                     }
                     $idx++;
-                    if(isset($record[$p->name]["OUT"]))
-                    {
+                    if (isset($record[$p->name]["OUT"])) {
                         $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($record[$p->name]["OUT"]));
-                    }
-                    else
-                    {
+                    } else {
                         $activeWorksheet->setCellValue($letters[$idx] . $count, "");
                     }
                     $idx++;
-
-                }
-                else
-                {
+                } else {
                     if ($idx >= count($letters)) {
                         break;
                     }
@@ -405,7 +377,6 @@ class Record extends Component
             }
 
             $count += 1;
-
         }
 
         $count += 1;
@@ -416,32 +387,28 @@ class Record extends Component
         $activeWorksheet->setCellValue('B' . $count, '');
         $activeWorksheet->setCellValue('C' . $count, '');
         $activeWorksheet->setCellValue('D' . $count, '');
-        foreach($this->payments as $p)
-        {
-            if(isset($this->totals[$p->name]))
-            {
-                if($p->type == 'ALL'){
+        $activeWorksheet->setCellValue('E' . $count, '');
+
+        foreach ($this->payments as $p) {
+            if (isset($this->totals[$p->name])) {
+                if ($p->type == 'ALL') {
                     $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
                     $idx++;
                     $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
                     $idx++;
-                }
-                elseif($p->type == 'IN'){
+                } elseif ($p->type == 'IN') {
                     $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"]));
                     $idx++;
                     $activeWorksheet->setCellValue($letters[$idx] . $count, "");
                     $idx++;
-                }
-                elseif($p->type == 'OUT'){
+                } elseif ($p->type == 'OUT') {
                     $activeWorksheet->setCellValue($letters[$idx] . $count, "");
                     $idx++;
                     $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"]));
                     $idx++;
                 }
-            }
-            else
-            {
-                if($p->type == 'ALL'){
+            } else {
+                if ($p->type == 'ALL') {
                     if ($idx >= count($letters)) {
                         break;
                     }
@@ -450,8 +417,7 @@ class Record extends Component
                     $idx++;
                     $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
                     $idx++;
-                }
-                elseif($p->type == 'IN'){
+                } elseif ($p->type == 'IN') {
                     if ($idx >= count($letters)) {
                         break;
                     }
@@ -460,8 +426,7 @@ class Record extends Component
                     $idx++;
                     $activeWorksheet->setCellValue($letters[$idx] . $count, "");
                     $idx++;
-                }
-                elseif($p->type == 'OUT'){
+                } elseif ($p->type == 'OUT') {
                     if ($idx >= count($letters)) {
                         break;
                     }
@@ -474,20 +439,19 @@ class Record extends Component
             }
         }
 
-        $activeWorksheet->getStyle('A' . $count . ':P' . $count)->getFont()->setBold(true);
+        $activeWorksheet->getStyle('A' . $count . ':Q' . $count)->getFont()->setBold(true);
 
         $activeWorksheet->getColumnDimension('A')->setWidth(20);
         $activeWorksheet->getColumnDimension('B')->setWidth(40);
         $activeWorksheet->getColumnDimension('C')->setWidth(40);
         $activeWorksheet->getColumnDimension('D')->setWidth(40);
-        foreach($letters as $l)
+        $activeWorksheet->getColumnDimension('E')->setWidth(20);
+        foreach ($letters as $l)
             $activeWorksheet->getColumnDimension($l)->setWidth(20);
 
         $writer = new Xlsx($spreadsheet);
         $writer->save($path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx'));
 
         return response()->download($path)->deleteFileAfterSend();
-
     }
-
 }