Created
July 26, 2011 17:59
-
-
Save cmaujean/1107381 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Updates the +payment_state+ attribute according to the following logic: | |
# | |
# paid when +payment_total+ is equal to +total+ | |
# balance_due when +payment_total+ is less than +total+ | |
# credit_owed when +payment_total+ is greater than +total+ | |
# failed when most recent payment is in the failed state | |
# | |
# The +payment_state+ value helps with reporting, etc. since it provides a quick and easy way to locate Orders needing attention. | |
def update_payment_state | |
if round_money(payment_total) < round_money(total) | |
self.payment_state = "balance_due" | |
self.payment_state = "failed" if payments.present? and payments.last.state == "failed" | |
elsif round_money(payment_total) > round_money(total) | |
self.payment_state = "credit_owed" | |
else | |
self.payment_state = "paid" | |
end | |
if old_payment_state = self.changed_attributes["payment_state"] | |
self.state_events.create({ | |
:previous_state => old_payment_state, | |
:next_state => self.payment_state, | |
:name => "payment" , | |
:user_id => (User.respond_to?(:current) && User.current && User.current.id) || self.user_id | |
}) | |
end | |
end | |
# round the money to 2 decimal places | |
def round_money(n) | |
(n*100).round / 100.0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment