Add Purchase system
[cs356-p2-videostore.git] / app / controllers / purchase_controller.rb
1 class PurchaseController < ApplicationController
2
3   def index
4     redirect_to :action => :begin
5   end
6
7   def list
8     @purchase_pages, @purchase = paginate :purchases, :per_page => 100
9   end
10
11   def begin
12     # enter a customer id here
13     render :action => 'begin'
14     session[:total] = 0.00
15   end
16
17   def customer_ok
18     if Customer.find_by_id(params[:customer_id])
19       session[:customer_id] = params[:customer_id]
20       redirect_to :action => :menu
21     else
22       flash[:error] = "Customer ID is invalid"
23       redirect_to :action => :begin
24     end
25   end
26
27   def menu
28     @customer = Customer.find_by_id(session[:customer_id])
29     @total_price = session[:total]
30     render :action => 'menu'
31   end
32
33   def rent_begin
34     render :action => 'rent_begin'
35   end
36
37   def rent_validate
38     @customer = Customer.find_by_id(session[:customer_id])
39     @rentable = Rentable.find_by_id(params[:rentable_id])
40
41     if @customer.nil?
42       flash[:error] = "Customer ID is invalid"
43       redirect_to :action => :begin
44       return
45     end
46
47     if @rentable.nil?
48       flash[:error] = "Rentable ID is invalid"
49       redirect_to :action => :rent_begin
50       return
51     end
52
53     # Actually record the purchase
54     purchase = RentablePurchase.new
55     purchase.customer_id = session[:customer_id]
56     purchase.date = Time.now.to_date
57     purchase.price = @rentable.calculated_price
58     session[:total] += @rentable.calculated_price
59     purchase.rentable = @rentable
60     purchase.save!
61
62     flash[:notice] = "Successfully made purchase"
63     redirect_to :action => :menu
64   end
65
66   def buy_begin
67     render :action => 'buy_begin'
68   end
69
70   def buy_validate
71     @customer = Customer.find_by_id(session[:customer_id])
72     @merchandise = Merchandise.find_by_id(params[:merchandise_id])
73   
74     if @customer.nil?
75       flash[:error] = "Customer ID is invalid"
76       redirect_to :action => :begin
77       return
78     end
79
80     if @merchandise.nil?
81       flash[:error] = "Merchandise ID is invalid"
82       redirect_to :action => :buy_begin
83       return
84     end
85
86     if @merchandise.quantity < 1
87       flash[:error] = "The system thinks we are out of this merchandise item!"
88       redirect_to :action => :buy_begin
89       return
90     end
91
92     # Actually record the purchase
93     purchase = MerchandisePurchase.new
94     purchase.customer_id = session[:customer_id]
95     purchase.date = Time.now.to_date
96     purchase.price = @merchandise.price
97     session[:total] += @merchandise.price
98     purchase.merchandise = @merchandise
99     purchase.quantity = 1
100     @merchandise.quantity -= 1
101
102     # Save both the merchandise (we changed the quantity) and the purchase to the log
103     @merchandise.save!
104     purchase.save!
105
106     flash[:notice] = "Successfully made purchase"
107     redirect_to :action => :menu
108   end
109 end