1 # Promotions allow discounts to be applied to orders, and
2 # have the ability to add items to an order as well.
7 include DataMapper::Persistable
11 'Percent of total order' => 1,
12 'Buy [n] get 1 free' => 2
19 property :code, :string, :length => 15, :default => "", :nullable => false
20 property :discount_type, :integer, :default => 0, :nullable => false
21 property :discount_amount, :float, :default => 0.0, :nullable => false
22 property :start, :datetime, :nullable => false
23 property :end, :datetime, :nullable => false
24 property :minimum_cart_value, :float
25 property :description, :string, :default => "", :nullable => false
28 #validates_presence_of :code, :discount_amount, :discount_type, :description
29 validates_uniqueness_of :code
30 validates_numericality_of :discount_amount
32 before_save :clean_code
35 self.code.gsub!(' ', '')
38 # Makes sure if 'buy n get one free' discount type that
39 # a product is selected.
41 if self.discount_type == 2 && self.item_id.nil?
42 errors.add(:item_id, "Please add an item for the 'Buy [n] get 1 free' promotion")
46 # Generates a 15 character alphanumeric code
47 # that we use to track affiliates and promotions.
49 def self.generate_code(size=15)
50 # characters used to generate affiliate code
51 chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
52 # create a new record object to satisfy while loop initially
53 record = Promotion.new
54 # loop through, creating random affiliate codes until
55 # we create one that doesn't exist in the db
60 pos = rand(chars.length)
61 test_code += chars[pos..pos]
63 # find any affiliates with this same code string
64 # if none are found the while loop exits
65 record = first(:code => test_code)
67 # return our random code
71 # Lets us know if any promotions are active.
76 :conditions => "NOW() BETWEEN start AND end"
81 Time.now.between?(self.start, self.end)
84 # Setter for product name. Uses suggestion JS and pop-up.
86 # We only really use the first item, which is the ID to look
88 def product_name=(name)
89 item = StoreItem.find_by_code(name.split(':')[0])
93 self.item_id = item.id