From 57faf6eeb5a7840b6c725f40b8b0760b92cc04ee Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 13 Oct 2020 14:25:51 +1100 Subject: [PATCH] Use credits for payments in client portal --- .../ClientPortal/PaymentController.php | 18 ++++-------------- app/Services/Client/ClientService.php | 11 +++++++++++ .../portal/default/gateways/pay_now.blade.php | 7 ++++++- .../gateways/stripe/credit_card.blade.php | 18 +++++++++++++++--- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index 6b7dc20d2a07..26f458ff17fe 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -155,27 +155,16 @@ class PaymentController extends Controller }); } - //$payment_methods = auth()->user()->client->getPaymentMethods(array_sum(array_column($payable_invoices, 'amount_with_fee'))); - $payment_method_id = request()->input('payment_method_id'); $invoice_totals = array_sum(array_column($payable_invoices, 'amount')); $first_invoice = $invoices->first(); + $credit_totals = $first_invoice->client->service()->getCreditBalance(); + $starting_invoice_amount = $first_invoice->amount; - // $fee_totals = round($gateway->calcGatewayFee($invoice_totals, true), $first_invoice->client->currency()->precision); - - // if (!$first_invoice->uses_inclusive_taxes) { - // $fee_tax = 0; - // $fee_tax += round(($first_invoice->tax_rate1 / 100) * $fee_totals, $first_invoice->client->currency()->precision); - // $fee_tax += round(($first_invoice->tax_rate2 / 100) * $fee_totals, $first_invoice->client->currency()->precision); - // $fee_tax += round(($first_invoice->tax_rate3 / 100) * $fee_totals, $first_invoice->client->currency()->precision); - - // $fee_totals += $fee_tax; - // } - $first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save(); /** @@ -194,9 +183,10 @@ class PaymentController extends Controller $payment_hash->save(); $totals = [ + 'credit_totals' => $credit_totals, 'invoice_totals' => $invoice_totals, 'fee_total' => $fee_totals, - 'amount_with_fee' => $invoice_totals + $fee_totals, + 'amount_with_fee' => max(0, (($invoice_totals + $fee_totals) - $credit_totals)), ]; $data = [ diff --git a/app/Services/Client/ClientService.php b/app/Services/Client/ClientService.php index a79e5714db86..3cd30b4ea13c 100644 --- a/app/Services/Client/ClientService.php +++ b/app/Services/Client/ClientService.php @@ -12,6 +12,7 @@ namespace App\Services\Client; use App\Models\Client; +use App\Utils\Number; class ClientService { @@ -43,6 +44,16 @@ class ClientService return $this; } + public function getCreditBalance() :float + { + $credits = $this->client->credits + ->where('is_deleted', false) + ->where('balance', '>', 0) + ->sortBy('created_at'); + + return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision); + } + public function save() :Client { $this->client->save(); diff --git a/resources/views/portal/default/gateways/pay_now.blade.php b/resources/views/portal/default/gateways/pay_now.blade.php index 21d10106488c..6e97da3731b4 100644 --- a/resources/views/portal/default/gateways/pay_now.blade.php +++ b/resources/views/portal/default/gateways/pay_now.blade.php @@ -38,7 +38,12 @@
  • {{ ctrans('texts.total')}}

    {{ $amount }}

  • - @if($fee) + @if($credit_totals > 0) +
  • {{ ctrans('texts.credit_amount')}} +

    {{ $credit_totals }}

    +
  • + @endifs + @if($fee > 0)
  • {{ ctrans('texts.gateway_fee')}}

    {{ $fee }}

  • diff --git a/resources/views/portal/ninja2020/gateways/stripe/credit_card.blade.php b/resources/views/portal/ninja2020/gateways/stripe/credit_card.blade.php index c2cbe6d6e62a..37aa836f5133 100644 --- a/resources/views/portal/ninja2020/gateways/stripe/credit_card.blade.php +++ b/resources/views/portal/ninja2020/gateways/stripe/credit_card.blade.php @@ -39,21 +39,33 @@
    {{ App\Utils\Number::formatMoney($total['invoice_totals'], $client) }}
    + @if($total['fee_total'] > 0)
    {{ ctrans('texts.gateway_fees') }}
    {{ App\Utils\Number::formatMoney($total['fee_total'], $client) }}
    + @endif + @if($total['credit_totals'] > 0)
    - {{ ctrans('texts.total') }} + {{ ctrans('texts.credit_amount') }} +
    +
    + {{ App\Utils\Number::formatMoney($total['credit_totals'], $client) }} +
    + @endif +
    + {{ ctrans('texts.amount_due') }}
    {{ App\Utils\Number::formatMoney($total['amount_with_fee'], $client) }}
    - - @if($token) + @if((int)$total['amount_with_fee'] == 0) + + + @elseif($token)
    {{ ctrans('texts.credit_card') }}