Skip to content
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
wants to merge 9 commits into
base: monterail
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ node_modules
/node_modules
yarn-debug.log*
.yarn-integrity
/yarn-error.log
/yarn-error.log
config/master.key
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ gem 'bootsnap', require: false
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Geocoder
gem 'geocoder', '~> 1.6', '>= 1.6.7'

# Use Puma as the app server
gem 'puma'

Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ GEM
friendly_id (5.4.2)
activerecord (>= 4.0.0)
gem-release (2.2.1)
geocoder (1.6.7)
github_changelog_generator (1.16.2)
activesupport
async (>= 1.25.0)
Expand Down Expand Up @@ -704,6 +705,7 @@ DEPENDENCIES
flipper-redis
flipper-ui
font_assets
geocoder (~> 1.6, >= 1.6.7)
letter_opener
listen
memory_profiler
Expand Down Expand Up @@ -744,4 +746,4 @@ RUBY VERSION
ruby 2.7.3p183

BUNDLED WITH
2.1.4
2.2.25
20 changes: 20 additions & 0 deletions app/models/spree/address_decorator.rb
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)


77 changes: 77 additions & 0 deletions app/models/spree/calculator/shipping/store_distance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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: 2.5
preference :base_amount_up_to_3_5_mi, :decimal, default: 3.5
preference :base_amount_up_to_6_mi, :decimal, default: 4.5
preference :base_amount_up_to_9_mi, :decimal, default: 5.5

def self.description
Spree.t(:shipping_store_distance)
end

def compute_package(package)
compute_store_distance(package)
end

def vendors_coordinates(package)
package.order.variants.map do |variant|
variant.vendor.to_coordinates
end
end

def total_product_weight(package)
total_weight = package.order.variants.map do |variant|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use inject/reduce here

variant.weight
end
total_load = total_weight.inject(0) { |sum, number| sum + number }

case total_load
when 0..25 then total_load = 1
when 26..36 then total_load = 1.2
when 36..50 then total_load = 1.4
end
end

def total_distance(package)
vendor_co = vendors_coordinates(package)

milage_price_band = 0

vendor_co.unshift(package.stock_location.to_coordinates)
vendor_co << package.order.ship_address.to_coordinates
vendor_co << package.stock_location.to_coordinates

vendor_co[0...-1].each_with_index do |val, index|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://til.hashrocket.com/posts/52tcyljcgl-reducing-with-an-index-in-ruby

I think you can extend vendor_coordinates array in a way you won't have to calculate, total_distance, outside this loop.

milage_price_band += Geocoder::Calculations.distance_between(vendor_co[index], vendor_co[index + 1])
end

case milage_price_band
when 0..2.5 then milage_price_band = 2.5
when 2.6..3.5 then milage_price_band = 3.5
when 3.6..5.9 then milage_price_band = 4.5
when 6.0..9 then milage_price_band = 5.5
else raise "We do not deliver further than 9 miles"
end
end

def store_collection_charge(package)
vendors_coordinates(package).inject(-0.5) { |sum, number| sum + 0.5 }
end

def compute_store_distance(package)
binding.pry
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls remove it

milage_price_band = total_distance(package)

store_collection_charge(package)

total_weight = total_product_weight(package)

(milage_price_band * total_weight) + store_collection_charge(package)
end
end
end
end
17 changes: 17 additions & 0 deletions app/models/spree/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Spree
module ProductDecorator
def self.prepended(base)
def base.ascend_by_postcode(postcode)
vend_array = Spree::Vendor.near(postcode)

tabl = vend_array.map.with_index { |vendor, index| "WHEN spree_products.vendor_id = '#{vendor.id}' THEN #{index}" }
order_sql = "CASE #{tabl.join(' ')} ELSE #{tabl.size} END ASC"

joins(master: :default_price).order(Arel.sql(order_sql))
end

