diff --git a/__tryton__.py b/__tryton__.py
index 5cee0b4f50..222d8b7b2b 100644
--- a/__tryton__.py
+++ b/__tryton__.py
@@ -24,10 +24,12 @@
'company',
'currency',
'party',
+ 'account_invoice',
],
'xml': [
'statement.xml',
'journal.xml',
+ 'invoice.xml',
],
'translation': [
'de_DE.csv',
diff --git a/invoice.xml b/invoice.xml
new file mode 100644
index 0000000000..b11c50e522
--- /dev/null
+++ b/invoice.xml
@@ -0,0 +1,22 @@
+
+
+
+
+
+ account.invoice
+ form
+
+
+
+
+
+
+ ]]>
+
+
+
+
diff --git a/journal.py b/journal.py
index b0b8dbb045..0051fc42c6 100644
--- a/journal.py
+++ b/journal.py
@@ -1,4 +1,5 @@
-#This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms.
+#This file is part of Tryton. The COPYRIGHT file at the top level of
+#this repository contains the full copyright notices and license terms.
"Statement Journal"
from trytond.osv import fields, OSV
@@ -10,7 +11,7 @@ class Journal(OSV):
_description = __doc__
name = fields.Char('Name', required=True)
- journal = fields.Many2One('account.journal', 'Statement Journal', required=True,
+ journal = fields.Many2One('account.journal', 'Journal', required=True,
domain="[('type', '=', 'statement')]")
currency = fields.Many2One('currency.currency', 'Currency', required=True)
company = fields.Many2One('company.company', 'Company', required=True)
diff --git a/statement.py b/statement.py
index 34e23656af..c83e8d441f 100644
--- a/statement.py
+++ b/statement.py
@@ -1,4 +1,5 @@
-#This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms.
+#This file is part of Tryton. The COPYRIGHT file at the top level of
+#this repository contains the full copyright notices and license terms.
"Statement"
from trytond.osv import fields, OSV
@@ -14,13 +15,19 @@ class Statement(OSV):
_description = __doc__
journal = fields.Many2One('account.statement.journal', 'Journal', required=True,
- states=_STATES, on_change=['journal'], select=1)
+ states={
+ 'readonly': "(state != 'draft') or (bool(lines))",
+ }, on_change=['journal'], select=1)
+ currency_digits = fields.Function('get_currency_digits', type='integer',
+ string='Currency Digits', on_change_with=['journal'])
date = fields.Date('Date', required=True, states=_STATES, select=1)
start_balance = fields.Numeric('Start Balance', digits=(16, 2),
states=_STATES)
end_balance = fields.Numeric('End Balance', digits=(16, 2), states=_STATES)
lines = fields.One2Many('account.statement.line', 'statement',
- 'Transactions', states=_STATES)
+ 'Transactions', states={
+ 'readonly': "(state != 'draft') or (not bool(journal))",
+ }, on_change=['lines', 'journal'])
state = fields.Selection([
('draft', 'Draft'),
('validated', 'Validated'),
@@ -37,9 +44,7 @@ class Statement(OSV):
]
self._order[0] = ('id', 'DESC')
self._error_messages.update({
- 'wrong_end_balance': 'Wrong End balance:\n' \
- ' * Expected: %s\n' \
- ' * Computed: %s',
+ 'wrong_end_balance': 'End Balance must be %s!',
})
def default_state(self, cursor, user, context=None):
@@ -49,6 +54,16 @@ class Statement(OSV):
date_obj = self.pool.get('ir.date')
return date_obj.today(cursor, user, context=context)
+ def default_currency_digits(self, cursor, user, context=None):
+ company_obj = self.pool.get('company.company')
+ if context is None:
+ context = {}
+ if context.get('company'):
+ company = company_obj.browse(cursor, user, context['company'],
+ context=context)
+ return company.currency.digits
+ return 2
+
def on_change_journal(self, cursor, user, ids, value, context=None):
res = {}
if not value.get('journal'):
@@ -66,6 +81,21 @@ class Statement(OSV):
res['start_balance'] = statement.end_balance
return res
+ def on_change_with_currency_digits(self, cursor, user, ids, vals,
+ context=None):
+ journal_obj = self.pool.get('account.statement.journal')
+ if vals.get('journal'):
+ journal = journal_obj.browse(cursor, user, vals['journal'],
+ context=context)
+ return journal.currency.digits
+ return 2
+
+ def get_currency_digits(self, cursor, user, ids, name, arg, context=None):
+ res = {}
+ for statement in self.browse(cursor, user, ids, context=context):
+ res[statement.id] = statement.journal.currency.digits
+ return res
+
def get_move_lines(self, cursor, user, ids, name, args, context=None):
'''
Return the move lines that have been generated by the statements.
@@ -89,8 +119,61 @@ class Statement(OSV):
res[statement.id] += line.amount
return res
+ def on_change_lines(self, cursor, user, ids, values, context=None):
+ invoice_obj = self.pool.get('account.invoice')
+ journal_obj = self.pool.get('account.statement.journal')
+ currency_obj = self.pool.get('currency.currency')
+ res = {
+ 'lines': {},
+ }
+ if values.get('journal') and values.get('lines'):
+ journal = journal_obj.browse(cursor, user, values['journal'],
+ context=context)
+ 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(cursor, user, invoice_ids,
+ context=context):
+ invoice_id2amount_to_pay[invoice.id] = currency_obj.compute(
+ cursor, user, invoice.currency, invoice.amount_to_pay,
+ journal.currency, context=context)
+
+ 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(cursor, user, journal.currency,
+ amount_to_pay):
+ res['lines']['update'].append({
+ 'id': line['id'],
+ 'invoice': False,
+ })
+ 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']
+ vals['invoice'] = False
+ res['lines']['add'].append(vals)
+ invoice_id2amount_to_pay[line['invoice']] = \
+ amount_to_pay - abs(line['amount'])
+ return res
+
def set_state_validated(self, cursor, user, statement_id, context=None):
statement_line_obj = self.pool.get('account.statement.line')
+ lang_obj = self.pool.get('ir.lang')
+
+ if context is None:
+ context = {}
statement = self.browse(cursor, user, statement_id, context=context)
@@ -98,9 +181,18 @@ class Statement(OSV):
for line in statement.lines:
computed_end_balance += line.amount
if computed_end_balance != statement.end_balance:
+ for code in [context.get('language', False) or 'en_US', 'en_US']:
+ lang_ids = lang_obj.search(cursor, user, [
+ ('code', '=', code),
+ ], context=context)
+ if lang_ids:
+ break
+ lang = lang_obj.browse(cursor, user, lang_ids[0], context=context)
+
+ amount = lang_obj.currency(lang, computed_end_balance,
+ statement.journal.currency, symbol=False, grouping=True)
self.raise_user_error(cursor, 'wrong_end_balance',
- (statement.end_balance, computed_end_balance),
- context=context)
+ error_args=(amount,), context=context)
for line in statement.lines:
statement_line_obj.create_move(cursor, user, line, context=context)
self.write(cursor, user, statement_id, {
@@ -148,53 +240,108 @@ class Line(OSV):
statement = fields.Many2One('account.statement', 'Statement',
required=True, ondelete='CASCADE')
date = fields.Date('Date', required=True)
- amount = fields.Numeric('Amount', digits=(16,2), required=True,
- on_change=['amount', 'party', 'account'])
+ amount = fields.Numeric('Amount', required=True,
+ digits="(16, _parent_statement.currency_digits)",
+ on_change=['amount', 'party', 'account', 'invoice',
+ '_parent_statement.journal'])
party = fields.Many2One('party.party', 'Party',
- on_change=['amount', 'party'])
- account = fields.Many2One('account.account', 'Account', required=True)
+ on_change=['amount', 'party', 'invoice'])
+ account = fields.Many2One('account.account', 'Account', required=True,
+ on_change=['account', 'invoice'])
description = fields.Char('Description')
move = fields.Many2One('account.move', 'Account Move', readonly=True)
+ invoice = fields.Many2One('account.invoice', 'Invoice',
+ domain="[('party', '=', party), ('account', '=', account)] " \
+ "+ (_parent_statement.state == 'draft' and " \
+ "[('state', '=', 'open')]) or []",
+ states={
+ 'readonly': "not bool(amount)",
+ })
def __init__(self):
super(Line, self).__init__()
self._error_messages.update({
'debit_credit_account_bank_journal': 'Please provide debit and ' \
'credit account on bank journal.',
+ 'same_debit_credit_account': 'Credit or debit account on ' \
+ 'journal is the same than the statement line account!',
})
def on_change_party(self, cursor, user, ids, value, context=None):
- res = {}
- if not (value.get('party') and value.get('amount')):
- return res
party_obj = self.pool.get('party.party')
account_obj = self.pool.get('account.account')
- party = party_obj.browse(cursor, user, value['party'], context=context)
- if value['amount'] > Decimal("0.0"):
- account = party.account_receivable
- else:
- account = party.account_payable
- res['account'] = account_obj.name_get(cursor, user, account.id,
- context=context)[0]
+ invoice_obj = self.pool.get('account.invoice')
+ res = {}
+
+ if value.get('party'):
+ party = party_obj.browse(cursor, user, value['party'],
+ context=context)
+ if value.get('amount'):
+ if value['amount'] > Decimal("0.0"):
+ account = party.account_receivable
+ else:
+ account = party.account_payable
+ res['account'] = account_obj.name_get(cursor, user, account.id,
+ context=context)[0]
+
+ if value.get('invoice'):
+ if value.get('party'):
+ invoice = invoice_obj.browse(cursor, user, value['invoice'],
+ context=context)
+ if invoice.party != value['party']:
+ res['invoice'] = False
+ else:
+ res['invoice'] = False
return res
def on_change_amount(self, cursor, user, ids, value, context=None):
- res = {}
- if not (value.get('party') and value.get('amount')):
- return res
party_obj = self.pool.get('party.party')
account_obj = self.pool.get('account.account')
- party = party_obj.browse(cursor, user, value['party'], context=context)
- 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.
- return res
- if value['amount'] > Decimal("0.0"):
- account = party.account_receivable
- else:
- account = party.account_payable
- res['account'] = account_obj.name_get(cursor, user, account.id,
- context=context)[0]
+ invoice_obj = self.pool.get('account.invoice')
+ journal_obj = self.pool.get('account.statement.journal')
+ currency_obj = self.pool.get('currency.currency')
+ res = {}
+
+ if value.get('party'):
+ party = party_obj.browse(cursor, user, value['party'], context=context)
+ 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_obj.name_get(cursor, user, account.id,
+ context=context)[0]
+ if value.get('invoice'):
+ if value.get('amount') and value.get('_parent_statement.journal'):
+ invoice = invoice_obj.browse(cursor, user, value['invoice'],
+ context=context)
+ journal = journal_obj.browse(cursor, user,
+ value['_parent_statement.journal'], context=context)
+ amount_to_pay = currency_obj.compute(cursor, user,
+ invoice.currency, invoice.amount_to_pay,
+ journal.currency, context=context)
+ if abs(value['amount']) > amount_to_pay:
+ res['invoice'] = False
+ else:
+ res['invoice'] = False
+ return res
+
+ def on_change_account(self, cursor, user, ids, value, context=None):
+ invoice_obj = self.pool.get('account.invoice')
+ res = {}
+
+ if value.get('invoice'):
+ if value.get('account'):
+ invoice = invoice_obj.browse(cursor, user, value['invoice'],
+ context=context)
+ if invoice.account.id != value['account']:
+ res['invoice'] = False
+ else:
+ res['invoice'] = False
return res
def create_move(self, cursor, user, line, context=None):
@@ -209,6 +356,10 @@ class Line(OSV):
'''
move_obj = self.pool.get('account.move')
period_obj = self.pool.get('account.period')
+ invoice_obj = self.pool.get('account.invoice')
+ currency_obj = self.pool.get('currency.currency')
+ move_line_obj = self.pool.get('account.move.line')
+
period_id = period_obj.find(cursor, user,
line.statement.journal.company.id, date=line.date,
context=context)
@@ -221,9 +372,32 @@ class Line(OSV):
'date': line.date,
'lines': [('create', x) for x in move_lines],
}, context=context)
+
self.write(cursor, user, line.id, {
'move': move_id,
}, context=context)
+
+ if line.invoice:
+ amount = currency_obj.compute(cursor, user,
+ line.statement.journal.currency, line.amount,
+ line.statement.journal.company.currency,
+ context=context)
+
+ reconcile_lines = invoice_obj.get_reconcile_lines_for_amount(cursor,
+ user, line.invoice, abs(amount))
+
+ move = move_obj.browse(cursor, user, move_id, context=context)
+ line_id = None
+ for move_line in move.lines:
+ if move_line.account.id == line.invoice.account.id:
+ line_id = move_line.id
+ invoice_obj.write(cursor, user, line.invoice.id, {
+ 'payment_lines': [('add', line_id)],
+ }, context=context)
+ break
+ if reconcile_lines[1] == Decimal('0.0'):
+ line_ids = reconcile_lines[0] + [line_id]
+ move_line_obj.reconcile(cursor, user, line_ids, context=context)
return move_id
def post_move(self, cursor, user, lines, context=None):
@@ -278,6 +452,9 @@ class Line(OSV):
if not account:
self.raise_user_error(cursor, 'debit_credit_account_bank_journal',
context=context)
+ if statement_line.account.id == account.id:
+ self.raise_user_error(cursor, 'same_debit_credit_account',
+ context=context)
vals.append({
'name': statement_line.date,
'debit': amount >= zero and amount or zero,
diff --git a/statement.xml b/statement.xml
index 9cda03a672..6cf4b13181 100644
--- a/statement.xml
+++ b/statement.xml
@@ -1,5 +1,6 @@
-
+
@@ -31,6 +32,7 @@
+
@@ -52,6 +54,7 @@
+
]]>
@@ -160,8 +163,11 @@
+
+
+
-
+
@@ -178,6 +184,7 @@
+
]]>