From 8142cd2865891ac56759950e2f7aa31b112b5ce9 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 5 Aug 2015 18:57:26 +0530 Subject: [PATCH] [fix] fifo in stock ledger --- .../stock/doctype/stock_entry/stock_entry.js | 5 ++-- erpnext/stock/stock_ledger.py | 29 +++++++++---------- erpnext/stock/utils.py | 27 +++++++++-------- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.js b/erpnext/stock/doctype/stock_entry/stock_entry.js index d5bdde72e5..04ef935732 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.js +++ b/erpnext/stock/doctype/stock_entry/stock_entry.js @@ -133,11 +133,12 @@ erpnext.stock.StockEntry = erpnext.stock.StockController.extend({ var me = this; this.toggle_enable_bom(); - return this.frm.call({ - method: "get_production_order_details", + return frappe.call({ + method: "erpnext.stock.doctype.stock_entry.stock_entry.get_production_order_details", args: {production_order: this.frm.doc.production_order}, callback: function(r) { if (!r.exc) { + me.frm.set_value(r.message); if (me.frm.doc.purpose == "Material Transfer for Manufacture" && !me.frm.doc.to_warehouse) me.frm.set_value("to_warehouse", r.message["wip_warehouse"]); me.frm.set_value("from_bom", 1); diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index c92663f8c1..c0d5cd02c4 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -300,22 +300,21 @@ class update_entries_after(object): # select first batch or the batch with same rate 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]: + # 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 - else: - # qty found in current batch - # consume it and exit - batch[0] = batch[0] - qty_to_pop - qty_to_pop = 0 + 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_qty = sum((flt(batch[0]) for batch in self.stock_queue)) diff --git a/erpnext/stock/utils.py b/erpnext/stock/utils.py index 4f1e427f14..ee4303b641 100644 --- a/erpnext/stock/utils.py +++ b/erpnext/stock/utils.py @@ -133,20 +133,19 @@ def get_fifo_rate(previous_stock_queue, qty): qty_to_pop = abs(qty) while qty_to_pop and previous_stock_queue: batch = previous_stock_queue[0] - if batch[0]: - if 0 < batch[0] <= qty_to_pop: - # if batch qty > 0 - # not enough or exactly same qty in current batch, clear batch - available_qty_for_outgoing += flt(batch[0]) - outgoing_cost += flt(batch[0]) * flt(batch[1]) - qty_to_pop -= batch[0] - previous_stock_queue.pop(0) - else: - # all from current batch - available_qty_for_outgoing += flt(qty_to_pop) - outgoing_cost += flt(qty_to_pop) * flt(batch[1]) - batch[0] -= qty_to_pop - qty_to_pop = 0 + if 0 < batch[0] <= qty_to_pop: + # if batch qty > 0 + # not enough or exactly same qty in current batch, clear batch + available_qty_for_outgoing += flt(batch[0]) + outgoing_cost += flt(batch[0]) * flt(batch[1]) + qty_to_pop -= batch[0] + previous_stock_queue.pop(0) + else: + # all from current batch + available_qty_for_outgoing += flt(qty_to_pop) + outgoing_cost += flt(qty_to_pop) * flt(batch[1]) + batch[0] -= qty_to_pop + qty_to_pop = 0 return outgoing_cost / available_qty_for_outgoing