Skip to content

Instantly share code, notes, and snippets.

@shwoodard
Created May 22, 2009 17:44
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/116261 to your computer and use it in GitHub Desktop.
Save shwoodard/116261 to your computer and use it in GitHub Desktop.
def adjust_wac(time = Time.zone.now)
adjustment_date = time.beginning_of_day
qty = value = '0'.to_d
transaction do
# Compute WAC as of specified time.....then adjust future inventory adjustment entries
first_entry = true
(inv_adj_entries = inventory_adjustment_entries.find_all).each do |entry|
if entry.posted_date >= adjustment_date and !first_entry
# Adjust future entries
if entry.account_transaction.respond_to?(:adjust_inventory_entry_cost)
entry = entry.account_transaction.adjust_inventory_entry_cost(entry.id, qty == 0 ? 0 : (value / qty).round(2))
end
end
# Continue computing WAC, using possible updated entry
qty, value = update_wac_from_entry(entry, qty, value)
first_entry = false
end
# In addition, gather all invoices using this item that were not originally
# tracking its inventory, and add the inventory entries.
if inventory_adjustment_entries.any?
<<<<<<< .merge_file_EgkULS
txns = CustomerInvoiceTransaction.not_tracking_item(self.id).all(:conditions => ['account_transactions.posted_date >= ?', adjustment_date]).uniq
qty_needed = 0.to_d
=======
wac = (qty == 0) ? 0.to_d : (value / qty).round(2)
txns = CustomerInvoiceTransaction.not_tracking_item(self.id).all(:conditions => ['account_transactions.posted_date >= ?', adjustment_date]).uniq
qty_needed = 0.to_d
$app_logger.info "Adjusting #{txns.length} non-tracking invoice transactions for item #{self.name} on or after #{time} using WAC #{wac}"
>>>>>>> .merge_file_3FTg7H
txns.each do |txn|
wac, unused, unused = calc_wac_and_qty(inv_adj_entries, txn.posted_date)
$app_logger.info "Converting non-tracking invoice transaction #{txn.id} for item #{self.name} using WAC #{wac}"
qty_needed += CustomerInvoiceTransaction.find(txn.id).add_inventory_entries_for_item!(self.id, wac)
end
raise Item::NegativeQtyException.new("Quantity is insufficient to start tracking inventory", time, self) if qty < qty_needed
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment