render :action => 'begin'
session[:total] = 0.00
session[:items] = []
+ session[:bonus] = nil
end
def receipt
@debt = @customer.debt
@total = session[:total] + @debt
@items = session[:items]
+ @bonus = session[:bonus]
@time = Time.now
# Record a Late Fee Payment if we need to
- if @debt
+ if not @debt.zero?
purchase = LateFeePurchase.new
purchase.customer = @customer
purchase.date = Time.now.to_date
session[:customer_id] = nil
session[:total] = nil
session[:items] = nil
+ session[:bonus] = nil
# Show the receipt
render :action => 'receipt'
return
end
+ # Check for a Bonus
+ for bonus in BonusPolicy.find_all_by_bonus_type(@rentable.class.to_s)
+ # Find the earliest the period for this bonus extends
+ since_date = Time.now.advance(:days => (-1 * bonus.days)).to_date
+
+ # Find the date of the latest bonus in this period
+ last_bonus_date = BonusPurchase.last_bonus_date(@customer, @rentable.class, since_date)
+
+ # Find the number of rentals of this type in the period since the last bonus
+ count = 0
+ for rental in RentablePurchase.find_all_by_customer_id(@customer, :conditions => ["date > ?", last_bonus_date])
+ if rental.rentable.class == @rentable.class
+ count += 1
+ end
+ end
+
+ # We are eligible for a bonus!
+ if count >= bonus.number
+
+ # Check out the item
+ checkout = Coitem.new
+ checkout.customer = @customer
+ checkout.rentable = @rentable
+ checkout.out_date = Time.now.to_date
+ checkout.due_date = @rentable.due_date
+ checkout.save!
+
+ # Record the BonusPurchase
+ purchase = BonusPurchase.new
+ purchase.customer = @customer
+ purchase.date = Time.now.to_date
+ purchase.price = @rentable.calculated_price
+ purchase.rentable = @rentable
+ purchase.save!
+
+ # Add to session variables
+ session[:items].push @rentable
+ session[:bonus] = @rentable
+
+ flash[:notice] = "Successfully made bonus purchase"
+ redirect_to :action => :menu
+ return
+ end
+ end
+
# Check out the item
checkout = Coitem.new
checkout.customer = @customer