2017-08-09 14:55:17 +02:00
|
|
|
# -*- 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 """
|
|
|
|
|
2017-10-29 10:56:53 +01:00
|
|
|
from datetime import timedelta
|
2017-08-09 14:55:17 +02:00
|
|
|
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)
|
|
|
|
|
2017-08-09 15:50:25 +02:00
|
|
|
state = fields.Selection([('draft', 'Todo'), ('done', 'Done'),
|
|
|
|
('cancel', 'Canceled')], string='Status',
|
2017-08-09 17:07:49 +02:00
|
|
|
required=True, default='draft')
|
2017-08-09 14:55:17 +02:00
|
|
|
priority = fields.Selection([('0', 'Not evaluated'), ('1', 'Low'),
|
|
|
|
('2', 'Medium'), ('3', 'High'),
|
|
|
|
('4', 'Urgent')],
|
2017-10-29 10:58:17 +01:00
|
|
|
default='0', index=True)
|
2017-08-09 15:50:25 +02:00
|
|
|
details = fields.Text()
|
2017-08-09 14:55:17 +02:00
|
|
|
|
|
|
|
partner_id = fields.Many2one('res.partner', string='Customer', index=True)
|
2017-11-26 08:18:38 +01:00
|
|
|
parent_id = fields.Many2one(related='partner_id.parent_id')
|
2017-10-29 10:58:17 +01:00
|
|
|
date = fields.Datetime(required=True, default=fields.Date.context_today)
|
2017-08-09 14:55:17 +02:00
|
|
|
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')
|
2018-03-14 07:12:14 +01:00
|
|
|
event_id = fields.Many2one('calendar.event', 'Calendar event', ondelete='cascade',
|
|
|
|
copy=False)
|
2017-10-29 10:12:22 +01:00
|
|
|
action_type_id = fields.Many2one(
|
|
|
|
'crm.action.type', string='Type', required=True,
|
|
|
|
default=lambda self: self.env['crm.action.type'].search([], limit=1))
|
2017-08-09 14:55:17 +02:00
|
|
|
action_type_name = fields.Char(related='action_type_id.name')
|
|
|
|
|
2017-10-29 10:17:36 +01:00
|
|
|
@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
|
|
|
|
|
|
|
|
|
2017-08-09 14:55:17 +02:00
|
|
|
@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
|
2017-08-09 14:55:17 +02:00
|
|
|
action.display_name = u'[{}]{}'.format(action.action_type_name,
|
2017-08-09 17:07:49 +02:00
|
|
|
details)
|
2017-11-23 07:12:16 +01:00
|
|
|
@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'})
|
2017-08-09 14:55:17 +02:00
|
|
|
|
|
|
|
@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,
|
2017-08-09 15:50:25 +02:00
|
|
|
'name': action.display_name,
|
2017-08-09 14:55:17 +02:00
|
|
|
'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()
|
|
|
|
|
2017-10-29 10:56:53 +01:00
|
|
|
@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
|
|
|
|
|
2017-08-09 14:55:17 +02:00
|
|
|
|
|
|
|
class CrmActionType(models.Model):
|
|
|
|
""" CRM Action Type """
|
|
|
|
_name = 'crm.action.type'
|
|
|
|
_description = 'CRM Action Type'
|
|
|
|
_order = 'priority'
|
|
|
|
|
2017-10-29 10:58:17 +01:00
|
|
|
name = fields.Char(required=True)
|
|
|
|
priority = fields.Integer(required=True, default=0)
|
|
|
|
active = fields.Boolean(default=True)
|