solidus_me 3.0.1 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c4a738fff9ed84b19c321528752084dc9babb7a0b4d0244c701ef8f4ff70ce4
4
- data.tar.gz: b0a0bb227356491430735369efa29905c1ed1ba69ef291a0a0f2e8942aeb7705
3
+ metadata.gz: 9db290c638a2d01484e2fa15aab8c66e9aa5aa9d655360b2aa59a290654c9131
4
+ data.tar.gz: c6f27dc5ad820d3b778805e138b566de84733caa75a1ec0f49d0bbcb21580ecd
5
5
  SHA512:
6
- metadata.gz: 317e8749cf16503be49ede490d313e7db31decf89a182ee980a83f18afe3bc257aff67216956a3a32f4ea83e1e31050124eb8011a5ca547d4a8cc4781a23f20b
7
- data.tar.gz: 63e591008b243e92dfa89cedb389a608524840540bd2945e58b4c250ec23ab67b382aafbdcd89dbcdf48459f461c67040a3df60825e079d173a0557b608c894a
6
+ metadata.gz: 69fbcd15348fe78ff501e5245558da1be4156a298889a4afb347f70350d99cd4d857f5ae3445de1d5d02e4e4bbfd3f945b56cd62bb328776fd344cd98d3f1569
7
+ data.tar.gz: 229e6ce800fb87c1330033303358ac7ae6f5ca41eb934e5da7dbdeb60c6a913d968dcc2047696da70a6b094f47c9870c0b69f6d9f936ac6dd299b4bc0d0d267e
@@ -1,13 +1,39 @@
1
1
  module SolidusMe
2
2
 
3
3
  class MelhorEnvio
4
-
4
+ SHIPPING_METHODS = [
5
+ { name: "Correios PAC", carrier: "Correios", service_level: "PAC", external_id: 1 },
6
+ { name: "Correios SEDEX", carrier: "Correios", service_level: "SEDEX", external_id: 2 },
7
+ { name: "Correios Mini Envios", carrier: "Correios", service_level: "Mini Envios", external_id: 17 },
8
+ { name: "Jadlog .Package", carrier: "Jadlog", service_level: ".Package", external_id: 3 },
9
+ { name: "Jadlog .Com", carrier: "Jadlog", service_level: ".Com", external_id: 4 }
10
+ ]
11
+
5
12
  attr_accessor :package, :shipping_methods
6
13
  def initialize(package, shipping_methods)
7
14
  @package = package
8
15
  @shipping_methods = shipping_methods
9
16
  end
10
17
 
18
+ def self.generate_code external_id
19
+ (self.name.underscore + "_#{external_id}").downcase
20
+ end
21
+
22
+ def self.install
23
+ SHIPPING_METHODS.each do |provider_method|
24
+ Spree::ShippingMethod.find_or_create_by(code: generate_code(provider_method[:external_id])) do |shipping_method|
25
+ shipping_method.name = provider_method[:name]
26
+ shipping_method.carrier = provider_method[:carrier]
27
+ shipping_method.service_level = provider_method[:service_level]
28
+ shipping_method.code = generate_code(provider_method[:external_id])
29
+ shipping_method.calculator = Spree::Calculator::Shipping::FlatRate.create
30
+ shipping_method.shipping_categories = [Spree::ShippingCategory.first]
31
+ shipping_method.available_to_users = false
32
+ shipping_method.provider_id = SolidusTecEstimator::Provider.find_by(class_name: self.to_s).id
33
+ end
34
+ end
35
+ end
36
+
11
37
  def shipping_rates
12
38
  @me_account = Account.first
13
39
  return [] if @me_account.blank?
@@ -18,8 +44,7 @@ module SolidusMe
18
44
  melhor_envio_rates = get_rates_from_melhor_envio(package)
19
45
  shipping_rates = melhor_envio_rates.map do |melhor_envio_rate|
20
46
  build_shipping_rate(melhor_envio_rate, package)
21
- end
22
- shipping_rates = shipping_rates.select { |rate| shipping_methods.include?(rate.shipping_method) }
47
+ end.compact
23
48
 
24
49
  shipping_rates
25
50
  end
@@ -30,13 +55,8 @@ module SolidusMe
30
55
  price = package.contents.map { |content| content.price }.sum
31
56
  weight = package.weight
32
57
 
