[FIX]
[odoo/odoo.git] / addons / crm / wizard / crm_merge_opportunities.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 from osv import osv, fields
22 from tools.translate import _
23
24 class crm_merge_opportunity(osv.osv_memory):
25     """Merge two Opportunities"""
26
27     _name = 'crm.merge.opportunity'
28     _description = 'Merge two Opportunities'
29
30     def _get_first_not_null_id(self, attr, ops):
31         for op in ops:
32             if hasattr(op, attr) and getattr(op, attr):
33                 return getattr(op, attr).id
34         return False
35
36     def _get_first_not_null(self, attr, ops):
37         for op in ops:
38             if hasattr(op, attr) and getattr(op, attr):
39                 return getattr(op, attr)
40         return False
41
42     def _concat_all(self, attr, ops):
43         return ', '.join([getattr(op, attr) for op in ops if hasattr(op, attr) and getattr(op, attr)])
44
45
46     def get_attachments(self, cr, uid, id, context=None):
47         attach_obj = self.pool.get('ir.attachment')
48         result = []
49         attach_ids = attach_obj.search(cr, uid, [('res_model' , '=', 'crm.lead'), ('res_id', '=', id)])
50         return attach_ids
51
52     def set_attachements_res_id(self, cr, uid, op_id, attach_ids, context=None):
53         attach_obj = self.pool.get('ir.attachment')
54         attach_obj.write(cr, uid, attach_ids, {'res_id' : op_id})
55
56
57
58     def merge(self, cr, uid, op_ids, context=None):
59         """
60             @param opp_ids : list of opportunities ids to merge
61         """
62         opp_obj = self.pool.get('crm.lead')
63         message_obj = self.pool.get('mailgate.message')
64
65         lead_ids = context and context.pop('lead_ids', []) or []
66
67         if len(op_ids) <= 1:
68             raise osv.except_osv(_('Warning !'),_('Please select more than one opportunities.'))
69
70         opportunities = opp_obj.browse(cr, uid, lead_ids, context=context)
71         opportunities_list = list(set(op_ids) - set(opportunities))
72
73         if opportunities :
74             first_opportunity = opportunities[0]
75             tail_opportunities = opportunities_list
76         else:
77             first_opportunity = opportunities_list[0]
78             tail_opportunities = opportunities_list[1:]
79
80         data = {
81                 'partner_id': self._get_first_not_null_id('partner_id', op_ids),  # !!
82                 'title': self._get_first_not_null_id('title', op_ids),
83                 'name' : self._concat_all('name', op_ids),  #not lost
84                 'categ_id' : self._get_first_not_null_id('categ_id', op_ids), # !!
85                 'channel_id' : self._get_first_not_null_id('channel_id', op_ids), # !!
86                 'city' : self._get_first_not_null('city', op_ids),  # !!
87                 'company_id' : self._get_first_not_null_id('company_id', op_ids), #!!
88                 'contact_name' : self._concat_all('contact_name', op_ids), #not lost
89                 'country_id' : self._get_first_not_null_id('country_id', op_ids), #!!
90                 'partner_address_id' : self._get_first_not_null_id('partner_address_id', op_ids), #!!
91                 'partner_assigned_id' : hasattr(opp_obj,'partner_assigned_id') and self._get_first_not_null_id('partner_assigned_id', op_ids), #!!
92                 'type_id' : self._get_first_not_null_id('type_id', op_ids), #!!
93                 'user_id' : self._get_first_not_null_id('user_id', op_ids), #!!
94                 'section_id' : self._get_first_not_null_id('section_id', op_ids), #!!
95                 'state_id' : self._get_first_not_null_id('state_id', op_ids),
96                 'description' : self._concat_all('description', op_ids),  #not lost
97                 'email' : self._get_first_not_null('email', op_ids), # !!
98                 'fax' : self._get_first_not_null('fax', op_ids),
99                 'mobile' : self._get_first_not_null('mobile', op_ids),
100                 'partner_latitude' : hasattr(opp_obj,'partner_latitude') and self._get_first_not_null('partner_latitude', op_ids),
101                 'partner_longitude' : hasattr(opp_obj,'partner_longitude') and self._get_first_not_null('partner_longitude', op_ids),
102                 'partner_name' : self._get_first_not_null('partner_name', op_ids),
103                 'phone' : self._get_first_not_null('phone', op_ids),
104                 'probability' : self._get_first_not_null('probability', op_ids),
105                 'planned_revenue' : self._get_first_not_null('planned_revenue', op_ids),
106                 'street' : self._get_first_not_null('street', op_ids),
107                 'street2' : self._get_first_not_null('street2', op_ids),
108                 'zip' : self._get_first_not_null('zip', op_ids),
109
110             }
111
112         #copy message into the first opportunity + merge attachement
113         for opp in tail_opportunities:
114             attach_ids = self.get_attachments(cr, uid, opp, context=context)
115             self.set_attachements_res_id(cr, uid, first_opportunity.id, attach_ids)
116             for history in opp.message_ids:
117                 new_history = message_obj.copy(cr, uid, history.id, default={'res_id': opp.id})
118
119         #Notification about loss of information
120         details = []
121         subject = ['Merged opportunities :']
122         for opp in op_ids:
123             subject.append(opp.name)
124             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 '',
125                         opp.stage_id.name or '',
126                         opp.section_id.name or '',
127                         opp.user_id.name or '',
128                         opp.categ_id.name or '',
129                         opp.channel_id.name or '',
130                         opp.company_id.name or '',
131                         opp.contact_name or '',
132                         opp.email_from or '',
133                         opp.phone or '',
134                         opp.fax or '',
135                         opp.mobile or '',
136                         opp.state_id.name or '',
137                         opp.description or '',
138                         opp.probability or '',
139                         opp.planned_revenue or '',
140                         opp.country_id.name or '',
141                         opp.city or '',
142                         opp.street or '',
143                         opp.street2 or '',
144                         opp.zip or '',
145                         ))
146         subject = subject[0] + ", ".join(subject[1:])
147         details = "\n\n".join(details)
148
149         opp_obj._history(cr, uid, [first_opportunity], subject, details=details)
150
151         #data.update({'message_ids' : [(6, 0 ,self._concat_o2m('message_ids', op_ids))]})
152         opp_obj.write(cr, uid, [first_opportunity.id], data)
153
154         unlink_ids = map(lambda x: x.id, tail_opportunities)
155         opp_obj.unlink(cr, uid, unlink_ids, context=context)
156
157         models_data = self.pool.get('ir.model.data')
158
159
160
161         # Get Opportunity views
162         result = models_data._get_id(
163             cr, uid, 'crm', 'view_crm_case_opportunities_filter')
164         opportunity_view_form = models_data._get_id(
165             cr, uid, 'crm', 'crm_case_form_view_oppor')
166         opportunity_view_tree = models_data._get_id(
167             cr, uid, 'crm', 'crm_case_tree_view_oppor')
168         if opportunity_view_form:
169             opportunity_view_form = models_data.browse(
170                 cr, uid, opportunity_view_form, context=context).res_id
171         if opportunity_view_tree:
172             opportunity_view_tree = models_data.browse(
173                 cr, uid, opportunity_view_tree, context=context).res_id
174
175         return {
176                 'name': _('Opportunity'),
177                 'view_type': 'form',
178                 'view_mode': 'tree, form',
179                 'res_model': 'crm.lead',
180                 'domain': [('type', '=', 'opportunity')],
181                 'res_id': int(first_opportunity.id),
182                 'view_id': False,
183                 'views': [(opportunity_view_form, 'form'),
184                           (opportunity_view_tree, 'tree'),
185                           (False, 'calendar'), (False, 'graph')],
186                 'type': 'ir.actions.act_window',
187         }
188
189
190     def action_merge(self, cr, uid, ids, context=None):
191         obj_opportunity = self.browse(cr, uid, ids[0], context=context)
192         op_ids = obj_opportunity.opportunity_ids
193
194         self.write(cr, uid, ids, {'opportunity_ids' : [(6,0, [op_ids[0].id])]}, context=context)
195         return self.merge(cr, uid, op_ids, context)
196
197
198     _columns = {
199         'opportunity_ids' : fields.many2many('crm.lead',  'merge_opportunity_rel', 'merge_id', 'opportunity_id', 'Opportunities', domain=[('type', '=', 'opportunity')]),
200     }
201
202     def default_get(self, cr, uid, fields, context=None):
203         """
204         This function gets default values
205         @param self: The object pointer
206         @param cr: the current row, from the database cursor,
207         @param uid: the current user’s ID for security checks,
208         @param fields: List of fields for default value
209         @param context: A standard dictionary for contextual values
210
211         @return : default values of fields.
212         """
213         record_ids = context and context.get('active_ids', False) or False
214         res = super(crm_merge_opportunity, self).default_get(cr, uid, fields, context=context)
215
216         if record_ids:
217             if 'opportunity_ids' in fields:
218                 res.update({'opportunity_ids': record_ids})
219
220         return res
221
222 crm_merge_opportunity()
223
224 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: