From 1d2134e59f03bd7ef809bcc520168d4e38baa538 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Sun, 19 Nov 2017 11:55:01 +0200 Subject: [PATCH] working on payment currency --- app/Http/Controllers/ReportController.php | 1 + app/Ninja/Datatables/PaymentDatatable.php | 8 ++++++- app/Ninja/Reports/PaymentReport.php | 24 +++++++++++++++---- app/Ninja/Repositories/PaymentRepository.php | 4 +++- app/Ninja/Transformers/PaymentTransformer.php | 2 ++ resources/lang/en/texts.php | 2 ++ resources/views/export/payments.blade.php | 2 ++ resources/views/payments/edit.blade.php | 2 +- .../views/reports/chart_builder.blade.php | 7 ++++++ 9 files changed, 45 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 0a48bd6fcd..7df9cb498e 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -100,6 +100,7 @@ class ReportController extends BaseController 'invoice_status' => request()->invoice_status, 'group_dates_by' => request()->group_dates_by, 'document_filter' => request()->document_filter, + 'currency_type' => request()->currency_type, 'export_format' => $format, ]; $report = new $reportClass($startDate, $endDate, $isExport, $options); diff --git a/app/Ninja/Datatables/PaymentDatatable.php b/app/Ninja/Datatables/PaymentDatatable.php index 435b610f92..193207f9d8 100644 --- a/app/Ninja/Datatables/PaymentDatatable.php +++ b/app/Ninja/Datatables/PaymentDatatable.php @@ -91,7 +91,13 @@ class PaymentDatatable extends EntityDatatable [ 'amount', function ($model) { - return Utils::formatMoney($model->amount, $model->currency_id, $model->country_id); + $amount = Utils::formatMoney($model->amount, $model->currency_id, $model->country_id); + + if ($model->exchange_currency_id && $model->exchange_rate != 1) { + $amount .= ' | ' . Utils::formatMoney($model->amount * $model->exchange_rate, $model->exchange_currency_id, $model->country_id); + } + + return $amount; }, ], [ diff --git a/app/Ninja/Reports/PaymentReport.php b/app/Ninja/Reports/PaymentReport.php index f3589ca003..53d201f188 100644 --- a/app/Ninja/Reports/PaymentReport.php +++ b/app/Ninja/Reports/PaymentReport.php @@ -4,6 +4,7 @@ namespace App\Ninja\Reports; use App\Models\Payment; use Auth; +use Utils; class PaymentReport extends AbstractReport { @@ -20,6 +21,7 @@ class PaymentReport extends AbstractReport public function run() { $account = Auth::user()->account; + $currencyType = $this->options['currency_type']; $invoiceMap = []; $payments = Payment::scope() @@ -39,22 +41,36 @@ class PaymentReport extends AbstractReport foreach ($payments->get() as $payment) { $invoice = $payment->invoice; $client = $payment->client; + $amount = $payment->getCompletedAmount(); + + if ($currencyType == 'converted') { + $amount *= $payment->exchange_rate; + $this->addToTotals($payment->exchange_currency_id, 'paid', $amount); + $amount = Utils::formatMoney($amount, $payment->exchange_currency_id); + } else { + $this->addToTotals($client->currency_id, 'paid', $amount); + $amount = $account->formatMoney($amount, $client); + } + $this->data[] = [ $this->isExport ? $client->getDisplayName() : $client->present()->link, $this->isExport ? $invoice->invoice_number : $invoice->present()->link, $invoice->present()->invoice_date, $account->formatMoney($invoice->amount, $client), $payment->present()->payment_date, - $account->formatMoney($payment->getCompletedAmount(), $client), + $amount, $payment->present()->method, ]; if (! isset($invoiceMap[$invoice->id])) { - $this->addToTotals($client->currency_id, 'amount', $invoice->amount); $invoiceMap[$invoice->id] = true; - } - $this->addToTotals($client->currency_id, 'paid', $payment->getCompletedAmount()); + if ($currencyType == 'converted') { + $this->addToTotals($payment->exchange_currency_id, 'amount', $invoice->amount * $payment->exchange_rate); + } else { + $this->addToTotals($client->currency_id, 'amount', $invoice->amount); + } + } } } } diff --git a/app/Ninja/Repositories/PaymentRepository.php b/app/Ninja/Repositories/PaymentRepository.php index 06f019a143..1aa7907749 100644 --- a/app/Ninja/Repositories/PaymentRepository.php +++ b/app/Ninja/Repositories/PaymentRepository.php @@ -64,6 +64,8 @@ class PaymentRepository extends BaseRepository 'payments.routing_number', 'payments.bank_name', 'payments.private_notes', + 'payments.exchange_rate', + 'payments.exchange_currency_id', 'invoices.is_deleted as invoice_is_deleted', 'gateways.name as gateway_name', 'gateways.id as gateway_id', @@ -187,7 +189,7 @@ class PaymentRepository extends BaseRepository $payment->payment_date = date('Y-m-d'); } - $payment->fill(request()->all()); + $payment->fill($input); if (! $publicId) { $clientId = $input['client_id']; diff --git a/app/Ninja/Transformers/PaymentTransformer.php b/app/Ninja/Transformers/PaymentTransformer.php index ff78eff829..23906c3029 100644 --- a/app/Ninja/Transformers/PaymentTransformer.php +++ b/app/Ninja/Transformers/PaymentTransformer.php @@ -60,6 +60,8 @@ class PaymentTransformer extends EntityTransformer 'invoice_id' => (int) ($this->invoice ? $this->invoice->public_id : $payment->invoice->public_id), 'invoice_number' => $this->invoice ? $this->invoice->invoice_number : $payment->invoice->invoice_number, 'private_notes' => $payment->private_notes, + 'exchange_rate' => (float) $payment->exchange_rate, + 'exchange_currency_id' => (int) $payment->exchange_currency_id, ]); } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index f00c4d867b..17e59c4633 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2524,6 +2524,8 @@ $LANG = array( 'your_password_reset_link' => 'Your Password Reset Link', 'subdomain_taken' => 'The subdomain is already in use', 'client_login' => 'Client Login', + 'converted_amount' => 'Converted Amount', + 'default' => 'Default', ); diff --git a/resources/views/export/payments.blade.php b/resources/views/export/payments.blade.php index 898cae71e9..c40a00de6d 100644 --- a/resources/views/export/payments.blade.php +++ b/resources/views/export/payments.blade.php @@ -9,6 +9,7 @@ {{ trans('texts.method') }} {{ trans('texts.transaction_reference') }} {{ trans('texts.private_notes') }} + {{ trans('texts.converted_amount') }} @foreach ($payments as $payment) @@ -24,6 +25,7 @@ {{ $payment->present()->method }} {{ $payment->transaction_reference }} {{ $payment->private_notes }} + {{ Utils::formatMoney($payment->amount * $payment->exchange_rate, $payment->exchange_currency_id) }} @endif @endforeach diff --git a/resources/views/payments/edit.blade.php b/resources/views/payments/edit.blade.php index 90031a4c34..ce2a028353 100644 --- a/resources/views/payments/edit.blade.php +++ b/resources/views/payments/edit.blade.php @@ -90,7 +90,7 @@ {!! Former::text('exchange_rate') ->data_bind("value: exchange_rate, enable: enableExchangeRate, valueUpdate: 'afterkeydown'") !!} {!! Former::text('') - ->label(trans('texts.amount')) + ->label(trans('texts.converted_amount')) ->data_bind("value: convertedAmount, enable: enableExchangeRate") ->append('') !!} diff --git a/resources/views/reports/chart_builder.blade.php b/resources/views/reports/chart_builder.blade.php index 33dc8535f4..4e4c237b4e 100644 --- a/resources/views/reports/chart_builder.blade.php +++ b/resources/views/reports/chart_builder.blade.php @@ -141,6 +141,12 @@ ->addOption(trans('texts.payment_date'), FILTER_PAYMENT_DATE) !!} + +