Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10821 from rioug/10727-vouchers-percentage-rate
[Vouchers] Percentage rate
- Loading branch information
Showing
21 changed files
with
638 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: false | ||
|
||
module Vouchers | ||
class FlatRate < Voucher | ||
validates :amount, | ||
presence: true, | ||
numericality: { greater_than: 0 } | ||
|
||
def display_value | ||
Spree::Money.new(amount) | ||
end | ||
|
||
# We limit adjustment to the maximum amount needed to cover the order, ie if the voucher | ||
# covers more than the order.total we only need to create an adjustment covering the order.total | ||
def compute_amount(order) | ||
-amount.clamp(0, order.pre_discount_total) | ||
end | ||
|
||
def rate(order) | ||
amount = compute_amount(order) | ||
|
||
amount / order.pre_discount_total | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: false | ||
|
||
module Vouchers | ||
class PercentageRate < Voucher | ||
validates :amount, | ||
presence: true, | ||
numericality: { greater_than: 0, less_than_or_equal_to: 100 } | ||
|
||
def display_value | ||
ActionController::Base.helpers.number_to_percentage(amount, precision: 2) | ||
end | ||
|
||
def compute_amount(order) | ||
rate(order) * order.pre_discount_total | ||
end | ||
|
||
def rate(_order) | ||
-amount / 100 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AddTypeToVouchers < ActiveRecord::Migration[7.0] | ||
def up | ||
# It will set all the vouchers till now to Vouchers::FlatRate | ||
add_column :vouchers, :type, :string, limit: 255, null: false, default: "Vouchers::FlatRate" | ||
end | ||
|
||
def down | ||
remove_column :vouchers, :type | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :voucher do | ||
factory :voucher, class: Voucher do | ||
code { "new_code" } | ||
enterprise { build(:distributor_enterprise) } | ||
amount { 10 } | ||
end | ||
|
||
factory :voucher_flat_rate, parent: :voucher, class: Vouchers::FlatRate do | ||
amount { 15 } | ||
end | ||
|
||
factory :voucher_percentage_rate, parent: :voucher, class: Vouchers::PercentageRate do | ||
amount { rand(1..100) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.