Skip to content
Open
40 changes: 22 additions & 18 deletions child_compassion/models/compassion_hold.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,24 +486,28 @@ def postpone_no_money_hold(self, additional_text=None):
}
hold.write(hold_vals)

body = (
"The no money hold for child {local_id} was expiring on "
"{old_expiration} and was extended to {new_expiration} "
"({extension_description} extension).{additional_text}"
)
hold.message_post(
body=_(body.format(**values)),
subject=_("No money hold extension"),
subtype_xmlid="mail.mt_comment",
)

else:
body = _(
"The no money hold for child {local_id} is expiring on "
"{old_expiration} and will not be extended since "
"no sponsorship exists for this child."
)
hold.message_post(body=body.format(**values))
# ----------------------------------------------------
# NOTIFICATION (Only for NO_MONEY_HOLD)
# ----------------------------------------------------
if hold.type == HoldType.NO_MONEY_HOLD.value:
if hold.child_id.sponsor_id:
body = (
"The no money hold for child {local_id} was expiring on "
"{old_expiration} and was extended to {new_expiration} "
"({extension_description} extension).{additional_text}"
)
hold.message_post(
body=_(body.format(**values)),
subject=_("No money hold extension"),
subtype_xmlid="mail.mt_comment",
)
else:
body = _(
"The no money hold for child {local_id} is expiring on "
"{old_expiration} and will not be extended since "
"no sponsorship exists for this child."
)
hold.message_post(body=body.format(**values))

##########################################################################
# Mapping METHOD #
Expand Down
3 changes: 2 additions & 1 deletion partner_communication_compassion/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# pylint: disable=C8101
{
"name": "Compassion Partner Communications",
"version": "14.0.1.1.0",
"version": "14.0.1.1.1",
"category": "Other",
"author": "Compassion CH",
"license": "AGPL-3",
Expand All @@ -49,6 +49,7 @@
"data/depart_communications.xml",
"data/communication_config.xml",
"data/utm_data.xml",
"data/ir_cron_data.xml",
"views/communication_job_view.xml",
"views/disaster_alert_view.xml",
"views/partner_compassion_view.xml",
Expand Down
32 changes: 8 additions & 24 deletions partner_communication_compassion/data/communication_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@

<!-- Project Lifecycle -->
<record id="project_suspension" model="partner.communication.config">
<field name="name">Project Suspension</field>
<field name="name">Project Suspension Announcement</field>
<field name="email_template_id" ref="email_project_suspension" />
<field
name="report_id"
Expand All @@ -266,31 +266,15 @@
<field name="print_if_not_email">True</field>
<field name="model_id" ref="model_compassion_child" />
</record>
<record id="project_suspension_e1" model="partner.communication.config">
<field name="name">Project Suspension Extension 1</field>
<field name="email_template_id" ref="email_project_suspension_e1" />
<field
name="report_id"
ref="partner_communication.report_a4_communication"
/>
<field name="send_mode">digital</field>
<field name="print_if_not_email">True</field>
<field name="model_id" ref="model_compassion_child" />
</record>
<record id="project_suspension_e2" model="partner.communication.config">
<field name="name">Project Suspension Extension 2</field>
<field name="email_template_id" ref="email_project_suspension_e2" />
<record
id="project_suspension_follow_up"
model="partner.communication.config"
>
<field name="name">Project Suspension Follow Up</field>
<field
name="report_id"
ref="partner_communication.report_a4_communication"
name="email_template_id"
ref="email_project_suspension_follow_up"
/>
<field name="send_mode">digital</field>
<field name="print_if_not_email">True</field>
<field name="model_id" ref="model_compassion_child" />
</record>
<record id="project_reactivation" model="partner.communication.config">
<field name="name">Project Reactivation</field>
<field name="email_template_id" ref="email_project_reactivation" />
<field
name="report_id"
ref="partner_communication.report_a4_communication"
Expand Down
19 changes: 19 additions & 0 deletions partner_communication_compassion/data/ir_cron_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data noupdate="1">
<record id="ir_cron_suspension_announcement" model="ir.cron">
<field
name="name"
>Project: 2-Month Suspension Annoucement/Follow-Up</field>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is a typo in the cron job's name. "Annoucement" should be "Announcement".

Suggested change
>Project: 2-Month Suspension Annoucement/Follow-Up</field>
>Project: 2-Month Suspension Announcement/Follow-Up</field>

<field name="model_id" ref="model_compassion_project" />
<field name="state">code</field>
<field name="code">model._cron_suspension_communication()</field>
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="active" eval="True" />
<field name="doall" eval="False" />
</record>
</data>
</odoo>
128 changes: 0 additions & 128 deletions partner_communication_compassion/data/project_lifecycle_emails.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,134 +73,6 @@
</field>
</record>

<record id="email_project_suspension_e1" model="mail.template">
<field name="name">Project Suspension Extension 1</field>
<field
name="model_id"
ref="partner_communication.model_partner_communication_job"
/>
<field
name="partner_to"
>${object.partner_id and object.partner_id.email and object.partner_id.id or False }</field>
<field name="subject">Extension of the project suspension</field>
<field
name="email_from"
>"${object.user_id.company_id.name}" &lt;${object.user_id.company_id.email}&gt;</field>
<field name="body_html" type="html">
<div>
% set child = object.get_objects()
% set project = child.mapped('project_id')[0]
% set suspension = project.lifecycle_ids[0]
% set reasons = suspension.suspension_reason_ids
% set details = suspension.suspension_detail
</div>
<p>
${object.partner_id.salutation},
<br />
<br />
We informed you some time ago about the project suspension of the center ${project.fcp_id}.
<br />
<br />
We received the information that the suspension was extended for 3 months
% if suspension.extension_1_reason_ids:
<!-- space --> because of ${suspension.get_list('extension_1_reason_ids.value')}.
% else:
in order to put back the project on a sustainable process.
% endif
<br />
<br />
You will receive updated information as soon as we get them.
<br />
<br />
Yours sincerely
</p>
</field>
</record>

<record id="email_project_suspension_e2" model="mail.template">
<field name="name">Project Suspension Extension 2</field>
<field
name="model_id"
ref="partner_communication.model_partner_communication_job"
/>
<field
name="partner_to"
>${object.partner_id and object.partner_id.email and object.partner_id.id or False }</field>
<field name="subject">Extension of the project suspension</field>
<field
name="email_from"
>"${object.user_id.company_id.name}" &lt;${object.user_id.company_id.email}&gt;</field>
<field name="body_html" type="html">
<div>
% set child = object.get_objects()
% set project = child.mapped('project_id')[0]
% set suspension = project.lifecycle_ids[0]
% set reasons = suspension.suspension_reason_ids
% set details = suspension.suspension_detail
</div>
<p>
${object.partner_id.salutation},
<br />
<br />
We informed you some time ago about the extension of the project suspension ${project.fcp_id}.
<br />
<br />
We received the information that the suspension was again extended for 3 months
% if suspension.extension_2_reason_ids:
<!-- space --> because of ${suspension.get_list('extension_2_reason_ids.value')}.
% else:
in order to put back the project on a sustainable process.
% endif
<!-- space --> This is the last chance for the center to correct their processes or the project will be closed.
<br />
<br />
You will receive updated information as soon as we get them.
<br />
<br />
Yours sincerely
</p>
</field>
</record>

<record id="email_project_reactivation" model="mail.template">
<field name="name">Project Reactivation</field>
<field
name="model_id"
ref="partner_communication.model_partner_communication_job"
/>
<field
name="partner_to"
>${object.partner_id and object.partner_id.email and object.partner_id.id or False }</field>
<field name="subject">Project Reactivation</field>
<field
name="email_from"
>"${object.user_id.company_id.name}" &lt;${object.user_id.company_id.email}&gt;</field>
<field name="body_html" type="html">
<div>
% set child = object.get_objects()
% set project = child.mapped('project_id')[0]
% set reactivation = project.lifecycle_ids[0]
% set details = reactivation.fcp_improvement_desc
</div>
<p>
${object.partner_id.salutation},
<br />
<br />
A few time ago the project ${project.fcp_id} where ${child.get('your sponsored child')} ${child.get('is')} registered was on suspension.
We are happy to inform you that the problem is resolved and that the project is again sane and active.
The project will do regular tuitions and spiritual activites to keep the situation healthy.
<br />
<br />
% if details:
${details}
<br />
<br />
% endif
Yours sincerely
</p>
</field>
</record>

<!-- FCP Transition -->
<record id="email_project_transition" model="mail.template">
<field name="name">Project Transition</field>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from openupgradelib import openupgrade

from odoo import SUPERUSER_ID, api


def migrate(cr, version):
env = api.Environment(cr, SUPERUSER_ID, {})

# List of XML IDs to be removed
obsolete_xml_ids = [
"project_suspension_e1",
"project_suspension_e2",
"project_reactivation",
]

for xml_id in obsolete_xml_ids:
# ref() finds the database record using the XML ID
record = env.ref(
f"partner_communication_compassion.{xml_id}", raise_if_not_found=False
)

if record:
# 1. Find and delete all communication logs linked to this config
logs = env["partner.communication.job"].search(
[("config_id", "=", record.id)]
)
if logs:
logs.unlink()

# 2. Delete the config record itself
record.unlink()

# Define XML IDs for the communication configs "FCP Suspension Follow up"
cr = env.cr
openupgrade.add_xmlid(
cr,
"partner_communication_compassion",
"email_project_suspension_follow_up",
"mail.template",
292,
noupdate=False,
)
80 changes: 79 additions & 1 deletion partner_communication_compassion/models/compassion_project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from odoo import fields, models
from dateutil.relativedelta import relativedelta

from odoo import api, fields, models


class CompassionProject(models.Model):
Expand All @@ -10,3 +12,79 @@ class CompassionProject(models.Model):
"communication.snippet",
string="Caption to use for pictures, or prayer shared by fcp, or else",
)

last_suspension_communication_date = fields.Date(
string="Date of the last suspension communication",
help="Records the date of the last suspension communication.",
)

def reactivate_project(self):
"""
Inherit reactivation to trigger 'FCP Suspension Follow Up'
"""
# 1. Execute original logic
res = super(CompassionProject, self).reactivate_project()

# 2. Trigger the communication
self._trigger_communication("project_suspension_follow_up")

return res

def _trigger_communication(self, config_xml_id):
"""
Finds all active sponsors for children in the project and
sends communication to the sponsor.
"""
# 1. Get the communication configuration record
comm_config = self.env.ref(f"partner_communication_compassion.{config_xml_id}")

for project in self:
# 2. Find all active sponsorships for children in this project
contracts = self.env["recurring.contract"].search(
[("child_id.project_id", "=", project.id), ("state", "=", "active")]
)

# 3. Send communication to each sponsor
for contract in contracts:
contract.with_context(
default_object_ids=contract.child_id.id
).send_communication(communication=comm_config, correspondent=True)

# 4. Update the last suspension communication date
project.last_suspension_communication_date = fields.Date.today()

@api.model
def _cron_suspension_communication(self):
"""
Finds all projects suspended for more than 2 months without follow-up
communication and triggers the appropriate communication.
"""
today = fields.Date.today()
two_months_ago = today - relativedelta(months=2)

# Find projects suspended for > 2 months
# AND (no follow-up yet OR last follow-up was > 2 months ago)
projects = self.search(
[
("suspension", "in", ["suspended", "fund-suspended"]),
("last_lifecycle_id.type", "=", "Suspension"),
("last_lifecycle_id.date", "<=", two_months_ago),
"|",
("last_suspension_communication_date", "=", False),
("last_suspension_communication_date", "<=", two_months_ago),
]
)

for project in projects:
last_lifecycle_date = project.last_lifecycle_id.date

if (
not project.last_suspension_communication_date
or project.last_suspension_communication_date < last_lifecycle_date
):
# 1. First time for this lifecycle event: Announcement
project._trigger_communication("project_suspension")

else:
# 2. We already announced it, but 2 months have passed: Follow-up
project._trigger_communication("project_suspension_follow_up")
Loading