Class: Dolar::Bna::DolarCotization

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/dolar/app/models/dolar_cotization.rb

Constant Summary collapse

DOLAR_TYPES =

validates_uniqueness_of :date, scope: [:dolar_type, :dolar_sell], if: :message: ‘Ya existe una cotización de ese tipo para la fecha ingresada’

["Divisa", "Billete", "Blue", "MEP", "CCL", "Tarjeta", "Agro"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.search_by_date(search) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/dolar/app/models/dolar_cotization.rb', line 28

def self.search_by_date search
  unless search.blank?
    where(date: search)
  else
    all
  end
end

.search_by_date_range(search) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/dolar/app/models/dolar_cotization.rb', line 18

def self.search_by_date_range search
  unless search.blank?
    # La fecha la pasa un daterangepicker que la envia en este formato '01/01/2023 - 31/12/2023' 
    first_date, last_date = search.split(" - ")
    where(date: first_date .. last_date)
  else
    all
  end
end

.search_by_dolar_type(search) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/dolar/app/models/dolar_cotization.rb', line 36

def self.search_by_dolar_type search
  unless search.blank?
    where(dolar_type: search)
  else
    all
  end
end

Instance Method Details

#dolar_agro_changer?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/dolar/app/models/dolar_cotization.rb', line 44

def dolar_agro_changer?
  dolar_type == 'MEP' || dolar_type == 'Divisa'
end

#set_dolar_agroObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/dolar/app/models/dolar_cotization.rb', line 48

def set_dolar_agro
  first_dolar_multiplier = (dolar_type == 'MEP' ? 0.2 : 0.8)
  second_dolar_multiplier = (dolar_type == 'MEP' ? 0.8 : 0.2)
  other_dolar_to_calc = Dolar::Bna::DolarCotization.where(dolar_type: dolar_type == 'MEP' ? 'Divisa' : 'MEP', date: self.date).order(date: :asc, created_at: :asc).last
  if other_dolar_to_calc
    dolar_buy_average = ((self.dolar_buy.to_f * first_dolar_multiplier) + (other_dolar_to_calc.dolar_buy.to_f * second_dolar_multiplier)).round(2)
    dolar_sell_average = ((self.dolar_sell.to_f * first_dolar_multiplier) + (other_dolar_to_calc.dolar_sell.to_f * second_dolar_multiplier)).round(2)
    Dolar::Bna::DolarCotization.where(dolar_type: 'Agro', dolar_buy: dolar_buy_average, dolar_sell: dolar_sell_average, date: self.date).first_or_create
  end
end