From c37203076c2e31cfec51fcfc31a657fc35c8f833 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Wed, 18 Apr 2018 14:34:33 +0300 Subject: [PATCH] fixed discount percentage #297 --- app/Models/Expense/Bill.php | 14 ++++++-------- app/Models/Income/Invoice.php | 14 ++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/app/Models/Expense/Bill.php b/app/Models/Expense/Bill.php index 832c59ba1..655712881 100644 --- a/app/Models/Expense/Bill.php +++ b/app/Models/Expense/Bill.php @@ -170,18 +170,16 @@ class Bill extends Model */ public function getDiscountAttribute() { - $discount = 0; + $percent = 0; - foreach ($this->totals as $total) { - if ($total->code != 'discount') { - continue; - } + $discount = $this->totals()->where('code', 'discount')->value('amount'); - $discount = number_format((($total->amount * 100) / $this->amount), 0); + if ($discount) { + $sub_total = $this->totals()->where('code', 'sub_total')->value('amount'); - break; + $percent = number_format((($discount * 100) / $sub_total), 0); } - return $discount; + return $percent; } } diff --git a/app/Models/Income/Invoice.php b/app/Models/Income/Invoice.php index 9554fb7a6..16e5cfa9f 100644 --- a/app/Models/Income/Invoice.php +++ b/app/Models/Income/Invoice.php @@ -172,18 +172,16 @@ class Invoice extends Model */ public function getDiscountAttribute() { - $discount = 0; + $percent = 0; - foreach ($this->totals as $total) { - if ($total->code != 'discount') { - continue; - } + $discount = $this->totals()->where('code', 'discount')->value('amount'); - $discount = number_format((($total->amount * 100) / $this->amount), 0); + if ($discount) { + $sub_total = $this->totals()->where('code', 'sub_total')->value('amount'); - break; + $percent = number_format((($discount * 100) / $sub_total), 0); } - return $discount; + return $percent; } }