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

« back to all changes in this revision

Viewing changes to production_costs/wizard/structural_costs_impact_wizard.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
    Wizard to impact structural costs over products
 
23
"""
 
24
 
 
25
from osv import osv, fields
 
26
import time
 
27
import decimal_precision as dp
 
28
import calendar
 
29
from tools.translate import _
 
30
 
 
31
COST_METHODS = [
 
32
    ('novalid','Please, select a method for impacting costs...'),
 
33
    ('uniform','Uniform'),
 
34
]
 
35
 
 
36
 
 
37
class structural_costs_impact_wizard(osv.osv_memory):
 
38
    """
 
39
    Wizard to percentually impact structural costs on service products
 
40
    """
 
41
 
 
42
    def onchange_analytic_account(self, cr, uid, ids, fiscalyear_id, period_id, account_id):
 
43
        """
 
44
            Gets total amount from chosen analytical account and year
 
45
        """
 
46
        res ={}
 
47
        context = {}
 
48
        res['structural_cost'] = 0.0
 
49
        struct_cost_facade = self.pool.get('product.percent.struct.costs')
 
50
        sales_facade = self.pool.get('sale.order')
 
51
        if account_id:
 
52
            fiscalyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id)
 
53
            if not period_id:
 
54
                context['from_date'] =fiscalyear.date_start
 
55
                context['to_date'] = fiscalyear.date_stop
 
56
            else:
 
57
                period = self.pool.get('account.period').browse(cr, uid, period_id)
 
58
                context['from_date'] = period.date_start
 
59
                context['to_date'] = period.date_stop
 
60
            account = self.pool.get('account.analytic.account').browse(cr, uid, account_id, context)
 
61
            res['structural_cost'] = account.credit
 
62
 
 
63
            #Forces creation of product lines based on sales made on chosen period and fiscal year...
 
64
            period_sales = sales_facade.browse(cr, uid, sales_facade.search(cr, uid, [('state','not in',['draft','cancel']),('date_order','<=', context['to_date']), ('date_order','>=', context['from_date'])]))
 
65
            distinct_sale_products = {}
 
66
            product_line_ids = []
 
67
            for sale in period_sales:
 
68
                for line in sale.order_line:
 
69
                    if line.product_id:
 
70
                        if line.product_id.id not in distinct_sale_products.keys():
 
71
                            distinct_sale_products[line.product_id.id] = line.product_uom_qty
 
72
                        else:
 
73
                            distinct_sale_products[line.product_id.id] += line.product_uom_qty
 
74
                            
 
75
            for product_id in distinct_sale_products.keys():
 
76
                vals_product_line = {
 
77
                    'product_id': product_id,
 
78
                    'total_sales': distinct_sale_products[product_id],
 
79
                    'forecasted_sales': distinct_sale_products[product_id],
 
80
                    'wizard_id': self.next_id + 1
 
81
                }
 
82
                line_id = struct_cost_facade.create(cr, uid, vals_product_line)
 
83
                product_line_ids.append(line_id)
 
84
 
 
85
            res['products_percent'] = product_line_ids
 
86
 
 
87
        return {'value': res}
 
88
 
 
89
    def onchange_cost_method(self, cr, uid, ids, cost_method, structural_cost, products_percent):
 
90
        """
 
91
            Gets amount to impact over chosen products based on sales forecast
 
92
        """
 
93
        res ={}
 
94
        res['cost_to_impact'] = 0.0
 
95
        sum_forecasted_sales = 0.0
 
96
        if cost_method == 'uniform':
 
97
            for product_line in products_percent:
 
98
                sum_forecasted_sales += product_line[2]['forecasted_sales']
 
99
                res['cost_to_impact'] = structural_cost / sum_forecasted_sales
 
100
        return {'value': res}
 
101
 
 
102
 
 
103
    def _get_current_user_company(self, cr, uid, context={}):
 
104
        """
 
105
            Obtiene la compañía del usuario activo
 
106
        """
 
107
        current_user = self.pool.get('res.users').browse(cr,uid,uid)
 
108
        return current_user.company_id.id
 
109
 
 
110
    def _get_previous_fiscalyear(self, cr, uid, context):
 
111
        """
 
