From 5949a7ecff0020bb07de586892cc5d1dec44b904 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 1 Dec 2022 15:59:42 +0530 Subject: [PATCH 1/2] fix: default clear repost logs --- erpnext/patches.txt | 1 + .../patches/v14_0/setup_clear_repost_logs.py | 8 ++++++ erpnext/setup/install.py | 8 ++++++ .../repost_item_valuation.py | 14 +++++++++++ .../test_repost_item_valuation.py | 25 +++++++++++++++++++ 5 files changed, 56 insertions(+) create mode 100644 erpnext/patches/v14_0/setup_clear_repost_logs.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 166faf9c18..0aad1d34e5 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -318,3 +318,4 @@ erpnext.patches.v13_0.update_schedule_type_in_loans erpnext.patches.v14_0.create_accounting_dimensions_for_asset_capitalization erpnext.patches.v14_0.update_partial_tds_fields erpnext.patches.v14_0.create_incoterms_and_migrate_shipment +erpnext.patches.v14_0.setup_clear_repost_logs \ No newline at end of file diff --git a/erpnext/patches/v14_0/setup_clear_repost_logs.py b/erpnext/patches/v14_0/setup_clear_repost_logs.py new file mode 100644 index 0000000000..be9ddcab7a --- /dev/null +++ b/erpnext/patches/v14_0/setup_clear_repost_logs.py @@ -0,0 +1,8 @@ +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors +# License: MIT. See LICENSE + +from erpnext.setup.install import setup_log_settings + + +def execute(): + setup_log_settings() diff --git a/erpnext/setup/install.py b/erpnext/setup/install.py index d3b47f985d..1f7dddfb95 100644 --- a/erpnext/setup/install.py +++ b/erpnext/setup/install.py @@ -30,6 +30,7 @@ def after_install(): add_company_to_session_defaults() add_standard_navbar_items() add_app_name() + setup_log_settings() frappe.db.commit() @@ -197,3 +198,10 @@ def add_standard_navbar_items(): def add_app_name(): frappe.db.set_value("System Settings", None, "app_name", "ERPNext") + + +def setup_log_settings(): + log_settings = frappe.get_single("Log Settings") + log_settings.append("logs_to_clear", {"ref_doctype": "Repost Item Valuation", "days": 60}) + + log_settings.save(ignore_permissions=True) diff --git a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py index 8e914e6b80..bbed099da9 100644 --- a/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/repost_item_valuation.py @@ -5,6 +5,8 @@ import frappe from frappe import _ from frappe.exceptions import QueryDeadlockError, QueryTimeoutError from frappe.model.document import Document +from frappe.query_builder import DocType, Interval +from frappe.query_builder.functions import Now from frappe.utils import cint, get_link_to_form, get_weekday, getdate, now, nowtime from frappe.utils.user import get_users_with_role from rq.timeouts import JobTimeoutException @@ -21,6 +23,18 @@ RecoverableErrors = (JobTimeoutException, QueryDeadlockError, QueryTimeoutError) class RepostItemValuation(Document): + @staticmethod + def clear_old_logs(days=None): + days = days or 90 + table = DocType("Repost Item Valuation") + frappe.db.delete( + table, + filters=( + (table.modified < (Now() - Interval(days=days))) + & (table.status.isin(["Completed", "Skipped"])) + ), + ) + def validate(self): self.set_status(write=False) self.reset_field_values() diff --git a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py index f1667757a7..d212693bed 100644 --- a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py @@ -86,6 +86,31 @@ class TestRepostItemValuation(FrappeTestCase, StockTestMixin): msg=f"Exepcted false from : {case}", ) + def test_clear_old_logs(self): + # create 10 logs + for i in range(1, 20): + frappe.get_doc( + doctype="Repost Item Valuation", + item_code="_Test Item", + warehouse="_Test Warehouse - _TC", + based_on="Item and Warehouse", + creation=add_to_date(today(), days=-i * 10), + modified=add_to_date(today(), days=-i * 10), + posting_date=nowdate(), + status="Skipped", + posting_time="00:01:00", + ).insert(ignore_permissions=True) + + logs = frappe.get_all("Repost Item Valuation", filters={"status": "Skipped"}) + self.assertTrue(len(logs) > 10) + + from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import RepostItemValuation + + RepostItemValuation.clear_old_logs(days=1) + + logs = frappe.get_all("Repost Item Valuation", filters={"status": "Skipped"}) + self.assertTrue(len(logs) == 0) + def test_create_item_wise_repost_item_valuation_entries(self): pr = make_purchase_receipt( company="_Test Company with perpetual inventory", From 2cce6f2a340ba7d052b79b79fc676fb0c575b79c Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Fri, 2 Dec 2022 10:58:32 +0530 Subject: [PATCH 2/2] fix: test case --- .../test_repost_item_valuation.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py index d212693bed..96ac4352dc 100644 --- a/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py +++ b/erpnext/stock/doctype/repost_item_valuation/test_repost_item_valuation.py @@ -6,8 +6,7 @@ from unittest.mock import MagicMock, call import frappe from frappe.tests.utils import FrappeTestCase -from frappe.utils import nowdate -from frappe.utils.data import add_to_date, today +from frappe.utils import add_days, add_to_date, now, nowdate, today from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice from erpnext.accounts.utils import repost_gle_for_stock_vouchers @@ -89,18 +88,20 @@ class TestRepostItemValuation(FrappeTestCase, StockTestMixin): def test_clear_old_logs(self): # create 10 logs for i in range(1, 20): - frappe.get_doc( + repost_doc = frappe.get_doc( doctype="Repost Item Valuation", item_code="_Test Item", warehouse="_Test Warehouse - _TC", based_on="Item and Warehouse", - creation=add_to_date(today(), days=-i * 10), - modified=add_to_date(today(), days=-i * 10), posting_date=nowdate(), status="Skipped", posting_time="00:01:00", ).insert(ignore_permissions=True) + repost_doc.load_from_db() + repost_doc.modified = add_days(now(), days=-i * 10) + repost_doc.db_update_all() + logs = frappe.get_all("Repost Item Valuation", filters={"status": "Skipped"}) self.assertTrue(len(logs) > 10)