Skip to content

Commit

Permalink
Merge pull request #40187 from GursheenK/valuation-tax-gle-via-lcv
Browse files Browse the repository at this point in the history
fix: valuation tax entries on LCV after billing PR
  • Loading branch information
ruthra-kumar authored Jun 15, 2024
2 parents 99695df + 58c7922 commit 7479459
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 25 deletions.
8 changes: 6 additions & 2 deletions erpnext/controllers/stock_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def validate_duplicate_serial_and_batch_bundle(self, table_name):
)
)

def make_gl_entries(self, gl_entries=None, from_repost=False):
def make_gl_entries(self, gl_entries=None, from_repost=False, via_landed_cost_voucher=False):
if self.docstatus == 2:
make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name)

Expand All @@ -118,7 +118,11 @@ def make_gl_entries(self, gl_entries=None, from_repost=False):

if self.docstatus == 1:
if not gl_entries:
gl_entries = self.get_gl_entries(warehouse_account)
gl_entries = (
self.get_gl_entries(warehouse_account, via_landed_cost_voucher)
if self.doctype == "Purchase Receipt"
else self.get_gl_entries(warehouse_account)
)
make_gl_entries(gl_entries, from_repost=from_repost)

def validate_serialized_batch(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ def update_landed_cost(self):
doc.docstatus = 1
doc.make_bundle_using_old_serial_batch_fields(via_landed_cost_voucher=True)
doc.update_stock_ledger(allow_negative_stock=True, via_landed_cost_voucher=True)
doc.make_gl_entries()
if d.receipt_document_type == "Purchase Receipt":
doc.make_gl_entries(via_landed_cost_voucher=True)
else:
doc.make_gl_entries()
doc.repost_future_sle_and_gle(via_landed_cost_voucher=True)

def validate_asset_qty_and_status(self, receipt_document_type, receipt_document):
Expand Down
27 changes: 13 additions & 14 deletions erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,13 @@ def on_cancel(self):
self.delete_auto_created_batches()
self.set_consumed_qty_in_subcontract_order()

def get_gl_entries(self, warehouse_account=None):
def get_gl_entries(self, warehouse_account=None, via_landed_cost_voucher=False):
from erpnext.accounts.general_ledger import process_gl_map

gl_entries = []

self.make_item_gl_entries(gl_entries, warehouse_account=warehouse_account)
self.make_tax_gl_entries(gl_entries)
self.make_tax_gl_entries(gl_entries, via_landed_cost_voucher)
update_regional_gl_entries(gl_entries, self)

return process_gl_map(gl_entries)
Expand Down Expand Up @@ -767,7 +767,7 @@ def add_provisional_gl_entry(
posting_date=posting_date,
)

def make_tax_gl_entries(self, gl_entries):
def make_tax_gl_entries(self, gl_entries, via_landed_cost_voucher=False):
negative_expense_to_be_booked = sum([flt(d.item_tax_amount) for d in self.get("items")])
is_asset_pr = any(d.is_fixed_asset for d in self.get("items"))
# Cost center-wise amount breakup for other charges included for valuation
Expand Down Expand Up @@ -802,18 +802,17 @@ def make_tax_gl_entries(self, gl_entries):
i = 1
for tax in self.get("taxes"):
if valuation_tax.get(tax.name):
negative_expense_booked_in_pi = frappe.db.sql(
"""select name from `tabPurchase Invoice Item` pi
where docstatus = 1 and purchase_receipt=%s
and exists(select name from `tabGL Entry` where voucher_type='Purchase Invoice'
and voucher_no=pi.parent and account=%s)""",
(self.name, tax.account_head),
)

if negative_expense_booked_in_pi:
account = stock_rbnb
else:
if via_landed_cost_voucher:
account = tax.account_head
else:
negative_expense_booked_in_pi = frappe.db.sql(
"""select name from `tabPurchase Invoice Item` pi
where docstatus = 1 and purchase_receipt=%s
and exists(select name from `tabGL Entry` where voucher_type='Purchase Invoice'
and voucher_no=pi.parent and account=%s)""",
(self.name, tax.account_head),
)
account = stock_rbnb if negative_expense_booked_in_pi else tax.account_head

if i == len(valuation_tax):
applicable_amount = amount_including_divisional_loss
Expand Down
74 changes: 66 additions & 8 deletions erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from erpnext.accounts.doctype.account.test_account import get_inventory_account
from erpnext.controllers.accounts_controller import InvalidQtyError
from erpnext.controllers.buying_controller import QtyMismatchError
from erpnext.stock import get_warehouse_account_map
from erpnext.stock.doctype.item.test_item import create_item, make_item
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
from erpnext.stock.doctype.serial_and_batch_bundle.serial_and_batch_bundle import (
Expand Down Expand Up @@ -1720,7 +1721,6 @@ def test_validate_received_qty_for_internal_pr(self):
frappe.db.set_single_value("Stock Settings", "over_delivery_receipt_allowance", 0)

def test_internal_pr_gl_entries(self):
from erpnext.stock import get_warehouse_account_map
from erpnext.stock.doctype.delivery_note.delivery_note import make_inter_company_purchase_receipt
from erpnext.stock.doctype.delivery_note.test_delivery_note import create_delivery_note
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
Expand Down Expand Up @@ -2466,6 +2466,54 @@ def test_pr_billed_amount_against_return_entry(self):
pr.reload()
self.assertEqual(pr.per_billed, 100)

def test_valuation_taxes_lcv_repost_after_billing(self):
from erpnext.stock.doctype.landed_cost_voucher.test_landed_cost_voucher import (
make_landed_cost_voucher,
)

old_perpetual_inventory = erpnext.is_perpetual_inventory_enabled("_Test Company")
frappe.local.enable_perpetual_inventory["_Test Company"] = 1
frappe.db.set_value(
"Company",
"_Test Company",
"stock_received_but_not_billed",
"Stock Received But Not Billed - _TC",
)

pr = make_purchase_receipt(qty=10, rate=1000, do_not_submit=1)
pr.append(
"taxes",
{
"category": "Valuation and Total",
"charge_type": "Actual",
"account_head": "Freight and Forwarding Charges - _TC",
"tax_amount": 2000,
"description": "Test",
},
)
pr.submit()
pi = make_purchase_invoice(pr.name)
pi.submit()
make_landed_cost_voucher(
company=pr.company,
receipt_document_type="Purchase Receipt",
receipt_document=pr.name,
charges=2000,
distribute_charges_based_on="Qty",
expense_account="Expenses Included In Valuation - _TC",
)

gl_entries = get_gl_entries("Purchase Receipt", pr.name, skip_cancelled=True, as_dict=False)
warehouse_account = get_warehouse_account_map("_Test Company")
expected_gle = (
("Stock Received But Not Billed - _TC", 0, 10000, "Main - _TC"),
("Freight and Forwarding Charges - _TC", 0, 2000, "Main - _TC"),
("Expenses Included In Valuation - _TC", 0, 2000, "Main - _TC"),
(warehouse_account[pr.items[0].warehouse]["account"], 14000, 0, "Main - _TC"),
)
self.assertSequenceEqual(expected_gle, gl_entries)
frappe.local.enable_perpetual_inventory["_Test Company"] = old_perpetual_inventory

def test_purchase_receipt_with_use_serial_batch_field_for_rejected_qty(self):
batch_item = make_item(
"_Test Purchase Receipt Batch Item For Rejected Qty",
Expand Down Expand Up @@ -2999,14 +3047,24 @@ def get_sl_entries(voucher_type, voucher_no):
)


def get_gl_entries(voucher_type, voucher_no):
return frappe.db.sql(
"""select account, debit, credit, cost_center, is_cancelled
from `tabGL Entry` where voucher_type=%s and voucher_no=%s
order by account desc""",
(voucher_type, voucher_no),
as_dict=1,
def get_gl_entries(voucher_type, voucher_no, skip_cancelled=False, as_dict=True):
gl = frappe.qb.DocType("GL Entry")
gl_query = (
frappe.qb.from_(gl)
.select(
gl.account,
gl.debit,
gl.credit,
gl.cost_center,
)
.where((gl.voucher_type == voucher_type) & (gl.voucher_no == voucher_no))
.orderby(gl.account, order=frappe.qb.desc)
)
if skip_cancelled:
gl_query = gl_query.where(gl.is_cancelled == 0)
else:
gl_query = gl_query.select(gl.is_cancelled)
return gl_query.run(as_dict=as_dict)


def get_taxes(**args):
Expand Down

0 comments on commit 7479459

Please sign in to comment.