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 osv, fields
23
from tools.translate import _
25
class account_invoice(osv.osv):
26
_name = 'account.invoice'
27
_inherit = 'account.invoice'
30
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
33
def line_get_convert(self, cr, uid, x, part, date, context=None):
34
res = super(account_invoice, self).line_get_convert(cr, uid, x, part, date, context=context)
35
res['analytic_distribution_id'] = x.get('analytic_distribution_id', False)
38
def button_analytic_distribution_from_direct_inv(self, cr, uid, ids, context=None):
41
st_line_id = self.read(cr, uid, context['active_id'], ['register_line_ids'])
42
context['from_direct_inv'] = st_line_id.get('register_line_ids') and st_line_id['register_line_ids'][0]
43
return self.button_analytic_distribution(cr, uid, ids, context)
46
def button_analytic_distribution(self, cr, uid, ids, context=None):
48
Launch analytic distribution wizard on an invoice
53
if isinstance(ids, (int, long)):
56
invoice = self.browse(cr, uid, ids[0], context=context)
58
# Search elements for currency
59
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
60
currency = invoice.currency_id and invoice.currency_id.id or company_currency
61
for line in invoice.invoice_line:
62
amount += line.price_subtotal
63
# Get analytic_distribution_id
64
distrib_id = invoice.analytic_distribution_id and invoice.analytic_distribution_id.id
65
# Prepare values for wizard
67
'total_amount': amount,
68
'invoice_id': invoice.id,
69
'currency_id': currency or False,
71
'posting_date': invoice.date_invoice,
72
'document_date': invoice.document_date,
74
if context.get('from_direct_inv'):
75
vals['from_direct_inv'] = context['from_direct_inv']
77
vals.update({'distribution_id': distrib_id,})
79
wiz_obj = self.pool.get('analytic.distribution.wizard')
80
wiz_id = wiz_obj.create(cr, uid, vals, context=context)
81
# Update some context values
88
'name': _('Global analytic distribution'),
89
'type': 'ir.actions.act_window',
90
'res_model': 'analytic.distribution.wizard',
100
class account_invoice_line(osv.osv):
101
_name = 'account.invoice.line'
102
_inherit = 'account.invoice.line'
105
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
108
def button_analytic_distribution_from_direct_inv_line(self, cr, uid, ids, context=None):
111
acc_inv_line = self.browse(cr, uid, context['active_id'])
112
context['from_direct_inv'] = acc_inv_line.invoice_id and acc_inv_line.invoice_id.register_line_ids and acc_inv_line.invoice_id.register_line_ids[0].id
113
return self.button_analytic_distribution(cr, uid, ids, context)
115
def button_analytic_distribution(self, cr, uid, ids, context=None):
117
Launch analytic distribution wizard on an invoice line
122
if isinstance(ids, (int, long)):
125
raise osv.except_osv(_('Error'), _('No invoice line given. Please save your invoice line before.'))
126
# Prepare some values
127
invoice_line = self.browse(cr, uid, ids[0], context=context)
130
amount = invoice_line.price_subtotal or 0.0
131
# Search elements for currency
132
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
133
currency = invoice_line.invoice_id.currency_id and invoice_line.invoice_id.currency_id.id or company_currency
134
# Change amount sign if necessary
135
if invoice_line.invoice_id.type in ['out_invoice', 'in_refund']:
139
# Get analytic distribution id from this line
140
distrib_id = invoice_line and invoice_line.analytic_distribution_id and invoice_line.analytic_distribution_id.id or False
141
# Prepare values for wizard
143
'total_amount': amount,
144
'invoice_line_id': invoice_line.id,
145
'currency_id': currency or False,
147
'account_id': invoice_line.account_id and invoice_line.account_id.id or False,
148
'posting_date': invoice_line.invoice_id.date_invoice,
149
'document_date': invoice_line.invoice_id.document_date,
151
if context.get('from_direct_inv'):
152
vals['from_direct_inv'] = context['from_direct_inv']
154
vals.update({'distribution_id': distrib_id,})
156
wiz_obj = self.pool.get('analytic.distribution.wizard')
157
wiz_id = wiz_obj.create(cr, uid, vals, context=context)
158
# Update some context values
165
'name': _('Analytic distribution'),
166
'type': 'ir.actions.act_window',
167
'res_model': 'analytic.distribution.wizard',
175
def move_line_get_item(self, cr, uid, line, context=None):
177
Give right analytic distribution when creating move lines
183
res = super(account_invoice_line, self).move_line_get_item(cr, uid, line, context=context)
184
# Update result by copying analytic distribution from invoice line
185
ana_obj = self.pool.get('analytic.distribution')
186
if line.analytic_distribution_id:
187
new_distrib_id = ana_obj.copy(cr, uid, line.analytic_distribution_id.id, {}, context=context)
189
res['analytic_distribution_id'] = new_distrib_id
190
# If no distribution on invoice line, take those from invoice and copy it!
191
elif line.invoice_id and line.invoice_id.analytic_distribution_id:
192
new_distrib_id = ana_obj.copy(cr, uid, line.invoice_id.analytic_distribution_id.id, {}, context=context)
194
res['analytic_distribution_id'] = new_distrib_id
197
account_invoice_line()