Class: Billingly::Invoice

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

Instance Method Summary collapse

Instance Method Details

#chargeObject

Settle an invoice by moving money from the cash balance into an expense, after charged the invoice becomes paid.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/billingly/invoice.rb', line 18

def charge
  return if paid?
  return unless deleted_on.nil?
  return if period_start > Time.now
  return if customer.ledger[:cash] < amount
  
  update_attribute(:paid_on, Time.now)
  extra = {invoice: self, subscription: subscription}
  customer.add_to_journal(-(amount), :cash, extra)
  customer.add_to_journal(amount, :spent, extra)
  
  return self
end

#deleted?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'app/models/billingly/invoice.rb', line 12

def deleted?
  not deleted_on.nil?
end

#notify_overdueObject



68
69
70
71
72
73
74
75
76
# File 'app/models/billingly/invoice.rb', line 68

def notify_overdue
  return unless notified_overdue_on.nil?
  return if paid?
  return if deleted?
  return if due_on > Time.now
  return if customer.do_not_email?
  Billingly::Mailer.overdue_notification(self).deliver!
  update_attribute(:notified_overdue_on, Time.now)
end

#notify_paidObject



78
79
80
81
82
83
84
85
# File 'app/models/billingly/invoice.rb', line 78

def notify_paid
  return unless paid?
  return unless notified_paid_on.nil?
  return if deleted?
  return if customer.do_not_email?
  Billingly::Mailer.paid_notification(self).deliver!
  update_attribute(:notified_paid_on, Time.now)
end

#notify_pendingObject



58
59
60
61
62
63
64
65
66
# File 'app/models/billingly/invoice.rb', line 58

def notify_pending
  return unless notified_pending_on.nil?
  return if paid?
  return if deleted?
  return if customer.do_not_email?
  return if due_on > subscription.grace_period.from_now
  Billingly::Mailer.pending_notification(self).deliver!
  update_attribute(:notified_pending_on, Time.now)
end

#paid?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'app/models/billingly/invoice.rb', line 8

def paid?
  not paid_on.nil?
end

#truncateObject

When a subscription terminates, it’s last invoice gets truncated so that it does not extend beyond the duration of the subscription.



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/billingly/invoice.rb', line 34

def truncate
  return if Time.now > self.period_end
  old_amount = self.amount
  new_amount = if self.period_start < Time.now 
    whole_period = self.period_end - self.period_start
    used_period = Time.now - self.period_start
    self.period_end = Time.now
    used_period.round * old_amount / whole_period.round
  else
    self.deleted_on = Time.now
    0
  end
  self.amount = BigDecimal.new(new_amount.round(2).to_s)
  if paid?
    reimburse = (old_amount - self.amount).round(2)
    extra = {invoice: self, subscription: subscription}
    customer.add_to_journal(reimburse, :cash, extra)
    customer.add_to_journal(-(reimburse), :spent, extra)
  end
  save!
  self.charge
  return self
end