Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Open source Stock management in admin (#12260)

Łukasz Adamczykbb5b492e73a0
diff --git a/admin/app/controllers/spree/admin/stock_items_controller.rb b/admin/app/controllers/spree/admin/stock_items_controller.rb
new file mode 100644
index 0000000000..aa7673f4a8
--- /dev/null
+++ b/admin/app/controllers/spree/admin/stock_items_controller.rb
@@ -0,0 +1,25 @@
+module Spree
+ module Admin
+ class StockItemsController < ResourceController
+ private
+
+ def update_turbo_stream_enabled?
+ true
+ end
+
+ def collection
+ params[:q] ||= {}
+ params[:q][:s] ||= 'created_at desc'
+
+ @search = super.accessible_by(current_ability, :update).ransack(params[:q])
+ @stock_items = @search.result.
+ joins(:variant).
+ where(spree_variants: { track_inventory: true }).
+ merge(current_store.variants.eligible).
+ includes(:stock_location, [variant: [product: [variants: [:images], master: [:images]], images: []]]).
+ page(params[:page]).
+ per(params[:per_page])
+ end
+ end
+ end
+end
diff --git a/admin/app/controllers/spree/admin/stock_transfers_controller.rb b/admin/app/controllers/spree/admin/stock_transfers_controller.rb
new file mode 100644
index 0000000000..51c4d6e38b
--- /dev/null
+++ b/admin/app/controllers/spree/admin/stock_transfers_controller.rb
@@ -0,0 +1,45 @@
+module Spree
+ module Admin
+ class StockTransfersController < ResourceController
+ before_action :prepare_params, only: :create
+
+ private
+
+ def location_after_save
+ spree.admin_stock_transfer_path(@object)
+ end
+
+ def collection
+ params[:q] ||= {}
+ params[:q][:s] ||= 'created_at desc'
+
+ @search = super.accessible_by(current_ability, :index).ransack(params[:q])
+ @stock_transfers = @search.result.
+ page(params[:page]).
+ per(params[:per_page])
+ end
+
+ def permitted_resource_params
+ params.require(:stock_transfer).permit(:source_location_id, :destination_location_id, :reference,
+ stock_movements_attributes: [:variant_id, :quantity, :originator_id, :stock_item_id])
+ end
+
+ def prepare_params
+ if params.dig(:stock_transfer, :stock_movements_attributes).blank?
+ flash[:error] = Spree.t('stock_transfer.errors.must_have_variant')
+ render :new, status: :unprocessable_entity
+ return
+ end
+
+ params.dig(:stock_transfer, :stock_movements_attributes).each do |_key, sm_params|
+ if sm_params[:stock_item_id].blank? && sm_params[:originator_id].present?
+ sm_params[:stock_item_id] =
+ Spree::StockLocation.accessible_by(current_ability).find(sm_params[:originator_id]).stock_item_or_create(sm_params[:variant_id]).id
+ end
+
+ sm_params.delete(:variant_id)
+ end
+ end
+ end
+ end
+end
diff --git a/admin/app/views/spree/admin/stock_items/_filters.html.erb b/admin/app/views/spree/admin/stock_items/_filters.html.erb
new file mode 100644
index 0000000000..d76eaf2f2d
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_items/_filters.html.erb
@@ -0,0 +1,27 @@
+<div data-hook="admin_stock_items">
+ <%= search_form_for [:admin, @search] do |f| %>
+ <div data-hook="admin_stock_items_index_search" class="row">
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= f.label :variant_product_name_cont, Spree.t(:product_name) %>
+ <%= f.text_field :variant_product_name_cont, include_blank: true, size: 15, class: 'form-control js-quick-search-target js-filterable' %>
+ </div>
+ </div>
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= f.label :variant_sku_cont, Spree.t(:sku) %>
+ <%= f.text_field :variant_sku_cont, size: 15, class: "form-control js-filterable" %>
+ </div>
+ </div>
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= f.label :stock_location_id_eq, Spree.t(:stock_location) %>
+ <%= f.select :stock_location_id_eq, available_stock_locations_list, { include_blank: true }, class: 'custom-select js-filterable' %>
+ </div>
+ </div>
+ </div>
+ <div class="form-actions">
+ <%= button Spree.t(:search), 'search' %>
+ </div>
+ <% end %>
+</div>
diff --git a/admin/app/views/spree/admin/stock_items/_stock_item.html.erb b/admin/app/views/spree/admin/stock_items/_stock_item.html.erb
new file mode 100644
index 0000000000..433a94745f
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_items/_stock_item.html.erb
@@ -0,0 +1,43 @@
+<tr>
+ <td scope="row" class="w-50">
+ <%= render 'spree/admin/variants/variant', variant: stock_item.variant %>
+ </td>
+ <td class="w-20">
+ <% if can?(:manage, stock_item.stock_location) %>
+ <%= link_to stock_item.stock_location.display_name, spree.edit_admin_stock_location_path(stock_item.stock_location) %>
+ <% else %>
+ <%= stock_item.stock_location.display_name %>
+ <% end %>
+ </td>
+ <td class="text-center w-10">
+ <% if can?(:update, stock_item) %>
+ <%= form_for stock_item,
+ url: spree.admin_stock_item_path(stock_item),
+ method: :put,
+ html: { data: { controller: 'auto-submit'}, class: 'd-flex align-items-center justify-content-center' } do |f| %>
+ <div class="custom-control custom-checkbox">
+ <%= f.check_box :backorderable, class: 'custom-control-input', id: "stock_item_backorderable_#{stock_item.id}", data: { action: 'auto-submit#submit' } %>
+ <label class="custom-control-label" for="stock_item_backorderable_<%= stock_item.id %>"><span></span></label>
+ </div>
+ <% end %>
+ <% else %>
+ <%= active_badge(stock_item.backorderable?) %>
+ <% end %>
+ </td>
+ <td class="w-20">
+ <% if can?(:update, stock_item) %>
+ <%= turbo_frame_tag stock_item do %>
+ <%= form_for stock_item,
+ url: spree.admin_stock_item_path(stock_item),
+ method: :put,
+ html: { class: 'd-flex' } do |f| %>
+
+ <%= f.number_field :count_on_hand, class: 'form-control mr-2', required: true, min: 0 %>
+ <%= turbo_save_button_tag Spree.t('actions.update'), class: 'btn btn-primary' %>
+ <% end %>
+ <% end %>
+ <% else %>
+ <%= stock_item.count_on_hand %>
+ <% end %>
+ </td>
+</tr>
diff --git a/admin/app/views/spree/admin/stock_items/index.html.erb b/admin/app/views/spree/admin/stock_items/index.html.erb
new file mode 100644
index 0000000000..7ef1b185af
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_items/index.html.erb
@@ -0,0 +1,38 @@
+<% content_for :page_title do %>
+ <%= Spree.t(:stock) %>
+<% end %>
+
+<% content_for :page_tabs do %>
+ <%= render 'spree/admin/shared/stock_nav' %>
+<% end %>
+
+<% content_for :table_filter do %>
+ <%= render 'filters' %>
+<% end %>
+
+<% if @stock_items.any? %>
+ <div class="table-responsive">
+ <table class="table" id="listing_products">
+ <thead class="text-muted">
+ <tr>
+ <th scope="col"><%= Spree.t(:variant) %></th>
+ <th scope="col"><%= Spree.t(:stock_location) %></th>
+ <th scope="col" class="text-center">
+ <%= Spree.t(:backorderable) %>
+ <%= help_bubble(Spree.t(:continue_selling_when_out_of_stock)) %>
+ </th>
+ <th scope="col"><%= Spree.t(:count_on_hand) %></th>
+ </tr>
+ </thead>
+ <tbody>
+ <%= render partial: 'stock_item', collection: @stock_items %>
+ </tbody>
+ </table>
+ </div>
+<% else %>
+ <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">
+ <%= Spree.t(:no_resource_found, resource: Spree.t('admin.stock_items.plural_resource_name')) %>
+ </div>
+<% end %>
+
+<%= render 'spree/admin/shared/index_table_options', collection: @stock_items, simple: true if @stock_items.any? %>
diff --git a/admin/app/views/spree/admin/stock_items/update.turbo_stream.erb b/admin/app/views/spree/admin/stock_items/update.turbo_stream.erb
new file mode 100644
index 0000000000..1bda62b309
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_items/update.turbo_stream.erb
@@ -0,0 +1 @@
+<%= turbo_render_alerts %>
diff --git a/admin/app/views/spree/admin/stock_transfers/_destination_movement_form.html.erb b/admin/app/views/spree/admin/stock_transfers/_destination_movement_form.html.erb
new file mode 100644
index 0000000000..68781b2d72
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/_destination_movement_form.html.erb
@@ -0,0 +1,33 @@
+<% variant_id = f.object.variant&.id || 'VARIANT_ID' %>
+<div class="d-flex align-items-center justify-content-between nested-form-wrapper" data-new-record="true">
+ <%= f.hidden_field :variant_id, value: variant_id %>
+ <%= f.hidden_field :stock_item_id %>
+ <%= f.hidden_field :originator_id, data: { stock_transfer_target: "destinationInput" }, value: f.object.originator_id || 'DESTINATION_ID' %>
+ <div class="d-flex align-items-center gap-2">
+ <% if f.object.variant.present? %>
+ <%= render 'spree/admin/shared/product_image', object: f.object.variant %>
+ <% else %>
+ VARIANT_IMAGE
+ <% end %>
+ <div class="d-flex flex-column align-items-start justify-content-center">
+ <% if f.object.variant.present? %>
+ <%= f.object.variant.name %>
+ <span class="text-muted">
+ <%= "(#{f.object.variant.options_text})" if f.object.variant.options_text.present? %>
+ </span>
+ <% if f.object.variant.sku.present? %>
+ <span class="text-muted"><strong><%= Spree.t(:sku) %>:</strong> <%= f.object.variant.sku %></span>
+ <% end %>
+ <% else %>
+ VARIANT_INFO
+ <% end %>
+ </div>
+ </div>
+ <div class="d-flex align-items-center gap-2">
+ <%= f.number_field :quantity, class: 'form-control w-auto', value: f.object.quantity.zero? ? 1 : f.object.quantity, required: true, min: 1, data: { action: "stock-transfer#updateQuantity", variant_id: variant_id } %>
+ <%= button_tag class: 'btn btn-sm btn-danger', type: :button, data: { action: 'stock-transfer#remove', variant_id: variant_id } do %>
+ <%= icon('trash', class: 'icon icon-trash') %>
+ <%= Spree.t(:remove) %>
+ <% end %>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/stock_transfers/_filters.html.erb b/admin/app/views/spree/admin/stock_transfers/_filters.html.erb
new file mode 100644
index 0000000000..5fb40f2f79
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/_filters.html.erb
@@ -0,0 +1,33 @@
+<div >
+ <%= search_form_for [:admin, @search] do |f| %>
+ <div class="row">
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= f.label :number_cont, Spree.t(:number) %>
+ <%= f.text_field :number_cont, include_blank: true, size: 15, class: 'form-control js-quick-search-target js-filterable' %>
+ </div>
+ </div>
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= f.label :reference_cont, Spree.t(:reference) %>
+ <%= f.text_field :reference_cont, include_blank: true, size: 15, class: 'form-control js-filterable' %>
+ </div>
+ </div>
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= f.label :source_location_id_eq, Spree.t(:source) %>
+ <%= f.select :source_location_id_eq, available_stock_locations_list, { include_blank: true }, class: 'custom-select js-filterable' %>
+ </div>
+ </div>
+ <div class="col-12 col-lg-6">
+ <div class="form-group">
+ <%= f.label :destination_location_id_eq, Spree.t(:destination) %>
+ <%= f.select :destination_location_id_eq, available_stock_locations_list, { include_blank: true }, class: 'custom-select js-filterable' %>
+ </div>
+ </div>
+ </div>
+ <div class="form-actions">
+ <%= button Spree.t(:search), 'search' %>
+ </div>
+ <% end %>
+</div>
diff --git a/admin/app/views/spree/admin/stock_transfers/_new_variant_modal.html.erb b/admin/app/views/spree/admin/stock_transfers/_new_variant_modal.html.erb
new file mode 100644
index 0000000000..b9406fe9de
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/_new_variant_modal.html.erb
@@ -0,0 +1,30 @@
+<div class="modal fade" id="stock_transfer_modal" tabindex="-1" aria-labelledby="stock_transfer_modal_label" aria-hidden="true">
+ <div class="modal-dialog modal-dialog-centered">
+ <div class="modal-content" data-controller="search-picker">
+ <div class="modal-header">
+ <h5 class="modal-title" id="stock_transfer_modal_label"><%= Spree.t(:add_variant) %></h5>
+ <button type="button" class="btn-close" data-action="search-picker#closeAndClear" data-dismiss="modal" aria-label="Close" />
+ </div>
+ <div class="modal-body border-bottom pb-3">
+ <%= form_with url: spree.admin_variants_search_path, method: :post, data: { controller: 'auto-submit', turbo_frame: '_top' } do |form| %>
+ <%= form.search_field :q, value: params[:q], class: 'form-control', data: { search_picker_target: :search, action: 'auto-submit#submit' }, autofocus: true %>
+ <%= form.hidden_field :param_name, value: 'stock_transfer' %>
+ <%= form.hidden_field :all, value: true %>
+ <%= form.hidden_field :eligible, value: true %>
+ <%= form.hidden_field :omit_ids, value: params[:variants]&.keys&.join(','), data: { stock_transfer_target: :newVariantOmitIds } %>
+ <%= form.hidden_field :stock_location_id, value: params.dig(:stock_transfer, :source_location_id), data: {stock_transfer_target: :newVariantStockLocationId} %>
+ <% end %>
+ </div>
+ <div class="modal-body p-0" style="min-height: 200px">
+ <div id="variants_search_results" data-search-picker-target="results" class="overflow-y-auto overflow-x-hidden" style="max-height: 300px">
+ <p class="text-muted text-center pb-3 pt-5">
+ <%= Spree.t(:start_typing_to_search_for_variants) %>
+ </p>
+ </div>
+ </div>
+ <div class="modal-footer border-top d-flex justify-content-between">
+ <%= button_tag Spree.t(:add_selected_variant), class: "btn btn-primary text-center", disabled: true, data: { search_picker_target: :submit, action: 'stock-transfer#add search-picker#closeAndClear', dismiss: 'modal' } %>
+ </div>
+ </div>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/stock_transfers/_new_variant_template.html.erb b/admin/app/views/spree/admin/stock_transfers/_new_variant_template.html.erb
new file mode 100644
index 0000000000..ab2c51c79e
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/_new_variant_template.html.erb
@@ -0,0 +1,8 @@
+<template data-stock-transfer-target="template">
+ <%= f.fields_for :stock_movements, Spree::StockMovement.new, child_index: 'NEW_RECORD' do |destination_movement_form| %>
+ <%= render 'spree/admin/stock_transfers/destination_movement_form', f: destination_movement_form %>
+ <% end %>
+ <%= f.fields_for :stock_movements, Spree::StockMovement.new, child_index: 'NEW_RECORD2' do |source_movement_form| %>
+ <%= render 'spree/admin/stock_transfers/source_movement_form', f: source_movement_form %>
+ <% end %>
+</template>
diff --git a/admin/app/views/spree/admin/stock_transfers/_source_movement_form.html.erb b/admin/app/views/spree/admin/stock_transfers/_source_movement_form.html.erb
new file mode 100644
index 0000000000..f2414bee2c
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/_source_movement_form.html.erb
@@ -0,0 +1,6 @@
+<div data-source-movement>
+ <%= f.hidden_field :variant_id, value: f.object.variant&.id || 'VARIANT_ID' %>
+ <%= f.hidden_field :stock_item_id, value: f.object.stock_item_id %>
+ <%= f.hidden_field :originator_id, data: { stock_transfer_target: "sourceInput" }, value: f.object.originator_id || 'SOURCE_ID' %>
+ <%= f.hidden_field :quantity, value: f.object.quantity.zero? ? -1 : f.object.quantity, data: { source_quantity: true } %>
+</div>
diff --git a/admin/app/views/spree/admin/stock_transfers/_stock_movement.html.erb b/admin/app/views/spree/admin/stock_transfers/_stock_movement.html.erb
new file mode 100644
index 0000000000..53b11516d1
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/_stock_movement.html.erb
@@ -0,0 +1,5 @@
+<tr>
+ <td><%= render 'spree/admin/variants/variant', variant: stock_movement.variant %></td>
+ <td><%= stock_movement.quantity %></td>
+ <td><%= stock_movement.stock_item.count_on_hand %></td>
+</tr>
diff --git a/admin/app/views/spree/admin/stock_transfers/_stock_transfer.html.erb b/admin/app/views/spree/admin/stock_transfers/_stock_transfer.html.erb
new file mode 100644
index 0000000000..bf9435ac4e
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/_stock_transfer.html.erb
@@ -0,0 +1,11 @@
+<tr id="<%= spree_dom_id stock_transfer %>" data-controller="row-link" class="cursor-pointer">
+ <td scope="row" data-action="click->row-link#openLink"><%= stock_transfer.number %></td>
+ <td class="text-truncate" data-action="click->row-link#openLink"><%= stock_transfer.reference.presence %></td>
+ <td data-action="click->row-link#openLink"><%= stock_transfer.source_location&.display_name.presence || Spree.t(:none) %></td>
+ <td data-action="click->row-link#openLink"><%= stock_transfer.destination_location.display_name %></td>
+ <td data-action="click->row-link#openLink"><%= stock_transfer.destination_movements.size %></td>
+ <td data-action="click->row-link#openLink"><%= local_time_ago(stock_transfer.created_at) %></td>
+ <td class="actions" data-action="click->row-link#openLink">
+ <%= link_to '', admin_stock_transfer_path(stock_transfer), data: { row_link_target: 'link' }, class: 'd-none' %>
+ </td>
+</tr>
diff --git a/admin/app/views/spree/admin/stock_transfers/index.html.erb b/admin/app/views/spree/admin/stock_transfers/index.html.erb
new file mode 100644
index 0000000000..02f7b8d73d
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/index.html.erb
@@ -0,0 +1,42 @@
+<% content_for :page_title do %>
+ <%= Spree.t(:stock_transfers) %>
+<% end %>
+<% content_for :page_tabs do %>
+ <%= render 'spree/admin/shared/stock_nav' %>
+<% end %>
+<% if false %>
+ <% content_for :table_filter do %>
+ <%= render 'filters' %>
+ <% end %>
+<% end %>
+<% content_for :page_actions do %>
+ <%= link_to Spree.t(:new_stock_transfer), spree.new_admin_stock_transfer_path, class: "btn btn-primary" if can?(:create, Spree::StockTransfer.new) %>
+<% end %>
+<% content_for :table_filter do %>
+ <%= render 'filters' %>
+<% end %>
+<% if @stock_transfers.any? %>
+ <div class="table-responsive">
+ <table class="table" id="listing_products">
+ <thead class="text-muted">
+ <tr>
+ <th scope="col"><%= Spree.t(:number) %></th>
+ <th scope="col"><%= Spree.t(:reference) %></th>
+ <th scope="col"><%= Spree.t(:source) %></th>
+ <th scope="col"><%= Spree.t(:destination) %></th>
+ <th scope="col"><%= Spree.t(:variants_count) %></th>
+ <th scope="col"><%= Spree.t(:created_at) %></th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ <%= render partial: 'stock_transfer', collection: @stock_transfers %>
+ </tbody>
+ </table>
+ </div>
+<% else %>
+ <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">
+ <%= Spree.t(:no_resource_found, resource: Spree.t(:stock_transfers)) %>
+ </div>
+<% end %>
+<%= render 'spree/admin/shared/index_table_options', collection: @stock_transfers, simple: true if @stock_transfers.any? %>
diff --git a/admin/app/views/spree/admin/stock_transfers/new.html.erb b/admin/app/views/spree/admin/stock_transfers/new.html.erb
new file mode 100644
index 0000000000..234bfc4ec5
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/new.html.erb
@@ -0,0 +1,65 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_stock_transfers_path %>
+ <%= Spree.t(:new_stock_transfer) %>
+<% end %>
+<% content_for :title do %>
+ <%= Spree.t(:new_stock_transfer) %>
+<% end %>
+<div data-controller="stock-transfer">
+ <%= render 'spree/admin/stock_transfers/new_variant_modal' %>
+ <%= form_with model: [:admin, @stock_transfer] do |f| %>
+ <%= render 'spree/admin/stock_transfers/new_variant_template', f: f %>
+ <% stock_locations = available_stock_locations.to_tom_select_json %>
+ <div class="card mb-4">
+ <div class="card-body" >
+ <div class="row">
+ <div class="form-group col-12 col-lg-6">
+ <%= f.label :source_location_id, Spree.t(:origin), class: 'form-label' %>
+ <%= tom_select_tag "stock_transfer[source_location_id]", empty_option: Spree.t(:none), options: stock_locations, active_option: @stock_transfer.source_location_id, select_data: {action: 'stock-transfer#updateSourceLocation'} %>
+ </div>
+ <div class="form-group col-12 col-lg-6">
+ <%= f.label :destination_location_id, Spree.t(:destination), class: 'form-label' %>
+ <%= tom_select_tag "stock_transfer[destination_location_id]", options: stock_locations, required: true, active_option: @stock_transfer.destination_location_id, select_data: {action: 'stock-transfer#updateDestinationLocation'} %>
+ </div>
+ </div>
+ <div class="form-group">
+ <%= f.label :reference, class: 'form-label' %>
+ <%= f.text_field :reference, class: 'form-control' %>
+ </div>
+ </div>
+ </div>
+ <div class="card mb-4">
+ <div class="card-header d-flex align-items-center justify-content-between">
+ <h5 class="card-title">
+ <%= Spree.t(:products) %>
+ </h5>
+ <span data-toggle="modal" data-target="#stock_transfer_modal">
+ <%= button_tag type: 'button', class: 'btn btn-light btn-sm' do %>
+ <%= icon("plus", class: "icon icon-plus") %>
+ <%= Spree.t(:add_products) %>
+ <% end %>
+ </span>
+ </div>
+ <div class="card-body p-0" style="min-height: 300px">
+ <div class="d-flex flex-column gap-3 overflow-y-auto overflow-x-hidden p-3" style="max-height: 300px">
+ <%= f.fields_for :stock_movements do |smf| %>
+ <% if smf.object.errors[:quantity].present? %>
+ <div class="alert alert-danger w-100 mb-0">
+ <% details = smf.object.errors.details[:quantity].first %>
+ <% error_message = smf.object.errors.generate_message(:quantity, :less_than_or_equal_to, {count: -details[:count], value: details[:value]}) %>
+ <%= smf.object.errors.full_message(:quantity, error_message) %>
+ </div>
+ <% end %>
+ <% if smf.object.quantity < 0 %>
+ <%= render 'spree/admin/stock_transfers/source_movement_form', f: smf %>
+ <% else %>
+ <%= render 'spree/admin/stock_transfers/destination_movement_form', f: smf %>
+ <% end %>
+ <% end %>
+ <div data-stock-transfer-target="target"></div>
+ </div>
+ </div>
+ </div>
+ <%= render 'spree/admin/shared/new_resource_links', f: f %>
+ <% end %>
+</div>
diff --git a/admin/app/views/spree/admin/stock_transfers/show.html.erb b/admin/app/views/spree/admin/stock_transfers/show.html.erb
new file mode 100644
index 0000000000..558ee8e4fc
--- /dev/null
+++ b/admin/app/views/spree/admin/stock_transfers/show.html.erb
@@ -0,0 +1,58 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_stock_transfers_path %>
+ <%= @stock_transfer.number %>
+<% end %>
+<% content_for :title do %>
+ <%= @stock_transfer.number %>
+<% end %>
+<div class="row">
+ <div class="col-lg-8">
+ <div class="card mb-4">
+ <div class="card-header d-flex align-items-center justify-content-between">
+ <h5 class="card-title">
+ <%= Spree.t(:products) %>
+ </h5>
+ </div>
+ <div class="card-body p-0" style="min-height: 300px">
+ <div class="d-block overflow-y-auto overflow-x-hidden" style="max-height: 300px">
+ <div class="table-responsive rounded bg-white">
+ <table class="table sortable">
+ <thead>
+ <tr>
+ <th><%= Spree.t(:variant) %></th>
+ <th><%= Spree.t(:quantity) %></th>
+ <th><%= Spree.t(:count_on_hand) %></th>
+ </tr>
+ </thead>
+ <tbody>
+ <%= render collection: @stock_transfer.destination_movements.includes(stock_item: :variant), partial: 'spree/admin/stock_transfers/stock_movement', cached: spree_base_cache_scope %>
+ </tbody>
+ </table>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div class="col-lg-4">
+ <div class="card mb-4">
+ <div class="card-header d-flex align-items-center justify-content-between">
+ <h5 class="card-title">
+ <%= Spree.t(:details) %>
+ </h5>
+ </div>
+ <div class="card-body">
+ <ul class="list-group">
+ <li class="list-group-item d-flex justify-content-between">
+ <%= Spree.t(:origin) %> <span><%= @stock_transfer.source_location&.display_name.presence || Spree.t(:none) %></span>
+ </li>
+ <li class="list-group-item d-flex justify-content-between">
+ <%= Spree.t(:destination) %> <span><%= @stock_transfer.destination_location.display_name %></span>
+ </li>
+ <li class="list-group-item d-flex justify-content-between">
+ <%= Spree.t(:reference) %> <span><%= @stock_transfer.reference %></span>
+ </li>
+ </ul>
+ </div>
+ </div>
+ </div>
+</div>
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index d2481abe75..19bd450559 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -64,6 +64,10 @@ Spree::Core::Engine.add_routes do
resources :digitals, only: [:index, :create, :destroy]
end
+ # stock
+ resources :stock_items, only: [:index, :update, :destroy]
+ resources :stock_transfers, except: [:edit, :update]
+
# taxonomies and taxons
resources :taxonomies do
resources :taxons do
diff --git a/admin/spec/controllers/spree/admin/stock_items_controller_spec.rb b/admin/spec/controllers/spree/admin/stock_items_controller_spec.rb
new file mode 100644
index 0000000000..6250d764c3
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/stock_items_controller_spec.rb
@@ -0,0 +1,75 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::StockItemsController, type: :controller do
+ stub_authorization!
+
+ let!(:stock_location_1) { create(:stock_location, name: 'Stock location 1') }
+ let!(:stock_location_2) { create(:stock_location, name: 'Stock location 2') }
+
+ let!(:variant_1) { create(:variant, create_stock: false, sku: 'shirt-1', product: create(:base_product, name: 'Shirt')) }
+ let!(:variant_1_stock_1) { create(:stock_item, variant: variant_1, stock_location: stock_location_1) }
+ let!(:variant_1_stock_2) { create(:stock_item, variant: variant_1, stock_location: stock_location_2) }
+
+ let!(:variant_2) { create(:variant, create_stock: false, sku: 'shoes-2', product: create(:base_product, name: 'Shoes')) }
+ let!(:variant_2_stock_1) { create(:stock_item, variant: variant_2, stock_location: stock_location_1) }
+ let!(:variant_2_stock_2) { create(:stock_item, variant: variant_2, stock_location: stock_location_2) }
+
+ describe '#index' do
+ subject(:index) { get :index, params: params }
+
+ let(:params) { {} }
+
+ it 'lists all stock items' do
+ index
+ expect(response).to be_ok
+
+ expect(assigns[:collection]).to contain_exactly(
+ variant_1_stock_1, variant_1_stock_2,
+ variant_2_stock_1, variant_2_stock_2
+ )
+ end
+
+ describe 'search' do
+ let(:params) do
+ {
+ q: {
+ variant_product_name_cont: 'shoe'
+ }
+ }
+ end
+
+ it 'lists stock items searched by the variant name' do
+ index
+ expect(response).to be_ok
+
+ expect(assigns[:collection]).to contain_exactly(variant_2_stock_1, variant_2_stock_2)
+ end
+ end
+
+ describe 'filters' do
+ let(:params) { { q: q } }
+
+ context 'when filtering by SKU' do
+ let(:q) { { variant_sku_cont: 'shirt' } }
+
+ it 'lists stock items filtered by SKU' do
+ index
+ expect(response).to be_ok
+
+ expect(assigns[:collection]).to contain_exactly(variant_1_stock_1, variant_1_stock_2)
+ end
+ end
+
+ context 'when filtering by the location' do
+ let(:q) { { stock_location_id_eq: stock_location_2.id } }
+
+ it 'lists stock items filtered by the stock location' do
+ index
+ expect(response).to be_ok
+
+ expect(assigns[:collection]).to contain_exactly(variant_1_stock_2, variant_2_stock_2)
+ end
+ end
+ end
+ end
+end
diff --git a/admin/spec/controllers/spree/admin/stock_transfers_controller_spec.rb b/admin/spec/controllers/spree/admin/stock_transfers_controller_spec.rb
new file mode 100644
index 0000000000..e279ddd8fd
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/stock_transfers_controller_spec.rb
@@ -0,0 +1,69 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::StockTransfersController, type: :controller do
+ stub_authorization!
+
+ let(:source_location) { create(:stock_location) }
+ let(:destination_location) { create(:stock_location) }
+ let(:variant) { create(:variant) }
+ let(:source_stock_item) { source_location.stock_item_or_create(variant) }
+ let(:destination_stock_item) { destination_location.stock_item_or_create(variant) }
+
+ before do
+ source_stock_item.update(count_on_hand: 10)
+ destination_stock_item.update(count_on_hand: 0)
+ end
+
+ describe '#create' do
+ context 'when stock_movements_attributes are missing' do
+ it 'renders the new template with an error' do
+ post :create, params: { stock_transfer: { source_location_id: source_location.id, destination_location_id: destination_location.id } }
+
+ expect(response).to render_template(:new)
+ expect(flash[:error]).to eq(Spree.t('stock_transfer.errors.must_have_variant'))
+ expect(response.status).to eq(422)
+ end
+ end
+
+ context 'when stock_movements_attributes are present' do
+ it 'creates a stock transfer from source to destination and redirects to the stock transfer path' do
+ post :create, params: {
+ stock_transfer: {
+ source_location_id: source_location.id,
+ destination_location_id: destination_location.id,
+ stock_movements_attributes: {
+ '0' => { variant_id: variant.id, quantity: -10, originator_id: source_location.id },
+ '1' => { variant_id: variant.id, quantity: 10, originator_id: destination_location.id }
+ }
+ }
+ }
+
+ expect(response).to redirect_to(spree.admin_stock_transfer_path(assigns(:object)))
+ expect(assigns(:object).stock_movements.count).to eq(2)
+
+ expect(source_location.stock_items.find_by(variant: variant).count_on_hand).to eq(0)
+ expect(destination_location.stock_items.find_by(variant: variant).count_on_hand).to eq(10)
+ end
+ end
+
+ context 'when originator_id is null' do
+ it 'creates a stock transfer to destination and redirects to the stock transfer path' do
+ post :create, params: {
+ stock_transfer: {
+ source_location_id: source_location.id,
+ destination_location_id: destination_location.id,
+ stock_movements_attributes: {
+ '0' => { variant_id: variant.id, quantity: 10, originator_id: destination_location.id }
+ }
+ }
+ }
+
+ expect(response).to redirect_to(spree.admin_stock_transfer_path(assigns(:object)))
+ expect(assigns(:object).stock_movements.count).to eq(1)
+
+ expect(source_location.stock_items.find_by(variant: variant).count_on_hand).to eq(10)
+ expect(destination_location.stock_items.find_by(variant: variant).count_on_hand).to eq(10)
+ end
+ end
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/variant_factory.rb b/core/lib/spree/testing_support/factories/variant_factory.rb
index c543757b44..eb88e53c2b 100644
--- a/core/lib/spree/testing_support/factories/variant_factory.rb
+++ b/core/lib/spree/testing_support/factories/variant_factory.rb
@@ -15,14 +15,22 @@ FactoryBot.define do
product { |p| p.association(:base_product, stores: [Spree::Store.default]) }
option_values { [create(:option_value)] }
+ transient do
+ create_stock { true }
+ end
+
# ensure stock item will be created for this variant
- before(:create) { create(:stock_location) unless Spree::StockLocation.any? }
+ before(:create) do |variant, evaluator|
+ create(:stock_location) if evaluator.create_stock && !Spree::StockLocation.any?
+ end
- after(:create) do |variant|
- Spree::StockLocation.all.each do |stock_location|
- next if stock_location.stock_item(variant).present?
+ after(:create) do |variant, evaluator|
+ if evaluator.create_stock
+ Spree::StockLocation.all.each do |stock_location|
+ next if stock_location.stock_item(variant).present?
- stock_location.propagate_variant(variant)
+ stock_location.propagate_variant(variant)
+ end
end
end