mirror of
https://github.com/internetee/registry.git
synced 2025-06-06 12:47:29 +02:00
parent
d85e57d800
commit
c807bb9fcd
2 changed files with 36 additions and 3 deletions
|
@ -2,7 +2,18 @@ class InvoiceItem < ActiveRecord::Base
|
||||||
include Versions
|
include Versions
|
||||||
belongs_to :invoice
|
belongs_to :invoice
|
||||||
|
|
||||||
|
delegate :vat_rate, to: :invoice
|
||||||
|
|
||||||
def item_sum_without_vat
|
def item_sum_without_vat
|
||||||
(price * quantity).round(2)
|
(price * quantity).round(2)
|
||||||
end
|
end
|
||||||
end
|
alias_method :subtotal, :item_sum_without_vat
|
||||||
|
|
||||||
|
def vat_amount
|
||||||
|
subtotal * (vat_rate / 100)
|
||||||
|
end
|
||||||
|
|
||||||
|
def total
|
||||||
|
subtotal + vat_amount
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,8 +1,30 @@
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class InvoiceItemTest < ActiveSupport::TestCase
|
class InvoiceItemTest < ActiveSupport::TestCase
|
||||||
def test_calculates_sum_without_vat
|
def test_calculates_subtotal
|
||||||
invoice_item = InvoiceItem.new(price: 5, quantity: 2)
|
invoice_item = InvoiceItem.new(price: 5, quantity: 2)
|
||||||
assert_equal 10, invoice_item.item_sum_without_vat
|
assert_equal 10, invoice_item.item_sum_without_vat
|
||||||
|
assert_equal 10, invoice_item.subtotal
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
def test_returns_vat_rate
|
||||||
|
vat_rate = 20
|
||||||
|
invoice = Invoice.new(vat_rate: vat_rate)
|
||||||
|
|
||||||
|
invoice_item = InvoiceItem.new(invoice: invoice)
|
||||||
|
|
||||||
|
assert_equal vat_rate, invoice_item.vat_rate
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_calculates_vat_amount
|
||||||
|
invoice = Invoice.new(vat_rate: 20)
|
||||||
|
invoice_item = InvoiceItem.new(price: 5, quantity: 2, invoice: invoice)
|
||||||
|
assert_equal 2, invoice_item.vat_amount
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_calculates_total
|
||||||
|
invoice = Invoice.new(vat_rate: 20)
|
||||||
|
invoice_item = InvoiceItem.new(price: 5, quantity: 2, invoice: invoice)
|
||||||
|
assert_equal 12, invoice_item.total
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue