forked from spree/spree_starter
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom calculator #12
Open
RubyDiver
wants to merge
9
commits into
monterail
Choose a base branch
from
custom_calculator
base: monterail
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb217d6
add calculator logic
RubyDiver e7f404f
add fixes
RubyDiver 503c371
Fix calculator
RubyDiver 55701c4
add test
RubyDiver bcfe458
Add vendor decorator
RubyDiver 6b998e7
Adding cosmetics corrections
RubyDiver ad05f84
Debbuging calculator
RubyDiver 7be500e
add vendor factory
RubyDiver 98eb96c
calculator correction
RubyDiver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,4 +48,5 @@ node_modules | |
/node_modules | ||
yarn-debug.log* | ||
.yarn-integrity | ||
/yarn-error.log | ||
/yarn-error.log | ||
config/master.key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module SpreeStarter | ||
module Spree | ||
module AddressDecorator | ||
def self.prepended(base) | ||
base.attr_accessor :postcode | ||
|
||
base.geocoded_by :coordinates | ||
base.after_validation :geocode | ||
end | ||
|
||
def coordinates | ||
[address1, address2, city, zipcode].compact.join(', ') | ||
end | ||
end | ||
end | ||
end | ||
|
||
::Spree::Address.prepend SpreeStarter::Spree::AddressDecorator if ::Spree::Address.included_modules.exclude?(SpreeStarter::Spree::AddressDecorator) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'pry' | ||
require_dependency 'spree/shipping_calculator' | ||
|
||
module Spree | ||
module Calculator::Shipping | ||
class StoreDistance < Spree::ShippingCalculator | ||
preference :base_amount_up_to_2_5_mi, :decimal, default: 0 | ||
preference :base_amount_up_to_3_5_mi, :decimal, default: 0 | ||
preference :base_amount_up_to_6_mi, :decimal, default: 0 | ||
preference :base_amount_up_to_9_mi, :decimal, default: 0 | ||
|
||
def self.description | ||
Spree.t(:shipping_store_distance) | ||
end | ||
|
||
def compute_package(package) | ||
compute_store_distance(package) | ||
end | ||
|
||
delegate :compute_store_distance, to: :distance_calculator | ||
|
||
private | ||
|
||
def distance_calculator | ||
::Spree::Calculator::StoreDistance.new( | ||
preferred_base_amount_up_to_2_5_mi: preferred_base_amount_up_to_2_5_mi, | ||
preferred_base_amount_up_to_3_5_mi: preferred_base_amount_up_to_3_5_mi, | ||
preferred_base_amount_up_to_6_mi: preferred_base_amount_up_to_6_mi, | ||
preferred_base_amount_up_to_9_mi: preferred_base_amount_up_to_9_mi | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'spree/calculator' | ||
|
||
module Spree | ||
class Calculator::StoreDistance < Calculator | ||
preference :base_amount_up_to_2_5_mi, :decimal, default: 0 | ||
preference :base_amount_up_to_3_5_mi, :decimal, default: 0 | ||
preference :base_amount_up_to_6_mi, :decimal, default: 0 | ||
preference :base_amount_up_to_9_mi, :decimal, default: 0 | ||
|
||
def self.description | ||
Spree.t(:store_distance) | ||
end | ||
|
||
def self.available?(_object) | ||
true | ||
end | ||
|
||
def compute(object) | ||
compute_store_distance(object.distance) | ||
end | ||
|
||
|
||
def compute_store_distance(distance) | ||
vendors = distance.order.variants.map do |vend| | ||
vend.vendor.to_coordinates | ||
end | ||
|
||
total = 0 | ||
|
||
start = distance.to_shipment.stock_location.to_coordinates | ||
total += Geocoder::Calculations.distance_between(start, vendors[0]) | ||
|
||
n = 1 | ||
x = vendors.count - 1 | ||
for a in n...x | ||
vendor_distance = Geocoder::Calculations.distance_between(vendors[n], vendors[n - 1]) | ||
total += vendor_distance | ||
end | ||
|
||
final_location = distance.order.ship_address.to_coordinates | ||
total += Geocoder::Calculations.distance_between(vendors.last, final_location) | ||
|
||
total += Geocoder::Calculations.distance_between(final_location, start) | ||
|
||
store_collection_charge = -0.5 | ||
vendors.map { |store_charge| store_collection_charge += 0.5 } | ||
|
||
case total | ||
when 0..2.5 | ||
total = preferred_base_amount_up_to_2_5_mi | ||
when 2.6..3.5 | ||
total = preferred_base_amount_up_to_3_5_mi | ||
when 3.6..5.9 | ||
total = preferred_base_amount_up_to_6_mi | ||
when 6.0..9 | ||
total = preferred_base_amount_up_to_9_mi | ||
end | ||
|
||
total_weight = 0 | ||
x = distance.order.variants.all.count | ||
|
||
for b in 0...x do | ||
total_weight = distance.order.variants.map do |all| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
all.weight | ||
end | ||
end | ||
total_load = total_weight.sum | ||
|
||
case total_load | ||
when 0..25 | ||
total_load = 1 | ||
when 26..36 | ||
total_load = 1.2 | ||
when 36..50 | ||
total_load = 1.4 | ||
end | ||
|
||
total = total + store_collection_charge | ||
total_price = (total * total_load) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (base_amount + store_collection_charge) * total_weight_factor |
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module SpreeStarter | ||
module Spree | ||
module StockLocationDecorator | ||
def self.prepended(base) | ||
base.attr_accessor :postcode | ||
|
||
base.geocoded_by :coordinates | ||
base.after_validation :geocode | ||
end | ||
|
||
def coordinates | ||
[address1, address2, zipcode ].compact.join(', ') | ||
end | ||
end | ||
end | ||
end | ||
|
||
::Spree::StockLocation.prepend SpreeStarter::Spree::StockLocationDecorator if ::Spree::StockLocation.included_modules.exclude?(SpreeStarter::Spree::StockLocationDecorator) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module SpreeStarter | ||
module Spree | ||
module ZoneDecorator | ||
def self.prepended(base) | ||
base.attr_accessor :postcode | ||
|
||
base.geocoded_by :coordinates | ||
base.after_validation :geocode | ||
end | ||
|
||
def coordinates | ||
[starting_point].compact.join(', ') | ||
end | ||
end | ||
end | ||
end | ||
|
||
::Spree::Zone.prepend SpreeStarter::Spree::ZoneDecorator if ::Spree::Zone.included_modules.exclude?(SpreeStarter::Spree::ZoneDecorator) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20210921131436_add_starting_point_to_spree_zones.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddStartingPointToSpreeZones < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :spree_zones, :starting_point, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddCoordinatesToSpreeZone < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :spree_zones, :latitude, :float | ||
add_column :spree_zones, :longitude, :float | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20210924072245_add_coordinates_to_spree_stock_location.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddCoordinatesToSpreeStockLocation < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :spree_stock_locations, :latitude, :float | ||
add_column :spree_stock_locations, :longitude, :float | ||
end | ||
end |
6 changes: 6 additions & 0 deletions
6
db/migrate/20210924073830_add_coordinates_to_spree_addresses.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddCoordinatesToSpreeAddresses < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :spree_addresses, :latitude, :float | ||
add_column :spree_addresses, :longitude, :float | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
spec/models/spec/calculator/shipping/store_distance_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'spec_helper' | ||
|
||
module Spree | ||
module Calculator::Shipping | ||
describe StoreDistance, type: :model do | ||
let(:subject) { StoreDistance.new } | ||
|
||
let(:variant1) { build(:variant, price: 19) } | ||
let(:variant2) { build(:variant, price: 25) } | ||
|
||
let(:package) do | ||
build(:stock_package, variants_contents: {variant1 => 5}) | ||
end | ||
let!(:vendor) { Spree::Vendor.create!(name: "vendor1", about_us: "50 Newton St, Birmingham B4 6NE") } | ||
let!(:vendor2) { Spree::Vendor.create!(name: "vendor2", about_us: "Ryder St, Birmingham B4 7NE") } | ||
|
||
let(:stock_location1) { build(:stock_location) } | ||
|
||
let(:line_item1) { build(:line_item, variant1: variant1) } | ||
let(:line_item2) { build(:line_item, variant2: variant2) } | ||
|
||
it 'returns address' do | ||
expect(subject.compute(package)).to eq(0) | ||
end | ||
|
||
it 'return vendor' do | ||
expect(vendor.to_coordinates).to eq(Spree::Vendor.first.to_coordinates) | ||
end | ||
|
||
it 'allows creation of new object with all the attributes' do | ||
StoreDistance.new( | ||
preferred_base_amount_up_to_2_5_mi: 1, | ||
preferred_base_amount_up_to_3_5_mi: 2, | ||
preferred_base_amount_up_to_6_mi: 3, | ||
preferred_base_amount_up_to_9_mi: 4 | ||
) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://apidock.com/ruby/Enumerable/inject