130 lines
5.0 KiB
Python
130 lines
5.0 KiB
Python
|
# -*- 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 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(selection_add=[('cancel', 'Canceled')])
|
||
|
priority = fields.Selection([('0', 'Not evaluated'), ('1', 'Low'),
|
||
|
('2', 'Medium'), ('3', 'High'),
|
||
|
('4', 'Urgent')],
|
||
|
string='Priority', default='0', index=True)
|
||
|
|
||
|
partner_id = fields.Many2one('res.partner', string='Customer', index=True)
|
||
|
date = fields.Date('Date', 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')
|
||
|
action_type_id = fields.Many2one('crm.action.type', string='Type', required=True)
|
||
|
action_type_name = fields.Char(related='action_type_id.name')
|
||
|
|
||
|
@api.onchange('lead_id')
|
||
|
def onchange_lead(self):
|
||
|
""" If lead change, adapts partner """
|
||
|
for action in self:
|
||
|
if action.lead_id and action.lead_id.partner_id:
|
||
|
action.partner_id = action.lead_id.partner_id
|
||
|
|
||
|
@api.depends('action_type_id.name', 'details')
|
||
|
def _compute_display_name(self):
|
||
|
for action in self:
|
||
|
action.display_name = u'[{}]{}'.format(action.action_type_name,
|
||
|
action.details or u'')
|
||
|
|
||
|
@api.multi
|
||
|
def confirm(self):
|
||
|
""" Changes state to done """
|
||
|
self.state = 'done'
|
||
|
|
||
|
@api.multi
|
||
|
def set_to_draft(self):
|
||
|
""" Changes state to draft """
|
||
|
self.state = 'draft'
|
||
|
|
||
|
@api.multi
|
||
|
def cancel(self):
|
||
|
""" Cancel action """
|
||
|
self.ensure_one()
|
||
|
action = self[0]
|
||
|
return {'type': 'ir.actions.act_window',
|
||
|
'name': _('Cancel action'),
|
||
|
'res_model': 'dalby.crm.action.cancel.reason',
|
||
|
'view_mode': 'form',
|
||
|
'view_type': 'form',
|
||
|
'target': 'new',
|
||
|
'context': {'default_action_id': action.id}}
|
||
|
|
||
|
@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)
|
||
|
title = u'{} : {}'.format(action.action_type_name,
|
||
|
action.open_reason)
|
||
|
data = {'start': action.date,
|
||
|
'stop': action.date,
|
||
|
'duration': 1.0,
|
||
|
'allday': False,
|
||
|
'name': title,
|
||
|
'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()
|
||
|
|
||
|
|
||
|
class CrmActionType(models.Model):
|
||
|
""" CRM Action Type """
|
||
|
_name = 'crm.action.type'
|
||
|
_description = 'CRM Action Type'
|
||
|
_order = 'priority'
|
||
|
|
||
|
name = fields.Char('Name', required=True)
|
||
|
priority = fields.Integer('Priority', required=True, default=0)
|
||
|
active = fields.Boolean('Active', default=True)
|