solidus-returnly 0.10.0 → 0.11.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: ff60269717cc854516bde94a85ab7c708fad2eed35bacdee31933460416c502f
4
- data.tar.gz: 90e483a9b1da8e55de1eed1a01da9b008a8d6b206f5d0410a006249ba1da89cb
3
+ metadata.gz: bbba58335cd0c6733fd3ac7ac1cd1138f9fe60c3c9fd054d333d6cd214880af4
4
+ data.tar.gz: 92908dfff3ed9d1725e8439404ea3da94bbe7849d6b004ea95eb81dca871e7ac
5
5
  SHA512:
6
- metadata.gz: f273e4f016e1eb454eccb16a9cbfecfd3260069a70f90fca9a56a8e7ec23944dc2cbe10553f267fc644646c70eedd1b8d1f35f178720438599a79b99de3aeb72
7
- data.tar.gz: 373866d8194a4f50077d3e35d005ffaed3f037b666092a4108116db6efec2417809153b1d3d463511d061c393ea5c0ae0c6ccfffebfadf7a9207b447cfb0f0f5
6
+ metadata.gz: acb300cf3987ae5db4687721141f20336abb11cd333e27f44edd60a45a8b0b63d362a5c1cd31da59654c7788a2f07a8099b554facacf5bd8f9273c339d222f48
7
+ data.tar.gz: bc19abf556483df2f5c5931f1e607607fc2f414cb51e93423c0c1287091c4eee51ecef94e47de732a8c1f7645151ee8c7807e5d4fb403a4cb47191144784245c
@@ -8,23 +8,30 @@ module Returnly
8
8
  end
9
9
 
10
10
  def build_by_requested_line_items(requested_line_items)
11
- requested_line_items.each_with_object([]) do |request_line_item, return_items|
11
+ is_returned_item = false
12
+ items = requested_line_items.each_with_object([]) do |request_line_item, return_items|
12
13
  quantity = request_line_item[:units].to_i
13
14
  next return_items unless quantity > 0
14
15
 
15
- inventory_units_by(request_line_item[:order_line_item_id]).take(quantity).each do |inventory_unit|
16
+ inventory_units = inventory_units_by(request_line_item[:order_line_item_id])
17
+ if inventory_units.empty?
18
+ inventory_units = returned_inventory_units_by(request_line_item[:order_line_item_id])
19
+ request_line_item[:find_return?] = true
20
+ is_returned_item = true
21
+ end
22
+ inventory_units.take(quantity).each do |inventory_unit|
16
23
  return_items << build_by_inventory_unit(inventory_unit, request_line_item)
17
24
  end
18
25
 
19
26
  return_items
20
27
  end
28
+ {
29
+ items: items,
30
+ is_returned_items: is_returned_item
31
+ }
21
32
  end
22
33
 
23
- def build_for_estimate(requested_line_items)
24
- build_by_requested_line_items(requested_line_items)
25
- end
26
-
27
- def build_by_inventory_unit(inventory_unit, options = {})
34
+ def exact_build_by_inventory_unit(inventory_unit, options = {})
28
35
  Spree::ReturnItem.new(
29
36
  amount: inventory_unit.line_item.price,
30
37
  acceptance_status: 'accepted',
@@ -34,6 +41,14 @@ module Returnly
34
41
  )
35
42
  end
36
43
 
44
+ def build_by_inventory_unit(inventory_unit, options = {})
45
+ return exact_build_by_inventory_unit(inventory_unit, options) unless options[:find_return?]
46
+ Spree::ReturnItem.find_by(
47
+ amount: inventory_unit.line_item.price,
48
+ inventory_unit_id: inventory_unit.id
49
+ )
50
+ end
51
+
37
52
  private
38
53
 
39
54
  def inventory_units_by(line_item_ids)
@@ -41,6 +56,12 @@ module Returnly
41
56
  .where(line_item_id: line_item_ids)
42
57
  .where.not(state: 'returned')
43
58
  end
59
+
60
+ def returned_inventory_units_by(line_item_ids)
61
+ order.inventory_units.includes(:line_item, :return_items)
62
+ .where(line_item_id: line_item_ids, state: 'returned')
63
+ .where(spree_return_items: { reimbursement_id: nil })
64
+ end
44
65
  end
45
66
  end
46
67
  end
@@ -20,13 +20,18 @@ module Returnly
20
20
  (line_item.adjustments.tax.sum(&:amount).to_d / line_item.quantity).round(2, :down)
21
21
  end
22
22
 
23
- def line_items_return_items(is_estimate)
24
- available_return_items(is_estimate).each_with_object({}) do |return_item, return_items|
23
+ def line_items_return_items
24
+ available_to_return = available_return_items
25
+ items = available_to_return[:items].each_with_object({}) do |return_item, return_items|
25
26
  line_item_id = return_item.inventory_unit.line_item_id
26
27
  return_items[line_item_id] ||= []
27
28
  return_items[line_item_id] << set_tax(return_item)
28
29
  return_items
29
30
  end
31
+ {
32
+ items: items,
33
+ is_returned_items: available_to_return[:is_returned_items]
34
+ }
30
35
  end
31
36
 
32
37
  def shipping_tax
@@ -35,9 +40,7 @@ module Returnly
35
40
 
36
41
  protected
37
42
 
38
- def available_return_items(is_estimate)
39
- return self.class.return_item_builder_class.new(order).build_for_estimate(line_items) if is_estimate
40
-
43
+ def available_return_items
41
44
  self.class.return_item_builder_class.new(order).build_by_requested_line_items(line_items)
42
45
  end
43
46
 
@@ -5,12 +5,16 @@ module Returnly
5
5
  sub_total = Money.new 0
6
6
  tax = Money.new 0
7
7
 
8
- calculator.line_items_return_items(true).values.flatten.each do |return_item|
8
+ return_items = calculator.line_items_return_items
9
+ return_items[:items].values.flatten.each do |return_item|
9
10
  sub_total += Money.from_amount return_item.total_excluding_vat
10
11
  tax += Money.from_amount calculator.line_item_tax(return_item.inventory_unit.line_item)
11
12
  end
12
13
 
13
14
  discount = Money.from_amount(calculator.discount)
15
+ if return_items[:is_returned_items]
16
+ sub_total += discount
17
+ end
14
18
  total_refund_amount = sub_total + tax - (discount * -1) # discount is negative, so we multiply it by -1 to make it positive for subtraction
15
19
  shipping_tax = calculator.shipping_tax
16
20
  {
@@ -95,7 +95,7 @@ module Returnly
95
95
  end
96
96
 
97
97
  def each_return_item
98
- return_items = refund_calculator.line_items_return_items(false).values.flatten
98
+ return_items = refund_calculator.line_items_return_items[:items].values.flatten
99
99
  self.customer_return = Returnly::Builders::CustomerReturn.build_by_return_items(return_items)
100
100
 
101
101
  return_items.each do |return_item|
@@ -1,5 +1,5 @@
1
1
  module Returnly
2
- VERSION = '0.10.0'
2
+ VERSION = '0.11.0'
3
3
 
4
4
  def platform_version
5
5
  defined?(Solidus)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus-returnly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Returnly Technologies, Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-18 00:00:00.000000000 Z
11
+ date: 2018-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus