chore: fix some bugs, refactor some functions, add proper notes

This commit is contained in:
anandbaburajan 2022-12-12 16:35:30 +05:30
parent 90e1b9cb3d
commit ec6505d747
6 changed files with 100 additions and 79 deletions

View File

@ -1190,8 +1190,11 @@ class SalesInvoice(SellingController):
else:
if asset.calculate_depreciation:
notes = _(
"This schedule was created when the Asset {0} was sold through Sales Invoice {1}."
).format(asset.name, self.get("name"))
"This schedule was created when Asset {0} was sold through Sales Invoice {1}."
).format(
get_link_to_form(asset.doctype, asset.name),
get_link_to_form(self.doctype, self.get("name")),
)
depreciate_asset(asset, self.posting_date, notes)
fixed_asset_gl_entries = get_gl_entries_on_asset_disposal(

View File

@ -14,6 +14,7 @@ from frappe.utils import (
flt,
get_datetime,
get_last_day,
get_link_to_form,
getdate,
is_last_day_of_the_month,
month_diff,
@ -31,9 +32,10 @@ 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_doc_of_asset,
get_depr_schedule_from_asset_depr_schedule_of_asset,
make_draft_asset_depr_schedules,
make_new_active_asset_depr_schedules_and_cancel_current_ones,
set_draft_asset_depr_schedule_details,
update_draft_asset_depr_schedules,
)
from erpnext.controllers.accounts_controller import AccountsController
@ -48,7 +50,6 @@ class Asset(AccountsController):
self.set_missing_values()
if not self.split_from:
self.prepare_depreciation_data()
update_draft_asset_depr_schedules(self)
self.validate_gross_and_purchase_amount()
self.validate_expected_value_after_useful_life()
@ -97,6 +98,7 @@ class Asset(AccountsController):
if self.calculate_depreciation:
self.value_after_depreciation = 0
self.set_depreciation_rate()
update_draft_asset_depr_schedules(self)
else:
self.finance_books = []
self.value_after_depreciation = flt(self.gross_purchase_amount) - flt(
@ -858,12 +860,12 @@ def split_asset(asset_name, split_qty):
remaining_qty = asset.asset_quantity - split_qty
new_asset = create_new_asset_after_split(asset, split_qty)
update_existing_asset(asset, remaining_qty)
update_existing_asset(asset, remaining_qty, new_asset.name)
return new_asset
def update_existing_asset(asset, remaining_qty):
def update_existing_asset(asset, remaining_qty, new_asset_name):
remaining_gross_purchase_amount = flt(
(asset.gross_purchase_amount * remaining_qty) / asset.asset_quantity
)
@ -898,18 +900,31 @@ def update_existing_asset(asset, remaining_qty):
expected_value_after_useful_life,
)
accumulated_depreciation = 0
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(asset.name, row.finance_book)
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(
asset.name, row.finance_book
)
new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc)
for term in depr_schedule:
set_draft_asset_depr_schedule_details(new_asset_depr_schedule_doc, asset, row)
accumulated_depreciation = 0
for term in new_asset_depr_schedule_doc.get("depreciation_schedule"):
depreciation_amount = flt((term.depreciation_amount * remaining_qty) / asset.asset_quantity)
frappe.db.set_value(
"Depreciation Schedule", term.name, "depreciation_amount", depreciation_amount
)
term.depreciation_amount = depreciation_amount
accumulated_depreciation += depreciation_amount
frappe.db.set_value(
"Depreciation Schedule", term.name, "accumulated_depreciation_amount", accumulated_depreciation
term.accumulated_depreciation_amount = accumulated_depreciation
notes = _(
"This schedule was created when Asset {0} was updated after being split into new Asset {1}."
).format(
get_link_to_form(asset.doctype, asset.name), get_link_to_form(asset.doctype, new_asset_name)
)
new_asset_depr_schedule_doc.notes = notes
current_asset_depr_schedule_doc.cancel()
new_asset_depr_schedule_doc.submit()
def create_new_asset_after_split(asset, split_qty):
@ -932,33 +947,42 @@ def create_new_asset_after_split(asset, split_qty):
(row.expected_value_after_useful_life * split_qty) / asset.asset_quantity
)
new_asset.submit()
make_new_active_asset_depr_schedules_and_cancel_current_ones(
new_asset, "create_new_asset_after_split TODO", submit=False
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(
asset.name, row.finance_book
)
new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc)
set_draft_asset_depr_schedule_details(new_asset_depr_schedule_doc, new_asset, row)
for row in new_asset.get("finance_books"):
accumulated_depreciation = 0
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(
new_asset.name, row.finance_book
)
for term in depr_schedule:
for term in new_asset_depr_schedule_doc.get("depreciation_schedule"):
depreciation_amount = flt((term.depreciation_amount * split_qty) / asset.asset_quantity)
term.depreciation_amount = depreciation_amount
accumulated_depreciation += depreciation_amount
term.accumulated_depreciation_amount = accumulated_depreciation
notes = _("This schedule was created when new Asset {0} was split from Asset {1}.").format(
get_link_to_form(new_asset.doctype, new_asset.name), get_link_to_form(asset.doctype, asset.name)
)
new_asset_depr_schedule_doc.notes = notes
new_asset_depr_schedule_doc.insert()
new_asset.submit()
new_asset.set_status()
for row in new_asset.get("finance_books"):
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(
new_asset.name, row.finance_book
)
for term in depr_schedule:
# Update references in JV
if term.journal_entry:
add_reference_in_jv_on_split(
term.journal_entry, new_asset.name, asset.name, term.depreciation_amount
)
new_asset.set_status()
return new_asset

