From 49f98422fdf2b35d2483c98a02bc22afdb41c3cf Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 16 Oct 2019 16:27:05 +0100 Subject: [PATCH] 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 --- .../spree/admin/products_controller_decorator.rb | 10 ++++++++++ .../spree/admin/products_controller_spec.rb | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index 3987de1577..10742f51cb 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -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 diff --git a/spec/controllers/spree/admin/products_controller_spec.rb b/spec/controllers/spree/admin/products_controller_spec.rb index 7a73954f42..e8ae658e9e 100644 --- a/spec/controllers/spree/admin/products_controller_spec.rb +++ b/spec/controllers/spree/admin/products_controller_spec.rb @@ -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