20
20
##############################################################################
22
from osv import osv, fields
23
from osv import fields
24
from tools.translate import _
24
26
class account_invoice(osv.osv):
25
27
_name = 'account.invoice'
26
28
_inherit = 'account.invoice'
28
def _have_analytic_distribution(self, cr, uid, ids, name, arg, context={}):
30
def _get_distribution_line_count(self, cr, uid, ids, name, args, context={}):
30
If invoice have an analytic distribution, return True, else return False
32
Return analytic distribution line count (given by analytic distribution)
32
34
# Some verifications
35
37
if isinstance(ids, (int, long)):
41
# Browse given invoices
38
42
for inv in self.browse(cr, uid, ids, context=context):
40
if inv.analytic_distribution_id:
43
res[inv.id] = inv.analytic_distribution_id and inv.analytic_distribution_id.lines_count or 'None'
45
'have_analytic_distribution': fields.function(_have_analytic_distribution, method=True, type='boolean', string='Have an analytic distribution?'),
47
'analytic_distribution_line_count': fields.function(_get_distribution_line_count, method=True, type='char', size=256,
48
string="Analytic distribution count", readonly=True, store=False),
48
51
def _hook_fields_for_refund(self, cr, uid, *args):
112
115
default.update({'analytic_distribution_id': new_distrib_id})
113
116
return super(account_invoice, self).copy(cr, uid, id, default, context)
118
def action_open_invoice(self, cr, uid, ids, context={}, *args):
120
Add verification on all lines for analytic_distribution_id to be present and valid !
125
if isinstance(ids, (int, long)):
127
# Browse invoice and all invoice lines to detect a non-valid line
128
for inv in self.browse(cr, uid, ids, context=context):
129
for invl in inv.invoice_line:
130
if inv.from_yml_test or invl.from_yml_test:
132
if invl.analytic_distribution_state != 'valid':
133
raise osv.except_osv(_('Error'), _('Analytic distribution is not valid for "%s"' % invl.name))
134
# FIXME: copy invoice analytic distribution header if valid and no analytic_distribution_id
135
# FIXME: what about analytic accountancy?
136
return super(account_invoice, self).action_open_invoice(cr, uid, ids, context, args)
115
138
account_invoice()
140
class account_invoice_line(osv.osv):
141
_name = 'account.invoice.line'
142
_inherit = 'account.invoice.line'
144
def _get_distribution_state(self, cr, uid, ids, name, args, context={}):
146
Get state of distribution:
147
- if compatible with the invoice line, then "valid"
148
- if no distribution, take a tour of invoice distribution, if compatible, then "valid"
149
- if no distribution on invoice line and invoice, then "none"
150
- all other case are "invalid"
155
if isinstance(ids, (int, long)):
157
# Prepare some values
159
# Browse all given lines
160
for line in self.browse(cr, uid, ids, context=context):
161
if line.from_yml_test:
162
res[line.id] = 'valid'
164
res[line.id] = self.pool.get('analytic.distribution')._get_distribution_state(cr, uid, line.analytic_distribution_id.id, line.invoice_id.analytic_distribution_id.id, line.account_id.id)
167
def _get_distribution_line_count(self, cr, uid, ids, name, args, context={}):
169
Return analytic distribution line count (given by analytic distribution)
174
if isinstance(ids, (int, long)):
176
# Prepare some values
178
# Browse given invoices
179
for invl in self.browse(cr, uid, ids, context=context):
180
res[invl.id] = invl.analytic_distribution_id and invl.analytic_distribution_id.lines_count or ''
183
def _have_analytic_distribution_from_header(self, cr, uid, ids, name, arg, context={}):
185
If invoice have an analytic distribution, return False, else return True
190
if isinstance(ids, (int, long)):
193
for inv in self.browse(cr, uid, ids, context=context):
195
if inv.analytic_distribution_id:
200
'analytic_distribution_line_count': fields.function(_get_distribution_line_count, method=True, type='char', size=256,
201
string="Analytic distribution count", readonly=True, store=False),
202
'analytic_distribution_state': fields.function(_get_distribution_state, method=True, type='selection',
203
selection=[('none', 'None'), ('valid', 'Valid'), ('invalid', 'Invalid')],
204
string="Distribution state", help="Informs from distribution state among 'none', 'valid', 'invalid."),
205
'have_analytic_distribution_from_header': fields.function(_have_analytic_distribution_from_header, method=True, type='boolean',
206
string='Header Distrib.?'),
207
'newline': fields.boolean('New line'),
211
'newline': lambda *a: True,
212
'have_analytic_distribution_from_header': lambda *a: True,
215
def create(self, cr, uid, vals, context={}):
216
vals['newline'] = False
217
return super(account_invoice_line, self).create(cr, uid, vals, context)
219
def copy_data(self, cr, uid, id, default={}, context={}):
221
Copy global distribution and give it to new invoice line
226
# Copy analytic distribution
227
invl = self.browse(cr, uid, [id], context=context)[0]
228
if invl.analytic_distribution_id:
229
new_distrib_id = self.pool.get('analytic.distribution').copy(cr, uid, invl.analytic_distribution_id.id, {}, context=context)
231
default.update({'analytic_distribution_id': new_distrib_id})
232
return super(account_invoice_line, self).copy_data(cr, uid, id, default, context)
234
account_invoice_line()
116
235
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: