[IMP] crm_claim, crm_fundraising: defined proper stage_find methods.
[odoo/odoo.git] / addons / crm_fundraising / crm_fundraising.py
1 #-*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 from crm import crm
24 from crm import wizard
25
26 wizard.mail_compose_message.SUPPORTED_MODELS.append('crm.fundraising')
27
28 class crm_fundraising(crm.crm_case, osv.osv):
29     """ Fund Raising Cases """
30
31     _name = "crm.fundraising"
32     _description = "Fund Raising"
33     _order = "id desc"
34     _inherit = ['mail.thread']
35     _columns = {
36             'id': fields.integer('ID', readonly=True),
37             'name': fields.char('Name', size=128, required=True),
38             'active': fields.boolean('Active', required=False),
39             'date_action_last': fields.datetime('Last Action', readonly=1),
40             'date_action_next': fields.datetime('Next Action', readonly=1),
41             'description': fields.text('Description'),
42             'create_date': fields.datetime('Creation Date' , readonly=True),
43             'write_date': fields.datetime('Update Date' , readonly=True),
44             'date_deadline': fields.date('Deadline'),
45             'user_id': fields.many2one('res.users', 'Responsible'),
46             'section_id': fields.many2one('crm.case.section', 'Sales Team', \
47                             select=True, help='Sales team to which Case belongs to. Define Responsible user and Email account for mail gateway.'),
48             'company_id': fields.many2one('res.company', 'Company'),
49             'partner_id': fields.many2one('res.partner', 'Partner'),
50             'email_cc': fields.text('Watchers Emails', size=252 , help="These email addresses will be added to the CC field of all inbound and outbound emails for this record before being sent. Separate multiple email addresses with a comma"),
51             'email_from': fields.char('Email', size=128, help="These people will receive email."),
52             'date_closed': fields.datetime('Closed', readonly=True),
53             'date': fields.datetime('Date'),
54             'priority': fields.selection(crm.AVAILABLE_PRIORITIES, 'Priority'),
55             'categ_id': fields.many2one('crm.case.categ', 'Category', \
56                                 domain="[('section_id','=',section_id),\
57                                 ('object_id.model', '=', 'crm.fundraising')]"),
58             'planned_revenue': fields.float('Planned Revenue'),
59             'planned_cost': fields.float('Planned Costs'),
60             'probability': fields.float('Probability (%)'),
61             'partner_name': fields.char("Employee's Name", size=64),
62             'partner_name2': fields.char('Employee Email', size=64),
63             'partner_phone': fields.char('Phone', size=32),
64             'partner_mobile': fields.char('Mobile', size=32),
65             'stage_id': fields.many2one ('crm.case.stage', 'Stage', domain="[('section_ids', '=', section_id)]"), 
66             'type_id': fields.many2one('crm.case.resource.type', 'Campaign', \
67                              domain="[('section_id','=',section_id)]"),
68             'duration': fields.float('Duration'),
69             'ref': fields.reference('Reference', selection=crm._links_get, size=128),
70             'ref2': fields.reference('Reference 2', selection=crm._links_get, size=128),
71             'state': fields.related('stage_id', 'state', type="selection", store=True,
72                     selection=crm.AVAILABLE_STATES, string="State", readonly=True,
73                     help='The state is set to \'Draft\', when a case is created.\
74                         If the case is in progress the state is set to \'Open\'.\
75                         When the case is over, the state is set to \'Done\'.\
76                         If the case needs to be reviewed then the state is \
77                         set to \'Pending\'.'),
78             'message_ids': fields.one2many('mail.message', 'res_id', 'Messages', domain=[('model','=',_name)]),
79         }
80
81     _defaults = {
82             'active': 1,
83             'user_id':  lambda s, cr, uid, c: s._get_default_user(cr, uid, c)
84             'partner_id':  lambda s, cr, uid, c: s._get_default_partner(cr, uid, c)
85             'email_from': lambda s, cr, uid, c: s._get_default_email(cr, uid, c)
86             'section_id': lambda s, cr, uid, c: s._get_default_section_id(cr, uid, c)
87             'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'crm.case', context=c),
88             'priority': crm.AVAILABLE_PRIORITIES[2][0],
89             'probability': 0.0,
90             'planned_cost': 0.0,
91             'planned_revenue': 0.0,
92     }
93
94     def stage_find(self, cr, uid, cases, section_id, domain=[], order='sequence', context=None):
95         """ Override of the base.stage method
96             Parameter of the stage search taken from the lead:
97             - type: stage type must be the same or 'both'
98             - section_id: if set, stages must belong to this section or
99               be a default case
100         """
101         if isinstance(cases, (int, long)):
102             cases = self.browse(cr, uid, cases, context=context)
103         domain = list(domain)
104         if section_id:
105                 domain += ['|', ('section_ids', '=', section_id), ('case_default', '=', True)]
106         for lead in cases:
107             lead_section_id = lead.section_id.id if lead.section_id else None
108             if lead_section_id:
109                 domain += ['|', ('section_ids', '=', lead_section_id), ('case_default', '=', True)]
110         stage_ids = self.pool.get('crm.case.stage').search(cr, uid, domain, order=order, context=context)
111         if stage_ids:
112             return stage_ids[0]
113         return False
114
115     def message_new(self, cr, uid, msg, custom_values=None, context=None):
116         """Automatically called when new email message arrives"""
117         res_id = super(crm_fundraising,self).message_new(cr, uid, msg, custom_values=custom_values, context=context)
118         vals = {
119             'name': msg.get('subject'),
120             'email_from': msg.get('from'),
121             'email_cc': msg.get('cc'),
122             'description': msg.get('body_text'),
123         }
124         priority = msg.get('priority')
125         if priority:
126             vals['priority'] = priority
127         vals.update(self.message_partner_by_email(cr, uid, msg.get('from')))
128         self.write(cr, uid, [res_id], vals, context=context)
129         return res_id
130
131
132 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: