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: julio (julio@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
import decimal_precision as dp
30
class mrp_production(osv.Model):
31
_inherit = 'mrp.production'
33
def copy(self, cr, uid, id, default=None, context=None):
38
'variation_finished_product_ids': [],
40
return super(mrp_production, self).copy(cr, uid, id, default, context)
43
'variation_ids': fields.one2many('mrp.variation', 'production_id',
44
'Variation Product Consumed', readonly=True),
45
'variation_finished_product_ids': fields.one2many(
46
'mrp.variation.finished.product', 'production_id',
47
'Variation Product Finished', readonly=True),
50
def action_finish(self, cr, uid, ids, context={}):
51
res = super(mrp_production, self).action_finish(
52
cr, uid, ids, context=context)
53
self.create_variation_consumed(cr, uid, ids, context=context)
54
self.create_variation_finished_product(cr, uid, ids, context=context)
57
def create_variation(self, cr, uid, ids, real={}, planned={}, context={}):
58
prod_product = self.pool.get('product.product')
60
for production in self.browse(cr, uid, ids, context=context):
62
lista.extend(real.keys())
63
lista.extend(planned.keys())
64
lista = list(set(lista))
65
res_diff = dict(planned)
66
for product_id in lista:
67
res_diff.setdefault(product_id, 0)
68
res_diff[product_id] -= real.get(product_id, 0)
69
for val_diff in res_diff.items():
70
val = {'product_id': val_diff[0],
71
'quantity': (val_diff[1])*-1,
72
'product_uom': prod_product.browse(cr, uid,
73
val_diff[0]).uom_id.id,
74
'production_id': production.id
79
def create_res_real_planned(self, cr, uid, dat=False):
80
product_product = self.pool.get('product.product')
85
res.setdefault(lin['product_id'], 0)
86
product = product_product.browse(cr, uid, lin['product_id'])
87
qty_uom_convert = self.pool.get('product.uom')._compute_qty(cr,
88
uid, lin['product_uom'], lin['product_qty'],
89
to_uom_id=product.uom_id.id)
90
res[lin['product_id']] += qty_uom_convert
93
def create_consume_real(self, cr, uid, ids, context={}):
94
for production in self.browse(cr, uid, ids, context=context):
96
SELECT sm.product_uom,sm.product_id,
97
sum(COALESCE(sm.product_qty,0)) AS product_qty
98
FROM mrp_production_move_ids mpmi JOIN stock_move sm
100
WHERE mpmi.production_id=%s
102
GROUP BY sm.product_id,sm.product_uom
103
""", (production.id,))
104
dat = cr.dictfetchall()
105
res_real = self.create_res_real_planned(cr, uid, dat)
108
def create_consume_planned(self, cr, uid, ids, context={}):
109
for production in self.browse(cr, uid, ids, context=context):
111
SELECT product_id,sum(COALESCE(product_qty,0))
112
AS product_qty,product_uom
113
FROM mrp_production_product_line
114
WHERE production_id=%s
115
GROUP BY product_id,product_uom
116
""", (production.id,))
117
dat = cr.dictfetchall()
118
res_planned = self.create_res_real_planned(cr, uid, dat)
121
def create_finished_product_real(self, cr, uid, ids, context={}):
122
for production in self.browse(cr, uid, ids, context=context):
124
SELECT product_id,product_uom,sum(product_qty)
127
WHERE production_id=%s
129
GROUP BY product_id,product_uom
130
""", (production.id,))
131
dat = cr.dictfetchall()
132
res_real = self.create_res_real_planned(cr, uid, dat)
135
def create_finished_product_planned(self, cr, uid, ids, context={}):
136
for production in self.browse(cr, uid, ids, context=context):
138
SELECT product_id,sum(quantity) AS product_qty, product_uom
139
FROM mrp_pt_planified
140
WHERE production_id=%s
141
GROUP BY product_id,product_uom
142
""", (production.id,))
143
dat = cr.dictfetchall()
144
res_planned = self.create_res_real_planned(cr, uid, dat)
147
def create_variation_consumed(self, cr, uid, ids, context={}):
148
prod_variation_consumed = self.pool.get('mrp.variation')
149
for production in self.browse(cr, uid, ids, context=context):
150
prod_variation_consumed.unlink(cr, uid, map(
151
lambda x: x.id, production.variation_ids))
152
real = self.create_consume_real(cr, uid, ids, context=context)
153
planned = self.create_consume_planned(
154
cr, uid, ids, context=context)
155
res = self.create_variation(
156
cr, uid, ids, real, planned, context=context)
157
[prod_variation_consumed.create(cr, uid, lin) for lin in res]
160
def create_variation_finished_product(self, cr, uid, ids, context={}):
161
prod_variation_finished_product = self.pool.get(
162
'mrp.variation.finished.product')
163
for production in self.browse(cr, uid, ids, context=context):
164
prod_variation_finished_product.unlink(cr, uid, map(
165
lambda x: x.id, production.variation_finished_product_ids))
166
real = self.create_finished_product_real(
167
cr, uid, ids, context=context)
168
planned = self.create_finished_product_planned(
169
cr, uid, ids, context=context)
170
res = self.create_variation(
171
cr, uid, ids, real, planned, context=context)
172
[prod_variation_finished_product.create(
173
cr, uid, lin) for lin in res]
177
class mrp_variation(osv.Model):
178
_name = 'mrp.variation'
179
_rec_name = 'product_id'
181
def _get_variation_cost(self, cr, uid, ids, field_name, args, context={}):
183
for variation in self.browse(cr, uid, ids, context=context):
184
res[variation.id] = variation.quantity * \
185
variation.product_id.standard_price
189
'product_id': fields.many2one('product.product', 'Product'),
190
'quantity': fields.float('Quantity',
191
digits_compute=dp.get_precision('Product UoM')),
192
'production_id': fields.many2one('mrp.production', 'production'),
193
'product_uom': fields.many2one('product.uom', 'UoM'),
194
'cost_variation': fields.function(_get_variation_cost, type='float',
195
digits_compute=dp.get_precision('Purchase Price'),
196
string='Variation Cost')
200
class mrp_variation_finished_product(osv.Model):
201
_name = 'mrp.variation.finished.product'
202
_rec_name = 'product_id'
204
def _get_variation_cost(self, cr, uid, ids, field_name, args, context={}):
206
for variation in self.browse(cr, uid, ids, context=context):
207
res[variation.id] = variation.quantity * \
208
variation.product_id.standard_price
212
'product_id': fields.many2one('product.product', 'Product'),
213
'quantity': fields.float('Quantity',
214
digits_compute=dp.get_precision('Product UoM')),
215
'production_id': fields.many2one('mrp.production', 'production'),
216
'product_uom': fields.many2one('product.uom', 'UoM'),
217
'cost_variation': fields.function(_get_variation_cost, type='float',
218
digits_compute=dp.get_precision('Purchase Price'),
219
string='Variation Cost')