diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 7718c478acb7..70b4308027c7 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -322,8 +322,8 @@ class BaseDriver extends AbstractPaymentDriver if (collect($invoice->line_items)->contains('type_id', '3')) { $invoice->service()->toggleFeesPaid()->save(); - $invoice->client->service()->updateBalance($fee_total)->save(); - $invoice->ledger()->updateInvoiceBalance($fee_total, "Gateway fee adjustment for invoice {$invoice->number}"); + // $invoice->client->service()->updateBalance($fee_total)->save(); + // $invoice->ledger()->updateInvoiceBalance($fee_total, "Gateway fee adjustment for invoice {$invoice->number}"); } $transaction = [ diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index 882e03fe3fab..773c299cb50c 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -74,6 +74,8 @@ class AddGatewayFee extends AbstractService private function processGatewayFee($gateway_fee) { + $balance = $this->invoice->balance; + App::forgetInstance('translator'); $t = app('translator'); $t->replace(Ninja::transformTranslations($this->invoice->company->settings)); @@ -100,11 +102,30 @@ class AddGatewayFee extends AbstractService /**Refresh Invoice values*/ $this->invoice = $this->invoice->calc()->getInvoice(); + $new_balance = $this->invoice->balance; + + if(floatval($new_balance) - floatval($balance) != 0) + { + $adjustment = $new_balance - $balance; + + $this->invoice + ->client + ->service() + ->updateBalance($adjustment) + ->save(); + + $this->invoice + ->ledger() + ->updateInvoiceBalance($adjustment, 'Adjustment for removing gateway fee'); + } + return $this->invoice; } private function processGatewayDiscount($gateway_fee) { + $balance = $this->invoice->balance; + App::forgetInstance('translator'); $t = app('translator'); $t->replace(Ninja::transformTranslations($this->invoice->company->settings)); @@ -129,6 +150,25 @@ class AddGatewayFee extends AbstractService $this->invoice = $this->invoice->calc()->getInvoice(); + $new_balance = $this->invoice->balance; + + + if(floatval($new_balance) - floatval($balance) != 0) + { + $adjustment = $new_balance - $balance; + + $this->invoice + ->client + ->service() + ->updateBalance($adjustment * -1) + ->save(); + + $this->invoice + ->ledger() + ->updateInvoiceBalance($adjustment * -1, 'Adjustment for removing gateway fee'); + } + + return $this->invoice; } } diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index 233644b207ff..97919f0a5011 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -361,6 +361,8 @@ class InvoiceService public function removeUnpaidGatewayFees() { + $balance = $this->invoice->balance; + //return early if type three does not exist. if(!collect($this->invoice->line_items)->contains('type_id', 3)) return $this; @@ -372,6 +374,25 @@ class InvoiceService $this->invoice = $this->invoice->calc()->getInvoice(); + /* 24-03-2022 */ + $new_balance = $this->invoice->balance; + + if(floatval($balance) - floatval($new_balance) != 0) + { + $adjustment = $balance - $new_balance; + + $this->invoice + ->client + ->service() + ->updateBalance($adjustment * -1) + ->save(); + + $this->invoice + ->ledger() + ->updateInvoiceBalance($adjustment * -1, 'Adjustment for removing gateway fee'); + } + + return $this; }