Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## app/models/order.rb
- class Order << ActiveRecord::Base
- has_many :transactions, :class_name => "OrderTransaction", :dependent => :destroy
- has_many :order_transactions, :dependent => :destroy
- has_many :line_items, :as => :cartable, :dependent => :destroy
- belongs_to :user
- named_scope :distinct_users, lambda { {:select => "DISTINCT user_id"} }
- named_scope :status, lambda { |*args| {:conditions => {:order_status => args } } }
- named_scope :current, lambda { {:conditions => ["orders.created_at between ? and ?", Var.current.previous_year_end, Var.current.end_date]} }
- class << self
- def prereserve_spaces(current_user)
- Cart.destroy_all
- ### set variables that will be used later
- disc_inventory = Product.msd.first
- #users = Order.distinct_users.status("paid")
- users = Order.distinct_users.status("confirmed","paid")
- reservations = []
- ### Loop through all users whose current orders had been paid
- users.each do |user|
- ### This looks redundant but isn't. The entries in the users array aren't proper User models
- ### so we have to do this secondary find in order to get the complete object.
- u = User.find(user.user_id)
- user_reservations = []
- user_reservations = Space.user(u.id)
- unless user_reservations.empty?
- ### If the user had any reservations, create new line items in their cart for each reservation,
- ### creating a new cart if one doesn't already exist
- cart = Cart.find_or_create_by_user_id(:user_id => u.id)
- user_reservations.each do |r|
- item = LineItem.new(:billable_id => r.id,
- :billable_type => "Space",
- :price => r.price,
- :quantity => 1)
- cart.line_items << item
- end
- cart.update_discounts(u)
- end
- end
- ### Loop through current orders
- Order.current.each do |order|
- ### return line items to inventory unless the order had been paid
- unless order.order_status == 'paid' or order.order_status == 'confirmed'
- order.line_items.each do |li|
- li.billable.previous_user = li.billable.user if li.billable.is_a? Space
- li.billable.save
- li.return_to_inventory
- li.destroy
- end
- end
- ### archive this order and record a transaction for it indicating the reason for archival
- order.archive! unless order.archived?
- order.transactions.create!(:test => true,
- :amount => 0,
- :user_id => current_user.id,
- :message => "Archiving completed order by close out year process",
- :success => true)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment