-
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
base: monterail
Are you sure you want to change the base?
Changes from all commits
fb217d6
e7f404f
503c371
55701c4
bcfe458
6b998e7
ad05f84
7be500e
98eb96c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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) | ||
|
||
|
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| | ||
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| | ||
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. 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 | ||
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. 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 |
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) |
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) | ||
|
||
|
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) |
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) | ||
|
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> |
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 |
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 |
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.
use inject/reduce here