Merge pull request #41912 from frappe/mergify/bp/version-14-hotfix/pr-40187
fix: valuation tax entries on LCV after billing PR (backport #40187)
This commit is contained in:
@@ -51,7 +51,7 @@ class StockController(AccountsController):
|
|||||||
self.validate_internal_transfer()
|
self.validate_internal_transfer()
|
||||||
self.validate_putaway_capacity()
|
self.validate_putaway_capacity()
|
||||||
|
|
||||||
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:
|
if self.docstatus == 2:
|
||||||
make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
|
make_reverse_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
|
||||||
|
|
||||||
@@ -72,7 +72,11 @@ class StockController(AccountsController):
|
|||||||
|
|
||||||
if self.docstatus == 1:
|
if self.docstatus == 1:
|
||||||
if not gl_entries:
|
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)
|
make_gl_entries(gl_entries, from_repost=from_repost)
|
||||||
|
|
||||||
def validate_serialized_batch(self):
|
def validate_serialized_batch(self):
|
||||||
|
|||||||
@@ -224,7 +224,10 @@ class LandedCostVoucher(Document):
|
|||||||
# update stock & gl entries for submit state of PR
|
# update stock & gl entries for submit state of PR
|
||||||
doc.docstatus = 1
|
doc.docstatus = 1
|
||||||
doc.update_stock_ledger(allow_negative_stock=True, 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()
|
doc.repost_future_sle_and_gle()
|
||||||
|
|
||||||
def validate_asset_qty_and_status(self, receipt_document_type, receipt_document):
|
def validate_asset_qty_and_status(self, receipt_document_type, receipt_document):
|
||||||
|
|||||||
@@ -307,13 +307,13 @@ class PurchaseReceipt(BuyingController):
|
|||||||
self.delete_auto_created_batches()
|
self.delete_auto_created_batches()
|
||||||
self.set_consumed_qty_in_subcontract_order()
|
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
|
from erpnext.accounts.general_ledger import process_gl_map
|
||||||
|
|
||||||
gl_entries = []
|
gl_entries = []
|
||||||
|
|
||||||
self.make_item_gl_entries(gl_entries, warehouse_account=warehouse_account)
|
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)
|
update_regional_gl_entries(gl_entries, self)
|
||||||
|
|
||||||
return process_gl_map(gl_entries)
|
return process_gl_map(gl_entries)
|
||||||
@@ -661,7 +661,7 @@ class PurchaseReceipt(BuyingController):
|
|||||||
posting_date=posting_date,
|
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")])
|
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"))
|
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
|
# Cost center-wise amount breakup for other charges included for valuation
|
||||||
@@ -696,18 +696,17 @@ class PurchaseReceipt(BuyingController):
|
|||||||
i = 1
|
i = 1
|
||||||
for tax in self.get("taxes"):
|
for tax in self.get("taxes"):
|
||||||
if valuation_tax.get(tax.name):
|
if valuation_tax.get(tax.name):
|
||||||
negative_expense_booked_in_pi = frappe.db.sql(
|
if via_landed_cost_voucher:
|
||||||
"""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:
|
|
||||||
account = tax.account_head
|
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):
|
if i == len(valuation_tax):
|
||||||
applicable_amount = amount_including_divisional_loss
|
applicable_amount = amount_including_divisional_loss
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ from pypika import functions as fn
|
|||||||
import erpnext
|
import erpnext
|
||||||
from erpnext.accounts.doctype.account.test_account import get_inventory_account
|
from erpnext.accounts.doctype.account.test_account import get_inventory_account
|
||||||
from erpnext.controllers.buying_controller import QtyMismatchError
|
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.item.test_item import create_item, make_item
|
||||||
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
|
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import make_purchase_invoice
|
||||||
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
|
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
|
||||||
@@ -1640,7 +1641,6 @@ class TestPurchaseReceipt(FrappeTestCase):
|
|||||||
frappe.db.set_single_value("Stock Settings", "over_delivery_receipt_allowance", 0)
|
frappe.db.set_single_value("Stock Settings", "over_delivery_receipt_allowance", 0)
|
||||||
|
|
||||||
def test_internal_pr_gl_entries(self):
|
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.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.delivery_note.test_delivery_note import create_delivery_note
|
||||||
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
|
from erpnext.stock.doctype.stock_entry.test_stock_entry import make_stock_entry
|
||||||
@@ -2301,6 +2301,54 @@ class TestPurchaseReceipt(FrappeTestCase):
|
|||||||
for index, d in enumerate(data):
|
for index, d in enumerate(data):
|
||||||
self.assertEqual(d.qty_after_transaction, 11 + index)
|
self.assertEqual(d.qty_after_transaction, 11 + index)
|
||||||
|
|
||||||
|
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 prepare_data_for_internal_transfer():
|
def prepare_data_for_internal_transfer():
|
||||||
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_internal_supplier
|
from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_internal_supplier
|
||||||
@@ -2347,14 +2395,24 @@ def get_sl_entries(voucher_type, voucher_no):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_gl_entries(voucher_type, voucher_no):
|
def get_gl_entries(voucher_type, voucher_no, skip_cancelled=False, as_dict=True):
|
||||||
return frappe.db.sql(
|
gl = frappe.qb.DocType("GL Entry")
|
||||||
"""select account, debit, credit, cost_center, is_cancelled
|
gl_query = (
|
||||||
from `tabGL Entry` where voucher_type=%s and voucher_no=%s
|
frappe.qb.from_(gl)
|
||||||
order by account desc""",
|
.select(
|
||||||
(voucher_type, voucher_no),
|
gl.account,
|
||||||
as_dict=1,
|
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):
|
def get_taxes(**args):
|
||||||
|
|||||||
Reference in New Issue
Block a user