From 86391093723a8dab52e5c53db38168fe86c1f3ab Mon Sep 17 00:00:00 2001 From: Lynne Davis Date: Tue, 4 Aug 2015 20:13:28 +0100 Subject: [PATCH] Variable weights improvements additions --- .../admin/bulk_order_management.js.coffee | 17 +++++++--- app/models/spree/line_item_decorator.rb | 4 +-- app/models/spree/order_decorator.rb | 2 +- .../api/admin/line_item_serializer.rb | 6 ++-- .../admin/orders/bulk_management.html.haml | 10 +++--- ...50719153136_rename_line_item_unit_value.rb | 5 +++ ...cision_on_line_item_final_weight_volume.rb | 11 +++++++ ...ate_spree_line_item_final_weight_volume.rb | 9 ++++++ .../spree/api/line_items_controller_spec.rb | 4 +-- .../admin/bulk_order_management_spec.rb | 30 ++++++++++++++++-- .../unit/bulk_order_management_spec.js.coffee | 31 +++++++++++++++---- 11 files changed, 102 insertions(+), 27 deletions(-) create mode 100644 db/migrate/20150719153136_rename_line_item_unit_value.rb create mode 100644 db/migrate/20150719153732_update_precision_on_line_item_final_weight_volume.rb create mode 100644 db/migrate/20150730160010_update_spree_line_item_final_weight_volume.rb diff --git a/app/assets/javascripts/admin/bulk_order_management.js.coffee b/app/assets/javascripts/admin/bulk_order_management.js.coffee index 5c2b5b8984..8cd3d5b204 100644 --- a/app/assets/javascripts/admin/bulk_order_management.js.coffee +++ b/app/assets/javascripts/admin/bulk_order_management.js.coffee @@ -30,7 +30,7 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [ variant: { name: "Variant", visible: true } quantity: { name: "Quantity", visible: true } max: { name: "Max", visible: true } - unit_value: { name: "Weight/Volume", visible: false } + final_weight_volume: { name: "Weight/Volume", visible: false } price: { name: "Price", visible: false } $scope.initialise = -> $scope.initialiseVariables() @@ -166,10 +166,10 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [ $scope.weightAdjustedPrice = (lineItem, oldValue) -> if oldValue <= 0 - oldValue = lineItem.units_variant.unit_value - if lineItem.unit_value <= 0 - lineItem.unit_value = lineItem.units_variant.unit_value - lineItem.price = lineItem.price * lineItem.unit_value / oldValue + oldValue = lineItem.units_variant.unit_value * line_item.quantity + if lineItem.final_weight_volume <= 0 + lineItem.final_weight_volume = lineItem.units_variant.unit_value * lineItem.quantity + lineItem.price = lineItem.price * lineItem.final_weight_volume / oldValue #$scope.bulk_order_form.line_item.price.$setViewValue($scope.bulk_order_form.line_item.price.$viewValue) $scope.unitValueLessThanZero = (lineItem) -> @@ -178,6 +178,13 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [ else false + $scope.updateOnQuantity = (lineItem, oldQuantity) -> + if lineItem.quantity <= 0 + lineItem.quantity = 1 + # reset price to original unit value + lineItem.price = lineItem.price * (oldQuantity * lineItem.units_variant.unit_value) / lineItem.final_weight_volume + lineItem.final_weight_volume = lineItem.units_variant.unit_value * lineItem.quantity + $scope.$watch "orderCycleFilter", (newVal, oldVal) -> unless $scope.orderCycleFilter == "0" || angular.equals(newVal, oldVal) $scope.startDate = $scope.orderCyclesByID[$scope.orderCycleFilter].first_order diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index f64f197d56..0648c7919f 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -1,6 +1,6 @@ Spree::LineItem.class_eval do - attr_accessible :max_quantity, :unit_value - attr_accessible :unit_value, :price, :as => :api + attr_accessible :max_quantity, :final_weight_volume + attr_accessible :final_weight_volume, :price, :as => :api # -- Scopes scope :managed_by, lambda { |user| diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index df478d8d2f..d3f1c6c5da 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -130,7 +130,7 @@ Spree::Order.class_eval do else current_item = Spree::LineItem.new(:quantity => quantity, max_quantity: max_quantity) current_item.variant = variant - current_item.unit_value = variant.unit_value + current_item.final_weight_volume = variant.unit_value * quantity if currency current_item.currency = currency unless currency.nil? current_item.price = variant.price_in(currency).amount diff --git a/app/serializers/api/admin/line_item_serializer.rb b/app/serializers/api/admin/line_item_serializer.rb index 21fde91145..d12e3b291a 100644 --- a/app/serializers/api/admin/line_item_serializer.rb +++ b/app/serializers/api/admin/line_item_serializer.rb @@ -1,5 +1,5 @@ class Api::Admin::LineItemSerializer < ActiveModel::Serializer - attributes :id, :quantity, :max_quantity, :supplier, :price, :unit_value, :units_product, :units_variant + attributes :id, :quantity, :max_quantity, :supplier, :price, :final_weight_volume, :units_product, :units_variant def supplier Api::Admin::IdNameSerializer.new(object.product.supplier).serializable_hash @@ -13,7 +13,7 @@ class Api::Admin::LineItemSerializer < ActiveModel::Serializer Api::Admin::UnitsVariantSerializer.new(object.variant).serializable_hash end - def unit_value - object.unit_value.to_f + def final_weight_volume + object.final_weight_volume.to_f end end diff --git a/app/views/spree/admin/orders/bulk_management.html.haml b/app/views/spree/admin/orders/bulk_management.html.haml index 71134e38a9..0e4c69b695 100644 --- a/app/views/spree/admin/orders/bulk_management.html.haml +++ b/app/views/spree/admin/orders/bulk_management.html.haml @@ -126,7 +126,7 @@ %a{ :href => '', 'ng-click' => "predicate = 'units_variant.full_name'; reverse = !reverse" } Product: Unit %th.quantity{ 'ng-show' => 'columns.quantity.visible' } Quantity %th.max{ 'ng-show' => 'columns.max.visible' } Max - %th.unit_value{ 'ng-show' => 'columns.unit_value.visible' } Weight/Volume + %th.final_weight_volume{ 'ng-show' => 'columns.final_weight_volume.visible' } Weight/Volume %th.price{ 'ng-show' => 'columns.price.visible' } Price %th.actions %th.actions @@ -146,12 +146,12 @@ %td.variant{ 'ng-show' => 'columns.variant.visible' } %a{ :href => '#', 'ng-click' => "setSelectedUnitsVariant(line_item.units_product,line_item.units_variant)" } {{ line_item.units_variant.full_name }} %td.quantity{ 'ng-show' => 'columns.quantity.visible' } - %input{ :type => 'number', :name => 'quantity', 'ng-model' => "line_item.quantity", 'obj-for-update' => "line_item", "attr-for-update" => "quantity" } + %input{ :type => 'number', :name => 'quantity', :id => 'quantity', :value => 'line_item.quantity', 'ng-model' => "line_item.quantity", 'ng-change' => "updateOnQuantity(line_item, {{ line_item.quantity }})", 'obj-for-update' => "line_item", "attr-for-update" => "quantity" } %td.max{ 'ng-show' => 'columns.max.visible' } {{ line_item.max_quantity }} - %td.unit_value{ 'ng-show' => 'columns.unit_value.visible' } - %input{ :type => 'number', :name => 'unit_value', :id => 'unit_value', 'ng-model' => "line_item.unit_value", 'ng-readonly' => "unitValueLessThanZero(line_item)", 'ng-change' => "weightAdjustedPrice(line_item, {{ line_item.unit_value }})", 'obj-for-update' => "line_item", "attr-for-update" => "unit_value" } + %td.final_weight_volume{ 'ng-show' => 'columns.final_weight_volume.visible' } + %input{ :type => 'text', :name => 'final_weight_volume', :id => 'final_weight_volume', :value => 'line_item.final_weight_volume', 'ng-model' => "line_item.final_weight_volume", 'ng-readonly' => "unitValueLessThanZero(line_item)", 'ng-change' => "weightAdjustedPrice(line_item, {{ line_item.final_weight_volume }})", 'obj-for-update' => "line_item", "attr-for-update" => "final_weight_volume" } %td.price{ 'ng-show' => 'columns.price.visible' } - %input{ :type => 'text', :name => 'price', :id => 'price', :value => '{{ line_item.price | currency }}', 'ng-readonly' => "true", 'obj-for-update' => "line_item", "attr-for-update" => "price" } + %input{ :type => 'text', :name => 'price', :id => 'price', :value => '{{ line_item.price * line_item.quantity | currency }}', 'ng-readonly' => "true", 'obj-for-update' => "line_item", "attr-for-update" => "price" } %td.actions %a{ :class => "edit-order icon-edit no-text", 'ofn-confirm-link-path' => "/admin/orders/{{line_item.order.number}}/edit" } %td.actions diff --git a/db/migrate/20150719153136_rename_line_item_unit_value.rb b/db/migrate/20150719153136_rename_line_item_unit_value.rb new file mode 100644 index 0000000000..9dbdce75c5 --- /dev/null +++ b/db/migrate/20150719153136_rename_line_item_unit_value.rb @@ -0,0 +1,5 @@ +class RenameLineItemUnitValue < ActiveRecord::Migration + def change + rename_column :spree_line_items, :unit_value, :final_weight_volume + end +end diff --git a/db/migrate/20150719153732_update_precision_on_line_item_final_weight_volume.rb b/db/migrate/20150719153732_update_precision_on_line_item_final_weight_volume.rb new file mode 100644 index 0000000000..c1b3482c0f --- /dev/null +++ b/db/migrate/20150719153732_update_precision_on_line_item_final_weight_volume.rb @@ -0,0 +1,11 @@ +class UpdatePrecisionOnLineItemFinalWeightVolume < ActiveRecord::Migration + def up + change_column :spree_line_items, :final_weight_volume, :decimal, :precision => 10, :scale => 2 + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end + + diff --git a/db/migrate/20150730160010_update_spree_line_item_final_weight_volume.rb b/db/migrate/20150730160010_update_spree_line_item_final_weight_volume.rb new file mode 100644 index 0000000000..4d51a7160b --- /dev/null +++ b/db/migrate/20150730160010_update_spree_line_item_final_weight_volume.rb @@ -0,0 +1,9 @@ +class UpdateSpreeLineItemFinalWeightVolume < ActiveRecord::Migration + def up + execute "UPDATE spree_line_items SET final_weight_volume = final_weight_volume * quantity" + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/spec/controllers/spree/api/line_items_controller_spec.rb b/spec/controllers/spree/api/line_items_controller_spec.rb index 37ec50eb7e..abb355f0f3 100644 --- a/spec/controllers/spree/api/line_items_controller_spec.rb +++ b/spec/controllers/spree/api/line_items_controller_spec.rb @@ -11,7 +11,7 @@ module Spree def self.make_simple_data! let!(:order) { FactoryGirl.create(:order, state: 'complete', completed_at: Time.now) } - let!(:line_item) { FactoryGirl.create(:line_item, order: order, unit_value: 500) } + let!(:line_item) { FactoryGirl.create(:line_item, order: order, final_weight_volume: 500) } end #test that when a line item is updated, an order's fees are updated too @@ -21,7 +21,7 @@ module Spree context "as a line item is updated" do it "update distribution charge on the order" do - line_item_params = { order_id: order.number, id: line_item.id, line_item: { id: line_item.id, unit_value: 520 }, format: :json} + line_item_params = { order_id: order.number, id: line_item.id, line_item: { id: line_item.id, final_weight_volume: 520 }, format: :json} allow(controller).to receive(:order) { order } expect(order).to receive(:update_distribution_charge!) spree_post :update, line_item_params diff --git a/spec/features/admin/bulk_order_management_spec.rb b/spec/features/admin/bulk_order_management_spec.rb index 3bbb23bb8a..f37ea911b1 100644 --- a/spec/features/admin/bulk_order_management_spec.rb +++ b/spec/features/admin/bulk_order_management_spec.rb @@ -144,7 +144,7 @@ feature %q{ let!(:p1) { FactoryGirl.create(:product_with_option_types, group_buy: true, group_buy_unit_size: 5000, variant_unit: "weight", variants: [FactoryGirl.create(:variant, unit_value: 1000)] ) } let!(:v1) { p1.variants.first } let!(:o1) { FactoryGirl.create(:order_with_distributor, state: 'complete', completed_at: Time.now ) } - let!(:li1) { FactoryGirl.create(:line_item, order: o1, variant: v1, :quantity => 5, :unit_value => 1000 ) } + let!(:li1) { FactoryGirl.create(:line_item, order: o1, variant: v1, :quantity => 5, :final_weight_volume => 1000 ) } context "modifying the weight/volume of a line item" do it "update-pending is added to variable 'price'" do @@ -152,12 +152,36 @@ feature %q{ first("div#columns_dropdown", :text => "COLUMNS").click first("div#columns_dropdown div.menu div.menu_item", text: "Weight/Volume").click page.should_not have_css "input[name='price'].update-pending" - li1_unit_value_column = find("tr#li_#{li1.id} td.unit_value") - li1_unit_value_column.fill_in "unit_value", :with => 1200 + li1_final_weight_volume_column = find("tr#li_#{li1.id} td.final_weight_volume") + li1_final_weight_volume_column.fill_in "final_weight_volume", :with => 1200 page.should have_css "input[name='price'].update-pending", :visible => false end end + context "modifying the quantity of a line item" do + it "update-pending is added to variable 'price'" do + visit '/admin/orders/bulk_management' + #first("div#columns_dropdown", :text => "COLUMNS").click + #first("div#columns_dropdown div.menu div.menu_item", text: "Quantity").click + page.should_not have_css "input[name='price'].update-pending" + li1_quantity_column = find("tr#li_#{li1.id} td.quantity") + li1_quantity_column.fill_in "quantity", :with => 6 + page.should have_css "input[name='price'].update-pending", :visible => false + end + end + + context "modifying the quantity of a line item" do + it "update-pending is added to variable 'weight/volume'" do + visit '/admin/orders/bulk_management' + first("div#columns_dropdown", :text => "COLUMNS").click + first("div#columns_dropdown div.menu div.menu_item", text: "Weight/Volume").click + page.should_not have_css "input[name='price'].update-pending" + li1_quantity_column = find("tr#li_#{li1.id} td.quantity") + li1_quantity_column.fill_in "quantity", :with => 6 + page.should have_css "input[name='final_weight_volume'].update-pending", :visible => false + end + end + context "using column display toggle" do it "shows a column display toggle button, which shows a list of columns when clicked" do visit '/admin/orders/bulk_management' diff --git a/spec/javascripts/unit/bulk_order_management_spec.js.coffee b/spec/javascripts/unit/bulk_order_management_spec.js.coffee index 7c1fe5da23..8ce465bb5b 100644 --- a/spec/javascripts/unit/bulk_order_management_spec.js.coffee +++ b/spec/javascripts/unit/bulk_order_management_spec.js.coffee @@ -354,25 +354,44 @@ describe "AdminOrderMgmtCtrl", -> it "resets the weight if the weight is set to zero", -> scope.filteredLineItems = [ - { units_variant: { unit_value: 100 }, price: 2, unit_value: 0 } + { units_variant: { unit_value: 100 }, price: 2, quantity: 1, final_weight_volume: 0 } ] expect(scope.weightAdjustedPrice(scope.filteredLineItems[0], 100)).toEqual scope.filteredLineItems[0].price it "updates the price if the weight is changed", -> scope.filteredLineItems = [ - { units_variant: { unit_value: 100 }, price: 2, unit_value: 200 } + { units_variant: { unit_value: 100 }, price: 2, final_weight_volume: 200 } ] old_value = scope.filteredLineItems[0].units_variant.unit_value - new_value = scope.filteredLineItems[0].unit_value + new_value = scope.filteredLineItems[0].final_weight_volume sp = scope.filteredLineItems[0].price * new_value / old_value expect(scope.weightAdjustedPrice(scope.filteredLineItems[0], old_value)).toEqual sp + it "updates the weight if the quantity is changed", -> + scope.filteredLineItems = [ + { units_variant: { unit_value: 150 }, price: 1, final_weight_volume: 100, quantity: 2 } + ] + old_value = 1 + nw = scope.filteredLineItems[0].units_variant.unit_value * scope.filteredLineItems[0].quantity + scope.updateOnQuantity(scope.filteredLineItems[0], old_value) + expect(scope.filteredLineItems[0].final_weight_volume).toEqual nw + + it "updates the price if the quantity is changed", -> + scope.filteredLineItems = [ + { units_variant: { unit_value: 150 }, price: 21, final_weight_volume: 100, quantity: 2 } + ] + old_value = 1 + np = scope.filteredLineItems[0].price * (old_value * scope.filteredLineItems[0].units_variant.unit_value) / scope.filteredLineItems[0].final_weight_volume + scope.updateOnQuantity(scope.filteredLineItems[0], old_value) + expect(scope.filteredLineItems[0].price).toEqual np + + it "doesn't update the price if the weight is not changed", -> scope.filteredLineItems = [ - { units_variant: { unit_value: 100 }, price: 2, unit_value: 100 } + { units_variant: { unit_value: 100 }, price: 2, final_weight_volume: 100 } ] - old_value = scope.filteredLineItems[0].unit_value - new_value = scope.filteredLineItems[0].unit_value + old_value = scope.filteredLineItems[0].final_weight_volume + new_value = scope.filteredLineItems[0].final_weight_volume sp = scope.filteredLineItems[0].price expect(scope.weightAdjustedPrice(scope.filteredLineItems[0], old_value)).toEqual sp