Class: NewStoreApi::LineItemPrice

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/new_store_api/models/line_item_price.rb

Overview

The required price points for a line item, in the currency's minor unit.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#check_for_conflict, #process_additional_properties, #process_array, #process_basic_value, #process_hash, #to_hash, #to_json

Constructor Details

#initialize(list_price = nil, price = nil, default_list_price = SKIP, discounts = SKIP, price_override = SKIP, price_tax = 0, pricebook = 'default', tax_lines = SKIP) ⇒ LineItemPrice

Returns a new instance of LineItemPrice.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/new_store_api/models/line_item_price.rb', line 91

def initialize(list_price = nil, price = nil, default_list_price = SKIP,
               discounts = SKIP, price_override = SKIP, price_tax = 0,
               pricebook = 'default', tax_lines = SKIP)
  @default_list_price = default_list_price unless default_list_price == SKIP
  @discounts = discounts unless discounts == SKIP
  @list_price = list_price
  @price = price
  @price_override = price_override unless price_override == SKIP
  @price_tax = price_tax unless price_tax == SKIP
  @pricebook = pricebook unless pricebook == SKIP
  @tax_lines = tax_lines unless tax_lines == SKIP
end

Instance Attribute Details

#default_list_priceInteger

The price of the product in the default pricebook, in the currency's minor unit. Differs from list_price when the item was priced from another pricebook. Null when not supplied.

Returns:

  • (Integer)


16
17
18
# File 'lib/new_store_api/models/line_item_price.rb', line 16

def default_list_price
  @default_list_price
end

#discountsArray[Object]

Discounts applied to this item, item-level or order-level prorated onto it (see scope). Their applied_amount is already deducted from price, so they describe how list_price became price rather than a further reduction to apply.

Returns:

  • (Array[Object])


23
24
25
# File 'lib/new_store_api/models/line_item_price.rb', line 23

def discounts
  @discounts
end

#list_priceInteger

The price of the product in the chosen pricebook, in the currency's minor unit. This is the price the item's discounts are applied to.

Returns:

  • (Integer)


28
29
30
# File 'lib/new_store_api/models/line_item_price.rb', line 28

def list_price
  @list_price
end

#priceInteger

The price of the item after its discounts have been applied, in the currency's minor unit. It must equal list_price minus the sum of discounts[].applied_amount, or, when a price_override is supplied, the overridden price.

Returns:

  • (Integer)


35
36
37
# File 'lib/new_store_api/models/line_item_price.rb', line 35

def price
  @price
end

#price_overridePriceOverride

Details of a manual price override applied to this item; null when none was applied.

Returns:



40
41
42
# File 'lib/new_store_api/models/line_item_price.rb', line 40

def price_override
  @price_override
end

#price_taxInteger

Total tax amount for the item, in the currency's minor unit. With tax_excluded pricing the customer pays price plus this tax; with tax_included pricing this is the tax already contained in price. Defaults to 0.

Returns:

  • (Integer)


47
48
49
# File 'lib/new_store_api/models/line_item_price.rb', line 47

def price_tax
  @price_tax
end

#pricebookString

A reference to the pricebook used to lookup list_price

Returns:

  • (String)


51
52
53
# File 'lib/new_store_api/models/line_item_price.rb', line 51

def pricebook
  @pricebook
end

#tax_linesArray[TaxLine]

Optional breakdown of the taxes that make up price_tax.

Returns:



55
56
57
# File 'lib/new_store_api/models/line_item_price.rb', line 55

def tax_lines
  @tax_lines
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates an instance of the object from a hash.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/new_store_api/models/line_item_price.rb', line 105

def self.from_hash(hash)
  return nil unless hash

  # Extract variables from the hash.
  list_price = hash.key?('list_price') ? hash['list_price'] : nil
  price = hash.key?('price') ? hash['price'] : nil
  default_list_price =
    hash.key?('default_list_price') ? hash['default_list_price'] : SKIP
  discounts = hash.key?('discounts') ? APIHelper.deserialize_union_type(
    UnionTypeLookUp.get(:LineItemPriceDiscounts), hash['discounts']
  ) : SKIP
  price_override = PriceOverride.from_hash(hash['price_override']) if hash['price_override']
  price_tax = hash['price_tax'] ||= 0
  pricebook = hash['pricebook'] ||= 'default'
  # Parameter is an array, so we need to iterate through it
  tax_lines = nil
  unless hash['tax_lines'].nil?
    tax_lines = []
    hash['tax_lines'].each do |structure|
      tax_lines << (TaxLine.from_hash(structure) if structure)
    end
  end

  tax_lines = SKIP unless hash.key?('tax_lines')

  # Create object from extracted values.
  LineItemPrice.new(list_price,
                    price,
                    default_list_price,
                    discounts,
                    price_override,
                    price_tax,
                    pricebook,
                    tax_lines)
end

.namesObject

A mapping from model property names to API property names.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/new_store_api/models/line_item_price.rb', line 58

def self.names
  @_hash = {} if @_hash.nil?
  @_hash['default_list_price'] = 'default_list_price'
  @_hash['discounts'] = 'discounts'
  @_hash['list_price'] = 'list_price'
  @_hash['price'] = 'price'
  @_hash['price_override'] = 'price_override'
  @_hash['price_tax'] = 'price_tax'
  @_hash['pricebook'] = 'pricebook'
  @_hash['tax_lines'] = 'tax_lines'
  @_hash
end

.nullablesObject

An array for nullable fields



84
85
86
87
88
89
# File 'lib/new_store_api/models/line_item_price.rb', line 84

def self.nullables
  %w[
    default_list_price
    price_override
  ]
end

.optionalsObject

An array for optional fields



72
73
74
75
76
77
78
79
80
81
# File 'lib/new_store_api/models/line_item_price.rb', line 72

def self.optionals
  %w[
    default_list_price
    discounts
    price_override
    price_tax
    pricebook
    tax_lines
  ]
end

.validate(value) ⇒ Object

Validates an instance of the object from a given value.

Parameters:

  • The (LineItemPrice | Hash)

    value against the validation is performed.



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/new_store_api/models/line_item_price.rb', line 143

def self.validate(value)
  if value.instance_of? self
    return (
      APIHelper.valid_type?(value.list_price,
                            ->(val) { val.instance_of? Integer }) and
        APIHelper.valid_type?(value.price,
                              ->(val) { val.instance_of? Integer })
    )
  end

  return false unless value.instance_of? Hash

  (
    APIHelper.valid_type?(value['list_price'],
                          ->(val) { val.instance_of? Integer }) and
      APIHelper.valid_type?(value['price'],
                            ->(val) { val.instance_of? Integer })
  )
end

Instance Method Details

#inspectObject

Provides a debugging-friendly string with detailed object information.



172
173
174
175
176
177
178
# File 'lib/new_store_api/models/line_item_price.rb', line 172

def inspect
  class_name = self.class.name.split('::').last
  "<#{class_name} default_list_price: #{@default_list_price.inspect}, discounts:"\
  " #{@discounts.inspect}, list_price: #{@list_price.inspect}, price: #{@price.inspect},"\
  " price_override: #{@price_override.inspect}, price_tax: #{@price_tax.inspect}, pricebook:"\
  " #{@pricebook.inspect}, tax_lines: #{@tax_lines.inspect}>"
end

#to_sObject

Provides a human-readable string representation of the object.



164
165
166
167
168
169
# File 'lib/new_store_api/models/line_item_price.rb', line 164

def to_s
  class_name = self.class.name.split('::').last
  "<#{class_name} default_list_price: #{@default_list_price}, discounts: #{@discounts},"\
  " list_price: #{@list_price}, price: #{@price}, price_override: #{@price_override},"\
  " price_tax: #{@price_tax}, pricebook: #{@pricebook}, tax_lines: #{@tax_lines}>"
end