Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T0024 - Automatic reminder for invalid partners #1663

Draft
wants to merge 16 commits into
base: 14.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions partner_communication_switzerland/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Compassion CH Partner Communications
This module adds all Sponsorship communications for Compassion
Switzerland.

- Adds SMS mass sending with 939 services
- Adds SMS mass sending with 939 services

**Table of contents**

Expand Down Expand Up @@ -60,7 +60,7 @@ Authors
Contributors
------------

- Emanuel Cino <ecino@compassion.ch>
- Emanuel Cino <ecino@compassion.ch>

Maintainers
-----------
Expand Down
2 changes: 2 additions & 0 deletions partner_communication_switzerland/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"data/form_data.xml",
"data/sponsorship_planned_emails.xml",
"data/tax_receipt_emails.xml",
"data/auto_reminder_archive_partners.xml",
"data/auto_reminder_archive_partners_cron.xml",
"data/manual_emails.xml",
"data/communication_config.xml",
"data/sponsorship_communications_cron.xml",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<odoo>
<data noupdate="1">
<record
id="auto_reminder_archive_partners_template"
model="mail.template"
>
<field
name="name"
>Automatic reminder to archive invalid partners (T0024)</field>
<field
name="model_id"
ref="partner_communication.model_partner_communication_job"
/>
<field name="email_from">&lt;compassion@compassion.ch&gt;</field>
<field name="reply_to">sds@compassion.ch</field>
<field name="use_default_to" eval="True" />
<field name="subject">Reminder: Archive invalid partners</field>
<field name="body_html" type="html">
<div>
<b>The following partners meet the criteria (see <a
href="/web#action=521&amp;active_id=895&amp;cids=1&amp;id=600&amp;menu_id=2029&amp;model=project.task&amp;view_type=form"
>T0024</a>) and are considered invalid : </b>
<span style="font-family: 'miller';">
% for partner in ctx.get('extra_email_data'):
<p><a
href="/web#id=${partner.id}&amp;model=res.partner&amp;view_type=form"
>Ref ${partner.ref} - ${partner.name}</a></p>
% endfor
</span>
</div>
</field>
</record>
</data>
</odoo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<odoo>
<data noupdate="1">
<record id="auto_reminder_archive_partners_cron" model="ir.cron">
<field
name="name"
>Automatic reminder to archive invalid partners (T0024)</field>
<field name="interval_number">1</field>
<field name="interval_type">weeks</field>
<field name="numbercall">-1</field>
<field name="model_id" ref="model_res_partner" />
<field name="state">code</field>
<field
name="code"
>model.cron_auto_reminder_archive_partners()</field>
<field name="active" eval="True" />
</record>
</data>
</odoo>
14 changes: 14 additions & 0 deletions partner_communication_switzerland/data/communication_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,19 @@
<field name="model_id" ref="model_recurring_contract" />
<field name="send_mode">physical</field>
</record>
<record
id="auto_reminder_archive_partners_config"
model="partner.communication.config"
>
<field
name="name"
>Automatic reminder to archive invalid partners (T0024)</field>
<field
name="email_template_id"
ref="auto_reminder_archive_partners_template"
/>
<field name="model_id" ref="model_res_partner" />
<field name="send_mode">digital</field>
</record>
</data>
</odoo>
117 changes: 113 additions & 4 deletions partner_communication_switzerland/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
##############################################################################
import logging
import uuid
from datetime import date
from datetime import date, datetime, timedelta
from typing import List

from dateutil.relativedelta import relativedelta

Expand Down Expand Up @@ -217,9 +218,9 @@ def compute_inverse_no_physical_letter(self):
if no_physical_letters:
vals = {
"nbmag": "no_mag" if self.nbmag == "no_mag" else "email",
"tax_certificate": "no"
if self.tax_certificate == "no"
else "only_email",
"tax_certificate": (
"no" if self.tax_certificate == "no" else "only_email"
),
"calendar": False,
"sponsorship_anniversary_card": False,
}
Expand Down Expand Up @@ -286,6 +287,114 @@ def _compute_plural(self):
)
)