View File

@ -4,7 +4,7 @@
import frappe
from frappe import _
from frappe.utils import add_months, cint, flt, getdate, nowdate, today
from frappe.utils import add_months, cint, flt, get_link_to_form, getdate, nowdate, today
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
get_checks_for_pl_and_bs_accounts,
@ -238,8 +238,8 @@ def scrap_asset(asset_name):
je.submit()
notes = _(
"This schedule was created when the Asset {0} was scrapped through Journal Entry {1}."
).format(asset.name, je.name)
"This schedule was created when Asset {0} was scrapped through Journal Entry {1}."
).format(get_link_to_form(asset.doctype, asset.name), get_link_to_form(je.doctype, je.name))
depreciate_asset(asset, date, notes)
frappe.db.set_value("Asset", asset_name, "disposal_date", date)
@ -291,9 +291,10 @@ def modify_depreciation_schedule_for_asset_repairs(asset):
if repair.increase_in_asset_life:
asset_repair = frappe.get_doc("Asset Repair", repair.name)
asset_repair.modify_depreciation_schedule()
notes = _(
"This schedule was created when the Asset {0} went through the Asset Repair {1}."
).format(asset.name, repair.name)
notes = _("This schedule was created when Asset {0} went through Asset Repair {1}.").format(
get_link_to_form(asset.doctype, asset.name),
get_link_to_form(asset_repair.doctype, asset_repair.name),
)
make_new_active_asset_depr_schedules_and_cancel_current_ones(asset, notes)

View File

@ -7,7 +7,7 @@ import frappe
# import erpnext
from frappe import _
from frappe.utils import cint, flt
from frappe.utils import cint, flt, get_link_to_form
from six import string_types
import erpnext
@ -431,8 +431,10 @@ class AssetCapitalization(StockController):
if asset.calculate_depreciation:
notes = _(
"This schedule was created when the Asset {0} was consumed through Asset Capitalization {1}."
).format(asset.name, self.get("name"))
"This schedule was created when Asset {0} was consumed when Asset Capitalization {1} was submitted."
).format(
get_link_to_form(asset.doctype, asset.name), get_link_to_form(self.doctype, self.get("name"))
)
depreciate_asset(asset, self.posting_date, notes)
asset.reload()
@ -519,8 +521,10 @@ class AssetCapitalization(StockController):
asset_doc.purchase_date = self.posting_date
asset_doc.gross_purchase_amount = total_target_asset_value
asset_doc.purchase_receipt_amount = total_target_asset_value
notes = _("This schedule was created when the Asset Capitalization {0} was submitted.").format(
self.name
notes = _(
"This schedule was created when target Asset {0} was updated when Asset Capitalization {1} was submitted."
).format(
get_link_to_form(asset_doc.doctype, asset_doc.name), get_link_to_form(self.doctype, self.name)
)
make_new_active_asset_depr_schedules_and_cancel_current_ones(asset_doc, notes)
elif self.docstatus == 2:
@ -532,8 +536,10 @@ class AssetCapitalization(StockController):
if asset.calculate_depreciation:
reverse_depreciation_entry_made_after_disposal(asset, self.posting_date)
notes = _(
"This schedule was created when the Asset Capitalization {0} was cancelled."
).format(self.name)
"This schedule was created when Asset {0} was restored when Asset Capitalization {1} was cancelled."
).format(
get_link_to_form(asset.doctype, asset.name), get_link_to_form(self.doctype, self.name)
)
reset_depreciation_schedule(asset, self.posting_date, notes)
def get_asset(self, item):

