Skip to content

Commit

Permalink
Merge pull request #1433 from CompassionCH/devel
Browse files Browse the repository at this point in the history
Stabilization 1.14
  • Loading branch information
ecino authored Dec 6, 2021
2 parents 21378a3 + 142e68e commit 1d63258
Show file tree
Hide file tree
Showing 43 changed files with 4,110 additions and 4,768 deletions.
12 changes: 9 additions & 3 deletions mass_mailing_switzerland/models/mass_mailing_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class MassMailingContact(models.Model):
sponsored_child_your_child = fields.Char(compute="_compute_sponsored_child_fields")
pending_letter_child_names = fields.Char(compute="_compute_sponsored_child_fields")

numspons = fields.Char(compute="_compute_sponsored_child_fields")

_sql_constraints = [(
"unique_email", "unique(email)", "This mailing contact already exists"
)]
Expand All @@ -83,9 +85,10 @@ def _compute_sponsored_child_fields(self):
for contact in self:
partners = contact.partner_ids.with_context(lang=contact.partner_id.lang)
# Allow option to take a child given in context, otherwise take
sponsored_child_ids = partners.mapped("sponsored_child_ids")
# the sponsored children.
child = self.env.context.get("mailchimp_child",
partners.mapped("sponsored_child_ids"))
child = self.env.context.get("mailchimp_child", sponsored_child_ids)
contact.numspons = len(list(child))
if country_filter_id:
child = child.filtered(
lambda c: c.field_office_id.id == country_filter_id)
Expand Down Expand Up @@ -228,7 +231,10 @@ def get_partner(self, email):
"""Override to fetch partner directly from relation if set."""
self.ensure_one()
if self.partner_ids:
return self.partner_ids[0].id
partners = self.partner_ids.with_context(lang=self.partner_id.lang)
partners_by_child_count = [(p, len(list(p.mapped("sponsored_child_ids")))) for p in partners]
partners_by_child_count.sort(key=lambda x:-x[-1])
return partners_by_child_count[0][0].id
else:
return self.env["res.partner"].search([
("email", "=ilike", email),
Expand Down
33 changes: 15 additions & 18 deletions partner_communication_switzerland/models/partner_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,31 +354,29 @@ def get_child_picture_attachment(self):
res[name] = ("partner_communication_switzerland.child_picture", pdf)
return res

def get_yearly_payment_slips_2bvr(self):
return self.get_yearly_payment_slips(bv_number=2)

def get_yearly_payment_slips(self, bv_number=3):
def get_yearly_payment_slips(self):
"""
Attach payment slips
:param bv_number number of BV on a page (switch between 2BV/3BV page)
:return: dict {attachment_name: [report_name, pdf_data]}
"""
self.ensure_one()
assert bv_number in (2, 3)
sponsorships = self.get_objects()
payment_mode_bvr = self.env.ref("sponsorship_switzerland.payment_mode_bvr")
pm_permanent = self.env.ref(
"sponsorship_switzerland.payment_mode_permanent_order")
attachments = dict()
# IF payment mode is BVR and partner is paying
# attach sponsorship payment slips
# Year 2022 only: we send Permanent Orders again for QR-update!
pay_bvr = sponsorships.filtered(
lambda s: s.payment_mode_id == payment_mode_bvr
and s.partner_id == self.partner_id
lambda s: s.payment_mode_id in (payment_mode_bvr, pm_permanent)
and s.partner_id == self.partner_id
)
if pay_bvr and pay_bvr.must_pay_next_year():
today = date.today()
date_start = today.replace(today.year + 1, 1, 1)
date_stop = date_start.replace(month=12, day=31)
report_name = f"report_compassion.{bv_number}bvr_sponsorship"
report_name = f"report_compassion.2bvr_sponsorship"
data = {
"doc_ids": pay_bvr.ids,
"date_start": date_start,
Expand All @@ -387,7 +385,7 @@ def get_yearly_payment_slips(self, bv_number=3):
}
pdf = self._get_pdf_from_data(
data, self.env.ref(
f"report_compassion.report_{bv_number}bvr_sponsorship")
f"report_compassion.report_2bvr_sponsorship")
)
attachments.update({_("sponsorship payment slips.pdf"): [report_name, pdf]})
# Attach gifts for correspondents
Expand All @@ -396,18 +394,17 @@ def get_yearly_payment_slips(self, bv_number=3):
if sponsorship.mapped(sponsorship.send_gifts_to) == self.partner_id:
pays_gift += sponsorship
if pays_gift:
nb_gifts = 4 if bv_number == 2 else 3
product_ids = self.env['product.product'].search([
('default_code', 'in', GIFT_REF[:nb_gifts])
('default_code', 'in', [GIFT_REF[0]] + GIFT_REF[2:4])
]).ids
report_name = f"report_compassion.{bv_number}bvr_gift_sponsorship"
report_name = f"report_compassion.2bvr_gift_sponsorship"
data = {
"doc_ids": pays_gift.ids,
"product_ids": product_ids
}
pdf = self._get_pdf_from_data(
data, self.env.ref(
f"report_compassion.report_{bv_number}bvr_gift_sponsorship"),
f"report_compassion.report_2bvr_gift_sponsorship"),
)
attachments.update({_("sponsorship gifts.pdf"): [report_name, pdf]})
return attachments
Expand Down Expand Up @@ -734,8 +731,8 @@ def get_sponsorship_payment_slip_attachments(self):
attachments = {}
# Payment slips
if bv_sponsorships:
report_name = "report_compassion.3bvr_sponsorship"
report_ref = self.env.ref("report_compassion.report_3bvr_sponsorship")
report_name = "report_compassion.report_2bvr_sponsorship"
report_ref = self.env.ref("report_compassion.report_2bvr_sponsorship")
if bv_sponsorships.mapped("payment_mode_id") == permanent_order:
# One single slip is enough for permanent order.
report_name = "report_compassion.bvr_sponsorship"
Expand Down Expand Up @@ -792,10 +789,10 @@ def get_csp_attachment(self):

