Skip to content

Commit

Permalink
fix: GL Entries for receiving non CWIP assets using Purchase Receipt (#…
Browse files Browse the repository at this point in the history
…37660)

* fix: GL Entries for receiving non CWIP assets using Purchase Receipt

* fix: rearrange functions

* chore: rearrange functions

* chore: rearrange functions

* fix: Purchase Invoice GL entires for assets

* test: cwip accounting unit tests

* chore: Attribute error

* chore: Purchase Invoice tests

* chore: Missing asset account

* chore: Missing asset account

* chore: update tests

* fix: Internal transfer GL Entries

* test: Deprecate tests

* test: Depricate tests

* test: Depricate tests

* chore: make `Reserve Stock` checkbox visible in SO

* refactor: rename field `Auto Reserve Stock for Sales Order`

* feat: add fields to hold SO and SO Item ref in PR Item

* test: Deprecate tests

* test: Depricate tests

* test: Depricate tests

* refactor: Remove expense included in valuation accounts

* chore: Add back default in transit warehousefield

---------

Co-authored-by: s-aga-r <[email protected]>
(cherry picked from commit 80774e2)

# Conflicts:
#	erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
#	erpnext/buying/doctype/purchase_order/purchase_order.py
#	erpnext/controllers/stock_controller.py
#	erpnext/setup/doctype/company/company.json
#	erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
#	erpnext/stock/doctype/purchase_receipt_item/purchase_receipt_item.json
  • Loading branch information
deepeshgarg007 authored and mergify[bot] committed Nov 16, 2023
1 parent 57dea69 commit 4f914a6
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 296 deletions.
92 changes: 36 additions & 56 deletions erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
from erpnext.accounts.party import get_due_date, get_party_account
from erpnext.accounts.utils import get_account_currency, get_fiscal_year
from erpnext.assets.doctype.asset.asset import get_asset_account, is_cwip_accounting_enabled
from erpnext.assets.doctype.asset.asset import is_cwip_accounting_enabled
from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account
from erpnext.buying.utils import check_on_hold_or_closed_status
from erpnext.controllers.accounts_controller import validate_account_head
Expand Down Expand Up @@ -281,9 +281,6 @@ def set_expense_account(self, for_validate=False):
# in case of auto inventory accounting,
# expense account is always "Stock Received But Not Billed" for a stock item
# except opening entry, drop-ship entry and fixed asset items
if item.item_code:
asset_category = frappe.get_cached_value("Item", item.item_code, "asset_category")

if (
auto_accounting_for_stock
and item.item_code in stock_items
Expand Down Expand Up @@ -350,22 +347,26 @@ def set_expense_account(self, for_validate=False):
frappe.msgprint(msg, title=_("Expense Head Changed"))

item.expense_account = stock_not_billed_account

elif item.is_fixed_asset and not is_cwip_accounting_enabled(asset_category):
elif item.is_fixed_asset and item.pr_detail:
if not asset_received_but_not_billed:
asset_received_but_not_billed = self.get_company_default("asset_received_but_not_billed")
item.expense_account = asset_received_but_not_billed
elif item.is_fixed_asset:
account_type = (
"capital_work_in_progress_account"
if is_cwip_accounting_enabled(item.asset_category)
else "fixed_asset_account"
)
asset_category_account = get_asset_category_account(
"fixed_asset_account", item=item.item_code, company=self.company
account_type, item=item.item_code, company=self.company
)
if not asset_category_account:
form_link = get_link_to_form("Asset Category", asset_category)
form_link = get_link_to_form("Asset Category", item.asset_category)
throw(
_("Please set Fixed Asset Account in {} against {}.").format(form_link, self.company),
title=_("Missing Account"),
)
item.expense_account = asset_category_account
elif item.is_fixed_asset and item.pr_detail:
if not asset_received_but_not_billed:
asset_received_but_not_billed = self.get_company_default("asset_received_but_not_billed")
item.expense_account = asset_received_but_not_billed
elif not item.expense_account and for_validate:
throw(_("Expense account is mandatory for item {0}").format(item.item_code or item.item_name))

Expand Down Expand Up @@ -584,12 +585,12 @@ def make_gl_entries(self, gl_entries=None, from_repost=False):

def get_gl_entries(self, warehouse_account=None):
self.auto_accounting_for_stock = erpnext.is_perpetual_inventory_enabled(self.company)
self.asset_received_but_not_billed = self.get_company_default("asset_received_but_not_billed")

if self.auto_accounting_for_stock:
self.stock_received_but_not_billed = self.get_company_default("stock_received_but_not_billed")
self.expenses_included_in_valuation = self.get_company_default("expenses_included_in_valuation")
else:
self.stock_received_but_not_billed = None
self.expenses_included_in_valuation = None

self.negative_expense_to_be_booked = 0.0
gl_entries = []
Expand All @@ -598,9 +599,6 @@ def get_gl_entries(self, warehouse_account=None):
self.make_item_gl_entries(gl_entries)
self.make_precision_loss_gl_entry(gl_entries)

if self.check_asset_cwip_enabled():
self.get_asset_gl_entry(gl_entries)

self.make_tax_gl_entries(gl_entries)
self.make_internal_transfer_gl_entries(gl_entries)

Expand Down Expand Up @@ -702,7 +700,11 @@ def make_item_gl_entries(self, gl_entries):
if item.item_code:
asset_category = frappe.get_cached_value("Item", item.item_code, "asset_category")

if self.update_stock and self.auto_accounting_for_stock and item.item_code in stock_items:
if (
self.update_stock
and self.auto_accounting_for_stock
and (item.item_code in stock_items or item.is_fixed_asset)
):
# warehouse account
warehouse_debit_amount = self.make_stock_adjustment_entry(
gl_entries, item, voucher_wise_stock_value, account_currency
Expand Down Expand Up @@ -817,9 +819,7 @@ def make_item_gl_entries(self, gl_entries):
)
)

elif not item.is_fixed_asset or (
item.is_fixed_asset and not is_cwip_accounting_enabled(asset_category)
):
else:
expense_account = (
item.expense_account
if (not item.enable_deferred_expense or self.is_return)
Expand Down Expand Up @@ -912,40 +912,6 @@ def make_item_gl_entries(self, gl_entries):
)
)

# If asset is bought through this document and not linked to PR
if self.update_stock and item.landed_cost_voucher_amount:
expenses_included_in_asset_valuation = self.get_company_default(
"expenses_included_in_asset_valuation"
)
# Amount added through landed-cost-voucher
gl_entries.append(
self.get_gl_dict(
{
"account": expenses_included_in_asset_valuation,
"against": expense_account,
"cost_center": item.cost_center,
"remarks": self.get("remarks") or _("Accounting Entry for Stock"),
"credit": flt(item.landed_cost_voucher_amount),
"project": item.project or self.project,
},
item=item,
)
)

gl_entries.append(
self.get_gl_dict(
{
"account": expense_account,
"against": expenses_included_in_asset_valuation,
"cost_center": item.cost_center,
"remarks": self.get("remarks") or _("Accounting Entry for Stock"),
"debit": flt(item.landed_cost_voucher_amount),
"project": item.project or self.project,
},
item=item,
)
)

# update gross amount of asset bought through this document
assets = frappe.db.get_all(
"Asset", filters={"purchase_invoice": self.name, "item_code": item.item_code}
Expand All @@ -970,11 +936,16 @@ def make_item_gl_entries(self, gl_entries):
(item.purchase_receipt, valuation_tax_accounts),
)

