~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to procurement_order_merge/procurement.py

  • Committer: Sabrina Romero
  • Date: 2013-08-20 21:10:39 UTC
  • mto: (543.7.272 vaddddddd)
  • mto: This revision was merged to the branch mainline in revision 840.
  • Revision ID: sabrina@vauxoo.com-20130820211039-9jqrffvg2nz8q3vx

[ADD] Module product_do_merge added

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
###########################################################################
 
3
#    Module Writen to OpenERP, Open Source Management Solution
 
4
#
 
5
#    Copyright (c) 2012 Vauxoo - http://www.vauxoo.com
 
6
#    All Rights Reserved.
 
7
#    info@vauxoo.com
 
8
############################################################################
 
9
#    Coded by: fernandoL (fernando_ld@vauxoo.com)
 
10
############################################################################
 
11
#
 
12
#    This program is free software: you can redistribute it and/or modify
 
13
#    it under the terms of the GNU Affero General Public License as
 
14
#    published by the Free Software Foundation, either version 3 of the
 
15
#    License, or (at your option) any later version.
 
16
#
 
17
#    This program is distributed in the hope that it will be useful,
 
18
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
#    GNU Affero General Public License for more details.
 
21
#
 
22
#    You should have received a copy of the GNU Affero General Public License
 
23
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
24
#
 
25
##############################################################################
 
26
from openerp.osv import osv, fields
 
27
from openerp.tools.translate import _
 
28
 
 
29
import openerp.netsvc as netsvc
 
30
from osv.orm import browse_record, browse_null
 
31
 
 
32
 
 
33
class procurement_order(osv.Model):
 
34
    _inherit = 'procurement.order'
 
35
 
 
36
    def do_merge(self, cr, uid, ids, context={}):
 
37
 
 
38
        def make_key(br, fields):
 
39
            list_key = []
 
40
            for field in fields:
 
41
                field_val = getattr(br, field)
 
42
                if field in ('product_id', 'location_id', 'procure_method'):
 
43
                    if not field_val:
 
44
                        field_val = False
 
45
                if isinstance(field_val, browse_record):
 
46
                    field_val = field_val.id
 
47
                elif isinstance(field_val, browse_null):
 
48
                    field_val = False
 
49
                elif isinstance(field_val, list):
 
50
                    field_val = ((6, 0, tuple([v.id for v in field_val])),)
 
51
                list_key.append((field, field_val))
 
52
            list_key.sort()
 
53
            return tuple(list_key)
 
54
 
 
55
        new_orders = {}
 
56
        mrp_production_pool = self.pool.get('mrp.production')
 
57
        old_orders = []
 
58
 
 
59
        for procurement in self.browse(cr, uid, ids):
 
60
            if procurement.state == 'draft':
 
61
                order_key = make_key(procurement, (
 
62
                    'product_id', 'location_id', 'procure_method'))
 
63
                new_order = new_orders.setdefault(order_key, ({}, []))
 
64
                new_order[1].append(procurement.id)
 
65
                order_infos = new_order[0]
 
66
                if not order_infos:
 
67
                    order_infos.update({
 
68
                        'name': procurement.name,
 
69
                        'origin': procurement.origin,
 
70
                        'product_id': procurement.product_id.id,
 
71
                        'location_id': procurement.location_id.id,
 
72
                        'product_qty': self.pool.get('product.uom')._compute_qty(cr,
 
73
                            uid, procurement.product_uom.id,
 
74
                            procurement.product_qty,
 
75
                            to_uom_id=procurement.product_id.uom_id.id),
 
76
                        'product_uom': procurement.product_id.uom_id.id,
 
77
                        'procure_method': procurement.procure_method,
 
78
                        'production_ids': procurement.production_ids and
 
79
                            [(4, procurement.production_ids[0].id)] or False
 
80
                    })
 
81
                else:
 
82
                    if procurement.name:
 
83
                        order_infos['name'] = (order_infos['name'] or '') +\
 
84
                        ',' + procurement.name
 
85
                    if procurement.origin:
 
86
                        order_infos['origin'] = (order_infos['origin'] or '') +\
 
87
                        ',' + procurement.origin
 
88
                    if procurement.product_qty:
 
89
                        order_infos['product_qty'] =\
 
90
                        (order_infos['product_qty'] or 0) +\
 
91
                        self.pool.get('product.uom')._compute_qty(
 
92
                            cr, uid, procurement.product_uom.id,
 
93
                            procurement.product_qty,
 
94
                            to_uom_id=procurement.product_id.uom_id.id)
 
95
                    if procurement.production_ids:
 
96
                        order_infos['production_ids'].append((
 
97
                            4, procurement.production_ids[0].id))
 
98
 
 
99
        allorders = []
 
100
        neworders = []
 
101
        orders_info = {}
 
102
 
 
103
        for order_key, (order_data, old_ids) in new_orders.iteritems():
 
104
        # skip merges with only one order
 
105
            if len(old_ids) < 2:
 
106
                allorders += (old_ids or [])
 
107
                continue
 
108
 
 
109
            # create the new procurement order
 
110
            neworder_id = self.create(cr, uid, order_data)
 
111
            orders_info.update({neworder_id: old_ids})
 
112
            neworders.append(neworder_id)
 
113
            for old_id in old_ids:
 
114
                wf_service = netsvc.LocalService("workflow")
 
115
                wf_service.trg_validate(
 
116
                    uid, 'procurement.order', old_id, 'button_cancel', cr)
 
117
 
 
118
        return neworders