21
21
##############################################################################
22
22
from osv import fields, osv
24
from tools.translate import _
24
26
class account_bank_statement_line(osv.osv):
25
27
_inherit = "account.bank.statement.line"
26
28
_name = "account.bank.statement.line"
28
def _display_analytic_button(self, cr, uid, ids, name, args, context=None):
30
def _display_analytic_button(self, cr, uid, ids, name, args, context={}):
30
32
Return True for all element that correspond to some criteria:
31
33
- The entry state is draft
32
- The account is analytic-a-holic
34
- The account is an expense account
35
37
for absl in self.browse(cr, uid, ids, context=context):
37
39
# False if st_line is hard posted
38
40
if absl.state == 'hard':
39
41
res[absl.id] = False
40
# False if account not allocatable
41
if not absl.account_id.is_analytic_addicted:
42
# False if account not an expense account
43
if absl.account_id.user_type.code not in ['expense']:
42
44
res[absl.id] = False
45
def _get_distribution_state(self, cr, uid, ids, name, args, context=None):
47
Get state of distribution:
48
- if compatible with the line, then "valid"
49
- if no distribution, then "none"
50
- all other case are "invalid"
55
if isinstance(ids, (int, long)):
59
# Browse all given lines
60
for line in self.read(cr, uid, ids, ['analytic_distribution_id', 'account_id'], context=context):
61
if not line.get('analytic_distribution_id', False):
62
res[line.get('id')] = 'none'
64
distribution_id = line.get('analytic_distribution_id')[0]
65
account_id = line.get('account_id', [False])[0]
66
res[line.get('id')] = self.pool.get('analytic.distribution')._get_distribution_state(cr, uid, distribution_id, False, account_id)
70
48
'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
71
'display_analytic_button': fields.function(_display_analytic_button, method=True, string='Display analytic button?', type='boolean', readonly=True,
49
'display_analytic_button': fields.function(_display_analytic_button, method=True, string='Display analytic button?', type='boolean', readonly=True,
72
50
help="This informs system that we can display or not an analytic button", store=False),
73
'analytic_distribution_state': fields.function(_get_distribution_state, method=True, type='selection',
74
selection=[('none', 'None'), ('valid', 'Valid'), ('invalid', 'Invalid')],
75
string="Distribution state", help="Informs from distribution state among 'none', 'valid', 'invalid."),
79
54
'display_analytic_button': lambda *a: True,
82
def button_analytic_distribution(self, cr, uid, ids, context=None):
57
def button_analytic_distribution(self, cr, uid, ids, context={}):
84
59
Launch analytic distribution wizard from a statement line
89
63
if isinstance(ids, (int, long)):
92
absl = self.browse(cr, uid, ids[0], context=context)
93
amount = absl.amount * -1 or 0.0
65
# we get the analytical distribution object linked to this line
67
statement_line_obj = self.browse(cr, uid, ids[0], context=context)
68
amount = statement_line_obj.amount * -1 or 0.0
94
69
# Search elements for currency
95
70
company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
96
currency = absl.statement_id.journal_id.currency and absl.statement_id.journal_id.currency.id or company_currency
97
# Get analytic distribution id from this line
98
distrib_id = absl.analytic_distribution_id and absl.analytic_distribution_id.id or False
99
# Prepare values for wizard
101
'total_amount': amount,
102
'register_line_id': absl.id,
103
'currency_id': currency or False,
105
'account_id': absl.account_id and absl.account_id.id or False,
106
'posting_date': absl.date,
107
'document_date': absl.document_date,
110
vals.update({'distribution_id': distrib_id,})
112
wiz_obj = self.pool.get('analytic.distribution.wizard')
113
wiz_id = wiz_obj.create(cr, uid, vals, context=context)
114
# Update some context values
71
currency = statement_line_obj.statement_id.journal_id.currency and statement_line_obj.statement_id.journal_id.currency.id or company_currency
72
if statement_line_obj.analytic_distribution_id:
73
distrib_id = statement_line_obj.analytic_distribution_id.id
75
distrib_id = self.pool.get('analytic.distribution').create(cr, uid, {}, context=context)
76
newvals={'analytic_distribution_id': distrib_id}
77
super(account_bank_statement_line, self).write(cr, uid, ids, newvals, context=context)
78
wiz_obj = self.pool.get('wizard.costcenter.distribution')
79
wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id, 'currency_id': currency}, context=context)
116
82
'active_id': ids[0],
117
83
'active_ids': ids,
84
'wizard_ids': {'cost_center': wiz_id},
121
'name': 'Analytic distribution',
122
87
'type': 'ir.actions.act_window',
123
'res_model': 'analytic.distribution.wizard',
88
'res_model': 'wizard.costcenter.distribution',
124
89
'view_type': 'form',
125
90
'view_mode': 'form',