From a9c4c8fc310dc743c4481ed6358b93cf88d4308b Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Mon, 9 May 2016 17:34:47 +0530 Subject: [PATCH 1/2] In Purchase Return entry, make sle and gle as per actual inward entry's valuation rate --- .../controllers/sales_and_purchase_return.py | 4 +-- .../purchase_receipt/purchase_receipt.py | 33 ++++++++++--------- erpnext/stock/stock_ledger.py | 2 -- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/erpnext/controllers/sales_and_purchase_return.py b/erpnext/controllers/sales_and_purchase_return.py index e76cf98789d..a48d7b7b8be 100644 --- a/erpnext/controllers/sales_and_purchase_return.py +++ b/erpnext/controllers/sales_and_purchase_return.py @@ -62,8 +62,8 @@ def validate_returned_items(doc): if doc.doctype in ("Delivery Note", "Sales Invoice"): - for d in frappe.db.sql("""select item_code, sum(qty) as qty, serial_no, batch_no from `tabPacked Item` - where parent = %s group by item_code""".format(doc.doctype), doc.return_against, as_dict=1): + for d in frappe.db.sql("""select item_code, qty, serial_no, batch_no from `tabPacked Item` + where parent = %s""".format(doc.doctype), doc.return_against, as_dict=1): valid_items = get_ref_item_dict(valid_items, d) already_returned_items = get_already_returned_items(doc) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 8ee6de9d0e1..10c845205a4 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -139,19 +139,24 @@ class PurchaseReceipt(BuyingController): pr_qty = flt(d.qty) * flt(d.conversion_factor) if pr_qty: - val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9 - rate = flt(d.valuation_rate, val_rate_db_precision) + sle = self.get_sl_entries(d, { "actual_qty": flt(pr_qty), "serial_no": cstr(d.serial_no).strip() }) if self.is_return: + original_incoming_rate = frappe.db.get_value("Stock Ledger Entry", + {"voucher_type": "Purchase Receipt", "voucher_no": self.return_against, + "item_code": d.item_code}, "incoming_rate") + sle.update({ - "outgoing_rate": rate + "outgoing_rate": original_incoming_rate }) else: + val_rate_db_precision = 6 if cint(self.precision("valuation_rate", d)) <= 6 else 9 + incoming_rate = flt(d.valuation_rate, val_rate_db_precision) sle.update({ - "incoming_rate": rate + "incoming_rate": incoming_rate }) sl_entries.append(sle) @@ -302,12 +307,9 @@ class PurchaseReceipt(BuyingController): for d in self.get("items"): if d.item_code in stock_items and flt(d.valuation_rate) and flt(d.qty): if warehouse_account.get(d.warehouse): - - val_rate_db_precision = 6 if cint(d.precision("valuation_rate")) <= 6 else 9 - - # warehouse account - stock_value_diff = flt(flt(d.valuation_rate, val_rate_db_precision) * flt(d.qty) - * flt(d.conversion_factor), d.precision("base_net_amount")) + 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") gl_entries.append(self.get_gl_dict({ "account": warehouse_account[d.warehouse]["name"], @@ -352,16 +354,15 @@ class PurchaseReceipt(BuyingController): }, warehouse_account[self.supplier_warehouse]["account_currency"])) # divisional loss adjustment - sle_valuation_amount = flt(flt(d.valuation_rate, val_rate_db_precision) * flt(d.qty) * flt(d.conversion_factor), - self.precision("base_net_amount", d)) - - distributed_amount = flt(flt(d.base_net_amount, self.precision("base_net_amount", d))) + \ + valuation_amount_as_per_doc = flt(d.base_net_amount, d.precision("base_net_amount")) + \ flt(d.landed_cost_voucher_amount) + flt(d.rm_supp_cost) + flt(d.item_tax_amount) - divisional_loss = flt(distributed_amount - sle_valuation_amount, self.precision("base_net_amount", d)) + divisional_loss = flt(valuation_amount_as_per_doc - stock_value_diff, + d.precision("base_net_amount")) + if divisional_loss: gl_entries.append(self.get_gl_dict({ - "account": stock_rbnb, + "account": expenses_included_in_valuation, "against": warehouse_account[d.warehouse]["name"], "cost_center": d.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"), diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index ed6732227a9..d3fa4823cd1 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -215,8 +215,6 @@ class update_entries_after(object): stock_value_change = 0 if incoming_rate: stock_value_change = actual_qty * incoming_rate - elif flt(sle.outgoing_rate): - stock_value_change = actual_qty * flt(sle.outgoing_rate) elif actual_qty < 0: # In case of delivery/stock issue, get average purchase rate # of serial nos of current entry From 199735036498b7efd288c17c3d527b60752f0910 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 11 May 2016 12:06:29 +0530 Subject: [PATCH 2/2] [fix] Divisional loss in purchase receipt --- erpnext/stock/doctype/purchase_receipt/purchase_receipt.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py index 10c845205a4..0ce3cd1aa8a 100644 --- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py @@ -361,8 +361,13 @@ class PurchaseReceipt(BuyingController): d.precision("base_net_amount")) if divisional_loss: + if self.is_return or flt(d.item_tax_amount): + loss_account = expenses_included_in_valuation + else: + loss_account = stock_rbnb + gl_entries.append(self.get_gl_dict({ - "account": expenses_included_in_valuation, + "account": loss_account, "against": warehouse_account[d.warehouse]["name"], "cost_center": d.cost_center, "remarks": self.get("remarks") or _("Accounting Entry for Stock"),