- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 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
class FinancialEventObserver < ActiveRecord::Observer
observe Payment, Invoice
def before_save(model)
event = nil
if model.class == Payment
if model.new_record?
event = FinancialEvent.new(:event => FinancialEvent::Event::PAYMENT_INVOICE,
:arguments => {:client_name => model.invoice.client.short_name, :invoice_number => model.invoice.invoice_number},
:company_id=>model.invoice.client.company.id)
end
elsif model.class == Invoice
i = Invoice.find_by_id model.id
if model.new_record? or i.status != model.status
if model.status == Invoice::Status::ESTIMATE
event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_SEND,
:arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
elsif model.status == Invoice::Status::APPROVED
event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_APPROVED,
:arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
elsif model.status == Invoice::Status::REJECTED
event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_REJECTED,
:arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
elsif model.status == Invoice::Status::SEND
event = FinancialEvent.new(:event => FinancialEvent::Event::INVOICE_SEND,
:arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
end
elsif !model.new_record? and i.state != model.state
if model.state == Invoice::State::DELETED
event = FinancialEvent.new(:event => FinancialEvent::Event::INVOICE_DELETED,
:arguments => {:invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
end
end
end
event.eventable = model.requester unless event.blank?
event.save unless event.blank?
end
def before_destroy(model)
if model.class == Payment
event = FinancialEvent.new(:event => FinancialEvent::Event::PAYMENT_DELETED,
:arguments => {:invoice_number => model.invoice.invoice_number},
:company_id=>model.invoice.client.company.id)
event.eventable = model.requester
event.save
end
end
end