33
- variants = package.contents.map { |content| content.variant }
34
- height_cm = height(variants)
35
- width_cm = width(variants)
36
- length_cm = length(variants)
37
-
38
58
  zipcode = package.order.ship_address.zipcode
39
- services = @me_account.services.blank? ? ["SEDEX", "PAC", "Mini Envios", ".Package", ".Com"] : @me_account.services
59
+ available_shipping_method_codes = @shipping_methods.pluck(:code)
40
60
 
41
61
  rates = @me_client.rates(
42
62
  from: @me_account.postal_code_from,
@@ -44,44 +64,27 @@ module SolidusMe
44
64
  weight_kg: (weight / 1000.00),
45
65
  contents_value_brl: price
46
66
  )
47
- rates.select { |rate| rate.price > 0 && services.include?(rate.service_name) }
67
+ rates.select { |rate| rate.price > 0 && available_shipping_method_codes.include?(self.class.generate_code(rate.id)) }
48
68
  rescue
49
69
  []
50
70
  end
51
71
 
52
72
  def build_shipping_rate(melhor_envio_rate, package)
53
- shipping_method = Spree::ShippingMethod.find_or_create_by(
54
- carrier: melhor_envio_rate.carrier_name,
55
- service_level: melhor_envio_rate.service_name
56
- ) do |shipping_method|
57
- shipping_method.name = "#{melhor_envio_rate.carrier_name} #{melhor_envio_rate.service_name}"
58
- shipping_method.calculator = Spree::Calculator::Shipping::FlatRate.create
59
- shipping_method.shipping_categories = [Spree::ShippingCategory.first]
60
- shipping_method.available_to_users = true
61
- shipping_method.provider_id = SolidusTecEstimator::Provider.find_by(class_name: self.class.to_s).id
62
- end
73
+ return unless melhor_envio_rate.max_delivery_time
74
+ shipping_method = Spree::ShippingMethod.find_by(code: self.class.generate_code(melhor_envio_rate.id))
75
+ estimate_prose = "Chegará em até #{ActionController::Base.helpers.pluralize(melhor_envio_rate.max_delivery_time, "dia", plural: "dias")} úteis"
63
76
 
64
77
  Spree::ShippingRate.new(
65
78
  shipment: package.shipment,
66
79
  shipping_method: shipping_method,
67
80
  cost: melhor_envio_rate.price,
68
- min_delivery_time: (Time.now + melhor_envio_rate.delivery_range["min"].days),
69
- max_delivery_time: (Time.now + melhor_envio_rate.delivery_range["max"].days)
81
+ min_delivery_time: melhor_envio_rate.min_delivery_time.business_days.from_now,
82
+ max_delivery_time: melhor_envio_rate.max_delivery_time.business_days.from_now,
83
+ estimate_prose: estimate_prose,
84
+ expires_in: Time.now + 2.hours
70
85
  )
71
86
  end
72
87
 
73
- def height variants
74
- variants.pluck(:height, :width, :depth).map(&:max).compact.max
75
- end
76
-
77
- def width variants
78
- variants.pluck(:height, :width, :depth).map { |dimensions| dimensions.sort[1] }.compact.max
79
- end
80
-
81
- def length variants
82
- variants.pluck(:height, :width, :depth).map(&:min).compact.sum
83
- end
84
-
85
88
  end
86
89
 
87
90
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusMe
4
- VERSION = "3.0.1"
4
+ VERSION = "3.1.0"
5
5
  end
data/lib/solidus_me.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "me_api"
4
4
  require "solidus_brazilian_adaptations"
5
+ require "business_time"
5
6
  require "solidus_me/configuration"
6
7
  require "solidus_me/version"
7
8
  require "solidus_me/engine"
data/solidus_me.gemspec CHANGED
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.add_dependency "solidus_support", "~> 0.5"
33
33
  spec.add_dependency "me_api"
34
34
  spec.add_dependency "solidus_brazilian_adaptations"
35
+ spec.add_dependency "business_time"
35
36
 
36
37
  spec.add_development_dependency "solidus_dev_support", "~> 2.7"
37
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_me
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hamilton Tumenas Borges
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-12 00:00:00.000000000 Z
11
+ date: 2024-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core
@@ -72,6 +72,20 @@ dependencies:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: business_time
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
75
89
  - !ruby/object:Gem::Dependency
76
90
  name: solidus_dev_support
77
91
  requirement: !ruby/object:Gem::Requirement