base.search_scopes << :ascend_by_postcode
end
end
end
::Spree::Product.prepend Spree::ProductDecorator if ::Spree::Product.included_modules.exclude?(Spree::ProductDecorator)
20 changes: 20 additions & 0 deletions app/models/spree/stock_location_decorator.rb
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)


18 changes: 18 additions & 0 deletions app/models/spree/vendor_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module SpreeStarter
module Spree
module VendorDecorator
def self.prepended(base)
base.attr_accessor :postcode

base.geocoded_by :coordinates
base.after_validation :geocode
end

def coordinates
[about_us].compact.join(', ')
end
end
end
end

::Spree::Vendor.prepend SpreeStarter::Spree::VendorDecorator if ::Spree::Vendor.included_modules.exclude?(SpreeStarter::Spree::VendorDecorator)
19 changes: 19 additions & 0 deletions app/models/spree/zone_decorator.rb
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)

36 changes: 36 additions & 0 deletions app/views/spree/admin/vendors/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @vendor } %>
<div class="form-group">
<%= f.field_container :name do %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<% end %>
<%= f.field_container :about_us do %>
<%= f.label :about_us %>
<%= f.text_area :about_us, class: 'form-control' %>
<% end %>
<%= f.field_container :contact_us do %>
<%= f.label :contact_us %>
<%= f.text_area :contact_us, class: 'form-control' %>
<% end %>
<%= f.field_container :state do %>
<%= f.label :state %>
<%= f.select :state,
options_for_select(vendor_state_options, selected: @vendor.state),
{}, class: 'select2', required: :required %>
<% end %>
<% if Spree.version.to_f >= 3.6 %>
<%= f.field_container :image do %>
<%= f.label :image %>
<%= f.file_field :image %>
<%= image_tag main_app.url_for(@vendor.image.url(:small)) if @vendor.image %>
<% end %>
<% end %>
<%= f.field_container :commission_rate do %>
<%= f.label :commission_rate %>
<%= f.number_field :commission_rate, step: '0.01', class: 'form-control' %>
<% end %>
<%= f.field_container :notification_email do%>
<%= f.label :notification_email %>
<%= f.email_field :notification_email, class: 'form-control' %>
<% end %>
</div>
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ class Application < Rails::Application

# flipper memoizing
config.middleware.use Flipper::Middleware::Memoizer

config.autoloader = :classic
end
end
4 changes: 4 additions & 0 deletions config/initializers/spree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
# Example:
# Uncomment to stop tracking inventory levels in the application
# config.track_inventory_levels = false

end

Spree.user_class = 'Spree::User'

config = Rails.application.config
config.spree.calculators.shipping_methods << Spree::Calculator::Shipping::StoreDistance
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
6 changes: 6 additions & 0 deletions db/migrate/20210922141756_add_coordinates_to_spree_zone.rb
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
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
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
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_08_10_140732) do
ActiveRecord::Schema.define(version: 2021_09_24_073830) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -110,6 +110,8 @@
t.integer "user_id"
t.datetime "deleted_at"
t.string "label"
t.float "latitude"
t.float "longitude"
t.index ["country_id"], name: "index_spree_addresses_on_country_id"
t.index ["deleted_at"], name: "index_spree_addresses_on_deleted_at"
t.index ["firstname"], name: "index_addresses_on_firstname"
Expand Down Expand Up @@ -969,6 +971,8 @@
t.boolean "propagate_all_variants", default: true
t.string "admin_name"
t.integer "vendor_id"
t.float "latitude"
t.float "longitude"
t.index ["active"], name: "index_spree_stock_locations_on_active"
t.index ["backorderable_default"], name: "index_spree_stock_locations_on_backorderable_default"
t.index ["country_id"], name: "index_spree_stock_locations_on_country_id"
Expand Down Expand Up @@ -1291,6 +1295,9 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "kind", default: "state"
t.string "starting_point"
t.float "latitude"
t.float "longitude"
t.index ["default_tax"], name: "index_spree_zones_on_default_tax"
t.index ["kind"], name: "index_spree_zones_on_kind"
end
Expand Down
Loading