diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 97e7666..8b6b610 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Support\Str; class Invoice extends Model @@ -72,6 +73,11 @@ class Invoice extends Model return $this->hasMany(InvoiceLine::class); } + public function payments(): HasMany + { + return $this->hasMany(Payment::class); + } + public function recalculateTotal(): void { $this->attributes['total'] = $this->lines()->sum('amount'); diff --git a/resources/views/components/⚡edit-payment.blade.php b/resources/views/components/⚡edit-payment.blade.php new file mode 100644 index 0000000..621d860 --- /dev/null +++ b/resources/views/components/⚡edit-payment.blade.php @@ -0,0 +1,151 @@ +with('client')->get()->sortBy('client.abbreviation'); + } + + public function loadContacts(): void + { + if (!$this->invoice_id) { + $this->contacts = []; + return; + } + $invoice = Invoice::find($this->invoice_id); + $this->contacts = $invoice?->client?->contacts ?? collect(); + } + + public function updatedInvoiceId(): void + { + $this->loadContacts(); + $this->contact_id = null; + } + + #[On('edit-payment')] + public function loadPayment(int $paymentId): void + { + $this->payment = Payment::findOrFail($paymentId); + + $this->invoice_id = $this->payment->invoice_id; + $this->loadContacts(); + $this->contact_id = $this->payment->contact_id; + $this->payment_date = $this->payment->payment_date->format('Y-m-d'); + $this->status = $this->payment->status; + $this->payment_method = $this->payment->payment_method; + $this->reference = $this->payment->reference; + $this->amount = $this->payment->amount; + $this->notes = $this->payment->notes; + + Flux::modal('edit-payment')->show(); + } + + public function save(): void + { + $this->validate(); + + $this->payment->update([ + 'invoice_id' => $this->invoice_id, + 'contact_id' => $this->contact_id, + 'payment_date' => $this->payment_date, + 'status' => $this->status, + 'payment_method' => $this->payment_method, + 'reference' => $this->reference, + 'amount' => $this->amount, + 'notes' => $this->notes, + ]); + + Flux::modal('edit-payment')->close(); + $this->dispatch('payment-updated'); + } +}; +?> + +