spree_zaez_correios 3.0.4 → 3.0.5

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
  SHA1:
3
- metadata.gz: 8e91f8fcf0ed0b9cc3a0887d65fbe8f0127553ac
4
- data.tar.gz: 3416cc8d1fcf7a14c64cae51dce7355707fb6952
3
+ metadata.gz: b30e23fbc5293123daf61b2e4b9038b2c459407d
4
+ data.tar.gz: 4b7ecb4c78ad7c1acd6a98db07d71b90e5a4671d
5
5
  SHA512:
6
- metadata.gz: 9bb71a124c0ce79ebf6a2c35bf01a8264fda66c6dffaa2f23f408e257ff65738e8988a2ef9e6d7c0e88c4bd03d3606a498d6b3f6b097a61d80b6d60504a223a2
7
- data.tar.gz: 78a0167704e884c350d7e0dd0d62cdaf3c92365477dfde6a79efeb632a65a8d51eafe49769c03988f4bc4715e5f9b508fa6da54d2b5b78d1969235909cf813be
6
+ metadata.gz: 3d59e8761e019426b7b948d2afca952c017426a72ce8e86fc45ca29d424d38075c7558415719678da193a4ac207d4c70292a3aa6cf36d84596196e91c678607b
7
+ data.tar.gz: f20d8b11ad51361889d46c59afc537f22d0f14ca917e21f1c46fc0fdda7115ca1d69016d505f6a9846c9782eeb1e3c1412d9fd93849e1d57a670334dc7965794
@@ -7,51 +7,186 @@ module Spree
7
7
  preference :declared_value, :boolean, default: false
8
8
  preference :receipt_notification, :boolean, default: false
9
9
  preference :receive_in_hands, :boolean, default: false
10
-
10
+
11
+ # Tamanhos maximos que um pacote pode ter para ser enviado pelos correios
12
+ # Soma maxima das dimensoes (compr. + alt. + larg.)
13
+ #
14
+ # documentacao em: http://www.correios.com.br/para-voce/precisa-de-ajuda/limites-de-dimensoes-e-de-peso
15
+ #
16
+ MAX_WIDTH = 105
17
+ MAX_DEPTH = 105
18
+ MAX_HEIGHT = 105
19
+ MAX_DIMENSIONS = 200
20
+
11
21
  attr_reader :delivery_time
12
-
13
- def compute_package(object)
14
- return if object.nil?
15
- order = if object.is_a?(Spree::Order) then object else object.order end
16
22
 
17
- stock_location = object.stock_location
23
+ # Calculates the shipping cost
24
+ #
25
+ # Algorithm inspired in Opencart extension created by Thalles Cardoso <thallescard@gmail.com>
26
+ #
27
+ # @param package [Spree::Package]
28
+ #
29
+ # @return [Hash]
30
+ #
31
+ def compute_package package
32
+ return if package.nil?
33
+ @order = package.order
34
+
35
+ @stock_location = package.stock_location
36
+
37
+ pkg = Correios::Frete::Pacote.new
38
+ pkg_dimensions = {width: 0, height: 0, depth: 0, weight: 0}
39
+ packages = [pkg]
40
+
41
+ package.contents.each do |item|
42
+ return {} unless valid_dimensions?(item.variant)
43
+
44
+ new_width = item.variant.width + pkg_dimensions[:width]
45
+ new_height = item.variant.height + pkg_dimensions[:height]
46
+ new_depth = item.variant.depth + pkg_dimensions[:depth]
47
+ new_weight = item.variant.weight + pkg_dimensions[:weight]
48
+
49
+ if (new_width <= MAX_WIDTH and verify_max_dimensions(pkg_dimensions, item.variant, :width)) and (new_weight <= max_weight)
50
+ pkg_dimensions[:weight] += item.variant.weight
51
+
52
+ pkg_dimensions[:width] += item.variant.width
53
+ pkg_dimensions[:height] = pkg_dimensions[:height] >= item.variant.height ? pkg_dimensions[:height] : item.variant.height
54
+ pkg_dimensions[:depth] = pkg_dimensions[:depth] >= item.variant.depth ? pkg_dimensions[:depth] : item.variant.depth
55
+ elsif (new_height <= MAX_HEIGHT and verify_max_dimensions(pkg_dimensions, item.variant, :height)) and (new_weight <= max_weight)
56
+ pkg_dimensions[:weight] += item.variant.weight
57
+
58
+ pkg_dimensions[:height] += item.variant.height
59
+ pkg_dimensions[:width] = pkg_dimensions[:width] >= item.variant.width ? pkg_dimensions[:width] : item.variant.width
60
+ pkg_dimensions[:depth] = pkg_dimensions[:depth] >= item.variant.depth ? pkg_dimensions[:depth] : item.variant.depth
61
+ elsif (new_depth <= MAX_DEPTH and verify_max_dimensions(pkg_dimensions, item.variant, :depth)) and (new_weight <= max_weight)
62
+ pkg_dimensions[:weight] += item.variant.weight
18
63
 
19
- require 'correios-frete'
64
+ pkg_dimensions[:depth] += item.variant.depth
65
+ pkg_dimensions[:height] = pkg_dimensions[:height] >= item.variant.height ? pkg_dimensions[:height] : item.variant.height
66
+ pkg_dimensions[:width] = pkg_dimensions[:width] >= item.variant.width ? pkg_dimensions[:width] : item.variant.width
67
+ else
68
+ pkg = Correios::Frete::Pacote.new
69
+ pkg_dimensions = {width: item.variant.width,
70
+ height: item.variant.height,
71
+ depth: item.variant.depth,
72
+ weight: item.variant.weight}
20
73
 
21
- package = Correios::Frete::Pacote.new
74
+ packages << pkg
75
+ end
76
+ package_item = Correios::Frete::PacoteItem.new(peso: item.variant.weight.to_f,
77
+ comprimento: item.variant.depth.to_f,
78
+ largura: item.variant.width.to_f,
79
+ altura: item.variant.height.to_f)
80
+ pkg.add_item(package_item)
81
+ end
82
+
83
+ value = 0
84
+ delivery_time = 0
85
+
86
+ packages.each do |pkg|
87
+ response = calculate_shipping pkg
22
88
 
23
- object.contents.each do |item|
24
- weight = item.variant.weight.to_f
25
- depth = item.variant.depth.to_f
26
- width = item.variant.width.to_f
27
- height = item.variant.height.to_f
28
- package_item = Correios::Frete::PacoteItem.new(peso: weight, comprimento: depth, largura: width, altura: height)
29
- package.add_item(package_item)
89
+ if response.is_a? Hash
90
+ value += response[:value]
91
+ delivery_time = response[:delivery_time] if response[:delivery_time] > delivery_time
92
+ else
93
+ return {}
94
+ end
30
95
  end
31
96
 
97
+ @delivery_time = delivery_time + preferred_additional_days
98
+ cost = value + preferred_additional_value
99
+ {cost: cost, delivery_time: @delivery_time}
100
+ rescue
101
+ {}
102
+ end
103
+
104
+ # Verify if the store has contract to Correios
105
+ #
106
+ # @author Isabella Santos
107
+ #
108
+ # @return [Boolean]
109
+ #
110
+ def has_contract?
111
+ preferred_token.present? && preferred_password.present?
112
+ end
113
+
114
+ protected
115
+
116
+ # Verify if the produtct has valid dimensions for Correios
117
+ #
118
+ # @author Isabella Santos
119
+ #
120
+ # @param variant [Spree::Variant]
121
+ #
122
+ # @return [Boolean]
123
+ #
124
+ def valid_dimensions? variant
125
+ dimensions = variant.width + variant.depth + variant.height
126
+ if variant.width > MAX_WIDTH or variant.depth > MAX_DEPTH or
127
+ variant.height > MAX_HEIGHT or dimensions > MAX_DIMENSIONS or
128
+ variant.weight > max_weight
129
+ return false
130
+ end
131
+ true
132
+ end
133
+
134
+ # Verify the size of the product and the available space inside the box
135
+ #
136
+ # @author Isabella Santos
137
+ #
138
+ # @param dimensions [Hash]
139
+ # used dimensions of the box
140
+ # @param variant [Spree::Variant]
141
+ # variant of the package
142
+ # @param position [Symbol]
143
+ # position of the box
144
+ # The options are: :width, :height or :depth
145
+ #
146
+ # @return [Boolean]
147
+ #
148
+ def verify_max_dimensions(dimensions, variant, position)
149
+ case position
150
+ when :width
151
+ width = dimensions[:width] + variant.width
152
+ height = dimensions[:height] >= variant.height ? dimensions[:height] : variant.height
153
+ depth = dimensions[:depth] >= variant.depth ? dimensions[:depth] : variant.depth
154
+ when :height
155
+ height = dimensions[:height] + variant.height
156
+ depth = dimensions[:depth] >= variant.depth ? dimensions[:depth] : variant.depth
157
+ width = dimensions[:width] >= variant.width ? dimensions[:width] : variant.width
158
+ when :depth
159
+ depth = dimensions[:depth] + variant.depth
160
+ height = dimensions[:height] >= variant.height ? dimensions[:height] : variant.height
161
+ width = dimensions[:width] >= variant.width ? dimensions[:width] : variant.width
162
+ end
163
+
164
+ (width + height + depth) <= MAX_DIMENSIONS
165
+ end
166
+
167
+ # Request to Correios webservice
168
+ # the cost and delivery time
169
+ #
170
+ # @param package [Correios::Frete::Pacote]
171
+ #
172
+ # @return [Hash]
173
+ #
174
+ def calculate_shipping(package)
32
175
  calculator = Correios::Frete::Calculador.new do |c|
