Skip to content

Conversation

@samirGuesmi
Copy link
Member

This addon add a field for first contract line start date and compute contract anniversary date, useful for renewals and customer relationship management.

@samirGuesmi
Copy link
Member Author

@sbejaoui

Copy link
Contributor

@sbejaoui sbejaoui left a comment

Choose a reason for hiding this comment

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

please add unit tests

"version": "18.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV, Odoo Community Association (OCA)",
"category": "marketing",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"category": "marketing",
"category": "contract",

Comment on lines 11 to 18
first_contract_line_start_date = fields.Date(
string="First Contract Date",
compute="_compute_contract_anniversary_date",
)
contract_anniversary_date = fields.Date(
compute="_compute_contract_anniversary_date",
search="_search_contract_anniversary_date",
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think these fields could just be stored fields updated by a cron that runs once a year. That would make the code much simpler and avoid performance issues with searches and computations on large databases.

def _cron_update_contract_anniversary(self):
    def _anniv(d: date, year: int) -> date:
        try:
            return d.replace(year=year)
        except ValueError:
            return date(year, 2, 28)
    
    partners = self.search([])
    groups = self.env['contract.line'].read_group(
        domain=[('is_canceled', '!=', True), ('partner_id', 'in', partners.ids)],
        fields=['date_start:min', 'partner_id'],
        groupby=['partner_id'],
    )
    first_start = {
        g['partner_id'][0]: fields.Date.to_date(g['date_start'])
        for g in groups if g.get('date_start')
    }
    this_year = fields.Date.context_today(self).year
    for p in partners:
        d0 = first_start.get(p.id)
        anniv = _anniv(d0, this_year) if d0 else False
        p.write({'first_contract_line_start_date': d0, 'contract_anniversary_date': anniv})
<record id="ir_cron_update_contract_anniversary" model="ir.cron">
      <field name="name">Partners: Update Contract Anniversaries</field>
      <field name="model_id" ref="base.model_res_partner"/>
      <field name="state">code</field>
      <field name="code">model._cron_update_contract_anniversary()</field>
      <field name="interval_number">12</field>
      <field name="interval_type">months</field>
      <field name="numbercall">-1</field>
      <field name="nextcall">2026-01-01 01:30:00</field>
      <field name="active">True</field>
    </record>

@qgroulard
Copy link
Contributor

@samirGuesmi Let's finalize this during OCA days 🚀

@samirGuesmi samirGuesmi force-pushed the 18.0-PartnerContractAnniversary-sgu branch from 8462e3f to b2eda4d Compare September 17, 2025 14:20
@samirGuesmi
Copy link
Member Author

Thanks for the review, here we are.

class ResPartner(models.Model):
_inherit = "res.partner"

first_contract_line_start_date = fields.Date(string="First Contract Date")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
first_contract_line_start_date = fields.Date(string="First Contract Date")
first_contract_line_start_date = fields.Date(string="First Contract Date", readonly=True)

_inherit = "res.partner"

first_contract_line_start_date = fields.Date(string="First Contract Date")
contract_anniversary_date = fields.Date()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
contract_anniversary_date = fields.Date()
contract_anniversary_date = fields.Date(readonly=True)


@tagged("post_install", "-at_install")
class TestContractPartnerAnniversary(TestContract):
def test_check_anniversary_date(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

please add a test for 29/02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants