126 lines
5.0 KiB
Python
126 lines
5.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from datetime import timedelta
|
|
from odoo import models, fields, api
|
|
|
|
|
|
class ContactEvent(models.Model):
|
|
_name = 'contact.event'
|
|
_description = 'Contact Event'
|
|
_order = 'date_start desc'
|
|
_inherit = ['mail.thread', 'mail.activity.mixin']
|
|
|
|
contact_id = fields.Many2one(
|
|
'dsrpt.contact',
|
|
string='Contact',
|
|
required=True,
|
|
ondelete='cascade',
|
|
tracking=True
|
|
)
|
|
date_start = fields.Datetime(
|
|
string='Date & Time',
|
|
required=True,
|
|
default=fields.Datetime.now,
|
|
tracking=True
|
|
)
|
|
duration = fields.Float(
|
|
string='Duration (hours)',
|
|
default=1.0,
|
|
tracking=True
|
|
)
|
|
user_id = fields.Many2one(
|
|
'res.users',
|
|
string='Responsible',
|
|
default=lambda self: self.env.user,
|
|
tracking=True
|
|
)
|
|
calendar_event_id = fields.Many2one(
|
|
'calendar.event',
|
|
string='Calendar Event',
|
|
readonly=True
|
|
)
|
|
notes = fields.Text(string='Notes', required=True, tracking=True)
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
records = super().create(vals_list)
|
|
for record in records:
|
|
# Get base URL for creating contact link
|
|
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url', 'http://localhost:8069')
|
|
contact_url = f"{base_url}/web#id={record.contact_id.id}&model=dsrpt.contact&view_type=form"
|
|
|
|
# Prepare description with contact link
|
|
description = f"📱 Contact in Odoo: {contact_url}\n\n"
|
|
if record.notes:
|
|
description += record.notes
|
|
|
|
calendar_event = self.env['calendar.event'].create({
|
|
'name': f"[{record.contact_id.name}] {record.notes[:50] if record.notes else 'Event'}",
|
|
'start': record.date_start,
|
|
'stop': record.date_start + timedelta(hours=record.duration),
|
|
'user_id': record.user_id.id,
|
|
'partner_ids': [(6, 0, [record.user_id.partner_id.id])] if record.user_id and record.user_id.partner_id else [],
|
|
'description': description,
|
|
})
|
|
record.calendar_event_id = calendar_event.id
|
|
|
|
# Update next_contact for affected contacts with the event date
|
|
for record in records:
|
|
record.contact_id._update_next_contact(record.date_start)
|
|
|
|
return records
|
|
|
|
def write(self, vals):
|
|
result = super().write(vals)
|
|
|
|
# Update calendar event if needed
|
|
for record in self:
|
|
if record.calendar_event_id:
|
|
update_vals = {}
|
|
if 'notes' in vals or 'contact_id' in vals:
|
|
update_vals['name'] = f"[{record.contact_id.name}] {record.notes[:50] if record.notes else 'Event'}"
|
|
if 'date_start' in vals:
|
|
update_vals['start'] = record.date_start
|
|
update_vals['stop'] = record.date_start + timedelta(hours=record.duration)
|
|
if 'duration' in vals:
|
|
update_vals['stop'] = record.date_start + timedelta(hours=record.duration)
|
|
if 'user_id' in vals:
|
|
update_vals['user_id'] = record.user_id.id
|
|
update_vals['partner_ids'] = [(6, 0, [record.user_id.partner_id.id])] if record.user_id and record.user_id.partner_id else []
|
|
if 'notes' in vals or 'contact_id' in vals:
|
|
# Get base URL for creating contact link
|
|
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url', 'http://localhost:8069')
|
|
contact_url = f"{base_url}/web#id={record.contact_id.id}&model=dsrpt.contact&view_type=form"
|
|
|
|
# Prepare description with contact link
|
|
description = f"📱 Contact in Odoo: {contact_url}\n\n"
|
|
if record.notes:
|
|
description += record.notes
|
|
update_vals['description'] = description
|
|
|
|
if update_vals:
|
|
record.calendar_event_id.write(update_vals)
|
|
|
|
# Update next_contact for affected contacts if relevant fields changed
|
|
if any(field in vals for field in ['date_start', 'contact_id']):
|
|
for record in self:
|
|
record.contact_id._update_next_contact(record.date_start)
|
|
|
|
return result
|
|
|
|
@api.ondelete(at_uninstall=False)
|
|
def _unlink_calendar_events(self):
|
|
for record in self:
|
|
if record.calendar_event_id:
|
|
record.calendar_event_id.unlink()
|
|
|
|
def unlink(self):
|
|
# Store contacts before deletion for clearing next_contact
|
|
contacts = self.mapped('contact_id')
|
|
result = super().unlink()
|
|
# Clear next_contact for affected contacts after deletion
|
|
for contact in contacts:
|
|
contact._update_next_contact(None)
|
|
return result
|
|
|