From 717cb26a3eec70c14b4a2c046c0af4904080d682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=87ak=C4=B1rel?= Date: Tue, 21 Jan 2020 20:00:22 +0300 Subject: [PATCH] Show grand total for bills/invoices in "Amount" field in "Add Payment" modal --- app/Http/Controllers/Purchases/Bills.php | 56 ++++++++++++++++++++ app/Http/Controllers/Sales/Invoices.php | 56 ++++++++++++++++++++ resources/assets/js/views/purchases/bills.js | 1 - resources/assets/js/views/sales/invoices.js | 1 - 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Purchases/Bills.php b/app/Http/Controllers/Purchases/Bills.php index 3def477d5..c828e8a04 100644 --- a/app/Http/Controllers/Purchases/Bills.php +++ b/app/Http/Controllers/Purchases/Bills.php @@ -13,6 +13,7 @@ use App\Jobs\Purchase\DeleteBill; use App\Jobs\Purchase\DuplicateBill; use App\Jobs\Purchase\UpdateBill; use App\Models\Banking\Account; +use App\Models\Banking\Transaction; use App\Models\Common\Contact; use App\Models\Common\Item; use App\Models\Purchase\Bill; @@ -74,6 +75,21 @@ class Bills extends Controller $date_format = $this->getCompanyDateFormat(); + $paid = $this->getPaidAmount($bill); + + // Get Bill Totals + foreach ($bill->totals as $bill_total) { + $bill->{$bill_total->code} = $bill_total->amount; + } + + $total = money($bill->total, $currency->code, true)->format(); + + $bill->grand_total = money($total, $currency->code)->getAmount(); + + if (!empty($paid)) { + $bill->grand_total = round($bill->total - $paid, $currency->precision) ; + } + return view('purchases.bills.show', compact('bill', 'accounts', 'currencies', 'currency', 'account_currency_code', 'vendors', 'categories', 'payment_methods', 'date_format')); } @@ -379,4 +395,44 @@ class Bills extends Controller return $bill; } + + protected function getPaidAmount($bill) + { + $paid = 0; + + // Get Bill Transactions + if (!$bill->transactions->count()) { + return $paid; + } + + $currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); + + foreach ($bill->transactions as $item) { + $default_amount = (double) $item->amount; + + if ($bill->currency_code == $item->currency_code) { + $amount = $default_amount; + } else { + $default_amount_model = new Transaction(); + $default_amount_model->default_currency_code = $bill->currency_code; + $default_amount_model->amount = $default_amount; + $default_amount_model->currency_code = $item->currency_code; + $default_amount_model->currency_rate = $currencies[$item->currency_code]; + + $default_amount = (double) $default_amount_model->getDivideConvertedAmount(); + + $convert_amount_model = new Transaction(); + $convert_amount_model->default_currency_code = $item->currency_code; + $convert_amount_model->amount = $default_amount; + $convert_amount_model->currency_code = $bill->currency_code; + $convert_amount_model->currency_rate = $currencies[$bill->currency_code]; + + $amount = (double) $convert_amount_model->getAmountConvertedFromCustomDefault(); + } + + $paid += $amount; + } + + return $paid; + } } diff --git a/app/Http/Controllers/Sales/Invoices.php b/app/Http/Controllers/Sales/Invoices.php index bae2fb4a6..6059e74c1 100644 --- a/app/Http/Controllers/Sales/Invoices.php +++ b/app/Http/Controllers/Sales/Invoices.php @@ -13,6 +13,7 @@ use App\Jobs\Sale\DeleteInvoice; use App\Jobs\Sale\DuplicateInvoice; use App\Jobs\Sale\UpdateInvoice; use App\Models\Banking\Account; +use App\Models\Banking\Transaction; use App\Models\Common\Contact; use App\Models\Common\Item; use App\Models\Sale\Invoice; @@ -77,6 +78,21 @@ class Invoices extends Controller $date_format = $this->getCompanyDateFormat(); + $paid = $this->getPaidAmount($invoice); + + // Get Invoice Totals + foreach ($invoice->totals as $invoice_total) { + $invoice->{$invoice_total->code} = $invoice_total->amount; + } + + $total = money($invoice->total, $currency->code, true)->format(); + + $invoice->grand_total = money($total, $currency->code)->getAmount(); + + if (!empty($paid)) { + $invoice->grand_total = round($invoice->total - $paid, $currency->precision); + } + return view('sales.invoices.show', compact('invoice', 'accounts', 'currencies', 'currency', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'signed_url', 'date_format')); } @@ -448,4 +464,44 @@ class Invoices extends Controller return $invoice; } + + protected function getPaidAmount($invoice) + { + $paid = 0; + + // Get invoice transactions + if (!$invoice->transactions->count()) { + return $paid; + } + + $currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); + + foreach ($invoice->transactions as $item) { + $default_amount = $item->amount; + + if ($invoice->currency_code == $item->currency_code) { + $amount = (double) $default_amount; + } else { + $default_amount_model = new Transaction(); + $default_amount_model->default_currency_code = $invoice->currency_code; + $default_amount_model->amount = $default_amount; + $default_amount_model->currency_code = $item->currency_code; + $default_amount_model->currency_rate = $currencies[$item->currency_code]; + + $default_amount = (double) $default_amount_model->getDivideConvertedAmount(); + + $convert_amount_model = new Transaction(); + $convert_amount_model->default_currency_code = $item->currency_code; + $convert_amount_model->amount = $default_amount; + $convert_amount_model->currency_code = $invoice->currency_code; + $convert_amount_model->currency_rate = $currencies[$invoice->currency_code]; + + $amount = (double) $convert_amount_model->getAmountConvertedFromCustomDefault(); + } + + $paid += $amount; + } + + return $paid; + } } diff --git a/resources/assets/js/views/purchases/bills.js b/resources/assets/js/views/purchases/bills.js index d05cc7a01..9c9bb43cc 100644 --- a/resources/assets/js/views/purchases/bills.js +++ b/resources/assets/js/views/purchases/bills.js @@ -206,7 +206,6 @@ const app = new Vue({ this.transaction_form.paid_at = form.paid_at; this.transaction_form.account_id = form.account_id; this.transaction_form.payment_method = form.payment_method; - this.transaction_form.amount = form.amount; }, addPayment() { diff --git a/resources/assets/js/views/sales/invoices.js b/resources/assets/js/views/sales/invoices.js index 63e5ed0c2..38c9e8854 100644 --- a/resources/assets/js/views/sales/invoices.js +++ b/resources/assets/js/views/sales/invoices.js @@ -206,7 +206,6 @@ const app = new Vue({ this.transaction_form.paid_at = form.paid_at; this.transaction_form.account_id = form.account_id; this.transaction_form.payment_method = form.payment_method; - this.transaction_form.amount = form.amount; }, addPayment() {