Class: XrateAverage

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/xrate_average.rb

Overview

== Schema Information

Table name: xrate_averages
Database name: primary

id :integer not null, primary key
cad_to_usd :float not null
effective_date :date not null
usd_to_cad :float not null
created_at :datetime not null
updated_at :datetime not null
creator_id :integer
updater_id :integer

Indexes

index_xrate_averages_on_effective_date (effective_date) UNIQUE

Constant Summary

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#cad_to_usdFloat

Returns CAD to USD exchange rate.

Returns:

  • (Float)

    CAD to USD exchange rate.



30
# File 'app/models/xrate_average.rb', line 30

validates :effective_date, :usd_to_cad, :cad_to_usd, presence: true

#effective_dateDate

Returns Date the rate applies to.

Returns:

  • (Date)

    Date the rate applies to.



30
# File 'app/models/xrate_average.rb', line 30

validates :effective_date, :usd_to_cad, :cad_to_usd, presence: true

#usd_to_cadFloat

Returns USD to CAD exchange rate.

Returns:

  • (Float)

    USD to CAD exchange rate.



30
# File 'app/models/xrate_average.rb', line 30

validates :effective_date, :usd_to_cad, :cad_to_usd, presence: true

Class Method Details

.daily_insert_xrate_averagevoid

This method returns an undefined value.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/xrate_average.rb', line 33

def self.daily_insert_xrate_average
  current_start_date = Date.current.beginning_of_month
  current_end_date = Date.current.end_of_month
  start_date = Date.current.day == 1 ? Date.current.days_ago(1).beginning_of_month : Date.current.beginning_of_month
  end_date = start_date.end_of_month

  sql = <<-SQL.squish
    delete
    from xrate_averages
    where effective_date between '#{current_start_date}' and '#{current_end_date}';

    insert into xrate_averages (effective_date,usd_to_cad,cad_to_usd,created_at,updated_at,creator_id,updater_id)
    select current_date as effective_date,coalesce(round((1 / avg(distinct consolidated_exchange_rate))::numeric,6),0) as usd_to_cad,
          coalesce(round(avg(distinct consolidated_exchange_rate)::numeric,6),0) as cad_to_usd,current_date as created_at,current_date as updated_at,
          null as creator_id,null as updater_id
    from invoices
    where invoice_type = 'SO'
    and state = 'paid'
    and currency = 'CAD'
    and timezone('America/Chicago', timestamptz(created_at))::date between '#{start_date}' and '#{end_date}';
  SQL

  ActiveRecord::Base.lease_connection.execute(sql)
end

.sortedActiveRecord::Relation<XrateAverage>

A relation of XrateAverages that are sorted. Active Record Scope

Returns:

See Also:



22
# File 'app/models/xrate_average.rb', line 22

scope :sorted, -> { order(:effective_date) }