Created
May 8, 2009 18:02
-
-
Save shwoodard/108905 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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