Class: Spina::Shop::Invoice
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Spina::Shop::Invoice
- Defined in:
- app/models/spina/shop/invoice.rb
Instance Method Summary collapse
- #filename ⇒ Object
- #sub_total ⇒ Object
- #tax_amount ⇒ Object
- #tax_amount_by_rates ⇒ Object
- #total ⇒ Object
Instance Method Details
#filename ⇒ Object
9 10 11 |
# File 'app/models/spina/shop/invoice.rb', line 9 def filename "inv_#{invoice_number}.pdf" end |
#sub_total ⇒ Object
13 14 15 16 17 18 19 |
# File 'app/models/spina/shop/invoice.rb', line 13 def sub_total if prices_include_tax invoice_lines.inject(BigDecimal(0)) { |t, i| t + (i.total / i.tax_modifier).round(2) } else invoice_lines.inject(BigDecimal(0)) { |t, i| t + i.total } end end |
#tax_amount ⇒ Object
21 22 23 |
# File 'app/models/spina/shop/invoice.rb', line 21 def tax_amount tax_amount_by_rates.inject(BigDecimal(0)) { |t, r| t + r[1][:tax_amount] } end |
#tax_amount_by_rates ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/spina/shop/invoice.rb', line 29 def tax_amount_by_rates if prices_include_tax rates = invoice_lines.inject({}) do |h, line| rate = h[line.tax_rate] ||= { tax_amount: BigDecimal(0), total: BigDecimal(0) } rate[:total] += (line.total / line.tax_modifier).round(2) rate[:tax_amount] += line.total - (line.total / line.tax_modifier).round(2) h end else rates = invoice_lines.inject({}) do |h, line| rate = h[line.tax_rate] ||= { tax_amount: BigDecimal(0), total: BigDecimal(0) } rate[:total] += line.total rate[:tax_amount] += (line.total * (BigDecimal(line.tax_rate) / BigDecimal(100))).round(2) h end end rates.sort{|x, y| y[0] <=> x[0]}.to_h end |
#total ⇒ Object
25 26 27 |
# File 'app/models/spina/shop/invoice.rb', line 25 def total sub_total + tax_amount end |