From 6dfc78b12aa148899d3f0d9220fcc78c3540b8ca Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 24 Jun 2016 12:28:55 +0530 Subject: [PATCH 1/2] Item Valuation Rate in case of negative inventory --- erpnext/patches.txt | 1 + ...t_valuation_rate_for_negative_inventory.py | 10 ++++ .../purchase_receipt/purchase_receipt.py | 3 +- erpnext/stock/stock_ledger.py | 49 +++++++++++-------- 4 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 erpnext/patches/v6_20x/repost_valuation_rate_for_negative_inventory.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 1842c5491f..e9af184709 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -259,3 +259,4 @@ erpnext.patches.v6_20x.remove_fiscal_year_from_holiday_list erpnext.patches.v6_24.map_customer_address_to_shipping_address_on_po erpnext.patches.v6_27.fix_recurring_order_status erpnext.patches.v6_20x.update_product_bundle_description +erpnext.patches.v6_20x.repost_valuation_rate_for_negative_inventory \ No newline at end of file diff --git a/erpnext/patches/v6_20x/repost_valuation_rate_for_negative_inventory.py b/erpnext/patches/v6_20x/repost_valuation_rate_for_negative_inventory.py new file mode 100644 index 0000000000..f4c27cfecc --- /dev/null +++ b/erpnext/patches/v6_20x/repost_valuation_rate_for_negative_inventory.py @@ -0,0 +1,10 @@ +# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors +# License: GNU General Public License v3. See license.txt + +from __future__ import unicode_literals +import frappe +from erpnext.stock.stock_balance import repost + +def execute(): + if frappe.db.get_value("Stock Settings", None, "allow_negative_stock"): + repost(only_actual=True) \ No newline at end of file diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 0ce3cd1aa8..aab81ae131 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -310,7 +310,8 @@ class PurchaseReceipt(BuyingController): stock_value_diff = frappe.db.get_value("Stock Ledger Entry", {"voucher_type": "Purchase Receipt", "voucher_no": self.name, "voucher_detail_no": d.name}, "stock_value_difference") - + if not stock_value_diff: + continue gl_entries.append(self.get_gl_dict({ "account": warehouse_account[d.warehouse]["name"], "against": stock_rbnb, diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index d3fa4823cd..023170812a 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -229,27 +229,35 @@ class update_entries_after(object): # calculate new valuation rate only if stock value is positive # else it remains the same as that of previous entry self.valuation_rate = new_stock_value / new_stock_qty - + def get_moving_average_values(self, sle): actual_qty = flt(sle.actual_qty) + new_stock_qty = flt(self.qty_after_transaction) + actual_qty + if new_stock_qty >= 0: + if actual_qty > 0: + if flt(self.qty_after_transaction) <= 0: + self.valuation_rate = sle.incoming_rate + else: + new_stock_value = (self.qty_after_transaction * self.valuation_rate) + \ + (actual_qty * sle.incoming_rate) - if actual_qty > 0 or flt(sle.outgoing_rate) > 0: - rate = flt(sle.incoming_rate) if actual_qty > 0 else flt(sle.outgoing_rate) + self.valuation_rate = new_stock_value / new_stock_qty - if self.qty_after_transaction < 0 and not self.valuation_rate: - # if negative stock, take current valuation rate as incoming rate - self.valuation_rate = rate + elif sle.outgoing_rate: + if new_stock_qty: + new_stock_value = (self.qty_after_transaction * self.valuation_rate) + \ + (actual_qty * sle.outgoing_rate) - new_stock_qty = abs(self.qty_after_transaction) + actual_qty - new_stock_value = (abs(self.qty_after_transaction) * self.valuation_rate) + (actual_qty * rate) + self.valuation_rate = new_stock_value / new_stock_qty + else: + self.valuation_rate = self.outgoing_rate - if new_stock_qty: - self.valuation_rate = new_stock_value / flt(new_stock_qty) + else: + if flt(self.qty_after_transaction) >= 0 and sle.outgoing_rate: + self.valuation_rate = sle.outgoing_rate - elif not self.valuation_rate and self.qty_after_transaction <= 0: - self.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, self.allow_zero_rate) - - self.valuation_rate = abs(flt(self.valuation_rate)) + if not self.valuation_rate and actual_qty > 0: + self.valuation_rate = sle.incoming_rate def get_fifo_values(self, sle): incoming_rate = flt(sle.incoming_rate) @@ -268,10 +276,7 @@ class update_entries_after(object): self.stock_queue.append([actual_qty, incoming_rate]) else: qty = self.stock_queue[-1][0] + actual_qty - if qty == 0: - self.stock_queue.pop(-1) - else: - self.stock_queue[-1] = [qty, incoming_rate] + self.stock_queue[-1] = [qty, incoming_rate] else: qty_to_pop = abs(actual_qty) while qty_to_pop: @@ -298,7 +303,7 @@ class update_entries_after(object): break else: index = 0 - + # select first batch or the batch with same rate batch = self.stock_queue[index] if qty_to_pop >= batch[0]: @@ -320,7 +325,11 @@ class update_entries_after(object): 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)) - self.valuation_rate = (stock_value / flt(stock_qty)) if stock_qty else 0 + if stock_qty: + self.valuation_rate = stock_value / flt(stock_qty) + + if not self.stock_queue: + self.stock_queue.append([0, sle.incoming_rate or sle.outgoing_rate or self.valuation_rate]) def get_sle_before_datetime(self): """get previous stock ledger entry before current time-bucket""" From 0e19e72a5dc67892fa9b465f9ca8f52b9b0d3482 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 24 Jun 2016 13:16:27 +0600 Subject: [PATCH 2/2] bumped to version 6.27.24 --- erpnext/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/__init__.py b/erpnext/__init__.py index e1f1f1376d..bb1465bced 100644 --- a/erpnext/__init__.py +++ b/erpnext/__init__.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -__version__ = '6.27.23' \ No newline at end of file +__version__ = '6.27.24' \ No newline at end of file