fix: handle make_gl_entry in case of cwip enable after puchasing (#21529)
* fix: handle make_gl_entry in case of cwip enable after puchasing * fix: invalid variable assignment * fix: make gl entries if cwip has been booked even if cwip is disabled * add tests * fix: conditions Co-authored-by: Nabin Hait <nabinhait@gmail.com>
This commit is contained in:
parent
fff00cee3a
commit
d76f167e96
@ -32,7 +32,7 @@ class Asset(AccountsController):
|
|||||||
self.validate_in_use_date()
|
self.validate_in_use_date()
|
||||||
self.set_status()
|
self.set_status()
|
||||||
self.make_asset_movement()
|
self.make_asset_movement()
|
||||||
if not self.booked_fixed_asset and is_cwip_accounting_enabled(self.asset_category):
|
if not self.booked_fixed_asset and self.validate_make_gl_entry():
|
||||||
self.make_gl_entries()
|
self.make_gl_entries()
|
||||||
|
|
||||||
def before_cancel(self):
|
def before_cancel(self):
|
||||||
@ -456,17 +456,54 @@ class Asset(AccountsController):
|
|||||||
if d.finance_book == self.default_finance_book:
|
if d.finance_book == self.default_finance_book:
|
||||||
return cint(d.idx) - 1
|
return cint(d.idx) - 1
|
||||||
|
|
||||||
|
def validate_make_gl_entry(self):
|
||||||
|
purchase_document = self.get_purchase_document()
|
||||||
|
asset_bought_with_invoice = purchase_document == self.purchase_invoice
|
||||||
|
fixed_asset_account, cwip_account = self.get_asset_accounts()
|
||||||
|
cwip_enabled = is_cwip_accounting_enabled(self.asset_category)
|
||||||
|
# check if expense already has been booked in case of cwip was enabled after purchasing asset
|
||||||
|
expense_booked = False
|
||||||
|
cwip_booked = False
|
||||||
|
|
||||||
|
if asset_bought_with_invoice:
|
||||||
|
expense_booked = frappe.db.sql("""SELECT name FROM `tabGL Entry` WHERE voucher_no = %s and account = %s""",
|
||||||
|
(purchase_document, fixed_asset_account), as_dict=1)
|
||||||
|
else:
|
||||||
|
cwip_booked = frappe.db.sql("""SELECT name FROM `tabGL Entry` WHERE voucher_no = %s and account = %s""",
|
||||||
|
(purchase_document, cwip_account), as_dict=1)
|
||||||
|
|
||||||
|
if cwip_enabled and (expense_booked or not cwip_booked):
|
||||||
|
# if expense has already booked from invoice or cwip is booked from receipt
|
||||||
|
return False
|
||||||
|
elif not cwip_enabled and (not expense_booked or cwip_booked):
|
||||||
|
# if cwip is disabled but expense hasn't been booked yet
|
||||||
|
return True
|
||||||
|
elif cwip_enabled:
|
||||||
|
# default condition
|
||||||
|
return True
|
||||||
|
|
||||||
|
def get_purchase_document(self):
|
||||||
|
asset_bought_with_invoice = self.purchase_invoice and frappe.db.get_value('Purchase Invoice', self.purchase_invoice, 'update_stock')
|
||||||
|
purchase_document = self.purchase_invoice if asset_bought_with_invoice else self.purchase_receipt
|
||||||
|
|
||||||
|
return purchase_document
|
||||||
|
|
||||||
|
def get_asset_accounts(self):
|
||||||
|
fixed_asset_account = get_asset_category_account('fixed_asset_account', asset=self.name,
|
||||||
|
asset_category = self.asset_category, company = self.company)
|
||||||
|
|
||||||
|
cwip_account = get_asset_account("capital_work_in_progress_account",
|
||||||
|
self.name, self.asset_category, self.company)
|
||||||
|
|
||||||
|
return fixed_asset_account, cwip_account
|
||||||
|
|
||||||
def make_gl_entries(self):
|
def make_gl_entries(self):
|
||||||
gl_entries = []
|
gl_entries = []
|
||||||
|
|
||||||
if ((self.purchase_receipt \
|
purchase_document = self.get_purchase_document()
|
||||||
or (self.purchase_invoice and frappe.db.get_value('Purchase Invoice', self.purchase_invoice, 'update_stock')))
|
fixed_asset_account, cwip_account = self.get_asset_accounts()
|
||||||
and self.purchase_receipt_amount and self.available_for_use_date <= nowdate()):
|
|
||||||
fixed_asset_account = get_asset_category_account('fixed_asset_account', asset=self.name,
|
|
||||||
asset_category = self.asset_category, company = self.company)
|
|
||||||
|
|
||||||
cwip_account = get_asset_account("capital_work_in_progress_account",
|
if (purchase_document and self.purchase_receipt_amount and self.available_for_use_date <= nowdate()):
|
||||||
self.name, self.asset_category, self.company)
|
|
||||||
|
|
||||||
gl_entries.append(self.get_gl_dict({
|
gl_entries.append(self.get_gl_dict({
|
||||||
"account": cwip_account,
|
"account": cwip_account,
|
||||||
|
|||||||
@ -560,6 +560,81 @@ class TestAsset(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(gle, expected_gle)
|
self.assertEqual(gle, expected_gle)
|
||||||
|
|
||||||
|
def test_gle_with_cwip_toggling(self):
|
||||||
|
# TEST: purchase an asset with cwip enabled and then disable cwip and try submitting the asset
|
||||||
|
frappe.db.set_value("Asset Category", "Computers", "enable_cwip_accounting", 1)
|
||||||
|
|
||||||
|
pr = make_purchase_receipt(item_code="Macbook Pro",
|
||||||
|
qty=1, rate=5000, do_not_submit=True, location="Test Location")
|
||||||
|
pr.set('taxes', [{
|
||||||
|
'category': 'Total',
|
||||||
|
'add_deduct_tax': 'Add',
|
||||||
|
'charge_type': 'On Net Total',
|
||||||
|
'account_head': '_Test Account Service Tax - _TC',
|
||||||
|
'description': '_Test Account Service Tax',
|
||||||
|
'cost_center': 'Main - _TC',
|
||||||
|
'rate': 5.0
|
||||||
|
}, {
|
||||||
|
'category': 'Valuation and Total',
|
||||||
|
'add_deduct_tax': 'Add',
|
||||||
|
'charge_type': 'On Net Total',
|
||||||
|
'account_head': '_Test Account Shipping Charges - _TC',
|
||||||
|
'description': '_Test Account Shipping Charges',
|
||||||
|
'cost_center': 'Main - _TC',
|
||||||
|
'rate': 5.0
|
||||||
|
}])
|
||||||
|
pr.submit()
|
||||||
|
expected_gle = (
|
||||||
|
("Asset Received But Not Billed - _TC", 0.0, 5250.0),
|
||||||
|
("CWIP Account - _TC", 5250.0, 0.0)
|
||||||
|
)
|
||||||
|
pr_gle = frappe.db.sql("""select account, debit, credit from `tabGL Entry`
|
||||||
|
where voucher_type='Purchase Receipt' and voucher_no = %s
|
||||||
|
order by account""", pr.name)
|
||||||
|
self.assertEqual(pr_gle, expected_gle)
|
||||||
|
|
||||||
|
pi = make_invoice(pr.name)
|
||||||
|
pi.submit()
|
||||||
|
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),
|
||||||
|
("Creditors - _TC", 0.0, 5500.0),
|
||||||
|
("Expenses Included In Asset Valuation - _TC", 0.0, 250.0),
|
||||||
|
)
|
||||||
|
pi_gle = frappe.db.sql("""select account, debit, credit from `tabGL Entry`
|
||||||
|
where voucher_type='Purchase Invoice' and voucher_no = %s
|
||||||
|
order by account""", pi.name)
|
||||||
|
self.assertEqual(pi_gle, expected_gle)
|
||||||
|
|
||||||
|
asset = frappe.db.get_value('Asset', {'purchase_receipt': pr.name, 'docstatus': 0}, 'name')
|
||||||
|
asset_doc = frappe.get_doc('Asset', asset)
|
||||||
|
month_end_date = get_last_day(nowdate())
|
||||||
|
asset_doc.available_for_use_date = nowdate() if nowdate() != month_end_date else add_days(nowdate(), -15)
|
||||||
|
self.assertEqual(asset_doc.gross_purchase_amount, 5250.0)
|
||||||
|
asset_doc.append("finance_books", {
|
||||||
|
"expected_value_after_useful_life": 200,
|
||||||
|
"depreciation_method": "Straight Line",
|
||||||
|
"total_number_of_depreciations": 3,
|
||||||
|
"frequency_of_depreciation": 10,
|
||||||
|
"depreciation_start_date": month_end_date
|
||||||
|
})
|
||||||
|
|
||||||
|
# disable cwip and try submitting
|
||||||
|
frappe.db.set_value("Asset Category", "Computers", "enable_cwip_accounting", 0)
|
||||||
|
asset_doc.submit()
|
||||||
|
# asset should have gl entries even if cwip is disabled
|
||||||
|
expected_gle = (
|
||||||
|
("_Test Fixed Asset - _TC", 5250.0, 0.0),
|
||||||
|
("CWIP Account - _TC", 0.0, 5250.0)
|
||||||
|
)
|
||||||
|
gle = frappe.db.sql("""select account, debit, credit from `tabGL Entry`
|
||||||
|
where voucher_type='Asset' and voucher_no = %s
|
||||||
|
order by account""", asset_doc.name)
|
||||||
|
self.assertEqual(gle, expected_gle)
|
||||||
|
|
||||||
|
frappe.db.set_value("Asset Category", "Computers", "enable_cwip_accounting", 1)
|
||||||
|
|
||||||
def test_expense_head(self):
|
def test_expense_head(self):
|
||||||
pr = make_purchase_receipt(item_code="Macbook Pro",
|
pr = make_purchase_receipt(item_code="Macbook Pro",
|
||||||
qty=2, rate=200000.0, location="Test Location")
|
qty=2, rate=200000.0, location="Test Location")
|
||||||
|
|||||||
@ -110,6 +110,7 @@ class AssetMovement(Document):
|
|||||||
ORDER BY
|
ORDER BY
|
||||||
asm.transaction_date asc
|
asm.transaction_date asc
|
||||||
""", (d.asset, self.company, 'Receipt'), as_dict=1)
|
""", (d.asset, self.company, 'Receipt'), as_dict=1)
|
||||||
|
|
||||||
if auto_gen_movement_entry and auto_gen_movement_entry[0].get('name') == self.name:
|
if auto_gen_movement_entry and auto_gen_movement_entry[0].get('name') == self.name:
|
||||||
frappe.throw(_('{0} will be cancelled automatically on asset cancellation as it was \
|
frappe.throw(_('{0} will be cancelled automatically on asset cancellation as it was \
|
||||||
auto generated for Asset {1}').format(self.name, d.asset))
|
auto generated for Asset {1}').format(self.name, d.asset))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user