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 4 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
1 change: 1 addition & 0 deletions partner_communication_switzerland/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"data/form_data.xml",
"data/sponsorship_planned_emails.xml",
"data/tax_receipt_emails.xml",
"data/auto_reminder_archive_contact.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,30 @@
<odoo>
<data noupdate="1">
<record id="auto_reminder_archive_contact_template" model="mail.template">
<field name="name">Archive contact reminder mail</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: Invalid partners</field>
<field name="body_html" type="html">
<div>
<b>The following partners meet the criteria and are considered invalid : </b>
% set partner_info = object.partner_id.get_archive_contact()
<span style="font-family: 'miller';">
% for partner in partner_info:
<p>ID ${partner.id} - ${partner.name}</p>
Gordack marked this conversation as resolved.
Show resolved Hide resolved
% endfor
</span>
</div>
</field>
</record>
</data>
</odoo>
Original file line number Diff line number Diff line change
Expand Up @@ -391,5 +391,11 @@
<field name="model_id" ref="model_recurring_contract" />
<field name="send_mode">physical</field>
</record>
<record id="auto_reminder_archive_contact_config" model="partner.communication.config">
<field name="name">Archive contact reminder</field>
<field name="email_template_id" ref="auto_reminder_archive_contact_template" />
<field name="model_id" ref="model_res_partner" />
<field name="send_mode">digital</field>
</record>
</data>
</odoo>
69 changes: 69 additions & 0 deletions partner_communication_switzerland/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import logging
import uuid
from datetime import date
from datetime import datetime
from datetime import timedelta

from dateutil.relativedelta import relativedelta

Expand Down Expand Up @@ -286,6 +288,73 @@ def _compute_plural(self):
)
)

def get_archive_contact(self):
config = self.env.ref("partner_communication_switzerland.auto_reminder_archive_contact_config")
# 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_contact(self):
reminder_receiver = (
self.env["res.partner"].sudo().search([("email", "=", "sds@compassion.ch")])
)
config = self.env.ref("partner_communication_switzerland.auto_reminder_archive_contact_config")

if reminder_receiver.get_archive_contact():
comm_vals = {
"config_id": config.id,
"partner_id": reminder_receiver.id,
"show_signature": True,
"print_subject": False,
"auto_send": True,
}
self.env["partner.communication.job"].create(comm_vals)

return True
@api.model
def generate_tax_receipts(self):
"""
Expand Down
Loading