solidus-returnly 0.6.0 → 0.6.1

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: '083e914f59852fdd042dcb656a7586374fe4f780'
4
- data.tar.gz: e99474dab16979ca9aa30dce9a1fa814dd1002c4
3
+ metadata.gz: 2043001dcfb19498dc6e5ef56b5b39248485632a
4
+ data.tar.gz: ec0c2f446acf7b5eac9687c8efa8d81e71f7b020
5
5
  SHA512:
6
- metadata.gz: 93c7c301d731ad1a45c41caefb37cd42d343af2413ec8466ad2b580b8c81c6d7fe45d7391cb7b182f7a5dc484384003d73ca5de182dc447af9ed37e317e44392
7
- data.tar.gz: b650b812d349d35541d116fed203d3400da769610e716a2c91286d9da516a09ddd676578c830dfd09a205ceae93509b3ee161af3e90b2d46d097b0a952a45ef6
6
+ metadata.gz: 29c6dfe656d053f93198ce4a5987b3070b7a0511d361b44f6cf0a79912f267bb086503e7c445b549a7900c894541965788b5c26d341adb5915fedfb752616964
7
+ data.tar.gz: b315483a1960b7640f5004e537a2ecaa21d7300a8a3cc06d4754007a1cf7f1a1a46c190d59f44a8aab59817c7c70ec03aa9af0a0292e81e735f878ac924638d4
@@ -2,7 +2,7 @@ module Spree
2
2
  module Api
3
3
  module Returnly
4
4
  class ApiController < Spree::Api::BaseController
5
- before_filter :set_headers
5
+ before_action :set_headers
6
6
 
7
7
  def set_headers
8
8
  response.headers['X-Returnly-Extension-Version'] = ::Returnly::VERSION
@@ -1,9 +1,12 @@
1
1
  require 'returnly/engine'
2
2
  require 'returnly/refunds_configuration'
3
3
  require 'returnly/refunder'
4
+ require 'returnly/discounts/order'
5
+ require 'returnly/discounts/line_item'
6
+ require 'returnly/discount_calculator'
4
7
  require 'returnly/refund_calculator'
5
8
 
6
9
  require 'returnly/version'
7
10
  require 'returnly/refund/amount_calculator'
8
11
  require 'returnly/refund/return_item_restock_policy'
9
- require 'returnly/refund_presenter'
12
+ require 'returnly/refund_presenter'
@@ -0,0 +1,51 @@
1
+ module Returnly
2
+ class DiscountCalculator
3
+ attr_accessor :order
4
+
5
+ DISCOUNTER_CLASSES = {
6
+ 'Spree::Order' => Returnly::Discounts::Order,
7
+ 'Spree::LineItem' => Returnly::Discounts::LineItem
8
+ }.freeze
9
+
10
+ def initialize(order)
11
+ @order = order
12
+ end
13
+
14
+ def calculate(line_items = [])
15
+ line_items.inject(0) do |discount_amount, item|
16
+ line_item = find_line_item(item[:order_line_item_id])
17
+ next discount_amount if line_item.nil?
18
+
19
+ discount_amount += discount_amount_for(line_item, item[:units].to_i)
20
+ discount_amount
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def discount_amount_for(line_item, units)
27
+ discounters.inject(0) do |amount, discounter|
28
+ amount += discounter.discount_amount(line_item, units)
29
+ amount
30
+ end
31
+ end
32
+
33
+ def discounters
34
+ order_promotion_adjustments.map { |promotion_adjustment| build_discounter_by(promotion_adjustment) }.compact
35
+ end
36
+
37
+ def build_discounter_by(promotion_adjustment)
38
+ discounter_class = DISCOUNTER_CLASSES[promotion_adjustment.adjustable_type]
39
+ return nil if discounter_class.nil?
40
+ discounter_class.new(order, promotion_adjustment)
41
+ end
42
+
43
+ def order_promotion_adjustments
44
+ order.all_adjustments.where(source_type: 'Spree::PromotionAction', eligible: true)
45
+ end
46
+
47
+ def find_line_item(id)
48
+ order.line_items.find_by(id: id)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,26 @@
1
+ module Returnly
2
+ module Discounts
3
+ class LineItem
4
+ attr_reader :adjustment, :order
5
+
6
+ def initialize(order, adjustment)
7
+ @adjustment = adjustment
8
+ @order = order
9
+ end
10
+
11
+ def discount_amount(line_item, units = 0)
12
+ return 0.0 if units <= 0
13
+ return 0.0 if adjustment.adjustable != line_item
14
+
15
+ units = line_item.quantity if units > line_item.quantity
16
+ adjustment.amount * weight_of(line_item, units.to_d)
17
+ end
18
+
19
+ private
20
+
21
+ def weight_of(line_item, units)
22
+ (units / line_item.quantity).round(2, :down)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ module Returnly
2
+ module Discounts
3
+ class Order
4
+ attr_reader :adjustment, :order
5
+
6
+ def initialize(order, adjustment)
7
+ @adjustment = adjustment
8
+ @order = order
9
+ end
10
+
11
+ def discount_amount(line_item, units = 0)
12
+ return 0.0 if units <= 0
13
+
14
+ units = line_item.quantity if units > line_item.quantity
15
+ (adjustment.amount * price_percent(line_item) / 100) * weight_of(line_item, units.to_d)
16
+ end
17
+
18
+ private
19
+
20
+ def price_percent(line_item)
21
+ (line_item.price * 100 / order_items_price).round(2, :down)
22
+ end
23
+
24
+ def order_items_price
25
+ order.line_items.sum(&:price)
26
+ end
27
+
28
+ def weight_of(line_item, units)
29
+ (units / line_item.quantity).round(2, :down)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -24,6 +24,10 @@ module Returnly
24
24
  return_item