stock_rbnb = (
self.asset_received_but_not_billed
if item.is_fixed_asset
else self.stock_received_but_not_billed
)
if not negative_expense_booked_in_pr:
gl_entries.append(
self.get_gl_dict(
{
"account": self.stock_received_but_not_billed,
"account": stock_rbnb,
"against": self.supplier,
"debit": flt(item.item_tax_amount, item.precision("item_tax_amount")),
"remarks": self.remarks or _("Accounting Entry for Stock"),
Expand All @@ -989,6 +960,7 @@ def make_item_gl_entries(self, gl_entries):
item.item_tax_amount, item.precision("item_tax_amount")
)

<<<<<<< HEAD
def get_asset_gl_entry(self, gl_entries):
arbnb_account = None
eiiav_account = None
Expand Down Expand Up @@ -1139,6 +1111,14 @@ def get_asset_gl_entry(self, gl_entries):
frappe.db.set_value("Asset", asset.name, "purchase_receipt_amount", flt(item.valuation_rate))

return gl_entries
=======
assets = frappe.db.get_all(
"Asset", filters={"purchase_invoice": self.name, "item_code": item.item_code}
)
for asset in assets:
frappe.db.set_value("Asset", asset.name, "gross_purchase_amount", flt(item.valuation_rate))
frappe.db.set_value("Asset", asset.name, "purchase_receipt_amount", flt(item.valuation_rate))
>>>>>>> 80774e2da1 (fix: GL Entries for receiving non CWIP assets using Purchase Receipt (#37660))

def make_stock_adjustment_entry(
self, gl_entries, item, voucher_wise_stock_value, account_currency
Expand Down
6 changes: 0 additions & 6 deletions erpnext/accounts/doctype/sales_invoice/test_sales_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2498,12 +2498,6 @@ def test_inter_company_transaction_without_default_warehouse(self):
"stock_received_but_not_billed",
"Stock Received But Not Billed - _TC1",
)
frappe.db.set_value(
"Company",
"_Test Company 1",
"expenses_included_in_valuation",
"Expenses Included In Valuation - _TC1",
)

# begin test
si = create_sales_invoice(
Expand Down
2 changes: 1 addition & 1 deletion erpnext/accounts/general_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def make_gl_entries(
from_repost=from_repost,
)
save_entries(gl_map, adv_adj, update_outstanding, from_repost)
# Post GL Map proccess there may no be any GL Entries
# Post GL Map process there may no be any GL Entries
elif gl_map:
frappe.throw(
_(
Expand Down
9 changes: 5 additions & 4 deletions erpnext/assets/doctype/asset/test_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def test_purchase_of_grouped_asset(self):
def test_is_fixed_asset_set(self):
asset = create_asset(is_existing_asset=1)
doc = frappe.new_doc("Purchase Invoice")
doc.company = "_Test Company"
doc.supplier = "_Test Supplier"
doc.append("items", {"item_code": "Macbook Pro", "qty": 1, "asset": asset.name})

Expand Down Expand Up @@ -534,7 +535,7 @@ def test_expense_head(self):

self.assertEqual("Asset Received But Not Billed - _TC", doc.items[0].expense_account)

# CWIP: Capital Work In Progress
# Capital Work In Progress
def test_cwip_accounting(self):
pr = make_purchase_receipt(
item_code="Macbook Pro", qty=1, rate=5000, do_not_submit=True, location="Test Location"
Expand Down Expand Up @@ -567,7 +568,8 @@ def test_cwip_accounting(self):
pr.submit()

expected_gle = (
("Asset Received But Not Billed - _TC", 0.0, 5250.0),
("_Test Account Shipping Charges - _TC", 0.0, 250.0),
("Asset Received But Not Billed - _TC", 0.0, 5000.0),
("CWIP Account - _TC", 5250.0, 0.0),
)

Expand All @@ -586,9 +588,8 @@ def test_cwip_accounting(self):
expected_gle = (
("_Test Account Service Tax - _TC", 250.0, 0.0),
("_Test Account Shipping Charges - _TC", 250.0, 0.0),
("Asset Received But Not Billed - _TC", 5250.0, 0.0),
("Asset Received But Not Billed - _TC", 5000.0, 0.0),
("Creditors - _TC", 0.0, 5500.0),
("Expenses Included In Asset Valuation - _TC", 0.0, 250.0),
)

pi_gle = frappe.db.sql(
Expand Down
3 changes: 3 additions & 0 deletions erpnext/buying/doctype/purchase_order/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,10 @@ def update_item(obj, target, source_parent):
"material_request_item": "material_request_item",
"sales_order": "sales_order",
"sales_order_item": "sales_order_item",
<<<<<<< HEAD
"wip_composite_asset": "wip_composite_asset",
=======
>>>>>>> 80774e2da1 (fix: GL Entries for receiving non CWIP assets using Purchase Receipt (#37660))
},
"postprocess": update_item,
"condition": lambda doc: abs(doc.received_qty) < abs(doc.qty)
Expand Down
6 changes: 6 additions & 0 deletions erpnext/controllers/stock_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ def make_gl_entries(self, gl_entries=None, from_repost=False):
)
)

is_asset_pr = any(d.get("is_fixed_asset") for d in self.get("items"))

if (
cint(erpnext.is_perpetual_inventory_enabled(self.company))
or provisional_accounting_for_non_stock_items
or is_asset_pr
):
warehouse_account = get_warehouse_account_map(self.company)

Expand All @@ -73,12 +76,15 @@ def make_gl_entries(self, gl_entries=None, from_repost=False):
gl_entries = self.get_gl_entries(warehouse_account)
make_gl_entries(gl_entries, from_repost=from_repost)

<<<<<<< HEAD
elif self.doctype in ["Purchase Receipt", "Purchase Invoice"] and self.docstatus == 1:
gl_entries = []
gl_entries = self.get_asset_gl_entry(gl_entries)
update_regional_gl_entries(gl_entries, self)
make_gl_entries(gl_entries, from_repost=from_repost)

=======
>>>>>>> 80774e2da1 (fix: GL Entries for receiving non CWIP assets using Purchase Receipt (#37660))
def validate_serialized_batch(self):
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos

Expand Down
8 changes: 4 additions & 4 deletions erpnext/manufacturing/doctype/bom/bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,12 +1196,12 @@ def get_children(parent=None, is_root=False, **filters):
def add_additional_cost(stock_entry, work_order):
# Add non stock items cost in the additional cost
stock_entry.additional_costs = []
expenses_included_in_valuation = frappe.get_cached_value(
"Company", work_order.company, "expenses_included_in_valuation"
default_expense_account = frappe.get_cached_value(
"Company", work_order.company, "default_expense_account"
)

add_non_stock_items_cost(stock_entry, work_order, expenses_included_in_valuation)
add_operations_cost(stock_entry, work_order, expenses_included_in_valuation)
add_non_stock_items_cost(stock_entry, work_order, default_expense_account)
add_operations_cost(stock_entry, work_order, default_expense_account)


def add_non_stock_items_cost(stock_entry, work_order, expense_account):
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions erpnext/setup/doctype/company/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ erpnext.company.setup_queries = function(frm) {
["cost_center", {}],
["round_off_cost_center", {}],
["depreciation_cost_center", {}],
["expenses_included_in_asset_valuation", {"account_type": "Expenses Included In Asset Valuation"}],
["capital_work_in_progress_account", {"account_type": "Capital Work in Progress"}],
["asset_received_but_not_billed", {"account_type": "Asset Received But Not Billed"}],
["unrealized_profit_loss_account", {"root_type": ["in", ["Liability", "Asset"]]}],
Expand All @@ -236,8 +235,6 @@ erpnext.company.setup_queries = function(frm) {
$.each([
["stock_adjustment_account",
{"root_type": "Expense", "account_type": "Stock Adjustment"}],
["expenses_included_in_valuation",
{"root_type": "Expense", "account_type": "Expenses Included in Valuation"}],
["stock_received_but_not_billed",
{"root_type": "Liability", "account_type": "Stock Received But Not Billed"}],
["service_received_but_not_billed",
Expand Down
Loading

0 comments on commit 4f914a6

Please sign in to comment.