112
            Get previous current year
 
113
        """
 
114
        fyear_facade = self.pool.get('account.fiscalyear')
 
115
        current_fyear = context and context.get('fiscalyear_id', None) or fyear_facade.browse(cr, uid, uid)
 
116
        try:
 
117
            prev_date_start = '%s-01-01' %(str(int(current_fyear.date_start[0:4])-1))
 
118
        except Exception:
 
119
            raise osv.except_osv(_('Error!'), _('Are fiscal years already defined...?'))
 
120
        prev_date_end = '%s-12-31' %(str(int(current_fyear.date_start[0:4])-1))
 
121
        prev_fyear = fyear_facade.search(cr, uid, [('company_id','=',current_fyear.company_id.id), ('date_start','>=',prev_date_start),('date_stop','<=',prev_date_end)])
 
122
        prev_fyear = prev_fyear and prev_fyear[0] or None
 
123
        if not prev_fyear:
 
124
            return current_fyear.id
 
125
        else:
 
126
            return prev_fyear
 
127
 
 
128
    def action_impact_struct_costs(self, cr, uid, ids, *args):
 
129
        """
 
130
            Simply opens a list view of all modified products
 
131
        """
 
132
        modified_prod_ids = []
 
133
        view_facade = self.pool.get('ir.ui.view')
 
134
        product_facade = self.pool.get('product.product')
 
135
        for wizard in self.browse(cr, uid, ids):
 
136
            for line in wizard.products_percent:
 
137
                product_facade.write(cr, uid, line.product_id.id, {'structural_cost': wizard.cost_to_impact})
 
138
                modified_prod_ids.append(line.product_id.id)
 
139
 
 
140
            product_list_view_id = view_facade.search(cr, uid, [('name', '=', 'view.add.costs.product.view.list')])[0]
 
141
            product_form_view_id = view_facade.search(cr, uid, [('name', '=', 'view.add.costs.product.view.form')])[0]
 
142
 
 
143
            #Finalmente, devolvemos la vista correspondiente...
 
144
            return {
 
145
                'name' : _('Impacted Products'),
 
146
                'type' : 'ir.actions.act_window',
 
147
                'res_model' : 'product.product',
 
148
                'view_type' : 'form',
 
149
                'view_mode' : 'tree,form',
 
150
                'domain' : "[('id', 'in', %s)]" % modified_prod_ids,
 
151
                'view_id' : False,
 
152
                'views': [(product_list_view_id, 'tree'), (product_form_view_id, 'form'), (False, 'calendar'), (False, 'graph')],
 
153
            }
 
154
 
 
155
 
 
156
    _name = 'structural.costs.impact.wizard'
 
157
    _description = "Structural costs impact Wizard"
 
158
 
 
159
    _columns = {
 
160
        'prev_fyear_id' : fields.many2one('account.fiscalyear', 'Fiscal Year to Look', required=True),
 
161
        'prev_period_id' : fields.many2one('account.period', 'Period'),
 
162
        'struct_analytic_acc_id': fields.many2one('account.analytic.account','Structural Expenses Analytic Account'),
 
163
        'structural_cost': fields.float('Structural Costs', digits_compute=dp.get_precision('Account')),
 
164
        'products_percent': fields.one2many('product.percent.struct.costs', 'wizard_id', 'Sold Products during chosen fiscal year and/or period'),
 
165
        'structural_cost_method': fields.selection(COST_METHODS, 'Cost Method',required=True, help='Uniform method: all costs are distributed equally amongst products.'),
 
166
        'cost_to_impact': fields.float('Cost over products', digits_compute=dp.get_precision('Account'), help='Cost based on forecasted sales.'),
 
167
        'company_id': fields.many2one('res.company', 'Company')
 
168
    }
 
169
    _defaults = {
 
170
        'prev_fyear_id': _get_previous_fiscalyear,
 
171
        'company_id': lambda self, cr, uid, context: self._get_current_user_company(cr, uid, context),
 
172
        'structural_cost_method': lambda *a: 'novalid'
 
173
    }
 
174
 
 
175
structural_costs_impact_wizard()
 
 
b'\\ No newline at end of file'