def find_potential_partners_to_archive(self) -> List["ResPartner"]:
"""Finds the list of partners which meet certain criteria and could be archived.
This is used to generate the `auto_reminder_archive_partners_template` email.

Returns:
List['ResPartner']:
partners which meet the criteria for potential archiving.
"""
# Search for partners who do not have a full address (missing street,
# city, state, zip, and country) and who have invalid email addresses,
# no active sponsorships, but are still marked as active partners.
partners = self.search(
[
("street", "=", False),
("city", "=", False),
("city_id", "=", False),
("state_id", "=", False),
("zip", "=", False),
("zip_id", "=", False),
("country_id", "=", False), # No Full Address
("invalid_mail", "!=", ""), # Invalid Email Address
("has_sponsorships", "=", False), # No active sponsorships
("active", "=", True),
]
)

end_result = [] # List to store partners who meet the criteria
for partner in partners:
date_sponsorships = [] # List to store last paid sponsorship dates
date_donations = [] # List to store last paid donation dates
diff_sponsorships = timedelta(
days=0
) # Default time difference for sponsorships
diff_donations = timedelta(days=0) # Default time difference for donations
sponsorship_ids = (
partner.sponsorship_ids
) # Get all sponsorship contracts for the partner
donations_ids = (
partner.other_contract_ids
) # Get all other contracts (donations) for the partner

# Check if the partner has sponsorship or donation contracts
if sponsorship_ids or donations_ids:
for contract in sponsorship_ids:
if contract.last_paid_invoice_date:
date_sponsorships.append(contract.last_paid_invoice_date)
for contract in donations_ids:
if contract.last_paid_invoice_date:
date_donations.append(contract.last_paid_invoice_date)

# If there are any sponsorship or donation dates, calculate the
# difference from today
if date_sponsorships or date_donations:
if date_sponsorships:
diff_sponsorships = date.today() - max(date_sponsorships)
if date_donations:
diff_donations = date.today() - max(date_donations)
else:
# If no sponsorships or donations, calculate the difference from
# the partner creation date
diff_sponsorships = datetime.now() - partner.create_date
diff_donations = datetime.now() - partner.create_date

# Add partner to the result if both last sponsorship and donation
# were more than the specified number of days ago Sponsorship: more
# than 5 years (1825 days), Donations: more than 3 years (1095 days)
if diff_sponsorships > timedelta(days=1825) and diff_donations > timedelta(
days=1095
):
end_result.append(partner)

return end_result

@api.model
def cron_auto_reminder_archive_partners(self):
"""Function called by a cron job in order to remind SDS to archive
invalid partners."""
reminder_receiver = (
self.env["res.partner"].sudo().search([("email", "=", "sds@compassion.ch")])
)
config = self.env.ref(
"partner_communication_switzerland.auto_reminder_archive_partners_config"
)

partners_to_archive = reminder_receiver.find_potential_partners_to_archive()
if len(partners_to_archive) > 0:
comm_vals = {
"config_id": config.id,
"partner_id": reminder_receiver.id,
"show_signature": True,
"print_subject": False,
"auto_send": True,
"send_mode": "digital", # Force sending by email
}
# Add the partners to archive to the context to avoid recomputing it
# in the template
self.with_context({"extra_email_data": partners_to_archive}).env[
"partner.communication.job"
].create(comm_vals)
_logger.info("Sent reminder to archive invalid partners")
else:
_logger.info(
"""Did not send reminder to archive invalid partners because
there are currently no invalid partners"""
)

return True

@api.model
def generate_tax_receipts(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.
Despite the name, some widely supported CSS2 features are used.

See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
Expand Down Expand Up @@ -274,7 +275,7 @@
margin-left: 2em ;
margin-right: 2em }

pre.code .ln { color: grey; } /* line numbers */
pre.code .ln { color: gray; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
Expand All @@ -300,7 +301,7 @@
span.pre {
white-space: pre }

span.problematic {
span.problematic, pre.problematic {
color: red }

span.section-subtitle {
Expand Down
Loading