-
-
Notifications
You must be signed in to change notification settings - Fork 574
[ADD] Add partner contract anniversary date fields #1285
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
base: 18.0
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| "category": "marketing", | |
| "category": "contract", |
| 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", | ||
| ) |
There was a problem hiding this comment.
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>
|
@samirGuesmi Let's finalize this during OCA days 🚀 |
8462e3f to
b2eda4d
Compare
|
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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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): |
There was a problem hiding this comment.
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
This addon add a field for first contract line start date and compute contract anniversary date, useful for renewals and customer relationship management.