chore: more refactoring

This commit is contained in:
anandbaburajan 2022-11-25 16:49:44 +05:30
parent 4e63ba3ac6
commit 7d09440579
10 changed files with 108 additions and 106 deletions

View File

@ -1189,8 +1189,7 @@ class SalesInvoice(SellingController):
else:
if asset.calculate_depreciation:
depreciate_asset(asset, self.posting_date)
asset.reload()
depreciate_asset(asset, self.posting_date, notes="Sell asset")
fixed_asset_gl_entries = get_gl_entries_on_asset_disposal(
asset, item.base_net_amount, item.finance_book, self.get("doctype"), self.get("name")

View File

@ -76,7 +76,6 @@ frappe.ui.form.on('Asset', {
refresh: function(frm) {
frappe.ui.form.trigger("Asset", "is_existing_asset");
frm.toggle_display("next_depreciation_date", frm.doc.docstatus < 1);
frm.events.make_schedules_editable(frm);
if (frm.doc.docstatus==1) {
if (in_list(["Submitted", "Partially Depreciated", "Fully Depreciated"], frm.doc.status)) {
@ -512,30 +511,6 @@ frappe.ui.form.on('Asset Finance Book', {
}
});
frappe.ui.form.on('Depreciation Schedule', {
make_depreciation_entry: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
if (!row.journal_entry) {
frappe.call({
method: "erpnext.assets.doctype.asset.depreciation.make_depreciation_entry",
args: {
"asset_name": frm.doc.name,
"date": row.schedule_date
},
callback: function(r) {
frappe.model.sync(r.message);
frm.refresh();
}
})
}
},
depreciation_amount: function(frm, cdt, cdn) {
erpnext.asset.set_accumulated_depreciation(frm);
}
})
erpnext.asset.set_accumulated_depreciation = function(frm) {
if(frm.doc.depreciation_method != "Manual") return;

View File

@ -52,8 +52,6 @@
"column_break_24",
"frequency_of_depreciation",
"next_depreciation_date",
"section_break_14",
"schedules",
"insurance_details",
"policy_number",
"insurer",
@ -307,19 +305,6 @@
"label": "Next Depreciation Date",
"no_copy": 1
},
{
"depends_on": "calculate_depreciation",
"fieldname": "section_break_14",
"fieldtype": "Section Break",
"label": "Depreciation Schedule"
},
{
"fieldname": "schedules",
"fieldtype": "Table",
"label": "Depreciation Schedule",
"no_copy": 1,
"options": "Depreciation Schedule"
},
{
"collapsible": 1,
"fieldname": "insurance_details",
@ -515,7 +500,7 @@
"link_fieldname": "asset"
}
],
"modified": "2022-11-01 15:25:27.669803",
"modified": "2022-11-25 12:47:19.689702",
"modified_by": "Administrator",
"module": "Assets",
"name": "Asset",

View File

@ -31,7 +31,7 @@ from erpnext.assets.doctype.asset_category.asset_category import get_asset_categ
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
cancel_asset_depr_schedules,
convert_draft_asset_depr_schedules_into_active,
get_asset_depr_schedule,
get_asset_depr_schedule_name,
get_depr_schedule_from_asset_depr_schedule_of_asset,
make_draft_asset_depr_schedules,
update_draft_asset_depr_schedules,
@ -247,12 +247,12 @@ class Asset(AccountsController):
return value_after_depreciation
def get_from_date(self, finance_book):
asset_depr_schedule = get_asset_depr_schedule(self.name, finance_book)
asset_depr_schedule_name = get_asset_depr_schedule_name(self.name, finance_book)
if not asset_depr_schedule:
if not asset_depr_schedule_name:
return self.available_for_use_date
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule)
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
return asset_depr_schedule_doc.get("depreciation_schedule")[-1].schedule_date
@ -367,12 +367,14 @@ class Asset(AccountsController):
def validate_expected_value_after_useful_life(self):
for row in self.get("finance_books"):
asset_depr_schedule = get_asset_depr_schedule(self.name, row.finance_book)
asset_depr_schedule_name = get_asset_depr_schedule_name(self.name, row.finance_book)
if not asset_depr_schedule:
if not asset_depr_schedule_name:
return
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule)
asset_depr_schedule_doc = frappe.get_doc(
"Asset Depreciation Schedule", asset_depr_schedule_name
)
accumulated_depreciation_after_full_schedule = [
d.accumulated_depreciation_amount for d in asset_depr_schedule_doc.get("depreciation_schedule")
@ -425,12 +427,14 @@ class Asset(AccountsController):
def delete_depreciation_entries(self):
for row in self.get("finance_books"):
asset_depr_schedule = get_asset_depr_schedule(self.name, row.finance_book)
asset_depr_schedule_name = get_asset_depr_schedule_name(self.name, row.finance_book)
if not asset_depr_schedule:
if not asset_depr_schedule_name:
return
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule)
asset_depr_schedule_doc = frappe.get_doc(
"Asset Depreciation Schedule", asset_depr_schedule_name
)
for d in asset_depr_schedule_doc.get("depreciation_schedule"):
if d.journal_entry:

View File

@ -11,7 +11,9 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
)
from erpnext.accounts.doctype.journal_entry.journal_entry import make_reverse_journal_entry
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
get_asset_depr_schedule_name,
get_temp_asset_depr_schedule_doc,
make_new_active_asset_depr_schedules_and_cancel_current_ones,
)
@ -25,7 +27,7 @@ def post_depreciation_entries(date=None, commit=True):
if not date:
date = today()
for asset in get_depreciable_assets(date):
make_depreciation_entry(asset, date)
make_depreciation_entry_for_all_asset_depr_schedules(asset, date)
if commit:
frappe.db.commit()
@ -41,13 +43,23 @@ def get_depreciable_assets(date):
)
def make_depreciation_entry_for_all_asset_depr_schedules(asset_doc, date=None):
for row in asset_doc.get("finance_books"):
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
make_depreciation_entry(asset_depr_schedule_name, date)
@frappe.whitelist()
def make_depreciation_entry(asset_name, date=None):
def make_depreciation_entry(asset_depr_schedule_name, date=None):
frappe.has_permission("Journal Entry", throw=True)
if not date:
date = today()
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
asset_name = asset_depr_schedule_doc.asset
asset = frappe.get_doc("Asset", asset_name)
(
fixed_asset_account,
@ -63,14 +75,14 @@ def make_depreciation_entry(asset_name, date=None):
accounting_dimensions = get_checks_for_pl_and_bs_accounts()
for d in asset.get("schedules"):
for d in asset_depr_schedule_doc.get("depreciation_schedule"):
if not d.journal_entry and getdate(d.schedule_date) <= getdate(date):
je = frappe.new_doc("Journal Entry")
je.voucher_type = "Depreciation Entry"
je.naming_series = depreciation_series
je.posting_date = d.schedule_date
je.company = asset.company
je.finance_book = d.finance_book
je.finance_book = asset_depr_schedule_doc.finance_book
je.remark = "Depreciation Entry against {0} worth {1}".format(asset_name, d.depreciation_amount)
credit_account, debit_account = get_credit_and_debit_accounts(
@ -121,14 +133,14 @@ def make_depreciation_entry(asset_name, date=None):
d.db_set("journal_entry", je.name)
idx = cint(d.finance_book_id)
finance_books = asset.get("finance_books")[idx - 1]
finance_books.value_after_depreciation -= d.depreciation_amount
finance_books.db_update()
idx = cint(asset_depr_schedule_doc.finance_book_id)
row = asset.get("finance_books")[idx - 1]
row.value_after_depreciation -= d.depreciation_amount
row.db_update()
asset.set_status()
return asset
return asset_depr_schedule_doc
def get_depreciation_accounts(asset):
@ -202,8 +214,7 @@ def scrap_asset(asset_name):
date = today()
depreciate_asset(asset, date)
asset.reload()
depreciate_asset(asset, date, notes="Scrap asset")
depreciation_series = frappe.get_cached_value(
"Company", asset.company, "series_for_depreciation_entry"
@ -247,22 +258,20 @@ def restore_asset(asset_name):
asset.set_status()
def depreciate_asset(asset, date):
asset.flags.ignore_validate_update_after_submit = True
asset.prepare_depreciation_data(date_of_disposal=date)
asset.save()
def depreciate_asset(asset, date, notes):
make_new_active_asset_depr_schedules_and_cancel_current_ones(
asset, date_of_disposal=date, notes=notes
)
make_depreciation_entry(asset.name, date)
make_depreciation_entry_for_all_asset_depr_schedules(asset, date)
def reset_depreciation_schedule(asset, date):
asset.flags.ignore_validate_update_after_submit = True
# recreate original depreciation schedule of the asset
asset.prepare_depreciation_data(date_of_return=date)
def reset_depreciation_schedule(asset, date, notes):
make_new_active_asset_depr_schedules_and_cancel_current_ones(
asset, date_of_return=date, notes=notes
)
modify_depreciation_schedule_for_asset_repairs(asset)
asset.save()
def modify_depreciation_schedule_for_asset_repairs(asset):

View File

@ -427,8 +427,7 @@ class AssetCapitalization(StockController):
asset = self.get_asset(item)
if asset.calculate_depreciation:
depreciate_asset(asset, self.posting_date)
asset.reload()
depreciate_asset(asset, self.posting_date, notes="TODO")
fixed_asset_gl_entries = get_gl_entries_on_asset_disposal(
asset,

View File

@ -1,8 +1,25 @@
// Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt
frappe.ui.form.on('Asset Depreciation Schedule', {
// refresh: function(frm) {
frappe.ui.form.on('Depreciation Schedule', {
make_depreciation_entry: function(frm, cdt, cdn) {
var row = locals[cdt][cdn];
if (!row.journal_entry) {
frappe.call({
method: "erpnext.assets.doctype.asset.depreciation.make_depreciation_entry",
args: {
"asset_depr_schedule_name": frm.doc.name,
"date": row.schedule_date
},
callback: function(r) {
frappe.model.sync(r.message);
frm.refresh();
}
})
}
},
// }
});
depreciation_amount: function(frm, cdt, cdn) {
erpnext.asset.set_accumulated_depreciation(frm);
}
})

View File

@ -11,6 +11,7 @@
"naming_series",
"column_break_2",
"finance_book",
"finance_book_id",
"depreciation_details_section",
"depreciation_method",
"total_number_of_depreciations",
@ -142,13 +143,21 @@
"label": "Expected Value After Useful Life",
"options": "Company:company:default_currency",
"read_only": 1
},
{
"fieldname": "finance_book_id",
"fieldtype": "Data",
"hidden": 1,
"label": "Finance Book Id",
"print_hide": 1,
"read_only": 1
}
],
"in_create": 1,
"index_web_pages_for_search": 1,
"is_submittable": 1,
"links": [],
"modified": "2022-11-14 14:33:53.360643",
"modified": "2022-11-25 14:31:52.668821",
"modified_by": "Administrator",
"module": "Assets",
"name": "Asset Depreciation Schedule",

View File

@ -33,12 +33,12 @@ def make_draft_asset_depr_schedules(asset_doc, date_of_disposal=None, date_of_re
def update_draft_asset_depr_schedules(asset_doc, date_of_disposal=None, date_of_return=None):
for row in asset_doc.get("finance_books"):
asset_depr_schedule = get_asset_depr_schedule(asset_doc.name, row.finance_book)
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
if not asset_depr_schedule:
if not asset_depr_schedule_name:
return
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule)
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
prepare_draft_asset_depr_schedule_data(
asset_depr_schedule_doc, asset_doc, row, date_of_disposal, date_of_return
@ -57,9 +57,10 @@ def prepare_draft_asset_depr_schedule_data(
)
def set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset, row):
asset_depr_schedule_doc.asset = asset
def set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset_name, row):
asset_depr_schedule_doc.asset = asset_name
asset_depr_schedule_doc.finance_book = row.finance_book
asset_depr_schedule_doc.finance_book_id = row.idx
asset_depr_schedule_doc.depreciation_method = row.depreciation_method
asset_depr_schedule_doc.total_number_of_depreciations = row.total_number_of_depreciations
asset_depr_schedule_doc.frequency_of_depreciation = row.frequency_of_depreciation
@ -70,29 +71,29 @@ def set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset, row):
def convert_draft_asset_depr_schedules_into_active(asset_doc):
for row in asset_doc.get("finance_books"):
asset_depr_schedule_name = get_asset_depr_schedule(asset_doc.name, row.finance_book)
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
if not asset_depr_schedule_name:
return
asset_depr_schedule = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
asset_depr_schedule.status = "Active"
asset_depr_schedule_doc.status = "Active"
asset_depr_schedule.submit()
asset_depr_schedule_doc.submit()
def make_new_active_asset_depr_schedules_and_cancel_current_ones(
asset_doc, date_of_disposal=None, date_of_return=None, notes=None
):
for row in asset_doc.get("finance_books"):
current_asset_depr_schedule = get_asset_depr_schedule(asset_doc.name, row.finance_book)
current_asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
if not current_asset_depr_schedule:
if not current_asset_depr_schedule_name:
return
current_asset_depr_schedule_doc = frappe.get_doc(
"Asset Depreciation Schedule", current_asset_depr_schedule
"Asset Depreciation Schedule", current_asset_depr_schedule_name
)
new_asset_depr_schedule_doc = frappe.copy_doc(
@ -123,30 +124,34 @@ def get_temp_asset_depr_schedule_doc(asset_doc, row, date_of_disposal=None, date
def cancel_asset_depr_schedules(asset_doc):
for row in asset_doc.get("finance_books"):
asset_depr_schedule = get_asset_depr_schedule(asset_doc.name, row.finance_book)
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
if not asset_depr_schedule:
if not asset_depr_schedule_name:
return
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule)
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
asset_depr_schedule_doc.cancel()
def get_asset_depr_schedule(asset, finance_book):
def get_asset_depr_schedule_name(asset_name, finance_book):
finance_book_filter = ["finance_book", "is", "not set"]
if finance_book:
finance_book_filter = ["finance_book", "=", finance_book]
return frappe.db.get_value(
doctype="Asset Depreciation Schedule",
filters=[
["asset", "=", asset],
["finance_book", "=", finance_book],
["asset", "=", asset_name],
finance_book_filter,
["docstatus", "<", 2],
],
)
def get_depr_schedule_from_asset_depr_schedule_of_asset(asset, finance_book):
asset_depr_schedule = get_asset_depr_schedule(asset, finance_book)
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule)
def get_depr_schedule_from_asset_depr_schedule_of_asset(asset_name, finance_book):
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, finance_book)
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
return asset_depr_schedule_doc.get("depreciation_schedule")

View File

@ -12,7 +12,7 @@ from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
)
from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
get_asset_depr_schedule,
get_asset_depr_schedule_name,
get_depreciation_amount,
set_accumulated_depreciation,
)
@ -116,9 +116,9 @@ class AssetValueAdjustment(Document):
for d in asset.finance_books:
d.value_after_depreciation = asset_value
current_asset_depr_schedule = get_asset_depr_schedule(asset.name, d.finance_book)
current_asset_depr_schedule_name = get_asset_depr_schedule_name(asset.name, d.finance_book)
current_asset_depr_schedule_doc = frappe.get_doc(
"Asset Depreciation Schedule", current_asset_depr_schedule
"Asset Depreciation Schedule", current_asset_depr_schedule_name
)
new_asset_depr_schedule_doc = frappe.copy_doc(