X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=app%2Fcontrollers%2Fpurchase_controller.rb;h=37b74012378cf88d003ba10a5e92fad19fca1b7a;hb=da871e3e27c31759e0d017c89ccf16a14db1a227;hp=6be425419fd37bb57c6578f5e91556b522e210db;hpb=31c3621ad8a43715ecc02655d58b20099308fe50;p=cs356-p2-videostore.git diff --git a/app/controllers/purchase_controller.rb b/app/controllers/purchase_controller.rb index 6be4254..37b7401 100644 --- a/app/controllers/purchase_controller.rb +++ b/app/controllers/purchase_controller.rb @@ -57,12 +57,15 @@ class PurchaseController < ApplicationController @end_date = Date.new params[:end_date]['(1i)'].to_i, params[:end_date]['(2i)'].to_i, params[:end_date]['(3i)'].to_i merchandises = MerchandisePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date]) rentables = RentablePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date]) + late_fees = LateFeePurchase.find(:all, :conditions => ['date >= ? AND date <= ?', @begin_date, @end_date]) @merch_count = merchandises.length @rent_count = rentables.length + @late_count = late_fees.length @merch_sum = merchandises.sum(&:price) @rent_sum = rentables.sum(&:price) - @total = @merch_sum + @rent_sum + @late_sum = late_fees.sum(&:price) + @total = @merch_sum + @rent_sum + @late_sum render :action => 'income_results' else render :action => 'income' @@ -74,19 +77,40 @@ class PurchaseController < ApplicationController render :action => 'begin' session[:total] = 0.00 session[:items] = [] + session[:bonus] = nil end - def success_end - # Set the customer's debt to $0.00. They MUST pay you before - # checking anything else out, of course + 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 not @debt.zero? + purchase = LateFeePurchase.new + purchase.customer = @customer + purchase.date = Time.now.to_date + purchase.price = @debt + purchase.save + end + + # Set the customer's debt to $0.00, she paid us @customer = Customer.find_by_id(session[:customer_id]) @customer.debt = 0.00 @customer.save + # Wipe session data session[:customer_id] = nil session[:total] = nil session[:items] = nil - redirect_to :action => :begin + session[:bonus] = nil + + # Show the receipt + render :action => 'receipt' end def customer_ok @@ -158,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 @@ -168,13 +237,13 @@ class PurchaseController < ApplicationController # Actually record the purchase purchase = RentablePurchase.new - purchase.customer_id = session[:customer_id][0] + purchase.customer = @customer purchase.date = Time.now.to_date purchase.price = @rentable.calculated_price purchase.rentable = @rentable purchase.save! - # Add te session variables + # Add to session variables session[:total] += @rentable.calculated_price session[:items].push @rentable