yaltik_odoo_custom/yaltik_crm_action/models/crm_action.py

143 lines
5.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# Copyright 2017 Fabien Bourgeois <fabien@yaltik.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
""" CRM Action """
from datetime import timedelta
from odoo import models, fields, api, _
class CrmAction(models.Model):
""" CRM Action """
_name = 'crm.action'
_description = 'CRM Action'
_rec_name = 'display_name'
display_name = fields.Char(compute='_compute_display_name', store=True)
state = fields.Selection([('draft', 'Todo'), ('done', 'Done'),
('cancel', 'Canceled')], string='Status',
2017-08-09 17:07:49 +02:00
required=True, default='draft')
priority = fields.Selection([('0', 'Not evaluated'), ('1', 'Low'),
('2', 'Medium'), ('3', 'High'),
('4', 'Urgent')],
default='0', index=True)
details = fields.Text()
partner_id = fields.Many2one('res.partner', string='Customer', index=True)
parent_id = fields.Many2one(related='partner_id.parent_id')
date = fields.Datetime(required=True, default=fields.Date.context_today)
user_id = fields.Many2one('res.users', string='User', required=True,
default=lambda self: self.env.user)
lead_id = fields.Many2one('crm.lead', string='Lead', ondelete='cascade')
event_id = fields.Many2one('calendar.event', 'Calendar event', ondelete='cascade',
copy=False)
action_type_id = fields.Many2one(
'crm.action.type', string='Type', required=True,
default=lambda self: self.env['crm.action.type'].search([], limit=1))
action_type_name = fields.Char(related='action_type_id.name')
@api.onchange('partner_id')
def onchange_partner(self):
""" If partner changes, adapts lead to False """
for action in self:
if action.partner_id and action.lead_id:
if action.lead_id.partner_id != action.partner_id:
action.lead_id = False
@api.depends('action_type_id.name', 'details')
def _compute_display_name(self):
for action in self:
2017-08-09 17:07:49 +02:00
details = u'...'
if action.details:
details = u' '.join(action.details.split()[:5]) + details
action.display_name = u'[{}]{}'.format(action.action_type_name,
2017-08-09 17:07:49 +02:00
details)
@api.multi
def set_to_draft(self):
""" Set actions to draft """
self.write({'state': 'draft'})
@api.multi
def set_to_done(self):
""" Set actions to done """
self.write({'state': 'done'})
@api.multi
def create_linked_event(self):
""" Creates a linked event """
for action in self:
if not action.event_id:
attendees = [action.user_id.partner_id.id]
if action.partner_id:
attendees.append(action.partner_id.id)
data = {'start': action.date,
'stop': action.date,
'duration': 1.0,
'allday': False,
'name': action.display_name,
'description': action.details,
'privacy': 'confidential',
'partner_ids': [(6, False, attendees)],
'partner_location_id': action.partner_id.id or False}
action.event_id = self.env['calendar.event'].create(data)
return {'type': 'ir.actions.act_window',
'name': _('Linked event'),
'res_model': 'calendar.event',
'res_id': action.event_id.id,
'view_mode': 'form',
'view_type': 'form',
'flags': {'initial_mode': 'edit'}}
@api.multi
def remove_linked_event(self):
""" Removes linked event and deletes it """
for action in self:
if action.event_id:
event = action.event_id
action.event_id = False
event.unlink()
@api.multi
def write(self, vals):
""" Ensures date event and basic partner_id coherence """
super(CrmAction, self).write(vals)
for action in self:
if action.event_id:
if action.event_id.start != action.date:
stop = fields.Datetime.from_string(action.date) + \
timedelta(hours=action.event_id.duration)
action.event_id.write({'start': action.date,
'stop': fields.Datetime.to_string(stop)})
if action.partner_id and \
action.partner_id not in action.event_id.partner_ids:
action.event_id.partner_ids |= action.partner_id
return True
class CrmActionType(models.Model):
""" CRM Action Type """
_name = 'crm.action.type'
_description = 'CRM Action Type'
_order = 'priority'
name = fields.Char(required=True)
priority = fields.Integer(required=True, default=0)
active = fields.Boolean(default=True)