class Docdata::LineItem
Object
representing a “LineItem”
@example
LineItem.new({ :name => "Ham and Eggs by dr. Seuss", :code => "EAN312313235", :quantity => 1, :description => "The famous childrens book", :image => "http://blogs.slj.com/afuse8production/files/2012/06/GreenEggsHam1.jpg", :price_per_unit => 1299, :vat_rate => 17.5, :vat_included => true }) @note Warning: do not use this part of the gem, for it will break. Be warned!
Attributes
code[RW]
description[RW]
errors[RW]
image[RW]
name[RW]
price_per_unit[RW]
quantity[RW]
unit_of_measure[RW]
vat_included[RW]
vat_rate[RW]
Public Class Methods
new(args=nil)
click to toggle source
Initializer to transform a Hash
into an LineItem
object
@param [Hash] args
# File lib/docdata/line_item.rb, line 47 def initialize(args=nil) @unit_of_measure = "PCS" @vat_rate = 0 @vat_included = true return if args.nil? args.each do |k,v| instance_variable_set("@#{k}", v) unless v.nil? end end
Public Instance Methods
error_message()
click to toggle source
@return [String] the string that contains all the errors for this line_item
# File lib/docdata/line_item.rb, line 64 def error_message "One of your line_items is invalid. Error messages: #{errors.full_messages.join(', ')}" end
gross_amount()
click to toggle source
# File lib/docdata/line_item.rb, line 73 def gross_amount if vat_included total_price else total_price + vat end end
nett_amount()
click to toggle source
# File lib/docdata/line_item.rb, line 81 def nett_amount if vat_included total_price - vat else total_vat end end
total_price()
click to toggle source
@return [Integer] total price of this line item
# File lib/docdata/line_item.rb, line 69 def total_price price_per_unit * quantity end
valid?()
click to toggle source
@return [Boolean] true/false, depending if this instanciated object is valid
# File lib/docdata/line_item.rb, line 58 def valid? validator = LineItemValidator.new validator.valid?(self) end
vat()
click to toggle source
@return [Integer] the total amount of VAT (in cents) that is applicable for this line item, based on the vat_rate
, quantity and price_per_unit
# File lib/docdata/line_item.rb, line 91 def vat if vat_included ((gross_amount.to_f * "1.#{vat_rate.to_s.gsub('.','')}".to_f) - gross_amount) * -1 else ((nett_amount.to_f * "1.#{vat_rate.to_s.gsub('.','')}".to_f) - nett_amount) end end