Remove product from Order Cycles if supplier changes as with a new supplier the rules/permissions to add a product to an Order Cycle may be different

This commit is contained in:
luisramos0
2019-10-16 16:27:05 +01:00
parent e46875376e
commit 49f98422fd
2 changed files with 23 additions and 0 deletions

View File

@@ -46,8 +46,11 @@ Spree::Admin::ProductsController.class_eval do
end
def update
original_supplier = @product.supplier_id
delete_stock_params_and_set_after do
super
remove_product_from_order_cycles if original_supplier != @product.supplier_id
end
end
@@ -190,6 +193,13 @@ Spree::Admin::ProductsController.class_eval do
end
end
def remove_product_from_order_cycles
variant_ids = @product.variants.map(&:id)
ExchangeVariant.
where(variant_id: variant_ids).
delete_all
end
def set_product_master_variant_price_to_zero
@product.price = 0 if @product.price.nil?
end

View File

@@ -177,6 +177,19 @@ describe Spree::Admin::ProductsController, type: :controller do
login_as_enterprise_user [producer]
end
describe "change product supplier" do
let(:distributor) { create(:distributor_enterprise) }
let!(:order_cycle) { create(:simple_order_cycle, variants: [product.variants.first], coordinator: distributor, distributors: [distributor]) }
it "should remove product from existing Order Cycles" do
new_producer = create(:enterprise)
spree_put :update, id: product, product: { supplier_id: new_producer.id }
expect(product.reload.supplier.id).to eq new_producer.id
expect(order_cycle.reload.distributed_variants).to_not include product.variants.first
end
end
describe "product stock setting with errors" do
it "notifies bugsnag and still raise error" do
# forces an error in the variant