33
- c.cep_origem = stock_location.zipcode
34
- c.cep_destino = order.ship_address.zipcode
176
+ c.cep_origem = @stock_location.zipcode
177
+ c.cep_destino = @order.ship_address.zipcode
35
178
  c.encomenda = package
36
179
  c.mao_propria = preferred_receive_in_hands
37
180
  c.aviso_recebimento = preferred_receipt_notification
38
- c.valor_declarado = order.amount.to_f if preferred_declared_value
39
- c.codigo_empresa = preferred_token if preferred_token.present?
40
- c.senha = preferred_password if preferred_password.present?
181
+ c.valor_declarado = @order.amount.to_f if preferred_declared_value
182
+ c.codigo_empresa = preferred_token if preferred_token.present?
183
+ c.senha = preferred_password if preferred_password.present?
41
184
  end
42
185
 
43
186
  webservice = calculator.calculate(shipping_method)
44
187
  return false if webservice.erro?
45
188
 
46
- @delivery_time = webservice.prazo_entrega + preferred_additional_days
47
- cost = webservice.valor + preferred_additional_value
48
- {cost: cost, delivery_time: @delivery_time}
49
- rescue
50
- {}
51
- end
52
-
53
- def has_contract?
54
- preferred_token.present? && preferred_password.present?
189
+ {value: webservice.valor, delivery_time: webservice.prazo_entrega}
55
190
  end
56
191
  end
57
192
  end
@@ -1,5 +1,6 @@
1
1
  module Spree
2
2
  class Calculator::Shipping::PAC < Calculator::Shipping::CorreiosBaseCalculator
3
+
3
4
  def self.description
4
5
  'PAC'
5
6
  end
@@ -19,5 +20,9 @@ module Spree
19
20
  41106
20
21
  end
21
22
  end
23
+
24
+ def max_weight
25
+ 30
26
+ end
22
27
  end
23
28
  end
@@ -19,5 +19,9 @@ module Spree
19
19
  40010
20
20
  end
21
21
  end
22
+
23
+ def max_weight
24
+ 30
25
+ end
22
26
  end
23
27
  end
@@ -11,5 +11,9 @@ module Spree
11
11
  def shipping_code
12
12
  40215
13
13
  end
14
+
15
+ def max_weight
16
+ 10
17
+ end
14
18
  end
15
19
  end
@@ -1,2 +1,3 @@
1
1
  require 'spree_core'
2
2
  require 'spree_zaez_correios/engine'
3
+ require 'correios-frete'
@@ -2,29 +2,29 @@ require 'spec_helper'
2
2
 
3
3
  describe Spree::Calculator::Shipping::CorreiosBaseCalculator do
4
4
 
5
- before { @calculator = Spree::Calculator::Shipping::CorreiosBaseCalculator.new }
5
+ let(:calculator) { subject.class.new }
6
6
 
7
7
  it 'should have preferences' do
8
8
  preferences = [:token, :password, :additional_days, :additional_value, :declared_value, :receipt_notification, :receive_in_hands]