# Payment slips
if is_payer and make_payment_pdf:
report_name = "report_compassion.3bvr_sponsorship"
report_name = "report_compassion.report_2bvr_sponsorship"
data = {"doc_ids": csp.ids, "background": self.send_mode != "physical"}
pdf = self._get_pdf_from_data(
data, self.env.ref("report_compassion.report_3bvr_sponsorship")
data, self.env.ref("report_compassion.report_2bvr_sponsorship")
)
attachments.update({_("csv payment slips.pdf"): [report_name, pdf]})
return attachments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# The licence is in the file __manifest__.py
#
##############################################################################
from random import randint
import random
import itertools

from odoo import api, models

Expand All @@ -16,33 +17,28 @@ class PartnerCommunication(models.Model):
_inherit = "partner.communication.config"

@api.multi
def generate_test_cases(self, lang="de_DE", send_mode="digital"):
def generate_test_cases_by_language_family_case(self, lang="de_DE", family_case="single", send_mode="digital"):
"""
Generates example communications for our multiple cases in CH.
Generates example communications for our multiple cases in CH
depending on the language and the family case
Outputs the texts in a file
:param lang:
:return: True
"""
self.ensure_one()
family = self.env.ref("partner_compassion.res_partner_title_family")
# Find a partner with > 3 sponsorships
family_pool = self.env["res.partner"].search([
("number_sponsorships", ">", 3),
("lang", "=", lang),
("title", "=", family.id)
], order="name asc", limit=50)
single_pool = self.env["res.partner"].search([
("number_sponsorships", ">", 3),
("lang", "=", lang),
("title", "!=", family.id),
("title.plural", "=", False)
], order="name asc", limit=50)
comm_obj = self.env["partner.communication.job"].with_context(must_skip_send_to_printer=True)

comm_obj = self.env["partner.communication.job"].with_context(
must_skip_send_to_printer=True)

res = []
for case in ["single", "family"]:
pool = single_pool if case == "single" else family_pool
partner = pool[min(randint(0, 49), len(pool) - 1)]

for number_sponsorship in [1, 3, 4]:
partner = self._find_partner(number_sponsorship, lang, family_case)
if partner is None:
continue
object_ids = self._get_test_objects(partner)
object_ids = ",".join([str(id)
for id in object_ids[0:number_sponsorship]])
temp_comm = comm_obj.create({
"partner_id": partner.id,
"config_id": self.id,
Expand All @@ -51,28 +47,46 @@ def generate_test_cases(self, lang="de_DE", send_mode="digital"):
"send_mode": send_mode,
})
res.append({
"case": f"{case}_4_children",
"subject": temp_comm.subject,
"body_html": temp_comm.body_html})
partner = pool[min(randint(0, 49), len(pool) - 1)]
object_ids = self._get_test_objects(partner)
temp_comm.object_ids = ",".join(map(str, object_ids[:3]))
temp_comm.partner_id = partner
temp_comm.refresh_text()
res.append({
"case": f"{case}_3_children",
"case": f"{family_case}_{number_sponsorship}_child",
"subject": temp_comm.subject,
"body_html": temp_comm.body_html})
partner = pool[min(randint(0, 49), len(pool) - 1)]
object_ids = self._get_test_objects(partner)
temp_comm.object_ids = object_ids[0]
temp_comm.partner_id = partner
temp_comm.refresh_text()
res.append({
"case": f"{case}_1_child",
"subject": temp_comm.subject,
"body_html": temp_comm.body_html})
"body_html": temp_comm.body_html
})
temp_comm.unlink()

