From 55e0d55dba5a7e09beacd66c04b14c275d165459 Mon Sep 17 00:00:00 2001 From: Unai Beristain Date: Thu, 10 Oct 2024 17:46:10 +0200 Subject: [PATCH] [IMP] crm_last_dates: Improve child_ids --- crm_last_dates/models/res_partner.py | 18 ++++++++----- crm_last_dates/views/res_partner_view.xml | 33 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/crm_last_dates/models/res_partner.py b/crm_last_dates/models/res_partner.py index cfdb319..4edd18d 100644 --- a/crm_last_dates/models/res_partner.py +++ b/crm_last_dates/models/res_partner.py @@ -17,20 +17,26 @@ class ResPartner(models.Model): store=True, ) - @api.depends("commercial_partner_id", "opportunity_ids") + @api.depends("commercial_partner_id", "opportunity_ids", "child_ids") def _compute_last_lead_date(self): for partner in self: - lead_dates = partner.mapped("opportunity_ids.create_date") + lead_dates = partner.mapped("opportunity_ids.create_date") + partner.mapped( + "child_ids.opportunity_ids.create_date" + ) partner.last_lead_date = max(lead_dates) if lead_dates else False - @api.depends("commercial_partner_id", "meeting_ids") + @api.depends("commercial_partner_id", "meeting_ids", "child_ids") def _compute_last_meeting_date(self): for partner in self: - meeting_dates = partner.mapped("meeting_ids.create_date") + meeting_dates = partner.mapped("meeting_ids.create_date") + partner.mapped( + "child_ids.meeting_ids.create_date" + ) partner.last_meeting_date = max(meeting_dates) if meeting_dates else False - @api.depends("commercial_partner_id", "invoice_ids") + @api.depends("commercial_partner_id", "invoice_ids", "child_ids") def _compute_last_invoice_date(self): for partner in self: - invoice_dates = partner.mapped("invoice_ids.create_date") + invoice_dates = partner.mapped("invoice_ids.create_date") + partner.mapped( + "child_ids.invoice_ids.create_date" + ) partner.last_invoice_date = max(invoice_dates) if invoice_dates else False diff --git a/crm_last_dates/views/res_partner_view.xml b/crm_last_dates/views/res_partner_view.xml index faac8a7..8baf02e 100644 --- a/crm_last_dates/views/res_partner_view.xml +++ b/crm_last_dates/views/res_partner_view.xml @@ -27,4 +27,37 @@ + + + CRM Last Dates + + code + +partners = env['res.partner'].search([]) + +for partner in partners: + # Calcular la última fecha de lead + lead_dates = partner.mapped("opportunity_ids.create_date") + \ + partner.mapped("child_ids.opportunity_ids.create_date") + last_lead_date = max(lead_dates) if lead_dates else False + + # Calcular la última fecha de reunión + meeting_dates = partner.mapped("meeting_ids.create_date") + \ + partner.mapped("child_ids.meeting_ids.create_date") + last_meeting_date = max(meeting_dates) if meeting_dates else False + + # Calcular la última fecha de factura + invoice_dates = partner.mapped("invoice_ids.create_date") + \ + partner.mapped("child_ids.invoice_ids.create_date") + last_invoice_date = max(invoice_dates) if invoice_dates else False + + # Usar write para actualizar los campos + partner.write({ + 'last_lead_date': last_lead_date, + 'last_meeting_date': last_meeting_date, + 'last_invoice_date': last_invoice_date, + }) + + +