Class: Voucher

Inherits:
Ruleby::Rulebook
  • Object
show all
Defined in:
lib/voucher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cart=(value) ⇒ Object (writeonly)

Sets the attribute cart

Parameters:

  • value

    the value to set the attribute cart to.



4
5
6
# File 'lib/voucher.rb', line 4

def cart=(value)
  @cart = value
end

#code=(value) ⇒ Object (writeonly)

Sets the attribute code

Parameters:

  • value

    the value to set the attribute code to.



4
5
6
# File 'lib/voucher.rb', line 4

def code=(value)
  @code = value
end

#free_product_ids=(value) ⇒ Object (writeonly)

Sets the attribute free_product_ids

Parameters:

  • value

    the value to set the attribute free_product_ids to.



4
5
6
# File 'lib/voucher.rb', line 4

def free_product_ids=(value)
  @free_product_ids = value
end

Instance Method Details

#rulesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/voucher.rb', line 6

def rules
  VoucherRule.find_all_by_active_and_code(true,@code).each do |voucher|
    rule eval(voucher.conditions) do |context|
      @cart.voucher = voucher.id

      # Voucher for a free product
      voucher.variables[:product_ids].each do |product_id|
        @free_product_ids << product_id
      end if voucher.variables[:product_ids]

      # Voucher for a product price discount
      if voucher.variables[:discount] and product = (context[:product] || context[:pack])
        rate = voucher.variables[:discount].to_f
        options = {:voucher_discount => false}
        discount_price = voucher.variables[:fixed_discount] ? rate : (product.price(options) * rate).to_f / 100
        product.voucher_discount_price = discount_price
        product.voucher_discount = (voucher.variables[:fixed_discount] ? "-#{rate}" : "-#{rate}%")
      end

      # Voucher for a free shipping
      if voucher.variables[:shipping]
        @cart.free_shipping = true
      end

      # Voucher for a cart total_amount discount
      if voucher.variables[:cart_discount]
        rate = voucher.variables[:cart_discount].to_f
        discount_price = voucher.variables[:fixed_discount] ? rate : (@cart.total({:cart_voucher_discount => false, :product_voucher_discount => false}) * rate).to_f / 100
        p "discount_price : #{discount_price}"
        @cart.voucher_discount_price = discount_price
        @cart.voucher_discount = (voucher.variables[:fixed_discount] ? "-#{rate}" :  "-#{rate}%")
      end
    end
  end
end