Class: Colppy::Invoice::Item

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/colppy/resources/invoice.rb

Constant Summary collapse

ATTRIBUTES_MAPPER =
{
  idItem: :product_id,
  minimo: :minimum_stock,
  tipoItem: :item_type,
  codigo: :codigo,
  Descripcion: :name,
  unidadMedida: :measure_unit,
  Cantidad: :quantity, # required
  ImporteUnitario: :unit_price,
  IVA: :tax,
  porcDesc: :discount_percentage,
  subtotal: :subtotal,
  idPlanCuenta: :sales_account_id,
  Comentario: :comment
}.freeze
PROTECTED_DATA_KEYS =
[:product_id].freeze
DATA_KEYS_SETTERS =
(ATTRIBUTES_MAPPER.values - PROTECTED_DATA_KEYS).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

rename_params_hash

Constructor Details

#initialize(params, company = nil) ⇒ Item

Returns a new instance of Item.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/colppy/resources/invoice.rb', line 91

def initialize(params, company = nil)
  @company = company if company.is_a?(Colppy::Company)

  @id = params.delete(:id)
  @product = params.delete(:product)
  @data = rename_params_hash(params, ATTRIBUTES_MAPPER, DATA_KEYS_SETTERS)

  if @product && @product.is_a?(Colppy::Product)
    @data[:product_id] = @product.id
  else
    @product = nil
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



70
71
72
# File 'lib/colppy/resources/invoice.rb', line 70

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



70
71
72
# File 'lib/colppy/resources/invoice.rb', line 70

def id
  @id
end

Instance Method Details

#chargedObject



133
134
135
136
137
138
139
# File 'lib/colppy/resources/invoice.rb', line 133

def charged
  if (percentage = discount_percentage.to_f) > 0
    unit_price * ((100 - percentage) / 100)
  else
    unit_price
  end
end

#codeObject



155
156
157
# File 'lib/colppy/resources/invoice.rb', line 155

def code
  (@data[:code] || product.code || "")
end

#commentObject



167
168
169
# File 'lib/colppy/resources/invoice.rb', line 167

def comment
  (@data[:comment] || "#{product.name}, #{product.detail}"  || "")
end

#inspectObject



192
193
194
# File 'lib/colppy/resources/invoice.rb', line 192

def inspect
  "#<#{self.class.name} product_id:#{@data[:product_id]} >"
end

#item_typeObject



152
153
154
# File 'lib/colppy/resources/invoice.rb', line 152

def item_type
  (@data[:item_type] || product.item_type || "P")
end

#measure_unitObject



161
162
163
# File 'lib/colppy/resources/invoice.rb', line 161

def measure_unit
  (@data[:measure_unit] || product.measure_unit || "u")
end

#minimum_stockObject



149
150
151
# File 'lib/colppy/resources/invoice.rb', line 149

def minimum_stock
  (@data[:minimum_stock] || product.minimum_stock || 0)
end

#nameObject



158
159
160
# File 'lib/colppy/resources/invoice.rb', line 158

def name
  (@data[:name] || product.name || "")
end

#productObject



118
119
120
# File 'lib/colppy/resources/invoice.rb', line 118

def product
  @product ||= @company.product(@data[:product_id])
end

#product_idObject



126
127
128
# File 'lib/colppy/resources/invoice.rb', line 126

def product_id
  @data[:product_id]
end

#product_id=(value) ⇒ Object



122
123
124
125
# File 'lib/colppy/resources/invoice.rb', line 122

def product_id=(value)
  @product = nil
  @data[:product_id] = value
end

#quantityObject



143
144
145
# File 'lib/colppy/resources/invoice.rb', line 143

def quantity
  @data[:quantity] || 0
end

#sales_account_idObject



164
165
166
# File 'lib/colppy/resources/invoice.rb', line 164

def 
  (@data[:sales_account_id] || product. || "")
end

#save_parametersObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/colppy/resources/invoice.rb', line 171

def save_parameters
  {
    idItem: product_id.to_s,
    minimo: minimum_stock.to_s,
    tipoItem: item_type,
    codigo: code,
    Descripcion: name,
    ccosto1: unhandle_data[:ccosto1] || "",
    ccosto2: unhandle_data[:ccosto2] || "",
    almacen: unhandle_data[:almacen] || "",
    unidadMedida: measure_unit,
    Cantidad: quantity,
    ImporteUnitario: unit_price.round(2),
    porcDesc: discount_percentage || 0,
    IVA: tax.to_s,
    subtotal: total_charged,
    idPlanCuenta: ,
    Comentario: comment
  }
end

#taxObject



130
131
132
# File 'lib/colppy/resources/invoice.rb', line 130

def tax
  (@data[:tax] || product.tax || 21).to_f
end

#total_chargedObject



146
147
148
# File 'lib/colppy/resources/invoice.rb', line 146

def total_charged
  ( charged * quantity ).round(2)
end

#unhandle_dataObject



114
115
116
# File 'lib/colppy/resources/invoice.rb', line 114

def unhandle_data
  @data[:unhandle] || {}
end

#unit_priceObject



140
141
142
# File 'lib/colppy/resources/invoice.rb', line 140

def unit_price
  (@data[:unit_price] || product.sell_price || 0).to_f
end