Class: Shoppy::OrdersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/shoppy/orders_controller.rb

Direct Known Subclasses

OrdersController

Instance Method Summary collapse

Instance Method Details

#deleteObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/shoppy/orders_controller.rb', line 98

def delete
  @order = Order.find_by(id: params[:order_id])
  (customer = Customer.find_by(id: params[:customer_id])) if params[:customer_id]
  if @order
    if @order.status == "Cancelled" || @order.status == "Pending"
      @order.destroy
      flash[:notice] = "Order no. #{@order.id} for '#{@order.customer.name}' was deleted"
      Log.newEvent("Orders",  "Order no. #{@order.id} for '#{@order.customer.name}' was deleted", current_admin.name)
    elsif @order.status == "Approved"
      flash[:warning] = "Order is currently approved, so it can't be deleted."
    elsif @order.status == "Shipped"
      flash[:warning] = "Order is currently shipped, so it can't be deleted."
    elsif @order.status == "Delivered"
      flash[:warning] = "Order is currently delivered, so it can't be deleted."
    else
      flash[:warning] = "Something wrong happend. Please try again later."
    end
    if params[:view] == "show"
      redirect_to '/accounts/#{@order.id}'
    else
      if customer
        redirect_to "/accounts/#{customer.id}/orders"
      else
        redirect_to "/orders"
      end
    end
  else
    page_not_found
  end
end

#editObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/controllers/shoppy/orders_controller.rb', line 55

def edit
  @order = Order.find_by(id: params[:order_id])
  (customer = Customer.find_by(id: params[:customer_id])) if params[:customer_id]
  if @order
    if params[:status] == "disapprove"
      if @order.status == "Approved"
        @order.status = "Cancelled"
        @order.save
        Log.newEvent("Orders",  "Order no. #{@order.id} for '#{@order.customer.name}' was cancelled", current_admin.name)
        flash[:notice] = "Order no. #{@order.id} for '#{@order.customer.name}' was cancelled"
      elsif @order.status == "Cancelled" || @order.status == "Pending"
        flash[:warning] = "Order is already not approved"
      elsif @order.status == "Processing" || @order.status == "Shipped"
        flash[:warning] = "Order is fully/partially shipped, so it can't be cancelled"
      else
        flash[:warning] = "Something went wrong. please try again later"
      end
    elsif params[:status] == "approve"
      if @order.status == "Cancelled" || @order.status == "Pending"
        @order.status = "Approved"
        @order.save
        Log.newEvent("Orders",  "Order no. #{@order.id} for '#{@order.customer.name}' was approved", current_admin.name)
        flash[:notice] = "Order no. #{@order.id} for '#{@order.customer.name}' was approved"
      else
        flash[:warning] = "Order is already approved"
      end
    else
      flash[:warning] = "Something went wrong. please try again later"
    end
    if params[:view] == "show"
      redirect_to "/orders/#{@order.id}"
    else
      if customer
        redirect_to "/accounts/#{customer.id}/orders"
      else
        redirect_to "/orders"
      end
    end
  else
    page_not_found
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/shoppy/orders_controller.rb', line 5

def index
  if params[:customer_id]
    @customer = Customer.find_by(id: params[:customer_id])
    if @customer
      @orders = Order.where(customer_id: params[:customer_id])
    else
      page_not_found
    end
  else
    @orders = Order.all
  end
end

#newObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/shoppy/orders_controller.rb', line 18

def new
  customer = Customer.find_by(id: params[:order][:customer_id])
  address  = CustomerAddress.find_by(id: params[:address_id])
  if customer
    if address
      order_lines_hash = {}
      order_lines = params[:orderLines]
      order_lines.each do |oli, old|
        new_ols = { old["variant_id"] => old["quantity"].to_i }
        order_lines_hash = order_lines_hash.update(new_ols) { |k, v1, v2| v1 + v2 }
      end
      order = OrdersHelper::new_order(customer, order_lines_hash, address)
      if order
        flash[:notice] = "Order has been created for a total amount #{order.total_price}"
      else
        flash[:warning] = "Something wrong happend. please try again later"
      end
      redirect_to "/accounts/#{customer.id}/orders"
    else
      flash[:warning] = "You didn't select a customer"
      redirect_to "/orders"
    end
  else
    flash[:warning] = "Please select a Customer Address."
    redirect_to "/orders"
  end
end

#showObject



46
47
48
49
50
51
52
53
# File 'app/controllers/shoppy/orders_controller.rb', line 46

def show
  r = Order.find_by(id: params[:order_id])
  if r
    @order = r
  else
    page_not_found
  end
end