Class: Colppy::Invoice::Item
- Inherits:
-
Object
- Object
- Colppy::Invoice::Item
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
- #charged ⇒ Object
- #code ⇒ Object
- #comment ⇒ Object
-
#initialize(params, company = nil) ⇒ Item
constructor
A new instance of Item.
- #inspect ⇒ Object
- #item_type ⇒ Object
- #measure_unit ⇒ Object
- #minimum_stock ⇒ Object
- #name ⇒ Object
- #product ⇒ Object
- #product_id ⇒ Object
- #product_id=(value) ⇒ Object
- #quantity ⇒ Object
- #sales_account_id ⇒ Object
- #save_parameters ⇒ Object
- #tax ⇒ Object
- #total_charged ⇒ Object
- #unhandle_data ⇒ Object
- #unit_price ⇒ Object
Methods included from Utils
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
70 71 72 |
# File 'lib/colppy/resources/invoice.rb', line 70 def data @data end |
#id ⇒ Object (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
#charged ⇒ Object
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 |
#code ⇒ Object
155 156 157 |
# File 'lib/colppy/resources/invoice.rb', line 155 def code (@data[:code] || product.code || "") end |
#comment ⇒ Object
167 168 169 |
# File 'lib/colppy/resources/invoice.rb', line 167 def comment (@data[:comment] || "#{product.name}, #{product.detail}" || "") end |
#inspect ⇒ Object
192 193 194 |
# File 'lib/colppy/resources/invoice.rb', line 192 def inspect "#<#{self.class.name} product_id:#{@data[:product_id]} >" end |
#item_type ⇒ Object
152 153 154 |
# File 'lib/colppy/resources/invoice.rb', line 152 def item_type (@data[:item_type] || product.item_type || "P") end |
#measure_unit ⇒ Object
161 162 163 |
# File 'lib/colppy/resources/invoice.rb', line 161 def measure_unit (@data[:measure_unit] || product.measure_unit || "u") end |
#minimum_stock ⇒ Object
149 150 151 |
# File 'lib/colppy/resources/invoice.rb', line 149 def minimum_stock (@data[:minimum_stock] || product.minimum_stock || 0) end |
#name ⇒ Object
158 159 160 |
# File 'lib/colppy/resources/invoice.rb', line 158 def name (@data[:name] || product.name || "") end |
#product ⇒ Object
118 119 120 |
# File 'lib/colppy/resources/invoice.rb', line 118 def product @product ||= @company.product(@data[:product_id]) end |
#product_id ⇒ Object
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 |
#quantity ⇒ Object
143 144 145 |
# File 'lib/colppy/resources/invoice.rb', line 143 def quantity @data[:quantity] || 0 end |
#sales_account_id ⇒ Object
164 165 166 |
# File 'lib/colppy/resources/invoice.rb', line 164 def sales_account_id (@data[:sales_account_id] || product.sales_account || "") end |
#save_parameters ⇒ Object
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: sales_account_id, Comentario: comment } end |
#tax ⇒ Object
130 131 132 |
# File 'lib/colppy/resources/invoice.rb', line 130 def tax (@data[:tax] || product.tax || 21).to_f end |
#total_charged ⇒ Object
146 147 148 |
# File 'lib/colppy/resources/invoice.rb', line 146 def total_charged ( charged * quantity ).round(2) end |
#unhandle_data ⇒ Object
114 115 116 |
# File 'lib/colppy/resources/invoice.rb', line 114 def unhandle_data @data[:unhandle] || {} end |
#unit_price ⇒ Object
140 141 142 |
# File 'lib/colppy/resources/invoice.rb', line 140 def unit_price (@data[:unit_price] || product.sell_price || 0).to_f end |