Skip to content
Browse files

keep track of upgrade pricing for products

  • Loading branch information...
1 parent 8a8db6c commit 66eb4e168f1804da2d251990542c596deff43c25 @pkqk pkqk committed Aug 12, 2008
View
5 app/models/cart.rb
@@ -2,13 +2,14 @@ class Cart
attr_reader :items
attr_accessor :id
attr_accessor :gst_charged
+ attr_accessor
def initialize(options = {:gst_charged => true})
@items = []
options.each_pair {|k, v| self.send(:"#{k}=", v) }
end
- def add_product_or_increase_quantity(product, quantity)
+ def add_product_or_increase_quantity(product, quantity, upgrade = false)
if product.is_a?(Coupon)
matching_product = cart_item_for_product( product.product )
if coupon || (quantity > 1)
@@ -25,7 +26,7 @@ def add_product_or_increase_quantity(product, quantity)
if current_item
current_item.quantity += quantity
else
- current_item = CartItem.new(product, quantity)
+ current_item = CartItem.new(product, quantity, upgrade)
@items << current_item
end
end
View
9 app/models/cart_item.rb
@@ -2,9 +2,10 @@ class CartItem
attr_reader :product
attr_accessor :quantity
- def initialize(product, quantity)
+ def initialize(product, quantity, upgrade = false)
@product = product
@quantity = quantity
+ @upgrade = upgrade
end
def code
@@ -13,7 +14,7 @@ def code
def subtotal(gst_charged = false)
subtotal = 0
- if price = @product.price_for_quantity(@quantity)
+ if price
subtotal = price * @quantity
subtotal -= @coupon.discount_per_order if coupon?
if gst_charged
@@ -32,6 +33,10 @@ def apply_coupon(coupon)
end
private
+
+ def price
+ @upgrade ? @product.upgrade_price : @product.price_for_quantity(@quantity)
+ end
def round_to_cents(amount)
(amount * 100.0).round.to_f / 100.0
View
9 app/models/product.rb
@@ -8,15 +8,20 @@ class Product < ActiveRecord::Base
has_many :product_prices, :dependent => :destroy
has_many :coupons, :dependent => :destroy
-
+
def product_price_for_quantity(quantity)
- self.product_prices.find(:first, :conditions => ['min_quantity <= ?', quantity], :order => 'min_quantity desc')
+ self.product_prices.find(:first, :conditions => ['min_quantity <= ? && upgrade is null', quantity], :order => 'min_quantity desc')
end
def first_product_price
self.product_prices.find(:first, :order => 'min_quantity')
end
+ def upgrade_price
+ pp = self.product_prices.find(:first, :conditions => { :upgrade => true })
+ pp && pp.price.to_f
+ end
+
def price_for_quantity(quantity)
pp = product_price_for_quantity(quantity)
pp && pp.price.to_f
View
6 app/models/product_price.rb
@@ -3,9 +3,13 @@ class ProductPrice < ActiveRecord::Base
belongs_to :product
validates_uniqueness_of :min_quantity, :scope => :product_id
-
+
def validate
errors.add(:price, "should be a positive value") if price.nil? || price < 0.01
end
+
+ def description
+ upgrade ? 'upgrade' : min_quantity
+ end
end
View
2 app/views/admin/product/index.rhtml
@@ -27,7 +27,7 @@
<tbody>
<% for product_price in product.product_prices -%>
<tr>
- <td class="min_quantity"><%= link_to h(product_price.min_quantity), product_price_edit_path(:id => product_price) -%></td>
+ <td class="min_quantity"><%= link_to h(product_price.description), product_price_edit_path(:id => product_price) -%></td>
<td class="price"><%= link_to h(product_price.price), product_price_edit_path(:id => product_price) -%></td>
</tr>
<% end -%>
View
7 app/views/admin/product_price/edit.rhtml
@@ -13,13 +13,18 @@
<tr>
<td class="label"><label for="product_price_min_quantity">Minimum Quantity</label></td>
<td class="field"><%= f.text_field :min_quantity, :size => 100, :class => 'textbox' %></td>
- <td class="help">Required. Only numbers allowed.</td>
+ <td class="help">Required. Only numbers allowed. (not required for upgrade price)</td>
</tr>
<tr>
<td class="label"><label for="product_price_price">Price</label></td>
<td class="field"><%= f.text_field :price, :size => 100, :class => 'textbox' %></td>
<td class="help">Required. Price at or above Minimum Quantity in dollars and cents.</td>
</tr>
+ <tr>
+ <td class="label"><label for="product_price_upgrade" >Upgrade?</label></td>
+ <td class="field" style="text-align:left;"><%= f.check_box :upgrade %></td>
+ <td class="help">Is this price only available as an upgrade with a valid previous license key?</td>
+ </tr>
</tbody>
</table>
<p class="buttons">
View
9 db/migrate/005_add_product_price_upgrade.rb
@@ -0,0 +1,9 @@
+class AddProductPriceUpgrade < ActiveRecord::Migration
+ def self.up
+ add_column :product_prices, :upgrade, :boolean
+ end
+
+ def self.down
+ remove_column :product_prices, :upgrade
+ end
+end

0 comments on commit 66eb4e1

Please sign in to comment.
Something went wrong with that request. Please try again.