From ca0b21b5832c050aa09c8c36c54ea089599723af Mon Sep 17 00:00:00 2001 From: denisdulici Date: Sat, 22 Dec 2018 14:59:58 +0300 Subject: [PATCH] fixed #695 --- app/Http/Controllers/Reports/ProfitLoss.php | 4 ++-- app/Models/Expense/Bill.php | 18 +++++++++++++++++- app/Models/Income/Invoice.php | 18 +++++++++++++++++- app/Traits/Currencies.php | 18 ++++++++++++------ 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Reports/ProfitLoss.php b/app/Http/Controllers/Reports/ProfitLoss.php index 9b4d6e62b..fd1f7b460 100644 --- a/app/Http/Controllers/Reports/ProfitLoss.php +++ b/app/Http/Controllers/Reports/ProfitLoss.php @@ -156,7 +156,7 @@ class ProfitLoss extends Controller private function setAmount(&$totals, &$compares, $items, $type, $date_field) { foreach ($items as $item) { - if ($item['table'] == 'bill_payments' || $item['table'] == 'invoice_payments') { + if (($item['table'] == 'bill_payments') || ($item['table'] == 'invoice_payments')) { $type_item = $item->$type; $item->category_id = $type_item->category_id; @@ -170,7 +170,7 @@ class ProfitLoss extends Controller continue; } - $amount = $item->getConvertedAmount(); + $amount = $item->getConvertedAmount(false, false); // Forecasting if ((($type == 'invoice') || ($type == 'bill')) && ($date_field == 'due_at')) { diff --git a/app/Models/Expense/Bill.php b/app/Models/Expense/Bill.php index 3588f0402..aaf7cbe98 100644 --- a/app/Models/Expense/Bill.php +++ b/app/Models/Expense/Bill.php @@ -23,7 +23,7 @@ class Bill extends Model * * @var array */ - protected $appends = ['attachment', 'discount', 'paid']; + protected $appends = ['attachment', 'amount_without_tax', 'discount', 'paid']; protected $dates = ['deleted_at', 'billed_at', 'due_at']; @@ -201,6 +201,22 @@ class Bill extends Model return $percent; } + /** + * Get the amount without tax. + * + * @return string + */ + public function getAmountWithoutTaxAttribute() + { + $amount = $this->amount; + + $this->totals()->where('code', 'tax')->each(function ($tax) use(&$amount) { + $amount -= $tax->amount; + }); + + return $amount; + } + /** * Get the paid amount. * diff --git a/app/Models/Income/Invoice.php b/app/Models/Income/Invoice.php index e6e3d9c36..3e6810828 100644 --- a/app/Models/Income/Invoice.php +++ b/app/Models/Income/Invoice.php @@ -24,7 +24,7 @@ class Invoice extends Model * * @var array */ - protected $appends = ['attachment', 'discount', 'paid']; + protected $appends = ['attachment', 'amount_without_tax', 'discount', 'paid']; protected $dates = ['deleted_at', 'invoiced_at', 'due_at']; @@ -205,6 +205,22 @@ class Invoice extends Model return $percent; } + /** + * Get the amount without tax. + * + * @return string + */ + public function getAmountWithoutTaxAttribute() + { + $amount = $this->amount; + + $this->totals()->where('code', 'tax')->each(function ($tax) use(&$amount) { + $amount -= $tax->amount; + }); + + return $amount; + } + /** * Get the paid amount. * diff --git a/app/Traits/Currencies.php b/app/Traits/Currencies.php index ae84cd911..cfdb785d8 100644 --- a/app/Traits/Currencies.php +++ b/app/Traits/Currencies.php @@ -60,18 +60,24 @@ trait Currencies return $money; } - public function getConvertedAmount($format = false) + public function getConvertedAmount($format = false, $with_tax = true) { - return $this->convert($this->amount, $this->currency_code, $this->currency_rate, $format); + $amount = $with_tax ? $this->amount : isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount; + + return $this->convert($amount, $this->currency_code, $this->currency_rate, $format); } - public function getReverseConvertedAmount($format = false) + public function getReverseConvertedAmount($format = false, $with_tax = true) { - return $this->reverseConvert($this->amount, $this->currency_code, $this->currency_rate, $format); + $amount = $with_tax ? $this->amount : isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount; + + return $this->reverseConvert($amount, $this->currency_code, $this->currency_rate, $format); } - public function getDynamicConvertedAmount($format = false) + public function getDynamicConvertedAmount($format = false, $with_tax = true) { - return $this->dynamicConvert($this->default_currency_code, $this->amount, $this->currency_code, $this->currency_rate, $format); + $amount = $with_tax ? $this->amount : isset($this->amount_without_tax) ? $this->amount_without_tax : $this->amount; + + return $this->dynamicConvert($this->default_currency_code, $amount, $this->currency_code, $this->currency_rate, $format); } } \ No newline at end of file