X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=app%2Fcontrollers%2Fpurchase_controller.rb;h=37b74012378cf88d003ba10a5e92fad19fca1b7a;hb=da871e3e27c31759e0d017c89ccf16a14db1a227;hp=a8bfa8f773673b27332329def0abdcb4a3fc214c;hpb=2b6a0952011552367bc40f8cce3f5ff23b1f473f;p=cs356-p2-videostore.git diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb index a8bfa8f..37b7401 100644 --- a/app/controllers/purchase_controller.rb +++ b/app/controllers/purchase_controller.rb @@ -77,18 +77,20 @@ class PurchaseController < ApplicationController render :action => 'begin' session[:total] = 0.00 session[:items] = [] + session[:bonus] = nil end - def reciept + def receipt # Save important data, since we're gonna wipe it out now @customer = Customer.find_by_id(session[:customer_id]) @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 @@ -105,9 +107,10 @@ class PurchaseController < ApplicationController session[:customer_id] = nil session[:total] = nil session[:items] = nil + session[:bonus] = nil - # Show the reciept - render :action => 'reciept' + # Show the receipt + render :action => 'receipt' end def customer_ok @@ -179,6 +182,51 @@ class PurchaseController < ApplicationController 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