mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user