Update for new Transaction

This commit is contained in:
Korbinian Preisler committed 2010-08-16 19:23:32 +02:00
1 parent 9e59cee506
commit 36745f0d5f
3 files changed
+110 -157

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@ Installing trytond_account_statement
Prerequisites
-------------
* Python 2.4 or later (http://www.python.org/)
* Python 2.5 or later (http://www.python.org/)
* trytond (http://www.tryton.org/)
* trytond_account (http://www.tryton.org/)
* trytond_company (http://www.tryton.org/)
+6 -10
View File
@@ -1,7 +1,7 @@
#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.model import ModelView, ModelSQL, fields
from trytond.transaction import Transaction
class Journal(ModelSQL, ModelView):
@@ -15,18 +15,14 @@ class Journal(ModelSQL, ModelView):
currency = fields.Many2One('currency.currency', 'Currency', required=True)
company = fields.Many2One('company.company', 'Company', required=True)
def default_currency(self, cursor, user, context=None):
if context and context.get('company'):
def default_currency(self):
if Transaction().context.get('company'):
company_obj = self.pool.get('company.company')
currency_obj = self.pool.get('currency.currency')
company = company_obj.browse(cursor, user, context['company'],
context=context)
company = company_obj.browse(Transaction().context['company'])
return company.currency.id
return False
def default_company(self, cursor, user, context=None):
if context and context.get('company'):
return context['company']
return False
def default_company(self):
return Transaction().context.get('company') or False
Journal()
+103 -146
View File
@@ -1,9 +1,9 @@
#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 decimal import Decimal
from trytond.model import ModelWorkflow, ModelView, ModelSQL, fields
from trytond.pyson import Not, Equal, Eval, Or, Bool, Get, If
from decimal import Decimal
from trytond.transaction import Transaction
_STATES = {'readonly': Not(Equal(Eval('state'), 'draft'))}
@@ -51,73 +51,66 @@ class Statement(ModelWorkflow, ModelSQL, ModelView):
'wrong_end_balance': 'End Balance must be %s!',
})
def default_state(self, cursor, user, context=None):
def default_state(self):
return 'draft'
def default_date(self, cursor, user, context=None):
def default_date(self):
date_obj = self.pool.get('ir.date')
return date_obj.today(cursor, user, context=context)
return date_obj.today()
def default_currency_digits(self, cursor, user, context=None):
def default_currency_digits(self):
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)
if Transaction().context.get('company'):
company = company_obj.browse(Transaction().context['company'])
return company.currency.digits
return 2
def on_change_journal(self, cursor, user, value, context=None):
def on_change_journal(self, value):
res = {}
if not value.get('journal'):
return res
statement_ids = self.search(cursor, user, [
statement_ids = self.search([
('journal', '=', value['journal']),
], order=[
('date', 'DESC'),
], limit=1, context=context)
], limit=1)
if not statement_ids:
return res
statement = self.browse(cursor, user, statement_ids[0], context=context)
statement = self.browse(statement_ids[0])
res['start_balance'] = statement.end_balance
return res
def on_change_with_currency_digits(self, cursor, user, vals, context=None):
def on_change_with_currency_digits(self, vals):
journal_obj = self.pool.get('account.statement.journal')
if vals.get('journal'):
journal = journal_obj.browse(cursor, user, vals['journal'],
context=context)
journal = journal_obj.browse(vals['journal'])
return journal.currency.digits
return 2
def get_currency_digits(self, cursor, user, ids, name, context=None):
def get_currency_digits(self, ids, name):
res = {}
for statement in self.browse(cursor, user, ids, context=context):
for statement in self.browse(ids):
res[statement.id] = statement.journal.currency.digits
return res
def get_rec_name(self, cursor, user, ids, name, context=None):
def get_rec_name(self, ids, name):
lang_obj = self.pool.get('ir.lang')
if context is None:
context = {}
if not ids:
return {}
for code in [context.get('language', False) or 'en_US', 'en_US']:
lang_ids = lang_obj.search(cursor, user, [
for code in [Transaction().language, 'en_US']:
lang_ids = lang_obj.search([
('code', '=', code),
], context=context)
])
if lang_ids:
break
lang = lang_obj.browse(cursor, user, lang_ids[0], context=context)
lang = lang_obj.browse(lang_ids[0])
res = {}
for statement in self.browse(cursor, user, ids, context=context):
for statement in self.browse(ids):
res[statement.id] = statement.journal.name + ' ' + \
lang.currency(lang, statement.start_balance,
statement.journal.currency, symbol=False,
@@ -127,21 +120,21 @@ class Statement(ModelWorkflow, ModelSQL, ModelView):
grouping=True)
return res
def search_rec_name(self, cursor, user, name, clause, context=None):
ids = self.search(cursor, user, ['OR',
def search_rec_name(self, name, clause):
ids = self.search(['OR',
('start_balance',) + clause[1:],
('end_balance',) + clause[1:],
], context=context)
])
if ids:
return [('id', 'in', ids)]
return [('journal',) + clause[1:]]
def get_move_lines(self, cursor, user, ids, name, context=None):
def get_move_lines(self, ids, name):
'''
Return the move lines that have been generated by the statements.
'''
res = {}
for statement in self.browse(cursor, user, ids, context=context):
for statement in self.browse(ids):
res[statement.id] = []
for line in statement.lines:
if not line.move:
@@ -150,8 +143,8 @@ class Statement(ModelWorkflow, ModelSQL, ModelView):
res[statement.id].append(move_line.id)
return res
def get_end_balance(self, cursor, user, ids, name, context=None):
statements = self.browse(cursor, user, ids, context=context)
def get_end_balance(self, ids, name):
statements = self.browse(ids)
res = {}
for statement in statements:
res[statement.id] = statement.start_balance
@@ -159,7 +152,7 @@ class Statement(ModelWorkflow, ModelSQL, ModelView):
res[statement.id] += line.amount
return res
def on_change_lines(self, cursor, user, values, context=None):
def on_change_lines(self, values):
invoice_obj = self.pool.get('account.invoice')
journal_obj = self.pool.get('account.statement.journal')
currency_obj = self.pool.get('currency.currency')
@@ -167,25 +160,23 @@ class Statement(ModelWorkflow, ModelSQL, ModelView):
'lines': {},
}
if values.get('journal') and values.get('lines'):
journal = journal_obj.browse(cursor, user, values['journal'],
context=context)
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(cursor, user, invoice_ids,
context=context):
for invoice in invoice_obj.browse(invoice_ids):
invoice_id2amount_to_pay[invoice.id] = currency_obj.compute(
cursor, user, invoice.currency, invoice.amount_to_pay,
journal.currency, context=context)
invoice.currency, invoice.amount_to_pay,
journal.currency)
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,
if currency_obj.is_zero(journal.currency,
amount_to_pay):
res['lines']['update'].append({
'id': line['id'],
@@ -208,63 +199,57 @@ class Statement(ModelWorkflow, ModelSQL, ModelView):
amount_to_pay - abs(line['amount'])
return res
def set_state_validated(self, cursor, user, statement_id, context=None):
def set_state_validated(self, statement_id):
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)
statement = self.browse(statement_id)
computed_end_balance = statement.start_balance
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, [
for code in [Transaction().language, 'en_US']:
lang_ids = lang_obj.search([
('code', '=', code),
], context=context)
])
if lang_ids:
break
lang = lang_obj.browse(cursor, user, lang_ids[0], context=context)
lang = lang_obj.browse(lang_ids[0])
amount = lang_obj.format(lang,
'%.' + str(statement.journal.currency.digits) + 'f',
computed_end_balance, True)
self.raise_user_error(cursor, 'wrong_end_balance',
error_args=(amount,), context=context)
self.raise_user_error('wrong_end_balance', error_args=(amount,))
for line in statement.lines:
statement_line_obj.create_move(cursor, user, line, context=context)
self.write(cursor, user, statement_id, {
statement_line_obj.create_move(line)
self.write(statement_id, {
'state':'validated',
}, context=context)
})
def set_state_posted(self, cursor, user, statement_id, context=None):
def set_state_posted(self, statement_id):
statement_line_obj = self.pool.get('account.statement.line')
statement = self.browse(cursor, user, statement_id, context=context)
statement_line_obj.post_move(cursor, user, statement.lines,
context=context)
self.write(cursor, user, statement_id, {
statement = self.browse(statement_id)
statement_line_obj.post_move(statement.lines)
self.write(statement_id, {
'state':'posted',
}, context=context)
})
def set_state_cancel(self, cursor, user, statement_id, context=None):
def set_state_cancel(self, statement_id):
statement_line_obj = self.pool.get('account.statement.line')
statement = self.browse(cursor, user, statement_id, context=context)
statement_line_obj.delete_move(cursor, user, statement.lines,
context=context)
self.write(cursor, user, statement_id, {
statement = self.browse(statement_id)
statement_line_obj.delete_move(statement.lines)
self.write(statement_id, {
'state':'cancel',
}, context=context)
})
def draft_workflow(self, cursor, user, ids, context=None):
self.workflow_trigger_create(cursor, user, ids, context=context)
self.write(cursor, user, ids, {
def draft_workflow(self, ids):
self.workflow_trigger_create(ids)
self.write(ids, {
'state': 'draft',
}, context=context)
})
return True
Statement()
@@ -311,15 +296,13 @@ class Line(ModelSQL, ModelView):
'the amount to pay of invoice!',
})
def on_change_party(self, cursor, user, value, context=None):
def on_change_party(self, value):
party_obj = self.pool.get('party.party')
account_obj = self.pool.get('account.account')
invoice_obj = self.pool.get('account.invoice')
res = {}
if value.get('party'):
party = party_obj.browse(cursor, user, value['party'],
context=context)
party = party_obj.browse(value['party'])
if value.get('amount'):
if value['amount'] > Decimal("0.0"):
account = party.account_receivable
@@ -330,24 +313,22 @@ class Line(ModelSQL, ModelView):
if value.get('invoice'):
if value.get('party'):
invoice = invoice_obj.browse(cursor, user, value['invoice'],
context=context)
invoice = invoice_obj.browse(value['invoice'])
if invoice.party != value['party']:
res['invoice'] = False
else:
res['invoice'] = False
return res
def on_change_amount(self, cursor, user, value, context=None):
def on_change_amount(self, value):
party_obj = self.pool.get('party.party')
account_obj = self.pool.get('account.account')
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)
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.
@@ -361,41 +342,34 @@ class Line(ModelSQL, ModelView):
res['account.rec_name'] = account.rec_name
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)
invoice = invoice_obj.browse(value['invoice'])
journal = journal_obj.browse(value['_parent_statement.journal'])
amount_to_pay = currency_obj.compute(invoice.currency,
invoice.amount_to_pay, journal.currency)
if abs(value['amount']) > amount_to_pay:
res['invoice'] = False
else:
res['invoice'] = False
return res
def on_change_account(self, cursor, user, value, context=None):
def on_change_account(self, value):
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)
invoice = invoice_obj.browse(value['invoice'])
if invoice.account.id != value['account']:
res['invoice'] = False
else:
res['invoice'] = False
return res
def create_move(self, cursor, user, line, context=None):
def create_move(self, line):
'''
Create move for the statement line
:param cursor: the database cursor
:param user: the user id
:param line: a BrowseRecord of the line
:param context: the contest
:return: the move id
'''
move_obj = self.pool.get('account.move')
@@ -405,95 +379,81 @@ class Line(ModelSQL, ModelView):
move_line_obj = self.pool.get('account.move.line')
lang_obj = self.pool.get('ir.lang')
if context is None:
context = {}
period_id = period_obj.find(line.statement.journal.company.id,
date=line.date)
period_id = period_obj.find(cursor, user,
line.statement.journal.company.id, date=line.date,
context=context)
move_lines = self._get_move_lines(cursor, user, line, context=context)
move_id = move_obj.create(cursor, user, {
move_lines = self._get_move_lines(line)
move_id = move_obj.create({
'name': line.date,
'period': period_id,
'journal': line.statement.journal.journal.id,
'date': line.date,
'lines': [('create', x) for x in move_lines],
}, context=context)
})
self.write(cursor, user, line.id, {
self.write(line.id, {
'move': move_id,
}, context=context)
})
if line.invoice:
amount_to_pay = currency_obj.compute(cursor, user,
line.invoice.currency, line.invoice.amount_to_pay,
line.statement.journal.currency, context=context)
amount_to_pay = currency_obj.compute(line.invoice.currency,
line.invoice.amount_to_pay, line.statement.journal.currency)
if amount_to_pay < abs(line.amount):
for code in [context.get('language', False) or 'en_US', 'en_US']:
lang_ids = lang_obj.search(cursor, user, [
for code in [Transaction().language, 'en_US']:
lang_ids = lang_obj.search([
('code', '=', code),
], context=context)
])
if lang_ids:
break
lang = lang_obj.browse(cursor, user, lang_ids[0], context=context)
lang = lang_obj.browse(lang_ids[0])
amount = lang_obj.format(lang,
'%.' + str(line.statement.journal.currency.digits) + 'f',
line.amount, True)
self.raise_user_error(cursor,
'amount_greater_invoice_amount_to_pay',
error_args=(amount,), context=context)
self.raise_user_error('amount_greater_invoice_amount_to_pay',
error_args=(amount,))
amount = currency_obj.compute(cursor, user,
line.statement.journal.currency, line.amount,
line.statement.journal.company.currency,
context=context)
amount = currency_obj.compute(line.statement.journal.currency,
line.amount, line.statement.journal.company.currency)
reconcile_lines = invoice_obj.get_reconcile_lines_for_amount(cursor,
user, line.invoice, abs(amount))
reconcile_lines = invoice_obj.get_reconcile_lines_for_amount(
line.invoice, abs(amount))
move = move_obj.browse(cursor, user, move_id, context=context)
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
invoice_obj.write(cursor, user, line.invoice.id, {
invoice_obj.write(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)
move_line_obj.reconcile(line_ids)
return move_id
def post_move(self, cursor, user, lines, context=None):
def post_move(self, lines):
move_obj = self.pool.get('account.move')
move_obj.post(cursor, user, [l.move.id for l in lines if l.move],
context=context)
move_obj.post([l.move.id for l in lines if l.move])
def delete_move(self, cursor, user, lines, context=None):
def delete_move(self, lines):
move_obj = self.pool.get('account.move')
move_obj.delete(cursor, user, [l.move.id for l in lines if l.move],
context=context)
move_obj.delete([l.move.id for l in lines if l.move])
def _get_move_lines(self, cursor, user, statement_line, context=None):
def _get_move_lines(self, statement_line):
'''
Return the values of the move lines for the statement line
:param cursor: the database cursor
:param user: the user id
:param statement_line: a BrowseRecord of the statement line
:param context: the context
:return: a list of dictionary of move line values
'''
currency_obj = self.pool.get('currency.currency')
zero = Decimal("0.0")
amount = currency_obj.compute(
cursor, user, statement_line.statement.journal.currency,
statement_line.amount,
statement_line.statement.journal.company.currency, context=context)
statement_line.statement.journal.currency, statement_line.amount,
statement_line.statement.journal.company.currency)
if statement_line.statement.journal.currency.id != \
statement_line.statement.journal.company.currency.id:
second_currency = statement_line.statement.journal.currency.id
@@ -519,12 +479,9 @@ class Line(ModelSQL, ModelView):
else:
account = journal.debit_account
if not account:
self.raise_user_error(cursor,
'debit_credit_account_statement_journal',
context=context)
self.raise_user_error('debit_credit_account_statement_journal')
if statement_line.account.id == account.id:
self.raise_user_error(cursor, 'same_debit_credit_account',
context=context)
self.raise_user_error('same_debit_credit_account')
vals.append({
'name': statement_line.date,
'debit': amount >= zero and amount or zero,