Skip to content

Instantly share code, notes, and snippets.

@shwoodard
Created May 8, 2009 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shwoodard/108905 to your computer and use it in GitHub Desktop.
Save shwoodard/108905 to your computer and use it in GitHub Desktop.
# InventoryAdjustmentTransaction
def validate
return unless @item_id || !inventory_adjustment_entry.item
item = (self.inventory_adjustment_entry && self.inventory_adjustment_entry.item) || Item.find(@item_id)
if item.date_qty_falls_below_zero(inventory_adjustment_entry)
errors.add('item_qty', 'Cannot cause inventory qty to fall below zero.')
end
end
#Item
def date_qty_falls_below_zero(modified_entry, time = :forever)
time = time.end_of_day if time.is_a?(Time)
# Collect the item's inventory action entries and include the modified entry.
entries = (time != :forever) ? inventory_adjustment_entries.find_before(time) : inventory_adjustment_entries.find_all
current_qty = entries.inject('0'.to_d) do |qty, entry|
if entry.debit_credit_flag == 'Debit'
qty += entry.item_qty
else
qty -= entry.tem_qty
end
end
return modified_entry.debit_credit_flag == 'Debit' ? (current_qty + modified_entry.item_qty < 0) : (current_qty - modified_entry.item_qty < 0)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment