Class: FinancialTxn

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/financial_txn.rb

Overview

Table Definition ########################### create_table :financial_txns do |t| t.integer :money_id t.date :apply_date

t.timestamps

end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_filters(filters, statement = nil) ⇒ ActiveRecord::Relation

Filter records

Parameters:

  • filters (Hash) —

    a hash of filters to be applied,

  • statement (ActiveRecord::Relation) (defaults to: nil) —

    the query being built

Returns:

  • (ActiveRecord::Relation) —

    the query being built



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/financial_txn.rb', line 24

def apply_filters(filters, statement=nil)
  unless statement
    statement = FinancialTxn
  end

  statement = statement.joins(:biz_txn_event)

  biz_txn_event_tbl = BizTxnEvent.arel_table

  # filter by query which will filter on description
  if filters[:query]
    statement = statement.where('biz_txn_events.description like ?', "%#{filters[:query].strip}%")
  end

  # filter by WorkEffortType
  unless filters[:biz_txn_type_iids].blank?
    statement = statement.where(biz_txn_events: {biz_txn_type_id: BizTxnType.where(internal_identifier: filters[:biz_txn_type_iids])})
  end

  # filter by start_at
  unless filters[:start_date].blank?
    statement = statement.where(biz_txn_event_tbl[:entered_date].gteq(Time.parse(filters[:start_date])))
  end

  # filter by end_at
  unless filters[:end_date].blank?
    statement = statement.where(biz_txn_event_tbl[:entered_date].lteq(Time.parse(filters[:end_date])))
  end

  unless filters[:parties].blank?
    data = JSON.parse(filters[:parties])

    statement = statement.scope_by_party(data['party_ids'].split(','), {role_types: data['role_types']})
  end

  statement
end

.scope_by_dba_organization(dba_organization) ⇒ ActiveRecord::Relation Also known as: scope_by_dba

scope by dba organization

Parameters:

  • dba_organization (Party) —

    dba organization to scope by

Returns:

  • (ActiveRecord::Relation)


71
72
73
74
75
76
77
78
# File 'app/models/financial_txn.rb', line 71

def scope_by_dba_organization(dba_organization)
  dba_org_role_type = BizTxnPartyRoleType.find_or_create('dba_org', 'DBA Organization')

  joins(:biz_txn_event)
      .joins("inner join biz_txn_party_roles on biz_txn_party_roles.biz_txn_event_id = biz_txn_events.id")
      .where('biz_txn_party_roles.party_id' => dba_organization)
      .where('biz_txn_party_roles.biz_txn_party_role_type_id' => dba_org_role_type)
end

.scope_by_party(party, options = {}) ⇒ ActiveRecord::Relation

scope by party

or an array of Party ids comma separated or an Array

Parameters:

  • party (Integer | Party | Array) —

    either a id of Party record, a Party record, an array of Party records

  • options (Hash) (defaults to: {}) —

    options to apply to this scope

Options Hash (options):

  • :role_types (String | Array) —

    BizTxnPartyRoleType internal identifiers to include in the scope,

Returns:

  • (ActiveRecord::Relation)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/financial_txn.rb', line 91

def scope_by_party(party, options={})
  statement = joins(:biz_txn_event)
                  .joins("inner join biz_txn_party_roles on biz_txn_party_roles.biz_txn_event_id = biz_txn_events.id")
                  .where("biz_txn_party_roles.party_id" => party).uniq

  if options[:role_types]
    role_types = options[:role_types]
    unless role_types.is_a? Array
      role_types = role_types.split(',')
    end

    statement = statement.joins("inner join biz_txn_party_role_types
                                 on biz_txn_party_role_types.id = biz_txn_party_roles.biz_txn_party_role_type_id")
                    .where(biz_txn_party_role_types: {internal_identifier: role_types})
  end

  statement
end

Instance Method Details

#to_data_hash ⇒ Hash

converts this record a hash data representation

Returns:

  • (Hash)


115
116
117
118
119
120
121
# File 'app/models/financial_txn.rb', line 115

def to_data_hash
  data = to_hash(only: [:id, :description, :apply_date])

  data[:amount] = self.money.nil? ? 0 : self.money.amount

  data
end