Skip to content

Instantly share code, notes, and snippets.

@jimmybaker
Created March 1, 2010 21:10
Show Gist options
  • Save jimmybaker/318815 to your computer and use it in GitHub Desktop.
Save jimmybaker/318815 to your computer and use it in GitHub Desktop.
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