~vauxoo/addons-vauxoo/rico_contract_fix_email_and_more_dev_ernesto

« back to all changes in this revision

Viewing changes to mrp_webkit_report_wizard/wizard/wizard_report_variation.py

[ADD]mrp_webkit_report_wizard Se agregó un wizard para reportear usando webkit, las variaciones en las órdenes de producción, de los productos que se seleccionen en el wizard, en el rango de fecha dado.Lo hace también para sus supproducciones.

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) 2013 Vauxoo - http://www.vauxoo.com/
 
6
#    All Rights Reserved.
 
7
#    info Vauxoo (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
 
 
27
import time
 
28
 
 
29
from osv import osv, fields
 
30
from tools.translate import _
 
31
 
 
32
 
 
33
class wizard_report_variation(osv.osv_memory):
 
34
    _name = 'wizard.report.variation'
 
35
 
 
36
    _columns = {
 
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"),
 
41
    }
 
42
    
 
43
    _defaults = {
 
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'),
 
46
    }
 
47
 
 
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.
 
56
        """
 
57
        if context is None:
 
58
            context = {}
 
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:
 
64
            return res
 
65
        prod_list=[]
 
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
 
69
        return res
 
70
        
 
71
    def check_report(self, cr, uid, ids, context=None):
 
72
        datas = {}
 
73
        if context is None:
 
74
            context = {}
 
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')])
 
78
            if not myids:
 
79
                raise osv.except_osv(_('Advice'), _('There is no production orders for the products you selected in the range of dates you specified.'))
 
80
            
 
81
            
 
82
            datas = {
 
83
                'ids': myids,
 
84
                'model': 'wizard.report.variation',
 
85
                'form': data,
 
86
                'uid': uid,
 
87
            }
 
88
 
 
89
            return {
 
90
                'type': 'ir.actions.report.xml',
 
91
                'report_name': 'webkitmrp.production_variation',
 
92
                'datas': datas,
 
93
            }
 
94
 
 
95
        if data.get('type') == 'group':
 
96
            data_tuple = self.generate_datas_dict(cr, uid, ids, context=None, child_dict=None)
 
97
            datas = data_tuple[0]
 
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)
 
101
            child_prod_ids = {}
 
102
            datas2={}
 
103
            for mrp in mrp_data:
 
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 [])
 
110
            
 
111
            return {
 
112
                'type': 'ir.actions.report.xml',
 
113
                'report_name': 'webkitmrp.production_variation_group',
 
114
                'datas': datas,
 
115
            }
 
116
 
 
117
    def generate_datas_dict(self, cr, uid, ids, context=None, child_dict=None):
 
118
        if context is None:
 
119
            context = {}
 
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')
 
125
        else:
 
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.'))
 
133
 
 
134
        #obtain data for variation in consumed products
 
135
        cr.execute("""
 
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 
 
142
%s
 
143
GROUP BY product_id
 
144
        """, (tuple(production_ids),))
 
145
        records = cr.fetchall()
 
146
        consumed_variation = []
 
147
        for line in records:
 
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]))
 
150
        
 
151
        #obtain data for variation in finished products
 
152
        cr.execute("""
 
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 
 
159
%s
 
160
GROUP BY product_id
 
161
        """, (tuple(production_ids),))
 
162
        records2 = cr.fetchall()
 
163
        finished_variation = []
 
164
        if records2:
 
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]))
 
168
 
 
169
        report_datas = {
 
170
            'ids': ids,
 
171
            'model': 'wizard.report.variation',
 
172
            'form': data,
 
173
            'uid': uid,
 
174
            'query_dict': consumed_variation,
 
175
            'finished_dict': finished_variation
 
176
        }
 
177
        return (report_datas, production_ids)
 
178
    
 
179
wizard_report_variation()