solidus_me 3.0.1 → 3.0.2

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: 9ee90fa75f6efce0402e68f7992665696df6f0fc0fea34c1098489216bfa462d
4
+ data.tar.gz: 130f9dec9c6ca3730f25a07e062d22172368967cf752ebce0c131cfbf418c56c
5
5
  SHA512:
6
- metadata.gz: 317e8749cf16503be49ede490d313e7db31decf89a182ee980a83f18afe3bc257aff67216956a3a32f4ea83e1e31050124eb8011a5ca547d4a8cc4781a23f20b
7
- data.tar.gz: 63e591008b243e92dfa89cedb389a608524840540bd2945e58b4c250ec23ab67b382aafbdcd89dbcdf48459f461c67040a3df60825e079d173a0557b608c894a
6
+ metadata.gz: '08327213cb9db79a153e56150d7d70ad364148e28cfa4d92dc2c14e80199b27f1da0dc5e5dda75a78cbda68c49b1ccc70fb364359080130a51d8be0bbd27babd'
7
+ data.tar.gz: c34ecfaa9435d0603d86e156a1d950463e7e187f59229b69bc1b74f4f09f114e882a9c86222b49eb5d37996f3b5cb286474d1e3b0bd864024a4b9dadf2659700
@@ -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?
@@ -19,7 +45,6 @@ module SolidusMe
19
45
  shipping_rates = melhor_envio_rates.map do |melhor_envio_rate|
20
46
  build_shipping_rate(melhor_envio_rate, package)
21
47
  end
22
- shipping_rates = shipping_rates.select { |rate| shipping_methods.include?(rate.shipping_method) }
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,22 +64,13 @@ 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
+ shipping_method = Spree::ShippingMethod.find_by(code: self.class.generate_code(melhor_envio_rate.id))
63
74
 
64
75
  Spree::ShippingRate.new(
65
76
  shipment: package.shipment,
@@ -70,18 +81,6 @@ module SolidusMe
70
81
  )
71
82
  end
72
83
 
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
84
  end
86
85
 
87
86
  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.0.2"
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: 3.0.1
4
+ version: 3.0.2
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-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core