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

Bug fix/update inventory #213

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
partsTable.on("click", "a.set_count_admin_product_part_link", function() {
var params;
params = {
count: $("input", $(this).parent().parent()).val()
count: $("input#count", $(this).parent().parent()).val(),
change_part_id: $("select", $(this).parent().parent()).val()
};
return makePostRequest($(this), params);
});
Expand Down
124 changes: 74 additions & 50 deletions app/controllers/spree/admin/parts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,87 @@
class Spree::Admin::PartsController < Spree::Admin::BaseController
helper_method :product
module Spree
module Admin
class PartsController < Spree::Admin::BaseController
helper_method :product
before_action :find_assembly_part, only: [:set_count, :remove]

def index
@parts = product.assemblies_parts.includes(:assembly, :part)
end
def index
@parts = product.assemblies_parts.includes(:assembly, :part)
end

def remove
assembly_part = Spree::AssembliesPart.find(params[:id])
assembly_part.destroy
render 'spree/admin/parts/update_parts_table'
end
def remove
@assembly_part.delete_all if @assembly_part.present?
render 'spree/admin/parts/update_parts_table'
end

def set_count
save_part(existing_part_params)
end
def set_count
@assembly_part.update_all(count: params[:count], part_id: params[:change_part_id]) if @assembly_part.present?
render 'spree/admin/parts/update_parts_table'
rescue Exception => e
render 'spree/admin/parts/update_parts_table', status: 422, json: e.to_json
end

def available
if params[:q].blank?
@available_products = []
else
query = "%#{params[:q]}%"
@available_products = Spree::Product.search_can_be_part(query).distinct!
end
respond_to do |format|
format.html {render :layout => false}
format.js {render :layout => false}
end
end
def available
if params[:q].blank?
@available_products = []
else
query = "%#{params[:q]}%"
@available_products = Spree::Product.search_can_be_part(query).distinct!
end
respond_to do |format|
format.html { render layout: false }
format.js { render layout: false }
end
end

def create
save_part(new_part_params)
end
def create
save_part(new_part_params)
end

private
def update_positions
params[:positions].each do |id, index|
Spree::AssembliesPart.where(assembly_id: params[:assembly_id], part_id: id).update_all(position: index)
end

def save_part(part_params)
form = Spree::AssignPartToBundleForm.new(product, part_params)
if form.submit
render 'spree/admin/parts/update_parts_table'
else
error_message = form.errors.full_messages.to_sentence
render json: error_message.to_json, status: 422
product = Spree::Product.find_by_slug(params[:product_id])
product.touch if product.present?

respond_to do |format|
format.js { render plain: 'Ok' }
end
end
end

def product
@product ||= Spree::Product.find_by(slug: params[:product_id])
end
private

def new_part_params
params.require(:assemblies_part).permit(
:count,
:part_id,
:assembly_id,
:variant_selection_deferred
)
end
def save_part(part_params)
form = Spree::AssignPartToBundleForm.new(product, part_params)
if form.submit
render 'spree/admin/parts/update_parts_table'
else
error_message = form.errors.full_messages.to_sentence
render json: error_message.to_json, status: 422
end
end

def product
@product ||= Spree::Product.find_by(slug: params[:product_id])
end

def new_part_params
params.require(:assemblies_part).permit(
:count,
:part_id,
:assembly_id,
:variant_selection_deferred
)
end

def existing_part_params
params.permit(:id, :count)
def existing_part_params
params.permit(:id, :count, :part_id)
end

def find_assembly_part
@assembly_part = Spree::AssembliesPart.where(assembly_id: params[:id], part_id: params[:part_id])
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/spree/assemblies_part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class AssembliesPart < ActiveRecord::Base

belongs_to :part, class_name: "Spree::Variant", foreign_key: "part_id"

default_scope { order(:position) }
delegate :name, :sku, to: :part

after_create :set_master_unlimited_stock

def self.get(assembly_id, part_id)
Expand Down
2 changes: 1 addition & 1 deletion app/models/spree/line_item_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def quantity_by_variant
private

def update_inventory
if (changed? || target_shipment.present?) &&
if (saved_changes? || target_shipment.present?) &&
order.has_checkout_step?("delivery")
if product.assembly?
Spree::OrderInventoryAssembly.new(self).verify(target_shipment)
Expand Down
2 changes: 1 addition & 1 deletion app/services/spree/cart/add_item_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def add_to_line_item(order:, variant:, quantity: nil, options: {})
end

line_item.target_shipment = options[:shipment] if options.key? :shipment
line_item.save!
return failure(line_item) unless line_item.save

line_item.reload.update_price

Expand Down
48 changes: 36 additions & 12 deletions app/views/spree/admin/parts/_parts_table.html.erb
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
<table class="table">
<table class="table sortable" data-sortable-link="<%= update_positions_admin_product_parts_url(assembly_id: parts&.first&.assembly&.id) %>">
<thead>
<tr>
<th><%= Spree.t(:sku) %></th>
<th><%= Spree.t(:name) %></th>
<tr>
<th></th>
<th><%= Spree.t(:sku) %></th>
<th><%= Spree.t(:name) %></th>
<th><%= Spree.t(:options) %></th>
<th><%= Spree.t(:variant) %></th>
<th><%= Spree.t(:qty) %></th>
</tr>
<th><%= Spree.t(:qty) %></th>
<th class="actions"></th>
<th>Stock</th>
</tr>
</thead>
<tbody>
<% parts.each do |part| %>
<tr id="<%= dom_id(part, :sel)%>">
<% next unless part.part %>
<%= hidden_field_tag "assembly[id]", part.assembly.id %>
<tr id="spree_part_<%= part.part_id %>">
<td class="handle move-handle">
<span class="icon icon-move handle"></span>
</td>
<td><%= part.sku %></td>
<td><%= part.name %></td>
<td><%= part.options_text %></td>
<td>
<% if part.part.product.has_variants? %>
<%- opts = part.part.product.variants.map {|v| [v.options_text, v.id] } %>
<%= select_tag "part[id]",
options_for_select(opts, part&.part_id),
include_blank: Spree.t(:user_selectable), class: 'form-control'
%>
<% else %>
<%= hidden_field_tag "part[id]", part.part.master.id %>
<%= Spree.t(:no_variants) %>
<% end %>
</td>


<td><%= variant_including_master_options part.assembly %></td>
<td><%= text_field_tag :count, part.count, class: "form-control" %></td>
<td class="actions">
<td class="actions">
<%= image_tag "spinner.gif", :style => "display:none", :class => "spinner" %>
<%= link_to_with_icon("edit", Spree.t("edit"),
set_count_admin_product_part_url(product, part),
set_count_admin_product_part_url(product, id: part.assembly.id, part_id: part.part_id),
:class => "set_count_admin_product_part_link btn btn-primary btn-sm", :no_text => true) %>

<%= link_to_with_icon("delete", Spree.t("delete"),
remove_admin_product_part_url(product, part),
remove_admin_product_part_url(product, id: part.assembly.id, part_id: part.part_id),
:class => "remove_admin_product_part_link btn btn-danger btn-sm delete-resource", :no_text => true) %>
</td>
</td>
<td>
<%= part.part.stock_items.where(state: "available").sum(:count_on_hand) %>
</td>
</tr>
<% end %>
<% if parts.empty? %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
collection do
post :available
get :selected
post :update_positions
end
end
end
Expand Down