From 898e41acfb6ab7dada1eaa381a89910f3e94520b Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 19 Aug 2018 20:23:31 +0100 Subject: [PATCH 1/2] Removed monkeypatchs to scope variants on inventory_unit and line_item decorators. We need to make the scoping somewhere else in spree2, see #2559 --- app/models/spree/inventory_unit_decorator.rb | 16 ----------- app/models/spree/line_item_decorator.rb | 28 -------------------- 2 files changed, 44 deletions(-) delete mode 100644 app/models/spree/inventory_unit_decorator.rb diff --git a/app/models/spree/inventory_unit_decorator.rb b/app/models/spree/inventory_unit_decorator.rb deleted file mode 100644 index 9868596b41..0000000000 --- a/app/models/spree/inventory_unit_decorator.rb +++ /dev/null @@ -1,16 +0,0 @@ -module Spree - InventoryUnit.class_eval do - def self.assign_opening_inventory(order) - return [] unless order.completed? - - #increase inventory to meet initial requirements - scoper = OpenFoodNetwork::ScopeVariantToHub.new(order.distributor) - order.line_items.each do |line_item| - # Scope variant to hub so that stock levels may be subtracted from VariantOverride. - scoper.scope(line_item.variant) - - increase(order, line_item.variant, line_item.quantity) - end - end - end -end diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 6f74e19530..7af42821f6 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -110,34 +110,6 @@ Spree::LineItem.class_eval do final_weight_volume / quantity end - # MONKEYPATCH of Spree method - # Enables scoping of variant to hub/shop, stock drawn down from inventory - def update_inventory - return true unless order.completed? - - scoper.scope(variant) # this line added - - if new_record? - Spree::InventoryUnit.increase(order, variant, quantity) - elsif old_quantity = self.changed_attributes['quantity'] - if old_quantity < quantity - Spree::InventoryUnit.increase(order, variant, (quantity - old_quantity)) - elsif old_quantity > quantity - Spree::InventoryUnit.decrease(order, variant, (old_quantity - quantity)) - end - end - end - - # MONKEYPATCH of Spree method - # Enables scoping of variant to hub/shop, stock replaced to inventory - def remove_inventory - return true unless order.completed? - - scoper.scope(variant) # this line added - - Spree::InventoryUnit.decrease(order, variant, quantity) - end - # MONKEYPATCH of Spree method # Enables scoping of variant to hub/shop, so we check stock against relevant overrides if they exist # Also skips stock check if requested quantity is zero From a97138dd58245a37321d9ac4cac9e0340df87d26 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 22 Aug 2018 23:20:30 +0100 Subject: [PATCH 2/2] Fixed bulk_line_items_controller_spec by setting shipment on test line_items and by allowing shipment decorator to allow no adjustments in the shipment --- app/models/spree/shipment_decorator.rb | 1 + .../admin/bulk_line_items_controller_spec.rb | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/models/spree/shipment_decorator.rb b/app/models/spree/shipment_decorator.rb index f544d95517..625cf06dec 100644 --- a/app/models/spree/shipment_decorator.rb +++ b/app/models/spree/shipment_decorator.rb @@ -2,6 +2,7 @@ module Spree Shipment.class_eval do def ensure_correct_adjustment_with_included_tax ensure_correct_adjustment_without_included_tax + return unless adjustment if Config.shipment_inc_vat && (order.distributor.nil? || order.distributor.charges_sales_tax) adjustment.set_included_tax! Config.shipping_tax_rate diff --git a/spec/controllers/admin/bulk_line_items_controller_spec.rb b/spec/controllers/admin/bulk_line_items_controller_spec.rb index dd5c1c6326..cc4201d0ce 100644 --- a/spec/controllers/admin/bulk_line_items_controller_spec.rb +++ b/spec/controllers/admin/bulk_line_items_controller_spec.rb @@ -11,10 +11,13 @@ describe Admin::BulkLineItemsController, type: :controller do let!(:order1) { FactoryBot.create(:order, state: 'complete', completed_at: 1.day.ago, distributor: dist1, billing_address: FactoryBot.create(:address) ) } let!(:order2) { FactoryBot.create(:order, state: 'complete', completed_at: Time.zone.now, distributor: dist1, billing_address: FactoryBot.create(:address) ) } let!(:order3) { FactoryBot.create(:order, state: 'complete', completed_at: Time.zone.now, distributor: dist1, billing_address: FactoryBot.create(:address) ) } - let!(:line_item1) { FactoryBot.create(:line_item, order: order1) } - let!(:line_item2) { FactoryBot.create(:line_item, order: order2) } - let!(:line_item3) { FactoryBot.create(:line_item, order: order2) } - let!(:line_item4) { FactoryBot.create(:line_item, order: order3) } + let(:shipment1) { FactoryBot.create(:shipment, order:order1) } + let(:shipment2) { FactoryBot.create(:shipment, order:order2) } + let(:shipment3) { FactoryBot.create(:shipment, order:order3) } + let!(:line_item1) { FactoryBot.create(:line_item, order: order1, target_shipment: shipment1) } + let!(:line_item2) { FactoryBot.create(:line_item, order: order2, target_shipment: shipment2) } + let!(:line_item3) { FactoryBot.create(:line_item, order: order2, target_shipment: shipment2) } + let!(:line_item4) { FactoryBot.create(:line_item, order: order3, target_shipment: shipment3) } context "as a normal user" do before { controller.stub spree_current_user: create_enterprise_user } @@ -83,10 +86,12 @@ describe Admin::BulkLineItemsController, type: :controller do let(:coordinator) { create(:distributor_enterprise) } let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator) } let!(:order1) { FactoryBot.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now, distributor: distributor1, billing_address: FactoryBot.create(:address) ) } - let!(:line_item1) { FactoryBot.create(:line_item, order: order1, product: FactoryBot.create(:product, supplier: supplier)) } - let!(:line_item2) { FactoryBot.create(:line_item, order: order1, product: FactoryBot.create(:product, supplier: supplier)) } + let(:shipment1) { FactoryBot.create(:shipment, order:order1) } + let(:shipment2) { FactoryBot.create(:shipment, order:order2) } + let!(:line_item1) { FactoryBot.create(:line_item, order: order1, target_shipment: shipment1, product: FactoryBot.create(:product, supplier: supplier)) } + let!(:line_item2) { FactoryBot.create(:line_item, order: order1, target_shipment: shipment1, product: FactoryBot.create(:product, supplier: supplier)) } let!(:order2) { FactoryBot.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now, distributor: distributor2, billing_address: FactoryBot.create(:address) ) } - let!(:line_item3) { FactoryBot.create(:line_item, order: order2, product: FactoryBot.create(:product, supplier: supplier)) } + let!(:line_item3) { FactoryBot.create(:line_item, order: order2, target_shipment: shipment2, product: FactoryBot.create(:product, supplier: supplier)) } context "producer enterprise" do before do @@ -131,7 +136,12 @@ describe Admin::BulkLineItemsController, type: :controller do let(:coordinator) { create(:distributor_enterprise) } let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator) } let!(:order1) { FactoryBot.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now, distributor: distributor1, billing_address: FactoryBot.create(:address) ) } - let!(:line_item1) { FactoryBot.create(:line_item, order: order1, product: FactoryBot.create(:product, supplier: supplier)) } + let(:shipment1) { FactoryBot.create(:shipment, order:order1) } + let!(:line_item1) { + line_item1 = FactoryBot.create(:line_item, order: order1, target_shipment: shipment1, product: FactoryBot.create(:product, supplier: supplier)) + # make sure target_shipment is available through db reloads of this line_item + line_item1.tap(&:save!) + } let(:line_item_params) { { quantity: 3, final_weight_volume: 3000, price: 3.00 } } let(:params) { { id: line_item1.id, order_id: order1.number, line_item: line_item_params } } @@ -227,7 +237,8 @@ describe Admin::BulkLineItemsController, type: :controller do let(:coordinator) { create(:distributor_enterprise) } let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator) } let!(:order1) { FactoryBot.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now, distributor: distributor1, billing_address: FactoryBot.create(:address) ) } - let!(:line_item1) { FactoryBot.create(:line_item, order: order1, product: FactoryBot.create(:product, supplier: supplier)) } + let(:shipment1) { FactoryBot.create(:shipment, order:order1) } + let!(:line_item1) { FactoryBot.create(:line_item, order: order1, target_shipment: shipment1, product: FactoryBot.create(:product, supplier: supplier)) } let(:params) { { id: line_item1.id, order_id: order1.number } } before do