From a2ef847f545ef6f852c79676bb181ffd0a2e0003 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 2 Apr 2022 16:13:31 +1100 Subject: [PATCH] Fixes for ledger --- app/Http/Controllers/ClientPortal/InvoiceController.php | 2 +- app/Http/Requests/Statements/CreateStatementRequest.php | 8 +++++--- app/PaymentDrivers/BaseDriver.php | 2 +- app/Services/ClientPortal/InstantPayment.php | 3 +-- app/Services/Invoice/AutoBillInvoice.php | 2 +- app/Services/Invoice/InvoiceService.php | 3 +-- app/Services/Payment/UpdateInvoicePayment.php | 8 ++++++-- 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index 42cd3598d1b4..53f643794582 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -163,7 +163,7 @@ class InvoiceController extends Controller //format data $invoices->map(function ($invoice) { - $invoice->service()->removeUnpaidGatewayFees()->save(); + $invoice->service()->removeUnpaidGatewayFees(); $invoice->balance = $invoice->balance > 0 ? Number::formatValue($invoice->balance, $invoice->client->currency()) : 0; $invoice->partial = $invoice->partial > 0 ? Number::formatValue($invoice->partial, $invoice->client->currency()) : 0; diff --git a/app/Http/Requests/Statements/CreateStatementRequest.php b/app/Http/Requests/Statements/CreateStatementRequest.php index 804cd071dbfd..05d043b6a7c6 100644 --- a/app/Http/Requests/Statements/CreateStatementRequest.php +++ b/app/Http/Requests/Statements/CreateStatementRequest.php @@ -16,7 +16,10 @@ class CreateStatementRequest extends Request */ public function authorize(): bool { - return auth()->user()->isAdmin(); + // return auth()->user()->isAdmin(); + + return auth()->user()->can('view', $this->client()); + } /** @@ -52,7 +55,6 @@ class CreateStatementRequest extends Request public function client(): ?Client { - // return Client::without('gateway_tokens','documents','contacts.company',)->where('id', $this->client_id)->withTrashed()->first(); - return Client::without('company',)->where('id', $this->client_id)->withTrashed()->first(); + return Client::without('company')->where('id', $this->client_id)->withTrashed()->first(); } } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 36f730224711..2841ebd1bfc2 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -349,7 +349,7 @@ class BaseDriver extends AbstractPaymentDriver $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->get(); $invoices->each(function ($invoice) { - $invoice->service()->removeUnpaidGatewayFees()->save(); + $invoice->service()->removeUnpaidGatewayFees(); }); } diff --git a/app/Services/ClientPortal/InstantPayment.php b/app/Services/ClientPortal/InstantPayment.php index c628c803f853..743f92fe58d4 100644 --- a/app/Services/ClientPortal/InstantPayment.php +++ b/app/Services/ClientPortal/InstantPayment.php @@ -72,8 +72,7 @@ class InstantPayment $invoices->each(function($invoice){ $invoice->service() ->markSent() - ->removeUnpaidGatewayFees() - ->save(); + ->removeUnpaidGatewayFees(); }); /* pop non payable invoice from the $payable_invoices array */ diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 761069a3b763..26ea686c077e 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -125,7 +125,7 @@ class AutoBillInvoice extends AbstractService } catch(\Exception $e){ nlog("payment NOT captured for ". $this->invoice->number . " with error " . $e->getMessage()); - $this->invoice->service()->removeUnpaidGatewayFees()->save(); + $this->invoice->service()->removeUnpaidGatewayFees(); } if($payment){ diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index b5ef8bd06ba3..8ecc39274f44 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -370,7 +370,6 @@ class InvoiceService })->toArray(); $this->invoice = $this->invoice->calc()->getInvoice(); - $this->invoice->save(); /* 24-03-2022 */ $new_balance = $this->invoice->balance; @@ -378,7 +377,7 @@ class InvoiceService $post_count = count($this->invoice->line_items); nlog("pre count = {$pre_count} post count = {$post_count}"); - if($pre_count != $post_count) + if((int)$pre_count != (int)$post_count) { $adjustment = $balance - $new_balance; diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 64c2b2b5233d..a55f65fb703a 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -75,12 +75,16 @@ class UpdateInvoicePayment //caution what if we amount paid was less than partial - we wipe it! $invoice = $invoice->service() ->clearPartial() - ->updateBalance($paid_amount * -1) - ->updatePaidToDate($paid_amount) + // ->updateBalance($paid_amount * -1) + // ->updatePaidToDate($paid_amount) ->updateStatus() ->touchPdf() ->save(); + $invoice->balance -= $paid_amount; + $invoice->paid_to_date += $paid_amount; + $invoice->save(); + $invoice->service() ->workFlow() ->save();