[IMP]improve company form view as per review. improve email_template,user form and...
[odoo/odoo.git] / addons / purchase / wizard / purchase_order_group.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 import time
22
23 from osv import fields, osv
24 import netsvc
25 import pooler
26 from osv.orm import browse_record, browse_null
27 from tools.translate import _
28
29 class purchase_order_group(osv.osv_memory):
30     _name = "purchase.order.group"
31     _description = "Purchase Order Merge"
32
33     def fields_view_get(self, cr, uid, view_id=None, view_type='form',
34                         context=None, toolbar=False, submenu=False):
35         """
36          Changes the view dynamically
37          @param self: The object pointer.
38          @param cr: A database cursor
39          @param uid: ID of the user currently logged in
40          @param context: A standard dictionary
41          @return: New arch of view.
42         """
43         if context is None:
44             context={}
45         res = super(purchase_order_group, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar,submenu=False)
46         if context.get('active_model','') == 'purchase.order' and len(context['active_ids']) < 2:
47             raise osv.except_osv(_('Warning'),
48             _('Please select multiple order to merge in the list view.'))
49         return res
50     def merge_orders(self, cr, uid, ids, context=None):
51         """
52              To merge similar type of purchase orders.
53
54              @param self: The object pointer.
55              @param cr: A database cursor
56              @param uid: ID of the user currently logged in
57              @param ids: the ID or list of IDs
58              @param context: A standard dictionary
59
60              @return: purchase order view
61
62         """
63         order_obj = self.pool.get('purchase.order')
64         proc_obj = self.pool.get('procurement.order')
65         mod_obj =self.pool.get('ir.model.data')
66         if context is None:
67             context = {}
68         result = mod_obj._get_id(cr, uid, 'purchase', 'view_purchase_order_filter')
69         id = mod_obj.read(cr, uid, result, ['res_id'])
70
71         allorders = order_obj.do_merge(cr, uid, context.get('active_ids',[]), context)
72         for new_order in allorders:
73             proc_ids = proc_obj.search(cr, uid, [('purchase_id', 'in', allorders[new_order])], context=context)
74             for proc in proc_obj.browse(cr, uid, proc_ids, context=context):
75                 if proc.purchase_id:
76                     proc_obj.write(cr, uid, [proc.id], {'purchase_id': new_order}, context)
77
78         return {
79             'domain': "[('id','in', [" + ','.join(map(str, allorders.keys())) + "])]",
80             'name': _('Purchase Orders'),
81             'view_type': 'form',
82             'view_mode': 'tree,form',
83             'res_model': 'purchase.order',
84             'view_id': False,
85             'type': 'ir.actions.act_window',
86             'search_view_id': id['res_id']
87         }
88
89 purchase_order_group()
90
91 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: