Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Forked from jimmybaker/gist:318815
Created March 1, 2010 22:43
Show Gist options
  • Save rubysolo/318901 to your computer and use it in GitHub Desktop.
Save rubysolo/318901 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 validate_applied_discounts
discount_assignments.select { |a|
! a.discount.applicable?(self)
}.each { |a|
a.destroy
}
end
def calculate_discount_total
self.discount_total = discounts(true).inject(0) do |sum,discount|
sum += discount.calculate(self)
end
end
###
# in discount:
def compatible?(*discounts)
discounts.length == 0 || (can_use_with_other_discounts? && discounts.all?{|d| d.can_use_with_other_discounts? })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment