diff --git a/app/Models/PaymentHash.php b/app/Models/PaymentHash.php index c2ea7232b651..a19164ea058a 100644 --- a/app/Models/PaymentHash.php +++ b/app/Models/PaymentHash.php @@ -38,7 +38,7 @@ class PaymentHash extends Model public function fee_invoice() { - return $this->belongsTo(Invoice::class, 'fee_invoice_id', 'id'); + return $this->belongsTo(Invoice::class, 'fee_invoice_id', 'id')->withTrashed(); } public function withData(string $property, $value): self diff --git a/app/Services/Credit/ApplyPayment.php b/app/Services/Credit/ApplyPayment.php index ca1edf32d665..21838ebe0896 100644 --- a/app/Services/Credit/ApplyPayment.php +++ b/app/Services/Credit/ApplyPayment.php @@ -148,6 +148,7 @@ class ApplyPayment if ((int)$this->invoice->balance == 0) { $this->invoice->service()->deletePdf(); + $this->invoice = $this->invoice->fresh(); event(new InvoiceWasPaid($this->invoice, $this->payment, $this->payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); } } diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index ec5ddc18fe1a..4fe87c4ec4e0 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -47,7 +47,7 @@ class AutoBillInvoice extends AbstractService MultiDB::setDb($this->db); - $this->client = $this->invoice->client; + $this->client = $this->invoice->client->fresh(); $is_partial = false; @@ -178,14 +178,16 @@ class AutoBillInvoice extends AbstractService } $payment->ledger() - ->updatePaymentBalance($amount * -1) - ->save(); + ->updatePaymentBalance($amount * -1) + ->save(); - $this->invoice->client->service() - ->updateBalance($amount * -1) - ->updatePaidToDate($amount) - ->adjustCreditBalance($amount * -1) - ->save(); + $client = $this->invoice->client->fresh(); + + $client->service() + ->updateBalance($amount * -1) + ->updatePaidToDate($amount) + ->adjustCreditBalance($amount * -1) + ->save(); $this->invoice->ledger() ->updateInvoiceBalance($amount * -1, "Invoice {$this->invoice->number} payment using Credit {$current_credit->number}") diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 7065705c28bb..556ddd817eb1 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -41,6 +41,8 @@ class UpdateInvoicePayment collect($paid_invoices)->each(function ($paid_invoice) use ($invoices) { + $client = $this->payment->client->fresh(); + $invoice = $invoices->first(function ($inv) use ($paid_invoice) { return $paid_invoice->invoice_id == $inv->hashed_id; }); @@ -70,8 +72,7 @@ class UpdateInvoicePayment ->ledger() ->updatePaymentBalance($paid_amount * -1); - $this->payment - ->client + $client ->service() ->updateBalance($paid_amount * -1) ->updatePaidToDate($paid_amount) @@ -94,11 +95,12 @@ class UpdateInvoicePayment $this->payment->saveQuietly(); $invoices->each(function ($invoice) { - + + $invoice = $invoice->fresh(); event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); }); - return $this->payment; + return $this->payment->fresh(); } }