Module: Voucherify::Utils
- Defined in:
- lib/voucherify/utils.rb
Class Method Summary collapse
- .calculate_discount(base_price, voucher, unit_price = nil) ⇒ Object
- .calculate_price(base_price, voucher, unit_price = nil) ⇒ Object
- .round_money(value) ⇒ Object
- .validate_amount_discount(discount) ⇒ Object
- .validate_percent_discount(discount) ⇒ Object
- .validate_unit_discount(discount) ⇒ Object
Class Method Details
.calculate_discount(base_price, voucher, unit_price = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/voucherify/utils.rb', line 63 def self.calculate_discount(base_price, voucher, unit_price = nil) e = 100.0 if voucher[:gift] discount = [voucher[:gift][:balance] / e, base_price].min return round_money(discount) end if !voucher[:discount] raise 'Unsupported voucher type.' end if voucher[:discount][:type] === 'PERCENT' discount = voucher[:discount][:percent_off] validate_percent_discount(discount); price_discount = base_price * (discount / 100.0) return round_money(price_discount) elsif voucher[:discount][:type] === 'AMOUNT' discount = voucher[:discount][:amount_off] / e validate_amount_discount(discount) new_price = base_price - discount return round_money(new_price > 0 ? (discount) : (base_price)) elsif voucher[:discount][:type] === 'UNIT' if !unit_price raise 'Missing unit_price argument.' end discount = voucher[:discount][:unit_off] validate_unit_discount(discount) price_discount = unit_price * discount return round_money(price_discount > base_price ? (base_price) : (price_discount)) else raise 'Unsupported discount type' end end |
.calculate_price(base_price, voucher, unit_price = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/voucherify/utils.rb', line 25 def self.calculate_price(base_price, voucher, unit_price = nil) e = 100.0 if voucher[:gift] discount = [voucher[:gift][:balance] / e, base_price].min return round_money(base_price - discount) end if !voucher[:discount] raise 'Unsupported voucher type.' end if voucher[:discount][:type] === 'PERCENT' discount = voucher[:discount][:percent_off] validate_percent_discount(discount); price_discount = base_price * (discount / 100.0) return round_money(base_price - price_discount) elsif voucher[:discount][:type] === 'AMOUNT' discount = voucher[:discount][:amount_off] / e validate_amount_discount(discount) new_price = base_price - discount return round_money(new_price > 0 ? (new_price) : 0) elsif voucher[:discount][:type] === 'UNIT' if !unit_price raise 'Missing unit_price argument.' end discount = voucher[:discount][:unit_off] validate_unit_discount(discount) new_price = base_price - unit_price * discount return round_money(new_price > 0 ? (new_price) : 0) else raise 'Unsupported discount type' end end |
.round_money(value) ⇒ Object
3 4 5 |
# File 'lib/voucherify/utils.rb', line 3 def self.round_money(value) value.round(2) end |
.validate_amount_discount(discount) ⇒ Object
13 14 15 16 17 |
# File 'lib/voucherify/utils.rb', line 13 def self.validate_amount_discount(discount) if !(discount.is_a? Numeric) || discount < 0 raise 'Invalid voucher, amount discount must be bigger than zero.' end end |
.validate_percent_discount(discount) ⇒ Object
7 8 9 10 11 |
# File 'lib/voucherify/utils.rb', line 7 def self.validate_percent_discount(discount) if !(discount.is_a? Numeric) || !discount.between?(0, 100) raise 'Invalid voucher, percent discount should be between 0-100.' end end |
.validate_unit_discount(discount) ⇒ Object
19 20 21 22 23 |
# File 'lib/voucherify/utils.rb', line 19 def self.validate_unit_discount(discount) if !(discount.is_a? Numeric) || discount < 0 raise 'Invalid voucher, unit discount must be bigger than zero.' end end |