Guest User

Untitled

a guest
Jul 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. ## app/models/order.rb
  2. class Order << ActiveRecord::Base
  3.  
  4. has_many :transactions, :class_name => "OrderTransaction", :dependent => :destroy
  5. has_many :order_transactions, :dependent => :destroy
  6. has_many :line_items, :as => :cartable, :dependent => :destroy
  7. belongs_to :user
  8.  
  9. named_scope :distinct_users, lambda { {:select => "DISTINCT user_id"} }
  10. named_scope :status, lambda { |*args| {:conditions => {:order_status => args } } }
  11. named_scope :current, lambda { {:conditions => ["orders.created_at between ? and ?", Var.current.previous_year_end, Var.current.end_date]} }
  12.  
  13. class << self
  14. def prereserve_spaces(current_user)
  15. Cart.destroy_all
  16. ### set variables that will be used later
  17. disc_inventory = Product.msd.first
  18. #users = Order.distinct_users.status("paid")
  19. users = Order.distinct_users.status("confirmed","paid")
  20.  
  21. reservations = []
  22. ### Loop through all users whose current orders had been paid
  23. users.each do |user|
  24. ### This looks redundant but isn't. The entries in the users array aren't proper User models
  25. ### so we have to do this secondary find in order to get the complete object.
  26. u = User.find(user.user_id)
  27. user_reservations = []
  28. user_reservations = Space.user(u.id)
  29. unless user_reservations.empty?
  30. ### If the user had any reservations, create new line items in their cart for each reservation,
  31. ### creating a new cart if one doesn't already exist
  32. cart = Cart.find_or_create_by_user_id(:user_id => u.id)
  33. user_reservations.each do |r|
  34. item = LineItem.new(:billable_id => r.id,
  35. :billable_type => "Space",
  36. :price => r.price,
  37. :quantity => 1)
  38. cart.line_items << item
  39. end
  40. cart.update_discounts(u)
  41. end
  42. end
  43.  
  44. ### Loop through current orders
  45. Order.current.each do |order|
  46. ### return line items to inventory unless the order had been paid
  47. unless order.order_status == 'paid' or order.order_status == 'confirmed'
  48. order.line_items.each do |li|
  49. li.billable.previous_user = li.billable.user if li.billable.is_a? Space
  50. li.billable.save
  51. li.return_to_inventory
  52. li.destroy
  53. end
  54. end
  55. ### archive this order and record a transaction for it indicating the reason for archival
  56. order.archive! unless order.archived?
  57. order.transactions.create!(:test => true,
  58. :amount => 0,
  59. :user_id => current_user.id,
  60. :message => "Archiving completed order by close out year process",
  61. :success => true)
  62. end
  63. end
  64. end
  65. end
Add Comment
Please, Sign In to add comment