~pexego/openobject-addons/6.1-pexego-sale_commission

« back to all changes in this revision

Viewing changes to production_costs/wizard/product_percent_struct_costs.py

  • Committer: Omar (pexego)
  • Date: 2012-07-27 08:40:22 UTC
  • Revision ID: omar@pexego.es-20120727084022-qp3ludpr3vsuyuf6
[ADD] Traceability modules ported to 6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved.
 
6
#    
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License as published by
 
9
#    the Free Software Foundation, either version 3 of the License, or
 
10
#    (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
##############################################################################
 
21
 
 
22
"""
 
23
    Auxiliary object to mate products with structural costs
 
24
"""
 
25
 
 
26
from osv import osv, fields
 
27
import time
 
28
import decimal_precision as dp
 
29
import calendar
 
30
from tools.translate import _
 
31
 
 
32
class product_percent_struct_costs(osv.osv_memory):
 
33
    """
 
34
        Auxiliar object to associate percentually costs to products
 
35
    """
 
36
    def onchange_product_id(self, cr, uid, ids, prev_fyear_id, prev_period_id, product_id):
 
37
        """
 
38
            Gets total sales for this product, fiscal year and period
 
39
        """
 
40
        res ={}
 
41
        total_sales = 0.0
 
42
        sales_facade = self.pool.get('sale.order')
 
43
        fiscalyear = self.pool.get('account.fiscalyear').browse(cr, uid, prev_fyear_id)
 
44
        if not prev_period_id:
 
45
            from_date =fiscalyear.date_start
 
46
            to_date = fiscalyear.date_stop
 
47
        else:
 
48
            period = self.pool.get('account.period').browse(cr, uid, prev_period_id)
 
49
            from_date = period.date_start
 
50
            to_date = period.date_stop
 
51
 
 
52
        period_sales = sales_facade.browse(cr, uid, sales_facade.search(cr, uid, [('state','not in',['draft','cancel']),('date_order','<=', to_date), ('date_order','>=', from_date)]))
 
53
        for sale in period_sales:
 
54
            for line in sale.order_line:
 
55
                if line.product_id:
 
56
                    if line.product_id.id == product_id:
 
57
                        total_sales += line.product_uom_qty
 
58
 
 
59
        res['total_sales'] = total_sales
 
60
        res['forecasted_sales'] = total_sales
 
61
 
 
62
        return {'value': res}
 
63
 
 
64
 
 
65
    def onchange_total_sales(self, cr, uid, ids, total_sales):
 
66
        """
 
67
            Refresh forecasted values according total sales
 
68
        """
 
69
        res ={}
 
70
        if total_sales:
 
71
            res['forecasted_sales'] = total_sales
 
72
        else:
 
73
            res['forecasted_sales'] = 0.0
 
74
        return {'value': res}
 
75
 
 
76
 
 
77
    _name = 'product.percent.struct.costs'
 
78
    _description = 'Structural products cost'
 
79
    _columns = {
 
80
        'product_id': fields.many2one('product.product','Product',required=True),
 
81
        'total_sales': fields.float('Sold Units', digits_compute=dp.get_precision('Account'), required=True),
 
82
        'forecasted_sales': fields.float('Forecasted Sold Units', digits_compute=dp.get_precision('Account')),
 
83
        'wizard_id': fields.many2one('structural.costs.impact.wizard','Wizard'),
 
84
    }
 
85
 
 
86
    _defaults = {
 
87
        'wizard_id': lambda self, cr, uid, context: context.get('parent_id') and context['parent_id'] or False,
 
88
    }
 
89
 
 
90
 
 
91
product_percent_struct_costs()
 
 
b'\\ No newline at end of file'