Guest User

Untitled

a guest
Jul 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. def update_discounts(current_user)
  2. ### this is ugly, but necessary due to the many variations of discounting
  3. ### Essentially, count spaces in the cart, spaces the user has reserved in previous current
  4. ### orders, and then calculate the cart spaces that have not been previously reserved
  5. cart_discounts = 0
  6. cart_spaces = line_items.select { |i| i.billable_type == "Space" }
  7. cart_space_ids = cart_spaces.collect { |s| s.billable_id }
  8. cart_space_count = cart_spaces.size
  9. order_spaces = Order.user(current_user.id).status("paid","awaiting_payment","confirmed").collect { |o| o.get_reservations }.flatten
  10. order_space_ids = order_spaces.collect { |s| s.billable_id }
  11. order_space_count = order_spaces.size
  12. new_spaces = cart_space_ids - order_space_ids
  13. new_space_count = new_spaces.size
  14.  
  15.  
  16. ### This section actually determines how many discounts to be applied
  17. if order_space_count > 0
  18. if cart_space_count == order_space_count && new_space_count == 0
  19. cart_discounts = cart_space_count - 1
  20. else
  21. cart_discounts = new_space_count
  22. end
  23. elsif cart_space_count > 1
  24. cart_discounts = cart_space_count - 1
  25. end
  26. Rails.logger.warn("Inside update_discounts: CSC: #{cart_space_count} OSC: #{order_space_count} NSC: #{new_space_count} CD: #{cart_discounts}")
  27. ### And this section applies the appropriate discount or removes it, as applicable
  28. multispace_discount = line_items.select { |li| li.billable == Product.msd.first }.first
  29. if multispace_discount
  30. Rails.logger.warn("Inside if msd: CD: #{cart_discounts}, MSD is a #{multispace_discount.class}")
  31. if cart_discounts < 1
  32. multispace_discount.destroy
  33. else
  34. multispace_discount.update_attribute(:quantity, cart_discounts)
  35. end
  36. else
  37. unless cart_discounts < 1
  38. self.line_items << LineItem.new(:price => Product.msd.first.price,
  39. :quantity => cart_discounts,
  40. :billable => Product.msd.first)
  41. end
  42. end
  43. end
Add Comment
Please, Sign In to add comment