spree_payment_calculator 0.70.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -9,7 +9,7 @@ Basic Installation
9
9
 
10
10
  1. Add the following to your Gemfile
11
11
  <pre>
12
- gem 'spree_payment_calculator', '~> 0.70.1'
12
+ gem 'spree_payment_calculator', '~> 1.0.0'
13
13
  </pre>
14
14
  2. Run `bundle install`
15
15
  3. Go to Payment Method in the Admin panel and add a calculator for each type of payment you prefer
@@ -0,0 +1,51 @@
1
+ require_dependency 'spree/calculator'
2
+
3
+ module Spree
4
+ class PaymentCalculator::DefaultTax < Calculator
5
+ def self.description
6
+ I18n.t(:default_tax)
7
+ end
8
+
9
+ def compute(computable)
10
+ case computable
11
+ when Spree::Order
12
+ compute_order(computable)
13
+ when Spree::LineItem
14
+ compute_line_item(computable)
15
+ end
16
+ end
17
+
18
+
19
+ private
20
+
21
+ def rate
22
+ rate = self.calculable
23
+ end
24
+
25
+ def compute_order(order)
26
+ matched_line_items = order.line_items.select do |line_item|
27
+ line_item.product.tax_category == rate.tax_category
28
+ end
29
+
30
+ line_items_total = matched_line_items.sum(&:total)
31
+ round_to_two_places(line_items_total * rate.amount)
32
+ end
33
+
34
+ def compute_line_item(line_item)
35
+ if line_item.product.tax_category == rate.tax_category
36
+ deduced_total_by_rate(line_item.total, rate)
37
+ else
38
+ 0
39
+ end
40
+ end
41
+
42
+ def round_to_two_places(amount)
43
+ BigDecimal.new(amount.to_s).round(2, BigDecimal::ROUND_HALF_UP)
44
+ end
45
+
46
+ def deduced_total_by_rate(total, rate)
47
+ round_to_two_places(total - ( total / (1 + rate.amount) ) )
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,20 @@
1
+ require_dependency 'spree/calculator'
2
+
3
+ module Spree
4
+ class PaymentCalculator::FlatPercentItemTotal < Calculator
5
+ preference :flat_percent, :decimal, :default => 0
6
+
7
+ attr_accessible :preferred_flat_percent
8
+
9
+ def self.description
10
+ I18n.t(:flat_percent)
11
+ end
12
+
13
+ def compute(object)
14
+ return unless object.present? and object.line_items.present?
15
+ item_total = object.line_items.map(&:amount).sum
16
+ value = item_total * BigDecimal(self.preferred_flat_percent.to_s) / 100.0
17
+ (value * 100).round.to_f / 100
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ require_dependency 'spree/calculator'
2
+
3
+ module Spree
4
+ class PaymentCalculator::FlatRate < Calculator
5
+ preference :amount, :decimal, :default => 0
6
+
7
+ attr_accessible :preferred_amount
8
+
9
+ def self.description
10
+ I18n.t(:flat_rate_per_order)
11
+ end
12
+
13
+ def compute(object=nil)
14
+ self.preferred_amount
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ require_dependency 'spree/calculator'
2
+
3
+ module Spree
4
+ class PaymentCalculator::FlexiRate < Calculator
5
+ preference :first_item, :decimal, :default => 0.0
6
+ preference :additional_item, :decimal, :default => 0.0
7
+ preference :max_items, :integer, :default => 0
8
+
9
+ attr_accessible :preferred_first_item, :preferred_additional_item, :preferred_max_items
10
+
11
+ def self.description
12
+ I18n.t(:flexible_rate)
13
+ end
14
+
15
+ def self.available?(object)
16
+ true
17
+ end
18
+
19
+ def compute(object)
20
+ sum = 0
21
+ max = self.preferred_max_items.to_i
22
+ items_count = object.line_items.map(&:quantity).sum
23
+ items_count.times do |i|
24
+ # check max value to avoid divide by 0 errors
25
+ if (max == 0 && i == 0) || (max > 0) && (i % max == 0)
26
+ sum += self.preferred_first_item.to_f
27
+ else
28
+ sum += self.preferred_additional_item.to_f
29
+ end
30
+ end
31
+
32
+ sum
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ require_dependency 'spree/calculator'
2
+
3
+ module Spree
4
+ class PaymentCalculator::PerItem < Calculator
5
+ preference :amount, :decimal, :default => 0
6
+
7
+ attr_accessible :preferred_amount
8
+
9
+ def self.description
10
+ I18n.t(:flat_rate_per_item)
11
+ end
12
+
13
+ def compute(object=nil)
14
+ return 0 if object.nil?
15
+ self.preferred_amount * object.line_items.reduce(0) do |sum, value|
16
+ sum + value.quantity
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ require_dependency 'spree/calculator'
2
+
3
+ module Spree
4
+ class PaymentCalculator::PriceSack < Calculator
5
+ preference :minimal_amount, :decimal, :default => 0
6
+ preference :normal_amount, :decimal, :default => 0
7
+ preference :discount_amount, :decimal, :default => 0
8
+
9
+ attr_accessible :preferred_minimal_amount,
10
+ :preferred_normal_amount,
11
+ :preferred_discount_amount
12
+
13
+ def self.description
14
+ I18n.t(:price_sack)
15
+ end
16
+
17
+ # as object we always get line items, as calculable we have Coupon, ShippingMethod
18
+ def compute(object)
19
+ if object.is_a?(Array)
20
+ base = object.map { |o| o.respond_to?(:amount) ? o.amount : o.to_d }.sum
21
+ else
22
+ base = object.respond_to?(:amount) ? object.amount : object.to_d
23
+ end
24
+
25
+ if base >= self.preferred_minimal_amount
26
+ self.preferred_normal_amount
27
+ else
28
+ self.preferred_discount_amount
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,4 @@
1
- Payment.class_eval do
1
+ Spree::Payment.class_eval do
2
2
  has_one :adjustment, :as => :source