return res

@api.multi
def generate_test_case_by_partner(self, partner=None, send_mode="digital"):
"""
Generates example communications for our multiple cases in CH
depending on partner
Outputs the texts in a file
:param partner:
:return: True
"""
self.ensure_one()

comm_obj = self.env["partner.communication.job"].with_context(
must_skip_send_to_printer=True)

res = []

object_ids = self._get_test_objects(partner)
object_ids = ",".join([str(id) for id in object_ids])
temp_comm = comm_obj.create({
"partner_id": partner.id,
"config_id": self.id,
"object_ids": object_ids,
"auto_send": False,
"send_mode": send_mode,
})
res = {
"case": "partner",
"subject": temp_comm.subject,
"body_html": temp_comm.body_html
}
temp_comm.unlink()

return res

def open_test_case_wizard(self):
Expand Down Expand Up @@ -101,3 +115,25 @@ def _get_test_objects(self, partner):
("invoice_id.invoice_category", "=", "fund")
], limit=4).ids
return object_ids

def _find_partner(self, number_sponsorships, lang, family_case):
family = self.env.ref("partner_compassion.res_partner_title_family")

query = [
("number_sponsorships", "=", number_sponsorships),
("lang", "=", lang),
]
if family_case == "single":
query += [("title", "!=", family.id), ("title.plural", "=", False)]
else:
query += [("title", "=", family.id)]

answers = self.env["res.partner"].search(query, limit=50)

# check that the query returned a result
if len(answers) <= 0:
return None

# randomly select one
answer = random.choice(answers)
return answer
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
<?xml version="1.0"?>
<odoo>
<record id="communication_test_case_form" model="ir.ui.view">
<field name="name">partner.communication.test.cases.wizard.form</field>
<field name="model">partner.communication.test.cases.wizard</field>
<field name="arch" type="xml">
<form>
<header>
<button name="generate_test_cases" string="Generate test cases" type="object" class="oe_highlight"/>
<button name="generate_test_cases_single" attrs="{'invisible': [('partner_selected', '=', True)]}" string="Generate single person test cases" type="object" class="oe_highlight"/>
<button name="generate_test_cases_family" attrs="{'invisible': [('partner_selected', '=', True)]}" string="Generate family test cases" type="object" class="oe_highlight"/>
<button name="generate_test_cases_partner" attrs="{'invisible': [('partner_selected', '!=', True)]}" string="Generate partner test case" type="object" class="oe_highlight"/>
</header>
<sheet>
<field name="partner_selected" invisible="1"/>
<group>
<group>
<field name="config_id"/>
<field name="language"/>
</group>
<group>
<field name="language" attrs="{'invisible': [('partner_selected', '=', True)]}"/>
<field name="partner"/>
</group>
</group>
<notebook>
<page string="Single person">
<group>
<page string="Selected language / Single person" attrs="{'invisible': [('partner_selected', '=', True)]}">
<group col="3">
<group string="1 Child">
<field name="single_1_child_subject" string="Subject"/>
<field name="single_1_child_body" string="Body"/>
</group>
<group string="3 Children">
<field name="single_3_children_subject" string="Subject"/>
<field name="single_3_children_body" string="Body"/>
<field name="single_3_child_subject" string="Subject"/>
<field name="single_3_child_body" string="Body"/>
</group>
<group string="4 Children">
<field name="single_4_children_subject" string="Subject"/>
<field name="single_4_children_body" string="Body"/>
<field name="single_4_child_subject" string="Subject"/>
<field name="single_4_child_body" string="Body"/>
</group>
</group>
</page>
<page string="Family">
<group>
<page string="Selected language / Family" attrs="{'invisible': [('partner_selected', '=', True)]}">
<group col="3">
<group string="1 Child">
<field name="family_1_child_subject" string="Subject"/>
<field name="family_1_child_body" string="Body"/>
</group>
<group string="3 Children">
<field name="family_3_children_subject" string="Subject"/>
<field name="family_3_children_body" string="Body"/>
<field name="family_3_child_subject" string="Subject"/>
<field name="family_3_child_body" string="Body"/>
</group>
<group string="4 Children">
<field name="family_4_children_subject" string="Subject"/>
<field name="family_4_children_body" string="Body"/>
<field name="family_4_child_subject" string="Subject"/>
<field name="family_4_child_body" string="Body"/>
</group>
</group>
</page>
<page string="Partner" attrs="{'invisible': [('partner_selected', '!=', True)]}">
<group>
<field name="partner_subject" string="Subject"/>
<field name="partner_body" string="Body"/>
</group>
</page>
</notebook>
</sheet>
</form>
Expand Down
Loading

0 comments on commit 1d63258

Please sign in to comment.