~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to mrp_variation/mrp_variation.py

  • Committer: Gabriela (Vauxoo)
  • Date: 2012-01-02 16:24:49 UTC
  • Revision ID: gabrielaquilarque97@gmail.com-20120102162449-lhxnrtif2ud36du2

[ADD] Added new module invoice_so.

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: julio (julio@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
 
import decimal_precision as dp
28
 
 
29
 
 
30
 
class mrp_production(osv.Model):
31
 
    _inherit = 'mrp.production'
32
 
 
33
 
    def copy(self, cr, uid, id, default=None, context=None):
34
 
        if default is None:
35
 
            default = {}
36
 
        default.update({
37
 
            'variation_ids': [],
38
 
            'variation_finished_product_ids': [],
39
 
        })
40
 
        return super(mrp_production, self).copy(cr, uid, id, default, context)
41
 
 
42
 
    _columns = {
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),
48
 
    }
49
 
 
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)
55
 
        return res
56
 
 
57
 
    def create_variation(self, cr, uid, ids, real={}, planned={}, context={}):
58
 
        prod_product = self.pool.get('product.product')
59
 
        list_val = []
60
 
        for production in self.browse(cr, uid, ids, context=context):
61
 
            lista = []
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
75
 
                       }
76
 
                list_val.append(val)
77
 
        return list_val
78
 
 
79
 
    def create_res_real_planned(self, cr, uid, dat=False):
80
 
        product_product = self.pool.get('product.product')
81
 
        if dat is False:
82
 
            dat = []
83
 
        res = {}
84
 
        for lin in dat:
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
91
 
        return res
92
 
 
93
 
    def create_consume_real(self, cr, uid, ids, context={}):
94
 
        for production in self.browse(cr, uid, ids, context=context):
95
 
            cr.execute("""
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
99
 
                        ON sm.id=mpmi.move_id
100
 
                    WHERE mpmi.production_id=%s
101
 
                    AND sm.state='done'
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)
106
 
        return res_real
107
 
 
108
 
    def create_consume_planned(self, cr, uid, ids, context={}):
109
 
        for production in self.browse(cr, uid, ids, context=context):
110
 
            cr.execute("""
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)
119
 
        return res_planned
120
 
 
121
 
    def create_finished_product_real(self, cr, uid, ids, context={}):
122
 
        for production in self.browse(cr, uid, ids, context=context):
123
 
            cr.execute("""
124
 
                    SELECT product_id,product_uom,sum(product_qty)
125
 
                        AS product_qty
126
 
                        FROM stock_move
127
 
                    WHERE production_id=%s
128
 
                    AND state='done'
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)
133
 
        return res_real
134
 
 
135
 
    def create_finished_product_planned(self, cr, uid, ids, context={}):
136
 
        for production in self.browse(cr, uid, ids, context=context):
137
 
            cr.execute("""
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)
145
 
        return res_planned
146
 
 
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]
158
 
        return True
159
 
 
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]
174
 
        return True
175
 
 
176
 
 
177
 
class mrp_variation(osv.Model):
178
 
    _name = 'mrp.variation'
179
 
    _rec_name = 'product_id'
180
 
 
181
 
    def _get_variation_cost(self, cr, uid, ids, field_name, args, context={}):
182
 
        res = {}
183
 
        for variation in self.browse(cr, uid, ids, context=context):
184
 
            res[variation.id] = variation.quantity * \
185
 
                variation.product_id.standard_price
186
 
        return res
187
 
 
188
 
    _columns = {
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')
197
 
    }
198
 
 
199
 
 
200
 
class mrp_variation_finished_product(osv.Model):
201
 
    _name = 'mrp.variation.finished.product'
202
 
    _rec_name = 'product_id'
203
 
 
204
 
    def _get_variation_cost(self, cr, uid, ids, field_name, args, context={}):
205
 
        res = {}
206
 
        for variation in self.browse(cr, uid, ids, context=context):
207
 
            res[variation.id] = variation.quantity * \
208
 
                variation.product_id.standard_price
209
 
        return res
210
 
 
211
 
    _columns = {
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')
220
 
    }