[FIX]crm:fix the global name error of osv
[odoo/odoo.git] / addons / base_status / base_state.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-today OpenERP SA (<http://www.openerp.com>)
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
23 from tools.translate import _
24
25 class base_state(object):
26     """ Base utility mixin class for objects willing to manage their state.
27         Object subclassing this class should define the following colums:
28         - ``date_open`` (datetime field)
29         - ``date_closed`` (datetime field)
30         - ``user_id`` (many2one to res.users)
31         - ``partner_id`` (many2one to res.partner)
32         - ``state`` (selection field)
33     """
34
35     def _get_default_partner(self, cr, uid, context=None):
36         """ Gives id of partner for current user
37             :param context: if portal not in context returns False
38         """
39         if context is None:
40             context = {}
41         if not context or not context.get('portal'):
42             return False
43         user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
44         if hasattr(user, 'partner_address_id') and user.partner_address_id:
45             return user.partner_address_id
46         return user.company_id.partner_id.id
47
48     def _get_default_email(self, cr, uid, context=None):
49         """ Gives default email address for current user
50             :param context: if portal not in context returns False
51         """
52         if context is None:
53             context = {}
54         if not context or not context.get('portal'):
55             return False
56         user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
57         return user.user_email
58
59     def _get_default_user(self, cr, uid, context=None):
60         """ Gives current user id
61             :param context: if portal not in context returns False
62         """
63         if context is None:
64             context = {}
65         if not context or not context.get('portal'):
66             return False
67         return uid
68
69     def onchange_partner_address_id(self, cr, uid, ids, add, email=False):
70         """ This function returns value of partner email based on Partner Address
71             :param add: Id of Partner's address
72             :param email: Partner's email ID
73         """
74         data = {'value': {'email_from': False, 'phone':False}}
75         if add:
76             address = self.pool.get('res.partner').browse(cr, uid, add)
77             data['value'] = {'email_from': address and address.email or False ,
78                              'phone':  address and address.phone or False}
79         if 'phone' not in self._columns:
80             del data['value']['phone']
81         return data
82
83     def onchange_partner_id(self, cr, uid, ids, part, email=False):
84         """ This function returns value of partner address based on partner
85             :param part: Partner's id
86             :param email: Partner's email ID
87         """
88         data={}
89         if  part:
90             addr = self.pool.get('res.partner').address_get(cr, uid, [part], ['contact'])
91             data.update(self.onchange_partner_address_id(cr, uid, ids, addr['contact'])['value'])
92         return {'value': data}
93
94     def case_open(self, cr, uid, ids, context=None):
95         """ Opens case """
96         cases = self.browse(cr, uid, ids, context=context)
97         for case in cases:
98             values = {'active': True}
99             if case.state == 'draft':
100                 values['date_open'] = fields.datetime.now()
101             if not case.user_id:
102                 values['user_id'] = uid
103             self.case_set(cr, uid, [case.id], 'open', values, context=context)
104             self.case_open_send_note(cr, uid, [case.id], context=context)
105         return True
106
107     def case_close(self, cr, uid, ids, context=None):
108         """ Closes case """
109         self.case_set(cr, uid, ids, 'done', {'date_closed': fields.datetime.now()}, context=context)
110         self.case_close_send_note(cr, uid, ids, context=context)
111         return True
112
113     def case_cancel(self, cr, uid, ids, context=None):
114         """ Cancels case """
115         self.case_set(cr, uid, ids, 'cancel', {'active': True}, context=context)
116         self.case_cancel_send_note(cr, uid, ids, context=context)
117         return True
118
119     def case_pending(self, cr, uid, ids, context=None):
120         """ Sets case as pending """
121         self.case_set(cr, uid, ids, 'pending', {'active': True}, context=context)
122         self.case_pending_send_note(cr, uid, ids, context=context)
123         return True
124
125     def case_reset(self, cr, uid, ids, context=None):
126         """ Resets case as draft """
127         self.case_set(cr, uid, ids, 'draft', {'active': True}, context=context)
128         self.case_reset_send_note(cr, uid, ids, context=context)
129         return True
130     
131     def case_set(self, cr, uid, ids, state_name, update_values=None, context=None):
132         """ Generic method for setting case. This methods wraps the update
133             of the record, as well as call to _action and browse_record
134             case setting to fill the cache.
135             
136             :params: state_name: the new value of the state, such as 
137                      'draft' or 'close'.
138             :params: update_values: values that will be added with the state
139                      update when writing values to the record.
140         """
141         cases = self.browse(cr, uid, ids, context=context)
142         cases[0].state # fill browse record cache, for _action having old and new values
143         if update_values is None:
144             update_values = {}
145         update_values['state'] = state_name
146         self.write(cr, uid, ids, update_values, context=context)
147         self._action(cr, uid, cases, state_name, context=context)
148
149     def _action(self, cr, uid, cases, state_to, scrit=None, context=None):
150         if context is None:
151             context = {}
152         context['state_to'] = state_to
153         rule_obj = self.pool.get('base.action.rule')
154         model_obj = self.pool.get('ir.model')
155         model_ids = model_obj.search(cr, uid, [('model','=',self._name)])
156         rule_ids = rule_obj.search(cr, uid, [('model_id','=',model_ids[0])])
157         return rule_obj._action(cr, uid, rule_ids, cases, scrit=scrit, context=context)
158     
159     # ******************************
160     # Notifications
161     # ******************************
162     
163         def case_get_note_msg_prefix(self, cr, uid, id, context=None):
164                 return ''
165         
166     def case_open_send_note(self, cr, uid, ids, context=None):
167         for id in ids:
168             msg = _('%s has been <b>opened</b>.') % (self.case_get_note_msg_prefix(cr, uid, id, context=context))
169             self.message_append_note(cr, uid, [id], body=msg, context=context)
170         return True
171
172     def case_close_send_note(self, cr, uid, ids, context=None):
173         for id in ids:
174             msg = _('%s has been <b>closed</b>.') % (self.case_get_note_msg_prefix(cr, uid, id, context=context))
175             self.message_append_note(cr, uid, [id], body=msg, context=context)
176         return True
177
178     def case_cancel_send_note(self, cr, uid, ids, context=None):
179         for id in ids:
180             msg = _('%s has been <b>canceled</b>.') % (self.case_get_note_msg_prefix(cr, uid, id, context=context))
181             self.message_append_note(cr, uid, [id], body=msg, context=context)
182         return True
183
184     def case_pending_send_note(self, cr, uid, ids, context=None):
185         for id in ids:
186             msg = _('%s is now <b>pending</b>.') % (self.case_get_note_msg_prefix(cr, uid, id, context=context))
187             self.message_append_note(cr, uid, [id], body=msg, context=context)
188         return True
189
190     def case_reset_send_note(self, cr, uid, ids, context=None):
191         for id in ids:
192             msg = _('%s has been <b>renewed</b>.') % (self.case_get_note_msg_prefix(cr, uid, id, context=context))
193             self.message_append_note(cr, uid, [id], body=msg, context=context)
194         return True