bemade-addons/customer_itch_cycle/models/res_partner.py

54 lines
1.7 KiB
Python
Raw Normal View History

2025-01-02 08:06:56 -05:00
from odoo import api, fields, models
2024-12-14 06:53:23 -05:00
class ResPartner(models.Model):
2025-01-02 08:06:56 -05:00
"""
Extends the partner model to add sales cycle tracking functionality.
"""
2024-12-14 06:53:23 -05:00
_inherit = 'res.partner'
itch_cycle_product_ids = fields.One2many(
comodel_name='itch.cycle.product.partner',
inverse_name='partner_id',
2025-01-02 08:06:56 -05:00
string="Product Sales Cycles",
help="List of all product sales cycles associated with this customer",
2024-12-14 06:53:23 -05:00
)
2024-12-17 12:51:45 -05:00
reorder_cycle_product_ids = fields.One2many(
comodel_name='itch.cycle.product.partner',
inverse_name='partner_id',
2025-01-02 08:06:56 -05:00
string="Active Sales Cycles",
domain=[('quantity_of_orders', '>', 1)],
help="List of product sales cycles with established patterns",
2024-12-17 12:51:45 -05:00
)
2024-12-14 06:53:23 -05:00
itch_next_delay = fields.Date(
2025-01-02 08:06:56 -05:00
string="Next Follow-up Date",
compute="_compute_itch_next_delay",
store=True,
help="Earliest follow-up date",
2024-12-14 06:53:23 -05:00
)
2025-01-02 08:06:56 -05:00
@api.depends(
'itch_cycle_product_ids',
'itch_cycle_product_ids.date_next_follow_up'
)
2024-12-14 06:53:23 -05:00
def _compute_itch_next_delay(self):
"""
2025-01-02 08:06:56 -05:00
Compute the earliest follow-up date.
The date is calculated from all associated product cycles.
"""
2024-12-14 06:53:23 -05:00
for partner in self:
2025-01-02 08:06:56 -05:00
cycles = partner.reorder_cycle_product_ids
dates = cycles.mapped('date_next_follow_up')
partner.itch_next_delay = min(dates) if dates else False
2024-12-14 06:53:23 -05:00
def action_populate_itch_cycles(self):
"""
2025-01-02 08:06:56 -05:00
Initialize or update product cycles.
The cycles are created from historical order data.
2024-12-14 06:53:23 -05:00
"""
2025-01-02 08:06:56 -05:00
self.env['itch.cycle.product.partner'].populate_from_past_orders()