solidus_me 2.3.1 → 3.0.1

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: ebf45ba51acd9453e579a60e54137080a16c9f2eae6c30a67b4d652b3567d3cd
4
- data.tar.gz: 3eb9e0aee256fb909dc26be5b95b32ad8747a43ea1f52148b84848803f76aa86
3
+ metadata.gz: 6c4a738fff9ed84b19c321528752084dc9babb7a0b4d0244c701ef8f4ff70ce4
4
+ data.tar.gz: b0a0bb227356491430735369efa29905c1ed1ba69ef291a0a0f2e8942aeb7705
5
5
  SHA512:
6
- metadata.gz: d42258f7940a6957d1cb589c46d4281db466e48b3039661da773595f5ee843ec3d9b4ef72d6587a4715f5d2fe981a7fe774cbd3d515ab0ebd0e8a5de9cb69652
7
- data.tar.gz: ddaf8698dc583f58c603f7dfeb142cb8b2fbdb6102ec92857c43dc494f4fc1c9572434100a3a24c88f71f1130d5a79c48087c2c956c46ac843a0b72ff95a1220
6
+ metadata.gz: 317e8749cf16503be49ede490d313e7db31decf89a182ee980a83f18afe3bc257aff67216956a3a32f4ea83e1e31050124eb8011a5ca547d4a8cc4781a23f20b
7
+ data.tar.gz: 63e591008b243e92dfa89cedb389a608524840540bd2945e58b4c250ec23ab67b382aafbdcd89dbcdf48459f461c67040a3df60825e079d173a0557b608c894a
@@ -0,0 +1,87 @@
1
+ module SolidusMe
2
+
3
+ class MelhorEnvio
4
+
5
+ attr_accessor :package, :shipping_methods
6
+ def initialize(package, shipping_methods)
7
+ @package = package
8
+ @shipping_methods = shipping_methods
9
+ end
10
+
11
+ def shipping_rates
12
+ @me_account = Account.first
13
+ return [] if @me_account.blank?
14
+
15
+ @me_account.check_token
16
+ @me_client = MeApi::Client.new(@me_account.access_token)
17
+
18
+ melhor_envio_rates = get_rates_from_melhor_envio(package)
19
+ shipping_rates = melhor_envio_rates.map do |melhor_envio_rate|
20
+ build_shipping_rate(melhor_envio_rate, package)
21
+ end
22
+ shipping_rates = shipping_rates.select { |rate| shipping_methods.include?(rate.shipping_method) }
23
+
24
+ shipping_rates
25
+ end
26
+
27
+ private
28
+
29
+ def get_rates_from_melhor_envio(package)
30
+ price = package.contents.map { |content| content.price }.sum
31
+ weight = package.weight
32
+
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
+ zipcode = package.order.ship_address.zipcode
39
+ services = @me_account.services.blank? ? ["SEDEX", "PAC", "Mini Envios", ".Package", ".Com"] : @me_account.services
40
+
41
+ rates = @me_client.rates(
42
+ from: @me_account.postal_code_from,
43
+ to: zipcode,
44
+ weight_kg: (weight / 1000.00),
45
+ contents_value_brl: price
46
+ )
47
+ rates.select { |rate| rate.price > 0 && services.include?(rate.service_name) }
48
+ rescue
49
+ []
50
+ end
51
+
52
+ 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
63
+
64
+ Spree::ShippingRate.new(
65
+ shipment: package.shipment,
66
+ shipping_method: shipping_method,
67
+ 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)
70
+ )
71
+ end
72
+
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
+ end
86
+
87
+ end
@@ -60,8 +60,8 @@ module SolidusMe
60
60
  shipment: package.shipment,
61
61
  shipping_method: shipping_method,
62
62
  cost: melhor_envio_rate.price,
63
- min_delivery_time: melhor_envio_rate.delivery_range["min"],
64
- max_delivery_time: melhor_envio_rate.delivery_range["max"]
63
+ min_delivery_time: (Time.now + melhor_envio_rate.delivery_range["min"].days),
64
+ max_delivery_time: (Time.now + melhor_envio_rate.delivery_range["max"].days)
65
65
  )
66
66
  end
67
67
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusMe
4
- VERSION = "2.3.1"
4
+ VERSION = "3.0.1"
5
5
  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: 2.3.1
4
+ version: 3.0.1
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-07-19 00:00:00.000000000 Z
11
+ date: 2024-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core
@@ -106,6 +106,7 @@ files:
106
106
  - Rakefile
107
107
  - app/controller/spree/admin/solidus_me/accounts_controller.rb
108
108
  - app/models/solidus_me/account.rb
109
+ - app/models/solidus_me/melhor_envio.rb
109
110
  - app/models/solidus_me/shipping_estimator.rb
110
111
  - app/views/spree/admin/solidus_me/accounts/_form.html.erb
111
112
  - app/views/spree/admin/solidus_me/accounts/_link_auth.html.erb