Skip to content

Commit

Permalink
[17.0] T1771 sponsorship_compassion: migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ecino committed Sep 17, 2024
1 parent 2e409a4 commit 8e018c4
Show file tree
Hide file tree
Showing 23 changed files with 138 additions and 235 deletions.
6 changes: 3 additions & 3 deletions sponsorship_compassion/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Compassion Sponsorships
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-CompassionCH%2Fcompassion--modules-lightgray.png?logo=github
:target: https://github.com/CompassionCH/compassion-modules/tree/14.0/sponsorship_compassion
:target: https://github.com/CompassionCH/compassion-modules/tree/17.0/sponsorship_compassion
:alt: CompassionCH/compassion-modules

|badge1| |badge2| |badge3|
Expand Down Expand Up @@ -62,7 +62,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/CompassionCH/compassion-modules/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/CompassionCH/compassion-modules/issues/new?body=module:%20sponsorship_compassion%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/CompassionCH/compassion-modules/issues/new?body=module:%20sponsorship_compassion%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand All @@ -84,6 +84,6 @@ Contributors
Maintainers
-----------

This module is part of the `CompassionCH/compassion-modules <https://github.com/CompassionCH/compassion-modules/tree/14.0/sponsorship_compassion>`_ project on GitHub.
This module is part of the `CompassionCH/compassion-modules <https://github.com/CompassionCH/compassion-modules/tree/17.0/sponsorship_compassion>`_ project on GitHub.

You are welcome to contribute.
4 changes: 2 additions & 2 deletions sponsorship_compassion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
from . import models, wizards


def load_mappings(cr, registry):
def load_mappings(env):
path = "sponsorship_compassion/static/mappings/"
files = [
"anonymize_partner.json",
"cancel_sponsorship.json",
"create_sponsorship.json",
"upsert_partner.json",
]
load_mapping_files(cr, path, files)
load_mapping_files(env, path, files)
8 changes: 2 additions & 6 deletions sponsorship_compassion/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# pylint: disable=C8101
{
"name": "Compassion Sponsorships",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"category": "Compassion",
"author": "Compassion CH",
"license": "AGPL-3",
Expand All @@ -41,9 +41,7 @@
"child_compassion",
"partner_personal_info",
"web_m2x_options", # oca/web
"web_notify", # oca/web
"partner_firstname", # oca/partner-contact
"account_analytic_attribution", # compassion-accounting
"sale",
],
"data": [
Expand Down Expand Up @@ -76,9 +74,7 @@
"views/product_views.xml",
"views/res_config_settings_view.xml",
],
"demo": [
"demo/demo_data.xml",
],
"demo": ["demo/product_product.xml"],
"installable": True,
"auto_install": False,
"development_status": "Production/Stable",
Expand Down
13 changes: 0 additions & 13 deletions sponsorship_compassion/demo/demo_data.xml

This file was deleted.

16 changes: 16 additions & 0 deletions sponsorship_compassion/demo/product_product.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<odoo>
<record model="product.product" id="sponsorship">
<field name="name">Sponsorship</field>
<field name="categ_id" ref="product_category_sponsorship" />
<field name="default_code">sponsorship</field>
<field name="list_price">42</field>
<field name="image_128" type="base64" file="child_compassion/static/description/icon.png" />
</record>
<record model="product.product" id="general_fund">
<field name="name">General FUND</field>
<field name="categ_id" ref="product_category_fund" />
<field name="default_code">fund_gen</field>
<field name="list_price">8</field>
<field name="image_128" type="base64" file="child_compassion/static/description/icon.png" />
</record>
</odoo>
1 change: 0 additions & 1 deletion sponsorship_compassion/models/child_compassion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class ChildCompassion(models.Model):
"recurring.contract",
compute="_compute_related_contracts",
string="Sponsorships",
readonly=True,
)
has_been_sponsored = fields.Boolean(compute="_compute_has_been_sponsored")

Expand Down
18 changes: 0 additions & 18 deletions sponsorship_compassion/models/contract_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class ContractGroup(models.Model):
contains_sponsorship = fields.Boolean(
string="Contains sponsorship",
compute="_compute_contains_sponsorship",
readonly=True,
default=lambda s: s.env.context.get("default_type", None)
and "S" in s.env.context.get("default_type", "O"),
)

##########################################################################
Expand All @@ -49,21 +46,6 @@ def _generate_invoices(self, invoicer, contract_id=None):
contracts._generate_gifts(invoicer, CHRISTMAS_GIFT)
return True

def build_inv_line_data(
self, invoicing_date=False, gift_wizard=False, contract_line=False
):
# Push analytic account
res = super().build_inv_line_data(invoicing_date, gift_wizard, contract_line)
if gift_wizard:
res[
"analytic_account_id"
] = gift_wizard.contract_id.origin_id.analytic_id.id
elif contract_line:
res[
"analytic_account_id"
] = contract_line.contract_id.origin_id.analytic_id.id
return res

def _get_partner_for_contract(self, contract, gift_wizard=False):
if gift_wizard and contract.send_gifts_to:
return contract[contract.send_gifts_to]
Expand Down
12 changes: 6 additions & 6 deletions sponsorship_compassion/models/contract_origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class ContractOrigin(models.Model):
required=True,
index=True,
)
partner_id = fields.Many2one("res.partner", "Partner", readonly=False)
analytic_id = fields.Many2one(
"account.analytic.account", "Analytic Account", readonly=False
)
partner_id = fields.Many2one("res.partner", "Partner")
analytic_id = fields.Many2one("account.analytic.account", "Analytic Account")
contract_ids = fields.One2many(
"recurring.contract", "origin_id", "Sponsorships originated", readonly=True
"recurring.contract",
"origin_id",
"Sponsorships originated",
)
country_id = fields.Many2one("res.country", "Country", readonly=False)
country_id = fields.Many2one("res.country", "Country")
other_name = fields.Char("Give details", size=128)
won_sponsorships = fields.Integer(compute="_compute_won_sponsorships", store=True)
conversion_rate = fields.Float(compute="_compute_won_sponsorships", store=True)
Expand Down
93 changes: 29 additions & 64 deletions sponsorship_compassion/models/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class SponsorshipContract(models.Model):
string="Correspondent",
tracking=True,
index=True,
readonly=False,
)
partner_codega = fields.Char(
"Partner ref", related="correspondent_id.ref", readonly=True
Expand All @@ -70,14 +69,9 @@ class SponsorshipContract(models.Model):
"res.lang.compassion",
"Preferred language",
tracking=True,
readonly=False,
)
transfer_partner_id = fields.Many2one(
"compassion.global.partner", "Transferred to", readonly=False
)
gmc_commitment_id = fields.Char(
help="Connect global ID", readonly=True, copy=False, tracking=True
)
transfer_partner_id = fields.Many2one("compassion.global.partner", "Transferred to")
gmc_commitment_id = fields.Char(help="Connect global ID", copy=False, tracking=True)
hold_expiration_date = fields.Datetime(
help="Used for setting a hold after sponsorship cancellation"
)
Expand All @@ -96,13 +90,7 @@ class SponsorshipContract(models.Model):
child_id = fields.Many2one(
"compassion.child",
"Sponsored child",
readonly=True,
copy=False,
states={
"draft": [("readonly", False)],
"waiting": [("readonly", False)],
"mandate": [("readonly", False)],
},
ondelete="restrict",
tracking=True,
index=True,
Expand Down Expand Up @@ -131,18 +119,16 @@ class SponsorshipContract(models.Model):
ondelete="restrict",
tracking=True,
index=True,
readonly=False,
)
parent_id = fields.Many2one(
"recurring.contract",
"Previous sponsorship",
tracking=True,
index=True,
copy=False,
readonly=False,
)
sub_sponsorship_id = fields.Many2one(
"recurring.contract", "sub sponsorship", readonly=True, copy=False, index=True
"recurring.contract", "sub sponsorship", copy=False, index=True
)
partner_lang = fields.Selection(
string="Partner language", related="partner_id.lang", store=True
Expand All @@ -151,16 +137,14 @@ class SponsorshipContract(models.Model):
"res.partner",
"Partner",
required=True,
readonly=False,
states={"terminated": [("readonly", True)]},
ondelete="restrict",
tracking=True,
)
gmc_payer_partner_id = fields.Many2one(
"res.partner", readonly=True, help="Partner synchronized with GMC as a payer."
"res.partner", help="Partner synchronized with GMC as a payer."
)
gmc_correspondent_commitment_id = fields.Char(
readonly=True, help="Id of the correspondent commitment.", tracking=True
help="Id of the correspondent commitment.", tracking=True
)
type = fields.Selection(
[
Expand All @@ -175,7 +159,8 @@ class SponsorshipContract(models.Model):
string="Sponsorship type",
)
group_freq = fields.Char(
string="Payment frequency", compute="_compute_frequency", readonly=True
string="Payment frequency",
compute="_compute_frequency",
)
is_first_sponsorship = fields.Boolean(
compute="_compute_is_first_sponsorship", store=True
Expand Down Expand Up @@ -264,23 +249,22 @@ def _get_sponsorship_standard_lines(self, correspondence):
"default_code 'fund_gen' first."
)
)

sponsorship_product = sponsorship_product.product_variant_id
gen_product = gen_product.product_variant_id
if self.company_id:
pricelist = self.pricelist_id
sponsorship_product.with_context(
{"pricelist": pricelist.id, "partner": self.partner_id.id}
)._compute_product_price()
gen_product.with_context(
{"pricelist": pricelist.id, "partner": self.partner_id.id}
)._compute_product_price()

pricelist = self.pricelist_id
sponsorship_price = pricelist._get_product_price(
sponsorship_product,
1.0,
)
gen_price = pricelist._get_product_price(
gen_product,
1.0,
)
sponsorship_vals = {
"product_id": sponsorship_product.id,
"quantity": 0 if correspondence else 1,
"amount": 0 if correspondence else sponsorship_product.list_price,
"subtotal": 0 if correspondence else sponsorship_product.list_price,
"amount": 0 if correspondence else sponsorship_price,
"subtotal": 0 if correspondence else sponsorship_price,
}
res.append((0, 0, sponsorship_vals))
# Avoid appending the GEN fund when one line already exists
Expand All @@ -289,8 +273,8 @@ def _get_sponsorship_standard_lines(self, correspondence):
gen_vals = {
"product_id": gen_product.id,
"quantity": 0 if correspondence else 1,
"amount": 0 if correspondence else gen_product.list_price,
"subtotal": 0 if correspondence else gen_product.list_price,
"amount": 0 if correspondence else gen_price,
"subtotal": 0 if correspondence else gen_price,
}
res.append((0, 0, gen_vals))
return res
Expand Down Expand Up @@ -364,18 +348,19 @@ def _compute_contract_products(self):
)

@api.depends("partner_id", "partner_id.ref", "child_id", "child_id.local_id")
def name_get(self):
def _compute_display_name(self):
"""Gives a friendly name for a sponsorship"""
result = []
super()._compute_display_name()
for contract in self:
if contract.partner_id.ref or contract.reference:
name = contract.partner_id.ref or contract.reference
if contract.child_id:
name += " - " + contract.child_code
elif contract.contract_line_ids:
name += " - " + contract.contract_line_ids[0].product_id.name
result.append((contract.id, name))
return result
contract.display_name = name
else:
contract.display_name = "Contract"

@api.depends("activation_date", "state")
def _compute_active(self):
Expand Down Expand Up @@ -614,7 +599,7 @@ def write(self, vals):
child.child_sponsored(vals["correspondent_id"])

if "reading_language" in vals:
(self - updated_correspondents)._on_language_changed()
(self - updated_correspondents).upsert_sponsorship()

if old_partners:
self.mapped("partner_id").update_number_sponsorships()
Expand Down Expand Up @@ -843,7 +828,7 @@ def _contract_terminated(self, vals):

def contract_waiting(self):
contracts = self.filtered(lambda c: c.type == "O")
super(SponsorshipContract, self).contract_waiting()
super().contract_waiting()
for contract in self - contracts:
if not contract.start_date:
contract.start_date = fields.Datetime.now()
Expand Down Expand Up @@ -903,26 +888,6 @@ def upsert_sponsorship(self):
)
return messages

##########################################################################
# PRIVATE METHODS #
##########################################################################
def _on_language_changed(self):
"""Update the preferred language in GMC."""
messages = self.upsert_sponsorship().with_context({"async_mode": False})
error_msg = (
"Error when updating sponsorship language. "
"You may be out of sync with GMC - please try again."
)
for message in messages:
try:
message.process_messages()
if "failure" in message.state:
failure = message.failure_reason or error_msg
self.env.user.notify_danger(failure, "Language update failed.")
except Exception:
self.env.user.notify_danger(error_msg, "Language update failed.")
logger.error(error_msg, exc_info=True)

def _change_correspondent(self):
self.ensure_one()
if not self.correspondent_id.global_id:
Expand Down Expand Up @@ -1312,9 +1277,9 @@ def migrate_gmc_correspondence_commitment(self):
)
answer = onramp.send_message(f"beneficiaries/{global_id}/summary", "GET")
commitments = (
answer.get("content").get("Commitments")
answer.get("content", {}).get("Commitments", [])
if answer.get("code") == 200
else False
else []
)
if commitments:
for commitment in commitments:
Expand Down
4 changes: 3 additions & 1 deletion sponsorship_compassion/models/recurring_contract_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class SponsorshipLine(models.Model):
readonly=True,
)
sponsorship_id = fields.Many2one(
"recurring.contract", "Sponsorship", ondelete="cascade", readonly=False
"recurring.contract",
"Sponsorship",
ondelete="cascade",
)

@api.model
Expand Down
Loading

0 comments on commit 8e018c4

Please sign in to comment.