Skip to content

Commit 7098240

Browse files
[IMP] l10n_ar_tax: add monthly cron to clean old padron files.
1) Add _cron_clean_old_padron_files method to delete old records. 2) Create cron in data (noupdate="1") with monthly execution. 3) Add test_cron_deletes_only_old_padrons test ( odoo-argentina/l10n_ar_tax/tests/test_padron_cleanup_cron.py ) to verify old records are deleted after cron execution. Task Adhoc side: 65316 closes #1375 Signed-off-by: Katherine Zaoral - kz (#l10n) <kz@adhoc.com.ar>
1 parent e55cd7d commit 7098240

5 files changed

Lines changed: 77 additions & 0 deletions

File tree

l10n_ar_tax/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"category": "Accounting & Finance",
2727
"data": [
2828
"security/ir.model.access.csv",
29+
"data/ir_cron_data.xml",
2930
"views/report_withholding_certificate_templates.xml",
3031
"views/account_payment_view.xml",
3132
"views/res_company_jurisdiction_padron_view.xml",

l10n_ar_tax/data/ir_cron_data.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<odoo noupdate="1">
3+
4+
<record id="cron_clean_old_padron_files" model="ir.cron">
5+
<field name="name">l10n_ar_tax: Clean old padron files</field>
6+
<field name="model_id" ref="model_res_company_jurisdiction_padron"/>
7+
<field name="state">code</field>
8+
<field name="code">model._cron_clean_old_padron_files()</field>
9+
<field name="interval_number">1</field>
10+
<field name="interval_type">months</field>
11+
</record>
12+
13+
</odoo>

l10n_ar_tax/models/res_company_jurisdiction_padron.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,19 @@ def _get_aliquot(self, partner):
174174
else:
175175
aliquot_ret = aliquot and aliquot.replace(",", ".")
176176
return nro, aliquot_ret, aliquot_per
177+
178+
@api.model
179+
def _cron_clean_old_padron_files(self):
180+
"""Delete old padron files to reduce storage usage."""
181+
last_year_date = fields.Date.subtract(fields.Date.start_of(fields.Date.context_today(self), "month"), years=1)
182+
if old_padrons := self.search(
183+
[
184+
("l10n_ar_padron_to_date", "<", last_year_date),
185+
]
186+
):
187+
_logger.info(
188+
"Padron cleanup: deleting %s old padrones older than %s",
189+
len(old_padrons),
190+
last_year_date,
191+
)
192+
old_padrons.unlink()

l10n_ar_tax/tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from . import test_arba
2+
from . import test_padron_cleanup_cron
23
from . import test_payment_receiptbook_and_withholding
34
from . import test_payment_withholding_validation
45
from . import test_withholding_thresholds
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import base64
2+
3+
from dateutil.relativedelta import relativedelta
4+
from odoo import fields
5+
from odoo.tests import common
6+
7+
8+
class TestPadronCleanupCron(common.TransactionCase):
9+
@classmethod
10+
def setUpClass(cls):
11+
super().setUpClass()
12+
cls.padron_model = cls.env["res.company.jurisdiction.padron"]
13+
cls.state_arba = cls.env.ref("base.state_ar_b")
14+
cls.state_santa_fe = cls.env.ref("base.state_ar_s")
15+
cls.company = cls.env.company
16+
cls.dummy_file = base64.b64encode(b"dummy padron").decode()
17+
18+
def _create_padron(self, state, to_date):
19+
return self.padron_model.create(
20+
{
21+
"company_id": self.company.id,
22+
"state_id": state.id,
23+
"file_padron": self.dummy_file,
24+
"filename": "padron.txt",
25+
"l10n_ar_padron_from_date": to_date + relativedelta(days=-30),
26+
"l10n_ar_padron_to_date": to_date,
27+
}
28+
)
29+
30+
def test_cron_deletes_only_old_padrons(self):
31+
"""Create 2 old and 2 new padrones, then verify the cron deletes only the 2 old ones."""
32+
threshold_date = fields.Date.start_of(fields.Date.context_today(self.padron_model), "month") + relativedelta(
33+
years=-1
34+
)
35+
36+
old_arba = self._create_padron(self.state_arba, threshold_date + relativedelta(days=-1))
37+
old_santa_fe = self._create_padron(self.state_santa_fe, threshold_date + relativedelta(days=-10))
38+
new_arba = self._create_padron(self.state_arba, threshold_date + relativedelta(days=1))
39+
new_santa_fe = self._create_padron(self.state_santa_fe, threshold_date + relativedelta(days=30))
40+
41+
self.padron_model._cron_clean_old_padron_files()
42+
43+
self.assertFalse(old_arba.exists())
44+
self.assertFalse(old_santa_fe.exists())
45+
self.assertTrue(new_arba.exists())
46+
self.assertTrue(new_santa_fe.exists())

0 commit comments

Comments
 (0)