solidus_me 2.3.0 → 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: 218050180c9a5f7fae3eddc4c5abebe30e461e3b3cbee550ba8f7c027e6ddfdb
4
- data.tar.gz: a0ba7812be1237002a42eb5d34cf3f54bc02d393d30737d31e69d15d625e49cc
3
+ metadata.gz: 18b2087c7138f439e7653a3c01bb919fec8db078ad8581c9684f8129377c3a51
4
+ data.tar.gz: c54e202d154f70b7f3124a5d86d6ca7d1af6626979d1ac4beb9672344bb6ea99
5
5
  SHA512:
6
- metadata.gz: df0787404684271e42545f346349b9ed2ed3d0660c881700b9c2ea91791f1a7c220969b77bf5f24f6c2949f5d0933be97feb6087bf24cedc0d8b99bbfd96e700
7
- data.tar.gz: 63c0b143c51e8f97faf8c6c41b28699a6da38ecfb1e3692f6ece06c33d1aa588988274804f0639356e9c352a09d071fc1e82c353a1b8ad253c2f57a8768d9f7f
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
@@ -38,9 +38,6 @@ module SolidusMe
38
38
  from: @me_account.postal_code_from,
39
39
  to: zipcode,
40
40
  weight_kg: (weight / 1000.00),
41
- height_cm: height_cm,
42
- width_cm: width_cm,
43
- length_cm: length_cm,
44
41
  contents_value_brl: price
45
42
  )
46
43
  rates.select { |rate| rate.price > 0 && services.include?(rate.service_name) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusMe
4
- VERSION = "2.3.0"
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.0
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-06-11 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
@@ -157,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
157
158
  - !ruby/object:Gem::Version
158
159
  version: '0'
159
160
  requirements: []
160
- rubygems_version: 3.5.10
161
+ rubygems_version: 3.5.11
161
162
  signing_key:
162
163
  specification_version: 4
163
164
  summary: Gem para integração com a API do Melhor Envio