1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). All Rights Reserved.
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.
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.
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/>.
20
##############################################################################
22
Wizard to impact structural costs over products
25
from osv import osv, fields
27
import decimal_precision as dp
29
from tools.translate import _
32
('novalid','Please, select a method for impacting costs...'),
33
('uniform','Uniform'),
37
class structural_costs_impact_wizard(osv.osv_memory):
39
Wizard to percentually impact structural costs on service products
42
def onchange_analytic_account(self, cr, uid, ids, fiscalyear_id, period_id, account_id):
44
Gets total amount from chosen analytical account and year
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')
52
fiscalyear = self.pool.get('account.fiscalyear').browse(cr, uid, fiscalyear_id)
54
context['from_date'] =fiscalyear.date_start
55
context['to_date'] = fiscalyear.date_stop
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
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 = {}
67
for sale in period_sales:
68
for line in sale.order_line:
70
if line.product_id.id not in distinct_sale_products.keys():
71
distinct_sale_products[line.product_id.id] = line.product_uom_qty
73
distinct_sale_products[line.product_id.id] += line.product_uom_qty
75
for product_id in distinct_sale_products.keys():
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
82
line_id = struct_cost_facade.create(cr, uid, vals_product_line)
83
product_line_ids.append(line_id)
85
res['products_percent'] = product_line_ids
89
def onchange_cost_method(self, cr, uid, ids, cost_method, structural_cost, products_percent):
91
Gets amount to impact over chosen products based on sales forecast
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}
103
def _get_current_user_company(self, cr, uid, context={}):
105
Obtiene la compañía del usuario activo
107
current_user = self.pool.get('res.users').browse(cr,uid,uid)
108
return current_user.company_id.id
110
def _get_previous_fiscalyear(self, cr, uid, context):
112
Get previous current year
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)
117
prev_date_start = '%s-01-01' %(str(int(current_fyear.date_start[0:4])-1))
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
124
return current_fyear.id
128
def action_impact_struct_costs(self, cr, uid, ids, *args):
130
Simply opens a list view of all modified products
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)
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]
143
#Finalmente, devolvemos la vista correspondiente...
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,
152
'views': [(product_list_view_id, 'tree'), (product_form_view_id, 'form'), (False, 'calendar'), (False, 'graph')],
156
_name = 'structural.costs.impact.wizard'
157
_description = "Structural costs impact Wizard"
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')
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'
175
structural_costs_impact_wizard()
b'\\ No newline at end of file'