solidus_me 2.3.1 → 3.0.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: ebf45ba51acd9453e579a60e54137080a16c9f2eae6c30a67b4d652b3567d3cd
4
- data.tar.gz: 3eb9e0aee256fb909dc26be5b95b32ad8747a43ea1f52148b84848803f76aa86
3
+ metadata.gz: 18b2087c7138f439e7653a3c01bb919fec8db078ad8581c9684f8129377c3a51
4
+ data.tar.gz: c54e202d154f70b7f3124a5d86d6ca7d1af6626979d1ac4beb9672344bb6ea99
5
5
  SHA512:
6
- metadata.gz: d42258f7940a6957d1cb589c46d4281db466e48b3039661da773595f5ee843ec3d9b4ef72d6587a4715f5d2fe981a7fe774cbd3d515ab0ebd0e8a5de9cb69652
7
- data.tar.gz: ddaf8698dc583f58c603f7dfeb142cb8b2fbdb6102ec92857c43dc494f4fc1c9572434100a3a24c88f71f1130d5a79c48087c2c956c46ac843a0b72ff95a1220
6
+ metadata.gz: 65c235b6adc9de111a611df497368e8cee192c1c730812975b0bf5c9fde645d889aa9a9979ecbffd6d18b12d0325c5b928bc55af7832a2c83e934bbf2b138cf0
7
+ data.tar.gz: f210df49830604944ce755fda0f83a664a088756c9ea89c5158650653ffb2ccfe403ec7e4bccac64b6028590751882d59827cce13087345eabe88b1a0a282a7d
@@ -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
@@ -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.0"
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.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-07-19 00:00:00.000000000 Z
11
+ date: 2024-08-09 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