Merge pull request #3779 from nabinhait/fifo_stack

[fix] Get fifo rate only qty exists in batch
This commit is contained in:
Anand Doshi 2015-08-04 12:26:42 +05:30
commit 4a0edd04c4
3 changed files with 30 additions and 28 deletions

View File

@ -187,3 +187,4 @@ execute:frappe.db.sql("update `tabLeave Type` set include_holiday=0")
erpnext.patches.v5_4.set_root_and_report_type erpnext.patches.v5_4.set_root_and_report_type
erpnext.patches.v5_4.notify_system_managers_regarding_wrong_tax_calculation erpnext.patches.v5_4.notify_system_managers_regarding_wrong_tax_calculation
erpnext.patches.v5_4.fix_invoice_outstanding erpnext.patches.v5_4.fix_invoice_outstanding
execute:frappe.db.sql("update `tabStock Ledger Entry` set stock_queue = '[]' where voucher_type = 'Stock Reconciliation' and ifnull(qty_after_transaction, 0) = 0")

View File

@ -300,22 +300,22 @@ class update_entries_after(object):
# select first batch or the batch with same rate # select first batch or the batch with same rate
batch = self.stock_queue[index] batch = self.stock_queue[index]
if batch[0]:
if qty_to_pop >= batch[0]:
# consume current batch
qty_to_pop = qty_to_pop - batch[0]
self.stock_queue.pop(index)
if not self.stock_queue and qty_to_pop:
# stock finished, qty still remains to be withdrawn
# negative stock, keep in as a negative batch
self.stock_queue.append([-qty_to_pop, outgoing_rate or batch[1]])
break
if qty_to_pop >= batch[0]: else:
# consume current batch # qty found in current batch
qty_to_pop = qty_to_pop - batch[0] # consume it and exit
self.stock_queue.pop(index) batch[0] = batch[0] - qty_to_pop
if not self.stock_queue and qty_to_pop: qty_to_pop = 0
# stock finished, qty still remains to be withdrawn
# negative stock, keep in as a negative batch
self.stock_queue.append([-qty_to_pop, outgoing_rate or batch[1]])
break
else:
# qty found in current batch
# consume it and exit
batch[0] = batch[0] - qty_to_pop
qty_to_pop = 0
stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.stock_queue)) stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.stock_queue))
stock_qty = sum((flt(batch[0]) for batch in self.stock_queue)) stock_qty = sum((flt(batch[0]) for batch in self.stock_queue))

View File

@ -133,19 +133,20 @@ def get_fifo_rate(previous_stock_queue, qty):
qty_to_pop = abs(qty) qty_to_pop = abs(qty)
while qty_to_pop and previous_stock_queue: while qty_to_pop and previous_stock_queue:
batch = previous_stock_queue[0] batch = previous_stock_queue[0]
if 0 < batch[0] <= qty_to_pop: if batch[0]:
# if batch qty > 0 if 0 < batch[0] <= qty_to_pop:
# not enough or exactly same qty in current batch, clear batch # if batch qty > 0
available_qty_for_outgoing += flt(batch[0]) # not enough or exactly same qty in current batch, clear batch
outgoing_cost += flt(batch[0]) * flt(batch[1]) available_qty_for_outgoing += flt(batch[0])
qty_to_pop -= batch[0] outgoing_cost += flt(batch[0]) * flt(batch[1])
previous_stock_queue.pop(0) qty_to_pop -= batch[0]
else: previous_stock_queue.pop(0)
# all from current batch else:
available_qty_for_outgoing += flt(qty_to_pop) # all from current batch
outgoing_cost += flt(qty_to_pop) * flt(batch[1]) available_qty_for_outgoing += flt(qty_to_pop)
batch[0] -= qty_to_pop outgoing_cost += flt(qty_to_pop) * flt(batch[1])
qty_to_pop = 0 batch[0] -= qty_to_pop
qty_to_pop = 0
return outgoing_cost / available_qty_for_outgoing return outgoing_cost / available_qty_for_outgoing