Permalink
Browse files

add ability to support minimum quantity increments

  • Loading branch information...
1 parent 2df3179 commit d7604ec3ab4462959f532d1326dd8b72491bea64 @rounders committed Nov 30, 2011
View
@@ -2,10 +2,25 @@
validate :must_meet_minimum
private
+
def must_meet_minimum
- minimum = variant.is_master? ? variant.product.minimum_quantity : variant.minimum_quantity
- minimum ||= 0
- errors.add(:quantity, "does not meet minimum quantity required") if quantity < minimum && quantity != 0
+ if variant.is_master?
+ minimum_qty = variant.product.minimum_quantity
+ minimum_qty_increment = variant.product.minimum_quantity_increment
+ else
+ minimum_qty = variant.minimum_quantity
+ minimum_qty_increment = variant.minimum_quantity_increment
+ end
+
+ minimum_qty ||= 0
+ minimum_qty_increment ||= 0
+
+ errors.add(:quantity, "#{variant.product.name} has a minimum order quantity of #{minimum_qty}") if quantity < minimum_qty && quantity != 0
+
+ if minimum_qty_increment != 0 && (quantity - minimum_qty)%minimum_qty_increment != 0 && quantity > minimum_qty
+ errors.add(:quantity, "#{variant.product.name} can only be ordered in increments of #{minimum_qty_increment}")
+ end
+
end
@@ -0,0 +1,11 @@
+class AddMinimumQuantityIncrementFields < ActiveRecord::Migration
+ def self.up
+ add_column :variants, :minimum_quantity_increment, :integer
+ add_column :products, :minimum_quantity_increment, :integer
+ end
+
+ def self.down
+ remove_column :products, :minimum_quantity_increment
+ remove_column :variants, :minimum_quantity_increment
+ end
+end

0 comments on commit d7604ec

Please sign in to comment.