Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def update_discounts(current_user)
- ### this is ugly, but necessary due to the many variations of discounting
- ### Essentially, count spaces in the cart, spaces the user has reserved in previous current
- ### orders, and then calculate the cart spaces that have not been previously reserved
- cart_discounts = 0
- cart_spaces = line_items.select { |i| i.billable_type == "Space" }
- cart_space_ids = cart_spaces.collect { |s| s.billable_id }
- cart_space_count = cart_spaces.size
- order_spaces = Order.user(current_user.id).status("paid","awaiting_payment","confirmed").collect { |o| o.get_reservations }.flatten
- order_space_ids = order_spaces.collect { |s| s.billable_id }
- order_space_count = order_spaces.size
- new_spaces = cart_space_ids - order_space_ids
- new_space_count = new_spaces.size
- ### This section actually determines how many discounts to be applied
- if order_space_count > 0
- if cart_space_count == order_space_count && new_space_count == 0
- cart_discounts = cart_space_count - 1
- else
- cart_discounts = new_space_count
- end
- elsif cart_space_count > 1
- cart_discounts = cart_space_count - 1
- end
- Rails.logger.warn("Inside update_discounts: CSC: #{cart_space_count} OSC: #{order_space_count} NSC: #{new_space_count} CD: #{cart_discounts}")
- ### And this section applies the appropriate discount or removes it, as applicable
- multispace_discount = line_items.select { |li| li.billable == Product.msd.first }.first
- if multispace_discount
- Rails.logger.warn("Inside if msd: CD: #{cart_discounts}, MSD is a #{multispace_discount.class}")
- if cart_discounts < 1
- multispace_discount.destroy
- else
- multispace_discount.update_attribute(:quantity, cart_discounts)
- end
- else
- unless cart_discounts < 1
- self.line_items << LineItem.new(:price => Product.msd.first.price,
- :quantity => cart_discounts,
- :billable => Product.msd.first)
- end
- end
- end
Add Comment
Please, Sign In to add comment