3
3
  before_save :remove_old_adjustment
4
4
  after_save :ensure_correct_adjustment, :update_order, :if => Proc.new {|p| !p.payment_method.calculator.blank?}
@@ -0,0 +1,3 @@
1
+ Spree::PaymentMethod.class_eval do
2
+ calculated_adjustments
3
+ end
@@ -1,5 +1,5 @@
1
- Deface::Override.new(:virtual_path => 'admin/payment_methods/_form',
1
+ Deface::Override.new(:virtual_path => 'spree/admin/payment_methods/_form',
2
2
  :name => 'add_calculators_to_payment_methods',
3
3
  :insert_before => %q{[id='preference-settings']},
4
- :text => %q{<% @calculators = PaymentMethod.send_calculator(params[:id]) %><%= render :partial => 'admin/shared/calculator_fields', :locals => {:f => f}},
4
+ :text => %q{<% @calculators = Spree::PaymentMethod.calculators.sort_by(&:name) rescue nil %><%= render :partial => 'spree/admin/shared/calculator_fields', :locals => {:f => f}},
5
5
  :disabled => false)
@@ -1,3 +1,4 @@
1
1
  ---
2
2
  en:
3
- payment_cost: "Fixed Costs by Type of Payment"
3
+ payment_cost: "Fixed Costs by Type of Payment"
4
+ payment_adjustment: "Fees"
@@ -1,3 +1,4 @@
1
1
  ---
2
2
  en:
3
- payment_cost: "Costi Fissi per Tipologia di Pagamento"
3
+ payment_cost: "Costi Fissi per Tipologia di Pagamento"
4
+ payment_adjustment: "Costo Aggiuntivo"
data/config/routes.rb CHANGED
@@ -1,3 +1,3 @@
1
- Rails.application.routes.draw do
1
+ Spree::Core::Engine.routes.prepend do
2
2
  # Add your extension routes here
3
3
  end
@@ -11,6 +11,7 @@ module SpreePaymentCalculator
11
11
  #inject_into_file "app/assets/stylesheets/store/all.css", " *= require store/spree_payment_calculator\n", :before => /\*\//, :verbose => true
12
12
  #inject_into_file "app/assets/stylesheets/admin/all.css", " *= require admin/spree_payment_calculator\n", :before => /\*\//, :verbose => true
13
13
  end
14
+
14
15
  end
15
16
  end
16
17
  end
@@ -0,0 +1,11 @@
1
+ module Spree
2
+ module Core
3
+ class Environment
4
+ class Calculators
5
+ include EnvironmentExtension
6
+
7
+ attr_accessor :shipping_methods, :tax_rates, :payment_methods
8
+ end
9
+ end
10
+ end
11
+ end
@@ -17,35 +17,14 @@ module SpreePaymentCalculator
17
17
  Dir.glob(File.join(File.dirname(__FILE__), "../../app/overrides/*.rb")) do |c|
18
18
  Rails.application.config.cache_classes ? require(c) : load(c)
19
19
  end
20
+
21
+ Dir.glob(File.join(File.dirname(__FILE__), "../../lib/spree/environment/calculator.rb")) do |c|
22
+ Rails.application.config.cache_classes ? require(c) : load(c)
23
+ end
20
24
  end
21
-
22
25
  initializer 'spree.register.calculators' do |app|
23
- if Gem::Specification::find_by_name('spree_paypal_express')
24
- BillingIntegration.class_eval do
25
- calculated_adjustments
26
- end
27
- app.config.spree.calculators.add_class('billing_integrations')
28
- app.config.spree.calculators.billing_integrations = [
29
- PaymentCalculator::PriceSack,
30
- PaymentCalculator::FlatPercentItemTotal,
31
- PaymentCalculator::FlatRate,
32
- PaymentCalculator::FlexiRate,
33
- PaymentCalculator::PerItem
34
- ]
35
- end
36
-
37
- app.config.spree.calculators.add_class('payment_methods')
38
- app.config.spree.calculators.add_class('gateways')
39
-
40
26
  app.config.spree.calculators.payment_methods = [
41
- PaymentCalculator::PriceSack,
42
- PaymentCalculator::FlatPercentItemTotal,
43
- PaymentCalculator::FlatRate,
44
- PaymentCalculator::FlexiRate,
45
- PaymentCalculator::PerItem
46
- ]
47
-
48
- app.config.spree.calculators.gateways = [
27
+ PaymentCalculator::DefaultTax,
49
28
  PaymentCalculator::PriceSack,
50
29
  PaymentCalculator::FlatPercentItemTotal,
51
30
  PaymentCalculator::FlatRate,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_payment_calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.70.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-13 00:00:00.000000000 Z
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spree_core
@@ -52,20 +52,21 @@ files:
52
52
  - README.md
53
53
  - LICENSE
54
54
  - lib/generators/spree_payment_calculator/install/install_generator.rb
55
+ - lib/spree/core/environment/calculators.rb
55
56
  - lib/spree_payment_calculator/engine.rb
56
57
  - lib/spree_payment_calculator.rb
57
58
  - app/assets/javascripts/admin/spree_payment_calculator.js
58
59
  - app/assets/javascripts/store/spree_payment_calculator.js
59
60
  - app/assets/stylesheets/admin/spree_payment_calculator.css
60
61
  - app/assets/stylesheets/store/spree_payment_calculator.css
61
- - app/model/gateway_decorator.rb
62
- - app/model/payment_calculator/flat_percent_item_total.rb
63
- - app/model/payment_calculator/flat_rate.rb
64
- - app/model/payment_calculator/flexi_rate.rb
65
- - app/model/payment_calculator/per_item.rb
66
- - app/model/payment_calculator/price_sack.rb
67
- - app/model/payment_decorator.rb
68
- - app/model/payment_method_decorator.rb
62
+ - app/model/spree/payment_calculator/default_tax.rb
63
+ - app/model/spree/payment_calculator/flat_percent_item_total.rb
64
+ - app/model/spree/payment_calculator/flat_rate.rb
65
+ - app/model/spree/payment_calculator/flexi_rate.rb
66
+ - app/model/spree/payment_calculator/per_item.rb
67
+ - app/model/spree/payment_calculator/price_sack.rb
68
+ - app/model/spree/payment_decorator.rb
69
+ - app/model/spree/payment_method_decorator.rb
69
70
  - app/overrides/add_calculators_to_payment_methods.rb
70
71
  - config/locales/en.yml
71
72
  - config/locales/it.yml
@@ -1,3 +0,0 @@
1
- Gateway.class_eval do
2
- calculated_adjustments
3
- end
@@ -1,14 +0,0 @@
1
- class PaymentCalculator::FlatPercentItemTotal < Calculator
2
- preference :flat_percent, :decimal, :default => 0
3
-
4
- def self.description
5
- I18n.t("flat_percent")
6
- end
7
-
8
- def compute(object)
9
- return unless object.present? and object.line_items.present?
10
- item_total = object.line_items.map(&:amount).sum
11
- value = item_total * self.preferred_flat_percent / 100.0
12
- (value * 100).round.to_f / 100
13
- end
14
- end
@@ -1,11 +0,0 @@
1
- class PaymentCalculator::FlatRate < Calculator
2
- preference :amount, :decimal, :default => 0
3
-
4
- def self.description
5
- I18n.t("flat_rate_per_order")
6
- end
7
-
8
- def compute(object=nil)
9
- self.preferred_amount
10
- end
11
- end
@@ -1,27 +0,0 @@
1
- class PaymentCalculator::FlexiRate < Calculator
2
- preference :first_item, :decimal, :default => 0
3
- preference :additional_item, :decimal, :default => 0
4
- preference :max_items, :decimal, :default => 0
5
-
6
- def self.description
7
- I18n.t("flexible_rate")
8
- end
9
-
10
- def self.available?(object)
11
- true
12
- end
13
-
14
- def compute(object)
15
- sum = 0
16
- max = self.preferred_max_items
17
- items_count = object.line_items.map(&:quantity).sum
18
- items_count.times do |i|
19
- if (i % max == 0) && (max > 0)
20
- sum += self.preferred_first_item
21
- else
22
- sum += self.preferred_additional_item
23
- end
24
- end
25
- return(sum)
26
- end
27
- end
@@ -1,11 +0,0 @@
1
- class PaymentCalculator::PerItem < Calculator
2
- preference :amount, :decimal, :default => 0
3
-
4
- def self.description
5
- I18n.t("flat_rate_per_item")
6
- end
7
-
8
- def compute(object=nil)
9
- self.preferred_amount * object.line_items.length
10
- end
11
- end
@@ -1,24 +0,0 @@
1
- class PaymentCalculator::PriceSack < Calculator
2
- preference :minimal_amount, :decimal, :default => 0
3
- preference :normal_amount, :decimal, :default => 0
4
- preference :discount_amount, :decimal, :default => 0
5
-
6
- def self.description
7
- I18n.t("price_sack")
8
- end
9
-
10
- # as object we always get line items, as calculable we have Coupon, ShippingMethod
11
- def compute(object)
12
- if object.is_a?(Array)
13
- base = object.map{ |o| o.respond_to?(:amount) ? o.amount : o.to_d }.sum
14
- else
15
- base = object.respond_to?(:amount) ? object.amount : object.to_d
16
- end
17
-
18
- if base >= self.preferred_minimal_amount
19
- self.preferred_normal_amount
20
- else
21
- self.preferred_discount_amount
22
- end
23
- end
24
- end
@@ -1,17 +0,0 @@
1
- PaymentMethod.class_eval do
2
- calculated_adjustments
3
-
4
- def self.send_calculator(id = nil)
5
- if id
6
- if PaymentMethod.find(id).class.to_s.match(/Gateway/)
7
- return Gateway.calculators.sort_by(&:name)
8
- elsif PaymentMethod.find(id).class.to_s.match(/BillingIntegration/)
9
- return BillingIntegration.calculators.sort_by(&:name)
10
- else
11
- return PaymentMethod.calculators.sort_by(&:name)
12
- end
13
- else
14
- return PaymentMethod.calculators.sort_by(&:name)
15
- end
16
- end
17
- end