View File

@ -33,12 +33,7 @@ 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_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
if not asset_depr_schedule_name:
return
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(asset_doc.name, row.finance_book)
prepare_draft_asset_depr_schedule_data(
asset_depr_schedule_doc, asset_doc, row, date_of_disposal, date_of_return
@ -72,12 +67,7 @@ def set_draft_asset_depr_schedule_details(asset_depr_schedule_doc, asset_doc, ro
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_name(asset_doc.name, row.finance_book)
if not asset_depr_schedule_name:
return
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(asset_doc.name, row.finance_book)
asset_depr_schedule_doc.status = "Active"
@ -85,16 +75,11 @@ def convert_draft_asset_depr_schedules_into_active(asset_doc):
def make_new_active_asset_depr_schedules_and_cancel_current_ones(
asset_doc, notes, submit=True, date_of_disposal=None, date_of_return=None
asset_doc, notes, date_of_disposal=None, date_of_return=None
):
for row in asset_doc.get("finance_books"):
current_asset_depr_schedule_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
if not current_asset_depr_schedule_name:
return
current_asset_depr_schedule_doc = frappe.get_doc(
"Asset Depreciation Schedule", current_asset_depr_schedule_name
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(
asset_doc.name, row.finance_book
)
new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc)
@ -106,9 +91,6 @@ def make_new_active_asset_depr_schedules_and_cancel_current_ones(
current_asset_depr_schedule_doc.cancel()
new_asset_depr_schedule_doc.insert()
if submit:
new_asset_depr_schedule_doc.submit()
@ -124,12 +106,9 @@ 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_name = get_asset_depr_schedule_name(asset_doc.name, row.finance_book)
asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(asset_doc.name, row.finance_book)
if not asset_depr_schedule_name:
return
asset_depr_schedule_doc = frappe.get_doc("Asset Depreciation Schedule", asset_depr_schedule_name)
asset_depr_schedule_doc.status = "Cancelled"
asset_depr_schedule_doc.cancel()
@ -151,6 +130,12 @@ def get_asset_depr_schedule_name(asset_name, finance_book):
@frappe.whitelist()
def get_depr_schedule_from_asset_depr_schedule_of_asset(asset_name, finance_book):
asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(asset_name, finance_book)
return asset_depr_schedule_doc.get("depreciation_schedule")
def get_asset_depr_schedule_doc_of_asset(asset_name, finance_book):
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, finance_book)
if not asset_depr_schedule_name:
@ -158,7 +143,7 @@ def get_depr_schedule_from_asset_depr_schedule_of_asset(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")
return asset_depr_schedule_doc
def make_depr_schedule(asset_depr_schedule_doc, asset_doc, row, date_of_disposal):

View File

@ -3,7 +3,7 @@
import frappe
from frappe import _
from frappe.utils import add_months, cint, flt, getdate, time_diff_in_hours
from frappe.utils import add_months, cint, flt, get_link_to_form, getdate, time_diff_in_hours
import erpnext
from erpnext.accounts.general_ledger import make_gl_entries
@ -55,8 +55,9 @@ class AssetRepair(AccountsController):
and self.increase_in_asset_life
):
self.modify_depreciation_schedule()
notes = _("This schedule was created when the Asset Repair {0} was submitted.").format(
self.name
notes = _("This schedule was created when Asset Repair {0} was submitted.").format(
get_link_to_form(self.doctype, self.name)
)
make_new_active_asset_depr_schedules_and_cancel_current_ones(self.asset_doc, notes)
@ -76,8 +77,9 @@ class AssetRepair(AccountsController):
and self.increase_in_asset_life
):
self.revert_depreciation_schedule_on_cancellation()
notes = _("This schedule was created when the Asset Repair {0} was cancelled.").format(
self.name
notes = _("This schedule was created when Asset Repair {0} was cancelled.").format(
get_link_to_form(self.doctype, self.name)
)
make_new_active_asset_depr_schedules_and_cancel_current_ones(self.asset_doc, notes)