1
# -*- encoding: utf-8 -*-
2
###########################################################################
3
# Module Writen to OpenERP, Open Source Management Solution
5
# Copyright (c) 2013 Vauxoo - http://www.vauxoo.com/
7
# info Vauxoo (info@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
##############################################################################
29
from osv import osv, fields
30
from tools.translate import _
33
class wizard_report_variation(osv.osv_memory):
34
_name = 'wizard.report.variation'
37
'product_ids': fields.many2many('product.product','temp_product_rel','temp_id','product_id','Productos', required=True),
38
'date_start': fields.datetime('Start Date', required=True),
39
'date_finished': fields.datetime('End Date', required=True),
40
'type': fields.selection([('single','Detail'),('group','Resume')], 'Type', required=True, help="Only calculates for productions not in draft or cancelled"),
44
'date_finished': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
45
'date_start': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
48
def default_get(self, cr, uid, fields, context=None):
49
""" To get default values for the object.
50
@param self: The object pointer.
51
@param cr: A database cursor
52
@param uid: ID of the user currently logged in
53
@param fields: List of fields for which we want default values
54
@param context: A standard dictionary
55
@return: A dictionary which of fields with values.
59
prod_obj = self.pool.get('product.product')
60
production_obj = self.pool.get('mrp.production')
61
res = super(wizard_report_variation, self).default_get(cr, uid, fields, context=context)
62
production_ids = context.get('active_ids', [])
63
if not production_ids:
66
for production in production_obj.browse(cr,uid,production_ids):
67
prod_list.append(production.product_id.id)
68
res['product_ids']=prod_list
71
def check_report(self, cr, uid, ids, context=None):
75
data = self.read(cr, uid, ids)[0]
76
if data.get('type') == 'single':
77
myids = self.pool.get('mrp.production').search(cr, uid, [('product_id', 'in', data.get('product_ids')),('date_planned', '>', data.get('date_start')),('date_planned', '<', data.get('date_finished')),('state', '<>', 'cancel')])
79
raise osv.except_osv(_('Advice'), _('There is no production orders for the products you selected in the range of dates you specified.'))
84
'model': 'wizard.report.variation',
90
'type': 'ir.actions.report.xml',
91
'report_name': 'webkitmrp.production_variation',
95
if data.get('type') == 'group':
96
data_tuple = self.generate_datas_dict(cr, uid, ids, context=None, child_dict=None)
98
#obtain ids of the products of children productions.
99
mrp_obj = self.pool.get('mrp.production')
100
mrp_data = mrp_obj.browse(cr, uid, data_tuple[1], context=context)
104
if mrp.subproduction_ids:
105
for subp in mrp.subproduction_ids:
106
child_prod_ids.setdefault(subp.product_id.id, subp.product_id.id)
107
datas2 = self.generate_datas_dict(cr, uid, ids, context, child_prod_ids.values())
108
datas.setdefault('child_finished', datas2 and datas2[0].get('finished_dict') or [])
109
datas.setdefault('child_consumed', datas2 and datas2[0].get('query_dict') or [])
112
'type': 'ir.actions.report.xml',
113
'report_name': 'webkitmrp.production_variation_group',
117
def generate_datas_dict(self, cr, uid, ids, context=None, child_dict=None):
120
user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
121
company_id = context.get('company_id', user.company_id.id)
122
data = self.read(cr, uid, ids)[0]
123
if child_dict is None:
124
prod_ids = data.get('product_ids')
126
prod_ids = child_dict
127
mrp_obj = self.pool.get('mrp.production')
128
production_ids = mrp_obj.search(cr, uid , [('state', 'not in', ('draft', 'cancel')), \
129
('product_id', 'in', prod_ids), ('date_planned', '>', data.get('date_start')), \
130
('date_planned', '<', data.get('date_finished')), ('company_id', '=', company_id)])
131
if not production_ids:
132
raise osv.except_osv(_('Advice'), _('There is no production orders for the products you selected in the range of dates you specified.'))
134
#obtain data for variation in consumed products
136
SELECT product_id, sum(quantity), sum(standard_price * quantity) FROM mrp_variation
137
INNER JOIN product_product
138
ON product_product.id = mrp_variation.product_id
139
INNER JOIN product_template
140
ON product_template.id = product_product.product_tmpl_id
141
WHERE production_id IN
144
""", (tuple(production_ids),))
145
records = cr.fetchall()
146
consumed_variation = []
148
product_data = self.pool.get('product.product').browse(cr, uid, line[0], context=context)
149
consumed_variation.append((product_data.name, line[1], product_data.uom_id.name, line[2]))
151
#obtain data for variation in finished products
153
SELECT product_id, sum(quantity), sum(standard_price * quantity) FROM mrp_variation_finished_product
154
INNER JOIN product_product
155
ON product_product.id = mrp_variation_finished_product.product_id
156
INNER JOIN product_template
157
ON product_template.id = product_product.product_tmpl_id
158
WHERE production_id IN
161
""", (tuple(production_ids),))
162
records2 = cr.fetchall()
163
finished_variation = []
165
for line in records2:
166
finished_data = self.pool.get('product.product').browse(cr, uid, line[0], context=context)
167
finished_variation.append((finished_data.name, line[1], finished_data.uom_id.name, line[2]))
171
'model': 'wizard.report.variation',
174
'query_dict': consumed_variation,
175
'finished_dict': finished_variation
177
return (report_datas, production_ids)
179
wizard_report_variation()