20
20
##############################################################################
24
from tools.translate import _
26
class analytic_distribution(osv.osv):
27
_name = 'analytic.distribution'
28
_inherit = 'analytic.distribution'
30
def _get_distribution_state(self, cr, uid, distrib_id, parent_id, account_id, context=None):
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):
32
59
Return distribution state
34
61
if context is None:
36
# Have an analytic distribution on another account than analytic-a-holic account make no sense. So their analytic distribution is valid
37
logger = netsvc.Logger()
63
# Have an analytic distribution on another account than expense account make no sense. So their analytic distribution is valid
39
account = self.pool.get('account.account').read(cr, uid, account_id, ['is_analytic_addicted'])
40
if account and not account.get('is_analytic_addicted', False):
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':
44
70
return self._get_distribution_state(cr, uid, parent_id, False, account_id, context)
45
logger.notifyChannel("analytic distribution", netsvc.LOG_WARNING, _("%s: NONE!") % (distrib_id or ''))
47
distrib = self.browse(cr, uid, distrib_id)
72
distrib = self.browse(cr, uid, id)
48
73
# Search MSF Private Fund element, because it's valid with all accounts
50
fp_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution',
75
fp_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution',
51
76
'analytic_account_msf_private_funds')[1]
54
account = self.pool.get('account.account').read(cr, uid, account_id, ['destination_ids'])
79
account = self.pool.get('account.account').browse(cr, uid, account_id)
55
80
# Check Cost Center lines with destination/account link
56
81
for cc_line in distrib.cost_center_lines:
57
if cc_line.destination_id.id not in account.get('destination_ids', []):
58
logger.notifyChannel("analytic distribution", netsvc.LOG_WARNING, _("%s: Error, destination not compatible with G/L account in CC lines") % (distrib_id or ''))
82
if cc_line.destination_id.id not in [x.id for x in account.destination_ids]:
60
84
# Check Funding pool lines regarding:
61
85
# - destination / account
62
86
# - If analytic account is MSF Private funds
63
87
# - Cost center and funding pool compatibility
64
88
for fp_line in distrib.funding_pool_lines:
65
if fp_line.destination_id.id not in account.get('destination_ids', []):
66
logger.notifyChannel("analytic distribution", netsvc.LOG_WARNING, _("%s: Error, destination not compatible with G/L account for FP lines") % (distrib_id or ''))
89
if fp_line.destination_id.id not in [x.id for x in account.destination_ids]:
68
91
# If fp_line is MSF Private Fund, all is ok
69
92
if fp_line.analytic_id.id == fp_id:
71
94
if (account_id, fp_line.destination_id.id) not in [x.account_id and x.destination_id and (x.account_id.id, x.destination_id.id) for x in fp_line.analytic_id.tuple_destination_account_ids]:
72
logger.notifyChannel("analytic distribution", netsvc.LOG_WARNING, _("%s: Error, account/destination tuple not compatible with given FP analytic account") % (distrib_id or ''))
74
96
if fp_line.cost_center_id.id not in [x.id for x in fp_line.analytic_id.cost_center_ids]:
75
logger.notifyChannel("analytic distribution", netsvc.LOG_WARNING, _("%s: Error, CC is not compatible with given FP analytic account") % (distrib_id or ''))
79
def analytic_state_from_info(self, cr, uid, account_id, destination_id, cost_center_id, analytic_id, context=None):
81
Give analytic state from the given information.
82
Return result and some info if needed.
90
ana_obj = self.pool.get('account.analytic.account')
91
account = self.pool.get('account.account').browse(cr, uid, account_id, context=context)
92
fp = ana_obj.browse(cr, uid, analytic_id, context=context)
94
fp_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution', 'analytic_account_msf_private_funds')[1]
97
is_private_fund = False
98
if analytic_id == fp_id:
99
is_private_fund = True
100
# DISTRIBUTION VERIFICATION
101
# Check account user_type
102
if account.user_type_code != 'expense':
103
return res, _('Not an expense account')
104
# Check that destination is compatible with account
105
if destination_id not in [x.id for x in account.destination_ids]:
106
return 'invalid', _('Destination not compatible with account')
107
if not is_private_fund:
108
# Check that cost center is compatible with FP (except if FP is MSF Private Fund)
109
if cost_center_id not in [x.id for x in fp.cost_center_ids]:
110
return 'invalid', _('Cost Center not compatible with FP')
111
# Check that tuple account/destination is compatible with FP (except if FP is MSF Private Fund):
112
if (account_id, destination_id) not in [x.account_id and x.destination_id and (x.account_id.id, x.destination_id.id) for x in fp.tuple_destination_account_ids]:
113
return 'invalid', _('account/destination tuple not compatible with given FP analytic account')
100
analytic_distribution1()
102
class distribution_line(osv.osv):
103
_name = "distribution.line"
106
'name': fields.char('Name', size=64),
107
"distribution_id": fields.many2one('analytic.distribution', 'Associated Analytic Distribution', ondelete='cascade'),
108
"analytic_id": fields.many2one('account.analytic.account', 'Analytical Account'),
109
"amount": fields.float('Amount', digits_compute=dp.get_precision('Account')),
110
"percentage": fields.float('Percentage', digits=(16,4)),
111
"currency_id": fields.many2one('res.currency', 'Currency', required=True),
112
"date": fields.date(string="Date"),
113
"source_date": fields.date(string="Source Date", help="This date is for source_date for analytic lines"),
117
'name': 'Distribution Line',
118
'date': lambda *a: strftime('%Y-%m-%d'),
119
'source_date': lambda *a: strftime('%Y-%m-%d'),
122
def create_analytic_lines(self, cr, uid, ids, move_line_id, date, document_date, source_date=False, name=False, context=None):
124
Creates an analytic lines from a distribution line and an account.move.line
126
if isinstance(ids, (int, long)):
130
move_line = self.pool.get('account.move.line').browse(cr, uid, move_line_id)
131
company_currency_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.currency_id.id
133
for line in self.browse(cr, uid, ids):
134
amount_cur = (move_line.credit_currency - move_line.debit_currency) * line.percentage / 100
135
ctx = {'date': source_date or date}
136
amount = self.pool.get('res.currency').compute(cr, uid, move_line.currency_id.id, company_currency_id, amount_cur, round=False, context=ctx)
138
'account_id': line.analytic_id.id,
139
'amount_currency': amount_cur,
141
'currency_id': move_line.currency_id.id,
142
'general_account_id': move_line.account_id.id,
144
'source_date': source_date,
145
'document_date': document_date,
146
'journal_id': move_line.journal_id and move_line.journal_id.analytic_journal_id and move_line.journal_id.analytic_journal_id.id or False,
147
'move_id': move_line.id,
148
'name': name or move_line.name,
149
'distrib_id': line.distribution_id.id,
150
'distrib_line_id': '%s,%s'%(self._name, line.id),
152
if self._name == 'funding.pool.distribution.line':
154
'destination_id': line.destination_id and line.destination_id.id or False,
155
'cost_center_id': line.cost_center_id and line.cost_center_id.id or False,
157
ret[line.id] = self.pool.get('account.analytic.line').create(cr, uid, vals)
164
class cost_center_distribution_line(osv.osv):
165
_name = "cost.center.distribution.line"
166
_inherit = "distribution.line"
168
"destination_id": fields.many2one('account.analytic.account', 'Destination', domain="[('type', '!=', 'view'), ('category', '=', 'DEST')]", required=True),
171
cost_center_distribution_line()
173
class funding_pool_distribution_line(osv.osv):
174
_name = "funding.pool.distribution.line"
175
_inherit = "distribution.line"
177
"cost_center_id": fields.many2one('account.analytic.account', 'Cost Center Account', required=True),
178
"destination_id": fields.many2one('account.analytic.account', 'Destination', domain="[('type', '!=', 'view'), ('category', '=', 'DEST')]", required=True),
181
funding_pool_distribution_line()
183
class free_1_distribution_line(osv.osv):
184
_name = "free.1.distribution.line"
185
_inherit = "distribution.line"
187
"destination_id": fields.many2one('account.analytic.account', 'Destination', domain="[('type', '!=', 'view'), ('category', '=', 'DEST')]", required=False),
190
free_1_distribution_line()
192
class free_2_distribution_line(osv.osv):
193
_name = "free.2.distribution.line"
194
_inherit = "distribution.line"
196
"destination_id": fields.many2one('account.analytic.account', 'Destination', domain="[('type', '!=', 'view'), ('category', '=', 'DEST')]", required=False),
199
free_2_distribution_line()
201
class analytic_distribution(osv.osv):
202
_name = 'analytic.distribution'
203
_inherit = "analytic.distribution"
205
def _get_lines_count(self, cr, uid, ids, name=False, args=False, context=None):
207
Get count of each analytic distribution lines type.
208
Example: with an analytic distribution with 2 cost center, 3 funding pool and 1 Free 1:
209
2 CC; 3 FP; 1 F1; 0 F2;
210
(Number of chars: 20 chars + 4 x some lines number)
215
if isinstance(ids, (int, long)):
217
# Prepare some values
221
# Browse given invoices
222
for distrib in self.browse(cr, uid, ids, context=context):
224
txt += str(len(distrib.cost_center_lines) or '0') + ' CC; '
225
txt += str(len(distrib.funding_pool_lines) or '0') + ' FP; '
226
txt += str(len(distrib.free_1_lines) or '0') + ' F1; '
227
txt += str(len(distrib.free_2_lines) or '0') + ' F2'
230
res[distrib.id] = txt
234
'cost_center_lines': fields.one2many('cost.center.distribution.line', 'distribution_id', 'Cost Center Distribution'),
235
'funding_pool_lines': fields.one2many('funding.pool.distribution.line', 'distribution_id', 'Funding Pool Distribution'),
236
'free_1_lines': fields.one2many('free.1.distribution.line', 'distribution_id', 'Free 1 Distribution'),
237
'free_2_lines': fields.one2many('free.2.distribution.line', 'distribution_id', 'Free 2 Distribution'),
238
'name': fields.function(_get_lines_count, method=True, type='char', size=256, string="Name", readonly=True, store=False),
241
def update_distribution_line_amount(self, cr, uid, ids, amount=False, context=None):
243
Update amount on distribution lines for given distribution (ids)
248
if isinstance(ids, (int, long)):
252
# Process distributions
253
for distrib_id in ids:
254
for dl_name in ['cost.center.distribution.line', 'funding.pool.distribution.line', 'free.1.distribution.line', 'free.2.distribution.line']:
255
dl_obj = self.pool.get(dl_name)
256
dl_ids = dl_obj.search(cr, uid, [('distribution_id', '=', distrib_id)], context=context)
257
for dl in dl_obj.browse(cr, uid, dl_ids, context=context):
259
'amount': round(dl.percentage * amount) / 100.0,
261
dl_obj.write(cr, uid, [dl.id], dl_vals, context=context)
264
def update_distribution_line_account(self, cr, uid, line_ids, account_id, context=None):
266
Update account on distribution line
271
if isinstance(line_ids, (int, long)):
272
line_ids = [line_ids]
275
# Prepare some values
276
account = self.pool.get('account.analytic.account').browse(cr, uid, [account_id], context=context)[0]
278
if account.category == 'OC':
279
vals = {'cost_center_id': account_id}
281
vals = {'analytic_id': account_id}
282
return self.pool.get('funding.pool.distribution.line').write(cr, uid, line_ids, vals)
284
def create_funding_pool_lines(self, cr, uid, ids, account_id=False, context=None):
286
Create funding pool lines regarding cost_center_lines from analytic distribution.
287
If funding_pool_lines exists, then nothing appends.
288
By default, add funding_pool_lines with MSF Private Fund element (written in an OpenERP demo file).
289
For destination axis, get those from account_id default configuration (default_destination_id).
294
if isinstance(ids, (int, long)):
296
# Prepare some values
298
# Browse distributions
299
for distrib in self.browse(cr, uid, ids, context=context):
300
if distrib.funding_pool_lines:
301
res[distrib.id] = False
303
# Browse cost center lines
304
for line in distrib.cost_center_lines:
305
# Search MSF Private Fund
307
pf_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution',
308
'analytic_account_msf_private_funds')[1]
313
'analytic_id': pf_id,
314
'amount': line.amount or 0.0,
315
'percentage': line.percentage or 0.0,
316
'currency_id': line.currency_id and line.currency_id.id or False,
317
'distribution_id': distrib.id or False,
318
'cost_center_id': line.analytic_id and line.analytic_id.id or False,
319
'destination_id': line.destination_id and line.destination_id.id or False,
321
# Search default destination if no one given
322
if account_id and not vals.get('destination_id'):
323
account = self.pool.get('account.account').browse(cr, uid, account_id)
324
if account and account.user_type and account.user_type.code == 'expense':
325
vals.update({'destination_id': account.default_destination_id and account.default_destination_id.id or False})
326
new_pf_line_id = self.pool.get('funding.pool.distribution.line').create(cr, uid, vals, context=context)
327
res[distrib.id] = True
330
def create_analytic_lines(self, cr, uid, ids, name, date, amount, journal_id, currency_id, document_date=False, ref=False, source_date=False, general_account_id=False, \
331
move_id=False, invoice_line_id=False, commitment_line_id=False, context=None):
333
Create analytic lines from given elements:
337
- journal_id (analytic_journal_id)
340
- source_date (optional)
341
- general_account_id (optional)
343
- invoice_line_id (optional)
344
- commitment_line_id (optional)
345
Return all created ids, otherwise return false (or [])
350
if isinstance(ids, (int, long)):
352
if not name or not date or not amount or not journal_id or not currency_id:
354
if not document_date:
356
# Prepare some values
360
'date': source_date or date,
361
'document_date': document_date,
363
'journal_id': journal_id,
364
'general_account_id': general_account_id or False,
365
'move_id': move_id or False,
366
'invoice_line_id': invoice_line_id or False,
368
'currency_id': currency_id,
369
'source_date': source_date or False,
370
'commitment_line_id': commitment_line_id or False,
372
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
373
# Browse distribution(s)
374
for distrib in self.browse(cr, uid, ids, context=context):
375
vals.update({'distribution_id': distrib.id,})
377
for distrib_lines in [distrib.funding_pool_lines, distrib.free_1_lines, distrib.free_2_lines]:
378
for distrib_line in distrib_lines:
379
context.update({'date': source_date or date}) # for amount computing
380
anal_amount = (distrib_line.percentage * amount) / 100
382
'amount': -1 * self.pool.get('res.currency').compute(cr, uid, currency_id, company_currency,
383
anal_amount, round=False, context=context),
384
'amount_currency': -1 * anal_amount,
385
'account_id': distrib_line.analytic_id.id,
386
'cost_center_id': False,
387
'destination_id': False,
388
'distrib_line_id': '%s,%s'%(distrib_line._name, distrib_line.id),
390
# Update values if we come from a funding pool
391
if distrib_line._name == 'funding.pool.distribution.line':
392
vals.update({'cost_center_id': distrib_line.cost_center_id and distrib_line.cost_center_id.id or False,
393
'destination_id': distrib_line.destination_id and distrib_line.destination_id.id or False,})
394
# create analytic line
395
al_id = self.pool.get('account.analytic.line').create(cr, uid, vals, context=context)
116
399
analytic_distribution()
117
400
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: