Class: BillingAccount
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- BillingAccount
- Defined in:
- app/models/billing_account.rb
Class Method Summary collapse
Instance Method Summary collapse
- #all_documents ⇒ Object
- #balance ⇒ Object
- #balance=(amount, currency = Currency.usd) ⇒ Object
-
#balance_date ⇒ Object
override balance_date for today if calculate_balance is set to true.
- #billing_date ⇒ Object
- #calculate_balance ⇒ Object
- #current_invoice ⇒ Object
-
#due_date ⇒ Object
override due_date for invoice.invoice_date.
- #get_payment_applications(status = :all) ⇒ Object
- #has_outstanding_balance? ⇒ Boolean
- #has_payments?(status) ⇒ Boolean
- #has_recurring_payment_enabled? ⇒ Boolean
- #outstanding_balance ⇒ Object
-
#payment_due ⇒ Object
payment due is determined by last invoice.
- #payment_due=(amount, currency = Currency.usd) ⇒ Object
- #send_email_notification ⇒ Object
- #send_sms_notification ⇒ Object
- #total_payments ⇒ Object
- #total_pending_payments ⇒ Object
Class Method Details
.find_by_account_number(account_number) ⇒ Object
26 27 28 29 |
# File 'app/models/billing_account.rb', line 26 def self.find_by_account_number(account_number) #self.includes(:financial_txn_account).where(:financial_txn_accounts => {:account_number => account_number.to_s}).first self.where(:account_number => account_number.to_s).first end |
Instance Method Details
#all_documents ⇒ Object
40 41 42 |
# File 'app/models/billing_account.rb', line 40 def all_documents (self.invoices.collect(&:document) | self.documents).flatten end |
#balance ⇒ Object
117 118 119 |
# File 'app/models/billing_account.rb', line 117 def balance self.financial_txn_account.balance.amount end |
#balance=(amount, currency = Currency.usd) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/billing_account.rb', line 121 def balance=(amount, currency=Currency.usd) if amount.is_a?(Array) currency = amount.last amount = amount.first end if self.financial_txn_account.balance self.financial_txn_account.balance.amount = amount else self.financial_txn_account.balance = Money.create(:amount => amount, :currency => currency) end self.financial_txn_account.balance.save end |
#balance_date ⇒ Object
override balance_date for today if calculate_balance is set to true
152 153 154 155 156 157 158 159 160 161 162 |
# File 'app/models/billing_account.rb', line 152 def balance_date if self.calculate_balance_strategy_type.nil? self.financial_txn_account.balance_date else if self.calculate_balance_strategy_type.iid == 'invoices_and_payments' and self.invoices.empty? current_invoice.invoice_date else self.financial_txn_account.balance_date end end end |
#billing_date ⇒ Object
134 135 136 137 138 139 140 |
# File 'app/models/billing_account.rb', line 134 def billing_date unless self.invoices.empty? current_invoice.invoice_date else self.attributes['billing_date'] end end |
#calculate_balance ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/models/billing_account.rb', line 61 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_invoice ⇒ Object
164 165 166 |
# File 'app/models/billing_account.rb', line 164 def current_invoice self.invoices.by_invoice_date.last end |
#due_date ⇒ Object
override due_date for invoice.invoice_date
143 144 145 146 147 148 149 |
# File 'app/models/billing_account.rb', line 143 def due_date unless self.invoices.empty? current_invoice.due_date else self.financial_txn_account.due_date end end |
#get_payment_applications(status = :all) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/billing_account.rb', line 44 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
77 78 79 |
# File 'app/models/billing_account.rb', line 77 def has_outstanding_balance? (outstanding_balance > 0) end |
#has_payments?(status) ⇒ Boolean
35 36 37 38 |
# File 'app/models/billing_account.rb', line 35 def has_payments?(status) selected_payment_applications = self.get_payment_applications(status) !(selected_payment_applications.nil? or selected_payment_applications.empty?) end |
#has_recurring_payment_enabled? ⇒ Boolean
31 32 33 |
# File 'app/models/billing_account.rb', line 31 def has_recurring_payment_enabled? !self.recurring_payment.nil? and self.recurring_payment.enabled end |
#outstanding_balance ⇒ Object
81 82 83 84 |
# File 'app/models/billing_account.rb', line 81 def outstanding_balance outstanding_balance_amt = (calculate_balance - total_pending_payments) outstanding_balance_amt == 0 ? 0 : outstanding_balance_amt.round(2) end |
#payment_due ⇒ Object
payment due is determined by last invoice
95 96 97 98 99 100 101 |
# File 'app/models/billing_account.rb', line 95 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.financial_txn_account.payment_due.amount end end |
#payment_due=(amount, currency = Currency.usd) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/billing_account.rb', line 103 def payment_due=(amount, currency=Currency.usd) currency = Currency.usd if amount.is_a?(Array) currency = amount.last amount = amount.first end if self.financial_txn_account.payment_due self.financial_txn_account.payment_due.amount = amount else self.financial_txn_account.payment_due = Money.create(:amount => amount, :currency => currency) end self.financial_txn_account.payment_due.save end |
#send_email_notification ⇒ Object
219 220 221 222 223 224 |
# File 'app/models/billing_account.rb', line 219 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_notification ⇒ Object
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'app/models/billing_account.rb', line 168 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? = 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: #{}" clikatell = ErpTechSvcs::SmsWrapper::Clickatell.new cmm_evt.external_identifier = clikatell.(to_number.phone_number, , :mo => 1, :from => from_number.phone_number) unless cmm_evt.external_identifier.nil? cmm_evt.save return true else return false end end end |
#total_payments ⇒ Object
90 91 92 |
# File 'app/models/billing_account.rb', line 90 def total_payments self.payment_applications.successful.sum{|item| item.money.amount} end |
#total_pending_payments ⇒ Object
86 87 88 |
# File 'app/models/billing_account.rb', line 86 def total_pending_payments self.payment_applications.pending.sum{|item| item.money.amount} end |