1
# -*- encoding: utf-8 -*-
2
###########################################################################
3
# Module Writen to OpenERP, Open Source Management Solution
5
# Copyright (c) 2012 Vauxoo - http://www.vauxoo.com
8
############################################################################
9
# Coded by: fernandoL (fernando_ld@vauxoo.com)
10
############################################################################
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.
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.
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/>.
25
##############################################################################
26
from openerp.osv import osv, fields
27
from openerp.tools.translate import _
29
import openerp.netsvc as netsvc
30
from osv.orm import browse_record, browse_null
33
class procurement_order(osv.Model):
34
_inherit = 'procurement.order'
36
def do_merge(self, cr, uid, ids, context={}):
38
def make_key(br, fields):
41
field_val = getattr(br, field)
42
if field in ('product_id', 'location_id', 'procure_method'):
45
if isinstance(field_val, browse_record):
46
field_val = field_val.id
47
elif isinstance(field_val, browse_null):
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))
53
return tuple(list_key)
56
mrp_production_pool = self.pool.get('mrp.production')
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]
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
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))
103
for order_key, (order_data, old_ids) in new_orders.iteritems():
104
# skip merges with only one order
106
allorders += (old_ids or [])
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)