chore: add some validation, shorten some function names

This commit is contained in:
anandbaburajan 2022-12-12 17:25:38 +05:30
parent f8cd424559
commit b997d1eb87
6 changed files with 37 additions and 37 deletions

View File

@ -24,7 +24,7 @@ from erpnext.accounts.utils import (
get_stock_and_account_balance,
)
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
get_depr_schedule_from_asset_depr_schedule_of_asset,
get_depr_schedule,
)
from erpnext.controllers.accounts_controller import AccountsController
@ -287,9 +287,7 @@ class JournalEntry(AccountsController):
if d.reference_type == "Asset" and d.reference_name:
asset = frappe.get_doc("Asset", d.reference_name)
for row in asset.get("finance_books"):
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(
asset.name, row.finance_book
)
depr_schedule = get_depr_schedule(asset.name, row.finance_book)
for s in depr_schedule:
if s.journal_entry == self.name:

View File

@ -209,7 +209,7 @@ frappe.ui.form.on('Asset', {
if (frm.doc.finance_books.length == 1) {
depr_schedule = (await frappe.call(
"erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule.get_depr_schedule_from_asset_depr_schedule_of_asset",
"erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule.get_depr_schedule",
{
asset_name: frm.doc.name,
finance_book: frm.doc.finance_books[0].finance_book || null

View File

@ -32,8 +32,8 @@ 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,
get_asset_depr_schedule_doc,
get_depr_schedule,
make_draft_asset_depr_schedules,
set_draft_asset_depr_schedule_details,
update_draft_asset_depr_schedules,
@ -50,6 +50,7 @@ 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()
@ -98,7 +99,6 @@ 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(
@ -350,13 +350,11 @@ class Asset(AccountsController):
return depreciation_amount_for_last_row
def get_depreciation_amount_for_first_row(self, finance_book):
return get_depr_schedule_from_asset_depr_schedule_of_asset(self.name, finance_book)[
0
].depreciation_amount
return get_depr_schedule(self.name, finance_book)[0].depreciation_amount
def validate_expected_value_after_useful_life(self):
for row in self.get("finance_books"):
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(self.name, row.finance_book)
depr_schedule = get_depr_schedule(self.name, row.finance_book)
if not depr_schedule:
continue
@ -412,7 +410,7 @@ class Asset(AccountsController):
def delete_depreciation_entries(self):
for row in self.get("finance_books"):
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(self.name, row.finance_book)
depr_schedule = get_depr_schedule(self.name, row.finance_book)
for d in depr_schedule.get("depreciation_schedule"):
if d.journal_entry:
@ -900,9 +898,7 @@ def update_existing_asset(asset, remaining_qty, new_asset_name):
expected_value_after_useful_life,
)
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(
asset.name, row.finance_book
)
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(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, asset, row)
@ -947,9 +943,7 @@ def create_new_asset_after_split(asset, split_qty):
(row.expected_value_after_useful_life * split_qty) / asset.asset_quantity
)
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(
asset.name, row.finance_book
)
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(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)
@ -973,9 +967,7 @@ def create_new_asset_after_split(asset, split_qty):
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
)
depr_schedule = get_depr_schedule(new_asset.name, row.finance_book)
for term in depr_schedule:
# Update references in JV
if term.journal_entry:

View File

@ -12,7 +12,7 @@ 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_depr_schedule_from_asset_depr_schedule_of_asset,
get_depr_schedule,
get_temp_asset_depr_schedule_doc,
make_new_active_asset_depr_schedules_and_cancel_current_ones,
)
@ -300,7 +300,7 @@ def modify_depreciation_schedule_for_asset_repairs(asset):
def reverse_depreciation_entry_made_after_disposal(asset, date):
for row in asset.get("finance_books"):
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(asset, row.finance_book)
depr_schedule = get_depr_schedule(asset, row.finance_book)
for schedule_idx, schedule in enumerate(depr_schedule):
if schedule.schedule_date == date:

View File

@ -2,6 +2,7 @@
# For license information, please see license.txt
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import (
add_days,
@ -17,7 +18,18 @@ import erpnext
class AssetDepreciationSchedule(Document):
pass
def validate(self):
self.validate_another_asset_depr_schedule_does_not_exist()
def validate_another_asset_depr_schedule_does_not_exist(self):
asset_depr_schedule_name = get_asset_depr_schedule_name(self.asset, self.finance_book)
if asset_depr_schedule_name:
frappe.throw(
_("Asset Depreciation Schedule for Asset {0} and Finance Book {1} already exists.").format(
self.asset, self.finance_book
)
)
def make_draft_asset_depr_schedules(asset_doc, date_of_disposal=None, date_of_return=None):
@ -33,7 +45,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_doc = get_asset_depr_schedule_doc_of_asset(asset_doc.name, row.finance_book)
asset_depr_schedule_doc = get_asset_depr_schedule_doc(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
@ -67,7 +79,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_doc = get_asset_depr_schedule_doc_of_asset(asset_doc.name, row.finance_book)
asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_doc.name, row.finance_book)
asset_depr_schedule_doc.status = "Active"
@ -78,9 +90,7 @@ def make_new_active_asset_depr_schedules_and_cancel_current_ones(
asset_doc, notes, date_of_disposal=None, date_of_return=None
):
for row in asset_doc.get("finance_books"):
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc_of_asset(
asset_doc.name, row.finance_book
)
current_asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_doc.name, row.finance_book)
new_asset_depr_schedule_doc = frappe.copy_doc(current_asset_depr_schedule_doc)
@ -106,7 +116,7 @@ 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_doc = get_asset_depr_schedule_doc_of_asset(asset_doc.name, row.finance_book)
asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_doc.name, row.finance_book)
asset_depr_schedule_doc.status = "Cancelled"
@ -129,13 +139,13 @@ 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)
def get_depr_schedule(asset_name, finance_book):
asset_depr_schedule_doc = get_asset_depr_schedule_doc(asset_name, finance_book)
return asset_depr_schedule_doc.get("depreciation_schedule")
def get_asset_depr_schedule_doc_of_asset(asset_name, finance_book):
def get_asset_depr_schedule_doc(asset_name, finance_book):
asset_depr_schedule_name = get_asset_depr_schedule_name(asset_name, finance_book)
if not asset_depr_schedule_name:

View File

@ -9,7 +9,7 @@ import erpnext
from erpnext.accounts.general_ledger import make_gl_entries
from erpnext.assets.doctype.asset.asset import get_asset_account
from erpnext.assets.doctype.asset_depreciation_schedule.asset_depreciation_schedule import (
get_depr_schedule_from_asset_depr_schedule_of_asset,
get_depr_schedule,
make_new_active_asset_depr_schedules_and_cancel_current_ones,
)
from erpnext.controllers.accounts_controller import AccountsController
@ -285,7 +285,7 @@ class AssetRepair(AccountsController):
asset.number_of_depreciations_booked
)
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(asset.name, row.finance_book)
depr_schedule = get_depr_schedule(asset.name, row.finance_book)
# the Schedule Date in the final row of the old Depreciation Schedule
last_schedule_date = depr_schedule[len(depr_schedule) - 1].schedule_date
@ -318,7 +318,7 @@ class AssetRepair(AccountsController):
asset.number_of_depreciations_booked
)
depr_schedule = get_depr_schedule_from_asset_depr_schedule_of_asset(asset.name, row.finance_book)
depr_schedule = get_depr_schedule(asset.name, row.finance_book)
# the Schedule Date in the final row of the modified Depreciation Schedule
last_schedule_date = depr_schedule[len(depr_schedule) - 1].schedule_date