9
- expect(@calculator.preferences.keys).to eq(preferences)
9
+ expect(calculator.preferences.keys).to eq(preferences)
10
10
  end
11
11
 
12
12
  it 'declared value should default to false' do
13
- expect(@calculator.preferred_declared_value).to eq(false)
13
+ expect(calculator.preferred_declared_value).to eq(false)
14
14
  end
15
15
 
16
16
  it 'receipt notification should default to false' do
17
- expect(@calculator.preferred_receipt_notification).to eq(false)
17
+ expect(calculator.preferred_receipt_notification).to eq(false)
18
18
  end
19
19
 
20
20
  it 'receive in hands should default to false' do
21
- expect(@calculator.preferred_receive_in_hands).to eq(false)
21
+ expect(calculator.preferred_receive_in_hands).to eq(false)
22
22
  end
23
23
 
24
24
  it 'should have a contract if both token and password are given' do
25
- expect(@calculator).not_to have_contract
26
- @calculator.preferred_token = 'some token'
27
- @calculator.preferred_password = 'some password'
28
- expect(@calculator).to have_contract
25
+ expect(calculator).not_to have_contract
26
+ calculator.preferred_token = 'some token'
27
+ calculator.preferred_password = 'some password'
28
+ expect(calculator).to have_contract
29
29
  end
30
30
  end
@@ -1,38 +1,36 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spree::Calculator::Shipping::PAC do
4
- before do
5
- @pac = Spree::Calculator::Shipping::PAC.new
6
- end
4
+ let(:pac) { subject.class.new }
7
5
 
8
6
  it_behaves_like 'correios calculator'
9
7
 
10
8
  it 'should have a description' do
11
- expect(@pac.description).to eq('PAC')
9
+ expect(pac.description).to eq('PAC')
12
10
  end
13
11
 
14
12
  context 'without a token and password' do
15
13
  it 'should have a shipping method of :pac' do
16
- expect(@pac.shipping_method).to eq(:pac)
14
+ expect(pac.shipping_method).to eq(:pac)
17
15
  end
18
16
 
19
17
  it 'should have a shipping code of 41106' do
20
- expect(@pac.shipping_code).to eq(41106)
18
+ expect(pac.shipping_code).to eq(41106)
21
19
  end
22
20
  end
23
21
 
24
22
  context 'with a token and password' do
25
23
  before do
26
- @pac.preferred_token = 'some token'
27
- @pac.preferred_password = 'some password'
24
+ pac.preferred_token = 'some token'
25
+ pac.preferred_password = 'some password'
28
26
  end
29
27
 
30
28
  it 'should have a shipping method of :pac_com_contrato' do
31
- expect(@pac.shipping_method).to eq(:pac_com_contrato)
29
+ expect(pac.shipping_method).to eq(:pac_com_contrato)
32
30
  end
33
31
 
34
32
  it 'should have a shipping code of 41068' do
35
- expect(@pac.shipping_code).to eq(41068)
33
+ expect(pac.shipping_code).to eq(41068)
36
34
  end
37
35
  end
38
36
  end
@@ -1,23 +1,21 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spree::Calculator::Shipping::SEDEX10 do
4
- before do
5
- @sedex10 = Spree::Calculator::Shipping::SEDEX10.new
6
- end
4
+ let(:sedex10) { subject.class.new }
7
5
 
8
6
  it_behaves_like 'correios calculator'
9
7
 
10
8
  it 'should have a description' do
11
- expect(@sedex10.description).to eq('SEDEX 10')
9
+ expect(sedex10.description).to eq('SEDEX 10')
12
10
  end
13
11
 
14
12
  context 'without a token and password' do
15
13
  it 'should have a shipping method of :pac' do
16
- expect(@sedex10.shipping_method).to eq(:sedex_10)
14
+ expect(sedex10.shipping_method).to eq(:sedex_10)
17
15
  end
18
16
 
19
17
  it 'should have a shipping code of 40215' do
20
- expect(@sedex10.shipping_code).to eq(40215)
18
+ expect(sedex10.shipping_code).to eq(40215)
21
19
  end
22
20
  end
23
21
 
@@ -1,38 +1,36 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spree::Calculator::Shipping::SEDEX do
4
- before do
5
- @sedex = Spree::Calculator::Shipping::SEDEX.new
6
- end
4
+ let(:sedex) { subject.class.new }
7
5
 
8
6
  it_behaves_like 'correios calculator'
9
7
 
10
8
  it 'should have a description' do
11
- expect(@sedex.description).to eq('SEDEX')
9
+ expect(sedex.description).to eq('SEDEX')
12
10
  end
13
11
 
14
12
  context 'without a token and password' do
15
13
  it 'should have a shipping method of :pac' do
16
- expect(@sedex.shipping_method).to eq(:sedex)
14
+ expect(sedex.shipping_method).to eq(:sedex)
17
15
  end
18
16
 
19
17
  it 'should have a shipping code of 40010' do
20
- expect(@sedex.shipping_code).to eq(40010)
18
+ expect(sedex.shipping_code).to eq(40010)
21
19
  end
22
20
  end
23
21
 
24
22
  context 'with a token and password' do
25
23
  before do
26
- @sedex.preferred_token = 'some token'
27
- @sedex.preferred_password = 'some password'
24
+ sedex.preferred_token = 'some token'
25
+ sedex.preferred_password = 'some password'
28
26
  end
29
27
 
30
28
  it 'should have a shipping method of :pac_com_contrato' do
31
- expect(@sedex.shipping_method).to eq(:sedex_com_contrato_1)
29
+ expect(sedex.shipping_method).to eq(:sedex_com_contrato_1)
32
30
  end
33
31
 
34
32
  it 'should have a shipping code of 40096' do
35
- expect(@sedex.shipping_code).to eq(40096)
33
+ expect(sedex.shipping_code).to eq(40096)
36
34
  end
37
35
  end
38
36
  end
@@ -27,6 +27,19 @@ module Spree
27
27
  shipping_rates = subject.shipping_rates(package)
28
28
  expect(shipping_rates.first.delivery_time).to eq 1
29
29
  end
30
+
31
+ it 'should not create shipping rate if cost is unknow' do
32
+ allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :compute).and_return({})
33
+ shipping_rates = subject.shipping_rates(package)
34
+ expect(shipping_rates).to eq Array.new
35
+ end
36
+
37
+ it 'should save only cost if delivery time is not provided' do
38
+ allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :compute).and_return(10.0)
39
+ shipping_rate = subject.shipping_rates(package).first
40
+ expect(shipping_rate.cost).to eq 10.0
41
+ expect(shipping_rate.delivery_time).to be_nil
42
+ end
30
43
  end
31
44
  end
32
45
  end
@@ -1,105 +1,142 @@
1
1
  shared_examples_for 'correios calculator' do
2
2
 
3
- before { @calculator = subject.class.new }
3
+ let(:calculator) { subject.class.new }
4
4
 
5
5
  context 'compute_package' do
6
6
 
7
- # @param url [String]
7
+ # Stub the request to Correios Webservice
8
8
  #
9
- # @return [price Float, delivery_time Integer]
9
+ # @param params [Hash]
10
10
  #
11
- def get_correios_price_and_value_for(url)
12
- doc = Nokogiri::XML(open(url))
13
- price = doc.css('Valor').first.content.sub(/,(\d\d)$/, '.\1').to_f
14
- delivery_time = doc.css('PrazoEntrega').first.content.to_i
15
- return price, delivery_time
11
+ def stub_correios_request(params = {})
12
+ correios_attr = {valor: 10.0, prazo_entrega: 1}.merge!(params)
13
+ response = Correios::Frete::Servico.new(correios_attr)
14
+ allow_any_instance_of(Correios::Frete::Calculador).to receive(:calculate).and_return(response)
16
15
  end
17
16
 
18
- before do
19
- address = FactoryGirl.build(:address, zipcode: '17209420')
20
- variant = FactoryGirl.build(:variant, weight: 1, height: 5, width: 15, depth: 20)
21
-
22
- @order = FactoryGirl.build(:order_with_shipments, ship_address: address)
23
- line_item = FactoryGirl.build(:line_item, variant: variant, price: 100, order: @order)
24
- @order.line_items << line_item
25
-
26
- # stock location
27
- @stock_location = FactoryGirl.build(:stock_location, zipcode: '08465312')
28
-
29
- # shipment
30
- @shipment = FactoryGirl.build(:shipment, order: @order, stock_location: @stock_location)
31
- @shipment.inventory_units << FactoryGirl.build(:inventory_unit, variant: variant, order: @order, line_item: line_item, shipment: @shipment)
32
-
33
- # package
34
- @package = @shipment.to_package
35
- @package.add @shipment.inventory_units.first
36
-
37
- # default query
38
- @default_query = {
39
- nCdEmpresa: nil,
40
- sDsSenha: nil,
41
- sCepOrigem: '08465312',
42
- sCepDestino: '17209420',
43
- nVlPeso: 1,
44
- nCdFormato: 1,
45
- nVlComprimento: 20,
46
- nVlAltura: 5,
47
- nVlLargura: 15,
48
- sCdMaoPropria: 'n',
49
- nVlValorDeclarado: 0,
50
- sCdAvisoRecebimento: 'n',
51
- nCdServico: @calculator.shipping_code,
52
- nVlDiametro: 0,
53
- StrRetorno: 'xml'
54
- }
17
+ let(:address) { FactoryGirl.build(:address, zipcode: '12345678') }
18
+ let(:variant) { FactoryGirl.build(:variant, weight: 1, height: 5, width: 15, depth: 20) }
19
+ let(:order) { FactoryGirl.build(:order_with_shipments, ship_address: address) }
20
+ let(:line_item) {FactoryGirl.build(:line_item, variant: variant, price: 100, order: order) }
21
+ let(:stock_location) { FactoryGirl.build(:stock_location, zipcode: '87654321') }
22
+ let(:shipment) { FactoryGirl.build(:shipment, order: order, stock_location: stock_location) }
23
+ let(:inventory_unit) { FactoryGirl.build(:inventory_unit, variant: variant, order: order, line_item: line_item, shipment: shipment) }
24
+ let(:package) do
25
+ order.line_items << line_item
26
+ shipment.inventory_units << inventory_unit
27
+ package = shipment.to_package
28
+ package.add inventory_unit
29
+ package
55
30
  end
56
31
 
57
32
  it 'should calculate shipping cost and delivery time' do
58
- price, delivery_time = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?#{@default_query.to_query}")
33
+ stub_correios_request
59
34
 
60
- expect(@calculator.compute_package(@package)[:cost]).to eq(price)
61
- expect(@calculator.delivery_time).to eq(delivery_time)
35
+ response = calculator.compute_package(package)
36
+ expect(response[:cost]).to eq(10.0)
37
+ expect(response[:delivery_time]).to eq(1)
62
38
  end
63
39
 
64
40
  it 'should possible add days to delivery time' do
65
- price, delivery_time = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?#{@default_query.to_query}")
41
+ stub_correios_request
66
42
 
67
- @calculator.preferred_additional_days = 3
43
+ calculator.preferred_additional_days = 3
68
44
 
69
- @calculator.compute_package(@package)
70
- expect(@calculator.delivery_time).to eq(delivery_time + 3)
45
+ response = calculator.compute_package(package)
46
+ expect(response[:delivery_time]).to eq(4)
71
47
  end
72
48
 
73
49
  it 'should possible add some value to price' do
74
- price, delivery_time = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?#{@default_query.to_query}")
50
+ stub_correios_request
75
51
 
76
- @calculator.preferred_additional_value = 10.0
52
+ calculator.preferred_additional_value = 10.0
77
53
 
78
- expect(@calculator.compute_package(@package)[:cost]).to eq(price + 10.0)
54
+ response = calculator.compute_package(package)
55
+ expect(response[:cost]).to eq(20.0)
79
56
  end
80
57
 
81
58
  it 'should change price according to declared value' do
82
- query = @default_query.merge({nVlValorDeclarado: '100,00'})
83
- price, delivery_time = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?#{query.to_query}")
59
+ stub_correios_request({valor: 15.0, valor_valor_declarado: 100.0})
84
60
 
85
- @calculator.preferred_declared_value = true
86
- expect(@calculator.compute_package(@package)[:cost]).to eq(price)
61
+ calculator.preferred_declared_value = true
62
+
63
+ response = calculator.compute_package(package)
64
+ expect(response[:cost]).to eq(15.0)
87
65
  end
88
66
 
