Class: Shoppe::OrdersController

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

Instance Method Summary collapse

Instance Method Details

#acceptObject


80
81
82
83
84
85
# File 'app/controllers/shoppe/orders_controller.rb', line 80

def accept
  @order.accept!(current_user)
  redirect_to @order, :notice => t('shoppe.orders.accept_notice')
rescue Shoppe::Errors::PaymentDeclined => e
  redirect_to @order, :alert => e.message
end

#createObject


17
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/shoppe/orders_controller.rb', line 17

def create
  Shoppe::Order.transaction do
    @order = Shoppe::Order.new(safe_params)
    @order.status = 'confirming'

    if safe_params[:customer_id]
      @customer = Shoppe::Customer.find safe_params[:customer_id]
      @order.first_name = @customer.first_name
      @order.last_name = @customer.last_name
      @order.company = @customer.company
      @order.email_address = @customer.email
      @order.phone_number = @customer.phone
      if @customer.addresses.billing.present?
        billing = @customer.addresses.billing.first
        @order.billing_address1 = billing.address1
        @order.billing_address2 = billing.address2
        @order.billing_address3 = billing.address3
        @order.billing_address4 = billing.address4
        @order.billing_postcode = billing.postcode
        @order.billing_country_id = billing.country_id
      end
      if @customer.addresses.delivery.present?
        delivery = @customer.addresses.delivery.first
        @order.delivery_address1 = delivery.address1
        @order.delivery_address2 = delivery.address2
        @order.delivery_address3 = delivery.address3
        @order.delivery_address4 = delivery.address4
        @order.delivery_postcode = delivery.postcode
        @order.delivery_country_id = delivery.country_id
      end
    end

    if !request.xhr? && @order.save
      @order.confirm!
      redirect_to @order, :notice => t('shoppe.orders.create_notice')
    else
      @order.order_items.build(:ordered_item_type => 'Shoppe::Product')
      render :action => "new"
    end
  end
rescue Shoppe::Errors::InsufficientStockToFulfil => e
  flash.now[:alert] = t('shoppe.orders.insufficient_stock_order', out_of_stock_items: e.out_of_stock_items.map { |t| t.ordered_item.full_name }.to_sentence)
  render :action => 'new'
end

#despatch_noteObject


99
100
101
# File 'app/controllers/shoppe/orders_controller.rb', line 99

def despatch_note
  render :layout => 'shoppe/printable'
end

#indexObject


7
8
9
10
# File 'app/controllers/shoppe/orders_controller.rb', line 7

def index
  @query = Shoppe::Order.ordered.received.includes(:order_items => :ordered_item).page(params[:page]).search(params[:q])
  @orders = @query.result
end

#newObject


12
13
14
15
# File 'app/controllers/shoppe/orders_controller.rb', line 12

def new
  @order = Shoppe::Order.new
  @order.order_items.build(:ordered_item_type => 'Shoppe::Product')
end

#rejectObject


87
88
89
90
91
92
# File 'app/controllers/shoppe/orders_controller.rb', line 87

def reject
  @order.reject!(current_user)
  redirect_to @order, :notice => t('shoppe.orders.reject_notice')
rescue Shoppe::Errors::PaymentDeclined => e
  redirect_to @order, :alert => e.message
end

#searchObject


75
76
77
78
# File 'app/controllers/shoppe/orders_controller.rb', line 75

def search
  index
  render :action => "index"
end

#shipObject


94
95
96
97
# File 'app/controllers/shoppe/orders_controller.rb', line 94

def ship
  @order.ship!(params[:consignment_number], current_user)
  redirect_to @order, :notice => t('shoppe.orders.ship_notice')
end

#showObject


62
63
64
# File 'app/controllers/shoppe/orders_controller.rb', line 62

def show
  @payments = @order.payments.to_a
end

#updateObject


66
67
68
69
70
71
72
73
# File 'app/controllers/shoppe/orders_controller.rb', line 66

def update
  @order.attributes = safe_params
  if !request.xhr? && @order.update_attributes(safe_params)
    redirect_to @order, :notice => t('shoppe.orders.update_notice')
  else
    render :action => "edit"
  end
end