Class: Spree::CieloConfiguration

Inherits:
Preferences::Configuration
  • Object
show all
Defined in:
lib/spree/cielo_configuration.rb

Instance Method Summary collapse

Instance Method Details

#calculate_portion_value(order, portion) ⇒ Float

Calculate the value of the portion based on Cielo configuration (verify if the portion has tax)

Parameters:

  • order (Spree::Order)
  • portion (Integer)

Returns:

  • (Float)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/spree/cielo_configuration.rb', line 58

def calculate_portion_value(order, portion)
  amount = order.total.to_f
  amount = amount / 100 if amount.is_a? Integer
  tax = preferred_tax_value.to_f

  if tax <= 0 or portion <= preferred_portion_without_tax
    value = amount / portion
  else
    value = (amount * ((1 + tax / 100) ** portion)) / portion
  end
  value
end

#calculate_portions(order, cc_type) ⇒ Array

Calculates the portions of credit card type based on Cielo configuration

Parameters:

  • order (Spree::Order)
  • cc_type (String)

Returns:

  • (Array)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spree/cielo_configuration.rb', line 22

def calculate_portions(order, cc_type)
  amount = order.total.to_f
  ret = []
  if preferred_credit_cards.has_key? cc_type
    portions_number = preferred_credit_cards[cc_type]
    minimum_value = preferred_minimum_value.to_f
    tax = preferred_tax_value.to_f

    ret.push({portion: 1, value: amount, total: amount, tax_message: :cielo_without_tax})

    (2..portions_number).each do |number|
      if tax <= 0 or number <= preferred_portion_without_tax
        value = amount / number
        tax_message = :cielo_without_tax
      else
        value = (amount * ((1 + tax / 100) ** number)) / number
        tax_message = :cielo_with_tax
      end

      if value >= minimum_value
        value_total = value * number
        ret.push({portion: number, value: value, total: value_total, tax_message: tax_message})
      end
    end
  end
  ret
end