From 5eb84f65ef482f253d80e0f70bb33d4f1c24357e Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 25 Jan 2023 12:38:17 +0530 Subject: [PATCH 01/23] ci: documentation helper (backport #33757) (#33798) ci: documentation helper (#33757) refactor: documentation helper (cherry picked from commit d155042edd0ff1cb0df97313d317678d1c81f0a9) Co-authored-by: Raffael Meyer <14891507+barredterra@users.noreply.github.com> --- .github/helper/documentation.py | 99 ++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/.github/helper/documentation.py b/.github/helper/documentation.py index 378983e95f2..83346045f89 100644 --- a/.github/helper/documentation.py +++ b/.github/helper/documentation.py @@ -3,52 +3,71 @@ import requests from urllib.parse import urlparse -docs_repos = [ - "frappe_docs", - "erpnext_documentation", +WEBSITE_REPOS = [ "erpnext_com", "frappe_io", ] +DOCUMENTATION_DOMAINS = [ + "docs.erpnext.com", + "frappeframework.com", +] -def uri_validator(x): - result = urlparse(x) - return all([result.scheme, result.netloc, result.path]) -def docs_link_exists(body): - for line in body.splitlines(): - for word in line.split(): - if word.startswith('http') and uri_validator(word): - parsed_url = urlparse(word) - if parsed_url.netloc == "github.com": - parts = parsed_url.path.split('/') - if len(parts) == 5 and parts[1] == "frappe" and parts[2] in docs_repos: - return True - elif parsed_url.netloc == "docs.erpnext.com": - return True +def is_valid_url(url: str) -> bool: + parts = urlparse(url) + return all((parts.scheme, parts.netloc, parts.path)) + + +def is_documentation_link(word: str) -> bool: + if not word.startswith("http") or not is_valid_url(word): + return False + + parsed_url = urlparse(word) + if parsed_url.netloc in DOCUMENTATION_DOMAINS: + return True + + if parsed_url.netloc == "github.com": + parts = parsed_url.path.split("/") + if len(parts) == 5 and parts[1] == "frappe" and parts[2] in WEBSITE_REPOS: + return True + + return False + + +def contains_documentation_link(body: str) -> bool: + return any( + is_documentation_link(word) + for line in body.splitlines() + for word in line.split() + ) + + +def check_pull_request(number: str) -> "tuple[int, str]": + response = requests.get(f"https://api.github.com/repos/frappe/erpnext/pulls/{number}") + if not response.ok: + return 1, "Pull Request Not Found! ⚠️" + + payload = response.json() + title = (payload.get("title") or "").lower().strip() + head_sha = (payload.get("head") or {}).get("sha") + body = (payload.get("body") or "").lower() + + if ( + not title.startswith("feat") + or not head_sha + or "no-docs" in body + or "backport" in body + ): + return 0, "Skipping documentation checks... 🏃" + + if contains_documentation_link(body): + return 0, "Documentation Link Found. You're Awesome! 🎉" + + return 1, "Documentation Link Not Found! ⚠️" if __name__ == "__main__": - pr = sys.argv[1] - response = requests.get("https://api.github.com/repos/frappe/erpnext/pulls/{}".format(pr)) - - if response.ok: - payload = response.json() - title = (payload.get("title") or "").lower().strip() - head_sha = (payload.get("head") or {}).get("sha") - body = (payload.get("body") or "").lower() - - if (title.startswith("feat") - and head_sha - and "no-docs" not in body - and "backport" not in body - ): - if docs_link_exists(body): - print("Documentation Link Found. You're Awesome! 🎉") - - else: - print("Documentation Link Not Found! ⚠️") - sys.exit(1) - - else: - print("Skipping documentation checks... 🏃") + exit_code, message = check_pull_request(sys.argv[1]) + print(message) + sys.exit(exit_code) From 440e16dc10e61fbd2674d83d3023795fe0780037 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Wed, 25 Jan 2023 17:35:08 +0530 Subject: [PATCH 02/23] chore: remove broken translation [skip ci] --- erpnext/translations/zh.csv | 1 - 1 file changed, 1 deletion(-) diff --git a/erpnext/translations/zh.csv b/erpnext/translations/zh.csv index ea2b7f125eb..228ec39ddde 100644 --- a/erpnext/translations/zh.csv +++ b/erpnext/translations/zh.csv @@ -3537,7 +3537,6 @@ Quality Feedback Template,质量反馈模板, Rules for applying different promotional schemes.,适用不同促销计划的规则。, Shift,转移, Show {0},显示{0}, -"Special Characters except '-', '#', '.', '/', '{{' and '}}' not allowed in naming series {0}",命名系列中不允许使用除"-", "#", "।", "/", "{{" 和 "}}"之外的特殊字符 {0}, Target Details,目标细节, {0} already has a Parent Procedure {1}.,{0}已有父程序{1}。, API,应用程序界面, From f7eabcafde23e02b1db7209549462abbbf4aafde Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 25 Jan 2023 17:37:57 +0530 Subject: [PATCH 03/23] fix: use correct filter name in `item_query` (backport #33814) (#33816) fix: use correct filter name in `item_query` (#33814) (cherry picked from commit da323cbb4010d25b599dcdde9e9178bd17b0412e) Co-authored-by: Daizy Modi --- erpnext/stock/doctype/quality_inspection/quality_inspection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/stock/doctype/quality_inspection/quality_inspection.py b/erpnext/stock/doctype/quality_inspection/quality_inspection.py index 9321c2c166b..2a9f091bd09 100644 --- a/erpnext/stock/doctype/quality_inspection/quality_inspection.py +++ b/erpnext/stock/doctype/quality_inspection/quality_inspection.py @@ -221,7 +221,7 @@ class QualityInspection(Document): def item_query(doctype, txt, searchfield, start, page_len, filters): from frappe.desk.reportview import get_match_cond - from_doctype = cstr(filters.get("doctype")) + from_doctype = cstr(filters.get("from")) if not from_doctype or not frappe.db.exists("DocType", from_doctype): return [] From fc0ba2b9c82fa9f2bbbbee998dc7df70636a7caa Mon Sep 17 00:00:00 2001 From: anandbaburajan Date: Thu, 26 Jan 2023 13:15:01 +0530 Subject: [PATCH 04/23] chore: remove broken translation (cherry picked from commit 21f425660dccddb1bbab3ae81918fefff515b78e) --- erpnext/translations/zh_tw.csv | 1 - 1 file changed, 1 deletion(-) diff --git a/erpnext/translations/zh_tw.csv b/erpnext/translations/zh_tw.csv index 313908fed2c..1b7e18637d3 100644 --- a/erpnext/translations/zh_tw.csv +++ b/erpnext/translations/zh_tw.csv @@ -3311,7 +3311,6 @@ Quality Feedback Template,質量反饋模板, Rules for applying different promotional schemes.,適用不同促銷計劃的規則。, Shift,轉移, Show {0},顯示{0}, -"Special Characters except '-', '#', '.', '/', '{{' and '}}' not allowed in naming series {0}",命名系列中不允許使用除 "-", "#", "।", "/", "{{" 和 "}}"之外的特殊字符 {0}, Target Details,目標細節, API,API, Annual,年刊, From f124dd31120b52a4de15e8ba612ba82e5ed521d0 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 26 Jan 2023 20:26:50 +0530 Subject: [PATCH 05/23] fix: GST Category validation broken for pos unregistered customer who dont have address. (#33800) fix: GST Category validation broken for pos unregistered customer who dont have address. (#33800) * fix: GST Category validation is given for pos customer (cherry picked from commit c5ca8d74c4aa400e4123bdfe62009b261e43b1bf) Co-authored-by: Vishal Dhayagude --- erpnext/accounts/party.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/party.py b/erpnext/accounts/party.py index 67cf6443538..bfe73f02cdc 100644 --- a/erpnext/accounts/party.py +++ b/erpnext/accounts/party.py @@ -211,7 +211,13 @@ def set_address_details( else: party_details.update(get_company_address(company)) - if doctype and doctype in ["Delivery Note", "Sales Invoice", "Sales Order", "Quotation"]: + if doctype and doctype in [ + "Delivery Note", + "Sales Invoice", + "Sales Order", + "Quotation", + "POS Invoice", + ]: if party_details.company_address: party_details.update( get_fetch_values(doctype, "company_address", party_details.company_address) From 58c3e16fec68da1e784800daee76e0f13edd4ed7 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 26 Jan 2023 20:26:21 +0530 Subject: [PATCH 06/23] fix: disfuctional cost center filter on Journal Entries (#33815) * fix: missing cost_center filter for journal entries * test: cost center filter on invoices, journals and payments (cherry picked from commit b31b850db8775029714f41d0c40bc43f81f57bd5) --- .../payment_reconciliation.py | 4 ++ .../test_payment_reconciliation.py | 67 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py index 12c0b7a7bf7..154fdc039d4 100644 --- a/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py +++ b/erpnext/accounts/doctype/payment_reconciliation/payment_reconciliation.py @@ -69,6 +69,10 @@ class PaymentReconciliation(Document): def get_jv_entries(self): condition = self.get_conditions() + + if self.get("cost_center"): + condition += f" and t2.cost_center = '{self.cost_center}' " + dr_or_cr = ( "credit_in_account_currency" if erpnext.get_party_account_type(self.party_type) == "Receivable" diff --git a/erpnext/accounts/doctype/payment_reconciliation/test_payment_reconciliation.py b/erpnext/accounts/doctype/payment_reconciliation/test_payment_reconciliation.py index 2ba90b4da9f..00e3934f10c 100644 --- a/erpnext/accounts/doctype/payment_reconciliation/test_payment_reconciliation.py +++ b/erpnext/accounts/doctype/payment_reconciliation/test_payment_reconciliation.py @@ -747,6 +747,73 @@ class TestPaymentReconciliation(FrappeTestCase): self.assertEqual(len(pr.get("invoices")), 0) self.assertEqual(len(pr.get("payments")), 0) + def test_cost_center_filter_on_vouchers(self): + """ + Test Cost Center filter is applied on Invoices, Payment Entries and Journals + """ + transaction_date = nowdate() + rate = 100 + + # 'Main - PR' Cost Center + si1 = self.create_sales_invoice( + qty=1, rate=rate, posting_date=transaction_date, do_not_submit=True + ) + si1.cost_center = self.main_cc.name + si1.submit() + + pe1 = self.create_payment_entry(posting_date=transaction_date, amount=rate) + pe1.cost_center = self.main_cc.name + pe1 = pe1.save().submit() + + je1 = self.create_journal_entry(self.bank, self.debit_to, 100, transaction_date) + je1.accounts[0].cost_center = self.main_cc.name + je1.accounts[1].cost_center = self.main_cc.name + je1.accounts[1].party_type = "Customer" + je1.accounts[1].party = self.customer + je1 = je1.save().submit() + + # 'Sub - PR' Cost Center + si2 = self.create_sales_invoice( + qty=1, rate=rate, posting_date=transaction_date, do_not_submit=True + ) + si2.cost_center = self.sub_cc.name + si2.submit() + + pe2 = self.create_payment_entry(posting_date=transaction_date, amount=rate) + pe2.cost_center = self.sub_cc.name + pe2 = pe2.save().submit() + + je2 = self.create_journal_entry(self.bank, self.debit_to, 100, transaction_date) + je2.accounts[0].cost_center = self.sub_cc.name + je2.accounts[1].cost_center = self.sub_cc.name + je2.accounts[1].party_type = "Customer" + je2.accounts[1].party = self.customer + je2 = je2.save().submit() + + pr = self.create_payment_reconciliation() + pr.cost_center = self.main_cc.name + + pr.get_unreconciled_entries() + + # check PR tool output + self.assertEqual(len(pr.get("invoices")), 1) + self.assertEqual(pr.get("invoices")[0].get("invoice_number"), si1.name) + self.assertEqual(len(pr.get("payments")), 2) + payment_vouchers = [x.get("reference_name") for x in pr.get("payments")] + self.assertCountEqual(payment_vouchers, [pe1.name, je1.name]) + + # Change cost center + pr.cost_center = self.sub_cc.name + + pr.get_unreconciled_entries() + + # check PR tool output + self.assertEqual(len(pr.get("invoices")), 1) + self.assertEqual(pr.get("invoices")[0].get("invoice_number"), si2.name) + self.assertEqual(len(pr.get("payments")), 2) + payment_vouchers = [x.get("reference_name") for x in pr.get("payments")] + self.assertCountEqual(payment_vouchers, [je2.name, pe2.name]) + def make_customer(customer_name, currency=None): if not frappe.db.exists("Customer", customer_name): From ac2ebfbf5942dc127aec8ed05bba864007a89258 Mon Sep 17 00:00:00 2001 From: Devin Slauenwhite Date: Thu, 26 Jan 2023 16:53:05 -0500 Subject: [PATCH 07/23] perf: show update items dialog (cherry picked from commit a835c1a4187cda0a995b26408600c8f261ae0790) --- erpnext/public/js/utils.js | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/erpnext/public/js/utils.js b/erpnext/public/js/utils.js index d37b7bb43b3..51dcd64d9dd 100755 --- a/erpnext/public/js/utils.js +++ b/erpnext/public/js/utils.js @@ -491,7 +491,20 @@ erpnext.utils.update_child_items = function(opts) { const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`); const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision; - this.data = []; + this.data = frm.doc[opts.child_docname].map((d) => { + return { + "docname": d.name, + "name": d.name, + "item_code": d.item_code, + "delivery_date": d.delivery_date, + "schedule_date": d.schedule_date, + "conversion_factor": d.conversion_factor, + "qty": d.qty, + "rate": d.rate, + "uom": d.uom + } + }); + const fields = [{ fieldtype:'Data', fieldname:"docname", @@ -588,7 +601,7 @@ erpnext.utils.update_child_items = function(opts) { }) } - const dialog = new frappe.ui.Dialog({ + new frappe.ui.Dialog({ title: __("Update Items"), fields: [ { @@ -624,24 +637,7 @@ erpnext.utils.update_child_items = function(opts) { refresh_field("items"); }, primary_action_label: __('Update') - }); - - frm.doc[opts.child_docname].forEach(d => { - dialog.fields_dict.trans_items.df.data.push({ - "docname": d.name, - "name": d.name, - "item_code": d.item_code, - "delivery_date": d.delivery_date, - "schedule_date": d.schedule_date, - "conversion_factor": d.conversion_factor, - "qty": d.qty, - "rate": d.rate, - "uom": d.uom - }); - this.data = dialog.fields_dict.trans_items.df.data; - dialog.fields_dict.trans_items.grid.refresh(); - }) - dialog.show(); + }).show(); } erpnext.utils.map_current_doc = function(opts) { From 5d4967ceeefe9504f421d2230a08bb23f57eca5b Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sat, 28 Jan 2023 10:28:06 +0530 Subject: [PATCH 08/23] fix(patch): validation error on cost center allocation migration (#33835) fix(patch): validation error on cost center allocation migration If Distributed cost centers have GL postings on patch run date, patch failes with valiation error. (cherry picked from commit de10f2dc005e5aa493d08bfd1047a36b8040f46f) Co-authored-by: ruthra kumar --- .../cost_center_allocation/cost_center_allocation.py | 7 ++++++- erpnext/patches/v14_0/migrate_cost_center_allocations.py | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py b/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py index d25016fe596..54ffe21a152 100644 --- a/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py +++ b/erpnext/accounts/doctype/cost_center_allocation/cost_center_allocation.py @@ -28,9 +28,14 @@ class InvalidDateError(frappe.ValidationError): class CostCenterAllocation(Document): + def __init__(self, *args, **kwargs): + super(CostCenterAllocation, self).__init__(*args, **kwargs) + self._skip_from_date_validation = False + def validate(self): self.validate_total_allocation_percentage() - self.validate_from_date_based_on_existing_gle() + if not self._skip_from_date_validation: + self.validate_from_date_based_on_existing_gle() self.validate_backdated_allocation() self.validate_main_cost_center() self.validate_child_cost_centers() diff --git a/erpnext/patches/v14_0/migrate_cost_center_allocations.py b/erpnext/patches/v14_0/migrate_cost_center_allocations.py index 3bd26933bad..48f4e6d9893 100644 --- a/erpnext/patches/v14_0/migrate_cost_center_allocations.py +++ b/erpnext/patches/v14_0/migrate_cost_center_allocations.py @@ -18,9 +18,11 @@ def create_new_cost_center_allocation_records(cc_allocations): cca = frappe.new_doc("Cost Center Allocation") cca.main_cost_center = main_cc cca.valid_from = today() + cca._skip_from_date_validation = True for child_cc, percentage in allocations.items(): cca.append("allocation_percentages", ({"cost_center": child_cc, "percentage": percentage})) + cca.save() cca.submit() From b98d3514ab1bef6a6a26f2b5c0a3c9dde0f0b6fc Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Sat, 28 Jan 2023 13:51:33 +0530 Subject: [PATCH 09/23] fix: item rate not fetching (cherry picked from commit 0d7f98b496ac49872654b1a9c47183f337544ce1) --- erpnext/public/js/controllers/transaction.js | 4 ++++ erpnext/stock/doctype/item/item.js | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index b1bf8b24047..b6fab544220 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -1691,6 +1691,10 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe var me = this; var valid = true; + if (frappe.flags.ignore_company_party_validation) { + return valid; + } + $.each(["company", "customer"], function(i, fieldname) { if(frappe.meta.has_field(me.frm.doc.doctype, fieldname) && me.frm.doc.doctype != "Purchase Order") { if (!me.frm.doc[fieldname]) { diff --git a/erpnext/stock/doctype/item/item.js b/erpnext/stock/doctype/item/item.js index e61f0f514e3..5bcb05aa988 100644 --- a/erpnext/stock/doctype/item/item.js +++ b/erpnext/stock/doctype/item/item.js @@ -894,6 +894,12 @@ function open_form(frm, doctype, child_doctype, parentfield) { new_child_doc.uom = frm.doc.stock_uom; new_child_doc.description = frm.doc.description; - frappe.ui.form.make_quick_entry(doctype, null, null, new_doc); + frappe.run_serially([ + () => frappe.ui.form.make_quick_entry(doctype, null, null, new_doc), + () => { + frappe.flags.ignore_company_party_validation = true; + frappe.model.trigger("item_code", frm.doc.name, new_child_doc); + } + ]) }); } From 0fcf364aaada1d08bef06369ccbdcabfb5ad2935 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sun, 29 Jan 2023 17:08:30 +0530 Subject: [PATCH 10/23] fix: double salutation on quotation print (#33834) fix: double salutation on quotation print (#33834) 'lead_name' always has salutation. (cherry picked from commit f270880735cca4992bc6ced158c5c26a92cd9179) Co-authored-by: ruthra kumar --- erpnext/crm/doctype/lead/lead.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/crm/doctype/lead/lead.py b/erpnext/crm/doctype/lead/lead.py index 0d12499771e..5d3437c4a88 100644 --- a/erpnext/crm/doctype/lead/lead.py +++ b/erpnext/crm/doctype/lead/lead.py @@ -393,7 +393,7 @@ def get_lead_details(lead, posting_date=None, company=None): { "territory": lead.territory, "customer_name": lead.company_name or lead.lead_name, - "contact_display": " ".join(filter(None, [lead.salutation, lead.lead_name])), + "contact_display": " ".join(filter(None, [lead.lead_name])), "contact_email": lead.email_id, "contact_mobile": lead.mobile_no, "contact_phone": lead.phone, From a0e1ee0450478782a2ca320fbc3080703c954b2a Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Sun, 29 Jan 2023 21:26:06 +0530 Subject: [PATCH 11/23] fix: Ignore linked JE on JE cancellation (#33852) fix: Ignore linked JE on JE cancellation (#33852) (cherry picked from commit 428b099f63b88fb3ff37b04be4e91d645b33e041) Co-authored-by: Deepesh Garg --- erpnext/accounts/doctype/journal_entry/journal_entry.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js index 30a32015f5d..21f27aedc51 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.js +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js @@ -8,7 +8,7 @@ frappe.provide("erpnext.journal_entry"); frappe.ui.form.on("Journal Entry", { setup: function(frm) { frm.add_fetch("bank_account", "account", "account"); - frm.ignore_doctypes_on_cancel_all = ['Sales Invoice', 'Purchase Invoice']; + frm.ignore_doctypes_on_cancel_all = ['Sales Invoice', 'Purchase Invoice', 'Journal Entry']; }, refresh: function(frm) { From f5bde9cf6d5ec61b3e8c011c9dfe289dba54fb9a Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Thu, 19 Jan 2023 09:39:43 +0000 Subject: [PATCH 12/23] fix(gp): fetch buying amount from dn related to so (cherry picked from commit e8e20da78eeece6b5fad895c6aa6f50f57e561e4) --- .../report/gross_profit/gross_profit.py | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index 646fe85bac9..2d86020e972 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -655,10 +655,36 @@ class GrossProfitGenerator(object): return self.calculate_buying_amount_from_sle( row, my_sle, parenttype, parent, item_row, item_code ) + elif row.sales_order and row.so_detail: + incoming_amount = self.get_buying_amount_from_so_dn(row.sales_order, row.so_detail, item_code) + if incoming_amount: + return incoming_amount else: return flt(row.qty) * self.get_average_buying_rate(row, item_code) - return 0.0 + return flt(row.qty) * self.get_average_buying_rate(row, item_code) + + def get_buying_amount_from_so_dn(self, sales_order, so_detail, item_code): + from frappe.query_builder.functions import Sum + delivery_note = frappe.qb.DocType("Delivery Note") + delivery_note_item = frappe.qb.DocType("Delivery Note Item") + + query = ( + frappe.qb.from_(delivery_note) + .inner_join(delivery_note_item) + .on(delivery_note.name == delivery_note_item.parent) + .select( + Sum(delivery_note_item.incoming_rate * delivery_note_item.stock_qty) + ) + .where(delivery_note.docstatus == 1) + .where(delivery_note_item.item_code == item_code) + .where(delivery_note_item.against_sales_order == sales_order) + .where(delivery_note_item.so_detail == so_detail) + .groupby(delivery_note_item.item_code) + ) + + incoming_amount = query.run() + return flt(incoming_amount[0][0]) if incoming_amount else 0 def get_average_buying_rate(self, row, item_code): args = row @@ -760,7 +786,8 @@ class GrossProfitGenerator(object): `tabSales Invoice`.territory, `tabSales Invoice Item`.item_code, `tabSales Invoice Item`.item_name, `tabSales Invoice Item`.description, `tabSales Invoice Item`.warehouse, `tabSales Invoice Item`.item_group, - `tabSales Invoice Item`.brand, `tabSales Invoice Item`.dn_detail, + `tabSales Invoice Item`.brand, `tabSales Invoice Item`.so_detail, + `tabSales Invoice Item`.sales_order, `tabSales Invoice Item`.dn_detail, `tabSales Invoice Item`.delivery_note, `tabSales Invoice Item`.stock_qty as qty, `tabSales Invoice Item`.base_net_rate, `tabSales Invoice Item`.base_net_amount, `tabSales Invoice Item`.name as "item_row", `tabSales Invoice`.is_return, From a659208ed1d051761c6722ee764ce6d7a9be3881 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Thu, 19 Jan 2023 10:25:05 +0000 Subject: [PATCH 13/23] chore: linting issue (cherry picked from commit ef90e249312dce414c6dbe088b8880c47403b3cc) --- erpnext/accounts/report/gross_profit/gross_profit.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index 2d86020e972..8ce7d1d3368 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -666,6 +666,7 @@ class GrossProfitGenerator(object): def get_buying_amount_from_so_dn(self, sales_order, so_detail, item_code): from frappe.query_builder.functions import Sum + delivery_note = frappe.qb.DocType("Delivery Note") delivery_note_item = frappe.qb.DocType("Delivery Note Item") @@ -673,9 +674,7 @@ class GrossProfitGenerator(object): frappe.qb.from_(delivery_note) .inner_join(delivery_note_item) .on(delivery_note.name == delivery_note_item.parent) - .select( - Sum(delivery_note_item.incoming_rate * delivery_note_item.stock_qty) - ) + .select(Sum(delivery_note_item.incoming_rate * delivery_note_item.stock_qty)) .where(delivery_note.docstatus == 1) .where(delivery_note_item.item_code == item_code) .where(delivery_note_item.against_sales_order == sales_order) From 7a793ea58888801e1852756f102455255958be5b Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Mon, 30 Jan 2023 10:35:43 +0000 Subject: [PATCH 14/23] feat(gp): test for inv and dn related via so (cherry picked from commit 1f6ab86a659d9745fa8971656a54c19bc784de2b) --- .../report/gross_profit/test_gross_profit.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/erpnext/accounts/report/gross_profit/test_gross_profit.py b/erpnext/accounts/report/gross_profit/test_gross_profit.py index fa11a41df4a..332be8ef54e 100644 --- a/erpnext/accounts/report/gross_profit/test_gross_profit.py +++ b/erpnext/accounts/report/gross_profit/test_gross_profit.py @@ -302,3 +302,79 @@ class TestGrossProfit(FrappeTestCase): columns, data = execute(filters=filters) self.assertGreater(len(data), 0) + + def test_order_connected_dn_and_inv(self): + from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order + """ + Test gp calculation when invoice and delivery note aren't directly connected. + SO -- INV + | + DN + """ + se = make_stock_entry( + company=self.company, + item_code=self.item, + target=self.warehouse, + qty=3, + basic_rate=100, + do_not_submit=True, + ) + item = se.items[0] + se.append( + "items", + { + "item_code": item.item_code, + "s_warehouse": item.s_warehouse, + "t_warehouse": item.t_warehouse, + "qty": 10, + "basic_rate": 200, + "conversion_factor": item.conversion_factor or 1.0, + "transfer_qty": flt(item.qty) * (flt(item.conversion_factor) or 1.0), + "serial_no": item.serial_no, + "batch_no": item.batch_no, + "cost_center": item.cost_center, + "expense_account": item.expense_account, + }, + ) + se = se.save().submit() + + so = make_sales_order( + customer=self.customer, + company=self.company, + warehouse=self.warehouse, + item=self.item, + qty=4, + do_not_save=False, + do_not_submit=False, + ) + + from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note, make_sales_invoice + make_delivery_note(so.name).submit() + sinv = make_sales_invoice(so.name).submit() + + filters = frappe._dict( + company=self.company, from_date=nowdate(), to_date=nowdate(), group_by="Invoice" + ) + + columns, data = execute(filters=filters) + + # Without Delivery Note, buying rate should be 150 + expected_entry = { + "parent_invoice": sinv.name, + "currency": "INR", + "sales_invoice": self.item, + "customer": self.customer, + "posting_date": frappe.utils.datetime.date.fromisoformat(nowdate()), + "item_code": self.item, + "item_name": self.item, + "warehouse": "Stores - _GP", + "qty": 4.0, + "avg._selling_rate": 100.0, + "valuation_rate": 125.0, + "selling_amount": 400.0, + "buying_amount": 500.0, + "gross_profit": -100.0, + "gross_profit_%": -25.0, + } + gp_entry = [x for x in data if x.parent_invoice == sinv.name] + self.assertDictContainsSubset(expected_entry, gp_entry[0]) \ No newline at end of file From 5caa9e2240c90a6140c1ef94ff3d3a8c82dae808 Mon Sep 17 00:00:00 2001 From: Dany Robert Date: Mon, 30 Jan 2023 10:45:08 +0000 Subject: [PATCH 15/23] chore: linting issues (cherry picked from commit d69c8393692fa8ecc08ff7f10ca66a1c5a9f3b0f) --- .../accounts/report/gross_profit/test_gross_profit.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/report/gross_profit/test_gross_profit.py b/erpnext/accounts/report/gross_profit/test_gross_profit.py index 332be8ef54e..21681bef5b5 100644 --- a/erpnext/accounts/report/gross_profit/test_gross_profit.py +++ b/erpnext/accounts/report/gross_profit/test_gross_profit.py @@ -305,6 +305,7 @@ class TestGrossProfit(FrappeTestCase): def test_order_connected_dn_and_inv(self): from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order + """ Test gp calculation when invoice and delivery note aren't directly connected. SO -- INV @@ -348,7 +349,11 @@ class TestGrossProfit(FrappeTestCase): do_not_submit=False, ) - from erpnext.selling.doctype.sales_order.sales_order import make_delivery_note, make_sales_invoice + from erpnext.selling.doctype.sales_order.sales_order import ( + make_delivery_note, + make_sales_invoice, + ) + make_delivery_note(so.name).submit() sinv = make_sales_invoice(so.name).submit() @@ -357,8 +362,6 @@ class TestGrossProfit(FrappeTestCase): ) columns, data = execute(filters=filters) - - # Without Delivery Note, buying rate should be 150 expected_entry = { "parent_invoice": sinv.name, "currency": "INR", @@ -377,4 +380,4 @@ class TestGrossProfit(FrappeTestCase): "gross_profit_%": -25.0, } gp_entry = [x for x in data if x.parent_invoice == sinv.name] - self.assertDictContainsSubset(expected_entry, gp_entry[0]) \ No newline at end of file + self.assertDictContainsSubset(expected_entry, gp_entry[0]) From 4586806ed12fc3817563c3b2cf660b611dd50815 Mon Sep 17 00:00:00 2001 From: anandbaburajan Date: Mon, 30 Jan 2023 21:25:04 +0530 Subject: [PATCH 16/23] fix: disposal_was_made_on_original_schedule_date --- erpnext/assets/doctype/asset/asset.py | 7 +------ erpnext/assets/doctype/asset/depreciation.py | 11 ++++++++++- erpnext/assets/doctype/asset/test_asset.py | 7 +------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 6aaac6cea44..648cbb5b2be 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -26,6 +26,7 @@ from erpnext.accounts.general_ledger import make_reverse_gl_entries from erpnext.assets.doctype.asset.depreciation import ( get_depreciation_accounts, get_disposal_account_and_cost_center, + is_last_day_of_the_month, ) from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account from erpnext.controllers.accounts_controller import AccountsController @@ -1083,12 +1084,6 @@ def get_total_days(date, frequency): return date_diff(date, period_start_date) -def is_last_day_of_the_month(date): - last_day_of_the_month = get_last_day(date) - - return getdate(last_day_of_the_month) == getdate(date) - - @erpnext.allow_regional def get_depreciation_amount(asset, depreciable_value, row): if row.depreciation_method in ("Straight Line", "Manual"): diff --git a/erpnext/assets/doctype/asset/depreciation.py b/erpnext/assets/doctype/asset/depreciation.py index 92d040d4ebb..220534b9388 100644 --- a/erpnext/assets/doctype/asset/depreciation.py +++ b/erpnext/assets/doctype/asset/depreciation.py @@ -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_last_day, getdate, nowdate, today from frappe.utils.data import get_link_to_form from frappe.utils.user import get_users_with_role @@ -372,6 +372,9 @@ def disposal_was_made_on_original_schedule_date(asset, schedule, row, posting_da finance_book.depreciation_start_date, row * cint(finance_book.frequency_of_depreciation) ) + if is_last_day_of_the_month(finance_book.depreciation_start_date): + orginal_schedule_date = get_last_day(orginal_schedule_date) + if orginal_schedule_date == posting_date_of_disposal: return True return False @@ -575,3 +578,9 @@ def get_value_after_depreciation_on_disposal_date(asset, disposal_date, finance_ ) else: return flt(asset_doc.value_after_depreciation) + + +def is_last_day_of_the_month(date): + last_day_of_the_month = get_last_day(date) + + return getdate(last_day_of_the_month) == getdate(date) diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py index 5a31ca0e2d7..0d6c54c6b1b 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -22,6 +22,7 @@ from erpnext.assets.doctype.asset.asset import ( update_maintenance_status, ) from erpnext.assets.doctype.asset.depreciation import ( + is_last_day_of_the_month, post_depreciation_entries, restore_asset, scrap_asset, @@ -1577,9 +1578,3 @@ def set_depreciation_settings_in_company(company=None): def enable_cwip_accounting(asset_category, enable=1): frappe.db.set_value("Asset Category", asset_category, "enable_cwip_accounting", enable) - - -def is_last_day_of_the_month(dt): - last_day_of_the_month = get_last_day(dt) - - return getdate(dt) == getdate(last_day_of_the_month) From 3f87a0eceb76810538b2da661d6b26e61728e4b2 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 31 Jan 2023 08:49:43 +0530 Subject: [PATCH 17/23] ci: bump isort to 5.12.0 (backport #33875) (#33879) ci: bump isort to 5.12.0 (#33875) [skip ci] (cherry picked from commit 2bad86d8d8220f4eb729c3a918ce0002f6b87693) Co-authored-by: Deepesh Garg --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 73aae33e936..d70977c07e2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,8 +32,8 @@ repos: - id: black additional_dependencies: ['click==8.0.4'] - - repo: https://github.com/timothycrosley/isort - rev: 5.9.1 + - repo: https://github.com/PyCQA/isort + rev: 5.12.0 hooks: - id: isort exclude: ".*setup.py$" From 1b79837e1b34994475d0c23b873de48b1849a5b8 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 31 Jan 2023 09:19:46 +0530 Subject: [PATCH 18/23] chore: Resize numeric and date columns (#33858) chore: Resize numeric and date columns (#33858) --- erpnext/accounts/report/general_ledger/general_ledger.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/report/general_ledger/general_ledger.py b/erpnext/accounts/report/general_ledger/general_ledger.py index ab93893861b..745b6dfbdce 100644 --- a/erpnext/accounts/report/general_ledger/general_ledger.py +++ b/erpnext/accounts/report/general_ledger/general_ledger.py @@ -524,7 +524,7 @@ def get_columns(filters): "options": "GL Entry", "hidden": 1, }, - {"label": _("Posting Date"), "fieldname": "posting_date", "fieldtype": "Date", "width": 90}, + {"label": _("Posting Date"), "fieldname": "posting_date", "fieldtype": "Date", "width": 100}, { "label": _("Account"), "fieldname": "account", @@ -536,13 +536,13 @@ def get_columns(filters): "label": _("Debit ({0})").format(currency), "fieldname": "debit", "fieldtype": "Float", - "width": 100, + "width": 130, }, { "label": _("Credit ({0})").format(currency), "fieldname": "credit", "fieldtype": "Float", - "width": 100, + "width": 130, }, { "label": _("Balance ({0})").format(currency), From 44692e9b575e0af44d6d928a227e595953f71216 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 31 Jan 2023 09:20:10 +0530 Subject: [PATCH 19/23] fix: Lead to customer creation (#33859) fix: Lead to customer creation (#33859) (cherry picked from commit faecf3ee405abec3edf95fd9bf7790608852c2b5) Co-authored-by: Deepesh Garg --- erpnext/crm/doctype/lead/lead.py | 1 + 1 file changed, 1 insertion(+) diff --git a/erpnext/crm/doctype/lead/lead.py b/erpnext/crm/doctype/lead/lead.py index 5d3437c4a88..6443928bf66 100644 --- a/erpnext/crm/doctype/lead/lead.py +++ b/erpnext/crm/doctype/lead/lead.py @@ -285,6 +285,7 @@ def _make_customer(source_name, target_doc=None, ignore_permissions=False): "contact_no": "phone_1", "fax": "fax_1", }, + "field_no_map": ["disabled"], } }, target_doc, From 5605f1e3efe931a5240e8878a911a48bf62713c6 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 31 Jan 2023 09:20:33 +0530 Subject: [PATCH 20/23] fix: Amount validation in Payment Request against Purchase Order (#33855) fix: Amount validation in Payment Request against Purchase Order (#33855) fix: Amount validation in Payment Request againt Purchase Order (cherry picked from commit a34a1f8fd26849993f4d66df740273fabd066d55) Co-authored-by: Deepesh Garg --- erpnext/accounts/doctype/payment_request/payment_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index d8c00116140..68c2a32715c 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -51,7 +51,7 @@ class PaymentRequest(Document): if existing_payment_request_amount: ref_doc = frappe.get_doc(self.reference_doctype, self.reference_name) - if hasattr(ref_doc, "order_type") and getattr(ref_doc, "order_type") != "Shopping Cart": + if not hasattr(ref_doc, "order_type") or getattr(ref_doc, "order_type") != "Shopping Cart": ref_amount = get_amount(ref_doc, self.payment_account) if existing_payment_request_amount + flt(self.grand_total) > ref_amount: From f54e8625f6d10bb8f2547314d1a4b16411c02047 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 31 Jan 2023 09:21:59 +0530 Subject: [PATCH 21/23] fix: Currency symbol for tax withholding net total field (#33850) * fix: Currency symbol for tax withholding net total field (#33850) --- .../accounts/doctype/purchase_invoice/purchase_invoice.json | 6 ++++-- erpnext/buying/doctype/purchase_order/purchase_order.json | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json index c5c8d00a292..629a0ffb582 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.json @@ -1426,6 +1426,7 @@ }, { "default": "0", + "depends_on": "apply_tds", "fieldname": "tax_withholding_net_total", "fieldtype": "Currency", "hidden": 1, @@ -1435,12 +1436,13 @@ "read_only": 1 }, { + "depends_on": "apply_tds", "fieldname": "base_tax_withholding_net_total", "fieldtype": "Currency", "hidden": 1, "label": "Base Tax Withholding Net Total", "no_copy": 1, - "options": "currency", + "options": "Company:company:default_currency", "print_hide": 1, "read_only": 1 }, @@ -1554,7 +1556,7 @@ "idx": 204, "is_submittable": 1, "links": [], - "modified": "2022-12-14 18:37:38.142688", + "modified": "2023-01-28 19:18:56.586321", "modified_by": "Administrator", "module": "Accounts", "name": "Purchase Invoice", diff --git a/erpnext/buying/doctype/purchase_order/purchase_order.json b/erpnext/buying/doctype/purchase_order/purchase_order.json index e1dd6797815..29afc8476e4 100644 --- a/erpnext/buying/doctype/purchase_order/purchase_order.json +++ b/erpnext/buying/doctype/purchase_order/purchase_order.json @@ -1221,6 +1221,7 @@ }, { "default": "0", + "depends_on": "apply_tds", "fieldname": "tax_withholding_net_total", "fieldtype": "Currency", "hidden": 1, @@ -1230,12 +1231,13 @@ "read_only": 1 }, { + "depends_on": "apply_tds", "fieldname": "base_tax_withholding_net_total", "fieldtype": "Currency", "hidden": 1, "label": "Base Tax Withholding Net Total", "no_copy": 1, - "options": "currency", + "options": "Company:company:default_currency", "print_hide": 1, "read_only": 1 }, @@ -1269,7 +1271,7 @@ "idx": 105, "is_submittable": 1, "links": [], - "modified": "2022-12-25 18:08:59.074182", + "modified": "2023-01-28 18:59:16.322824", "modified_by": "Administrator", "module": "Buying", "name": "Purchase Order", From 868c8d65aec951248944698463af62bbd53a38eb Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 31 Jan 2023 10:15:56 +0530 Subject: [PATCH 22/23] fix: Fetch commission rate from sales partner (#33851) fix: Fetch commission rate from sales partner (#33851) --- erpnext/accounts/doctype/sales_invoice/sales_invoice.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json index 67cd867b97c..0c8ddeb2bae 100644 --- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.json +++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.json @@ -1770,6 +1770,8 @@ "width": "50%" }, { + "fetch_from": "sales_partner.commission_rate", + "fetch_if_empty": 1, "fieldname": "commission_rate", "fieldtype": "Float", "hide_days": 1, @@ -2124,7 +2126,7 @@ "link_fieldname": "consolidated_invoice" } ], - "modified": "2022-12-12 18:34:33.409895", + "modified": "2023-01-28 19:45:47.538163", "modified_by": "Administrator", "module": "Accounts", "name": "Sales Invoice", From f487eae28e0ec5fe77cfaff8b1edd16a98992e7f Mon Sep 17 00:00:00 2001 From: Anand Baburajan Date: Tue, 31 Jan 2023 10:59:38 +0530 Subject: [PATCH 23/23] fix: manual depr entry not updating asset value [v14] (#33788) * fix: get value_after_depreciation functions and manual depr entry not updating asset value * fix: reflect manual depr entry in chart and fixed asset register * fix: add linked JEs in asset connections * chore: add test * chore: add patch and fix some things * chore: get depreciation_expense_account properly * chore: fix patch * chore: fix patch * chore: fix patch again * chore: rename var in patch * fix: filter assets with finance books properly * chore: refactor get_value_after_depreciation * chore: use dict format for filters in assets_linked_to_fb * chore: refactor update_asset_value * chore: sort manual depr entries --- .../doctype/journal_entry/journal_entry.py | 67 +++++++++++++--- erpnext/assets/doctype/asset/asset.js | 40 ++++++---- erpnext/assets/doctype/asset/asset.json | 8 +- erpnext/assets/doctype/asset/asset.py | 72 +++++++++++++----- erpnext/assets/doctype/asset/depreciation.py | 12 +-- erpnext/assets/doctype/asset/test_asset.py | 31 ++++++++ .../asset_capitalization.py | 10 +-- .../doctype/asset_repair/test_asset_repair.py | 17 ++--- .../asset_value_adjustment.js | 2 +- .../asset_value_adjustment.py | 16 ++-- .../test_asset_value_adjustment.py | 8 +- .../fixed_asset_register.py | 76 ++++++++++++++----- erpnext/patches.txt | 1 + ...ate_asset_value_for_manual_depr_entries.py | 38 ++++++++++ 14 files changed, 298 insertions(+), 100 deletions(-) create mode 100644 erpnext/patches/v14_0/update_asset_value_for_manual_depr_entries.py diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.py b/erpnext/accounts/doctype/journal_entry/journal_entry.py index 88b030cae3d..b9507b73e29 100644 --- a/erpnext/accounts/doctype/journal_entry/journal_entry.py +++ b/erpnext/accounts/doctype/journal_entry/journal_entry.py @@ -81,6 +81,7 @@ class JournalEntry(AccountsController): self.check_credit_limit() self.make_gl_entries() self.update_advance_paid() + self.update_asset_value() self.update_inter_company_jv() self.update_invoice_discounting() @@ -225,6 +226,34 @@ class JournalEntry(AccountsController): for d in to_remove: self.remove(d) + def update_asset_value(self): + if self.voucher_type != "Depreciation Entry": + return + + processed_assets = [] + + for d in self.get("accounts"): + if ( + d.reference_type == "Asset" and d.reference_name and d.reference_name not in processed_assets + ): + processed_assets.append(d.reference_name) + + asset = frappe.db.get_value( + "Asset", d.reference_name, ["calculate_depreciation", "value_after_depreciation"], as_dict=1 + ) + + if asset.calculate_depreciation: + continue + + depr_value = d.debit or d.credit + + frappe.db.set_value( + "Asset", + d.reference_name, + "value_after_depreciation", + asset.value_after_depreciation - depr_value, + ) + def update_inter_company_jv(self): if ( self.voucher_type == "Inter Company Journal Entry" @@ -283,19 +312,39 @@ class JournalEntry(AccountsController): d.db_update() def unlink_asset_reference(self): + if self.voucher_type != "Depreciation Entry": + return + + processed_assets = [] + for d in self.get("accounts"): - if d.reference_type == "Asset" and d.reference_name: + if ( + d.reference_type == "Asset" and d.reference_name and d.reference_name not in processed_assets + ): + processed_assets.append(d.reference_name) + asset = frappe.get_doc("Asset", d.reference_name) - for s in asset.get("schedules"): - if s.journal_entry == self.name: - s.db_set("journal_entry", None) - idx = cint(s.finance_book_id) or 1 - finance_books = asset.get("finance_books")[idx - 1] - finance_books.value_after_depreciation += s.depreciation_amount - finance_books.db_update() + if asset.calculate_depreciation: + for s in asset.get("schedules"): + if s.journal_entry == self.name: + s.db_set("journal_entry", None) - asset.set_status() + idx = cint(s.finance_book_id) or 1 + finance_books = asset.get("finance_books")[idx - 1] + finance_books.value_after_depreciation += s.depreciation_amount + finance_books.db_update() + + asset.set_status() + else: + depr_value = d.debit or d.credit + + frappe.db.set_value( + "Asset", + d.reference_name, + "value_after_depreciation", + asset.value_after_depreciation + depr_value, + ) def unlink_inter_company_jv(self): if ( diff --git a/erpnext/assets/doctype/asset/asset.js b/erpnext/assets/doctype/asset/asset.js index 8940b28ca6c..276cc925208 100644 --- a/erpnext/assets/doctype/asset/asset.js +++ b/erpnext/assets/doctype/asset/asset.js @@ -205,7 +205,7 @@ frappe.ui.form.on('Asset', { }) }, - setup_chart: function(frm) { + setup_chart: async function(frm) { if(frm.doc.finance_books.length > 1) { return } @@ -223,20 +223,34 @@ frappe.ui.form.on('Asset', { flt(frm.doc.opening_accumulated_depreciation)); } - $.each(frm.doc.schedules || [], function(i, v) { - x_intervals.push(v.schedule_date); - var asset_value = flt(frm.doc.gross_purchase_amount) - flt(v.accumulated_depreciation_amount); - if(v.journal_entry) { - last_depreciation_date = v.schedule_date; - asset_values.push(asset_value); - } else { - if (in_list(["Scrapped", "Sold"], frm.doc.status)) { - asset_values.push(null); + if(frm.doc.calculate_depreciation) { + $.each(frm.doc.schedules || [], function(i, v) { + x_intervals.push(v.schedule_date); + var asset_value = flt(frm.doc.gross_purchase_amount) - flt(v.accumulated_depreciation_amount); + if(v.journal_entry) { + last_depreciation_date = v.schedule_date; + asset_values.push(asset_value); } else { - asset_values.push(asset_value) + if (in_list(["Scrapped", "Sold"], frm.doc.status)) { + asset_values.push(null); + } else { + asset_values.push(asset_value) + } } - } - }); + }); + } else { + let depr_entries = (await frappe.call({ + method: "get_manual_depreciation_entries", + doc: frm.doc, + })).message; + + $.each(depr_entries || [], function(i, v) { + x_intervals.push(v.posting_date); + last_depreciation_date = v.posting_date; + let last_asset_value = asset_values[asset_values.length - 1] + asset_values.push(last_asset_value - v.value); + }); + } if(in_list(["Scrapped", "Sold"], frm.doc.status)) { x_intervals.push(frm.doc.disposal_date); diff --git a/erpnext/assets/doctype/asset/asset.json b/erpnext/assets/doctype/asset/asset.json index 5e6fe14ac04..d581f52153e 100644 --- a/erpnext/assets/doctype/asset/asset.json +++ b/erpnext/assets/doctype/asset/asset.json @@ -519,9 +519,15 @@ "group": "Value", "link_doctype": "Asset Value Adjustment", "link_fieldname": "asset" + }, + { + "group": "Journal Entry", + "link_doctype": "Journal Entry", + "link_fieldname": "reference_name", + "table_fieldname": "accounts" } ], - "modified": "2023-01-16 23:35:37.423100", + "modified": "2023-01-25 17:45:48.649543", "modified_by": "Administrator", "module": "Assets", "name": "Asset", diff --git a/erpnext/assets/doctype/asset/asset.py b/erpnext/assets/doctype/asset/asset.py index 648cbb5b2be..cd9f3b420d1 100644 --- a/erpnext/assets/doctype/asset/asset.py +++ b/erpnext/assets/doctype/asset/asset.py @@ -244,7 +244,7 @@ class Asset(AccountsController): def _make_depreciation_schedule(self, finance_book, start, date_of_disposal): self.validate_asset_finance_books(finance_book) - value_after_depreciation = self._get_value_after_depreciation(finance_book) + value_after_depreciation = self._get_value_after_depreciation_for_making_schedule(finance_book) finance_book.value_after_depreciation = value_after_depreciation number_of_pending_depreciations = cint(finance_book.total_number_of_depreciations) - cint( @@ -377,8 +377,7 @@ class Asset(AccountsController): for idx, s in enumerate(self.schedules, 1): s.idx = idx - def _get_value_after_depreciation(self, finance_book): - # value_after_depreciation - current Asset value + def _get_value_after_depreciation_for_making_schedule(self, finance_book): if self.docstatus == 1 and finance_book.value_after_depreciation: value_after_depreciation = flt(finance_book.value_after_depreciation) else: @@ -567,7 +566,9 @@ class Asset(AccountsController): if int(d.finance_book_id) not in finance_books: accumulated_depreciation = flt(self.opening_accumulated_depreciation) - value_after_depreciation = flt(self.get_value_after_depreciation(d.finance_book_id)) + value_after_depreciation = flt( + self.get("finance_books")[cint(d.finance_book_id) - 1].value_after_depreciation + ) finance_books.append(int(d.finance_book_id)) depreciation_amount = flt(d.depreciation_amount, d.precision("depreciation_amount")) @@ -592,9 +593,6 @@ class Asset(AccountsController): accumulated_depreciation, d.precision("accumulated_depreciation_amount") ) - def get_value_after_depreciation(self, idx): - return flt(self.get("finance_books")[cint(idx) - 1].value_after_depreciation) - def validate_expected_value_after_useful_life(self): for row in self.get("finance_books"): accumulated_depreciation_after_full_schedule = [ @@ -649,15 +647,20 @@ class Asset(AccountsController): movement.cancel() def delete_depreciation_entries(self): - for d in self.get("schedules"): - if d.journal_entry: - frappe.get_doc("Journal Entry", d.journal_entry).cancel() - d.db_set("journal_entry", None) + if self.calculate_depreciation: + for d in self.get("schedules"): + if d.journal_entry: + frappe.get_doc("Journal Entry", d.journal_entry).cancel() + else: + depr_entries = self.get_manual_depreciation_entries() - self.db_set( - "value_after_depreciation", - (flt(self.gross_purchase_amount) - flt(self.opening_accumulated_depreciation)), - ) + for depr_entry in depr_entries or []: + frappe.get_doc("Journal Entry", depr_entry.name).cancel() + + self.db_set( + "value_after_depreciation", + (flt(self.gross_purchase_amount) - flt(self.opening_accumulated_depreciation)), + ) def set_status(self, status=None): """Get and update status""" @@ -688,6 +691,17 @@ class Asset(AccountsController): status = "Cancelled" return status + def get_value_after_depreciation(self, finance_book=None): + if not self.calculate_depreciation: + return self.value_after_depreciation + + if not finance_book: + return self.get("finance_books")[0].value_after_depreciation + + for row in self.get("finance_books"): + if finance_book == row.finance_book: + return row.value_after_depreciation + def get_default_finance_book_idx(self): if not self.get("default_finance_book") and self.company: self.default_finance_book = erpnext.get_default_finance_book(self.company) @@ -813,6 +827,24 @@ class Asset(AccountsController): make_gl_entries(gl_entries) self.db_set("booked_fixed_asset", 1) + @frappe.whitelist() + def get_manual_depreciation_entries(self): + (_, _, depreciation_expense_account) = get_depreciation_accounts(self) + + gle = frappe.qb.DocType("GL Entry") + + records = ( + frappe.qb.from_(gle) + .select(gle.voucher_no.as_("name"), gle.debit.as_("value"), gle.posting_date) + .where(gle.against_voucher == self.name) + .where(gle.account == depreciation_expense_account) + .where(gle.debit != 0) + .where(gle.is_cancelled == 0) + .orderby(gle.posting_date) + ).run(as_dict=True) + + return records + @frappe.whitelist() def get_depreciation_rate(self, args, on_validate=False): if isinstance(args, str): @@ -857,7 +889,6 @@ def update_maintenance_status(): def make_post_gl_entry(): - asset_categories = frappe.db.get_all("Asset Category", fields=["name", "enable_cwip_accounting"]) for asset_category in asset_categories: @@ -1010,7 +1041,7 @@ def make_journal_entry(asset_name): depreciation_expense_account, ) = get_depreciation_accounts(asset) - depreciation_cost_center, depreciation_series = frappe.db.get_value( + depreciation_cost_center, depreciation_series = frappe.get_cached_value( "Company", asset.company, ["depreciation_cost_center", "series_for_depreciation_entry"] ) depreciation_cost_center = asset.cost_center or depreciation_cost_center @@ -1075,6 +1106,13 @@ def is_cwip_accounting_enabled(asset_category): return cint(frappe.db.get_value("Asset Category", asset_category, "enable_cwip_accounting")) +@frappe.whitelist() +def get_asset_value_after_depreciation(asset_name, finance_book=None): + asset = frappe.get_doc("Asset", asset_name) + + return asset.get_value_after_depreciation(finance_book) + + def get_total_days(date, frequency): period_start_date = add_months(date, cint(frequency) * -1) diff --git a/erpnext/assets/doctype/asset/depreciation.py b/erpnext/assets/doctype/asset/depreciation.py index 220534b9388..196288e9e67 100644 --- a/erpnext/assets/doctype/asset/depreciation.py +++ b/erpnext/assets/doctype/asset/depreciation.py @@ -494,18 +494,8 @@ def get_asset_details(asset, finance_book=None): disposal_account, depreciation_cost_center = get_disposal_account_and_cost_center(asset.company) depreciation_cost_center = asset.cost_center or depreciation_cost_center - idx = 1 - if finance_book: - for d in asset.finance_books: - if d.finance_book == finance_book: - idx = d.idx - break + value_after_depreciation = asset.get_value_after_depreciation(finance_book) - value_after_depreciation = ( - asset.finance_books[idx - 1].value_after_depreciation - if asset.calculate_depreciation - else asset.value_after_depreciation - ) accumulated_depr_amount = flt(asset.gross_purchase_amount) - flt(value_after_depreciation) return ( diff --git a/erpnext/assets/doctype/asset/test_asset.py b/erpnext/assets/doctype/asset/test_asset.py index 0d6c54c6b1b..71f578c6703 100644 --- a/erpnext/assets/doctype/asset/test_asset.py +++ b/erpnext/assets/doctype/asset/test_asset.py @@ -15,6 +15,7 @@ from frappe.utils import ( nowdate, ) +from erpnext.accounts.doctype.journal_entry.test_journal_entry import make_journal_entry from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice from erpnext.assets.doctype.asset.asset import ( make_sales_invoice, @@ -1442,6 +1443,36 @@ class TestDepreciationBasics(AssetSetup): for i, schedule in enumerate(asset.schedules): self.assertEqual(getdate(expected_dates[i]), getdate(schedule.schedule_date)) + def test_manual_depreciation_for_existing_asset(self): + asset = create_asset( + item_code="Macbook Pro", + is_existing_asset=1, + purchase_date="2020-01-30", + available_for_use_date="2020-01-30", + submit=1, + ) + + self.assertEqual(asset.status, "Submitted") + self.assertEqual(asset.get("value_after_depreciation"), 100000) + + jv = make_journal_entry( + "_Test Depreciations - _TC", "_Test Accumulated Depreciations - _TC", 100, save=False + ) + for d in jv.accounts: + d.reference_type = "Asset" + d.reference_name = asset.name + jv.voucher_type = "Depreciation Entry" + jv.insert() + jv.submit() + + asset.reload() + self.assertEqual(asset.get("value_after_depreciation"), 99900) + + jv.cancel() + + asset.reload() + self.assertEqual(asset.get("value_after_depreciation"), 100000) + def create_asset_data(): if not frappe.db.exists("Asset Category", "Computers"): diff --git a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py index 08355f047e5..90b4d7e5834 100644 --- a/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py +++ b/erpnext/assets/doctype/asset_capitalization/asset_capitalization.py @@ -11,6 +11,7 @@ from frappe.utils import cint, flt from six import string_types import erpnext +from erpnext.assets.doctype.asset.asset import get_asset_value_after_depreciation from erpnext.assets.doctype.asset.depreciation import ( depreciate_asset, get_gl_entries_on_asset_disposal, @@ -19,9 +20,6 @@ from erpnext.assets.doctype.asset.depreciation import ( reverse_depreciation_entry_made_after_disposal, ) from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account -from erpnext.assets.doctype.asset_value_adjustment.asset_value_adjustment import ( - get_current_asset_value, -) from erpnext.controllers.stock_controller import StockController from erpnext.setup.doctype.brand.brand import get_brand_defaults from erpnext.setup.doctype.item_group.item_group import get_item_group_defaults @@ -259,7 +257,9 @@ class AssetCapitalization(StockController): for d in self.get("asset_items"): if d.asset: finance_book = d.get("finance_book") or self.get("finance_book") - d.current_asset_value = flt(get_current_asset_value(d.asset, finance_book=finance_book)) + d.current_asset_value = flt( + get_asset_value_after_depreciation(d.asset, finance_book=finance_book) + ) d.asset_value = get_value_after_depreciation_on_disposal_date( d.asset, self.posting_date, finance_book=finance_book ) @@ -696,7 +696,7 @@ def get_consumed_asset_details(args): if args.asset: out.current_asset_value = flt( - get_current_asset_value(args.asset, finance_book=args.finance_book) + get_asset_value_after_depreciation(args.asset, finance_book=args.finance_book) ) out.asset_value = get_value_after_depreciation_on_disposal_date( args.asset, args.posting_date, finance_book=args.finance_book diff --git a/erpnext/assets/doctype/asset_repair/test_asset_repair.py b/erpnext/assets/doctype/asset_repair/test_asset_repair.py index 6599c078231..3adb3516908 100644 --- a/erpnext/assets/doctype/asset_repair/test_asset_repair.py +++ b/erpnext/assets/doctype/asset_repair/test_asset_repair.py @@ -6,7 +6,10 @@ import unittest import frappe from frappe.utils import flt, nowdate -from erpnext.assets.doctype.asset.asset import get_asset_account +from erpnext.assets.doctype.asset.asset import ( + get_asset_account, + get_asset_value_after_depreciation, +) from erpnext.assets.doctype.asset.test_asset import ( create_asset, create_asset_data, @@ -106,20 +109,20 @@ class TestAssetRepair(unittest.TestCase): def test_increase_in_asset_value_due_to_stock_consumption(self): asset = create_asset(calculate_depreciation=1, submit=1) - initial_asset_value = get_asset_value(asset) + initial_asset_value = get_asset_value_after_depreciation(asset.name) asset_repair = create_asset_repair(asset=asset, stock_consumption=1, submit=1) asset.reload() - increase_in_asset_value = get_asset_value(asset) - initial_asset_value + increase_in_asset_value = get_asset_value_after_depreciation(asset.name) - initial_asset_value self.assertEqual(asset_repair.stock_items[0].total_value, increase_in_asset_value) def test_increase_in_asset_value_due_to_repair_cost_capitalisation(self): asset = create_asset(calculate_depreciation=1, submit=1) - initial_asset_value = get_asset_value(asset) + initial_asset_value = get_asset_value_after_depreciation(asset.name) asset_repair = create_asset_repair(asset=asset, capitalize_repair_cost=1, submit=1) asset.reload() - increase_in_asset_value = get_asset_value(asset) - initial_asset_value + increase_in_asset_value = get_asset_value_after_depreciation(asset.name) - initial_asset_value self.assertEqual(asset_repair.repair_cost, increase_in_asset_value) def test_purchase_invoice(self): @@ -244,10 +247,6 @@ class TestAssetRepair(unittest.TestCase): ) -def get_asset_value(asset): - return asset.finance_books[0].value_after_depreciation - - def num_of_depreciations(asset): return asset.finance_books[0].total_number_of_depreciations diff --git a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.js b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.js index 36f510b18ee..ae0e1bda020 100644 --- a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.js +++ b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.js @@ -47,7 +47,7 @@ frappe.ui.form.on('Asset Value Adjustment', { set_current_asset_value: function(frm) { if (frm.doc.asset) { frm.call({ - method: "erpnext.assets.doctype.asset_value_adjustment.asset_value_adjustment.get_current_asset_value", + method: "erpnext.assets.doctype.asset.asset.get_asset_value_after_depreciation", args: { asset: frm.doc.asset, finance_book: frm.doc.finance_book diff --git a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py index 59ab6a910d2..e718472e164 100644 --- a/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py +++ b/erpnext/assets/doctype/asset_value_adjustment/asset_value_adjustment.py @@ -10,7 +10,10 @@ from frappe.utils import cint, date_diff, flt, formatdate, getdate from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( get_checks_for_pl_and_bs_accounts, ) -from erpnext.assets.doctype.asset.asset import get_depreciation_amount +from erpnext.assets.doctype.asset.asset import ( + get_asset_value_after_depreciation, + get_depreciation_amount, +) from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts @@ -42,7 +45,7 @@ class AssetValueAdjustment(Document): def set_current_asset_value(self): if not self.current_asset_value and self.asset: - self.current_asset_value = get_current_asset_value(self.asset, self.finance_book) + self.current_asset_value = get_asset_value_after_depreciation(self.asset, self.finance_book) def make_depreciation_entry(self): asset = frappe.get_doc("Asset", self.asset) @@ -142,12 +145,3 @@ class AssetValueAdjustment(Document): for asset_data in asset.schedules: if not asset_data.journal_entry: asset_data.db_update() - - -@frappe.whitelist() -def get_current_asset_value(asset, finance_book=None): - cond = {"parent": asset, "parenttype": "Asset"} - if finance_book: - cond.update({"finance_book": finance_book}) - - return frappe.db.get_value("Asset Finance Book", cond, "value_after_depreciation") diff --git a/erpnext/assets/doctype/asset_value_adjustment/test_asset_value_adjustment.py b/erpnext/assets/doctype/asset_value_adjustment/test_asset_value_adjustment.py index 62c636624ce..b2aa3958080 100644 --- a/erpnext/assets/doctype/asset_value_adjustment/test_asset_value_adjustment.py +++ b/erpnext/assets/doctype/asset_value_adjustment/test_asset_value_adjustment.py @@ -6,10 +6,8 @@ import unittest import frappe from frappe.utils import add_days, get_last_day, nowdate +from erpnext.assets.doctype.asset.asset import get_asset_value_after_depreciation from erpnext.assets.doctype.asset.test_asset import create_asset_data -from erpnext.assets.doctype.asset_value_adjustment.asset_value_adjustment import ( - get_current_asset_value, -) from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt @@ -43,7 +41,7 @@ class TestAssetValueAdjustment(unittest.TestCase): ) asset_doc.submit() - current_value = get_current_asset_value(asset_doc.name) + current_value = get_asset_value_after_depreciation(asset_doc.name) self.assertEqual(current_value, 100000.0) def test_asset_depreciation_value_adjustment(self): @@ -73,7 +71,7 @@ class TestAssetValueAdjustment(unittest.TestCase): ) asset_doc.submit() - current_value = get_current_asset_value(asset_doc.name) + current_value = get_asset_value_after_depreciation(asset_doc.name) adj_doc = make_asset_value_adjustment( asset=asset_doc.name, current_asset_value=current_value, new_asset_value=50000.0 ) diff --git a/erpnext/assets/report/fixed_asset_register/fixed_asset_register.py b/erpnext/assets/report/fixed_asset_register/fixed_asset_register.py index dd5dfca8a22..5bfd4840d73 100644 --- a/erpnext/assets/report/fixed_asset_register/fixed_asset_register.py +++ b/erpnext/assets/report/fixed_asset_register/fixed_asset_register.py @@ -4,13 +4,16 @@ import frappe from frappe import _ -from frappe.utils import cstr, flt, formatdate, getdate +from frappe.query_builder.functions import Sum +from frappe.utils import cstr, formatdate, getdate from erpnext.accounts.report.financial_statements import ( get_fiscal_year_data, get_period_list, validate_fiscal_year, ) +from erpnext.assets.doctype.asset.asset import get_asset_value_after_depreciation +from erpnext.assets.doctype.asset.depreciation import get_depreciation_accounts def execute(filters=None): @@ -85,6 +88,7 @@ def get_data(filters): "asset_name", "status", "department", + "company", "cost_center", "calculate_depreciation", "purchase_receipt", @@ -98,8 +102,25 @@ def get_data(filters): ] assets_record = frappe.db.get_all("Asset", filters=conditions, fields=fields) + finance_book_filter = ("is", "not set") + if filters.finance_book: + finance_book_filter = ("=", filters.finance_book) + + assets_linked_to_fb = frappe.db.get_all( + doctype="Asset Finance Book", + filters={"finance_book": finance_book_filter}, + pluck="parent", + ) + for asset in assets_record: - asset_value = get_asset_value(asset, filters.finance_book) + if filters.finance_book: + if asset.asset_id not in assets_linked_to_fb: + continue + else: + if asset.calculate_depreciation and asset.asset_id not in assets_linked_to_fb: + continue + + asset_value = get_asset_value_after_depreciation(asset.asset_id, filters.finance_book) row = { "asset_id": asset.asset_id, "asset_name": asset.asset_name, @@ -110,7 +131,7 @@ def get_data(filters): or pi_supplier_map.get(asset.purchase_invoice), "gross_purchase_amount": asset.gross_purchase_amount, "opening_accumulated_depreciation": asset.opening_accumulated_depreciation, - "depreciated_amount": depreciation_amount_map.get(asset.asset_id) or 0.0, + "depreciated_amount": get_depreciation_amount_of_asset(asset, depreciation_amount_map, filters), "available_for_use_date": asset.available_for_use_date, "location": asset.location, "asset_category": asset.asset_category, @@ -122,21 +143,6 @@ def get_data(filters): return data -def get_asset_value(asset, finance_book=None): - if not asset.calculate_depreciation: - return flt(asset.gross_purchase_amount) - flt(asset.opening_accumulated_depreciation) - - 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 Finance Book", - filters=[["parent", "=", asset.asset_id], finance_book_filter], - fieldname="value_after_depreciation", - ) - - def prepare_chart_data(data, filters): labels_values_map = {} date_field = frappe.scrub(filters.date_based_on) @@ -182,6 +188,15 @@ def prepare_chart_data(data, filters): } +def get_depreciation_amount_of_asset(asset, depreciation_amount_map, filters): + if asset.calculate_depreciation: + depr_amount = depreciation_amount_map.get(asset.asset_id) or 0.0 + else: + depr_amount = get_manual_depreciation_amount_of_asset(asset, filters) + + return depr_amount + + def get_finance_book_value_map(filters): date = filters.to_date if filters.filter_based_on == "Date Range" else filters.year_end_date @@ -201,6 +216,31 @@ def get_finance_book_value_map(filters): ) +def get_manual_depreciation_amount_of_asset(asset, filters): + date = filters.to_date if filters.filter_based_on == "Date Range" else filters.year_end_date + + (_, _, depreciation_expense_account) = get_depreciation_accounts(asset) + + gle = frappe.qb.DocType("GL Entry") + + result = ( + frappe.qb.from_(gle) + .select(Sum(gle.debit)) + .where(gle.against_voucher == asset.asset_id) + .where(gle.account == depreciation_expense_account) + .where(gle.debit != 0) + .where(gle.is_cancelled == 0) + .where(gle.posting_date <= date) + ).run() + + if result and result[0] and result[0][0]: + depr_amount = result[0][0] + else: + depr_amount = 0 + + return depr_amount + + def get_purchase_receipt_supplier_map(): return frappe._dict( frappe.db.sql( diff --git a/erpnext/patches.txt b/erpnext/patches.txt index a6c822a1a14..9dcb9c1bcae 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -325,3 +325,4 @@ erpnext.patches.v14_0.setup_clear_repost_logs erpnext.patches.v14_0.create_accounting_dimensions_for_payment_request erpnext.patches.v14_0.update_entry_type_for_journal_entry erpnext.patches.v14_0.change_autoname_for_tax_withheld_vouchers +erpnext.patches.v14_0.update_asset_value_for_manual_depr_entries diff --git a/erpnext/patches/v14_0/update_asset_value_for_manual_depr_entries.py b/erpnext/patches/v14_0/update_asset_value_for_manual_depr_entries.py new file mode 100644 index 00000000000..5d7b5cf19c1 --- /dev/null +++ b/erpnext/patches/v14_0/update_asset_value_for_manual_depr_entries.py @@ -0,0 +1,38 @@ +import frappe +from frappe.query_builder.functions import IfNull, Sum + + +def execute(): + asset = frappe.qb.DocType("Asset") + gle = frappe.qb.DocType("GL Entry") + aca = frappe.qb.DocType("Asset Category Account") + company = frappe.qb.DocType("Company") + + asset_total_depr_value_map = ( + frappe.qb.from_(gle) + .join(asset) + .on(gle.against_voucher == asset.name) + .join(aca) + .on((aca.parent == asset.asset_category) & (aca.company_name == asset.company)) + .join(company) + .on(company.name == asset.company) + .select(Sum(gle.debit).as_("value"), asset.name.as_("asset_name")) + .where( + gle.account == IfNull(aca.depreciation_expense_account, company.depreciation_expense_account) + ) + .where(gle.debit != 0) + .where(gle.is_cancelled == 0) + .where(asset.docstatus == 1) + .where(asset.calculate_depreciation == 0) + .groupby(asset.name) + ) + + frappe.qb.update(asset).join(asset_total_depr_value_map).on( + asset_total_depr_value_map.asset_name == asset.name + ).set( + asset.value_after_depreciation, asset.value_after_depreciation - asset_total_depr_value_map.value + ).where( + asset.docstatus == 1 + ).where( + asset.calculate_depreciation == 0 + ).run()