diff --git a/app/views/admin/order_cycles/_exchange_supplied_products_form.html.haml b/app/views/admin/order_cycles/_exchange_supplied_products_form.html.haml index 092dfc6033..4523bd8337 100644 --- a/app/views/admin/order_cycles/_exchange_supplied_products_form.html.haml +++ b/app/views/admin/order_cycles/_exchange_supplied_products_form.html.haml @@ -1,10 +1,18 @@ / TODO: Unify this with exchange_distributed_products_form %td{:colspan => 3} .exchange-product{'ng-repeat' => 'product in enterprises[exchange.enterprise_id].supplied_products'} + .exchange-product-details = check_box_tag 'order_cycle_incoming_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}', 1, 1, 'ng-hide' => 'product.variants', 'ng-model' => 'exchange.variants[product.master_id]', 'ofn-sync-distributions' => '{{ product.master_id }}', 'id' => 'order_cycle_incoming_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}' %img{'ng-src' => '{{ product.image_url }}'} {{ product.name }} + + -# When the master variant is in the order cycle but the product has variants, we want to + -# be able to remove the master variant, since it serves no purpose. Display a checkbox to do so. + .exchange-product-variant{'ng-show' => 'exchange.variants[product.master_id] && product.variants'} + = check_box_tag 'order_cycle_incoming_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}', 1, 1, 'ng-model' => 'exchange.variants[product.master_id]', 'ofn-sync-distributions' => '{{ product.master_id }}', 'id' => 'order_cycle_incoming_exchange_{{ $parent.$index }}_variants_{{ product.master_id }}' + Obsolete master + .exchange-product-variant{'ng-repeat' => 'variant in product.variants'} = check_box_tag 'order_cycle_incoming_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}', 1, 1, 'ng-model' => 'exchange.variants[variant.id]', 'ofn-sync-distributions' => '{{ variant.id }}', 'id' => 'order_cycle_incoming_exchange_{{ $parent.$parent.$index }}_variants_{{ variant.id }}' {{ variant.label }} diff --git a/spec/features/admin/order_cycles_spec.rb b/spec/features/admin/order_cycles_spec.rb index 5cadd11a29..6d5c9e17f2 100644 --- a/spec/features/admin/order_cycles_spec.rb +++ b/spec/features/admin/order_cycles_spec.rb @@ -391,7 +391,33 @@ feature %q{ end - context 'as an Enterprise user' do + scenario "removing a master variant from an order cycle when further variants have been added" do + # Given a product with a variant, with its master variant included in the order cycle + # (this usually happens when a product is added to an order cycle, then variants are added + # to the product after the fact) + s = create(:supplier_enterprise) + p = create(:simple_product, supplier: s) + v = create(:variant, product: p) + d = create(:distributor_enterprise) + oc = create(:simple_order_cycle, suppliers: [s], distributors: [d], variants: [p.master]) + exchange_ids = oc.exchanges.pluck :id + ExchangeVariant.where(exchange_id: exchange_ids, variant_id: p.master.id).should_not be_empty + + # When I go to the order cycle page and remove the obsolete master + login_to_admin_section + click_link 'Order Cycles' + click_link oc.name + within("table.exchanges tbody tr.supplier") { page.find('td.products input').click } + page.find("#order_cycle_incoming_exchange_0_variants_#{p.master.id}", visible: true).click # uncheck + click_button "Update" + + # Then the master variant should have been removed from all exchanges + page.should have_content "Your order cycle has been updated." + ExchangeVariant.where(exchange_id: exchange_ids, variant_id: p.master.id).should be_empty + end + + + context "as an enterprise user" do let(:supplier1) { create(:supplier_enterprise, name: 'First Supplier') } let(:supplier2) { create(:supplier_enterprise, name: 'Another Supplier') }