fix(Asset Repair): Fix tests
This commit is contained in:
parent
42fd7ffbc0
commit
852881e33e
@ -263,7 +263,7 @@
|
|||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"is_submittable": 1,
|
"is_submittable": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2021-06-19 15:20:24.056706",
|
"modified": "2021-06-20 17:35:51.075537",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Assets",
|
"module": "Assets",
|
||||||
"name": "Asset Repair",
|
"name": "Asset Repair",
|
||||||
|
|||||||
@ -6,74 +6,72 @@ from __future__ import unicode_literals
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.utils import time_diff_in_hours, getdate, add_months, flt, cint
|
from frappe.utils import time_diff_in_hours, getdate, add_months, flt, cint
|
||||||
from frappe.model.document import Document
|
|
||||||
from erpnext.accounts.general_ledger import make_gl_entries
|
from erpnext.accounts.general_ledger import make_gl_entries
|
||||||
from erpnext.assets.doctype.asset.asset import get_asset_account
|
from erpnext.assets.doctype.asset.asset import get_asset_account
|
||||||
from erpnext.controllers.accounts_controller import AccountsController
|
from erpnext.controllers.accounts_controller import AccountsController
|
||||||
|
|
||||||
class AssetRepair(AccountsController):
|
class AssetRepair(AccountsController):
|
||||||
def validate(self):
|
def validate(self):
|
||||||
if self.repair_status == "Completed" and not self.completion_date:
|
self.asset_doc = frappe.get_doc('Asset', self.asset)
|
||||||
frappe.throw(_("Please select Completion Date for Completed Repair"))
|
|
||||||
|
|
||||||
self.update_status()
|
self.update_status()
|
||||||
self.set_total_value() # change later
|
if self.get('stock_items'):
|
||||||
|
self.set_total_value() # change later
|
||||||
self.calculate_total_repair_cost()
|
self.calculate_total_repair_cost()
|
||||||
|
|
||||||
def update_status(self):
|
def update_status(self):
|
||||||
if self.repair_status == 'Pending':
|
if self.repair_status == 'Pending':
|
||||||
frappe.db.set_value('Asset', self.asset, 'status', 'Out of Order')
|
frappe.db.set_value('Asset', self.asset, 'status', 'Out of Order')
|
||||||
else:
|
else:
|
||||||
asset = frappe.get_doc('Asset', self.asset)
|
self.asset_doc.set_status()
|
||||||
asset.set_status()
|
|
||||||
|
|
||||||
def set_total_value(self):
|
def set_total_value(self):
|
||||||
for item in self.stock_items:
|
for item in self.get('stock_items'):
|
||||||
item.total_value = flt(item.valuation_rate) * flt(item.consumed_quantity)
|
item.total_value = flt(item.valuation_rate) * flt(item.consumed_quantity)
|
||||||
|
|
||||||
def calculate_total_repair_cost(self):
|
def calculate_total_repair_cost(self):
|
||||||
self.total_repair_cost = self.repair_cost
|
self.total_repair_cost = self.repair_cost
|
||||||
if self.stock_consumption:
|
if self.get('stock_items'):
|
||||||
for item in self.stock_items:
|
for item in self.get('stock_items'):
|
||||||
self.total_repair_cost += item.total_value
|
self.total_repair_cost += item.total_value
|
||||||
|
|
||||||
def on_submit(self):
|
def on_submit(self):
|
||||||
self.check_repair_status()
|
self.check_repair_status()
|
||||||
|
|
||||||
if self.stock_consumption or self.capitalize_repair_cost:
|
if self.get('stock_consumption') or self.get('capitalize_repair_cost'):
|
||||||
self.increase_asset_value()
|
self.increase_asset_value()
|
||||||
if self.stock_consumption:
|
if self.get('stock_consumption'):
|
||||||
self.check_for_stock_items_and_warehouse()
|
self.check_for_stock_items_and_warehouse()
|
||||||
self.decrease_stock_quantity()
|
self.decrease_stock_quantity()
|
||||||
if self.capitalize_repair_cost:
|
if self.get('capitalize_repair_cost'):
|
||||||
self.make_gl_entries()
|
self.make_gl_entries()
|
||||||
if frappe.db.get_value('Asset', self.asset, 'calculate_depreciation') and self.increase_in_asset_life:
|
if frappe.db.get_value('Asset', self.asset, 'calculate_depreciation') and self.increase_in_asset_life:
|
||||||
self.modify_depreciation_schedule()
|
self.modify_depreciation_schedule()
|
||||||
|
|
||||||
|
self.asset_doc.flags.ignore_validate_update_after_submit = True
|
||||||
|
self.asset_doc.save()
|
||||||
|
|
||||||
def check_repair_status(self):
|
def check_repair_status(self):
|
||||||
if self.repair_status == "Pending":
|
if self.repair_status == "Pending":
|
||||||
frappe.throw(_("Please update Repair Status."))
|
frappe.throw(_("Please update Repair Status."))
|
||||||
|
|
||||||
def check_for_stock_items_and_warehouse(self):
|
def check_for_stock_items_and_warehouse(self):
|
||||||
if not self.stock_items:
|
if not self.get('stock_items'):
|
||||||
frappe.throw(_("Please enter Stock Items consumed during the Repair."), title=_("Missing Items"))
|
frappe.throw(_("Please enter Stock Items consumed during the Repair."), title=_("Missing Items"))
|
||||||
if not self.warehouse:
|
if not self.warehouse:
|
||||||
frappe.throw(_("Please enter Warehouse from which Stock Items consumed during the Repair were taken."), title=_("Missing Warehouse"))
|
frappe.throw(_("Please enter Warehouse from which Stock Items consumed during the Repair were taken."), title=_("Missing Warehouse"))
|
||||||
|
|
||||||
def increase_asset_value(self):
|
def increase_asset_value(self):
|
||||||
total_value_of_stock_consumed = 0
|
total_value_of_stock_consumed = 0
|
||||||
for item in self.stock_items:
|
if self.get('stock_consumption'):
|
||||||
total_value_of_stock_consumed += item.total_value
|
for item in self.get('stock_items'):
|
||||||
|
total_value_of_stock_consumed += item.total_value
|
||||||
|
|
||||||
asset = frappe.get_doc('Asset', self.asset)
|
if self.asset_doc.calculate_depreciation:
|
||||||
asset.flags.ignore_validate_update_after_submit = True
|
for row in self.asset_doc.finance_books:
|
||||||
if asset.calculate_depreciation:
|
|
||||||
for row in asset.finance_books:
|
|
||||||
row.value_after_depreciation += total_value_of_stock_consumed
|
row.value_after_depreciation += total_value_of_stock_consumed
|
||||||
|
|
||||||
if self.capitalize_repair_cost:
|
if self.capitalize_repair_cost:
|
||||||
row.value_after_depreciation += self.repair_cost
|
row.value_after_depreciation += self.repair_cost
|
||||||
asset.save()
|
|
||||||
|
|
||||||
def decrease_stock_quantity(self):
|
def decrease_stock_quantity(self):
|
||||||
stock_entry = frappe.get_doc({
|
stock_entry = frappe.get_doc({
|
||||||
@ -82,7 +80,7 @@ class AssetRepair(AccountsController):
|
|||||||
"company": self.company
|
"company": self.company
|
||||||
})
|
})
|
||||||
|
|
||||||
for stock_item in self.stock_items:
|
for stock_item in self.get('stock_items'):
|
||||||
stock_entry.append('items', {
|
stock_entry.append('items', {
|
||||||
"s_warehouse": self.warehouse,
|
"s_warehouse": self.warehouse,
|
||||||
"item_code": stock_item.item,
|
"item_code": stock_item.item,
|
||||||
@ -122,7 +120,7 @@ class AssetRepair(AccountsController):
|
|||||||
}, item=self)
|
}, item=self)
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.stock_consumption:
|
if self.get('stock_consumption'):
|
||||||
# creating GL Entries for each row in Stock Items based on the Stock Entry created for it
|
# creating GL Entries for each row in Stock Items based on the Stock Entry created for it
|
||||||
stock_entry = frappe.get_doc('Stock Entry', self.stock_entry)
|
stock_entry = frappe.get_doc('Stock Entry', self.stock_entry)
|
||||||
for item in stock_entry.items:
|
for item in stock_entry.items:
|
||||||
@ -159,18 +157,13 @@ class AssetRepair(AccountsController):
|
|||||||
return gl_entries
|
return gl_entries
|
||||||
|
|
||||||
def modify_depreciation_schedule(self):
|
def modify_depreciation_schedule(self):
|
||||||
asset = frappe.get_doc('Asset', self.asset)
|
for row in self.asset_doc.finance_books:
|
||||||
asset.flags.ignore_validate_update_after_submit = True
|
|
||||||
for row in asset.finance_books:
|
|
||||||
row.total_number_of_depreciations += self.increase_in_asset_life/row.frequency_of_depreciation
|
row.total_number_of_depreciations += self.increase_in_asset_life/row.frequency_of_depreciation
|
||||||
|
|
||||||
asset.flags.increase_in_asset_life = False
|
self.asset_doc.flags.increase_in_asset_life = False
|
||||||
extra_months = self.increase_in_asset_life % row.frequency_of_depreciation
|
extra_months = self.increase_in_asset_life % row.frequency_of_depreciation
|
||||||
if extra_months != 0:
|
if extra_months != 0:
|
||||||
self.calculate_last_schedule_date(asset, row, extra_months)
|
self.calculate_last_schedule_date(self.asset_doc, row, extra_months)
|
||||||
|
|
||||||
asset.prepare_depreciation_data()
|
|
||||||
asset.save()
|
|
||||||
|
|
||||||
# to help modify depreciation schedule when increase_in_asset_life is not a multiple of frequency_of_depreciation
|
# to help modify depreciation schedule when increase_in_asset_life is not a multiple of frequency_of_depreciation
|
||||||
def calculate_last_schedule_date(self, asset, row, extra_months):
|
def calculate_last_schedule_date(self, asset, row, extra_months):
|
||||||
|
|||||||
@ -105,7 +105,9 @@ class TestAssetRepair(unittest.TestCase):
|
|||||||
initial_num_of_depreciations = num_of_depreciations(asset)
|
initial_num_of_depreciations = num_of_depreciations(asset)
|
||||||
create_asset_repair(asset= asset, capitalize_repair_cost = 1, submit = 1)
|
create_asset_repair(asset= asset, capitalize_repair_cost = 1, submit = 1)
|
||||||
asset.reload()
|
asset.reload()
|
||||||
|
|
||||||
self.assertEqual((initial_num_of_depreciations + 1), num_of_depreciations(asset))
|
self.assertEqual((initial_num_of_depreciations + 1), num_of_depreciations(asset))
|
||||||
|
self.assertEqual(asset.schedules[-1].accumulated_depreciation_amount, asset.finance_books[0].value_after_depreciation)
|
||||||
|
|
||||||
def num_of_depreciations(asset):
|
def num_of_depreciations(asset):
|
||||||
return asset.finance_books[0].total_number_of_depreciations
|
return asset.finance_books[0].total_number_of_depreciations
|
||||||
@ -126,7 +128,8 @@ def create_asset_repair(**args):
|
|||||||
"asset_name": asset.asset_name,
|
"asset_name": asset.asset_name,
|
||||||
"failure_date": nowdate(),
|
"failure_date": nowdate(),
|
||||||
"description": "Test Description",
|
"description": "Test Description",
|
||||||
"repair_cost": 0
|
"repair_cost": 0,
|
||||||
|
"company": asset.company
|
||||||
})
|
})
|
||||||
|
|
||||||
if args.stock_consumption:
|
if args.stock_consumption:
|
||||||
@ -142,7 +145,7 @@ def create_asset_repair(**args):
|
|||||||
asset_repair.save()
|
asset_repair.save()
|
||||||
except frappe.DuplicateEntryError:
|
except frappe.DuplicateEntryError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if args.submit:
|
if args.submit:
|
||||||
asset_repair.repair_status = "Completed"
|
asset_repair.repair_status = "Completed"
|
||||||
asset_repair.cost_center = "_Test Cost Center - _TC"
|
asset_repair.cost_center = "_Test Cost Center - _TC"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user