Class: Shoppy::ShipmentsController

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

Direct Known Subclasses

ShipmentsController

Instance Method Summary collapse

Instance Method Details

#deleteObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/shoppy/shipments_controller.rb', line 97

def delete
  shipment = Shipment.find_by(id: params[:shipment_id])
  (order = Order.find_by(id: params[:order_id])) if params[:order_id]
  if shipment
    if shipment.status == "Cancelled" || shipment.status == "Processing"
      shipment.destroy
      Log.newEvent("Shipment",  "Shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was deleted.", current_admin.name)
    elsif shipment.status == "Shipped" || shipment.status == "Delivered"
      flash[:warning] = "Shipment has already been shipped, so it can't be deleted."
    else
      flash[:warning] = "Something went wrong. please try again later"
    end
    if params[:view] == "show"
      redirect_to '/orders/#{order.id}/shipments/#{@shioment.id}'
    else
      if order
        redirect_to "/orders/#{order.id}/shipments"
      else
        redirect_to "/shipments"
      end
    end
  else
    page_not_found
  end
end

#editObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
# File 'app/controllers/shoppy/shipments_controller.rb', line 27

def edit
  @shipment = Shipment.find_by(id: params[:shipment_id])
  (order = Order.find_by(id: params[:order_id])) if params[:order_id]
  if @shipment
    if params[:status] == "cancelled"
      if @shipment.status == "Processing"
        @shipment.status = "Cancelled"
        @shipment.save
        Log.newEvent("Shipments",  "shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was cancelled", current_admin.name)
        flash[:notice] = "Shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was cancelled"
      elsif @shipment.status == "Cancelled"
        flash[:warning] = "Shipment is already not cancelled"
      elsif @shipment.status == "Shipped" || @shipment.status == "Delivered"
        flash[:warning] = "Shipment is already shipped, so it can't be cancelled"
      else
        flash[:warning] = "Something went wrong. please try again later"
      end
    elsif params[:status] == "processing"
      if @shipment.status == "Cancelled"
        @shipment.status = "Processing"
        @shipment.save
        Log.newEvent("Shipment",  "Shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was marked processing.", current_admin.name)
        flash[:notice] = "Shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was marked processing."
      elsif @shipment.status == "Processing"
        flash[:warning] = "Shipment is already marked processing."
      elsif @shipment.status == "Shipped"
        flash[:warning] = "Shipment is already shipped, so it can't be edited"
      else
        flash[:warning] = "Something went wrong. please try again later"
      end
    elsif params[:status] == "shipped"
      if @shipment.status == "Processing"
        @shipment.status = "Shipped"
        @shipment.save
        Log.newEvent("Shipment",  "Shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was marked shipped.", current_admin.name)
        flash[:notice] = "Shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was marked shipped."
      elsif @shipment.status == "Cancelled"
        flash[:warning] = "Order is cancelled, hence can't be marked as shipped"
      elsif @shipment.status = "Shipped"
        flash[:notice] = "Shipment is already marked shipped."
      else
         flash[:warning] = "Something went wrong. please try again later"
      end
    elsif params[:status] == "delivered"
      if @shipment.status == "Shipped"
        @shipment.status = "Delivered"
        @shipment.delivery_date = Date.today
        @shipment.save
        Log.newEvent("Shipment",  "Shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was marked Delivered.", current_admin.name)
        flash[:notice] = "Shipment no. #{@shipment.id} for order no. '#{@shipment.order.id}' was marked Delivered."
      else
        flash[:warning] = "Order has to be shipped first, before you mark it delivered"
      end
    else
      flash[:warning] = "Something went wrong. please try again later"
    end
    if params[:view] == "show"
      redirect_to "/shipments/#{@shipment.id}"
    else
      if order
        redirect_to "/orders/#{order.id}/shipments"
      else
        redirect_to "/shipments"
      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/shipments_controller.rb', line 5

def index
  if params[:order_id]
    @order = Order.find_by(id: params[:order_id])
    if @order
      @shipments = Shipment.where(order_id: params[:order_id])
    else
      page_not_found
    end
  else
    @shipments = Shipment.all
  end
end

#showObject



18
19
20
21
22
23
24
25
# File 'app/controllers/shoppy/shipments_controller.rb', line 18

def show
  s = Shipment.find_by(id: params[:shipment_id])
  if s
    @shipment = s
  else
    page_not_found
  end
end