25
25
  end
26
26
 
27
+ def discount
28
+ Returnly::DiscountCalculator.new(order).calculate(line_items)
29
+ end
30
+
27
31
  def shipping_tax
28
32
  order.all_adjustments.where(source_type: 'Spree::TaxRate', adjustable_type: 'Spree::Shipment').sum(&:amount)
29
33
  end
@@ -10,12 +10,13 @@ module Returnly
10
10
  sub_total += Money.from_amount return_item.pre_tax_amount
11
11
  end
12
12
 
13
- total_refund_amount = total - calculator.order.promo_total
13
+ discount = Money.from_amount(calculator.discount)
14
+ total_refund_amount = total - (discount * -1) # discount is negative, so we multiply it by -1 to make it positive for subtraction
14
15
  shipping_tax = calculator.shipping_tax
15
16
  {
16
17
  product_amount: sub_total.to_f,
17
18
  tax_amount: (total - sub_total).to_f,
18
- discount_amount: calculator.order.promo_total.to_f,
19
+ discount_amount: discount.to_f,
19
20
  total_refund_amount: total_refund_amount.to_f,
20
21
  refundable_shipping_amount: calculator.order.shipment_total.to_f,
21
22
  refundable_shipping_tax_amount: shipping_tax.to_f,
@@ -1,5 +1,5 @@
1
1
  module Returnly
2
- VERSION = '0.6.0'
2
+ VERSION = '0.6.1'
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.6.0
4
+ version: 0.6.1
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: 2017-09-05 00:00:00.000000000 Z
11
+ date: 2017-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus
@@ -335,6 +335,9 @@ files:
335
335
  - config/locales/en.yml
336
336
  - config/routes.rb
337
337
  - lib/returnly.rb
338
+ - lib/returnly/discount_calculator.rb
339
+ - lib/returnly/discounts/line_item.rb
340
+ - lib/returnly/discounts/order.rb
338
341
  - lib/returnly/engine.rb
339
342
  - lib/returnly/factories.rb
340
343
  - lib/returnly/refund/amount_calculator.rb
@@ -366,7 +369,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
366
369
  version: '0'
367
370
  requirements: []
368
371
  rubyforge_project:
369
- rubygems_version: 2.6.11
372
+ rubygems_version: 2.4.5.1
370
373
  signing_key:
371
374
  specification_version: 4
372
375
  summary: Returnly integration with Solidus