Class: Docdata::LineItem

Inherits:
Object
  • Object
show all
Defined in:
lib/docdata/line_item.rb

Overview

Object representing a "LineItem"

@note Warning: do not use this part of the gem, for it will break. Be warned!

Examples:

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
})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ LineItem

Initializer to transform a +Hash+ into an LineItem object

Parameters:

  • args (Hash) (defaults to: nil)


47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#codeObject

Returns the value of attribute code.



34
35
36
# File 'lib/docdata/line_item.rb', line 34

def code
  @code
end

#descriptionObject

Returns the value of attribute description.



37
38
39
# File 'lib/docdata/line_item.rb', line 37

def description
  @description
end

#errorsObject

Returns the value of attribute errors.



32
33
34
# File 'lib/docdata/line_item.rb', line 32

def errors
  @errors
end

#imageObject

Returns the value of attribute image.



38
39
40
# File 'lib/docdata/line_item.rb', line 38

def image
  @image
end

#nameObject

Returns the value of attribute name.



33
34
35
# File 'lib/docdata/line_item.rb', line 33

def name
  @name
end

#price_per_unitObject

Returns the value of attribute price_per_unit.



39
40
41
# File 'lib/docdata/line_item.rb', line 39

def price_per_unit
  @price_per_unit
end

#quantityObject

Returns the value of attribute quantity.



35
36
37
# File 'lib/docdata/line_item.rb', line 35

def quantity
  @quantity
end

#unit_of_measureObject

Returns the value of attribute unit_of_measure.



36
37
38
# File 'lib/docdata/line_item.rb', line 36

def unit_of_measure
  @unit_of_measure
end

#vat_includedObject

Returns the value of attribute vat_included.



41
42
43
# File 'lib/docdata/line_item.rb', line 41

def vat_included
  @vat_included
end

#vat_rateObject

Returns the value of attribute vat_rate.



40
41
42
# File 'lib/docdata/line_item.rb', line 40

def vat_rate
  @vat_rate
end

Instance Method Details

#error_messageString

Returns the string that contains all the errors for this line_item.

Returns:

  • (String)

    the string that contains all the errors for this line_item



64
65
66
# 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_amountObject



73
74
75
76
77
78
79
# File 'lib/docdata/line_item.rb', line 73

def gross_amount
  if vat_included
    total_price
  else
    total_price + vat
  end
end

#nett_amountObject



81
82
83
84
85
86
87
# File 'lib/docdata/line_item.rb', line 81

def nett_amount
  if vat_included
    total_price - vat
  else
    total_vat
  end
end

#total_priceInteger

Returns total price of this line item.

Returns:

  • (Integer)

    total price of this line item



69
70
71
# File 'lib/docdata/line_item.rb', line 69

def total_price
  price_per_unit * quantity
end

#valid?Boolean

Returns true/false, depending if this instanciated object is valid.

Returns:

  • (Boolean)

    true/false, depending if this instanciated object is valid



58
59
60
61
# File 'lib/docdata/line_item.rb', line 58

def valid?
  validator = LineItemValidator.new
  validator.valid?(self)
end

#vatInteger

based on the vat_rate, quantity and price_per_unit

Returns:

  • (Integer)

    the total amount of VAT (in cents) that is applicable for this line item,



91
92
93
94
95
96
97
# 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