89
67
  it 'should change price according to in hands' do
90
- query = @default_query.merge({sCdMaoPropria: 's'})
91
- price, delivery_time = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?#{query.to_query}")
68
+ stub_correios_request({valor: 17.0, valor_mao_propria: 4.0})
69
+
70
+ calculator.preferred_receive_in_hands = true
92
71
 
93
- @calculator.preferred_receive_in_hands = true
94
- expect(@calculator.compute_package(@package)[:cost]).to eq(price)
72
+ response = calculator.compute_package(package)
73
+ expect(response[:cost]).to eq(17.0)
95
74
  end
96
75
 
97
76
  it 'should change price according to receipt notification' do
98
- query = @default_query.merge({sCdAvisoRecebimento: 's'})
99
- price, delivery_time = get_correios_price_and_value_for("http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?#{query.to_query}")
77
+ stub_correios_request({valor: 21.0, valor_aviso_recebimento: 5.0})
78
+
79
+ calculator.preferred_receipt_notification = true
80
+
81
+ response = calculator.compute_package(package)
82
+ expect(response[:cost]).to eq(21.0)
83
+ end
84
+
85
+ context 'validate dimensions' do
86
+ it 'should split in 2 boxes' do
87
+ variant.weight = 0.3
88
+ variant.width = 100
89
+ variant.height = 30
90
+ variant.depth = 30
91
+
92
+ package.add FactoryGirl.build(:inventory_unit, variant: variant, order: order, line_item: line_item, shipment: shipment)
93
+ package.add FactoryGirl.build(:inventory_unit, variant: variant, order: order, line_item: line_item, shipment: shipment)
94
+
95
+ stub_correios_request
96
+
97
+ # Pacote tem 3 produtos com as dimensoes 100x30x30
98
+ # resultando em duas caixas
99
+ # Cada caixa tem o frete determinado de 10
100
+ # entao o retorno deve ser de 20
101
+
102
+ response = calculator.compute_package(package)
103
+ expect(response[:cost]).to eq(20.0)
104
+ end
105
+ end
106
+ end
107
+
108
+ context 'valid_dimensions?' do
109
+ let(:variant) { FactoryGirl.build(:variant, width: 50, height: 50, depth: 50, weight: 5) }
110
+
111
+ it 'valid if dimensions is permitted' do
112
+ expect(calculator.send(:valid_dimensions?, variant)).to be true
113
+ end
114
+
115
+ it 'invalid if weight is greather than the permitted' do
116
+ variant.weight = calculator.max_weight + 1
117
+ expect(calculator.send(:valid_dimensions?, variant)).to be false
118
+ end
119
+
120
+ it 'invalid if height is greather than 105cm' do
121
+ variant.height = 120
122
+ expect(calculator.send(:valid_dimensions?, variant)).to be false
123
+ end
124
+
125
+ it 'invalid if width is greather than 105cm' do
126
+ variant.width = 120
127
+ expect(calculator.send(:valid_dimensions?, variant)).to be false
128
+ end
129
+
130
+ it 'invalid if depth is greather than 105cm' do
131
+ variant.depth = 120
132
+ expect(calculator.send(:valid_dimensions?, variant)).to be false
133
+ end
100
134
 
101
- @calculator.preferred_receipt_notification = true
102
- expect(@calculator.compute_package(@package)[:cost]).to eq(price)
135
+ it 'invalid if sum of dimensions is greather than 200cm' do
136
+ variant.height = 100
137
+ variant.width = 100
138
+ variant.depth = 100
139
+ expect(calculator.send(:valid_dimensions?, variant)).to be false
103
140
  end
104
141
  end
105
142
  end
@@ -2,7 +2,7 @@
2
2
  Gem::Specification.new do |s|
3
3
  s.platform = Gem::Platform::RUBY
4
4
  s.name = 'spree_zaez_correios'
5
- s.version = '3.0.4'
5
+ s.version = '3.0.5'
6
6
  s.summary = "Implements diverse functions in order to enable the use of services from Brazil's Correios in Spree Commerce"
7
7
  s.description = s.summary
8
8
  s.required_ruby_version = '>= 2.0.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_zaez_correios
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.4
4
+ version: 3.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaez Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-27 00:00:00.000000000 Z
11
+ date: 2015-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spree_core