Przeglądaj źródła

payment_methods - aggiunto origine/destinazione + modifica corrispettivo fiscale entrate

ferrari 1 miesiąc temu
rodzic
commit
4639ced73c

+ 37 - 24
app/Http/Livewire/PaymentMethod.php

@@ -1,17 +1,20 @@
 <?php
 
 namespace App\Http\Livewire;
+
 use Livewire\Component;
 use Illuminate\Support\Facades\Auth;
 use App\Http\Middleware\TenantMiddleware;
 
 class PaymentMethod extends Component
 {
-    public $records, $name, $enabled, $money, $type, $corrispettivo_fiscale, $dataId, $bank_id, $update = false, $add = false;
+    public $records, $name, $enabled, $money, $type, $corrispettivo_fiscale, $dataId, $origin_id, $destination_id, $update = false, $add = false;
     public $paymentMethods = [];
-    public $banks = array();
     public $showHidden = false;
 
+    public $origins = [];
+    public $destinations = [];
+
     protected $rules = [
         'name' => 'required'
     ];
@@ -20,7 +23,7 @@ class PaymentMethod extends Component
         'name.required' => 'Il nome è obbligatorio'
     ];
 
-    public $sortField ='name';
+    public $sortField = 'name';
     public $sortAsc = true;
 
     public function boot()
@@ -30,7 +33,7 @@ class PaymentMethod extends Component
 
     public function sortBy($field)
     {
-        if($this->sortField === $field) {
+        if ($this->sortField === $field) {
             $this->sortAsc = ! $this->sortAsc;
         } else {
             $this->sortAsc = true;
@@ -39,7 +42,8 @@ class PaymentMethod extends Component
         $this->sortField = $field;
     }
 
-    public function resetFields(){
+    public function resetFields()
+    {
         $this->name = '';
         $this->money = false;
         $this->type = 'ALL';
@@ -48,11 +52,13 @@ class PaymentMethod extends Component
         $this->emit('load-data-table');
     }
 
-    public function mount(){
-        if(Auth::user()->level != env('LEVEL_ADMIN', 0))
+    public function mount()
+    {
+        if (Auth::user()->level != env('LEVEL_ADMIN', 0))
             return redirect()->to('/dashboard');
 
-        $this->banks = \App\Models\Bank::select('id', 'name')->get();
+        $this->origins = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', ['OUT', 'ALL'])->get();
+        $this->destinations = \App\Models\Bank::select('id', 'name')->where('enabled', true)->whereIn('visibility', ['IN', 'ALL'])->get();
 
         $this->loadPaymentMethodOptions();
     }
@@ -61,7 +67,7 @@ class PaymentMethod extends Component
     {
         try {
             \App\Models\PaymentMethod::whereId($id)->update(['hidden' => true]);
-            session()->flash('success', 'Metodo pagamento nascosto');
+            session()->flash('success', 'Metodo di pagamento nascosto');
         } catch (\Exception $ex) {
             session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
@@ -71,7 +77,7 @@ class PaymentMethod extends Component
     {
         try {
             \App\Models\PaymentMethod::whereId($id)->update(['hidden' => false]);
-            session()->flash('success', 'Metodo pagamento ripristinato');
+            session()->flash('success', 'Metodo di pagamento ripristinato');
         } catch (\Exception $ex) {
             session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
@@ -92,9 +98,12 @@ class PaymentMethod extends Component
             })->get();
         }
 
-        foreach($this->records as $r) {
-            $r->bank = $r->bank ? $r->bank->name : '';
-        }
+        // foreach ($this->records as $r) {
+
+        //     dd($r, $r->origin);
+        //     $r->origin = $r->origin ? $r->origin->name : '';
+        //     $r->destination = $r->destination ? $r->destination->name : '';
+        // }
 
         return view('livewire.payment_method');
     }
@@ -112,38 +121,41 @@ class PaymentMethod extends Component
         try {
             \App\Models\PaymentMethod::create([
                 'name' => $this->name,
-                'bank_id' => $this->bank_id,
+                'origin_id' => $this->origin_id != "" ? $this->origin_id : null,
+                'destination_id' => $this->destination_id != "" ? $this->destination_id : null,
                 'money' => $this->money,
                 'type' => $this->type,
                 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
                 'enabled' => $this->enabled
             ]);
-            session()->flash('success','Metodo pagamento creato');
+            session()->flash('success', 'Metodo di pagamento creato');
             $this->resetFields();
             $this->add = false;
         } catch (\Exception $ex) {
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
     }
 
-    public function edit($id){
+    public function edit($id)
+    {
         try {
             $payment_method = \App\Models\PaymentMethod::findOrFail($id);
-            if( !$payment_method) {
-                session()->flash('error','Metodo pagamento non trovato');
+            if (!$payment_method) {
+                session()->flash('error', 'Metodo di pagamento non trovato');
             } else {
                 $this->name = $payment_method->name;
                 $this->enabled = $payment_method->enabled;
                 $this->corrispettivo_fiscale = $payment_method->corrispettivo_fiscale;
                 $this->money = $payment_method->money;
                 $this->type = $payment_method->type;
-                $this->bank_id = $payment_method->bank_id;
+                $this->origin_id = $payment_method->origin_id;
+                $this->destination_id = $payment_method->destination_id;
                 $this->dataId = $payment_method->id;
                 $this->update = true;
                 $this->add = false;
             }
         } catch (\Exception $ex) {
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
     }
 
@@ -153,17 +165,18 @@ class PaymentMethod extends Component
         try {
             \App\Models\PaymentMethod::whereId($this->dataId)->update([
                 'name' => $this->name,
-                'bank_id' => $this->bank_id,
+                'origin_id' => $this->origin_id != "" ? $this->origin_id : null,
+                'destination_id' => $this->destination_id != "" ? $this->destination_id : null,
                 'money' => $this->money,
                 'type' => $this->type,
                 'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
                 'enabled' => $this->enabled
             ]);
-            session()->flash('success','Metodo pagamento aggiornato');
+            session()->flash('success', 'Metodo di pagamento aggiornato');
             $this->resetFields();
             $this->update = false;
         } catch (\Exception $ex) {
-            session()->flash('error','Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
         }
     }
 

+ 33 - 3
app/Http/Livewire/RecordIN.php

@@ -75,10 +75,11 @@ class RecordIN extends Component
         $commercial, $update = false, $add = false;
 
     public $corrispettivo = [];
+    public $corrispettivo_destination = [];
 
     public $currentReceip;
 
-    public $filterMember = 0, $filterPaymentMethod = 0, $filterCausals = 0, $filterCourses = 0, $filterFrom = '', $filterTo = '', $filterCommercial = 0;
+    public $filterMember = 0, $filterPaymentMethod = 0, $filterCausals = 0, $filterCourses = 0, $filterSubscriptions = 0, $filterFrom = '', $filterTo = '', $filterCommercial = 0;
 
     public $hasFilter = false;
 
@@ -95,6 +96,7 @@ class RecordIN extends Component
     public $refreshAfter = 0;
     public $sconto = 0;
     public $canSave = true;
+
     public $newMemberFirstName = '';
     public $newMemberLastName = '';
     public $newMemberEmail = '';
@@ -189,9 +191,26 @@ class RecordIN extends Component
     public function updatedPaymentMethodId()
     {
         //$this->emit('refresh');
+
+        if ($this->payment_method_id) {
+            $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
+            $this->destination_id = $payment_method->destination_id ?? null;
+        }
+
         $this->canSave = $this->checkCanSave();
     }
 
+    public function updatedCorrispettivoFiscale()
+    {
+        if ($this->corrispettivo_fiscale) {
+            foreach ($this->payments as $payment) {
+                if ($payment->corrispettivo_fiscale) {
+                    $this->corrispettivo_destination[$payment->id] = $payment->destination_id;
+                }
+            }
+        }
+    }
+
     public function updatedAmount()
     {
         // $this->emit('refresh');
@@ -284,6 +303,7 @@ class RecordIN extends Component
         $this->rows = array();
         $this->rows[] = array('causal_id' => isset($_GET["causalId"]) ? $_GET["causalId"] : null, 'course_id' => null, 'course' => null, 'subscription_id' => null, 'subscription' => null, 'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')),  'amount' => null, 'vat_id' => null, 'note' => '', 'commercial' => 0, 'sconto' => 0);
         $this->corrispettivo = [];
+        $this->corrispettivo_destination = [];
         $this->emit('load-data-table');
     }
 
@@ -323,6 +343,15 @@ class RecordIN extends Component
         return $ret;
     }
 
+    public function getBankName($bank_id)
+    {
+        $ret = '';
+        if ($bank_id > 0) {
+            $ret = \App\Models\Bank::findOrFail($bank_id)->name;
+        }
+        return $ret;
+    }
+
     function buildTree($records, $parentId = 0)
     {
         $this->causals = array();
@@ -623,6 +652,7 @@ class RecordIN extends Component
         foreach ($this->payments as $p) {
             if ($p->corrispettivo_fiscale) {
                 $price = isset($this->corrispettivo[$p->id]) ? $this->currencyToDouble($this->corrispettivo[$p->id]) : 0;
+                $destination_id = isset($this->corrispettivo_destination[$p->id]) ? $this->corrispettivo_destination[$p->id] : null;
 
                 if ($price > 0) {
 
@@ -630,7 +660,7 @@ class RecordIN extends Component
                         'member_id' => $this->member_id,
                         'supplier_id' => null,
                         'payment_method_id' => $p->id,
-                        'destination_id' => $this->destination_id,
+                        'destination_id' => $destination_id,
                         'commercial' => $this->commercial,
                         'corrispettivo_fiscale' => $this->corrispettivo_fiscale,
                         'date' => $this->date,
@@ -639,7 +669,7 @@ class RecordIN extends Component
                         'financial_movement' => $this->financial_movement,
                         'deleted' => $this->deleted
                     ]);
-                        
+
                     if ($this->courseId && $this->courseId > 0) {
                         $mc = \App\Models\MemberCourse::findOrFail($this->courseId);
                         $this->course = \App\Models\Course::findOrFail($mc->course_id);

+ 7 - 0
app/Http/Livewire/RecordOUT.php

@@ -232,6 +232,13 @@ class RecordOUT extends Component
         $this->loadImportCausals();
     }
 
+    public function updatedPaymentMethodId() {
+        if ($this->payment_method_id) {
+            $payment_method = \App\Models\PaymentMethod::findOrFail($this->payment_method_id);
+            $this->origin_id = $payment_method->origin_id ?? null;
+        }
+    }
+
     public function loadImportCausals()
     {
         $causals = \App\Models\Causal::select('id', 'name', 'parent_id')

+ 1 - 1
app/Models/Bank.php

@@ -19,7 +19,7 @@ class Bank extends Model
 
     public function canDelete()
     {
-        return \App\Models\PaymentMethod::where('bank_id', $this->id)->count() == 0;
+        return \App\Models\PaymentMethod::where('origin_id', $this->id)->orWhere('destination_id', $this->id)->count() == 0;
     }
 
     public function getVisibility()

+ 8 - 3
app/Models/PaymentMethod.php

@@ -16,12 +16,17 @@ class PaymentMethod extends Model
         'hidden',
         'corrispettivo_fiscale',
         'enabled',
-        'bank_id'
+        'origin_id',
+        'destination_id'
     ];
 
-    public function bank()
+    public function origin()
     {
-        return $this->belongsTo(\App\Models\Bank::class);
+        return $this->belongsTo(\App\Models\Bank::class, 'origin_id', 'id');
     }
 
+    public function destination()
+    {
+        return $this->belongsTo(\App\Models\Bank::class, 'destination_id', 'id');
+    }
 }

+ 41 - 0
database/migrations/2025_12_17_101158_add_origin_destination_to_payment_methods_table.php

@@ -0,0 +1,41 @@
+<?php
+
+use App\Database\Migrations\TenantMigration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Support\Facades\DB;
+
+return new class extends TenantMigration
+{
+    public function up()
+    {
+        Schema::table('payment_methods', function (Blueprint $table) {
+            $table->dropForeign('payment_methods_bank_id_foreign');
+        });
+
+        DB::statement('ALTER TABLE `payment_methods` CHANGE `bank_id` `origin_id` BIGINT(20) UNSIGNED NULL');
+
+        Schema::table('payment_methods', function (Blueprint $table) {
+            $table->unsignedBigInteger('destination_id')->nullable()->after('origin_id');
+
+            $table->foreign('origin_id', 'payment_methods_origin_id_foreign')->references('id')->on('banks')->onUpdate('cascade')->onDelete('cascade');
+
+            $table->foreign('destination_id', 'payment_methods_destination_id_foreign')->references('id')->on('banks')->onUpdate('cascade')->onDelete('cascade');
+        });
+    }
+
+    public function down()
+    {
+        Schema::table('payment_methods', function (Blueprint $table) {
+            $table->dropForeign('payment_methods_destination_id_foreign');
+            $table->dropForeign('payment_methods_origin_id_foreign');
+            $table->dropColumn('destination_id');
+        });
+
+        DB::statement('ALTER TABLE `payment_methods`CHANGE `origin_id` `bank_id` BIGINT(20) UNSIGNED NULL');
+
+        Schema::table('payment_methods', function (Blueprint $table) {
+            $table->foreign('bank_id', 'payment_methods_bank_id_foreign')->references('id')->on('banks')->onUpdate('cascade')->onDelete('cascade');
+        });
+    }
+};

+ 31 - 16
resources/views/livewire/payment_method.blade.php

@@ -37,7 +37,8 @@
             <table class="table tablesaw tableHead tablesaw-stack" id="tablesaw-350" width="100%">
                 <thead>
                     <tr>
-                        <th scope="col">Banca</th>
+                        <th scope="col">Origine</th>
+                        <th scope="col">Destinazione</th>
                         <th scope="col">Nome</th>
                         <th scope="col">Tipo</th>
                         <th scope="col">Abilitato</th>
@@ -47,7 +48,8 @@
                 <tbody id="checkall-target">
                     @foreach($records as $record)
                         <tr @if(isset($record->hidden) && $record->hidden) style="opacity: 0.6; font-style: italic;" @endif>
-                            <td>{{$record->bank ? $record->bank : ''}}</td>
+                            <td>{{$record->origin_id ? $record->origin->name : ''}}</td>
+                            <td>{{$record->destination_id ? $record->destination->name : ''}}</td>
                             <td>
                                 {{$record->name}}
                                 @if(isset($record->hidden) && $record->hidden)
@@ -86,9 +88,9 @@
 
     @else
 
-        <div class="container">
+    <a class="btn--ui lightGrey" href="/payment_methods"><i class="fa-solid fa-arrow-left"></i></a><br><br>
 
-            <a class="btn--ui lightGrey" href="/payment_methods"><i class="fa-solid fa-arrow-left"></i></a><br><br>
+        <div class="container">
 
             @if (session()->has('error'))
                 <div class="alert alert-danger" role="alert">
@@ -116,18 +118,6 @@
                                     @enderror
                                 </div>
                             </div>
-
-                            <div class="col">
-                                <label for="bank_id" class="form-label">Banca</label>
-                                <select name="bank_id" class="form-select" aria-label="Seleziona una banca" wire:model="bank_id">
-                                    <option value="">--Seleziona--</option>
-                                    @foreach($banks as $bank)
-                                        <option value="{{$bank->id}}">{{$bank->name}}</option>
-                                    @endforeach
-                                </select>
-                            </div>
-                        </div>
-                        <div class="row mb-3">
                             <div class="col-md-6">
                                 <label for="type" class="form-label">Tipologia</label>
                                 <select name="type" class="form-select" aria-label="Seleziona una tipologia" wire:model="type">
@@ -137,6 +127,28 @@
                                 </select>
                             </div>
                         </div>
+                        <div class="row mb-3">
+
+                            <div class="col">
+                                <label for="origin_id" class="form-label">Origine</label>
+                                <select name="origin_id" class="form-select" aria-label="Seleziona un'origine" wire:model="origin_id">
+                                    <option value="">--Seleziona--
+                                    @foreach($origins as $origin)
+                                        <option value="{{$origin->id}}">{{$origin->name}}
+                                    @endforeach
+                                </select>
+                            </div>
+
+                            <div class="col">
+                                <label for="destination_id" class="form-label">Destinazione</label>
+                                <select name="destination_id" class="form-select" aria-label="Seleziona una destinazione" wire:model="destination_id">
+                                    <option value="">--Seleziona--
+                                    @foreach($destinations as $destination)
+                                        <option value="{{$destination->id}}">{{$destination->name}}
+                                    @endforeach
+                                </select>
+                            </div>
+                        </div>
 
                         <div class="form--item mb-3">
                             <div class="form--item">
@@ -225,6 +237,9 @@
                 thead: {
                 'th': {'background-color': 'blue'}
                 },
+                order: [
+                    [2, 'asc']
+                ],
                 layout: {
                     topStart : null,
                     topEnd : null,

+ 14 - 3
resources/views/livewire/records_in.blade.php

@@ -325,9 +325,12 @@
 
                             @if ($this->dataId > 0)
                                 <div class="row">
-                                    <div class="col-md-9 mt-3">
+                                    <div class="col-md-6 mt-3">
                                         {{$this->getPaymentMethod($payment_method_id)}}
                                     </div>
+                                    <div class="col-md-3 mt-3">
+                                        {{$this->getBankName($destination_id)}}
+                                    </div>
                                     <div class="col-md-3 mt-3">
                                         {{formatPrice($amount)}}
                                     </div>
@@ -336,9 +339,17 @@
                                 @foreach($payments as $payment)
                                     @if($payment->corrispettivo_fiscale)
                                         <div class="row">
-                                            <div class="col-md-9 mt-3">
+                                            <div class="col-md-6 mt-3">
                                                 {{$payment->name}}
                                             </div>
+                                            <div class="col-md-3 mt-3">                                                
+                                                <select id="corrispettivo_destination_{{$payment->id}}" name="corrispettivo_destination_{{$payment->id}}" wire:model="corrispettivo_destination.{{$payment->id}}" class="form-select" aria-label="Seleziona una destinazione" style="width:100%">
+                                                    <option value="">--Seleziona--
+                                                    @foreach($banks as $bank)
+                                                        <option value="{{$bank->id}}">{{$bank->name}}
+                                                    @endforeach
+                                                </select>
+                                            </div>
                                             <div class="col-md-3 mt-3">
                                                 <input type="text" class="form-control totalInput text-end" id="corrispettivo_{{$payment->id}}" wire:model="corrispettivo.{{$payment->id}}" onkeyup="onlyNumberAmount(this)" placeholder="€ 0,00">
                                             </div>
@@ -495,7 +506,7 @@
                                         </div>
                                     </div>
                                 @endif
-                                 <div class="row gx-2 mt-3 align-items-center">
+                                <div class="row gx-2 mt-3 align-items-center">
                                     <div class="col-md-4">
                                         <span class="total primary comp">Competenza</span>
                                     </div>