Class: MercadoPublicoCl::Resources::PurchaseOrder

Inherits:
Data
  • Object
show all
Extended by:
Base::ClassMethods
Defined in:
lib/mercado_publico_cl/resources/purchase_order.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base::ClassMethods

client, detail_payload, extract_list, fetch_list, fetch_single, list_key

Instance Attribute Details

#buyerObject (readonly)

Returns the value of attribute buyer

Returns:

  • (Object)

    the current value of buyer



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def buyer
  @buyer
end

#charge_amountObject (readonly)

Returns the value of attribute charge_amount

Returns:

  • (Object)

    the current value of charge_amount



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def charge_amount
  @charge_amount
end

#codeObject (readonly)

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def code
  @code
end

#countryObject (readonly)

Returns the value of attribute country

Returns:

  • (Object)

    the current value of country



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def country
  @country
end

#currencyObject (readonly)

Returns the value of attribute currency

Returns:

  • (Object)

    the current value of currency



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def currency
  @currency
end

#currency_codeObject (readonly)

Returns the value of attribute currency_code

Returns:

  • (Object)

    the current value of currency_code



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def currency_code
  @currency_code
end

#datesObject (readonly)

Returns the value of attribute dates

Returns:

  • (Object)

    the current value of dates



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def dates
  @dates
end

#descriptionObject (readonly)

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def description
  @description
end

#discount_amountObject (readonly)

Returns the value of attribute discount_amount

Returns:

  • (Object)

    the current value of discount_amount



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def discount_amount
  @discount_amount
end

#dispatch_typeObject (readonly)

Returns the value of attribute dispatch_type

Returns:

  • (Object)

    the current value of dispatch_type



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def dispatch_type
  @dispatch_type
end

#dispatch_type_codeObject (readonly)

Returns the value of attribute dispatch_type_code

Returns:

  • (Object)

    the current value of dispatch_type_code



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def dispatch_type_code
  @dispatch_type_code
end

#funding_sourceObject (readonly)

Returns the value of attribute funding_source

Returns:

  • (Object)

    the current value of funding_source



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def funding_source
  @funding_source
end

#has_itemsObject (readonly)

Returns the value of attribute has_items

Returns:

  • (Object)

    the current value of has_items



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def has_items
  @has_items
end

#itemsObject (readonly)

Returns the value of attribute items

Returns:

  • (Object)

    the current value of items



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def items
  @items
end

#iva_percentageObject (readonly)

Returns the value of attribute iva_percentage

Returns:

  • (Object)

    the current value of iva_percentage



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def iva_percentage
  @iva_percentage
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def name
  @name
end

#net_amountObject (readonly)

Returns the value of attribute net_amount

Returns:

  • (Object)

    the current value of net_amount



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def net_amount
  @net_amount
end

#payment_typeObject (readonly)

Returns the value of attribute payment_type

Returns:

  • (Object)

    the current value of payment_type



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def payment_type
  @payment_type
end

#payment_type_codeObject (readonly)

Returns the value of attribute payment_type_code

Returns:

  • (Object)

    the current value of payment_type_code



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def payment_type_code
  @payment_type_code
end

#purchase_order_typeObject (readonly)

Returns the value of attribute purchase_order_type

Returns:

  • (Object)

    the current value of purchase_order_type



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def purchase_order_type
  @purchase_order_type
end

#purchase_order_type_codeObject (readonly)

Returns the value of attribute purchase_order_type_code

Returns:

  • (Object)

    the current value of purchase_order_type_code



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def purchase_order_type_code
  @purchase_order_type_code
end

#purchase_order_type_labelObject (readonly)

Returns the value of attribute purchase_order_type_label

Returns:

  • (Object)

    the current value of purchase_order_type_label



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def purchase_order_type_label
  @purchase_order_type_label
end

#ratingObject (readonly)

Returns the value of attribute rating

Returns:

  • (Object)

    the current value of rating



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def rating
  @rating
end

#rating_countObject (readonly)

Returns the value of attribute rating_count

Returns:

  • (Object)

    the current value of rating_count



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def rating_count
  @rating_count
end

#rawObject (readonly)

Returns the value of attribute raw

Returns:

  • (Object)

    the current value of raw



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def raw
  @raw
end

#statusObject (readonly)

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def status
  @status
end

#status_codeObject (readonly)

Returns the value of attribute status_code

Returns:

  • (Object)

    the current value of status_code



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def status_code
  @status_code
end

#status_labelObject (readonly)

Returns the value of attribute status_label

Returns:

  • (Object)

    the current value of status_label



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def status_label
  @status_label
end

#supplier_statusObject (readonly)

Returns the value of attribute supplier_status

Returns:

  • (Object)

    the current value of supplier_status



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def supplier_status
  @supplier_status
end

#supplier_status_codeObject (readonly)

Returns the value of attribute supplier_status_code

Returns:

  • (Object)

    the current value of supplier_status_code



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def supplier_status_code
  @supplier_status_code
end

#supplier_status_labelObject (readonly)

Returns the value of attribute supplier_status_label

Returns:

  • (Object)

    the current value of supplier_status_label



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def supplier_status_label
  @supplier_status_label
end

#tax_amountObject (readonly)

Returns the value of attribute tax_amount

Returns:

  • (Object)

    the current value of tax_amount



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def tax_amount
  @tax_amount
end

#tender_codeObject (readonly)

Returns the value of attribute tender_code

Returns:

  • (Object)

    the current value of tender_code



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def tender_code
  @tender_code
end

#total_amountObject (readonly)

Returns the value of attribute total_amount

Returns:

  • (Object)

    the current value of total_amount



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def total_amount
  @total_amount
end

#vendorObject (readonly)

Returns the value of attribute vendor

Returns:

  • (Object)

    the current value of vendor



5
6
7
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 5

def vendor
  @vendor
end

Class Method Details

.allObject



55
56
57
58
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 55

def self.all
  query = Query.new(resource: :purchase_order, params: { status: :all })
  fetch_list(query.to_h)
end

.endpointObject



44
45
46
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 44

def self.endpoint
  "/servicios/v1/publico/ordenesdecompra.json"
end

.extract_attributes(raw) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 71

def self.extract_attributes(raw)
  status_code = Base.to_int(raw["CodigoEstado"])
  supplier_status_code = Base.to_int(raw["CodigoEstadoProveedor"])
  po_type_code = Base.to_int(raw["CodigoTipo"])
  currency_code = raw["TipoMoneda"] || raw["Moneda"]
  dispatch_code = Base.to_int(raw["TipoDespacho"] || raw["CodigoTipoDespacho"])
  payment_code = Base.to_int(raw["FormaPago"] || raw["CodigoTipoPago"])

  {
    code: raw["Codigo"] || raw["CodigoExterno"],
    name: raw["Nombre"],
    description: raw["Descripcion"],
    status: Enums::PurchaseOrderStatus.to_symbol(status_code),
    status_code: status_code,
    status_label: raw["Estado"],
    supplier_status: Enums::SupplierOrderStatus.to_symbol(supplier_status_code),
    supplier_status_code: supplier_status_code,
    supplier_status_label: raw["EstadoProveedor"],
    tender_code: presence(raw["CodigoLicitacion"]),
    purchase_order_type: Enums::PurchaseOrderType.to_symbol(po_type_code),
    purchase_order_type_code: po_type_code,
    purchase_order_type_label: raw["Tipo"],
    currency: Enums::Currency.to_symbol(currency_code),
    currency_code: currency_code,
    total_amount: Base.to_float(raw["Total"] || raw["MontoTotal"]),
    net_amount: Base.to_float(raw["TotalNeto"]),
    tax_amount: Base.to_float(raw["Impuestos"]),
    iva_percentage: Base.to_float(raw["PorcentajeIva"]),
    discount_amount: Base.to_float(raw["Descuentos"]),
    charge_amount: Base.to_float(raw["Cargos"]),
    dispatch_type: Enums::DispatchType.to_symbol(dispatch_code),
    dispatch_type_code: dispatch_code,
    payment_type: Enums::PurchaseOrderPaymentMethod.to_symbol(payment_code),
    payment_type_code: payment_code,
    funding_source: raw["Financiamiento"],
    country: raw["Pais"],
    has_items: Base.to_bool(raw["TieneItems"]),
    rating: Base.to_float(raw["PromedioCalificacion"]),
    rating_count: Base.to_int(raw["CantidadEvaluacion"]),
    buyer: POBuyer.from_payload(raw["Comprador"]),
    vendor: POVendor.from_payload(raw["Proveedor"]),
    items: Array(raw.dig("Items", "Listado")).map { |item| POItem.from_payload(item) },
    dates: PODates.from_payload(raw["Fechas"]),
    raw: raw
  }
end

.find(code) ⇒ Object

Raises:



48
49
50
51
52
53
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 48

def self.find(code)
  raise InvalidQueryError, "code is required" if code.nil? || code.to_s.strip.empty?

  query = Query.new(resource: :purchase_order, params: { code: code })
  fetch_single(query.to_h)
end

.from_payload(raw) ⇒ Object



67
68
69
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 67

def self.from_payload(raw)
  new(**extract_attributes(raw))
end

.presence(value) ⇒ Object



118
119
120
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 118

def self.presence(value)
  value.nil? || value.to_s.empty? ? nil : value
end

.where(date: nil, status: nil, supplier_code: nil, agency_code: nil) ⇒ Object



60
61
62
63
64
65
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 60

def self.where(date: nil, status: nil, supplier_code: nil, agency_code: nil)
  params = { date: date, status: status, supplier_code: supplier_code, agency_code: agency_code }
           .reject { |_, v| v.nil? }
  query = Query.new(resource: :purchase_order, params: params)
  fetch_list(query.to_h)
end

Instance Method Details

#acceptance_dateObject



150
151
152
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 150

def acceptance_date
  dates&.acceptance
end

#agency_codeObject



134
135
136
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 134

def agency_code
  buyer&.code
end

#agency_nameObject



138
139
140
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 138

def agency_name
  buyer&.name
end

#cancellation_dateObject



154
155
156
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 154

def cancellation_date
  dates&.cancellation
end

#creation_dateObject



142
143
144
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 142

def creation_date
  dates&.creation
end

#has_items?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 122

def has_items?
  has_items == true
end

#inspectObject Also known as: to_s



166
167
168
169
170
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 166

def inspect
  "#<MercadoPublicoCl::PurchaseOrder code=#{code.inspect} status=#{status.inspect} " \
    "type=#{purchase_order_type.inspect} total=#{total_amount.inspect} " \
    "supplier=#{supplier_name.inspect} agency=#{agency_name.inspect}>"
end

#last_modified_atObject



158
159
160
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 158

def last_modified_at
  dates&.last_modified
end

#sent_dateObject



146
147
148
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 146

def sent_date
  dates&.sent
end

#summary?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 162

def summary?
  raw["Comprador"].nil? && raw["Proveedor"].nil? && raw["Items"].nil? && raw["Fechas"].nil?
end

#supplier_codeObject



126
127
128
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 126

def supplier_code
  vendor&.code
end

#supplier_nameObject



130
131
132
# File 'lib/mercado_publico_cl/resources/purchase_order.rb', line 130

def supplier_name
  vendor&.name
end