-
-
Save rubysolo/318901 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 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