1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 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
23
import decimal_precision as dp
24
from tools.misc import flatten
25
from time import strftime
27
class analytic_distribution1(osv.osv):
28
_name = "analytic.distribution"
31
'analytic_lines': fields.one2many('account.analytic.line', 'distribution_id', 'Analytic Lines'),
32
'invoice_ids': fields.one2many('account.invoice', 'analytic_distribution_id', string="Invoices"),
33
'invoice_line_ids': fields.one2many('account.invoice.line', 'analytic_distribution_id', string="Invoice Lines"),
34
'register_line_ids': fields.one2many('account.bank.statement.line', 'analytic_distribution_id', string="Register Lines"),
35
'move_line_ids': fields.one2many('account.move.line', 'analytic_distribution_id', string="Move Lines"),
36
'commitment_ids': fields.one2many('account.commitment', 'analytic_distribution_id', string="Commitments voucher"),
37
'commitment_line_ids': fields.one2many('account.commitment.line', 'analytic_distribution_id', string="Commitment voucher lines"),
40
def copy(self, cr, uid, id, default=None, context=None):
42
Copy an analytic distribution without the one2many links
47
'analytic_lines': False,
49
'invoice_line_ids': False,
50
'register_line_ids': False,
51
'move_line_ids': False,
52
'commitment_ids': False,
53
'commitment_line_ids': False,
55
return super(osv.osv, self).copy(cr, uid, id, default, context=context)
57
def _get_distribution_state(self, cr, uid, id, parent_id, account_id, context=None):
59
Return distribution state
63
# Have an analytic distribution on another account than expense account make no sense. So their analaytic distribution is valid
65
account = self.pool.get('account.account').browse(cr, uid, account_id)
66
if account and account.user_type and account.user_type.code != 'expense':
70
return self._get_distribution_state(cr, uid, parent_id, False, account_id, context)
72
distri = self.browse(cr, uid, id)
73
# Search MSF Private Fund element, because it's valid with all accounts
75
fp_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution',
76
'analytic_account_msf_private_funds')[1]
79
for fp_line in distri.funding_pool_lines:
80
# If fp_line is MSF Private Fund, all is ok
81
if fp_line.analytic_id.id == fp_id:
83
if account_id not in [x.id for x in fp_line.analytic_id.account_ids]:
85
if fp_line.cost_center_id.id not in [x.id for x in fp_line.analytic_id.cost_center_ids]:
89
analytic_distribution1()
91
class distribution_line(osv.osv):
92
_name = "distribution.line"
95
'name': fields.char('Name', size=64),
96
"distribution_id": fields.many2one('analytic.distribution', 'Associated Analytic Distribution', ondelete='cascade'),
97
"analytic_id": fields.many2one('account.analytic.account', 'Analytical Account'),
98
"amount": fields.float('Amount', digits_compute=dp.get_precision('Account')),
99
"percentage": fields.float('Percentage', digits=(16,4)),
100
"currency_id": fields.many2one('res.currency', 'Currency', required=True),
101
"date": fields.date(string="Date"),
102
"source_date": fields.date(string="Source Date", help="This date is for source_date for analytic lines"),
106
'name': 'Distribution Line',
107
'date': lambda *a: strftime('%Y-%m-%d'),
108
'source_date': lambda *a: strftime('%Y-%m-%d'),
113
class cost_center_distribution_line(osv.osv):
114
_name = "cost.center.distribution.line"
115
_inherit = "distribution.line"
117
cost_center_distribution_line()
119
class funding_pool_distribution_line(osv.osv):
120
_name = "funding.pool.distribution.line"
121
_inherit = "distribution.line"
123
"cost_center_id": fields.many2one('account.analytic.account', 'Cost Center Account'),
126
funding_pool_distribution_line()
128
class free_1_distribution_line(osv.osv):
129
_name = "free.1.distribution.line"
130
_inherit = "distribution.line"
132
free_1_distribution_line()
134
class free_2_distribution_line(osv.osv):
135
_name = "free.2.distribution.line"
136
_inherit = "distribution.line"
138
free_2_distribution_line()
140
class analytic_distribution(osv.osv):
141
_inherit = "analytic.distribution"
143
def _get_lines_count(self, cr, uid, ids, name, args, context=None):
145
Get count of each analytic distribution lines type.
146
Example: with an analytic distribution with 2 cost center, 3 funding pool and 1 Free 1:
147
2 CC; 3 FP; 1 F1; 0 F2;
148
(Number of chars: 20 chars + 4 x some lines number)
153
if isinstance(ids, (int, long)):
155
# Prepare some values
157
# Browse given invoices
158
for distrib in self.browse(cr, uid, ids, context=context):
160
txt += str(len(distrib.cost_center_lines) or '0') + ' CC; '
161
txt += str(len(distrib.funding_pool_lines) or '0') + ' FP; '
162
txt += str(len(distrib.free_1_lines) or '0') + ' F1; '
163
txt += str(len(distrib.free_2_lines) or '0') + ' F2'
166
res[distrib.id] = txt
170
'cost_center_lines': fields.one2many('cost.center.distribution.line', 'distribution_id', 'Cost Center Distribution'),
171
'funding_pool_lines': fields.one2many('funding.pool.distribution.line', 'distribution_id', 'Funding Pool Distribution'),
172
'free_1_lines': fields.one2many('free.1.distribution.line', 'distribution_id', 'Free 1 Distribution'),
173
'free_2_lines': fields.one2many('free.2.distribution.line', 'distribution_id', 'Free 2 Distribution'),
174
'name': fields.function(_get_lines_count, method=True, type='char', size=256, string="Name", readonly=True, store=False),
177
def update_distribution_line_amount(self, cr, uid, ids, amount=False, context=None):
179
Update amount on distribution lines for given distribution (ids)
184
if isinstance(ids, (int, long)):
188
# Process distributions
189
for distrib_id in ids:
190
for dl_name in ['cost.center.distribution.line', 'funding.pool.distribution.line', 'free.1.distribution.line', 'free.2.distribution.line']:
191
dl_obj = self.pool.get(dl_name)
192
dl_ids = dl_obj.search(cr, uid, [('distribution_id', '=', distrib_id)], context=context)
193
for dl in dl_obj.browse(cr, uid, dl_ids, context=context):
195
'amount': round(dl.percentage * amount) / 100.0,
197
dl_obj.write(cr, uid, [dl.id], dl_vals, context=context)
200
def update_distribution_line_account(self, cr, uid, ids, old_account_id, account_id, context=None):
202
Update account on distribution line
207
if isinstance(ids, (int, long)):
209
if not old_account_id or not account_id:
211
# Prepare some values
212
account = self.pool.get('account.analytic.account').browse(cr, uid, [account_id], context=context)[0]
213
# Browse distribution
214
for distrib_id in ids:
215
for dl_name in ['cost.center.distribution.line', 'funding.pool.distribution.line', 'free.1.distribution.line', 'free.2.distribution.line']:
216
dl_obj = self.pool.get(dl_name)
217
dl_ids = dl_obj.search(cr, uid, [('distribution_id', '=', distrib_id), ('analytic_id', '=', old_account_id)], context=context)
218
for dl in dl_obj.browse(cr, uid, dl_ids, context=context):
219
if account.category == 'OC':
220
# Search funding pool line to update them
221
fp_line_ids = self.pool.get('funding.pool.distribution.line').search(cr, uid, [('distribution_id', "=", distrib_id),
222
('cost_center_id', '=', old_account_id)])
223
if isinstance(fp_line_ids, (int, long)):
224
fp_line_ids = [fp_line_ids]
225
# Update funding pool line(s)
226
self.pool.get('funding.pool.distribution.line').write(cr, uid, fp_line_ids, {'cost_center_id': account_id}, context=context)
227
# Update distribution line
228
dl_obj.write(cr, uid, [dl.id], {'analytic_id': account_id,}, context=context)
231
def create_funding_pool_lines(self, cr, uid, ids, context=None):
233
Create funding pool lines regarding cost_center_lines from analytic distribution.
234
If funding_pool_lines exists, then nothing appends.
235
By default, add funding_pool_lines with MSF Private Fund element (written in an OpenERP demo file).
240
if isinstance(ids, (int, long)):
242
# Prepare some values
244
# Browse distributions
245
for distrib in self.browse(cr, uid, ids, context=context):
246
if distrib.funding_pool_lines:
247
res[distrib.id] = False
249
# Browse cost center lines
250
for line in distrib.cost_center_lines:
251
# Search MSF Private Fund
253
pf_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution',
254
'analytic_account_msf_private_funds')[1]
257
# pf_id = self.pool.get('account.analytic.account').search(cr, uid, [('code', '=', 'PF'), ('category', '=', 'FUNDING')], context=context, limit=1)
260
'analytic_id': pf_id,
261
'amount': line.amount or 0.0,
262
'percentage': line.percentage or 0.0,
263
'currency_id': line.currency_id and line.currency_id.id or False,
264
'distribution_id': distrib.id or False,
265
'cost_center_id': line.analytic_id and line.analytic_id.id or False,
267
new_pf_line_id = self.pool.get('funding.pool.distribution.line').create(cr, uid, vals, context=context)
268
res[distrib.id] = True
271
def create_analytic_lines(self, cr, uid, ids, name, date, amount, journal_id, currency_id, ref=False, source_date=False, general_account_id=False, \
272
move_id=False, invoice_line_id=False, commitment_line_id=False, context=None):
274
Create analytic lines from given elements:
278
- journal_id (analytic_journal_id)
281
- source_date (optional)
282
- general_account_id (optional)
284
- invoice_line_id (optional)
285
- commitment_line_id (optional)
286
Return all created ids, otherwise return false (or [])
291
if isinstance(ids, (int, long)):
293
if not name or not date or not amount or not journal_id or not currency_id:
295
# Prepare some values
299
'date': source_date or date,
301
'journal_id': journal_id,
302
'general_account_id': general_account_id or False,
303
'move_id': move_id or False,
304
'invoice_line_id': invoice_line_id or False,
306
'currency_id': currency_id,
307
'source_date': source_date or False,
308
'commitment_line_id': commitment_line_id or False,
310
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
311
# Browse distribution(s)
312
for distrib in self.browse(cr, uid, ids, context=context):
313
vals.update({'distribution_id': distrib.id,})
315
for distrib_lines in [distrib.cost_center_lines, distrib.funding_pool_lines, distrib.free_1_lines, distrib.free_2_lines]:
316
for distrib_line in distrib_lines:
317
context.update({'date': source_date or date}) # for amount computing
318
anal_amount = (distrib_line.percentage * amount) / 100
320
'amount': -1 * self.pool.get('res.currency').compute(cr, uid, currency_id, company_currency,
321
anal_amount, round=False, context=context),
322
'amount_currency': -1 * anal_amount,
323
'account_id': distrib_line.analytic_id.id,
325
# Update values if we come from a funding pool
326
if distrib_line._name == 'funding.pool.distribution.line':
327
vals.update({'cost_center_id': distrib_line.cost_center_id and distrib_line.cost_center_id.id or False,})
328
# create analytic line
329
al_id = self.pool.get('account.analytic.line').create(cr, uid, vals, context=context)
333
analytic_distribution()
334
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: