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

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::EventPublishable

#publish_event

Class Method Details

.daily_insert_xrate_averageObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/xrate_average.rb', line 26

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

  sql = <<-SQL
    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.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) }