From b764b9cd7f9b5b9cf7f098f73688fe734d9ce820 Mon Sep 17 00:00:00 2001 From: Vincent Van Rossem Date: Mon, 21 Dec 2020 16:22:03 +0100 Subject: [PATCH] [12.0][ADD] emc_loan: ir_cron - change state to `closed` when `subscription_end_date` <= today --- easy_my_coop_loan/__manifest__.py | 1 + easy_my_coop_loan/data/ir_cron_data.xml | 13 +++++++++++++ easy_my_coop_loan/models/loan.py | 16 ++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 easy_my_coop_loan/data/ir_cron_data.xml diff --git a/easy_my_coop_loan/__manifest__.py b/easy_my_coop_loan/__manifest__.py index 97d24b7..c3517a6 100644 --- a/easy_my_coop_loan/__manifest__.py +++ b/easy_my_coop_loan/__manifest__.py @@ -17,6 +17,7 @@ "data": [ "security/ir.model.access.csv", "data/actions.xml", + "data/ir_cron_data.xml", "data/mail_template_data.xml", "views/loan_view.xml", "views/partner_view.xml", diff --git a/easy_my_coop_loan/data/ir_cron_data.xml b/easy_my_coop_loan/data/ir_cron_data.xml new file mode 100644 index 0000000..a692696 --- /dev/null +++ b/easy_my_coop_loan/data/ir_cron_data.xml @@ -0,0 +1,13 @@ + + + + Loan Issue: Check Subscription End Date + + code + model._cron_check_subscription_end_date() + 1 + days + -1 + + + diff --git a/easy_my_coop_loan/models/loan.py b/easy_my_coop_loan/models/loan.py index 0e686c5..2f47880 100644 --- a/easy_my_coop_loan/models/loan.py +++ b/easy_my_coop_loan/models/loan.py @@ -226,3 +226,19 @@ class LoanIssue(models.Model): "hasn't been implemented yet" ) ) + + def _cron_check_subscription_end_date(self): + today = fields.Date.today() + loans_to_close = self.search( + [("state", "!=", "closed"), ("subscription_end_date", "<=", today)] + ) + for loan in loans_to_close: + try: + loan.action_close() + self.env.cr.commit() + _logger.debug("Loan: '%s' - state: '%s'" % (loan, loan.state)) + except Exception: + _logger.exception( + "An exception occured while closing loan: '%s'" % (loan) + ) + self.env.cr.rollback()