Files
tryton/statement.py
T

581 lines
22 KiB
Python
Raw Normal View History

#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
2010-08-16 19:23:32 +02:00
from decimal import Decimal
2012-03-25 00:17:31 +01:00
from trytond.model import Workflow, ModelView, ModelSQL, fields
2012-03-30 14:20:44 +02:00
from trytond.pyson import Eval, If, Bool
2010-08-16 19:23:32 +02:00
from trytond.transaction import Transaction
2011-04-10 20:59:25 +02:00
from trytond.backend import TableHandler
2011-07-03 13:18:45 +02:00
from trytond.pool import Pool
2008-06-17 12:09:01 +02:00
2011-08-21 11:20:33 +02:00
_STATES = {'readonly': Eval('state') != 'draft'}
2011-08-04 16:57:19 +02:00
_DEPENDS = ['state']
2008-06-10 22:01:27 +02:00
2008-06-17 12:09:01 +02:00
2012-03-25 00:17:31 +01:00
class Statement(Workflow, ModelSQL, ModelView):
'Account Statement'
_name = 'account.statement'
2008-06-10 22:01:27 +02:00
_description = __doc__
2011-04-10 20:59:25 +02:00
company = fields.Many2One('company.company', 'Company', required=True,
2012-03-05 10:52:58 +01:00
select=True, states=_STATES, domain=[
2011-08-21 11:20:33 +02:00
('id', If(Eval('context', {}).contains('company'), '=', '!='),
Eval('context', {}).get('company', 0)),
2011-08-04 16:57:19 +02:00
],
depends=_DEPENDS)
journal = fields.Many2One('account.statement.journal', 'Journal', required=True,
2011-04-10 20:59:25 +02:00
domain=[
2011-08-21 11:20:33 +02:00
('company', '=', Eval('context', {}).get('company', 0)),
2011-08-04 16:57:19 +02:00
],
2011-04-10 20:59:25 +02:00
states={
2011-08-21 11:20:33 +02:00
'readonly': (Eval('state') != 'draft') | Eval('lines'),
2011-08-04 16:57:19 +02:00
},
2012-03-05 10:52:58 +01:00
on_change=['journal', 'state', 'lines'], select=True,
2011-08-04 16:57:19 +02:00
depends=['state', 'lines'])
2010-03-11 16:46:35 +01:00
currency_digits = fields.Function(fields.Integer('Currency Digits',
on_change_with=['journal']), 'get_currency_digits')
2011-08-04 16:57:19 +02:00
date = fields.Date('Date', required=True, states=_STATES, depends=_DEPENDS,
2012-03-05 10:52:58 +01:00
select=True)
start_balance = fields.Numeric('Start Balance', required=True,
2011-08-04 16:57:19 +02:00
digits=(16, Eval('currency_digits', 2)),
states=_STATES, depends=['state', 'currency_digits'])
end_balance = fields.Numeric('End Balance', required=True,
2011-08-04 16:57:19 +02:00
digits=(16, Eval('currency_digits', 2)),
states=_STATES, depends=['state', 'currency_digits'])
balance = fields.Function(
fields.Numeric('Balance',
digits=(16, Eval('currency_digits', 2)),
on_change_with=['start_balance', 'end_balance'],
depends=['currency_digits']),
'get_balance')
lines = fields.One2Many('account.statement.line', 'statement',
2011-08-04 16:57:19 +02:00
'Transactions', states={
2011-08-21 11:20:33 +02:00
'readonly': (Eval('state') != 'draft') | ~Eval('journal'),
2011-08-04 16:57:19 +02:00
}, on_change=['lines', 'journal'],
depends=['state', 'journal'])
state = fields.Selection([
('draft', 'Draft'),
('validated', 'Validated'),
2009-03-20 13:04:48 +01:00
('cancel', 'Canceled'),
('posted', 'Posted'),
2012-03-05 10:52:58 +01:00
], 'State', readonly=True, select=True)
2010-03-11 16:46:35 +01:00
move_lines = fields.Function(fields.One2Many('account.move.line',
None, 'Move Lines'), 'get_move_lines')
2008-06-10 22:01:27 +02:00
def __init__(self):
super(Statement, self).__init__()
2008-06-11 19:50:30 +02:00
self._order[0] = ('id', 'DESC')
2008-07-17 19:36:26 +02:00
self._error_messages.update({
'wrong_end_balance': 'End Balance must be %s!',
2008-07-17 19:36:26 +02:00
})
2012-03-25 00:17:31 +01:00
self._transitions |= set((
('draft', 'validated'),
('validated', 'posted'),
('validated', 'cancel'),
('cancel', 'draft'),
))
self._buttons.update({
'draft': {
'invisible': Eval('state') != 'cancel',
},
'validate': {
'invisible': Eval('state') != 'draft',
},
'post': {
'invisible': Eval('state') != 'validated',
},
'cancel': {
'invisible': Eval('state') != 'validated',
},
})
2008-06-10 22:01:27 +02:00
2011-04-10 20:59:25 +02:00
def init(self, module_name):
cursor = Transaction().cursor
# Migration from 1.8: new field company
table = TableHandler(cursor, self, module_name)
company_exist = table.column_exist('company')
super(Statement, self).init(module_name)
# Migration from 1.8: fill new field company
if not company_exist:
offset = 0
limit = cursor.IN_MAX
statement_ids = True
while statement_ids:
statement_ids = self.search([], offset=offset, limit=limit)
offset += limit
for statement in self.browse(statement_ids):
self.write(statement.id, {
'company': statement.journal.company.id,
})
table = TableHandler(cursor, self, module_name)
table.not_null_action('company', action='add')
def default_company(self):
2012-03-27 10:43:12 +02:00
return Transaction().context.get('company')
2011-04-10 20:59:25 +02:00
2010-08-16 19:23:32 +02:00
def default_state(self):
2008-06-10 22:01:27 +02:00
return 'draft'
2010-08-16 19:23:32 +02:00
def default_date(self):
2011-07-03 13:18:45 +02:00
date_obj = Pool().get('ir.date')
2010-08-16 19:23:32 +02:00
return date_obj.today()
2008-07-30 16:49:16 +02:00
2010-08-16 19:23:32 +02:00
def default_currency_digits(self):
2011-07-03 13:18:45 +02:00
company_obj = Pool().get('company.company')
2010-08-16 19:23:32 +02:00
if Transaction().context.get('company'):
company = company_obj.browse(Transaction().context['company'])
return company.currency.digits
return 2
2010-08-16 19:23:32 +02:00
def on_change_journal(self, value):
res = {}
2008-06-10 22:01:27 +02:00
if not value.get('journal'):
return res
2010-08-16 19:23:32 +02:00
statement_ids = self.search([
('journal', '=', value['journal']),
], order=[
('date', 'DESC'),
2010-08-16 19:23:32 +02:00
], limit=1)
2008-06-17 11:44:14 +02:00
if not statement_ids:
return res
2010-08-16 19:23:32 +02:00
statement = self.browse(statement_ids[0])
res['start_balance'] = statement.end_balance
return res
2008-06-10 22:01:27 +02:00
2010-08-16 19:23:32 +02:00
def on_change_with_currency_digits(self, vals):
2011-07-03 13:18:45 +02:00
journal_obj = Pool().get('account.statement.journal')
if vals.get('journal'):
journal = journal_obj.browse(vals['journal'])
return journal.currency.digits
return 2
2010-08-16 19:23:32 +02:00
def get_currency_digits(self, ids, name):
res = {}
2010-08-16 19:23:32 +02:00
for statement in self.browse(ids):
res[statement.id] = statement.journal.currency.digits
return res
2010-08-16 19:23:32 +02:00
def get_rec_name(self, ids, name):
2011-07-03 13:18:45 +02:00
lang_obj = Pool().get('ir.lang')
2008-12-18 20:13:43 +01:00
if not ids:
return {}
2008-12-18 20:13:43 +01:00
2012-01-27 21:15:07 +01:00
lang_id, = lang_obj.search([
('code', '=', Transaction().language),
2010-08-16 19:23:32 +02:00
])
2012-01-27 21:15:07 +01:00
lang = lang_obj.browse(lang_id)
2008-12-18 20:13:43 +01:00
res = {}
2010-08-16 19:23:32 +02:00
for statement in self.browse(ids):
res[statement.id] = statement.journal.name + ' ' + \
2008-12-18 20:13:43 +01:00
lang.currency(lang, statement.start_balance,
statement.journal.currency, symbol=False,
2008-12-18 20:13:43 +01:00
grouping=True) + \
lang.currency(lang, statement.end_balance,
statement.journal.currency, symbol=False,
grouping=True)
2008-12-18 20:13:43 +01:00
return res
2010-08-16 19:23:32 +02:00
def search_rec_name(self, name, clause):
ids = self.search(['OR',
('start_balance',) + clause[1:],
('end_balance',) + clause[1:],
2010-08-16 19:23:32 +02:00
])
if ids:
return [('id', 'in', ids)]
return [('journal',) + clause[1:]]
2008-12-18 20:13:43 +01:00
2010-08-16 19:23:32 +02:00
def get_move_lines(self, ids, name):
2008-07-30 18:27:49 +02:00
'''
Return the move lines that have been generated by the statements.
'''
res = {}
2010-08-16 19:23:32 +02:00
for statement in self.browse(ids):
2008-07-30 18:27:49 +02:00
res[statement.id] = []
for line in statement.lines:
if not line.move:
continue
for move_line in line.move.lines:
res[statement.id].append(move_line.id)
return res
2010-08-16 19:23:32 +02:00
def get_end_balance(self, ids, name):
statements = self.browse(ids)
2008-06-10 22:01:27 +02:00
res = {}
for statement in statements:
res[statement.id] = statement.start_balance
for line in statement.lines:
res[statement.id] += line.amount
return res
def get_balance(self, ids, name):
return dict((s.id, s.end_balance - s.start_balance)
for s in self.browse(ids))
def on_change_with_balance(self, values):
if not set(('end_balance', 'start_balance')) <= set(values):
return Decimal(0)
else:
return Decimal(values.get('end_balance') or 0
- values.get('start_balance') or 0)
2010-08-16 19:23:32 +02:00
def on_change_lines(self, values):
2011-07-03 13:18:45 +02:00
pool = Pool()
invoice_obj = pool.get('account.invoice')
journal_obj = pool.get('account.statement.journal')
currency_obj = pool.get('currency.currency')
res = {
'lines': {},
}
if values.get('journal') and values.get('lines'):
journal = journal_obj.browse(values['journal'])
invoice_ids = set()
for line in values['lines']:
if line['invoice']:
invoice_ids.add(line['invoice'])
invoice_id2amount_to_pay = {}
for invoice in invoice_obj.browse(list(invoice_ids)):
with Transaction().set_context(date=invoice.currency_date):
invoice_id2amount_to_pay[invoice.id] = currency_obj.compute(
invoice.currency.id, invoice.amount_to_pay,
journal.currency.id)
for line in values['lines']:
if line['invoice'] and line['id']:
amount_to_pay = invoice_id2amount_to_pay[line['invoice']]
if abs(line['amount']) > amount_to_pay:
res['lines'].setdefault('update', [])
if currency_obj.is_zero(journal.currency,
amount_to_pay):
res['lines']['update'].append({
'id': line['id'],
2012-03-27 10:43:12 +02:00
'invoice': None,
})
else:
res['lines']['update'].append({
'id': line['id'],
'amount': amount_to_pay,
})
res['lines'].setdefault('add', [])
vals = line.copy()
del vals['id']
vals['amount'] = abs(line['amount']) - amount_to_pay
if line['amount'] < 0:
vals['amount'] = - vals['amount']
2012-03-27 10:43:12 +02:00
vals['invoice'] = None
res['lines']['add'].append(vals)
invoice_id2amount_to_pay[line['invoice']] = \
amount_to_pay - abs(line['amount'])
return res
2012-03-25 00:17:31 +01:00
@ModelView.button
@Workflow.transition('draft')
def draft(self, ids):
pass
@ModelView.button
@Workflow.transition('validated')
def validate(self, ids):
2011-07-03 13:18:45 +02:00
statement_line_obj = Pool().get('account.statement.line')
lang_obj = Pool().get('ir.lang')
2012-03-25 00:17:31 +01:00
for statement in self.browse(ids):
computed_end_balance = statement.start_balance
for line in statement.lines:
computed_end_balance += line.amount
if computed_end_balance != statement.end_balance:
lang_id, = lang_obj.search([
('code', '=', Transaction().language),
])
lang = lang_obj.browse(lang_id)
2012-03-25 00:17:31 +01:00
amount = lang_obj.format(lang,
'%.' + str(statement.journal.currency.digits) + 'f',
computed_end_balance, True)
self.raise_user_error('wrong_end_balance', error_args=(amount,))
for line in statement.lines:
statement_line_obj.create_move(line)
2008-06-10 22:01:27 +02:00
2012-03-25 00:17:31 +01:00
@ModelView.button
@Workflow.transition('posted')
def post(self, ids):
2011-07-03 13:18:45 +02:00
statement_line_obj = Pool().get('account.statement.line')
2012-03-25 00:17:31 +01:00
statements = self.browse(ids)
lines = [l for s in statements for l in s.lines]
statement_line_obj.post_move(lines)
2008-06-10 22:01:27 +02:00
2012-03-25 00:17:31 +01:00
@ModelView.button
@Workflow.transition('cancel')
def cancel(self, ids):
2011-07-03 13:18:45 +02:00
statement_line_obj = Pool().get('account.statement.line')
2012-03-25 00:17:31 +01:00
statements = self.browse(ids)
lines = [l for s in statements for l in s.lines]
statement_line_obj.delete_move(lines)
2008-06-11 19:50:30 +02:00
2008-06-10 22:01:27 +02:00
Statement()
2008-06-17 12:09:01 +02:00
2009-02-28 16:58:08 +01:00
class Line(ModelSQL, ModelView):
'Account Statement Line'
_name = 'account.statement.line'
2008-06-10 22:01:27 +02:00
_description = __doc__
statement = fields.Many2One('account.statement', 'Statement',
required=True, ondelete='CASCADE')
2008-06-10 22:01:27 +02:00
date = fields.Date('Date', required=True)
amount = fields.Numeric('Amount', required=True,
2011-08-21 11:20:33 +02:00
digits=(16, Eval('_parent_statement', {}).get( 'currency_digits', 2)),
on_change=['amount', 'party', 'account', 'invoice',
'_parent_statement.journal'])
2008-10-22 20:09:11 +02:00
party = fields.Many2One('party.party', 'Party',
on_change=['amount', 'party', 'invoice'])
account = fields.Many2One('account.account', 'Account', required=True,
2011-08-21 11:20:33 +02:00
on_change=['account', 'invoice'], domain=[
('company', '=', Eval('_parent_statement', {}).get('company', 0)),
2012-03-30 14:20:44 +02:00
If(Bool(Eval('party')),
If(Eval('amount', 0) > 0,
('kind', '=', 'receivable'),
('kind', '=', 'payable')),
('kind', 'in', ['payable', 'receivable', 'revenue', 'expense',
'other'])),
],
depends=['party', 'amount'])
description = fields.Char('Description')
move = fields.Many2One('account.move', 'Account Move', readonly=True)
invoice = fields.Many2One('account.invoice', 'Invoice',
2011-08-04 16:57:19 +02:00
domain=[
('party', '=', Eval('party')),
('account', '=', Eval('account')),
2011-08-21 11:20:33 +02:00
('state', '=', If(Eval('_parent_statement', {}).get('state')
== 'draft', 'open', '')),
2010-01-20 10:57:50 +01:00
],
2011-08-04 16:57:19 +02:00
states={
2011-08-21 11:20:33 +02:00
'readonly': ~Eval('amount'),
2011-08-04 16:57:19 +02:00
},
depends=['party', 'account', 'amount'])
2008-06-10 22:01:27 +02:00
2008-07-17 19:36:26 +02:00
def __init__(self):
super(Line, self).__init__()
self._error_messages.update({
2008-12-18 20:13:11 +01:00
'debit_credit_account_statement_journal': 'Please provide debit and ' \
'credit account on statement journal.',
'same_debit_credit_account': 'Credit or debit account on ' \
'journal is the same than the statement line account!',
'amount_greater_invoice_amount_to_pay': 'Amount (%s) greater than '\
'the amount to pay of invoice!',
2008-07-17 19:36:26 +02:00
})
self._sql_constraints += [
('check_statement_line_amount', 'CHECK(amount != 0)',
'Amount should be a positive or negative value!'),
]
2008-07-17 19:36:26 +02:00
2010-08-16 19:23:32 +02:00
def on_change_party(self, value):
2011-07-03 13:18:45 +02:00
party_obj = Pool().get('party.party')
invoice_obj = Pool().get('account.invoice')
res = {}
if value.get('party'):
2010-08-16 19:23:32 +02:00
party = party_obj.browse(value['party'])
if value.get('amount'):
if value['amount'] > Decimal("0.0"):
account = party.account_receivable
else:
account = party.account_payable
res['account'] = account.id
res['account.rec_name'] = account.rec_name
if value.get('invoice'):
if value.get('party'):
2010-08-16 19:23:32 +02:00
invoice = invoice_obj.browse(value['invoice'])
if invoice.party != value['party']:
2012-03-27 10:43:12 +02:00
res['invoice'] = None
else:
2012-03-27 10:43:12 +02:00
res['invoice'] = None
return res
2008-06-10 22:01:27 +02:00
2010-08-16 19:23:32 +02:00
def on_change_amount(self, value):
2011-07-03 13:18:45 +02:00
pool = Pool()
party_obj = pool.get('party.party')
invoice_obj = pool.get('account.invoice')
journal_obj = pool.get('account.statement.journal')
currency_obj = pool.get('currency.currency')
res = {}
if value.get('party'):
2010-08-16 19:23:32 +02:00
party = party_obj.browse(value['party'])
if value.get('account') and value['account'] not in (
party.account_receivable.id, party.account_payable.id):
# The user has entered a non-default value, we keep it.
pass
elif value.get('amount'):
if value['amount'] > Decimal("0.0"):
account = party.account_receivable
else:
account = party.account_payable
res['account'] = account.id
res['account.rec_name'] = account.rec_name
if value.get('invoice'):
if value.get('amount') and value.get('_parent_statement.journal'):
2010-08-16 19:23:32 +02:00
invoice = invoice_obj.browse(value['invoice'])
journal = journal_obj.browse(value['_parent_statement.journal'])
with Transaction().set_context(date=invoice.currency_date):
amount_to_pay = currency_obj.compute(invoice.currency.id,
invoice.amount_to_pay, journal.currency.id)
if abs(value['amount']) > amount_to_pay:
2012-03-27 10:43:12 +02:00
res['invoice'] = None
else:
2012-03-27 10:43:12 +02:00
res['invoice'] = None
return res
2010-08-16 19:23:32 +02:00
def on_change_account(self, value):
2011-07-03 13:18:45 +02:00
invoice_obj = Pool().get('account.invoice')
res = {}
if value.get('invoice'):
if value.get('account'):
2010-08-16 19:23:32 +02:00
invoice = invoice_obj.browse(value['invoice'])
if invoice.account.id != value['account']:
2012-03-27 10:43:12 +02:00
res['invoice'] = None
else:
2012-03-27 10:43:12 +02:00
res['invoice'] = None
return res
2008-06-17 18:33:07 +02:00
2010-08-16 19:23:32 +02:00
def create_move(self, line):
2008-09-24 17:43:00 +02:00
'''
2012-03-25 00:17:31 +01:00
Create move for the statement line and return move id if created.
2008-09-24 17:43:00 +02:00
'''
2011-07-03 13:18:45 +02:00
pool = Pool()
move_obj = pool.get('account.move')
period_obj = pool.get('account.period')
invoice_obj = pool.get('account.invoice')
currency_obj = pool.get('currency.currency')
move_line_obj = pool.get('account.move.line')
lang_obj = pool.get('ir.lang')
2012-03-25 00:17:31 +01:00
if line.move:
return
2011-04-10 20:59:25 +02:00
period_id = period_obj.find(line.statement.company.id,
2010-08-16 19:23:32 +02:00
date=line.date)
2010-08-16 19:23:32 +02:00
move_lines = self._get_move_lines(line)
move_id = move_obj.create({
'name': unicode(line.date),
'period': period_id,
'journal': line.statement.journal.journal.id,
'date': line.date,
'lines': [('create', x) for x in move_lines],
2010-08-16 19:23:32 +02:00
})
2010-08-16 19:23:32 +02:00
self.write(line.id, {
'move': move_id,
2010-08-16 19:23:32 +02:00
})
if line.invoice:
with Transaction().set_context(date=line.invoice.currency_date):
amount_to_pay = currency_obj.compute(line.invoice.currency.id,
line.invoice.amount_to_pay,
line.statement.journal.currency.id)
if amount_to_pay < abs(line.amount):
2012-01-27 21:15:07 +01:00
lang_id, = lang_obj.search([
('code', '=', Transaction().language),
2010-08-16 19:23:32 +02:00
])
2012-01-27 21:15:07 +01:00
lang = lang_obj.browse(lang_id)
amount = lang_obj.format(lang,
'%.' + str(line.statement.journal.currency.digits) + 'f',
line.amount, True)
2010-08-16 19:23:32 +02:00
self.raise_user_error('amount_greater_invoice_amount_to_pay',
error_args=(amount,))
with Transaction().set_context(date=line.invoice.currency_date):
amount = currency_obj.compute(
line.statement.journal.currency.id, line.amount,
line.statement.company.currency.id)
2010-08-16 19:23:32 +02:00
reconcile_lines = invoice_obj.get_reconcile_lines_for_amount(
line.invoice, abs(amount))
2010-08-16 19:23:32 +02:00
move = move_obj.browse(move_id)
line_id = None
for move_line in move.lines:
if move_line.account.id == line.invoice.account.id:
line_id = move_line.id
2010-08-16 19:23:32 +02:00
invoice_obj.write(line.invoice.id, {
'payment_lines': [('add', line_id)],
2010-08-16 19:23:32 +02:00
})
break
if reconcile_lines[1] == Decimal('0.0'):
line_ids = reconcile_lines[0] + [line_id]
2010-08-16 19:23:32 +02:00
move_line_obj.reconcile(line_ids)
2008-09-24 17:43:00 +02:00
return move_id
2008-06-17 19:21:23 +02:00
2010-08-16 19:23:32 +02:00
def post_move(self, lines):
2011-07-03 13:18:45 +02:00
move_obj = Pool().get('account.move')
2010-08-16 19:23:32 +02:00
move_obj.post([l.move.id for l in lines if l.move])
2008-06-17 19:21:23 +02:00
2010-08-16 19:23:32 +02:00
def delete_move(self, lines):
2011-07-03 13:18:45 +02:00
move_obj = Pool().get('account.move')
2010-08-16 19:23:32 +02:00
move_obj.delete([l.move.id for l in lines if l.move])
2008-07-25 12:51:24 +02:00
2010-08-16 19:23:32 +02:00
def _get_move_lines(self, statement_line):
2008-09-24 17:43:00 +02:00
'''
Return the values of the move lines for the statement line
:param statement_line: a BrowseRecord of the statement line
:return: a list of dictionary of move line values
'''
2011-07-03 13:18:45 +02:00
currency_obj = Pool().get('currency.currency')
zero = Decimal("0.0")
amount = currency_obj.compute(
statement_line.statement.journal.currency, statement_line.amount,
2011-04-10 20:59:25 +02:00
statement_line.statement.company.currency)
if statement_line.statement.journal.currency.id != \
2011-04-10 20:59:25 +02:00
statement_line.statement.company.currency.id:
second_currency = statement_line.statement.journal.currency.id
amount_second_currency = abs(statement_line.amount)
else:
amount_second_currency = None
second_currency = None
party_id = statement_line.party.id if statement_line.party else None
vals = []
vals.append({
'name': unicode(statement_line.date),
'debit': amount < zero and -amount or zero,
'credit': amount >= zero and amount or zero,
'account': statement_line.account.id,
'party': party_id,
'second_currency': second_currency,
'amount_second_currency': amount_second_currency,
})
journal = statement_line.statement.journal.journal
if statement_line.amount >= zero:
account = journal.credit_account
else:
account = journal.debit_account
if not account:
2010-08-16 19:23:32 +02:00
self.raise_user_error('debit_credit_account_statement_journal')
if statement_line.account.id == account.id:
2010-08-16 19:23:32 +02:00
self.raise_user_error('same_debit_credit_account')
vals.append({
'name': unicode(statement_line.date),
'debit': amount >= zero and amount or zero,
'credit': amount < zero and -amount or zero,
'account': account.id,
'party': party_id,
'second_currency': second_currency,
'amount_second_currency': amount_second_currency,
})
return vals
2008-06-10 22:01:27 +02:00
Line()