1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 MSF, TeMPO consulting
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (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 Affero General Public License for more details.
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
22
from osv import fields, osv
24
from tools.translate import _
26
class account_move_line(osv.osv):
27
_inherit = 'account.move.line'
30
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
33
def create_analytic_lines(self, cr, uid, ids, context=None):
34
acc_ana_line_obj = self.pool.get('account.analytic.line')
35
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
36
for obj_line in self.browse(cr, uid, ids, context=context):
37
if obj_line.analytic_distribution_id and obj_line.account_id.user_type_code == 'expense':
38
if not obj_line.journal_id.analytic_journal_id:
39
raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (obj_line.journal_id.name, ))
40
distrib_obj = self.pool.get('analytic.distribution').browse(cr, uid, obj_line.analytic_distribution_id.id, context=context)
42
for distrib_lines in [distrib_obj.cost_center_lines, distrib_obj.funding_pool_lines, distrib_obj.free_1_lines, distrib_obj.free_2_lines]:
43
for distrib_line in distrib_lines:
44
context.update({'date': obj_line.source_date or obj_line.date})
46
'name': obj_line.name,
47
'date': obj_line.date,
49
'journal_id': obj_line.journal_id.analytic_journal_id.id,
50
'amount': -1 * self.pool.get('res.currency').compute(cr, uid, obj_line.currency_id.id, company_currency,
51
distrib_line.amount or 0.0, round=False, context=context),
52
'amount_currency': -1 * distrib_line.amount,
53
'account_id': distrib_line.analytic_id.id,
54
'general_account_id': obj_line.account_id.id,
55
'move_id': obj_line.id,
56
'distribution_id': distrib_obj.id,
58
'currency_id': obj_line.currency_id.id,
60
# Update values if we come from a funding pool
61
if distrib_line._name == 'funding.pool.distribution.line':
62
line_vals.update({'cost_center_id': distrib_line.cost_center_id and distrib_line.cost_center_id.id or False,})
63
# Add source_date value for account_move_line that are a correction of another account_move_line
64
if obj_line.corrected_line_id and obj_line.source_date:
65
line_vals.update({'source_date': obj_line.source_date})
66
self.pool.get('account.analytic.line').create(cr, uid, line_vals, context=context)
69
def button_analytic_distribution(self, cr, uid, ids, context={}):
71
Launch the analytic distribution wizard from a journal item (account_move_line)
76
if isinstance(ids, (int, long)):
78
# we get the analytical distribution object linked to this line
80
move_line_obj = self.browse(cr, uid, ids[0], context=context)
81
# Get amount using account_move_line amount_currency field
82
amount = move_line_obj.amount_currency and move_line_obj.amount_currency or 0.0
83
# Search elements for currency
84
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
85
currency = move_line_obj.currency_id and move_line_obj.currency_id.id or company_currency
86
if move_line_obj.analytic_distribution_id:
87
distrib_id = move_line_obj.analytic_distribution_id.id
89
raise osv.except_osv(_('No Analytic Distribution !'),_("You have to define an analytic distribution on the move line!"))
90
wiz_obj = self.pool.get('wizard.costcenter.distribution')
91
wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id, 'currency_id': currency}, context=context)
96
'wizard_ids': {'cost_center': wiz_id},
99
'type': 'ir.actions.act_window',
100
'res_model': 'wizard.costcenter.distribution',
109
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: