From 0216286efcf61b5484aa61fbcc17a510bef3a98a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 24 Sep 2020 11:29:47 +0200 Subject: [PATCH] Support for over & under payments --- .../ClientPortal/PaymentController.php | 44 +++++++++++++++++-- resources/lang/en/texts.php | 5 +++ .../ninja2020/invoices/payment.blade.php | 32 +++++++++++--- 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index 489c441d5df6..658ad9ff562f 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -73,9 +73,10 @@ class PaymentController extends Controller */ public function process() { - $gateway = CompanyGateway::find(request()->input('company_gateway_id')); + /*find invoices*/ + $payable_invoices = request()->payable_invoices; $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($payable_invoices, 'invoice_id')))->get(); @@ -91,9 +92,11 @@ class PaymentController extends Controller ->with(['warning' => 'No payable invoices selected.']); } - /*iterate through invoices and add gateway fees and other payment metadata*/ + $settings = auth()->user()->client->getMergedSettings(); + /*iterate through invoices and add gateway fees and other payment metadata*/ foreach ($payable_invoices as $key => $payable_invoice) { + $payable_invoices[$key]['amount'] = Number::parseFloat($payable_invoice['amount']); $payable_invoice['amount'] = $payable_invoices[$key]['amount']; @@ -101,6 +104,41 @@ class PaymentController extends Controller return $payable_invoice['invoice_id'] == $inv->hashed_id; }); + // Check if company supports over & under payments. + // In case it doesn't this is where process should stop. + + $payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), auth()->user()->client->currency()->precision); + $invoice_amount = Number::roundValue($invoice->amount, auth()->user()->client->currency()->precision); + + if ($settings->client_portal_allow_under_payment == false && $settings->client_portal_allow_over_payment == false) { + $payable_invoice['amount'] = $invoice->amount; + } // We don't allow either of these, reset the amount to default invoice (to prevent inspect element payments). + + if ($settings->client_portal_allow_under_payment) { + if ($payable_invoice['amount'] < $settings->client_portal_under_payment_minimum) { + return redirect() + ->route('client.invoices.index') + ->with('message', ctrans('texts.minimum_required_payment', ['amount' => $settings->client_portal_under_payment_minimum])); + } + } else { + $payable_amount = Number::roundValue(Number::parseFloat($payable_invoice['amount']), auth()->user()->client->currency()->precision); + $invoice_amount = Number::roundValue($invoice->amount, auth()->user()->client->currency()->precision); + + if ($payable_amount < $invoice_amount) { + return redirect() + ->route('client.invoices.index') + ->with('message', ctrans('texts.under_payments_disabled')); + } + } // Make sure 'amount' from form is not lower than 'amount' from invoice. + + if ($settings->client_portal_allow_over_payment == false) { + if ($payable_amount > $invoice_amount) { + return redirect() + ->route('client.invoices.index') + ->with('message', ctrans('texts.over_payments_disabled')); + } + } // Make sure 'amount' from form is not higher than 'amount' from invoice. + $payable_invoices[$key]['due_date'] = $this->formatDate($invoice->due_date, $invoice->client->date_format()); $payable_invoices[$key]['invoice_number'] = $invoice->number; @@ -129,7 +167,7 @@ class PaymentController extends Controller $first_invoice = $invoices->first(); $fee_totals = round($gateway->calcGatewayFee($invoice_totals, true), $first_invoice->client->currency()->precision); - if (! $first_invoice->uses_inclusive_taxes) { + 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); diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index df2ec1ec2253..0628fa616320 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -3272,4 +3272,9 @@ return [ 'password_strength' => 'Password strength too weak', 'thanks' => 'Thanks', + + 'minimum_required_payment' => 'Minimum required payment is :amount', + + 'under_payments_disabled' => 'Company doesn\'t support under payments.', + 'over_payments_disabled' => 'Company doesn\'t support over payments.', ]; diff --git a/resources/views/portal/ninja2020/invoices/payment.blade.php b/resources/views/portal/ninja2020/invoices/payment.blade.php index 9cd24264dd4b..6a67f6176e5f 100644 --- a/resources/views/portal/ninja2020/invoices/payment.blade.php +++ b/resources/views/portal/ninja2020/invoices/payment.blade.php @@ -36,7 +36,7 @@
@foreach($payment_methods as $payment_method) - + {{ $payment_method['label'] }} @endforeach @@ -94,11 +94,11 @@
@if($invoice->po_number) - {{ $invoice->po_number }} + {{ $invoice->po_number }} @elseif($invoice->public_notes) - {{ $invoice->public_notes }} + {{ $invoice->public_notes }} @else - {{ $invoice->date}} + {{ $invoice->date}} @endif
@@ -108,9 +108,25 @@
{{ ctrans('texts.amount') }}
-
+
- + + @if(!$settings->client_portal_allow_under_payment && !$settings->client_portal_allow_over_payment) + {{ App\Utils\Number::formatMoney($invoice->amount, $invoice->client) }} + @else +
+ + {{ $invoice->client->currency()->code }} ({{ $invoice->client->currency()->symbol }}) +
+ @endif + + @if($settings->client_portal_allow_under_payment) + {{ ctrans('texts.minimum_payment') }}: {{ $settings->client_portal_under_payment_minimum }} + @endif
@endif @@ -122,10 +138,12 @@ + @include('portal.ninja2020.invoices.includes.terms') @include('portal.ninja2020.invoices.includes.signature') + @endsection @push('footer') - + @endpush \ No newline at end of file