Created
March 1, 2010 21:10
-
-
Save jimmybaker/318815 to your computer and use it in GitHub Desktop.
This file contains 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 add_discount(promo_code) | |
discount = Discount.find_by_code(promo_code) | |
if discount && discount.applicable?(self) | |
if discount.customer_exclusive? | |
discount_assignment = discount.assignments.find_by_user_id(user.id) | |
discount_assignment.update_attributes(:sales_order_id => self.id) | |
else | |
discount_assignments.create(:discount => discount) | |
end | |
save | |
reload | |
discount | |
else | |
errors.add_to_base "The code you entered is invalid." | |
nil | |
end | |
end | |
def calculate_discount_total | |
applied_discounts = discounts.dup | |
discounts.delete_all | |
applied_discounts.each do |d| | |
end | |
self.discount_total = discounts(true).inject(0) do |sum,discount| | |
sum += discount.calculate(self) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment