Class: BillingAccount

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_account_number(account_number) ⇒ Object



20
21
22
23
# File 'app/models/billing_account.rb', line 20

def self.()
  #self.includes(:financial_txn_account).where(:financial_txn_accounts => {:account_number => account_number.to_s}).first
  self.where(:account_number => .to_s).first
end

Instance Method Details

#all_documentsObject



29
30
31
# File 'app/models/billing_account.rb', line 29

def all_documents
  (self.invoices.collect(&:document) | self.documents).flatten
end

#balanceObject



98
99
100
# File 'app/models/billing_account.rb', line 98

def balance
  self..balance.amount
end

#balance=(amount, currency = Currency.usd) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/billing_account.rb', line 102

def balance=(amount, currency=Currency.usd)
  if amount.is_a?(Array)
    currency = amount.last
    amount = amount.first
  end
  if self..balance
    self..balance.amount = amount
  else
    self..balance = Money.create(:amount => amount, :currency => currency)
  end
  self..balance.save
end

#balance_dateObject

override balance_date for today if calculate_balance is set to true



133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/billing_account.rb', line 133

def balance_date
  if self.calculate_balance_strategy_type.nil?
    self..balance_date
  else
    if self.calculate_balance_strategy_type.iid == 'invoices_and_payments' and self.invoices.empty?
      current_invoice.invoice_date
    else
      self..balance_date
    end
  end
end

#billing_dateObject



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

def billing_date
  unless self.invoices.empty?
    current_invoice.invoice_date
  else
    self.attributes['billing_date']
  end
end

#calculate_balanceObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/billing_account.rb', line 50

def calculate_balance
  unless self.calculate_balance_strategy_type.nil?
    case self.calculate_balance_strategy_type.internal_identifier
      when 'invoices_and_payments'
        (self.invoices.all.sum(&:calculate_balance) - self.total_payments)
      when 'payments'
        balance_amt = (self.balance - self.total_payments)
        balance_amt == 0 ? 0 : balance_amt.round(2)
      else
        self.balance == 0 ? 0 : self.balance.round(2)
    end
  else
    self.balance == 0 ? 0 : self.balance.round(2)
  end
end

#current_invoiceObject



145
146
147
# File 'app/models/billing_account.rb', line 145

def current_invoice
  self.invoices.by_invoice_date.last
end

#due_dateObject

override due_date for invoice.invoice_date



124
125
126
127
128
129
130
# File 'app/models/billing_account.rb', line 124

def due_date
  unless self.invoices.empty?
    current_invoice.due_date
  else
    self..due_date
  end
end

#get_payment_applications(status = :all) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/billing_account.rb', line 33

def get_payment_applications(status=:all)
  selected_payment_applications = case status.to_sym
  when :pending
    self.payment_applications.pending
  when :successful
    self.payment_applications.successful
  when :all
    self.payment_applications
  end

  unless self.invoices.empty?
    selected_payment_applications = (selected_payment_applications | self.invoices.collect{|item| item.get_payment_applications(status)}).flatten! unless (self.invoices.collect{|item| item.get_payment_applications(status)}.empty?)
  end

  selected_payment_applications
end

#has_outstanding_balance?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/billing_account.rb', line 66

def has_outstanding_balance?
  (outstanding_balance > 0)
end

#has_recurring_payment_enabled?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/models/billing_account.rb', line 25

def has_recurring_payment_enabled?
  !self.recurring_payment.nil? and self.recurring_payment.enabled
end

#outstanding_balanceObject



70
71
72
73
# File 'app/models/billing_account.rb', line 70

def outstanding_balance
   outstanding_balance_amt = (calculate_balance - total_pending_payments)
   outstanding_balance_amt == 0 ? 0 : outstanding_balance_amt.round(2)
end

#payment_dueObject

payment due is determined by last invoice



76
77
78
79
80
81
82
# File 'app/models/billing_account.rb', line 76

def payment_due
  if !self.calculate_balance_strategy_type.nil? and self.calculate_balance_strategy_type.iid == 'invoices_and_payments' and !self.invoices.empty?
    self.current_invoice.payment_due
  else
    self..payment_due.amount
  end
end

#payment_due=(amount, currency = Currency.usd) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/billing_account.rb', line 84

def payment_due=(amount, currency=Currency.usd)
  currency = Currency.usd
  if amount.is_a?(Array)
    currency = amount.last
    amount = amount.first
  end
  if self..payment_due
    self..payment_due.amount = amount
  else
    self..payment_due = Money.create(:amount => amount, :currency => currency)
  end
  self..payment_due.save
end

#send_email_notificationObject



200
201
202
203
204
205
# File 'app/models/billing_account.rb', line 200

def send_email_notification
  primary_party = self.find_parties_by_role('primary').first
  unless primary_party.billing_email_address.nil?
    #send email
  end
end

#send_sms_notificationObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/models/billing_account.rb', line 149

def send_sms_notification
  primary_party = self.find_parties_by_role('primary').first
  from_party = Party.find_by_description('Compass AE')
  from_number = from_party.default_phone_number
  to_number = primary_party.billing_phone_number

  # prevent multiple sms notifications being sent within the time window
  previous_cmm_evt = CommunicationEvent.find_by_sql("SELECT * FROM communication_events
                                  JOIN phone_numbers from_phone ON from_contact_mechanism_id=from_phone.id
                                  JOIN phone_numbers to_phone ON to_contact_mechanism_id=to_phone.id
                                  WHERE from_contact_mechanism_type = 'PhoneNumber' 
                                  AND from_contact_mechanism_type='PhoneNumber'
                                  AND from_phone.phone_number = '#{from_number.phone_number.to_s}'
                                  AND (to_phone.phone_number = '#{to_number.phone_number.to_s}' OR to_phone.phone_number = '#{to_number.phone_number[1..to_number.phone_number.length]}')
                                  AND communication_events.created_at > '#{SMS_TIME_WINDOW.minutes.ago.to_s}'
                                  ORDER BY communication_events.created_at DESC").first

  Rails.logger.info 'not sending sms notification, one has already been sent within time window' if !previous_cmm_evt.nil?

  unless primary_party.billing_phone_number.nil? or !previous_cmm_evt.nil?
    message = SMS_NOTIFICATION_MESSAGE.gsub('payment_due',self.payment_due.to_s)


    # get cmm event purpose type
    sms_purpose = CommEvtPurposeType.find_by_internal_identifier('sms_notification')

    # create cmm event
    cmm_evt = CommunicationEvent.new
    cmm_evt.short_description = 'SMS Notification'
    cmm_evt.from_role = RoleType.find_by_internal_identifier('application')
    cmm_evt.from_party = from_party
    cmm_evt.from_contact_mechanism = from_number
    cmm_evt.comm_evt_purpose_types << sms_purpose
    cmm_evt.to_contact_mechanism = to_number
    cmm_evt.to_role = RoleType.find_by_internal_identifier('customer')
    cmm_evt.to_party = primary_party
    cmm_evt.case_id = self.id
    cmm_evt.notes = "From Number: #{from_number}, To Number: #{to_number}, Message: #{message}"

    clikatell = ErpTechSvcs::SmsWrapper::Clickatell.new
    cmm_evt.external_identifier = clikatell.send_message(to_number.phone_number, message, :mo => 1, :from => from_number.phone_number)

    unless cmm_evt.external_identifier.nil?
      cmm_evt.save
      return true
    else
      return false
    end
  end
end