FabioFratini 8 месяцев назад
Родитель
Сommit
02b7673eb1
1 измененных файлов с 64 добавлено и 86 удалено
  1. 64 86
      app/Http/Livewire/Record.php

+ 64 - 86
app/Http/Livewire/Record.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace App\Http\Livewire;
+
 use Livewire\Component;
 use DateInterval;
 use DatePeriod;
@@ -52,16 +53,14 @@ class Record extends Component
         Log::info($this->suppliers);
         $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);
         }
     }
@@ -138,68 +137,56 @@ class Record extends Component
         }
         */
         $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
-                    ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
-                    //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
-                    ->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)  {
+            ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
+            //->where('records_rows.when', 'like', '%' . date("m-Y") . '%')
+            ->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)  {
                         $query->where('deleted', false)->orWhere('deleted', null);
                     })*/
-                    ->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->whereIn('causal_id', $causals);
+            ->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;
                     }
-                    if ($this->filterMember != null && $this->filterMember > 0)
-                    {
-                        $datas->where('member_id', $this->filterMember);
-                    }
-                    if ($this->filterSupplier != null && $this->filterSupplier > 0)
-                    {
-                        $datas->where('supplier_id', $this->filterSupplier);
-                    }
-                    $datas = $datas->orderBy('date', 'ASC')->orderBy('records.created_at', 'ASC')
-                    ->get();
+                }
+            }
+            $datas->whereIn('causal_id', $causals);
+        }
+        if ($this->filterMember != null && $this->filterMember > 0) {
+            $datas->where('member_id', $this->filterMember);
+        }
+        if ($this->filterSupplier != null && $this->filterSupplier > 0) {
+            $datas->where('supplier_id', $this->filterSupplier);
+        }
+        $datas = $datas->orderBy('date', 'ASC')->orderBy('records.created_at', 'ASC')
+            ->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;
                 }
                 /*$when = sizeof(json_decode($data->when));
@@ -218,21 +205,16 @@ class Record extends Component
                 //$causal = $prefix . $data->date . "§" . $causalCheck->getTree();
                 $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' : '');
 
-                if (isset($this->records[$causal]))
-                {
-                    if (isset($this->records[$causal][$data->payment_method->name]))
-                    {
-                        if ($data->commercial)
-                        {
+                if (isset($this->records[$causal])) {
+                    if (isset($this->records[$causal][$data->payment_method->name])) {
+                        if ($data->commercial) {
                             if ($data->deleted && $this->records[$causal][$data->payment_method->name][$data->type])
                                 $amount += $this->records[$causal][$data->payment_method->name][$data->type];
                         }
                     }
-
                 }
 
-                if (!isset($this->totals[$data->payment_method->name]))
-                {
+                if (!isset($this->totals[$data->payment_method->name])) {
                     $this->totals[$data->payment_method->name]["IN"] = 0;
                     $this->totals[$data->payment_method->name]["OUT"] = 0;
                 }
@@ -243,8 +225,6 @@ class Record extends Component
                     $this->totals[$data->payment_method->name][$data->type] += $amount; // $data->amount;//$this->records[$causal][$data->payment_method->name][$data->type];
 
             }
-
-
         }
 
         return view('livewire.records');
@@ -258,8 +238,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');
         }
 
@@ -275,15 +254,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;
                     }
@@ -291,13 +266,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;
                     }
@@ -310,7 +282,7 @@ class Record extends Component
         return $data;
     }
 
-   public function export()
+    public function export()
     {
         $letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
 
@@ -387,9 +359,16 @@ class Record extends Component
         foreach ($this->records as $causal => $record) {
 
             $check = strpos($causal, "$") ? explode("$", $causal)[1] : $causal;
-            list($d, $c, $j, $k, $deleted, $numeroLinea) = explode("§", $check);
 
-            $activeWorksheet->setCellValue('A' . $count, date("d/m/Y", strtotime($d)));
+            $parts = explode("§", $check);
+            $d = $parts[0] ?? '';
+            $c = $parts[1] ?? '';
+            $j = $parts[2] ?? '';
+            $k = $parts[3] ?? '';
+            $deleted = $parts[4] ?? '';
+            $numeroLinea = $parts[5] ?? '';
+
+            $activeWorksheet->setCellValue('A' . $count, !empty($d) ? date("d/m/Y", strtotime($d)) : '');
             $activeWorksheet->setCellValue('B' . $count, $c);
             $activeWorksheet->setCellValue('C' . $count, $k);
             $activeWorksheet->setCellValue('D' . $count, $j);
@@ -507,5 +486,4 @@ class Record extends Component
 
         return response()->download($path)->deleteFileAfterSend();
     }
-
 }