Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Add order shipments component #5514

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Prev Previous commit
Next Next commit
Add orders/show/shipment component
spaghetticode committed Feb 20, 2024
commit d7bffc4240d54572a66c75c6b24eaed7fe534e72
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="<%= stimulus_id %>">
<%= render component("orders/show").new(order: @order) %>
<%= render component("ui/modal").new(title: t(".title", number: @shipment.number), close_path: close_path) do |modal| %>
<%= form_for @shipment, url: solidus_admin.order_shipments_path(@order, shipment_id: @shipment.id), html: { id: form_id } do |f| %>
<%= form_for @shipment, url: solidus_admin.order_shipment_path(@order, @shipment), html: { id: form_id } do |f| %>
<%= render component("ui/forms/field").text_field(f, :tracking) %>
<%= render component("ui/forms/field").select(
f,
Original file line number Diff line number Diff line change
@@ -57,13 +57,13 @@
<td class="px-6 py-4">
<span class="text-gray-500 body-small whitespace-nowrap">
<%= render component("ui/forms/input").new(
value: item.line_item.quantity,
value: item.states["on_hand"],
form: form_id,
name: "variants[#{item.variant.id}][quantity]",
type: :number,
step: 1,
min: "1",
max: item.line_item.quantity,
max: item.states["on_hand"],
"data-#{stimulus_id}-target": "quantity",
"data-action": "focus->#{stimulus_id}#selectRow",
) %>
Original file line number Diff line number Diff line change
@@ -23,10 +23,10 @@ def render_split_action_button
render component("ui/button").new(
name: request_forgery_protection_token,
value: form_authenticity_token(form_options: {
action: solidus_admin.split_create_order_shipments_path(@order),
action: solidus_admin.split_create_order_shipment_path(@order, @shipment),
method: :put,
}),
formaction: solidus_admin.split_create_order_shipments_path(@order),
formaction: solidus_admin.split_create_order_shipment_path(@order, @shipment),
formmethod: :put,
form: form_id,
text: t('.split'),
36 changes: 33 additions & 3 deletions admin/app/controllers/solidus_admin/shipments_controller.rb
Original file line number Diff line number Diff line change
@@ -3,7 +3,9 @@
class SolidusAdmin::ShipmentsController < SolidusAdmin::BaseController
include Spree::Core::ControllerHelpers::StrongParameters

before_action :load_order, :load_shipment, only: [:edit, :update, :split_edit]
before_action :load_order, :load_shipment, only: [:edit, :update, :split_edit, :split_create]
before_action :load_shipment, only: [:split_edit, :split_create]
before_action :load_split_variants, only: [:split_create]
#before_action :load_transfer_params, only: [:split_create]

def edit
@@ -42,7 +44,27 @@ def split_edit
end

def split_create
raise
@desired_shipment = @shipment.order.shipments.build(stock_location: @shipment.stock_location)

ActiveRecord::Base.transaction do
results = @variants_with_quantity.map do |variant, quantity|
fulfilment_changer = Spree::FulfilmentChanger.new(
current_shipment: @shipment,
desired_shipment: @desired_shipment,
variant: variant,
quantity: quantity,
track_inventory: Spree::Config.track_inventory_levels
)
fulfilment_changer.run!
end
raise(ActiveRecord::Rollback) if results.include?(false)

if results.all?(true)
redirect_to order_path(@order), status: :see_other, notice: t('.success')
else
render json: { success: false, message: fulfilment_changer.errors.full_messages.to_sentence }, status: :accepted
end
end
end

def update
@@ -66,7 +88,7 @@ def load_order
end

def load_shipment
@shipment = @order.shipments.find_by(id: params[:shipment_id])
@shipment = @order.shipments.find_by!(number: params[:id])
end

# def load_transfer_params
@@ -78,6 +100,14 @@ def load_shipment
# authorize! :create, Shipment
# end

def load_split_variants
params[:variants].permit!
@variants_with_quantity = {}
params[:variants].each do |variant_id, qty|
@variants_with_quantity[Spree::Variant.find(variant_id)] = qty[:quantity].to_i
end
end

def shipment_params
if params[:shipment] && !params[:shipment].empty?
params.require(:shipment).permit(permitted_shipment_attributes)
2 changes: 2 additions & 0 deletions admin/config/locales/shipments.en.yml
Original file line number Diff line number Diff line change
@@ -5,3 +5,5 @@ en:
update:
success: "Shipment was updated successfully"
error: "Shipment could not be updated"
split_create:
success: "Shipment was splitted successfully"