Class: Spree::Admin::ShipmentsController

Inherits:
ResourceController show all
Includes:
StockLocationsHelper
Defined in:
app/controllers/spree/admin/shipments_controller.rb

Instance Method Summary collapse

Methods included from StockLocationsHelper

#available_stock_locations, #available_stock_locations_for_product, #available_stock_locations_list, #default_stock_location_for_product

Methods inherited from ResourceController

belongs_to, #create, #destroy, #edit, #index, #new

Methods included from TableConcern

#apply_table_sort, #custom_sort_active?, #process_table_query_state, #table, #table_key, #table_registered?

Methods included from BreadcrumbConcern

#add_breadcrumb_icon_instance_var

Instance Method Details

#shipObject



20
21
22
23
24
25
26
27
28
# File 'app/controllers/spree/admin/shipments_controller.rb', line 20

def ship
  if @shipment.shippable? && @shipment.ship
    flash[:success] = Spree.t(:shipment_successfully_shipped)
  else
    flash[:error] = Spree.t(:cannot_ship)
  end

  redirect_back fallback_location: spree.edit_admin_order_path(@order)
end

#splitObject



30
31
32
# File 'app/controllers/spree/admin/shipments_controller.rb', line 30

def split
  @max_quantity = @shipment.inventory_units.where(variant_id: @variant.id).sum(:quantity)
end

#transferObject



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
# File 'app/controllers/spree/admin/shipments_controller.rb', line 34

def transfer
  destination_name, destination_id = params[:destination].split('_')
  quantity = params[:quantity]&.to_i || 1

  errors = []
  errors << "#{Spree.t(:quantity)} #{Spree.t('validation.is_too_large')}" if quantity <= 0

  transfer = nil

  case destination_name
  when 'stock-location'
    destination = available_stock_locations.find(destination_id)
    transfer = @shipment.transfer_to_location(@variant, quantity, destination)
  when 'shipment'
    destination = parent.shipments.
                  where(stock_location: available_stock_locations).
                  find(destination_id)
    transfer = @shipment.transfer_to_shipment(@variant, quantity, destination)
  end

  errors << Spree.t('admin.shipment_transfer.wrong_destination') if transfer.nil?

  if errors.any?
    flash[:error] = errors.to_sentence
    return redirect_back(fallback_location: spree.edit_admin_order_path(@order))
  end

  if transfer.valid? && transfer.run!
    flash[:success] = Spree.t(:shipment_transfer_success)
  else
    flash[:error] = transfer.errors.full_messages.to_sentence
  end

  redirect_back(fallback_location: spree.edit_admin_order_path(@order))
end

#updateObject



13
14
15
16
17
18
# File 'app/controllers/spree/admin/shipments_controller.rb', line 13

def update
  @result = Spree.shipment_update_service.call(shipment: @shipment, shipment_attributes: permitted_resource_params)
  flash[:success] = Spree.t(:successfully_updated, resource: Spree.t(:shipment)) if @result.success?

  redirect_back fallback_location: spree.edit_admin_order_path(@order)
end