fix
[odoo/odoo.git] / addons / crm / wizard / crm_merge_opportunities.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20 from osv import osv, fields
21 from tools.translate import _
22
23 class crm_merge_opportunity(osv.osv_memory):
24     """Merge two Opportunities"""
25
26     _name = 'crm.merge.opportunity'
27     _description = 'Merge two Opportunities'
28
29     def _get_first_not_null_id(self, attr, ops, oldest):
30         if hasattr(oldest, attr) and getattr(oldest, attr):
31             return getattr(oldest, attr).id
32         
33         for op in ops:
34             if hasattr(op, attr) and getattr(op, attr):
35                 return getattr(op, attr).id
36         return False
37
38     def _get_first_not_null(self, attr, ops, oldest):
39         if hasattr(oldest, attr) and getattr(oldest, attr):
40             return getattr(oldest, attr)
41         
42         for op in ops:
43             if hasattr(op, attr) and getattr(op, attr):
44                 return getattr(op, attr)
45         return False
46
47     def _concat_all(self, attr, ops):
48         return ', '.join([getattr(op, attr) for op in ops if hasattr(op, attr) and getattr(op, attr)])
49
50
51     def get_attachments(self, cr, uid, id, context=None):
52         proxy = self.pool.get('ir.attachment')
53         ids = proxy.search(cr, uid, [('res_model', '=', 'crm.lead'), ('res_id', '=', id)], context=context)
54         return proxy.browse(cr, uid, ids, context=context)
55
56     def find_oldest(self, cr, uid, op_ids, context=None):
57         if not context:
58             context = {}
59         ids = [op_id.id for op_id in op_ids]
60         if context.get('convert'):
61             ids = list(set(ids) - set(context.get('lead_ids', False)) )
62         lead_obj = self.pool.get('crm.lead')
63         op_id = lead_obj.search(cr, uid, [('id', 'in', ids)], order='create_date' , context=context)
64         if not op_id:
65             return False
66         opps = lead_obj.browse(cr, uid, [op_id[0]], context=context)
67         return opps[0]
68
69     def _update_data(self, op_ids, oldest_opp):
70         data = {
71                 'partner_id': self._get_first_not_null_id('partner_id', op_ids, oldest_opp),  # !!
72                 'title': self._get_first_not_null_id('title', op_ids, oldest_opp),
73                 'name' : self._get_first_not_null('name', op_ids, oldest_opp),  #not lost
74                 'categ_id' : self._get_first_not_null_id('categ_id', op_ids, oldest_opp), # !!
75                 'channel_id' : self._get_first_not_null_id('channel_id', op_ids, oldest_opp), # !!
76                 'city' : self._get_first_not_null('city', op_ids, oldest_opp),  # !!
77                 'company_id' : self._get_first_not_null_id('company_id', op_ids, oldest_opp), #!!
78                 'contact_name' : self._get_first_not_null('contact_name', op_ids, oldest_opp), #not lost
79                 'country_id' : self._get_first_not_null_id('country_id', op_ids, oldest_opp), #!!
80                 'partner_address_id' : self._get_first_not_null_id('partner_address_id', op_ids, oldest_opp), #!!
81                 'type_id' : self._get_first_not_null_id('type_id', op_ids, oldest_opp), #!!
82                 'user_id' : self._get_first_not_null_id('user_id', op_ids, oldest_opp), #!!
83                 'section_id' : self._get_first_not_null_id('section_id', op_ids, oldest_opp), #!!
84                 'state_id' : self._get_first_not_null_id('state_id', op_ids, oldest_opp),
85                 'description' : self._concat_all('description', op_ids),  #not lost
86                 'email' : self._get_first_not_null('email', op_ids, oldest_opp), # !!
87                 'fax' : self._get_first_not_null('fax', op_ids, oldest_opp),
88                 'mobile' : self._get_first_not_null('mobile', op_ids, oldest_opp),
89                 'partner_name' : self._get_first_not_null('partner_name', op_ids, oldest_opp),
90                 'phone' : self._get_first_not_null('phone', op_ids, oldest_opp),
91                 'probability' : self._get_first_not_null('probability', op_ids, oldest_opp),
92                 'planned_revenue' : self._get_first_not_null('planned_revenue', op_ids, oldest_opp),
93                 'street' : self._get_first_not_null('street', op_ids, oldest_opp),
94                 'street2' : self._get_first_not_null('street2', op_ids, oldest_opp),
95                 'zip' : self._get_first_not_null('zip', op_ids, oldest_opp),
96                 'state' : 'open',
97                 'create_date' : self._get_first_not_null('create_date', op_ids, oldest_opp),
98                 'date_action_last': self._get_first_not_null('date_action_last', op_ids, oldest_opp),
99                 'date_action_next': self._get_first_not_null('date_action_next', op_ids, oldest_opp),
100                 'email_from' : self._get_first_not_null('email_from', op_ids, oldest_opp),
101                 'email_cc' : self._get_first_not_null('email_cc', op_ids, oldest_opp),
102                 'partner_name' : self._get_first_not_null('partner_name', op_ids, oldest_opp),
103         }
104         return data
105
106     def merge(self, cr, uid, op_ids, context=None):
107         """
108             :param opp_ids: list of opportunities ids to merge
109         """
110         opp_obj = self.pool.get('crm.lead')
111         message_obj = self.pool.get('mail.message')
112
113         lead_ids = context and context.get('lead_ids', []) or []
114
115         if len(op_ids) <= 1:
116             raise osv.except_osv(_('Warning !'),_('Please select more than one opportunities.'))
117
118         opportunities = opp_obj.browse(cr, uid, lead_ids, context=context)
119         opportunities_list = list(set(op_ids) - set(opportunities))
120         oldest_opp = self.find_oldest(cr, uid, op_ids, context=context)
121         if opportunities :
122             first_opportunity = opportunities[0]
123             tail_opportunities = opportunities_list
124         else:
125             first_opportunity = opportunities_list[0]
126             tail_opportunities = opportunities_list[1:]
127         data = self._update_data(op_ids, oldest_opp)
128
129         #copy message into the first opportunity + merge attachement
130         count = 1
131         first_attachments = self.get_attachments(cr, uid, first_opportunity, context=context)
132         for opp in tail_opportunities:
133             attachments = self.get_attachments(cr, uid, opp, context=context)
134             for first in first_attachments:
135                 for attachment in attachments:
136                     if attachment.name == first.name:
137                         values = dict(
138                             name = "%s (%s)" % (attachment.name, count,),
139                             res_id = first_opportunity.id,
140                         )
141                         attachment.write(values)
142                         count+=1
143                     
144             for history in opp.message_ids:
145                 message_obj.write(cr, uid, history.id, {'res_id': first_opportunity.id, 'name' : _("From %s : %s") % (opp.name, history.name) }, context=context)
146
147         #Notification about loss of information
148         details = []
149         subject = ['Merged opportunities :']
150         for opp in op_ids:
151             subject.append(opp.name)
152             details.append(_('Merged Opportunity: %s\n  Partner: %s\n  Stage: %s\n  Section: %s\n   Salesman: %s\n   Category: %s\n   Channel: %s\n   Company: %s\n   Contact name: %s\n   Email: %s\n   Phone number: %s\n   Fax: %s\n   Mobile: %s\n   State: %s\n   Description: %s\n   Probability: %s\n   Planned revennue: %s\n   Country: %s\n   City: %s\n   Street: %s\n   Street 2: %s\n   Zip 2: %s')  % ( opp.name, opp.partner_id.name or '',
153                         opp.stage_id.name or '',
154                         opp.section_id.name or '',
155                         opp.user_id.name or '',
156                         opp.categ_id.name or '',
157                         opp.channel_id.name or '',
158                         opp.company_id.name or '',
159                         opp.contact_name or '',
160                         opp.email_from or '',
161                         opp.phone or '',
162                         opp.fax or '',
163                         opp.mobile or '',
164                         opp.state_id.name or '',
165                         opp.description or '',
166                         opp.probability or '',
167                         opp.planned_revenue or '',
168                         opp.country_id.name or '',
169                         opp.city or '',
170                         opp.street or '',
171                         opp.street2 or '',
172                         opp.zip or '',
173                         ))
174         subject = subject[0] + ", ".join(subject[1:])
175         details = "\n\n".join(details)
176
177         opp_obj.message_append(cr, uid, [first_opportunity], subject, body_text=details)
178         #data.update({'message_ids' : [(6, 0 ,self._concat_o2m('message_ids', op_ids))]})
179         opp_obj.write(cr, uid, [first_opportunity.id], data, context=context)
180         unlink_ids = map(lambda x: x.id, tail_opportunities)
181         opp_obj.unlink(cr, uid, unlink_ids, context=context)
182
183         models_data = self.pool.get('ir.model.data')
184
185         # Get Opportunity views
186         opportunity_view_form = models_data._get_id(
187             cr, uid, 'crm', 'crm_case_form_view_oppor')
188         opportunity_view_tree = models_data._get_id(
189             cr, uid, 'crm', 'crm_case_tree_view_oppor')
190         if opportunity_view_form:
191             opportunity_view_form = models_data.browse(
192                 cr, uid, opportunity_view_form, context=context).res_id
193         if opportunity_view_tree:
194             opportunity_view_tree = models_data.browse(
195                 cr, uid, opportunity_view_tree, context=context).res_id
196
197         return {
198                 'name': _('Opportunity'),
199                 'view_type': 'form',
200                 'view_mode': 'tree, form',
201                 'res_model': 'crm.lead',
202                 'domain': [('type', '=', 'opportunity')],
203                 'res_id': int(first_opportunity.id),
204                 'view_id': False,
205                 'views': [(opportunity_view_form, 'form'),
206                           (opportunity_view_tree, 'tree'),
207                           (False, 'calendar'), (False, 'graph')],
208                 'type': 'ir.actions.act_window',
209         }
210
211
212     def action_merge(self, cr, uid, ids, context=None):
213         obj_opportunity = self.browse(cr, uid, ids[0], context=context)
214         op_ids = obj_opportunity.opportunity_ids
215         self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [op_ids[0].id])]}, context=context)
216         context['lead_ids'] = [op_ids[0].id]
217         return self.merge(cr, uid, op_ids, context)
218
219     _columns = {
220         'opportunity_ids' : fields.many2many('crm.lead',  'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),
221     }
222
223     def default_get(self, cr, uid, fields, context=None):
224         """
225         This function gets default values
226         @param self: The object pointer
227         @param cr: the current row, from the database cursor,
228         @param uid: the current users ID for security checks,
229         @param fields: List of fields for default value
230         @param context: A standard dictionary for contextual values
231
232         @return : default values of fields.
233         """
234         record_ids = context and context.get('active_ids', False) or False
235         res = super(crm_merge_opportunity, self).default_get(cr, uid, fields, context=context)
236
237         if record_ids:
238             opp_ids = []
239             opps = self.pool.get('crm.lead').browse(cr, uid, record_ids, context=context)
240             for opp in opps:
241                 if opp.state not in ('done', 'cancel'):
242                     opp_ids.append(opp.id)
243             if 'opportunity_ids' in fields:
244                 res.update({'opportunity_ids': opp_ids})
245
246         return res
247
248 crm_merge_opportunity()
249
250 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: