Created
March 18, 2014 19:39
-
-
Save chischaschos/9627782 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
def update_hto_state | |
Rails.logger.info "Here we go" | |
if hto? | |
determine_hto_state | |
end | |
update_attributes_without_callbacks(hto_state: hto_state) | |
if hto_state_changed? | |
state_changed('hto') | |
notify_hto | |
end | |
end | |
def notify_hto | |
notifier_name = if hto_state == 'pending_return' | |
'warning_email' | |
else | |
hto_state | |
end | |
mailer = HtoMailer.send("#{notifier_name}_email", self) | |
mailer.deliver | |
end | |
def determine_hto_state | |
self.hto_state = | |
if available? | |
if returned? | |
'returned' | |
elsif paid? | |
'paid' | |
elsif shipped? | |
if hto_overdue_days > 0 | |
'overdue' | |
elsif hto_warning_days > 0 | |
'pending_return' | |
elsif hto_delivered_days > 0 | |
'delivered' | |
else | |
'shipped' | |
end | |
else | |
'ready' | |
end | |
else | |
'unavailable' | |
end | |
end | |
def available? | |
completed? && !canceled? | |
end | |
def returned? | |
# maxs: now order becomes 'returned' if all it's inventory units are returned; payment state doesn't matter. | |
inventory_units.any?(&:returned?) && | |
!inventory_units.any?(&:shipped?) #&& !payments.any?(&:pending?) | |
end | |
def paid? | |
payments.any?(&:completed?) | |
end | |
def shipped? | |
shipments.any?(&:shipped?) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment