From 7e4d193ddcdddd825dc0a712f643ed7a2f19fecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 19 Aug 2010 22:02:00 +0200 Subject: [PATCH] Update for new Transaction --- INSTALL | 2 +- account.py | 1166 +++++++++++++++++++++---------------------------- fiscalyear.py | 164 +++---- journal.py | 107 +++-- move.py | 807 +++++++++++++++------------------- party.py | 53 +-- period.py | 126 +++--- tax.py | 438 ++++++++----------- 8 files changed, 1227 insertions(+), 1636 deletions(-) diff --git a/INSTALL b/INSTALL index 2b3970699e..2adf0d0829 100644 --- a/INSTALL +++ b/INSTALL @@ -4,7 +4,7 @@ Installing trytond_account Prerequisites ------------- - * Python 2.4 or later (http://www.python.org/) + * Python 2.5 or later (http://www.python.org/) * python-dateutil (http://labix.org/python-dateutil) * trytond (http://www.tryton.org/) * trytond_company (http://www.tryton.org/) diff --git a/account.py b/account.py index c79bcc0ddf..be69730e1b 100644 --- a/account.py +++ b/account.py @@ -1,15 +1,16 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -"Account" +from __future__ import with_statement +from decimal import Decimal +import datetime +import time +import os from trytond.model import ModelView, ModelSQL, fields from trytond.wizard import Wizard from trytond.report import Report from trytond.tools import reduce_ids from trytond.pyson import Equal, Eval, Not, PYSONEncoder, Date -from decimal import Decimal -import datetime -import time -import os +from trytond.transaction import Transaction class TypeTemplate(ModelSQL, ModelView): @@ -38,16 +39,16 @@ class TypeTemplate(ModelSQL, ModelView): }) self._order.insert(0, ('sequence', 'ASC')) - def default_balance_sheet(self, cursor, user, context=None): + def default_balance_sheet(self): return False - def default_income_statement(self, cursor, user, context=None): + def default_income_statement(self): return False - def default_display_balance(self, cursor, user, context=None): + def default_display_balance(self): return 'debit-credit' - def get_rec_name(self, cursor, user, ids, name, context=None): + def get_rec_name(self, ids, name): if not ids: return {} res = {} @@ -56,19 +57,15 @@ class TypeTemplate(ModelSQL, ModelView): return _name(type.parent) + '\\' + type.name else: return type.name - for type in self.browse(cursor, user, ids, context=context): + for type in self.browse(ids): res[type.id] = _name(type) return res - def _get_type_value(self, cursor, user, template, context=None, - type=None): + def _get_type_value(self, template, type=None): ''' Set the values for account creation. - :param cursor: the database cursor - :param user: the user id :param template: the BrowseRecord of the template - :param context: the context :param type: the BrowseRecord of the type to update :return: a dictionary with account fields as key and values as value ''' @@ -87,17 +84,14 @@ class TypeTemplate(ModelSQL, ModelView): res['template'] = template.id return res - def create_type(self, cursor, user, template, company_id, context=None, - template2type=None, parent_id=False): + def create_type(self, template, company_id, template2type=None, + parent_id=False): ''' Create recursively types based on template. - :param cursor: the database cursor - :param user: the user id :param template: the template id or the BrowseRecord of the template used for type creation :param company_id: the id of the company for which types are created - :param context: the context :param template2type: a dictionary with template id as key and type id as value, used to convert template id into type. The dictionary is filled with new types @@ -112,26 +106,23 @@ class TypeTemplate(ModelSQL, ModelView): template2type = {} if isinstance(template, (int, long)): - template = self.browse(cursor, user, template, context=context) + template = self.browse(template) if template.id not in template2type: - vals = self._get_type_value(cursor, user, template, context=context) + vals = self._get_type_value(template) vals['company'] = company_id vals['parent'] = parent_id - new_id = type_obj.create(cursor, user, vals, context=context) + new_id = type_obj.create(vals) prev_lang = template._context.get('language') or 'en_US' prev_data = {} for field_name, field in template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang template.setLang(lang) data = {} for field_name, field in template._columns.iteritems(): @@ -139,7 +130,8 @@ class TypeTemplate(ModelSQL, ModelView): and template[field_name] != prev_data[field_name]: data[field_name] = template[field_name] if data: - type_obj.write(cursor, user, new_id, data, context=ctx) + with Transaction().set_context(language=lang): + type_obj.write(new_id, data) template.setLang(prev_lang) template2type[template.id] = new_id else: @@ -147,8 +139,8 @@ class TypeTemplate(ModelSQL, ModelView): new_childs = [] for child in template.childs: - new_childs.append(self.create_type(cursor, user, child, company_id, - context=context, template2type=template2type, parent_id=new_id)) + new_childs.append(self.create_type(child, company_id, + template2type=template2type, parent_id=new_id)) return new_id TypeTemplate() @@ -189,60 +181,57 @@ class Type(ModelSQL, ModelView): }) self._order.insert(0, ('sequence', 'ASC')) - def default_balance_sheet(self, cursor, user, context=None): + def default_balance_sheet(self): return False - def default_income_statement(self, cursor, user, context=None): + def default_income_statement(self): return False - def default_display_balance(self, cursor, user, context=None): + def default_display_balance(self): return 'debit-credit' - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for type in self.browse(cursor, user, ids, context=context): + for type in self.browse(ids): res[type.id] = type.company.currency.digits return res - def get_amount(self, cursor, user, ids, name, context=None): + def get_amount(self, ids, name): account_obj = self.pool.get('account.account') currency_obj = self.pool.get('currency.currency') - if context is None: - context = {} res = {} for type_id in ids: res[type_id] = Decimal('0.0') - child_ids = self.search(cursor, user, [ + child_ids = self.search([ ('parent', 'child_of', ids), - ], context=context) + ]) type_sum = {} for type_id in child_ids: type_sum[type_id] = Decimal('0.0') - account_ids = account_obj.search(cursor, user, [ + account_ids = account_obj.search([ ('type', 'in', child_ids), - ], context=context) - for account in account_obj.browse(cursor, user, account_ids, - context=context): - type_sum[account.type.id] += currency_obj.round(cursor, user, + ]) + for account in account_obj.browse(account_ids): + type_sum[account.type.id] += currency_obj.round( account.company.currency, account.debit - account.credit) - types = self.browse(cursor, user, ids, context=context) + types = self.browse(ids) for type in types: - child_ids = self.search(cursor, user, [ + child_ids = self.search([ ('parent', 'child_of', [type.id]), - ], context=context) + ]) for child_id in child_ids: res[type.id] += type_sum[child_id] - res[type.id] = currency_obj.round(cursor, user, - type.company.currency, res[type.id]) + res[type.id] = currency_obj.round(type.company.currency, + res[type.id]) if type.display_balance == 'credit-debit': res[type.id] = - res[type.id] return res - def get_rec_name(self, cursor, user, ids, name, context=None): + def get_rec_name(self, ids, name): if not ids: return {} res = {} @@ -251,26 +240,23 @@ class Type(ModelSQL, ModelView): return _name(type.parent) + '\\' + type.name else: return type.name - for type in self.browse(cursor, user, ids, context=context): + for type in self.browse(ids): res[type.id] = _name(type) return res - def delete(self, cursor, user, ids, context=None): + def delete(self, ids): if isinstance(ids, (int, long)): ids = [ids] - type_ids = self.search(cursor, user, [ + type_ids = self.search([ ('parent', 'child_of', ids), - ], context=context) - return super(Type, self).delete(cursor, user, type_ids, context=context) + ]) + return super(Type, self).delete(type_ids) - def update_type(self, cursor, user, type, context=None, template2type=None): + def update_type(self, type, template2type=None): ''' Update recursively types based on template. - :param cursor: the database cursor - :param user: the user id :param type: a type id or the BrowseRecord of the type - :param context: the context :param template2type: a dictionary with template id as key and type id as value, used to convert template id into type. The dictionary is filled with new types @@ -282,25 +268,21 @@ class Type(ModelSQL, ModelView): template2type = {} if isinstance(type, (int, long)): - type = self.browse(cursor, user, type, context=context) + type = self.browse(type) if type.template: - vals = template_obj._get_type_value(cursor, user, type.template, - context=context, type=type) + vals = template_obj._get_type_value(type.template, type=type) if vals: - self.write(cursor, user, type.id, vals, context=context) + self.write(type.id, vals) prev_lang = type._context.get('language') or 'en_US' prev_data = {} for field_name, field in type.template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = type.template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang type.setLang(lang) data = {} for field_name, field in type.template._columns.iteritems(): @@ -309,13 +291,13 @@ class Type(ModelSQL, ModelView): prev_data[field_name]): data[field_name] = type.template[field_name] if data: - self.write(cursor, user, type.id, data, context=ctx) + with Transaction().set_context(language=lang): + self.write(type.id, data) type.setLang(prev_lang) template2type[type.template.id] = type.id for child in type.childs: - self.update_type(cursor, user, child, context=context, - template2type=template2type) + self.update_type(child, template2type=template2type) Type() @@ -364,43 +346,39 @@ class AccountTemplate(ModelSQL, ModelView): self._order.insert(0, ('code', 'ASC')) self._order.insert(1, ('name', 'ASC')) - def default_kind(self, cursor, user, context=None): + def default_kind(self): return 'view' - def default_reconcile(self, cursor, user, context=None): + def default_reconcile(self): return False - def default_deferral(self, cursor, user, context=None): + def default_deferral(self): return True - def get_rec_name(self, cursor, user, ids, name, context=None): + def get_rec_name(self, ids, name): if not ids: return {} res = {} - for template in self.browse(cursor, user, ids, context=context): + for template in self.browse(ids): if template.code: res[template.id] = template.code + ' - ' + template.name else: res[template.id] = template.name return res - def search_rec_name(self, cursor, user, name, clause, context=None): - ids = self.search(cursor, user, [ + def search_rec_name(self, name, clause): + ids = self.search([ ('code',) + clause[1:], - ], limit=1, context=context) + ], limit=1) if ids: return [('code',) + clause[1:]] return [(self._rec_name,) + clause[1:]] - def _get_account_value(self, cursor, user, template, context=None, - account=None): + def _get_account_value(self, template, account=None): ''' Set the values for account creation. - :param cursor: the database cursor - :param user: the user id :param template: the BrowseRecord of the template - :param context: the context :param account: the BrowseRecord of the account to update :return: a dictionary with account fields as key and values as value ''' @@ -419,18 +397,15 @@ class AccountTemplate(ModelSQL, ModelView): res['template'] = template.id return res - def create_account(self, cursor, user, template, company_id, context=None, - template2account=None, template2type=None, parent_id=False): + def create_account(self, template, company_id, template2account=None, + template2type=None, parent_id=False): ''' Create recursively accounts based on template. - :param cursor: the database cursor - :param user: the user id :param template: the template id or the BrowseRecord of template used for account creation :param company_id: the id of the company for which accounts are created - :param context: the context :param template2account: a dictionary with template id as key and account id as value, used to convert template id into account. The dictionary is filled with new accounts @@ -451,27 +426,24 @@ class AccountTemplate(ModelSQL, ModelView): template2type = {} if isinstance(template, (int, long)): - template = self.browse(cursor, user, template, context=context) + template = self.browse(template) if template.id not in template2account: - vals = self._get_account_value(cursor, user, template, context=context) + vals = self._get_account_value(template) vals['company'] = company_id vals['parent'] = parent_id vals['type'] = template2type.get(template.type.id, False) - new_id = account_obj.create(cursor, user, vals, context=context) + new_id = account_obj.create(vals) prev_lang = template._context.get('language') or 'en_US' prev_data = {} for field_name, field in template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang template.setLang(lang) data = {} for field_name, field in template._columns.iteritems(): @@ -479,7 +451,8 @@ class AccountTemplate(ModelSQL, ModelView): and template[field_name] != prev_data[field_name]: data[field_name] = template[field_name] if data: - account_obj.write(cursor, user, new_id, data, context=ctx) + with Transaction().set_context(language=lang): + account_obj.write(new_id, data) template.setLang(prev_lang) template2account[template.id] = new_id else: @@ -487,18 +460,16 @@ class AccountTemplate(ModelSQL, ModelView): new_childs = [] for child in template.childs: - new_childs.append(self.create_account(cursor, user, child, - company_id, context=context, template2account=template2account, - template2type=template2type, parent_id=new_id)) + new_childs.append(self.create_account(child, company_id, + template2account=template2account, template2type=template2type, + parent_id=new_id)) return new_id - def update_account_taxes(self, cursor, user, template, template2account, - template2tax, context=None, template_done=None): + def update_account_taxes(self, template, template2account, template2tax, + template_done=None): ''' Update recursively account taxes based on template. - :param cursor: the database cursor - :param user: the user id :param template: the template id or the BrowseRecord of template used for account creation :param template2account: a dictionary with template id as key @@ -507,7 +478,6 @@ class AccountTemplate(ModelSQL, ModelView): :param template2tax: a dictionary with tax template id as key and tax id as value, used to convert tax template id into tax. - :param context: the context :param template_done: a list of template id already updated. The list is filled. ''' @@ -521,19 +491,19 @@ class AccountTemplate(ModelSQL, ModelView): template_done = [] if isinstance(template, (int, long)): - template = self.browse(cursor, user, template, context=context) + template = self.browse(template) if template.id not in template_done: if template.taxes: - account_obj.write(cursor, user, template2account[template.id], { + account_obj.write(template2account[template.id], { 'taxes': [ ('add', template2tax[x.id]) for x in template.taxes], - }, context=context) + }) template_done.append(template.id) for child in template.childs: - self.update_account_taxes(cursor, user, child, template2account, - template2tax, context=context, template_done=template_done) + self.update_account_taxes(child, template2account, template2tax, + template_done=template_done) AccountTemplate() @@ -616,60 +586,53 @@ class Account(ModelSQL, ModelView): self._order.insert(0, ('code', 'ASC')) self._order.insert(1, ('name', 'ASC')) - def default_left(self, cursor, user, context=None): + def default_left(self): return 0 - def default_right(self, cursor, user, context=None): + def default_right(self): return 0 - def default_active(self, cursor, user, context=None): + def default_active(self): return True - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] + def default_company(self): + return Transaction().context.get('company') or False + + def default_reconcile(self): return False - def default_reconcile(self, cursor, user, context=None): - return False - - def default_deferral(self, cursor, user, context=None): + def default_deferral(self): return True - def default_kind(self, cursor, user, context=None): + def default_kind(self): return 'view' - def get_currency(self, cursor, user, ids, name, context=None): + def get_currency(self, ids, name): currency_obj = self.pool.get('currency.currency') res = {} - for account in self.browse(cursor, user, ids, context=context): + for account in self.browse(ids): res[account.id] = account.company.currency.id return res - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for account in self.browse(cursor, user, ids, context=context): + for account in self.browse(ids): res[account.id] = account.company.currency.digits return res - def get_balance(self, cursor, user, ids, name, context=None): + def get_balance(self, ids, name): res = {} company_obj = self.pool.get('company.company') currency_obj = self.pool.get('currency.currency') move_line_obj = self.pool.get('account.move.line') fiscalyear_obj = self.pool.get('account.fiscalyear') deferral_obj = self.pool.get('account.account.deferral') + cursor = Transaction().cursor - if context is None: - context = {} - - query_ids, args_ids = self.search(cursor, user, [ + query_ids, args_ids = self.search([ ('parent', 'child_of', ids), - ], context=context, query_string=True) - line_query, fiscalyear_ids = move_line_obj.query_get(cursor, user, - context=context) + ], query_string=True) + line_query, fiscalyear_ids = move_line_obj.query_get() cursor.execute('SELECT a.id, ' \ 'SUM((COALESCE(l.debit, 0) - COALESCE(l.credit, 0))) ' \ 'FROM account_account a ' \ @@ -690,9 +653,8 @@ class Account(ModelSQL, ModelView): account2company = {} id2company = {} id2account = {} - all_ids = self.search(cursor, user, [('parent', 'child_of', ids)], - context=context) - accounts = self.browse(cursor, user, all_ids, context=context) + all_ids = self.search([('parent', 'child_of', ids)]) + accounts = self.browse(all_ids) for account in accounts: account2company[account.id] = account.company.id id2company[account.company.id] = account.company @@ -700,44 +662,41 @@ class Account(ModelSQL, ModelView): for account_id in ids: res.setdefault(account_id, Decimal('0.0')) - child_ids = self.search(cursor, user, [ + child_ids = self.search([ ('parent', 'child_of', [account_id]), - ], context=context) + ]) company_id = account2company[account_id] to_currency = id2company[company_id].currency for child_id in child_ids: child_company_id = account2company[child_id] from_currency = id2company[child_company_id].currency - res[account_id] += currency_obj.compute(cursor, user, - from_currency, account_sum.get(child_id, Decimal('0.0')), - to_currency, round=True, context=context) + res[account_id] += currency_obj.compute(from_currency, + account_sum.get(child_id, Decimal('0.0')), to_currency, + round=True) youngest_fiscalyear = None - for fiscalyear in fiscalyear_obj.browse(cursor, user, fiscalyear_ids, - context=context): + for fiscalyear in fiscalyear_obj.browse(fiscalyear_ids): if not youngest_fiscalyear \ or youngest_fiscalyear.start_date > fiscalyear.start_date: youngest_fiscalyear = fiscalyear fiscalyear = None if youngest_fiscalyear: - fiscalyear_ids = fiscalyear_obj.search(cursor, user, [ + fiscalyear_ids = fiscalyear_obj.search([ ('end_date', '<=', youngest_fiscalyear.start_date), ('company', '=', youngest_fiscalyear.company), - ], order=[('end_date', 'DESC')], limit=1, context=context) + ], order=[('end_date', 'DESC')], limit=1) if fiscalyear_ids: - fiscalyear = fiscalyear_obj.browse(cursor, user, - fiscalyear_ids[0], context=context) + fiscalyear = fiscalyear_obj.browse(fiscalyear_ids[0]) if fiscalyear: if fiscalyear.state == 'close': - deferral_ids = deferral_obj.search(cursor, user, [ + deferral_ids = deferral_obj.search([ ('fiscalyear', '=', fiscalyear.id), ('account', 'in', ids), - ], context=context) + ]) id2deferral = {} - for deferral in deferral_obj.browse(cursor, user, deferral_ids, - context=context): + for deferral in deferral_obj.browse(deferral_ids): id2deferral[deferral.account.id] = deferral for account_id in ids: @@ -745,30 +704,24 @@ class Account(ModelSQL, ModelView): deferral = id2deferral[account_id] res[account_id] += deferral.debit - deferral.credit else: - ctx = context.copy() - ctx['fiscalyear'] = fiscalyear.id - if 'date' in context: - del context['date'] - res2 = self.get_balance(cursor, user, ids, name, context=ctx) + with Transaction().set_context(fiscalyear=fiscalyear.id, + date=False): + res2 = self.get_balance(ids, name) for account_id in ids: res[account_id] += res2[account_id] for account_id in ids: company_id = account2company[account_id] to_currency = id2company[company_id].currency - res[account_id] = currency_obj.round(cursor, user, to_currency, - res[account_id]) + res[account_id] = currency_obj.round(to_currency, res[account_id]) return res - def get_credit_debit(self, cursor, user, ids, names, context=None): + def get_credit_debit(self, ids, names): ''' Function to compute debit, credit for account ids. - :param cursor: the database cursor - :param user: the user id :param ids: the ids of the account :param names: the list of field name to compute - :param context: the context :return: a dictionary with all field names as key and a dictionary as value with id as key ''' @@ -778,17 +731,14 @@ class Account(ModelSQL, ModelView): currency_obj = self.pool.get('currency.currency') fiscalyear_obj = self.pool.get('account.fiscalyear') deferral_obj = self.pool.get('account.account.deferral') - - if context is None: - context = {} + cursor = Transaction().cursor for name in names: if name not in ('credit', 'debit'): raise Exception('Bad argument') res[name] = {} - line_query, fiscalyear_ids = move_line_obj.query_get(cursor, user, - context=context) + line_query, fiscalyear_ids = move_line_obj.query_get() for i in range(0, len(ids), cursor.IN_MAX): sub_ids = ids[i:i + cursor.IN_MAX] red_sql, red_ids = reduce_ids('a.id', sub_ids) @@ -814,7 +764,7 @@ class Account(ModelSQL, ModelView): account2company = {} id2company = {} - accounts = self.browse(cursor, user, ids, context=context) + accounts = self.browse(ids) for account in accounts: account2company[account.id] = account.company.id id2company[account.company.id] = account.company @@ -824,31 +774,28 @@ class Account(ModelSQL, ModelView): res[name].setdefault(account_id, Decimal('0.0')) youngest_fiscalyear = None - for fiscalyear in fiscalyear_obj.browse(cursor, user, fiscalyear_ids, - context=context): + for fiscalyear in fiscalyear_obj.browse(fiscalyear_ids): if not youngest_fiscalyear \ or youngest_fiscalyear.start_date > fiscalyear.start_date: youngest_fiscalyear = fiscalyear fiscalyear = None if youngest_fiscalyear: - fiscalyear_ids = fiscalyear_obj.search(cursor, user, [ + fiscalyear_ids = fiscalyear_obj.search([ ('end_date', '<=', youngest_fiscalyear.start_date), ('company', '=', youngest_fiscalyear.company), - ], order=[('end_date', 'DESC')], limit=1, context=context) + ], order=[('end_date', 'DESC')], limit=1) if fiscalyear_ids: - fiscalyear = fiscalyear_obj.browse(cursor, user, - fiscalyear_ids[0], context=context) + fiscalyear = fiscalyear_obj.browse(fiscalyear_ids[0]) if fiscalyear: if fiscalyear.state == 'close': - deferral_ids = deferral_obj.search(cursor, user, [ + deferral_ids = deferral_obj.search([ ('fiscalyear', '=', fiscalyear.id), ('account', 'in', ids), - ], context=context) + ]) id2deferral = {} - for deferral in deferral_obj.browse(cursor, user, deferral_ids, - context=context): + for deferral in deferral_obj.browse(deferral_ids): id2deferral[deferral.account.id] = deferral for account_id in ids: @@ -857,12 +804,9 @@ class Account(ModelSQL, ModelView): for name in names: res[name][account_id] += deferral[name] else: - ctx = context.copy() - ctx['fiscalyear'] = fiscalyear.id - if 'date' in ctx: - del ctx['date'] - res2 = self.get_credit_debit(cursor, user, ids, names, - context=ctx) + with Transaction().set_context(fiscalyear=fiscalyear.id, + date=False): + res2 = self.get_credit_debit(ids, names) for account_id in ids: for name in names: res[name][account_id] += res2[name][account_id] @@ -871,77 +815,70 @@ class Account(ModelSQL, ModelView): company_id = account2company[account_id] currency = id2company[company_id].currency for name in names: - res[name][account_id] = currency_obj.round(cursor, user, - currency, res[name][account_id]) + res[name][account_id] = currency_obj.round(currency, + res[name][account_id]) return res - def get_rec_name(self, cursor, user, ids, name, context=None): + def get_rec_name(self, ids, name): if not ids: return {} res = {} - for account in self.browse(cursor, user, ids, context=context): + for account in self.browse(ids): if account.code: res[account.id] = account.code + ' - ' + account.name else: res[account.id] = account.name return res - def search_rec_name(self, cursor, user, name, clause, context=None): - ids = self.search(cursor, user, [ + def search_rec_name(self, name, clause): + ids = self.search([ ('code',) + clause[1:], - ], limit=1, context=context) + ], limit=1) if ids: return [('code',) + clause[1:]] return [(self._rec_name,) + clause[1:]] - def copy(self, cursor, user, ids, default=None, context=None): + def copy(self, ids, default=None): if default is None: default = {} default['left'] = 0 default['right'] = 0 - res = super(Account, self).copy(cursor, user, ids, default=default, - context=context) - self._rebuild_tree(cursor, user, 'parent', False, 0) + res = super(Account, self).copy(ids, default=default) + self._rebuild_tree('parent', False, 0) return res - def write(self, cursor, user, ids, vals, context=None): + def write(self, ids, vals): if not vals.get('active', True): move_line_obj = self.pool.get('account.move.line') - account_ids = self.search(cursor, user, [ + account_ids = self.search([ ('parent', 'child_of', ids), - ], context=context) - if move_line_obj.search(cursor, user, [ + ]) + if move_line_obj.search([ ('account', 'in', account_ids), - ], context=context): + ]): vals = vals.copy() del vals['active'] - return super(Account, self).write(cursor, user, ids, vals, - context=context) + return super(Account, self).write(ids, vals) - def delete(self, cursor, user, ids, context=None): + def delete(self, ids): move_line_obj = self.pool.get('account.move.line') if isinstance(ids, (int, long)): ids = [ids] - account_ids = self.search(cursor, user, [ + account_ids = self.search([ ('parent', 'child_of', ids), - ], context=context) - if move_line_obj.search(cursor, user, [ + ]) + if move_line_obj.search([ ('account', 'in', account_ids), - ], context=context): - self.raise_user_error(cursor, - 'delete_account_containing_move_lines', context=context) - return super(Account, self).delete(cursor, user, account_ids, - context=context) + ]): + self.raise_user_error('delete_account_containing_move_lines') + return super(Account, self).delete(account_ids) - def update_account(self, cursor, user, account, context=None, - template2account=None, template2type=None): + def update_account(self, account, template2account=None, + template2type=None): ''' Update recursively accounts based on template. - :param cursor: the database cursor - :param user: the user id :param account: an account id or the BrowseRecord of the account - :param context: the context :param template2account: a dictionary with template id as key and account id as value, used to convert template id into account. The dictionary is filled with new accounts @@ -959,29 +896,26 @@ class Account(ModelSQL, ModelView): template2type = {} if isinstance(account, (int, long)): - account = self.browse(cursor, user, account, context=context) + account = self.browse(account) if account.template: - vals = template_obj._get_account_value(cursor, user, - account.template, context=context, account=account) + vals = template_obj._get_account_value(account.template, + account=account) if account.type.id != template2type.get(account.template.type.id, False): vals['type'] = template2type.get(account.template.type.id, False) if vals: - self.write(cursor, user, account.id, vals, context=context) + self.write(account.id, vals) prev_lang = account._context.get('language') or 'en_US' prev_data = {} for field_name, field in account.template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = account.template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang account.setLang(lang) data = {} for field_name, field in account.template._columns.iteritems(): @@ -990,22 +924,19 @@ class Account(ModelSQL, ModelView): prev_data[field_name]): data[field_name] = account.template[field_name] if data: - self.write(cursor, user, account.id, data, context=ctx) + with Transaction().set_context(language=lang): + self.write(account.id, data) account.setLang(prev_lang) template2account[account.template.id] = account.id for child in account.childs: - self.update_account(cursor, user, child, context=context, - template2account=template2account, + self.update_account(child, template2account=template2account, template2type=template2type) - def update_account_taxes(self, cursor, user, account, template2account, - template2tax, context=None): + def update_account_taxes(self, account, template2account, template2tax): ''' Update recursively account taxes base on template. - :param cursor: the database cursor - :param user: the user id :param account: the account id or the BrowseRecord of account :param template2account: a dictionary with template id as key and account id as value, used to convert template id into @@ -1013,7 +944,6 @@ class Account(ModelSQL, ModelView): :param template2tax: a dictionary with tax template id as key and tax id as value, used to convert tax template id into tax. - :param context: the context ''' if template2account is None: template2account = {} @@ -1022,7 +952,7 @@ class Account(ModelSQL, ModelView): template2tax = {} if isinstance(account, (int, long)): - account = self.browse(cursor, user, account, context=context) + account = self.browse(account) if account.template: if account.template.taxes: @@ -1031,17 +961,16 @@ class Account(ModelSQL, ModelView): old_tax_ids = [x.id for x in account.taxes] for tax_id in tax_ids: if tax_id not in old_tax_ids: - self.write(cursor, user, account.id, { + self.write(account.id, { 'taxes': [ ('add', template2tax[x.id]) \ for x in account.template.taxes \ if x.id in template2tax], - }, context=context) + }) break for child in account.childs: - self.update_account_taxes(cursor, user, child, template2account, - template2tax, context=context) + self.update_account_taxes(child, template2account, template2tax) Account() @@ -1076,30 +1005,30 @@ class AccountDeferral(ModelSQL, ModelView): 'write_deferral': 'You can not modify Account Deferral records', }) - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for deferral in self.browse(cursor, user, ids, context=context): + for deferral in self.browse(ids): res[deferral.id] = deferral.account.currency_digits return res - def get_rec_name(self, cursor, user, ids, name, context=None): + def get_rec_name(self, ids, name): if not ids: return {} res = {} - for deferral in self.browse(cursor, user, ids, context=context): + for deferral in self.browse(ids): res[deferral.id] = deferral.account.rec_name + ' - ' + \ deferral.fiscalyear.rec_name 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', ('account.rec_name',) + clause[1:], ('fiscalyear.rec_name',) + clause[1:], - ], context=context) + ]) return [('id', 'in', ids)] - def write(self, cursor, user, ids, vals, context=None): - self.raise_user_error(cursor, 'write_deferral', context=context) + def write(self, ids, vals): + self.raise_user_error('write_deferral') AccountDeferral() @@ -1112,7 +1041,7 @@ class OpenChartAccountInit(ModelView): help='Leave empty for all open fiscal year') posted = fields.Boolean('Posted Move', help='Show only posted move') - def default_posted(self, cursor, user, context=None): + def default_posted(self): return False OpenChartAccountInit() @@ -1141,12 +1070,11 @@ class OpenChartAccount(Wizard): }, } - def _action_open_chart(self, cursor, user, data, context=None): + def _action_open_chart(self, data): model_data_obj = self.pool.get('ir.model.data') act_window_obj = self.pool.get('ir.action.act_window') - act_window_id = model_data_obj.get_id(cursor, user, 'account', - 'act_account_tree2', context=context) - res = act_window_obj.read(cursor, user, act_window_id, context=context) + act_window_id = model_data_obj.get_id('account', 'act_account_tree2') + res = act_window_obj.read(act_window_id) res['pyson_context'] = PYSONEncoder().encode({ 'fiscalyear': data['form']['fiscalyear'], 'posted': data['form']['posted'], @@ -1177,30 +1105,24 @@ class PrintGeneralLegderInit(ModelView): empty_account = fields.Boolean('Empty Account', help='With account without move') - def default_fiscalyear(self, cursor, user, context=None): + def default_fiscalyear(self): fiscalyear_obj = self.pool.get('account.fiscalyear') - if context is None: - context = {} - fiscalyear_id = fiscalyear_obj.find(cursor, user, - context.get('company', False), exception=False, context=context) + fiscalyear_id = fiscalyear_obj.find( + Transaction().context.get('company', False), exception=False) if fiscalyear_id: return fiscalyear_id return False - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] + def default_company(self): + return Transaction().context.get('company') or False + + def default_posted(self): return False - def default_posted(self, cursor, user, context=None): + def default_empty_account(self): return False - def default_empty_account(self, cursor, user, context=None): - return False - - def on_change_fiscalyear(self, cursor, user, vals, context=None): + def on_change_fiscalyear(self, vals): return { 'start_period': False, 'end_period': False, @@ -1238,96 +1160,85 @@ PrintGeneralLegder() class GeneralLegder(Report): _name = 'account.account.general_ledger' - def parse(self, cursor, user, report, objects, datas, context): - if context is None: - context = {} - context = context.copy() + def parse(self, report, objects, datas, localcontext): account_obj = self.pool.get('account.account') period_obj = self.pool.get('account.period') company_obj = self.pool.get('company.company') - company = company_obj.browse(cursor, user, - datas['form']['company'], context=context) + company = company_obj.browse(datas['form']['company']) - account_ids = account_obj.search(cursor, user, [ + account_ids = account_obj.search([ ('company', '=', datas['form']['company']), ('kind', '!=', 'view'), - ], order=[('code', 'ASC'), ('id', 'ASC')], context=context) + ], order=[('code', 'ASC'), ('id', 'ASC')]) start_period_ids = [0] if datas['form']['start_period']: - start_period = period_obj.browse(cursor, user, - datas['form']['start_period'], context=context) - start_period_ids = period_obj.search(cursor, user, [ + start_period = period_obj.browse(datas['form']['start_period']) + start_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), ('end_date', '<=', start_period.start_date), - ], context=context) + ]) - start_context = context.copy() - start_context['fiscalyear'] = datas['form']['fiscalyear'] - start_context['periods'] = start_period_ids - start_context['posted'] = datas['form']['posted'] - start_accounts = account_obj.browse(cursor, user, account_ids, - context=start_context) + with Transaction().set_context( + fiscalyear=datas['form']['fiscalyear'], + periods=start_period_ids, + posted=datas['form']['posted']): + start_accounts = account_obj.browse(account_ids) id2start_account = {} for account in start_accounts: id2start_account[account.id] = account end_period_ids = [] if datas['form']['end_period']: - end_period = period_obj.browse(cursor, user, - datas['form']['end_period'], context=context) - end_period_ids = period_obj.search(cursor, user, [ + end_period = period_obj.browse(datas['form']['end_period']) + end_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), ('end_date', '<=', end_period.start_date), - ], context=context) + ]) if datas['form']['end_period'] not in end_period_ids: end_period_ids.append(datas['form']['end_period']) else: - end_period_ids = period_obj.search(cursor, user, [ + end_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), - ], context=context) + ]) - end_context = context.copy() - end_context['fiscalyear'] = datas['form']['fiscalyear'] - end_context['periods'] = end_period_ids - end_context['posted'] = datas['form']['posted'] - end_accounts = account_obj.browse(cursor, user, account_ids, - context=end_context) + with Transaction().set_context( + fiscalyear=datas['form']['fiscalyear'], + periods=end_period_ids, + posted=datas['form']['posted']): + end_accounts = account_obj.browse(account_ids) id2end_account = {} for account in end_accounts: id2end_account[account.id] = account - periods = period_obj.browse(cursor, user, end_period_ids, - context=context) + periods = period_obj.browse(end_period_ids) periods.sort(lambda x, y: cmp(x.start_date, y.start_date)) - context['start_period'] = periods[0] + localcontext['start_period'] = periods[0] periods.sort(lambda x, y: cmp(x.end_date, y.end_date)) - context['end_period'] = periods[-1] + localcontext['end_period'] = periods[-1] if not datas['form']['empty_account']: - account_id2lines = self.get_lines(cursor, user, account_ids, - end_period_ids, datas['form']['posted'], context) + account_id2lines = self.get_lines(account_ids, + end_period_ids, datas['form']['posted']) account_ids = account_id2lines.keys() account_id2lines = None - account_id2lines = self.lines(cursor, user, account_ids, - list(set(end_period_ids).difference( - set(start_period_ids))), datas['form']['posted'], context) + account_id2lines = self.lines(account_ids, + list(set(end_period_ids).difference(set(start_period_ids))), + datas['form']['posted']) - context['accounts'] = account_obj.browse(cursor, user, account_ids, - context=context) - context['id2start_account'] = id2start_account - context['id2end_account'] = id2end_account - context['digits'] = company.currency.digits - context['lines'] = lambda account_id: account_id2lines[account_id] - context['company'] = company + localcontext['accounts'] = account_obj.browse(account_ids) + localcontext['id2start_account'] = id2start_account + localcontext['id2end_account'] = id2end_account + localcontext['digits'] = company.currency.digits + localcontext['lines'] = lambda account_id: account_id2lines[account_id] + localcontext['company'] = company - return super(GeneralLegder, self).parse(cursor, user, report, objects, - datas, context) + return super(GeneralLegder, self).parse(report, objects, datas, + localcontext) - def get_lines(self, cursor, user, account_ids, period_ids, posted, - context): + def get_lines(self, account_ids, period_ids, posted): move_line_obj = self.pool.get('account.move.line') clause = [ ('account', 'in', account_ids), @@ -1336,23 +1247,20 @@ class GeneralLegder(Report): ] if posted: clause.append(('move.state', '=', 'posted')) - move_ids = move_line_obj.search(cursor, user, clause, - order=[], context=context) + move_ids = move_line_obj.search(clause, order=[]) res = {} - for move in move_line_obj.browse(cursor, user, move_ids, - context=context): + for move in move_line_obj.browse(move_ids): res.setdefault(move.account.id, []).append(move) return res - def lines(self, cursor, user, account_ids, period_ids, posted, context): + def lines(self, account_ids, period_ids, posted): move_line_obj = self.pool.get('account.move.line') move_obj = self.pool.get('account.move') res = dict((account_id, []) for account_id in account_ids) - account_id2lines = self.get_lines(cursor, user, account_ids, - period_ids, posted, context) + account_id2lines = self.get_lines(account_ids, period_ids, posted) - state_selections = dict(move_obj.fields_get(cursor, user, - fields_names=['state'], context=context)['state']['selection']) + state_selections = dict(move_obj.fields_get( + fields_names=['state'])['state']['selection']) for account_id, lines in account_id2lines.iteritems(): lines.sort(lambda x, y: cmp(x.date, y.date)) @@ -1396,30 +1304,24 @@ class PrintTrialBalanceInit(ModelView): empty_account = fields.Boolean('Empty Account', help='With account without move') - def default_fiscalyear(self, cursor, user, context=None): + def default_fiscalyear(self): fiscalyear_obj = self.pool.get('account.fiscalyear') - if context is None: - context = {} - fiscalyear_id = fiscalyear_obj.find(cursor, user, - context.get('company', False), exception=False, context=context) + fiscalyear_id = fiscalyear_obj.find( + Transaction().context.get('company') or False, exception=False) if fiscalyear_id: return fiscalyear_id return False - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] + def default_company(self): + return Transaction().context.get('company') or False + + def default_posted(self): return False - def default_posted(self, cursor, user, context=None): + def default_empty_account(self): return False - def default_empty_account(self, cursor, user, context=None): - return False - - def on_change_fiscalyear(self, cursor, user, vals, context=None): + def on_change_fiscalyear(self, vals): return { 'start_period': False, 'end_period': False, @@ -1457,56 +1359,49 @@ PrintTrialBalance() class TrialBalance(Report): _name = 'account.account.trial_balance' - def parse(self, cursor, user, report, objects, datas, context): - if context is None: - context = {} - context = context.copy() + def parse(self, report, objects, datas, localcontext): account_obj = self.pool.get('account.account') period_obj = self.pool.get('account.period') company_obj = self.pool.get('company.company') - company = company_obj.browse(cursor, user, - datas['form']['company'], context=context) + company = company_obj.browse(datas['form']['company']) - account_ids = account_obj.search(cursor, user, [ + account_ids = account_obj.search([ ('company', '=', datas['form']['company']), ('kind', '!=', 'view'), - ], context=context) + ]) start_period_ids = [0] if datas['form']['start_period']: - start_period = period_obj.browse(cursor, user, - datas['form']['start_period'], context=context) - start_period_ids = period_obj.search(cursor, user, [ + start_period = period_obj.browse(datas['form']['start_period']) + start_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), ('end_date', '<=', start_period.start_date), - ], context=context) + ]) end_period_ids = [] if datas['form']['end_period']: - end_period = period_obj.browse(cursor, user, - datas['form']['end_period'], context=context) - end_period_ids = period_obj.search(cursor, user, [ + end_period = period_obj.browse(datas['form']['end_period']) + end_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), ('end_date', '<=', end_period.start_date), - ], context=context) + ]) end_period_ids = list(set(end_period_ids).difference( set(start_period_ids))) if datas['form']['end_period'] not in end_period_ids: end_period_ids.append(datas['form']['end_period']) else: - end_period_ids = period_obj.search(cursor, user, [ + end_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), - ], context=context) + ]) end_period_ids = list(set(end_period_ids).difference( set(start_period_ids))) - ctx = context.copy() - ctx['fiscalyear'] = datas['form']['fiscalyear'] - ctx['periods'] = end_period_ids - ctx['posted'] = datas['form']['posted'] - accounts = account_obj.browse(cursor, user, account_ids, - context=ctx) + with Transaction().set_context( + fiscalyear=datas['form']['fiscalyear'], + periods=end_period_ids, + posted=datas['form']['posted']): + accounts = account_obj.browse(account_ids) if not datas['form']['empty_account']: to_remove = [] @@ -1517,21 +1412,19 @@ class TrialBalance(Report): for account in to_remove: accounts.remove(account) - periods = period_obj.browse(cursor, user, end_period_ids, - context=context) + periods = period_obj.browse(end_period_ids) - context['accounts'] = accounts - context['start_period'] = periods[0] - context['end_period'] = periods[-1] - context['company'] = company - context['digits'] = company.currency.digits - context['sum'] = lambda accounts, field: self.sum(cursor, user, - accounts, field, context) + localcontext['accounts'] = accounts + localcontext['start_period'] = periods[0] + localcontext['end_period'] = periods[-1] + localcontext['company'] = company + localcontext['digits'] = company.currency.digits + localcontext['sum'] = lambda accounts, field: self.sum(accounts, field) - return super(TrialBalance, self).parse(cursor, user, report, objects, - datas, context) + return super(TrialBalance, self).parse(report, objects, datas, + localcontext) - def sum(self, cursor, user, accounts, field, context): + def sum(self, accounts, field): amount = Decimal('0.0') for account in accounts: amount += account[field] @@ -1548,18 +1441,14 @@ class OpenBalanceSheetInit(ModelView): company = fields.Many2One('company.company', 'Company', required=True) posted = fields.Boolean('Posted Move', help='Show only posted move') - 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_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] - return False + def default_company(self): + return Transaction().context.get('company') or False - def default_posted(self, cursor, user, context=None): + def default_posted(self): return False OpenBalanceSheetInit() @@ -1588,29 +1477,26 @@ class OpenBalanceSheet(Wizard): }, } - def _action_open(self, cursor, user, datas, context=None): - if context is None: - context = {} + def _action_open(self, datas): model_data_obj = self.pool.get('ir.model.data') act_window_obj = self.pool.get('ir.action.act_window') company_obj = self.pool.get('company.company') lang_obj = self.pool.get('ir.lang') - company = company_obj.browse(cursor, user, datas['form']['company'], - context=context) - for code in [context.get('language', False) or 'en_US', 'en_US']: - lang_ids = lang_obj.search(cursor, user, [ + company = company_obj.browse(datas['form']['company']) + 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]) date = lang_obj.strftime(datas['form']['date'], lang.code, lang.date) - act_window_id = model_data_obj.get_id(cursor, user, 'account', - 'act_account_balance_sheet_tree', context=context) - res = act_window_obj.read(cursor, user, act_window_id, context=context) + act_window_id = model_data_obj.get_id('account', + 'act_account_balance_sheet_tree') + res = act_window_obj.read(act_window_id) res['pyson_context'] = PYSONEncoder().encode({ 'date': Date(datas['form']['date'].year, datas['form']['date'].month, @@ -1646,27 +1532,21 @@ class OpenIncomeStatementInit(ModelView): company = fields.Many2One('company.company', 'Company', required=True) posted = fields.Boolean('Posted Move', help='Show only posted move') - def default_fiscalyear(self, cursor, user, context=None): + def default_fiscalyear(self): fiscalyear_obj = self.pool.get('account.fiscalyear') - if context is None: - context = {} - fiscalyear_id = fiscalyear_obj.find(cursor, user, - context.get('company', False), exception=False, context=context) + fiscalyear_id = fiscalyear_obj.find( + Transaction().context.get('company') or False, exception=False) if fiscalyear_id: return fiscalyear_id return False - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] + def default_company(self): + return Transaction().context.get('company') or False + + def default_posted(self): return False - def default_posted(self, cursor, user, context=None): - return False - - def on_change_fiscalyear(self, cursor, user, vals, context=None): + def on_change_fiscalyear(self, vals): return { 'start_period': False, 'end_period': False, @@ -1698,44 +1578,40 @@ class OpenIncomeStatement(Wizard): }, } - def _action_open(self, cursor, user, datas, context=None): - if context is None: - context = {} + def _action_open(self, datas): model_data_obj = self.pool.get('ir.model.data') act_window_obj = self.pool.get('ir.action.act_window') period_obj = self.pool.get('account.period') start_period_ids = [0] if datas['form']['start_period']: - start_period = period_obj.browse(cursor, user, - datas['form']['start_period'], context=context) - start_period_ids = period_obj.search(cursor, user, [ + start_period = period_obj.browse(datas['form']['start_period']) + start_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), ('end_date', '<=', start_period.start_date), - ], context=context) + ]) end_period_ids = [] if datas['form']['end_period']: - end_period = period_obj.browse(cursor, user, - datas['form']['end_period'], context=context) - end_period_ids = period_obj.search(cursor, user, [ + end_period = period_obj.browse(datas['form']['end_period']) + end_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), ('end_date', '<=', end_period.start_date), - ], context=context) + ]) end_period_ids = list(set(end_period_ids).difference( set(start_period_ids))) if datas['form']['end_period'] not in end_period_ids: end_period_ids.append(datas['form']['end_period']) else: - end_period_ids = period_obj.search(cursor, user, [ + end_period_ids = period_obj.search([ ('fiscalyear', '=', datas['form']['fiscalyear']), - ], context=context) + ]) end_period_ids = list(set(end_period_ids).difference( set(start_period_ids))) - act_window_id = model_data_obj.get_id(cursor, user, 'account', - 'act_account_income_statement_tree', context=context) - res = act_window_obj.read(cursor, user, act_window_id, context=context) + act_window_id = model_data_obj.get_id('account', + 'act_account_income_statement_tree') + res = act_window_obj.read(act_window_id) res['pyson_context'] = PYSONEncoder().encode({ 'periods': end_period_ids, 'posted': datas['form']['posted'], @@ -1762,12 +1638,8 @@ class CreateChartAccountAccount(ModelView): account_template = fields.Many2One('account.account.template', 'Account Template', required=True, domain=[('parent', '=', False)]) - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] - return False + def default_company(self): + return Transaction().context.get('company') or False CreateChartAccountAccount() @@ -1839,7 +1711,7 @@ class CreateChartAccount(Wizard): }, } - def _action_create_account(self, cursor, user, datas, context=None): + def _action_create_account(self, datas): account_type_template_obj = \ self.pool.get('account.account.type.template') account_template_obj = self.pool.get('account.account.template') @@ -1849,119 +1721,115 @@ class CreateChartAccount(Wizard): tax_rule_line_template_obj = \ self.pool.get('account.tax.rule.line.template') - context=context.copy() - context['language'] = 'en_US' + with Transaction().set_context(language='en_US'): + account_template = account_template_obj.browse( + datas['form']['account_template']) - account_template = account_template_obj.browse(cursor, user, - datas['form']['account_template'], context=context) + # Create account types + template2type = {} + account_type_template_obj.create_type(account_template.type, + datas['form']['company'], template2type=template2type) - # Create account types - template2type = {} - account_type_template_obj.create_type(cursor, user, - account_template.type, datas['form']['company'], - context=context, template2type=template2type) - - # Create accounts - template2account = {} - account_template_obj.create_account(cursor, user, - account_template, datas['form']['company'], - context=context, template2account=template2account, - template2type=template2type) - - # Create tax codes - template2tax_code = {} - tax_code_template_ids = tax_code_template_obj.search(cursor, user, [ - ('account', '=', datas['form']['account_template']), - ('parent', '=', False), - ], context=context) - for tax_code_template in tax_code_template_obj.browse(cursor, user, - tax_code_template_ids, context=context): - tax_code_template_obj.create_tax_code(cursor, user, - tax_code_template, datas['form']['company'], - context=context, template2tax_code=template2tax_code) - - # Create taxes - template2tax = {} - tax_template_ids = tax_template_obj.search(cursor, user, [ - ('account', '=', datas['form']['account_template']), - ('parent', '=', False), - ], context=context) - for tax_template in tax_template_obj.browse(cursor, user, - tax_template_ids, context=context): - tax_template_obj.create_tax(cursor, user, tax_template, + # Create accounts + template2account = {} + account_template_obj.create_account(account_template, datas['form']['company'], - template2tax_code=template2tax_code, template2account=template2account, - context=context, template2tax=template2tax) + template2type=template2type) - # Update taxes on accounts - account_template_obj.update_account_taxes(cursor, user, - account_template, template2account, template2tax, context=context) + # Create tax codes + template2tax_code = {} + tax_code_template_ids = tax_code_template_obj.search([ + ('account', '=', datas['form']['account_template']), + ('parent', '=', False), + ]) + for tax_code_template in tax_code_template_obj.browse( + tax_code_template_ids): + tax_code_template_obj.create_tax_code(tax_code_template, + datas['form']['company'], + template2tax_code=template2tax_code) - # Create tax rules - template2rule = {} - tax_rule_template_ids = tax_rule_template_obj.search(cursor, user, [ - ('account', '=', datas['form']['account_template']), - ], context=context) - for tax_rule_template in tax_rule_template_obj.browse(cursor, user, - tax_rule_template_ids, context=context): - tax_rule_template_obj.create_rule(cursor, user, tax_rule_template, - datas['form']['company'], context=context, - template2rule=template2rule) + # Create taxes + template2tax = {} + tax_template_ids = tax_template_obj.search([ + ('account', '=', datas['form']['account_template']), + ('parent', '=', False), + ]) + for tax_template in tax_template_obj.browse(tax_template_ids): + tax_template_obj.create_tax(tax_template, + datas['form']['company'], + template2tax_code=template2tax_code, + template2account=template2account, + template2tax=template2tax) - # Create tax rule lines - template2rule_line = {} - tax_rule_line_template_ids = tax_rule_line_template_obj.search(cursor, - user, [ - ('rule.account', '=', datas['form']['account_template']), - ], context=context) - for tax_rule_line_template in tax_rule_line_template_obj.browse(cursor, - user, tax_rule_line_template_ids, context=context): - tax_rule_line_template_obj.create_rule_line(cursor, user, - tax_rule_line_template, template2tax, template2rule, - context=context, template2rule_line=template2rule_line) + # Update taxes on accounts + account_template_obj.update_account_taxes(account_template, + template2account, template2tax) + + # Create tax rules + template2rule = {} + tax_rule_template_ids = tax_rule_template_obj.search([ + ('account', '=', datas['form']['account_template']), + ]) + for tax_rule_template in tax_rule_template_obj.browse( + tax_rule_template_ids): + tax_rule_template_obj.create_rule(tax_rule_template, + datas['form']['company'], template2rule=template2rule) + + # Create tax rule lines + template2rule_line = {} + tax_rule_line_template_ids = tax_rule_line_template_obj.search([ + ('rule.account', '=', datas['form']['account_template']), + ]) + for tax_rule_line_template in tax_rule_line_template_obj.browse( + tax_rule_line_template_ids): + tax_rule_line_template_obj.create_rule_line( + tax_rule_line_template, template2tax, template2rule, + template2rule_line=template2rule_line) return {'company': datas['form']['company']} - def _action_create_properties(self, cursor, user, datas, context=None): + def _action_create_properties(self, datas): property_obj = self.pool.get('ir.property') model_field_obj = self.pool.get('ir.model.field') - account_receivable_field_id = model_field_obj.search(cursor, user, [ + account_receivable_field_id = model_field_obj.search([ ('model.model', '=', 'party.party'), ('name', '=', 'account_receivable'), - ], limit=1, context=context)[0] - property_ids = property_obj.search(cursor, user, [ + ], limit=1)[0] + property_ids = property_obj.search([ ('field', '=', account_receivable_field_id), ('res', '=', False), ('company', '=', datas['form']['company']), - ], context=context) - property_obj.delete(cursor, 0, property_ids, context=context) - property_obj.create(cursor, 0, { - 'name': 'account_receivable', - 'field': account_receivable_field_id, - 'value': 'account.account,' + \ - str(datas['form']['account_receivable']), - 'company': datas['form']['company'], - }, context=context) + ]) + with Transaction().set_user(0): + property_obj.delete(property_ids) + property_obj.create({ + 'name': 'account_receivable', + 'field': account_receivable_field_id, + 'value': 'account.account,' + \ + str(datas['form']['account_receivable']), + 'company': datas['form']['company'], + }) - account_payable_field_id = model_field_obj.search(cursor, user, [ + account_payable_field_id = model_field_obj.search([ ('model.model', '=', 'party.party'), ('name', '=', 'account_payable'), - ], limit=1, context=context)[0] - property_ids = property_obj.search(cursor, user, [ + ], limit=1)[0] + property_ids = property_obj.search([ ('field', '=', account_payable_field_id), ('res', '=', False), ('company', '=', datas['form']['company']), - ], context=context) - property_obj.delete(cursor, 0, property_ids, context=context) - property_obj.create(cursor, 0, { - 'name': 'account_payable', - 'field': account_payable_field_id, - 'value': 'account.account,' + \ - str(datas['form']['account_payable']), - 'company': datas['form']['company'], - }, context=context) + ]) + with Transaction().set_user(0): + property_obj.delete(property_ids) + property_obj.create({ + 'name': 'account_payable', + 'field': account_payable_field_id, + 'value': 'account.account,' + \ + str(datas['form']['account_payable']), + 'company': datas['form']['company'], + }) return {} CreateChartAccount() @@ -2011,7 +1879,7 @@ class UpdateChartAccount(Wizard): }, } - def _action_update_account(self, cursor, user, datas, context=None): + def _action_update_account(self, datas): account_type_obj = self.pool.get('account.account.type') account_type_template_obj = \ self.pool.get('account.account.type.template') @@ -2027,122 +1895,108 @@ class UpdateChartAccount(Wizard): tax_rule_line_template_obj = \ self.pool.get('account.tax.rule.line.template') - account = account_obj.browse(cursor, user, datas['form']['account'], - context=context) + account = account_obj.browse(datas['form']['account']) # Update account types template2type = {} - account_type_obj.update_type(cursor, user, account.type, - context=context, template2type=template2type) + account_type_obj.update_type(account.type, template2type=template2type) # Create missing account types if account.type.template: - account_type_template_obj.create_type(cursor, user, - account.type.template, account.company.id, context=context, - template2type=template2type) + account_type_template_obj.create_type(account.type.template, + account.company.id, template2type=template2type) # Update accounts template2account = {} - account_obj.update_account(cursor, user, account, context=context, - template2account=template2account, template2type=template2type) + account_obj.update_account(account, template2account=template2account, + template2type=template2type) # Create missing accounts if account.template: - account_template_obj.create_account(cursor, user, account.template, - account.company.id, context=context, - template2account=template2account, + account_template_obj.create_account(account.template, + account.company.id, template2account=template2account, template2type=template2type) # Update tax codes template2tax_code = {} - tax_code_ids = tax_code_obj.search(cursor, user, [ + tax_code_ids = tax_code_obj.search([ ('company', '=', account.company.id), ('parent', '=', False), - ], context=context) - for tax_code in tax_code_obj.browse(cursor, user, tax_code_ids, - context=context): - tax_code_obj.update_tax_code(cursor, user, tax_code, context=context, + ]) + for tax_code in tax_code_obj.browse(tax_code_ids): + tax_code_obj.update_tax_code(tax_code, template2tax_code=template2tax_code) # Create missing tax codes if account.template: - tax_code_template_ids = tax_code_template_obj.search(cursor, user, [ + tax_code_template_ids = tax_code_template_obj.search([ ('account', '=', account.template.id), ('parent', '=', False), - ], context=context) - for tax_code_template in tax_code_template_obj.browse(cursor, user, - tax_code_template_ids, context=context): - tax_code_template_obj.create_tax_code(cursor, user, - tax_code_template, account.company.id, context=context, + ]) + for tax_code_template in tax_code_template_obj.browse( + tax_code_template_ids): + tax_code_template_obj.create_tax_code(tax_code_template, + account.company.id, template2tax_code=template2tax_code) # Update taxes template2tax = {} - tax_ids = tax_obj.search(cursor, user, [ + tax_ids = tax_obj.search([ ('company', '=', account.company.id), ('parent', '=', False), - ], context=context) - for tax in tax_obj.browse(cursor, user, tax_ids, context=context): - tax_obj.update_tax(cursor, user, tax, - template2tax_code=template2tax_code, + ]) + for tax in tax_obj.browse(tax_ids): + tax_obj.update_tax(tax, template2tax_code=template2tax_code, template2account=template2account, - context=context, template2tax=template2tax) # Create missing taxes if account.template: - tax_template_ids = tax_template_obj.search(cursor, user, [ + tax_template_ids = tax_template_obj.search([ ('account', '=', account.template.id), ('parent', '=', False), - ], context=context) - for tax_template in tax_template_obj.browse(cursor, user, - tax_template_ids, context=context): - tax_template_obj.create_tax(cursor, user, tax_template, - account.company.id, template2tax_code=template2tax_code, - template2account=template2account, context=context, + ]) + for tax_template in tax_template_obj.browse(tax_template_ids): + tax_template_obj.create_tax(tax_template, account.company.id, + template2tax_code=template2tax_code, + template2account=template2account, template2tax=template2tax) # Update taxes on accounts - account_obj.update_account_taxes(cursor, user, account, - template2account, template2tax, context=context) + account_obj.update_account_taxes(account, template2account, + template2tax) # Update tax rules template2rule = {} - tax_rule_ids = tax_rule_obj.search(cursor, user, [ + tax_rule_ids = tax_rule_obj.search([ ('company', '=', account.company.id), - ], context=context) - for tax_rule in tax_rule_obj.browse(cursor, user, tax_rule_ids, - context=context): - tax_rule_obj.update_rule(cursor, user, tax_rule, context=context, - template2rule=template2rule) + ]) + for tax_rule in tax_rule_obj.browse(tax_rule_ids): + tax_rule_obj.update_rule(tax_rule, template2rule=template2rule) # Create missing tax rules if account.template: - tax_rule_template_ids = tax_rule_template_obj.search(cursor, user, [ + tax_rule_template_ids = tax_rule_template_obj.search([ ('account', '=', account.template.id), - ], context=context) - for tax_rule_template in tax_rule_template_obj.browse(cursor, user, - tax_rule_template_ids, context=context): - tax_rule_template_obj.create_rule(cursor, user, - tax_rule_template, account.company.id, context=context, - template2rule=template2rule) + ]) + for tax_rule_template in tax_rule_template_obj.browse( + tax_rule_template_ids): + tax_rule_template_obj.create_rule(tax_rule_template, + account.company.id, template2rule=template2rule) # Update tax rule lines template2rule_line = {} - tax_rule_line_ids = tax_rule_line_obj.search(cursor, user, [ + tax_rule_line_ids = tax_rule_line_obj.search([ ('rule.company', '=', account.company.id), - ], context=context) - for tax_rule_line in tax_rule_line_obj.browse(cursor, user, - tax_rule_line_ids, context=context): - tax_rule_line_obj.update_rule_line(cursor, user, tax_rule_line, - template2tax, template2rule, context=context, - template2rule_line=template2rule_line) + ]) + for tax_rule_line in tax_rule_line_obj.browse(tax_rule_line_ids): + tax_rule_line_obj.update_rule_line(tax_rule_line, template2tax, + template2rule, template2rule_line=template2rule_line) # Create missing tax rule lines if account.template: - tax_rule_line_template_ids = tax_rule_line_template_obj.search( - cursor, user, [ - ('rule.account', '=', account.template.id), - ], context=context) + tax_rule_line_template_ids = tax_rule_line_template_obj.search([ + ('rule.account', '=', account.template.id), + ]) for tax_rule_line_template in tax_rule_line_template_obj.browse( - cursor, user, tax_rule_line_template_ids, context=context): - tax_rule_line_template_obj.create_rule_line(cursor, user, + tax_rule_line_template_ids): + tax_rule_line_template_obj.create_rule_line( tax_rule_line_template, template2tax, template2rule, - context=context, template2rule_line=template2rule_line) + template2rule_line=template2rule_line) return {} UpdateChartAccount() @@ -2158,25 +2012,19 @@ class OpenThirdPartyBalanceInit(ModelView): posted = fields.Boolean('Posted Move', help='Show only posted move') - def default_fiscalyear(self, cursor, user, context=None): + def default_fiscalyear(self): fiscalyear_obj = self.pool.get('account.fiscalyear') - if context is None: - context = {} - fiscalyear_id = fiscalyear_obj.find(cursor, user, - context.get('company', False), exception=False, context=context) + fiscalyear_id = fiscalyear_obj.find( + Transaction().context.get('company') or False, exception=False) if fiscalyear_id: return fiscalyear_id return False - def default_posted(self, cursor, user, context=None): + def default_posted(self): return False - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] - return False + def default_company(self): + return Transaction().context.get('company') or False OpenThirdPartyBalanceInit() @@ -2210,19 +2058,18 @@ OpenThirdPartyBalance() class ThirdPartyBalance(Report): _name = 'account.account.third_party_balance' - def parse(self, cursor, user, report, objects, datas, context): + def parse(self, report, objects, datas, localcontext): party_obj = self.pool.get('party.party') move_line_obj = self.pool.get('account.move.line') company_obj = self.pool.get('company.company') date_obj = self.pool.get('ir.date') - context = context.copy() + cursor = Transaction().cursor - company = company_obj.browse(cursor, user, - datas['form']['company'], context=context) - context['company'] = company - context['digits'] = company.currency.digits - context['fiscalyear'] = datas['form']['fiscalyear'] - line_query, _ = move_line_obj.query_get(cursor, user, context=context) + company = company_obj.browse(datas['form']['company']) + localcontext['company'] = company + localcontext['digits'] = company.currency.digits + localcontext['fiscalyear'] = datas['form']['fiscalyear'] + line_query, _ = move_line_obj.query_get() if datas['form']['posted']: posted_clause = "AND m.state = 'posted' " else: @@ -2243,13 +2090,11 @@ class ThirdPartyBalance(Report): + posted_clause + \ 'GROUP BY l.party ' \ 'HAVING (SUM(l.debit) != 0 OR SUM(l.credit) != 0)', - (datas['form']['company'], date_obj.today(cursor, user, - context=context))) + (datas['form']['company'], date_obj.today())) res = cursor.fetchall() id2party = {} - for party in party_obj.browse(cursor, user, [x[0] for x in res], - context=context): + for party in party_obj.browse([x[0] for x in res]): id2party[party.id] = party objects = [{ 'name': id2party[x[0]].rec_name, @@ -2258,12 +2103,12 @@ class ThirdPartyBalance(Report): 'solde': x[1] - x[2], } for x in res] objects.sort(lambda x, y: cmp(x['name'], y['name'])) - context['total_debit'] = sum((x['debit'] for x in objects)) - context['total_credit'] = sum((x['credit'] for x in objects)) - context['total_solde'] = sum((x['solde'] for x in objects)) + localcontext['total_debit'] = sum((x['debit'] for x in objects)) + localcontext['total_credit'] = sum((x['credit'] for x in objects)) + localcontext['total_solde'] = sum((x['solde'] for x in objects)) - return super(ThirdPartyBalance, self).parse(cursor, user, report, - objects, datas, context) + return super(ThirdPartyBalance, self).parse(report, objects, datas, + localcontext) ThirdPartyBalance() @@ -2285,40 +2130,34 @@ class OpenAgedBalanceInit(ModelView): [('day', 'Day'), ('month', 'Month')], "Unit", required=True) posted = fields.Boolean('Posted Move', help='Show only posted move') - def default_fiscalyear(self, cursor, user, context=None): + def default_fiscalyear(self): fiscalyear_obj = self.pool.get('account.fiscalyear') - if context is None: - context = {} - fiscalyear_id = fiscalyear_obj.find(cursor, user, - context.get('company', False), exception=False, context=context) + fiscalyear_id = fiscalyear_obj.find( + Transaction().context.get('company') or False, exception=False) if fiscalyear_id: return fiscalyear_id return False - def default_balance_type(self, cursor, user, context=None): + def default_balance_type(self): return "customer" - def default_posted(self, cursor, user, context=None): + def default_posted(self): return False - def default_term1(self, cursor, user, context=None): + def default_term1(self): return 30 - def default_term2(self, cursor, user, context=None): + def default_term2(self): return 60 - def default_term3(self, cursor, user, context=None): + def default_term3(self): return 90 - def default_unit(self, cursor, user, context=None): + def default_unit(self): return 'day' - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] - return False + def default_company(self): + return Transaction().context.get('company') or False OpenAgedBalanceInit() @@ -2354,11 +2193,11 @@ class OpenAgedBalance(Wizard): 'warning': 'Warning', 'term_overlap_desc': 'You cannot define overlapping terms'}) - def check(self, cursor, user, datas, context=None): + def check(self, datas): if not (datas['form']['term1'] < datas['form']['term2'] \ < datas['form']['term3']): - self.raise_user_error(cursor, error="warning", - error_description="term_overlap_desc") + self.raise_user_error(error="warning", + error_description="term_overlap_desc") return datas['form'] OpenAgedBalance() @@ -2367,18 +2206,18 @@ OpenAgedBalance() class AgedBalance(Report): _name = 'account.account.aged_balance' - def parse(self, cursor, user, report, objects, datas, context): + def parse(self, report, objects, datas, localcontext): party_obj = self.pool.get('party.party') move_line_obj = self.pool.get('account.move.line') company_obj = self.pool.get('company.company') date_obj = self.pool.get('ir.date') + cursor = Transaction().cursor - company = company_obj.browse(cursor, user, - datas['form']['company'], context=context) - context['digits'] = company.currency.digits - context['fiscalyear'] = datas['form']['fiscalyear'] - context['posted'] = datas['form']['posted'] - line_query, _ = move_line_obj.query_get(cursor, user, context=context) + company = company_obj.browse(datas['form']['company']) + localcontext['digits'] = company.currency.digits + localcontext['fiscalyear'] = datas['form']['fiscalyear'] + localcontext['posted'] = datas['form']['posted'] + line_query, _ = move_line_obj.query_get() terms = (datas['form']['term1'], datas['form']['term2'], @@ -2398,16 +2237,15 @@ class AgedBalance(Report): if position == 0: term_query = '(l.maturity_date <= %s '\ 'OR l.maturity_date IS NULL) ' - term_args = (date_obj.today(cursor, user, context=context) + \ - datetime.timedelta(days=term*coef),) + term_args = (date_obj.today() + + datetime.timedelta(days=term*coef),) else: term_query = '(l.maturity_date <= %s '\ 'AND l.maturity_date >= %s) ' term_args = ( - date_obj.today(cursor, user, context=context) + \ - datetime.timedelta(days=term*coef), - date_obj.today(cursor, user, context=context) + \ - datetime.timedelta(days=terms[position-1]*coef), + date_obj.today() + datetime.timedelta(days=term*coef), + date_obj.today() + + datetime.timedelta(days=terms[position-1]*coef), ) cursor.execute('SELECT l.party, SUM(l.debit) - SUM(l.credit) ' \ @@ -2430,26 +2268,26 @@ class AgedBalance(Report): else: res[party] = [(i[0] == position) and solde \ or Decimal("0.0") for i in enumerate(terms)] - party_ids = party_obj.search(cursor, user, [ + party_ids = party_obj.search([ ('id', 'in', [k for k in res.iterkeys()]), - ], context=context) - parties = party_obj.browse(cursor, user, party_ids, context=context) + ]) + parties = party_obj.browse(party_ids) - context['main_title'] = datas['form']['balance_type'] - context['unit'] = datas['form']['unit'] + localcontext['main_title'] = datas['form']['balance_type'] + localcontext['unit'] = datas['form']['unit'] for i in range(3): - context['total' + str(i)] = sum((v[i] for v in res.itervalues())) - context['term' + str(i)] = terms[i] + localcontext['total' + str(i)] = sum((v[i] for v in res.itervalues())) + localcontext['term' + str(i)] = terms[i] - context['company'] = company - context['parties']= [{ + localcontext['company'] = company + localcontext['parties']= [{ 'name': p.rec_name, 'amount0': res[p.id][0], 'amount1': res[p.id][1], 'amount2': res[p.id][2], } for p in parties] - return super(AgedBalance, self).parse(cursor, user, report, objects, - datas, context) + return super(AgedBalance, self).parse(report, objects, datas, + localcontext) AgedBalance() diff --git a/fiscalyear.py b/fiscalyear.py index 1384cb4180..60c8ac8000 100644 --- a/fiscalyear.py +++ b/fiscalyear.py @@ -1,13 +1,13 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -'Fiscal Year' +import datetime +import time +from dateutil.relativedelta import relativedelta from trytond.model import ModelView, ModelSQL, fields from trytond.wizard import Wizard from trytond.tools import datetime_strftime from trytond.pyson import Equal, Eval, If, In, Get -import datetime -import time -from dateutil.relativedelta import relativedelta +from trytond.transaction import Transaction STATES = { 'readonly': Equal(Eval('state'), 'close'), @@ -78,18 +78,15 @@ class FiscalYear(ModelSQL, ModelView): 'there is more recent fiscal year closed!', }) - def default_state(self, cursor, user, context=None): + def default_state(self): return 'open' - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] - return False + def default_company(self): + return Transaction().context.get('company') or False - def check_dates(self, cursor, user, ids): - for fiscalyear in self.browse(cursor, user, ids): + def check_dates(self, ids): + cursor = Transaction().cursor + for fiscalyear in self.browse(ids): cursor.execute('SELECT id ' \ 'FROM ' + self._table + ' ' \ 'WHERE ((start_date <= %s AND end_date >= %s) ' \ @@ -105,49 +102,46 @@ class FiscalYear(ModelSQL, ModelView): return False return True - def check_post_move_sequence(self, cursor, user, ids): - for fiscalyear in self.browse(cursor, user, ids): - if self.search(cursor, user, [ + def check_post_move_sequence(self, ids): + for fiscalyear in self.browse(ids): + if self.search([ ('post_move_sequence', '=', fiscalyear.post_move_sequence.id), ('id', '!=', fiscalyear.id), ]): return False return True - def write(self, cursor, user, ids, vals, context=None): + def write(self, ids, vals): move_obj = self.pool.get('account.move') if vals.get('post_move_sequence'): - for fiscalyear in self.browse(cursor, user, ids, context=context): + for fiscalyear in self.browse(ids): if fiscalyear.post_move_sequence and \ fiscalyear.post_move_sequence.id != \ vals['post_move_sequence']: - self.raise_user_error(cursor, 'change_post_move_sequence', - context=context) + self.raise_user_error('change_post_move_sequence') vals = vals.copy() if 'periods' in vals: operator = ['delete', 'unlink_all', 'unlink', 'create', 'write', 'add', 'set'] vals['periods'].sort( lambda x, y: cmp(operator.index(x[0]), operator.index(y[0]))) - return super(FiscalYear, self).write(cursor, user, ids, vals, - context=context) + return super(FiscalYear, self).write(ids, vals) - def delete(self, cursor, user, ids, context=None): + def delete(self, ids): period_obj = self.pool.get('account.period') period_ids = [] - for fiscalyear in self.browse(cursor, user, ids, context=context): + for fiscalyear in self.browse(ids): period_ids.extend([x.id for x in fiscalyear.periods]) - period_obj.delete(cursor, user, period_ids, context=context) - return super(FiscalYear, self).delete(cursor, user, ids, - context=context) + period_obj.delete(period_ids) + return super(FiscalYear, self).delete(ids) - def create_period(self, cursor, user, ids, context=None, interval=1): + def create_period(self, ids, interval=1): ''' Create periods for the fiscal years with month interval ''' period_obj = self.pool.get('account.period') - for fiscalyear in self.browse(cursor, user, ids, context=context): + for fiscalyear in self.browse(ids): period_start_date = fiscalyear.start_date while period_start_date < fiscalyear.end_date: period_end_date = period_start_date + \ @@ -158,26 +152,24 @@ class FiscalYear(ModelSQL, ModelView): name = datetime_strftime(period_start_date, '%Y-%m') if name != datetime_strftime(period_end_date, '%Y-%m'): name += ' - ' + datetime_strftime(period_end_date, '%Y-%m') - period_obj.create(cursor, user, { + period_obj.create({ 'name': name, 'start_date': period_start_date, 'end_date': period_end_date, 'fiscalyear': fiscalyear.id, 'post_move_sequence': fiscalyear.post_move_sequence.id, 'type': 'standard', - }, context=context) + }) period_start_date = period_end_date + relativedelta(days=1) return True - def create_period_3(self, cursor, user, ids, context=None): + def create_period_3(self, ids): ''' Create periods for the fiscal years with 3 months interval ''' - return self.create_period(cursor, user, ids, context=context, - interval=3) + return self.create_period(ids, interval=3) - def find(self, cursor, user, company_id, date=None, exception=True, - context=None): + def find(self, company_id, date=None, exception=True): ''' Return the fiscal year for the company_id at the date or the current date. @@ -187,29 +179,25 @@ class FiscalYear(ModelSQL, ModelView): date_obj = self.pool.get('ir.date') if not date: - date = date_obj.today(cursor, user, context=context) - ids = self.search(cursor, user, [ + date = date_obj.today() + ids = self.search([ ('start_date', '<=', date), ('end_date', '>=', date), ('company', '=', company_id), - ], order=[('start_date', 'DESC')], limit=1, context=context) + ], order=[('start_date', 'DESC')], limit=1) if not ids: if exception: - self.raise_user_error(cursor, 'no_fiscalyear_date', - context=context) + self.raise_user_error('no_fiscalyear_date') else: return False return ids[0] - def _process_account(self, cursor, user, account, fiscalyear, context=None): + def _process_account(self, account, fiscalyear): ''' Process account for a fiscal year closed - :param cursor: the database cursor - :param user: the user id :param account: a BrowseRecord of the account :param fiscalyear: a BrowseRecord of the fiscal year closed - :param context: the context ''' currency_obj = self.pool.get('currency.currency') deferral_obj = self.pool.get('account.account.deferral') @@ -217,99 +205,86 @@ class FiscalYear(ModelSQL, ModelView): if account.kind == 'view': return if not account.deferral: - if not currency_obj.is_zero(cursor, user, - fiscalyear.company.currency, account.balance): - self.raise_user_error(cursor, 'account_balance_not_zero', - error_args=(account.rec_name,), context=context) + if not currency_obj.is_zero(fiscalyear.company.currency, + account.balance): + self.raise_user_error('account_balance_not_zero', + error_args=(account.rec_name,)) else: - deferral_obj.create(cursor, user, { + deferral_obj.create({ 'account': account.id, 'fiscalyear': fiscalyear.id, 'debit': account.debit, 'credit': account.credit, - }, context=context) + }) - def close(self, cursor, user, fiscalyear_id, context=None): + def close(self, fiscalyear_id): ''' Close a fiscal year - :param cursor: the database cursor - :param user: the user id :param fiscalyear_id: the fiscal year id - :param context: the context ''' period_obj = self.pool.get('account.period') account_obj = self.pool.get('account.account') - if context is None: - context = {} - if isinstance(fiscalyear_id, list): fiscalyear_id = fiscalyear_id[0] - fiscalyear = self.browse(cursor, user, fiscalyear_id, context=context) + fiscalyear = self.browse(fiscalyear_id) - if self.search(cursor, user, [ + if self.search([ ('end_date', '<=', fiscalyear.start_date), ('state', '=', 'open'), ('company', '=', fiscalyear.company.id), - ], context=context): - self.raise_user_error(cursor, 'close_error', context=context) + ]): + self.raise_user_error('close_error') #First close the fiscalyear to be sure #it will not have new period created between. - self.write(cursor, user, fiscalyear_id, { + self.write(fiscalyear_id, { 'state': 'close', - }, context=context) - period_ids = period_obj.search(cursor, user, [ + }) + period_ids = period_obj.search([ ('fiscalyear', '=', fiscalyear_id), - ], context=context) - period_obj.close(cursor, user, period_ids, context=context) + ]) + period_obj.close(period_ids) - ctx = context.copy() - ctx['fiscalyear'] = fiscalyear_id - if 'date' in context: - del context['date'] + with Transaction().set_context(fiscalyear=fiscalyear_id, + date=False): + account_ids = account_obj.search([ + ('company', '=', fiscalyear.company.id), + ]) + accounts = account_obj.browse(account_ids) + for account in accounts: + self._process_account(account, fiscalyear) - account_ids = account_obj.search(cursor, user, [ - ('company', '=', fiscalyear.company.id), - ], context=ctx) - for account in account_obj.browse(cursor, user, account_ids, - context=ctx): - self._process_account(cursor, user, account, fiscalyear, - context=context) - - def reopen(self, cursor, user, fiscalyear_id, context=None): + def reopen(self, fiscalyear_id): ''' Re-open a fiscal year - :param cursor: the database cursor - :param user: the user id :param fiscalyear_id: the fiscal year id - :param context: the context ''' deferral_obj = self.pool.get('account.account.deferral') if isinstance(fiscalyear_id, list): fiscalyear_id = fiscalyear_id[0] - fiscalyear = self.browse(cursor, user, fiscalyear_id, context=context) + fiscalyear = self.browse(fiscalyear_id) - if self.search(cursor, user, [ + if self.search([ ('start_date', '>=', fiscalyear.end_date), ('state', '=', 'close'), ('company', '=', fiscalyear.company.id), - ], context=context): - self.raise_user_error(cursor, 'reopen_error', context=context) + ]): + self.raise_user_error('reopen_error') - deferral_ids = deferral_obj.search(cursor, user, [ + deferral_ids = deferral_obj.search([ ('fiscalyear', '=', fiscalyear_id), - ], context=context) - deferral_obj.delete(cursor, user, deferral_ids, context=context) + ]) + deferral_obj.delete(deferral_ids) - self.write(cursor, user, fiscalyear_id, { + self.write(fiscalyear_id, { 'state': 'open', - }, context=context) + }) FiscalYear() @@ -348,11 +323,10 @@ class CloseFiscalYear(Wizard): }, } - def _close(self, cursor, user, data, context=None): + def _close(self, data): fiscalyear_obj = self.pool.get('account.fiscalyear') - fiscalyear_obj.close(cursor, user, data['form']['close_fiscalyear'], - context=context) + fiscalyear_obj.close(data['form']['close_fiscalyear']) return {} CloseFiscalYear() diff --git a/journal.py b/journal.py index d9ee94fa5d..be04719ea5 100644 --- a/journal.py +++ b/journal.py @@ -1,10 +1,11 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -"Journal" +from __future__ import with_statement from trytond.model import ModelView, ModelSQL, fields from trytond.wizard import Wizard from trytond.backend import TableHandler from trytond.pyson import Equal, Eval, Not, Get, Or, And, Bool +from trytond.transaction import Transaction STATES = { 'readonly': Equal(Eval('state'), 'close'), @@ -64,10 +65,10 @@ class Column(ModelSQL, ModelView): super(Column, self).__init__() self._order.insert(0, ('sequence', 'ASC')) - def default_required(self, cursor, user, context=None): + def default_required(self): return False - def default_readonly(self, cursor, user, context=None): + def default_readonly(self): return False Column() @@ -118,42 +119,44 @@ class Journal(ModelSQL, ModelView): super(Journal, self).__init__() self._order.insert(0, ('name', 'ASC')) - def init(self, cursor, module_name): - super(Journal, self).init(cursor, module_name) + def init(self, module_name): + super(Journal, self).init(module_name) + cursor = Transaction().cursor table = TableHandler(cursor, self, module_name) # Migration from 1.0 sequence Many2One change into Property if table.column_exist('sequence'): property_obj = self.pool.get('ir.property') cursor.execute('SELECT id, sequence FROM "' + self._table +'"') - for journal_id, sequence_id in cursor.fetchall(): - property_obj.set(cursor, 0, 'sequence', self._name, - journal_id, (sequence_id and \ + with Transaction().set_user(0): + for journal_id, sequence_id in cursor.fetchall(): + property_obj.set('sequence', self._name, + journal_id, (sequence_id and 'ir.sequence,' + str(sequence_id) or False)) table.drop_column('sequence', exception=True) - def default_active(self, cursor, user, context=None): + def default_active(self): return True - def default_centralised(self, cursor, user, context=None): + def default_centralised(self): return False - def default_update_posted(self, cursor, user, context=None): + def default_update_posted(self): return False - def default_sequence(self, cursor, user, context=None): + def default_sequence(self): return False - def get_types(self, cursor, user, context=None): + def get_types(self): type_obj = self.pool.get('account.journal.type') - type_ids = type_obj.search(cursor, user, [], context=context) - types = type_obj.browse(cursor, user, type_ids, context=context) + type_ids = type_obj.search([]) + types = type_obj.browse(type_ids) return [(x.code, x.name) for x in types] - def search_rec_name(self, cursor, user, name, clause, context=None): - ids = self.search(cursor, user, [ + def search_rec_name(self, name, clause): + ids = self.search([ ('code',) + clause[1:], - ], limit=1, order=[], context=context) + ], limit=1, order=[]) if ids: return [('code',) + clause[1:]] return [(self._rec_name,) + clause[1:]] @@ -194,69 +197,60 @@ class Period(ModelSQL, ModelView): 'a journal - period from a closed period!', }) - def default_active(self, cursor, user, context=None): + def default_active(self): return True - def default_state(self, cursor, user, context=None): + def default_state(self): return 'open' - def get_icon(self, cursor, user, ids, name, context=None): + def get_icon(self, ids, name): res = {} - for period in self.browse(cursor, user, ids, context=context): + for period in self.browse(ids): res[period.id] = _ICONS.get(period.state, '') return res - def _check(self, cursor, user, ids, context=None): + def _check(self, ids): move_obj = self.pool.get('account.move') - for period in self.browse(cursor, user, ids, context=context): - move_ids = move_obj.search(cursor, user, [ + for period in self.browse(ids): + move_ids = move_obj.search([ ('journal', '=', period.journal.id), ('period', '=', period.period.id), - ], limit=1, context=context) + ], limit=1) if move_ids: - self.raise_user_error(cursor, 'modify_del_journal_period', - context=context) + self.raise_user_error('modify_del_journal_period') return - def create(self, cursor, user, vals, context=None): + def create(self, vals): period_obj = self.pool.get('account.period') if vals.get('period'): - period = period_obj.browse(cursor, user, vals['period'], - context=context) + period = period_obj.browse(vals['period']) if period.state == 'close': - self.raise_user_error(cursor, 'create_journal_period', - context=context) - return super(Period, self).create(cursor, user, vals, context=context) + self.raise_user_error('create_journal_period') + return super(Period, self).create(vals) - def write(self, cursor, user, ids, vals, context=None): + def write(self, ids, vals): if vals != {'state': 'close'} \ and vals != {'state': 'open'}: - self._check(cursor, user, ids, context=context) + self._check(ids) if vals.get('state') == 'open': - for journal_period in self.browse(cursor, user, ids, - context=context): + for journal_period in self.browse(ids): if journal_period.period.state == 'close': - self.raise_user_error(cursor, 'open_journal_period', - context=context) - return super(Period, self).write(cursor, user, ids, vals, - context=context) + self.raise_user_error('open_journal_period') + return super(Period, self).write(ids, vals) - def delete(self, cursor, user, ids, context=None): - self._check(cursor, user, ids, context=context) - return super(Period, self).delete(cursor, user, ids, context=context) + def delete(self, ids): + self._check(ids) + return super(Period, self).delete(ids) - def close(self, cursor, user, ids, context=None): + def close(self, ids): ''' Close journal - period - :param cursor: the database cursor - :param user: the user id :param ids: the journal - period ids - :param context: the context ''' - self.write(cursor, user, ids, { + self.write(ids, { 'state': 'close', - }, context=context) + }) Period() @@ -274,10 +268,9 @@ class ClosePeriod(Wizard): }, } - def _close(self, cursor, user, data, context=None): + def _close(self, data): journal_period_obj = self.pool.get('account.journal.period') - journal_period_obj.close(cursor, user, data['ids'], - context=context) + journal_period_obj.close(data['ids']) return {} ClosePeriod() @@ -296,11 +289,11 @@ class ReOpenPeriod(Wizard): }, } - def _reopen(self, cursor, user, data, context=None): + def _reopen(self, data): journal_period_obj = self.pool.get('account.journal.period') - journal_period_obj.write(cursor, user, data['ids'], { + journal_period_obj.write(data['ids'], { 'state': 'open', - }, context=context) + }) return {} ReOpenPeriod() diff --git a/move.py b/move.py index 2e3cd93c31..f43e47a550 100644 --- a/move.py +++ b/move.py @@ -1,11 +1,6 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -'Move' -from trytond.model import ModelView, ModelSQL, fields -from trytond.wizard import Wizard -from trytond.report import Report -from trytond.backend import TableHandler, FIELDS -from trytond.pyson import Equal, Eval, Get, PYSONEncoder +from __future__ import with_statement from decimal import Decimal import datetime import time @@ -14,6 +9,12 @@ try: except ImportError: hashlib = None import md5 +from trytond.model import ModelView, ModelSQL, fields +from trytond.wizard import Wizard +from trytond.report import Report +from trytond.backend import TableHandler, FIELDS +from trytond.pyson import Equal, Eval, Get, PYSONEncoder +from trytond.transaction import Transaction _MOVE_STATES = { 'readonly': Equal(Eval('state'), 'posted'), @@ -80,56 +81,53 @@ class Move(ModelSQL, ModelView): 'with a date outside the period!', }) - def init(self, cursor, module_name): - super(Move, self).init(cursor, module_name) + def init(self, module_name): + super(Move, self).init(module_name) + cursor = Transaction().cursor table = TableHandler(cursor, self, module_name) table.index_action(['journal', 'period'], 'add') # Add index on create_date table.index_action('create_date', action='add') - def default_period(self, cursor, user, context=None): + def default_period(self): period_obj = self.pool.get('account.period') - if context is None: - context = {} - return period_obj.find(cursor, user, context.get('company', False), - exception=False, context=context) + return period_obj.find(Transaction().context.get('company') or False, + exception=False) - 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): period_obj = self.pool.get('account.period') date_obj = self.pool.get('ir.date') - period_id = self.default_period(cursor, user, context=context) + period_id = self.default_period() if period_id: - period = period_obj.browse(cursor, user, period_id, - context=context) + period = period_obj.browse(period_id) return period.start_date - return date_obj.today(cursor, user, context=context) + return date_obj.today() - def on_change_with_date(self, cursor, user, vals, context=None): + def on_change_with_date(self, vals): line_obj = self.pool.get('account.move.line') period_obj = self.pool.get('account.period') res = vals['date'] - line_ids = line_obj.search(cursor, user, [ + line_ids = line_obj.search([ ('journal', '=', vals.get('journal', False)), ('period', '=', vals.get('period', False)), - ], order=[('id', 'DESC')], limit=1, context=context) + ], order=[('id', 'DESC')], limit=1) if line_ids: - line = line_obj.browse(cursor, user, line_ids[0], context=context) + line = line_obj.browse(line_ids[0]) res = line.date elif vals.get('period'): - period = period_obj.browse(cursor, user, vals['period'], - context=context) + period = period_obj.browse(vals['period']) res = period.start_date return res - def check_centralisation(self, cursor, user, ids): - for move in self.browse(cursor, user, ids): + def check_centralisation(self, ids): + for move in self.browse(ids): if move.journal.centralised: - move_ids = self.search(cursor, user, [ + move_ids = self.search([ ('period', '=', move.period.id), ('journal', '=', move.journal.id), ('state', '!=', 'posted'), @@ -138,8 +136,8 @@ class Move(ModelSQL, ModelView): return False return True - def check_company(self, cursor, user, ids): - for move in self.browse(cursor, user, ids): + def check_company(self, ids): + for move in self.browse(ids): company_id = -1 for line in move.lines: if company_id < 0: @@ -148,73 +146,67 @@ class Move(ModelSQL, ModelView): return False return True - def check_date(self, cursor, user, ids): - for move in self.browse(cursor, user, ids): + def check_date(self, ids): + for move in self.browse(ids): if move.date < move.period.start_date: return False if move.date > move.period.end_date: return False return True - 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', ('reference',) + clause[1:], (self._rec_name,) + clause[1:], - ], context=context) + ]) return [('id', 'in', ids)] - def write(self, cursor, user, ids, vals, context=None): - res = super(Move, self).write(cursor, user, ids, vals, context=context) - self.validate(cursor, user, ids, context=context) + def write(self, ids, vals): + res = super(Move, self).write(ids, vals) + self.validate(ids) return res - def create(self, cursor, user, vals, context=None): + def create(self, vals): move_line_obj = self.pool.get('account.move.line') sequence_obj = self.pool.get('ir.sequence') journal_obj = self.pool.get('account.journal') - if context is None: - context = {} - if not vals.get('name'): - journal_id = vals.get('journal') or context.get('journal') + journal_id = (vals.get('journal') + or Transaction().context.get('journal')) if journal_id: vals = vals.copy() - journal = journal_obj.browse(cursor, user, journal_id, - context=context) - vals['name'] = sequence_obj.get_id(cursor, user, - journal.sequence.id, context=context) + journal = journal_obj.browse(journal_id) + vals['name'] = sequence_obj.get_id(journal.sequence.id) - res = super(Move, self).create(cursor, user, vals, context=context) - move = self.browse(cursor, user, res, context=context) + res = super(Move, self).create(vals) + move = self.browse(res) if move.journal.centralised: - line_id = move_line_obj.create(cursor, user, { + line_id = move_line_obj.create({ 'account': move.journal.credit_account.id, 'move': move.id, 'name': 'Centralised Counterpart', - }, context=context) - self.write(cursor, user, move.id, { + }) + self.write(move.id, { 'centralised_line': line_id, - }, context=context) + }) if 'lines' in vals: - self.validate(cursor, user, [res], context=context) + self.validate([res]) return res - def delete(self, cursor, user, ids, context=None): + def delete(self, ids): if isinstance(ids, (int, long)): ids = [ids] move_line_obj = self.pool.get('account.move.line') - for move in self.browse(cursor, user, ids, context=context): + for move in self.browse(ids): if move.state == 'posted': - self.raise_user_error(cursor, 'del_posted_move', - context=context) + self.raise_user_error('del_posted_move') if move.lines: move_line_ids = [x.id for x in move.lines] - move_line_obj.delete(cursor, user, move_line_ids, - context=context) - return super(Move, self).delete(cursor, user, ids, context=context) + move_line_obj.delete(move_line_ids) + return super(Move, self).delete(ids) - def copy(self, cursor, user, ids, default=None, context=None): + def copy(self, ids, default=None): line_obj = self.pool.get('account.move.line') int_id = False @@ -226,24 +218,23 @@ class Move(ModelSQL, ModelView): default = {} default = default.copy() default['reference'] = False - default['state'] = self.default_state(cursor, user, context=context) + default['state'] = self.default_state() default['post_date'] = False default['lines'] = False new_ids = [] - for move in self.browse(cursor, user, ids, context=context): - new_id = super(Move, self).copy(cursor, user, move.id, - default=default, context=context) - line_obj.copy(cursor, user, [x.id for x in move.lines], default={ + for move in self.browse(ids): + new_id = super(Move, self).copy(move.id, default=default) + line_obj.copy([x.id for x in move.lines], default={ 'move': new_id, - }, context=context) + }) new_ids.append(new_id) if int_id: return new_ids[0] return new_ids - def validate(self, cursor, user, ids, context=None): + def validate(self, ids): ''' Validate balanced move and centralise it if in centralised journal ''' @@ -251,7 +242,7 @@ class Move(ModelSQL, ModelView): move_line_obj = self.pool.get('account.move.line') if isinstance(ids, (int, long)): ids = [ids] - for move in self.browse(cursor, user, ids, context=context): + for move in self.browse(ids): if not move.lines: continue amount = Decimal('0.0') @@ -263,12 +254,12 @@ class Move(ModelSQL, ModelView): company = line.account.company if line.state == 'draft': draft_lines.append(line) - if not currency_obj.is_zero(cursor, user, company.currency, amount): + if not currency_obj.is_zero(company.currency, amount): if not move.journal.centralised: - move_line_obj.write(cursor, user, + move_line_obj.write( [x.id for x in move.lines if x.state != 'draft'], { 'state': 'draft', - }, context=context) + }) else: if not move.centralised_line: centralised_amount = - amount @@ -285,34 +276,31 @@ class Move(ModelSQL, ModelView): credit = - centralised_amount account_id = move.journal.credit_account.id if not move.centralised_line: - centralised_line_id = move_line_obj.create(cursor, - user, { - 'debit': debit, - 'credit': credit, - 'account': account_id, - 'move': move.id, - 'name': 'Centralised Counterpart', - }, context=context) - self.write(cursor, user, move.id, { + centralised_line_id = move_line_obj.create({ + 'debit': debit, + 'credit': credit, + 'account': account_id, + 'move': move.id, + 'name': 'Centralised Counterpart', + }) + self.write(move.id, { 'centralised_line': centralised_line_id, - }, context=context) + }) else: - move_line_obj.write(cursor, user, - move.centralised_line.id, { - 'debit': debit, - 'credit': credit, - 'account': account_id, - }, context=context) + move_line_obj.write( move.centralised_line.id, { + 'debit': debit, + 'credit': credit, + 'account': account_id, + }) continue if not draft_lines: continue - move_line_obj.write(cursor, user, - [x.id for x in draft_lines], { - 'state': 'valid', - }, context=context) + move_line_obj.write( [x.id for x in draft_lines], { + 'state': 'valid', + }) return - def post(self, cursor, user, ids, context=None): + def post(self, ids): currency_obj = self.pool.get('currency.currency') sequence_obj = self.pool.get('ir.sequence') date_obj = self.pool.get('ir.date') @@ -320,44 +308,40 @@ class Move(ModelSQL, ModelView): if isinstance(ids, (int, long)): ids = [ids] - moves = self.browse(cursor, user, ids, context=context) + moves = self.browse(ids) for move in moves: amount = Decimal('0.0') if not move.lines: - self.raise_user_error(cursor, 'post_empty_move', - context=context) + self.raise_user_error('post_empty_move') company = None for line in move.lines: amount += line.debit - line.credit if not company: company = line.account.company - if not currency_obj.is_zero(cursor, user, company.currency, amount): - self.raise_user_error(cursor, 'post_unbalanced_move', - context=context) + if not currency_obj.is_zero(company.currency, amount): + self.raise_user_error('post_unbalanced_move') for move in moves: - reference = sequence_obj.get_id(cursor, user, - move.period.post_move_sequence.id, context=context) - self.write(cursor, user, move.id, { + reference = sequence_obj.get_id(move.period.post_move_sequence.id) + self.write(move.id, { 'reference': reference, 'state': 'posted', - 'post_date': date_obj.today(cursor, user, context=context), - }, context=context) + 'post_date': date_obj.today(), + }) return - def draft(self, cursor, user, ids, context=None): - for move in self.browse(cursor, user, ids, context=context): + def draft(self, ids): + for move in self.browse(ids): if not move.journal.update_posted: - self.raise_user_error(cursor, 'modify_posted_move', - context=context) - return self.write(cursor, user, ids, { + self.raise_user_error('modify_posted_move') + return self.write(ids, { 'state': 'draft', - }, context=context) + }) - def button_post(self, cursor, user, ids, context=None): - return self.post(cursor, user, ids, context=None) + def button_post(self, ids): + return self.post(ids) - def button_draft(self, cursor, user, ids, context=None): - return self.draft(cursor, user, ids, context=context) + def button_draft(self, ids): + return self.draft(ids) Move() @@ -384,26 +368,25 @@ class Reconciliation(ModelSQL, ModelView): 'nor from the same party!', }) - def default_name(self, cursor, user, context=None): + def default_name(self): sequence_obj = self.pool.get('ir.sequence') - return sequence_obj.get(cursor, user, 'account.move.reconciliation', - context=context) + return sequence_obj.get('account.move.reconciliation') - def create(self, cursor, user, vals, context=None): + def create(self, vals): move_line_obj = self.pool.get('account.move.line') - res = super(Reconciliation, self).create(cursor, user, vals, context=context) - reconciliation = self.browse(cursor, user, res, context=context) + res = super(Reconciliation, self).create(vals) + reconciliation = self.browse(res) - move_line_obj.workflow_trigger_trigger(cursor, user, - [x.id for x in reconciliation.lines], context=context) + move_line_obj.workflow_trigger_trigger( + [x.id for x in reconciliation.lines]) return res - def write(self, cursor, user, ids, vals, context=None): - self.raise_user_error(cursor, 'modify', context=context) + def write(self, ids, vals): + self.raise_user_error('modify') - def check_lines(self, cursor, user, ids): + def check_lines(self, ids): currency_obj = self.pool.get('currency.currency') - for reconciliation in self.browse(cursor, user, ids): + for reconciliation in self.browse(ids): amount = Decimal('0.0') account = None party = None @@ -421,8 +404,7 @@ class Reconciliation(ModelSQL, ModelView): party = line.party elif line.party and party.id != line.party.id: return False - if not currency_obj.is_zero(cursor, user, account.company.currency, - amount): + if not currency_obj.is_zero(account.company.currency, amount): return False return True @@ -518,9 +500,10 @@ class Line(ModelSQL, ModelView): 'already_reconciled': 'Line "%s" (%d) already reconciled!', }) - def init(self, cursor, module_name): - super(Line, self).init(cursor, module_name) + def init(self, module_name): + super(Line, self).init(module_name) + cursor = Transaction().cursor table = TableHandler(cursor, self, module_name) # Index for General Ledger table.index_action(['move', 'account'], 'add') @@ -528,7 +511,7 @@ class Line(ModelSQL, ModelView): # Migration from 1.2 table.not_null_action('blocked', action='remove') - def default_date(self, cursor, user, context=None): + def default_date(self): ''' Return the date of the last line for journal, period or the starting date of the period @@ -537,44 +520,38 @@ class Line(ModelSQL, ModelView): period_obj = self.pool.get('account.period') date_obj = self.pool.get('ir.date') - if context is None: - context = {} - res = date_obj.today(cursor, user, context=context) - line_ids = self.search(cursor, user, [ - ('journal', '=', context.get('journal', False)), - ('period', '=', context.get('period', False)), - ], order=[('id', 'DESC')], limit=1, context=context) + res = date_obj.today() + line_ids = self.search([ + ('journal', '=', Transaction().context.get('journal', False)), + ('period', '=', Transaction().context.get('period', False)), + ], order=[('id', 'DESC')], limit=1) if line_ids: - line = self.browse(cursor, user, line_ids[0], context=context) + line = self.browse(line_ids[0]) res = line.date - elif context.get('period'): - period = period_obj.browse(cursor, user, context['period'], - context=context) + elif Transaction().context.get('period'): + period = period_obj.browse(Transaction().context['period']) res = period.start_date - if context.get('date'): - res = context['date'] + if Transaction().context.get('date'): + res = Transaction().context['date'] return res - def default_state(self, cursor, user, context=None): + def default_state(self): return 'draft' - def default_active(self, cursor, user, context=None): + def default_active(self): return True - def default_currency_digits(self, cursor, user, context=None): + def default_currency_digits(self): return 2 - def default_get(self, cursor, user, fields, context=None, - with_rec_name=True): - if context is None: - context = {} + def default_get(self, fields, with_rec_name=True): move_obj = self.pool.get('account.move') tax_obj = self.pool.get('account.tax') account_obj = self.pool.get('account.account') tax_code_obj = self.pool.get('account.tax.code') currency_obj = self.pool.get('currency.currency') - values = super(Line, self).default_get(cursor, user, fields, - context=context, with_rec_name=with_rec_name) + values = super(Line, self).default_get(fields, + with_rec_name=with_rec_name) if 'move' not in fields: #Not manual entry @@ -583,22 +560,23 @@ class Line(ModelSQL, ModelView): del values['date'] return values - if context.get('journal') and context.get('period'): - line_ids = self.search(cursor, user, [ - ('move.journal', '=', context['journal']), - ('move.period', '=', context['period']), + if (Transaction().context.get('journal') + and Transaction().context.get('period')): + line_ids = self.search([ + ('move.journal', '=', Transaction().context['journal']), + ('move.period', '=', Transaction().context['period']), ('create_uid', '=', user), ('state', '=', 'draft'), - ], order=[('id', 'DESC')], limit=1, context=context) + ], order=[('id', 'DESC')], limit=1) if not line_ids: return values - line = self.browse(cursor, user, line_ids[0], context=context) + line = self.browse(line_ids[0]) values['move'] = line.move.id if 'move' not in values: return values - move = move_obj.browse(cursor, user, values['move'], context=context) + move = move_obj.browse(values['move']) total = Decimal('0.0') taxes = {} no_code_taxes = [] @@ -673,9 +651,9 @@ class Line(ModelSQL, ModelView): key = 'credit_note' line_amount = Decimal('0.0') tax_amount = Decimal('0.0') - for tax_line in tax_obj.compute(cursor, user, + for tax_line in tax_obj.compute( [x.id for x in line.account.taxes], - line.debit or line.credit, 1, context=context): + line.debit or line.credit, 1): if (tax_line['tax'][key + '_account'].id \ or line.account.id) == account_id \ and tax_line['tax'][key + '_tax_code'].id \ @@ -687,9 +665,9 @@ class Line(ModelSQL, ModelView): line_amount -= tax_line['amount'] tax_amount += tax_line['amount'] * \ tax_line['tax'][key + '_tax_sign'] - line_amount = currency_obj.round(cursor, user, + line_amount = currency_obj.round( line.account.company.currency, line_amount) - tax_amount = currency_obj.round(cursor, user, + tax_amount = currency_obj.round( line.account.company.currency, tax_amount) if ('debit' in fields): values['debit'] = line_amount > Decimal('0.0') \ @@ -699,26 +677,25 @@ class Line(ModelSQL, ModelView): and - line_amount or Decimal('0.0') if 'account' in fields: values['account'] = account_id - values['account.rec_name'] = account_obj.browse(cursor, - user, account_id, context=context).rec_name + values['account.rec_name'] = account_obj.browse( + account_id).rec_name if 'tax_lines' in fields and code_id: values['tax_lines'] = [ { 'amount': tax_amount, 'currency_digits': line.currency_digits, 'code': code_id, - 'code.rec_name': tax_code_obj.browse(cursor, - user, code_id, context=context).rec_name, + 'code.rec_name': tax_code_obj.browse( + code_id).rec_name, 'tax': tax_id, - 'tax.rec_name': tax_obj.browse(cursor, user, - tax_id, context=context).rec_name + 'tax.rec_name': tax_obj.browse(tax_id).rec_name }, ] return values - def get_currency_digits(self, cursor, user, ids, names, context=None): + def get_currency_digits(self, ids, names): res = {} - for line in self.browse(cursor, user, ids, context=context): + for line in self.browse(ids): for name in names: res.setdefault(name, {}) res[name].setdefault(line.id, 2) @@ -729,69 +706,55 @@ class Line(ModelSQL, ModelView): res[name][line.id] = line.account.second_currency.digits return res - def on_change_debit(self, cursor, user, vals, context=None): + def on_change_debit(self, vals): res = {} - if context is None: - context = {} journal_obj = self.pool.get('account.journal') - if vals.get('journal') or context.get('journal'): - journal = journal_obj.browse(cursor, user, - vals.get('journal') or context.get('journal'), - context=context) + if vals.get('journal') or Transaction().context.get('journal'): + journal = journal_obj.browse(vals.get('journal') + or Transaction().context.get('journal')) if journal.type in ('expense', 'revenue'): - res['tax_lines'] = self._compute_tax_lines(cursor, user, - vals, journal.type, context=context) + res['tax_lines'] = self._compute_tax_lines(vals, journal.type) if not res['tax_lines']: del res['tax_lines'] if vals.get('debit'): res['credit'] = Decimal('0.0') return res - def on_change_credit(self, cursor, user, vals, context=None): + def on_change_credit(self, vals): res = {} - if context is None: - context = {} journal_obj = self.pool.get('account.journal') - if vals.get('journal') or context.get('journal'): - journal = journal_obj.browse(cursor, user, - vals.get('journal') or context.get('journal'), - context=context) + if vals.get('journal') or Transaction().context.get('journal'): + journal = journal_obj.browse(vals.get('journal') + or Transaction().context.get('journal')) if journal.type in ('expense', 'revenue'): - res['tax_lines'] = self._compute_tax_lines(cursor, user, - vals, journal.type, context=context) + res['tax_lines'] = self._compute_tax_lines( + vals, journal.type) if not res['tax_lines']: del res['tax_lines'] if vals.get('credit'): res['debit'] = Decimal('0.0') return res - def on_change_account(self, cursor, user, vals, context=None): + def on_change_account(self, vals): account_obj = self.pool.get('account.account') journal_obj = self.pool.get('account.journal') res = {} - if context is None: - context = {} - - if context.get('journal'): - journal = journal_obj.browse(cursor, user, - context['journal'], context=context) + if Transaction().context.get('journal'): + journal = journal_obj.browse(Transaction().context['journal']) if journal.type in ('expense', 'revenue'): - res['tax_lines'] = self._compute_tax_lines(cursor, user, - vals, journal.type, context=context) + res['tax_lines'] = self._compute_tax_lines(vals, journal.type) if not res['tax_lines']: del res['tax_lines'] if vals.get('account'): - account = account_obj.browse(cursor, user, vals['account'], - context=context) + account = account_obj.browse(vals['account']) res['currency_digits'] = account.currency_digits if account.second_currency: res['second_currency_digits'] = account.second_currency.digits return res - def _compute_tax_lines(self, cursor, user, vals, journal_type, - context=None): + def _compute_tax_lines(self, vals, journal_type): res = {} account_obj = self.pool.get('account.account') tax_code_obj = self.pool.get('account.tax.code') @@ -803,8 +766,7 @@ class Line(ModelSQL, ModelView): if vals.get('tax_lines'): res['remove'] = [x['id'] for x in vals['tax_lines']] if vals.get('account'): - account = account_obj.browse(cursor, user, vals['account'], - context=context) + account = account_obj.browse(vals['account']) debit = vals.get('debit', Decimal('0.0')) credit = vals.get('credit', Decimal('0.0')) for tax in account.taxes: @@ -819,9 +781,9 @@ class Line(ModelSQL, ModelView): else: key = 'credit_note' base_amounts = {} - for tax_line in tax_obj.compute(cursor, user, + for tax_line in tax_obj.compute( [x.id for x in account.taxes], - debit or credit, 1, context=context): + debit or credit, 1): code_id = tax_line['tax'][key + '_base_code'].id tax_id = tax_line['tax'].id base_amounts.setdefault((code_id, tax_id), Decimal('0.0')) @@ -834,24 +796,22 @@ class Line(ModelSQL, ModelView): 'amount': base_amounts[code_id, tax_id], 'currency_digits': account.currency_digits, 'code': code_id, - 'code.rec_name': tax_code_obj.browse(cursor, user, - code_id, context=context).rec_name, + 'code.rec_name': tax_code_obj.browse(code_id).rec_name, 'tax': tax_id, - 'tax.rec_name': tax_obj.browse(cursor, user, - tax_id, context=context).rec_name + 'tax.rec_name': tax_obj.browse(tax_id).rec_name, }) return res - def on_change_party(self, cursor, user, vals, context=None): + def on_change_party(self, vals): party_obj = self.pool.get('party.party') journal_obj = self.pool.get('account.journal') account_obj = self.pool.get('account.account') currency_obj = self.pool.get('currency.currency') + cursor = Transaction().cursor res = {} if (not vals.get('party')) or vals.get('account'): return res - party = party_obj.browse(cursor, user, vals.get('party'), - context=context) + party = party_obj.browse(vals.get('party')) if not party.account_receivable \ or not party.account_payable: @@ -872,30 +832,30 @@ class Line(ModelSQL, ModelView): # SQLite uses float for SUM if not isinstance(amount, Decimal): amout = Decimal(str(amount)) - if not currency_obj.is_zero(cursor, user, - party.account_receivable.currency, amount): + if not currency_obj.is_zero( party.account_receivable.currency, + amount): if amount > Decimal('0.0'): - res['credit'] = currency_obj.round(cursor, user, + res['credit'] = currency_obj.round( party.account_receivable.currency, amount) res['debit'] = Decimal('0.0') else: res['credit'] = Decimal('0.0') - res['debit'] = - currency_obj.round(cursor, user, + res['debit'] = - currency_obj.round( party.account_receivable.currency, amount) res['account'] = party.account_receivable.id res['account.rec_name'] = party.account_receivable.rec_name else: cursor.execute(query, (party.id, party.account_payable.id)) amount = cursor.fetchone()[0] - if not currency_obj.is_zero(cursor, user, - party.account_payable.currency, amount): + if not currency_obj.is_zero( party.account_payable.currency, + amount): if amount > Decimal('0.0'): - res['credit'] = currency_obj.round(cursor, user, + res['credit'] = currency_obj.round( party.account_payable.currency, amount) res['debit'] = Decimal('0.0') else: res['credit'] = Decimal('0.0') - res['debit'] = - currency_obj.round(cursor, user, + res['debit'] = - currency_obj.round( party.account_payable.currency, amount) res['account'] = party.account_payable.id res['account.rec_name'] = party.account_payable.rec_name @@ -920,10 +880,10 @@ class Line(ModelSQL, ModelView): res['account'] = party.account_receivable.id res['account.rec_name'] = party.account_receivable.rec_name - journal_id = vals.get('journal') or context.get('journal') + journal_id = (vals.get('journal') + or Transaction().context.get('journal')) if journal_id and party: - journal = journal_obj.browse(cursor, user, journal_id, - context=context) + journal = journal_obj.browse(journal_id) if journal.type == 'revenue': if 'account' not in res: res['account'] = party.account_receivable.id @@ -934,20 +894,20 @@ class Line(ModelSQL, ModelView): res['account.rec_name'] = party.account_payable.rec_name return res - def get_move_field(self, cursor, user, ids, name, context=None): + def get_move_field(self, ids, name): if name == 'move_state': name = 'state' if name not in ('period', 'journal', 'date', 'state'): raise Exception('Invalid name') res = {} - for line in self.browse(cursor, user, ids, context=context): + for line in self.browse(ids): if name in ('date', 'state'): res[line.id] = line.move[name] else: res[line.id] = line.move[name].id return res - def set_move_field(self, cursor, user, ids, name, value, context=None): + def set_move_field(self, ids, name, value): if name == 'move_state': name = 'state' if name not in ('period', 'journal', 'date', 'state'): @@ -955,41 +915,36 @@ class Line(ModelSQL, ModelView): if not value: return move_obj = self.pool.get('account.move') - lines = self.browse(cursor, user, ids, context=context) - move_obj.write(cursor, user, [line.move.id for line in lines], { + lines = self.browse(ids) + move_obj.write([line.move.id for line in lines], { name: value, - }, context=context) + }) - def search_move_field(self, cursor, user, name, clause, context=None): + def search_move_field(self, name, clause): if name == 'move_state': name = 'state' return [('move.' + name,) + clause[1:]] - def query_get(self, cursor, user, obj='l', context=None): + def query_get(self, obj='l'): ''' Return SQL clause and fiscal years for account move line depending of the context. - :param cursor: the database cursor - :param user: the user id :param obj: the SQL alias of account_move_line in the query - :param context: the context :return: a tuple with the SQL clause and the fiscalyear ids ''' fiscalyear_obj = self.pool.get('account.fiscalyear') - if context is None: - context = {} - if context.get('date'): - time.strptime(str(context['date']), '%Y-%m-%d') - fiscalyear_ids = fiscalyear_obj.search(cursor, user, [ - ('start_date', '<=', context['date']), - ('end_date', '>=', context['date']), - ], limit=1, context=context) + if Transaction().context.get('date'): + time.strptime(str(Transaction().context['date']), '%Y-%m-%d') + fiscalyear_ids = fiscalyear_obj.search([ + ('start_date', '<=', Transaction().context['date']), + ('end_date', '>=', Transaction().context['date']), + ], limit=1) fiscalyear_id = fiscalyear_ids and fiscalyear_ids[0] or 0 - if context.get('posted'): + if Transaction().context.get('posted'): return (obj + '.active ' \ 'AND ' + obj + '.state != \'draft\' ' \ 'AND ' + obj + '.move IN (' \ @@ -999,7 +954,8 @@ class Line(ModelSQL, ModelView): 'AND p.fiscalyear = ' + \ str(fiscalyear_id) + ' ' \ 'AND m.date <= date(\'' + \ - str(context['date']) + '\') ' \ + str(Transaction().context['date']) + \ + '\') ' \ 'AND m.state = \'posted\' ' \ ')', fiscalyear_ids) else: @@ -1012,21 +968,24 @@ class Line(ModelSQL, ModelView): 'AND p.fiscalyear = ' + \ str(fiscalyear_id) + ' ' \ 'AND m.date <= date(\'' + \ - str(context['date']) + '\')' \ + str(Transaction().context['date']) + \ + '\')' \ ')', fiscalyear_ids) - if not context.get('fiscalyear', False): - fiscalyear_ids = fiscalyear_obj.search(cursor, user, [ + if not Transaction().context.get('fiscalyear'): + fiscalyear_ids = fiscalyear_obj.search([ ('state', '=', 'open'), - ], context=context) + ]) fiscalyear_clause = (','.join(map(str, fiscalyear_ids))) or '0' else: - fiscalyear_ids = [int(context.get('fiscalyear'))] - fiscalyear_clause = '%s' % int(context.get('fiscalyear')) + fiscalyear_ids = [int(Transaction().context.get('fiscalyear'))] + fiscalyear_clause = '%s' % int( + Transaction().context.get('fiscalyear')) - if context.get('periods', False): - ids = ','.join(str(int(x)) for x in context['periods']) - if context.get('posted'): + if Transaction().context.get('periods'): + ids = ','.join( + str(int(x)) for x in Transaction().context['periods']) + if Transaction().context.get('posted'): return (obj + '.active ' \ 'AND ' + obj + '.state != \'draft\' ' \ 'AND ' + obj + '.move IN (' \ @@ -1042,7 +1001,7 @@ class Line(ModelSQL, ModelView): 'WHERE period IN (' + ids + ')' \ ')', []) else: - if context.get('posted'): + if Transaction().context.get('posted'): return (obj + '.active ' \ 'AND ' + obj + '.state != \'draft\' ' \ 'AND ' + obj + '.move IN (' \ @@ -1064,23 +1023,22 @@ class Line(ModelSQL, ModelView): ')' \ ')', fiscalyear_ids) - def on_write(self, cursor, user, ids, context=None): - lines = self.browse(cursor, user, ids, context) + def on_write(self, ids): + lines = self.browse(ids) res = [] for line in lines: res.extend([x.id for x in line.move.lines]) return list({}.fromkeys(res)) - def check_account(self, cursor, user, ids): - for line in self.browse(cursor, user, ids): + def check_account(self, ids): + for line in self.browse(ids): if line.account.kind in ('view',): return False if not line.account.active: return False return True - def check_journal_period_modify(self, cursor, user, period_id, - journal_id, context=None): + def check_journal_period_modify(self, period_id, journal_id): ''' Check if the lines can be modified or created for the journal - period and if there is no journal - period, create it @@ -1088,159 +1046,136 @@ class Line(ModelSQL, ModelView): journal_period_obj = self.pool.get('account.journal.period') journal_obj = self.pool.get('account.journal') period_obj = self.pool.get('account.period') - journal_period_ids = journal_period_obj.search(cursor, user, [ + journal_period_ids = journal_period_obj.search([ ('journal', '=', journal_id), ('period', '=', period_id), - ], limit=1, context=context) + ], limit=1) if journal_period_ids: - journal_period = journal_period_obj.browse(cursor, user, - journal_period_ids[0], context=context) + journal_period = journal_period_obj.browse(journal_period_ids[0]) if journal_period.state == 'close': - self.raise_user_error(cursor, - 'add_modify_closed_journal_period', context=context) + self.raise_user_error('add_modify_closed_journal_period') else: - journal = journal_obj.browse(cursor, user, journal_id, - context=context) - period = period_obj.browse(cursor, user, period_id, - context=context) - journal_period_obj.create(cursor, user, { + journal = journal_obj.browse(journal_id) + period = period_obj.browse(period_id) + journal_period_obj.create({ 'name': journal.name + ' - ' + period.name, 'journal': journal.id, 'period': period.id, - }, context=context) + }) return - def check_modify(self, cursor, user, ids, context=None): + def check_modify(self, ids): ''' Check if the lines can be modified ''' journal_period_done = [] - for line in self.browse(cursor, user, ids, context=context): + for line in self.browse(ids): if line.move.state == 'posted': - self.raise_user_error(cursor, 'modify_posted_move', - context=context) + self.raise_user_error('modify_posted_move') if line.reconciliation: - self.raise_user_error(cursor, 'modify_reconciled', - context=context) + self.raise_user_error('modify_reconciled') journal_period = (line.journal.id, line.period.id) if journal_period not in journal_period_done: - self.check_journal_period_modify(cursor, user, line.period.id, - line.journal.id, context=context) + self.check_journal_period_modify(line.period.id, + line.journal.id) journal_period_done.append(journal_period) return - def delete(self, cursor, user, ids, context=None): + def delete(self, ids): move_obj = self.pool.get('account.move') if isinstance(ids, (int, long)): ids = [ids] - self.check_modify(cursor, user, ids, context=context) - lines = self.browse(cursor, user, ids, context=context) + self.check_modify(ids) + lines = self.browse(ids) move_ids = [x.move.id for x in lines] - res = super(Line, self).delete(cursor, user, ids, context=context) - move_obj.validate(cursor, user, move_ids, context=context) + res = super(Line, self).delete(ids) + move_obj.validate(move_ids) return res - def write(self, cursor, user, ids, vals, context=None): + def write(self, ids, vals): move_obj = self.pool.get('account.move') - if context is None: - context = {} - if isinstance(ids, (int, long)): ids = [ids] if len(vals) > 1 or 'reconciliation' not in vals: - self.check_modify(cursor, user, ids, context=context) - lines = self.browse(cursor, user, ids, context=context) + self.check_modify(ids) + lines = self.browse(ids) move_ids = [x.move.id for x in lines] - res = super(Line, self).write(cursor, user, ids, vals, context=context) + res = super(Line, self).write(ids, vals) - if context.get('_timestamp'): - context = context.copy() - del context['_timestamp'] + Transaction().timestamp = {} - lines = self.browse(cursor, user, ids, context=context) + lines = self.browse(ids) for line in lines: if line.move.id not in move_ids: move_ids.append(line.move.id) - move_obj.validate(cursor, user, move_ids, context=context) + move_obj.validate(move_ids) return res - def create(self, cursor, user, vals, context=None): - if context is None: - context = {} + def create(self, vals): journal_obj = self.pool.get('account.journal') move_obj = self.pool.get('account.move') vals = vals.copy() if not vals.get('move'): - journal_id = vals.get('journal') or context.get('journal') + journal_id = (vals.get('journal') + or Transaction().context.get('journal')) if not journal_id: - self.raise_user_error(cursor, 'no_journal', - context=context) - journal = journal_obj.browse(cursor, user, journal_id, - context=context) + self.raise_user_error('no_journal') + journal = journal_obj.browse(journal_id) if journal.centralised: - move_ids = move_obj.search(cursor, user, [ - ('period', '=', - vals.get('period') or context.get('period')), + move_ids = move_obj.search([ + ('period', '=', vals.get('period') + or Transaction().context.get('period')), ('journal', '=', journal_id), ('state', '!=', 'posted'), - ], limit=1, context=context) + ], limit=1) if move_ids: vals['move'] = move_ids[0] if not vals.get('move'): - vals['move'] = move_obj.create(cursor, user, { - 'period': vals.get('period') or context.get('period'), + vals['move'] = move_obj.create({ + 'period': (vals.get('period') + or Transaction().context.get('period')), 'journal': journal_id, 'date': vals.get('date', False), - }, context=context) - res = super(Line, self).create(cursor, user, vals, context=context) - line = self.browse(cursor, user, res, context=context) - self.check_journal_period_modify(cursor, user, line.period.id, - line.journal.id, context=context) - move_obj.validate(cursor, user, [vals['move']], context=context) + }) + res = super(Line, self).create(vals) + line = self.browse(res) + self.check_journal_period_modify(line.period.id, line.journal.id) + move_obj.validate([vals['move']]) return res - def copy(self, cursor, user, ids, default=None, context=None): + def copy(self, ids, default=None): if default is None: default = {} if 'move' not in default: default['move'] = False if 'reconciliation' not in default: default['reconciliation'] = False - return super(Line, self).copy(cursor, user, ids, default=default, - context=context) + return super(Line, self).copy(ids, default=default) - def view_header_get(self, cursor, user, value, view_type='form', - context=None): + def view_header_get(self, value, view_type='form'): journal_period_obj = self.pool.get('account.journal.period') - if context is None: - context = {} - if not context.get('journal') or not context.get('period'): + if (not Transaction().context.get('journal') + or not Transaction().context.get('period')): return value - journal_period_ids = journal_period_obj.search(cursor, user, [ - ('journal', '=', context['journal']), - ('period', '=', context['period']), - ], context=context, limit=1) + journal_period_ids = journal_period_obj.search([ + ('journal', '=', Transaction().context['journal']), + ('period', '=', Transaction().context['period']), + ], limit=1) if not journal_period_ids: return value - journal_period = journal_period_obj.browse(cursor, user, - journal_period_ids[0], context=context) + journal_period = journal_period_obj.browse(journal_period_ids[0]) return value + ': ' + journal_period.rec_name - def fields_view_get(self, cursor, user, view_id=None, view_type='form', - context=None, toolbar=False, hexmd5=None): - if context is None: - context = {} + def fields_view_get(self, view_id=None, view_type='form', toolbar=False, + hexmd5=None): journal_obj = self.pool.get('account.journal') - result = super(Line, self).fields_view_get(cursor, user, - view_id=view_id, view_type=view_type, context=context, - toolbar=toolbar, hexmd5=hexmd5) - if view_type == 'tree' and 'journal' in context: - title = self.view_header_get(cursor, user, '', - view_type=view_type, context=context) - journal = journal_obj.browse(cursor, user, context['journal'], - context=context) + result = super(Line, self).fields_view_get(view_id=view_id, + view_type=view_type, toolbar=toolbar, hexmd5=hexmd5) + if view_type == 'tree' and 'journal' in Transaction().context: + title = self.view_header_get('', view_type=view_type) + journal = journal_obj.browse(Transaction().context['journal']) if not journal.view: return result @@ -1268,8 +1203,7 @@ class Line(ModelSQL, ModelView): fields.add('state') xml += '' result['arch'] = xml - result['fields'] = self.fields_get(cursor, user, - fields_names=list(fields), context=context) + result['fields'] = self.fields_get(fields_names=list(fields)) del result['md5'] if hashlib: result['md5'] = hashlib.md5(str(result)).hexdigest() @@ -1279,8 +1213,7 @@ class Line(ModelSQL, ModelView): return True return result - def reconcile(self, cursor, user, ids, journal_id=False, date=False, - account_id=False, context=None): + def reconcile(self, ids, journal_id=False, date=False, account_id=False): move_obj = self.pool.get('account.move') currency_obj = self.pool.get('currency.currency') reconciliation_obj = self.pool.get('account.move.reconciliation') @@ -1288,31 +1221,30 @@ class Line(ModelSQL, ModelView): date_obj = self.pool.get('ir.date') translation_obj = self.pool.get('ir.translation') - for line in self.browse(cursor, user, ids, context=context): + for line in self.browse(ids): if line.reconciliation: - self.raise_user_error(cursor, 'already_reconciled', - error_args=(line.name, line.id,), context=context) + self.raise_user_error('already_reconciled', + error_args=(line.name, line.id,)) ids = ids[:] if journal_id and account_id: if not date: - date = date_obj.today(cursor, user, context=context) + date = date_obj.today() account = None amount = Decimal('0.0') - for line in self.browse(cursor, user, ids, context=context): + for line in self.browse(ids): amount += line.debit - line.credit if not account: account = line.account - amount = currency_obj.round(cursor, user, account.currency, amount) - period_id = period_obj.find(cursor, user, account.company.id, - date=date, context=context) + amount = currency_obj.round(account.currency, amount) + period_id = period_obj.find(account.company.id, date=date) lang_code = 'en_US' if account.company.lang: lang_code = account.company.lang.code - writeoff = translation_obj._get_source(cursor, - 'account.move.reconcile_lines.writeoff', 'view', - lang_code, 'Write-Off') or 'Write-Off' - move_id = move_obj.create(cursor, user, { + writeoff = translation_obj._get_source( + 'account.move.reconcile_lines.writeoff', 'view', lang_code, + 'Write-Off') or 'Write-Off' + move_id = move_obj.create({ 'journal': journal_id, 'period': period_id, 'date': date, @@ -1334,18 +1266,18 @@ class Line(ModelSQL, ModelView): or Decimal('0.0'), }), ], - }, context=context) - ids += self.search(cursor, user, [ + }) + ids += self.search([ ('move', '=', move_id), ('account', '=', account.id), ('debit', '=', amount < Decimal('0.0') and - amount \ or Decimal('0.0')), ('credit', '=', amount > Decimal('0.0') and amount \ or Decimal('0.0')), - ], limit=1, context=context) - return reconciliation_obj.create(cursor, user, { + ], limit=1) + return reconciliation_obj.create({ 'lines': [('add', x) for x in ids], - }, context=context) + }) Line() @@ -1370,12 +1302,10 @@ class OpenJournalAsk(ModelView): Get(Eval('context', {}), 'company', 0)), ]) - def default_period(self, cursor, user, context=None): + def default_period(self): period_obj = self.pool.get('account.period') - if context is None: - context = {} - return period_obj.find(cursor, user, context.get('company', False), - exception=False, context=context) + return period_obj.find(Transaction().context.get('company') or False, + exception=False) OpenJournalAsk() @@ -1409,25 +1339,24 @@ class OpenJournal(Wizard): }, } - def _next(self, cursor, user, data, context=None): + def _next(self, data): if data.get('model', '') == 'account.journal.period' \ and data.get('id'): return 'open' return 'ask' - def _get_journal_period(self, cursor, user, data, context=None): + def _get_journal_period(self, data): journal_period_obj = self.pool.get('account.journal.period') if data.get('model', '') == 'account.journal.period' \ and data.get('id'): - journal_period = journal_period_obj.browse(cursor, user, - data['id'], context=context) + journal_period = journal_period_obj.browse(data['id']) return { 'journal': journal_period.journal.id, 'period': journal_period.period.id, } return {} - def _action_open_journal(self, cursor, user, data, context=None): + def _action_open_journal(self, data): journal_period_obj = self.pool.get('account.journal.period') journal_obj = self.pool.get('account.journal') period_obj = self.pool.get('account.period') @@ -1435,30 +1364,26 @@ class OpenJournal(Wizard): act_window_obj = self.pool.get('ir.action.act_window') if data.get('model', '') == 'account.journal.period' \ and data.get('id'): - journal_period = journal_period_obj.browse(cursor, user, - data['id'], context=context) + journal_period = journal_period_obj.browse(data['id']) journal_id = journal_period.journal.id period_id = journal_period.period.id else: journal_id = data['form']['journal'] period_id = data['form']['period'] - if not journal_period_obj.search(cursor, user, [ + if not journal_period_obj.search([ ('journal', '=', journal_id), ('period', '=', period_id), - ], context=context): - journal = journal_obj.browse(cursor, user, journal_id, - context=context) - period = period_obj.browse(cursor, user, period_id, - context=context) - journal_period_obj.create(cursor, user, { + ]): + journal = journal_obj.browse(journal_id) + period = period_obj.browse(period_id) + journal_period_obj.create({ 'name': journal.name + ' - ' + period.name, 'journal': journal.id, 'period': period.id, - }, context=context) + }) - act_window_id = model_data_obj.get_id(cursor, user, 'account', - 'act_move_line_form', context=context) - res = act_window_obj.read(cursor, user, act_window_id, context=context) + act_window_id = model_data_obj.get_id('account', 'act_move_line_form') + res = act_window_obj.read(act_window_id) # Remove name to use the one from view_header_get del res['name'] res['pyson_domain'] = PYSONEncoder().encode([ @@ -1487,38 +1412,34 @@ class OpenAccount(Wizard): }, } - def _action_open_account(self, cursor, user, data, context=None): - if context is None: - context = {} + def _action_open_account(self, data): model_data_obj = self.pool.get('ir.model.data') act_window_obj = self.pool.get('ir.action.act_window') fiscalyear_obj = self.pool.get('account.fiscalyear') - if not context.get('fiscalyear'): - fiscalyear_ids = fiscalyear_obj.search(cursor, user, [ + if not Transaction().context.get('fiscalyear'): + fiscalyear_ids = fiscalyear_obj.search([ ('state', '=', 'open'), - ], context=context) + ]) else: - fiscalyear_ids = [context['fiscalyear']] + fiscalyear_ids = [Transaction().context['fiscalyear']] period_ids = [] - for fiscalyear in fiscalyear_obj.browse(cursor, user, fiscalyear_ids, - context=context): + for fiscalyear in fiscalyear_obj.browse(fiscalyear_ids): for period in fiscalyear.periods: period_ids.append(period.id) - act_window_id = model_data_obj.get_id(cursor, user, 'account', - 'act_move_line_form', context=context) - res = act_window_obj.read(cursor, user, act_window_id, context=context) + act_window_id = model_data_obj.get_id('account', 'act_move_line_form') + res = act_window_obj.read(act_window_id) res['pyson_domain'] = [ ('period', 'in', period_ids), ('account', '=', data['id']), ] - if context.get('posted'): + if Transaction().context.get('posted'): res['pyson_domain'].append(('move.state', '=', 'posted')) res['pyson_domain'] = PYSONEncoder().encode(res['pyson_domain']) res['pyson_context'] = PYSONEncoder().encode({ - 'fiscalyear': context.get('fiscalyear'), + 'fiscalyear': Transaction().context.get('fiscalyear'), }) return res @@ -1534,9 +1455,9 @@ class ReconcileLinesWriteOff(ModelView): account = fields.Many2One('account.account', 'Account', required=True, domain=[('kind', '!=', 'view')]) - 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() ReconcileLinesWriteOff() @@ -1570,24 +1491,23 @@ class ReconcileLines(Wizard): }, } - def _check_writeoff(self, cursor, user, data, context=None): + def _check_writeoff(self, data): line_obj = self.pool.get('account.move.line') currency_obj = self.pool.get('currency.currency') company = None amount = Decimal('0.0') - for line in line_obj.browse(cursor, user, data['ids'], - context=context): + for line in line_obj.browse(data['ids']): amount += line.debit - line.credit if not company: company = line.account.company if not company: return 'end' - if currency_obj.is_zero(cursor, user, company.currency, amount): + if currency_obj.is_zero(company.currency, amount): return 'reconcile' return 'writeoff' - def _reconcile(self, cursor, user, data, context=None): + def _reconcile(self, data): line_obj = self.pool.get('account.move.line') if data['form']: @@ -1598,8 +1518,7 @@ class ReconcileLines(Wizard): journal_id = False date = False account_id = False - line_obj.reconcile(cursor, user, data['ids'], journal_id, date, - account_id, context=context) + line_obj.reconcile(data['ids'], journal_id, date, account_id) return {} ReconcileLines() @@ -1636,16 +1555,15 @@ class UnreconcileLines(Wizard): }, } - def _unreconcile(self, cursor, user, data, context=None): + def _unreconcile(self, data): line_obj = self.pool.get('account.move.line') reconciliation_obj = self.pool.get('account.move.reconciliation') - lines = line_obj.browse(cursor, user, data['ids'], context=context) + lines = line_obj.browse(data['ids']) reconciliation_ids = [x.reconciliation.id for x in lines \ if x.reconciliation] if reconciliation_ids: - reconciliation_obj.delete(cursor, user, reconciliation_ids, - context=context) + reconciliation_obj.delete(reconciliation_ids) return {} UnreconcileLines() @@ -1684,12 +1602,11 @@ class OpenReconcileLines(Wizard): }, } - def _action_open_reconcile_lines(self, cursor, user, data, context=None): + def _action_open_reconcile_lines(self, data): model_data_obj = self.pool.get('ir.model.data') act_window_obj = self.pool.get('ir.action.act_window') - act_window_id = model_data_obj.get_id(cursor, user, 'account', - 'act_move_line_form', context=context) - res = act_window_obj.read(cursor, user, act_window_id, context=context) + act_window_id = model_data_obj.get_id('account', 'act_move_line_form') + res = act_window_obj.read(act_window_id) res['pyson_domain'] = PYSONEncoder().encode([ ('account', '=', data['form']['account']), ('reconciliation', '=', False), @@ -1729,23 +1646,18 @@ class PrintGeneralJournalInit(ModelView): company = fields.Many2One('company.company', 'Company', required=True) posted = fields.Boolean('Posted Move', help='Show only posted move') - def default_from_date(self, cursor, user, context=None): + def default_from_date(self): date_obj = self.pool.get('ir.date') - return datetime.date( - date_obj.today(cursor, user, context=context).year, 1, 1) + return datetime.date(date_obj.today().year, 1, 1) - def default_to_date(self, cursor, user, context=None): + def default_to_date(self): date_obj = self.pool.get('ir.date') - return date_obj.today(cursor, user, context=context) + return date_obj.today() - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] - return False + def default_company(self): + return Transaction().context.get('company') or False - def default_posted(self, cursor, user, context=None): + def default_posted(self): return False PrintGeneralJournalInit() @@ -1780,7 +1692,7 @@ PrintGeneralJournal() class GeneralJournal(Report): _name = 'account.move.general_journal' - def _get_objects(self, cursor, user, ids, model, datas, context): + def _get_objects(self, ids, model, datas): move_obj = self.pool.get('account.move') clause = [ @@ -1789,26 +1701,21 @@ class GeneralJournal(Report): ] if datas['form']['posted']: clause.append(('state', '=', 'posted')) - move_ids = move_obj.search(cursor, user, clause, - order=[('date', 'ASC'), ('reference', 'ASC'), ('id', 'ASC')], - context=context) - return move_obj.browse(cursor, user, move_ids, context=context) + move_ids = move_obj.search(clause, + order=[('date', 'ASC'), ('reference', 'ASC'), ('id', 'ASC')]) + return move_obj.browse(move_ids) - def parse(self, cursor, user, report, objects, datas, context): - if context is None: - context = {} + def parse(self, report, objects, datas, localcontext): company_obj = self.pool.get('company.company') - context = context.copy() - company = company_obj.browse(cursor, user, - datas['form']['company'], context=context) + company = company_obj.browse(datas['form']['company']) - context['company'] = company - context['digits'] = company.currency.digits - context['from_date'] = datas['form']['from_date'] - context['to_date'] = datas['form']['to_date'] + localcontext['company'] = company + localcontext['digits'] = company.currency.digits + localcontext['from_date'] = datas['form']['from_date'] + localcontext['to_date'] = datas['form']['to_date'] - return super(GeneralJournal, self).parse(cursor, user, report, - objects, datas, context) + return super(GeneralJournal, self).parse(report, objects, datas, + localcontext) GeneralJournal() diff --git a/party.py b/party.py index 208e4d6fec..1516600a64 100644 --- a/party.py +++ b/party.py @@ -1,8 +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. +from decimal import Decimal from trytond.model import ModelView, ModelSQL, fields from trytond.pyson import Get, Eval, And, Bool, Not, Or -from decimal import Decimal +from trytond.transaction import Transaction class Party(ModelSQL, ModelView): @@ -44,17 +45,12 @@ class Party(ModelSQL, ModelView): payable_today = fields.Function(fields.Numeric('Payable Today'), 'get_receivable_payable', searcher='search_receivable_payable') - def get_receivable_payable(self, cursor, user_id, ids, names, - context=None): + def get_receivable_payable(self, ids, names): ''' Function to compute receivable, payable (today or not) for party ids. - :param cursor: the database cursor - :param user: the user id :param ids: the ids of the party :param names: the list of field name to compute - :param arg: optional argument - :param context: the context :return: a dictionary with all field names as key and a dictionary as value with id as key ''' @@ -63,9 +59,7 @@ class Party(ModelSQL, ModelView): company_obj = self.pool.get('company.company') user_obj = self.pool.get('res.user') date_obj = self.pool.get('ir.date') - - if context is None: - context = {} + cursor = Transaction().cursor for name in names: if name not in ('receivable', 'payable', @@ -77,13 +71,13 @@ class Party(ModelSQL, ModelView): return {} company_id = None - user = user_obj.browse(cursor, user_id, user_id, context=context) - if context.get('company'): - child_company_ids = company_obj.search(cursor, user_id, [ + user = user_obj.browse(Transaction().user) + if Transaction().context.get('company'): + child_company_ids = company_obj.search([ ('parent', 'child_of', [user.main_company.id]), - ], order=[], context=context) - if context['company'] in child_company_ids: - company_id = context['company'] + ], order=[]) + if Transaction().context['company'] in child_company_ids: + company_id = Transaction().context['company'] if not company_id: company_id = user.company.id or user.main_company.id @@ -91,7 +85,7 @@ class Party(ModelSQL, ModelView): if not company_id: return res - line_query, _ = move_line_obj.query_get(cursor, user_id, context=context) + line_query, _ = move_line_obj.query_get() for name in names: code = name @@ -101,7 +95,7 @@ class Party(ModelSQL, ModelView): code = name[:-6] today_query = 'AND (l.maturity_date <= %s ' \ 'OR l.maturity_date IS NULL) ' - today_value = [date_obj.today(cursor, user, context=context)] + today_value = [date_obj.today()] cursor.execute('SELECT l.party, ' \ 'SUM((COALESCE(l.debit, 0) - COALESCE(l.credit, 0))) ' \ @@ -125,28 +119,25 @@ class Party(ModelSQL, ModelView): res[name][party_id] = sum return res - def search_receivable_payable(self, cursor, user_id, name, clause, - context=None): + def search_receivable_payable(self, name, clause): move_line_obj = self.pool.get('account.move.line') company_obj = self.pool.get('company.company') user_obj = self.pool.get('res.user') date_obj = self.pool.get('ir.date') - - if context is None: - context = {} + cursor = Transaction().cursor if name not in ('receivable', 'payable', 'receivable_today', 'payable_today'): raise Exception('Bad argument') company_id = None - user = user_obj.browse(cursor, user_id, user_id, context=context) - if context.get('company'): - child_company_ids = company_obj.search(cursor, user_id, [ + user = user_obj.browse(Transaction().user) + if Transaction().context.get('company'): + child_company_ids = company_obj.search([ ('parent', 'child_of', [user.main_company.id]), - ], context=context) - if context['company'] in child_company_ids: - company_id = context['company'] + ]) + if Transaction().context['company'] in child_company_ids: + company_id = Transaction().context['company'] if not company_id: company_id = user.company.id or user.main_company.id @@ -161,9 +152,9 @@ class Party(ModelSQL, ModelView): code = name[:-6] today_query = 'AND (l.maturity_date <= %s ' \ 'OR l.maturity_date IS NULL) ' - today_value = [date_obj.today(cursor, user, context=context)] + today_value = [date_obj.today()] - line_query, _ = move_line_obj.query_get(cursor, user_id, context=context) + line_query, _ = move_line_obj.query_get() cursor.execute('SELECT l.party ' 'FROM account_move_line AS l, ' diff --git a/period.py b/period.py index 1c7c84e04b..de935b8285 100644 --- a/period.py +++ b/period.py @@ -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. -'Period' from trytond.model import ModelView, ModelSQL, fields, OPERATORS from trytond.wizard import Wizard from trytond.pyson import Equal, Eval +from trytond.transaction import Transaction _STATES = { 'readonly': Equal(Eval('state'), 'close'), @@ -66,14 +66,15 @@ class Period(ModelSQL, ModelView): 'the fiscal year dates', }) - def default_state(self, cursor, user, context=None): + def default_state(self): return 'open' - def default_type(self, cursor, user, context=None): + def default_type(self): return 'standard' - def check_dates(self, cursor, user, ids): - for period in self.browse(cursor, user, ids): + def check_dates(self, ids): + cursor = Transaction().cursor + for period in self.browse(ids): if period.type != 'standard': continue cursor.execute('SELECT id ' \ @@ -92,16 +93,16 @@ class Period(ModelSQL, ModelView): return False return True - def check_fiscalyear_dates(self, cursor, user, ids): - for period in self.browse(cursor, user, ids): + def check_fiscalyear_dates(self, ids): + for period in self.browse(ids): if period.start_date < period.fiscalyear.start_date \ or period.end_date > period.fiscalyear.end_date: return False return True - def check_post_move_sequence(self, cursor, user, ids): - for period in self.browse(cursor, user, ids): - if self.search(cursor, user, [ + def check_post_move_sequence(self, ids): + for period in self.browse(ids): + if self.search([ ('post_move_sequence', '=', period.post_move_sequence.id), ('fiscalyear', '!=', period.fiscalyear.id), ]): @@ -112,27 +113,23 @@ class Period(ModelSQL, ModelView): return False return True - def find(self, cursor, user, company_id, date=None, exception=True, - test_state=True, context=None): + def find(self, company_id, date=None, exception=True, test_state=True): ''' Return the period for the company_id at the date or the current date. If exception is set the function will raise an exception if any period is found. - :param cursor: the database cursor - :param user: the user id :param company_id: the company id :param date: the date searched :param exception: a boolean to raise or not an exception :param test_state: a boolean if true will search on non-closed periods - :param context: the context :return: the period id found or False ''' date_obj = self.pool.get('ir.date') if not date: - date = date_obj.today(cursor, user, context=context) + date = date_obj.today() clause = [ ('start_date', '<=', date), ('end_date', '>=', date), @@ -141,30 +138,27 @@ class Period(ModelSQL, ModelView): ] if test_state: clause.append(('state', '!=', 'close')) - ids = self.search(cursor, user, clause, order=[('start_date', 'DESC')], - limit=1, context=context) + ids = self.search(clause, order=[('start_date', 'DESC')], limit=1) if not ids: if exception: - self.raise_user_error(cursor, 'no_period_date', - context=context) + self.raise_user_error('no_period_date') else: return False return ids[0] - def _check(self, cursor, user, ids, context=None): + def _check(self, ids): move_obj = self.pool.get('account.move') if isinstance(ids, (int, long)): ids = [ids] - move_ids = move_obj.search(cursor, user, [ + move_ids = move_obj.search([ ('period', 'in', ids), - ], limit=1, context=context) + ], limit=1) if move_ids: - self.raise_user_error(cursor, 'modify_del_period_moves', - context=context) + self.raise_user_error('modify_del_period_moves') return - def search(self, cursor, user, args, offset=0, limit=None, order=None, - context=None, count=False, query_string=False): + def search(self, args, offset=0, limit=None, order=None, count=False, + query_string=False): args = args[:] def process_args(args): i = 0 @@ -178,85 +172,73 @@ class Period(ModelSQL, ModelView): if not args[i][2][0]: args[i] = ('id', '!=', '0') else: - period = self.browse(cursor, user, args[i][2][0], - context=context) + period = self.browse(args[i][2][0]) args[i] = (args[i][0], args[i][1], period[args[i][2][1]]) elif isinstance(args[i], list): process_args(args[i]) i += 1 process_args(args) - return super(Period, self).search(cursor, user, args, offset=offset, - limit=limit, order=order, context=context, count=count, - query_string=query_string) + return super(Period, self).search(args, offset=offset, limit=limit, + order=order, count=count, query_string=query_string) - def create(self, cursor, user, vals, context=None): + def create(self, vals): fiscalyear_obj = self.pool.get('account.fiscalyear') vals = vals.copy() if vals.get('fiscalyear'): - fiscalyear = fiscalyear_obj.browse(cursor, user, vals['fiscalyear'], - context=context) + fiscalyear = fiscalyear_obj.browse(vals['fiscalyear']) if fiscalyear.state == 'close': - self.raise_user_error(cursor, - 'create_period_closed_fiscalyear', context=context) + self.raise_user_error('create_period_closed_fiscalyear') if not vals.get('post_move_sequence'): vals['post_move_sequence'] = fiscalyear.post_move_sequence.id - return super(Period, self).create(cursor, user, vals, context=context) + return super(Period, self).create(vals) - def write(self, cursor, user, ids, vals, context=None): + def write(self, ids, vals): move_obj = self.pool.get('account.move') for key in vals.keys(): if key in ('start_date', 'end_date', 'fiscalyear'): - self._check(cursor, user, ids, context=context) + self._check(ids) break if vals.get('state') == 'open': - for period in self.browse(cursor, user, ids, - context=context): + for period in self.browse(ids): if period.fiscalyear.state == 'close': - self.raise_user_error(cursor, - 'open_period_closed_fiscalyear', context=context) + self.raise_user_error('open_period_closed_fiscalyear') if vals.get('post_move_sequence'): - for period in self.browse(cursor, user, ids, context=context): + for period in self.browse(ids): if period.post_move_sequence and \ period.post_move_sequence.id != \ vals['post_move_sequence']: - if move_obj.search(cursor, user, [ + if move_obj.search([ ('period', '=', period.id), ('state', '=', 'posted'), - ], context=context): - self.raise_user_error(cursor, - 'change_post_move_sequence', context=context) - return super(Period, self).write(cursor, user, ids, vals, - context=context) + ]): + self.raise_user_error('change_post_move_sequence') + return super(Period, self).write(ids, vals) - def delete(self, cursor, user, ids, context=None): - self._check(cursor, user, ids, context=context) - return super(Period, self).delete(cursor, user, ids, - context=context) + def delete(self, ids): + self._check(ids) + return super(Period, self).delete(ids) - def close(self, cursor, user, ids, context=None): + def close(self, ids): journal_period_obj = self.pool.get('account.journal.period') move_obj = self.pool.get('account.move') if isinstance(ids, (int, long)): ids = [ids] - if move_obj.search(cursor, user, [ + if move_obj.search([ ('period', 'in', ids), ('state', '!=', 'posted'), - ], context=context): - self.raise_user_error(cursor, 'close_period_non_posted_move', - context=context) + ]): + self.raise_user_error('close_period_non_posted_move') #First close the period to be sure #it will not have new journal.period created between. - self.write(cursor, user, ids, { + self.write(ids, { 'state': 'close', - }, context=context) - journal_period_ids = journal_period_obj.search(cursor, user, [ + }) + journal_period_ids = journal_period_obj.search([ ('period', 'in', ids), - ], context=context) - journal_period_obj.close(cursor, user, journal_period_ids, - context=context) - return + ]) + journal_period_obj.close(journal_period_ids) Period() @@ -274,9 +256,9 @@ class ClosePeriod(Wizard): }, } - def _close(self, cursor, user, data, context=None): + def _close(self, data): period_obj = self.pool.get('account.period') - period_obj.close(cursor, user, data['ids'], context=context) + period_obj.close(data['ids']) return {} ClosePeriod() @@ -295,11 +277,11 @@ class ReOpenPeriod(Wizard): }, } - def _reopen(self, cursor, user, data, context=None): + def _reopen(self, data): period_obj = self.pool.get('account.period') - period_obj.write(cursor, user, data['ids'], { + period_obj.write(data['ids'], { 'state': 'open', - }, context=context) + }) return {} ReOpenPeriod() diff --git a/tax.py b/tax.py index c6514d46fe..c02ba2e0eb 100644 --- a/tax.py +++ b/tax.py @@ -1,13 +1,14 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -"Tax" +from __future__ import with_statement +from decimal import Decimal from trytond.model import ModelView, ModelSQL, fields from trytond.wizard import Wizard -from decimal import Decimal from trytond.tools import Cache from trytond.backend import TableHandler from trytond.pyson import Eval, Not, Equal, If, In, Bool, Get, Or, And, \ PYSONEncoder +from trytond.transaction import Transaction class Group(ModelSQL, ModelView): @@ -18,8 +19,9 @@ class Group(ModelSQL, ModelView): name = fields.Char('Name', size=None, required=True, translate=True) code = fields.Char('Code', size=None, required=True) - def init(self, cursor, module_name): - super(Group, self).init(cursor, module_name) + def init(self, module_name): + super(Group, self).init(module_name) + cursor = Transaction().cursor table = TableHandler(cursor, self, module_name) # Migration from 1.4 drop code_uniq constraint @@ -52,15 +54,11 @@ class CodeTemplate(ModelSQL, ModelView): self._order.insert(0, ('code', 'ASC')) self._order.insert(0, ('account', 'ASC')) - def _get_tax_code_value(self, cursor, user, template, context=None, - code=None): + def _get_tax_code_value(self, template, code=None): ''' Set values for tax code creation. - :param cursor: the database cursor - :param user: the user id :param template: the BrowseRecord of the template - :param context: the context :param code: the BrowseRecord of the code to update :return: a dictionary with tax code fields as key and values as value ''' @@ -75,18 +73,15 @@ class CodeTemplate(ModelSQL, ModelView): res['template'] = template.id return res - def create_tax_code(self, cursor, user, template, company_id, context=None, - template2tax_code=None, parent_id=False): + def create_tax_code(self, template, company_id, template2tax_code=None, + parent_id=False): ''' Create recursively tax codes based on template. - :param cursor: the database cursor - :param user: the user id :param template: the template id or the BrowseRecord of template used for tax code creation :param company_id: the id of the company for which tax codes are created - :param context: the context :param template2tax_code: a dictionary with tax code template id as key and tax code id as value, used to convert template id into tax code. The dictionary is filled with new tax codes @@ -101,27 +96,23 @@ class CodeTemplate(ModelSQL, ModelView): template2tax_code = {} if isinstance(template, (int, long)): - template = self.browse(cursor, user, template, context=context) + template = self.browse(template) if template.id not in template2tax_code: - vals = self._get_tax_code_value(cursor, user, template, - context=context) + vals = self._get_tax_code_value(template) vals['company'] = company_id vals['parent'] = parent_id - new_id = tax_code_obj.create(cursor, user, vals, context=context) + new_id = tax_code_obj.create(vals) prev_lang = template._context.get('language') or 'en_US' prev_data = {} for field_name, field in template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang template.setLang(lang) data = {} for field_name, field in template._columns.iteritems(): @@ -129,7 +120,8 @@ class CodeTemplate(ModelSQL, ModelView): and template[field_name] != prev_data[field_name]: data[field_name] = template[field_name] if data: - tax_code_obj.write(cursor, user, new_id, data, context=ctx) + with Transaction().set_context(language=lang): + tax_code_obj.write(new_id, data) template.setLang(prev_lang) template2tax_code[template.id] = new_id else: @@ -137,8 +129,7 @@ class CodeTemplate(ModelSQL, ModelView): new_childs = [] for child in template.childs: - new_childs.append(self.create_tax_code(cursor, user, child, - company_id, context=context, + new_childs.append(self.create_tax_code(child, company_id, template2tax_code=template2tax_code, parent_id=new_id)) return new_id @@ -177,39 +168,34 @@ class Code(ModelSQL, ModelView): }) self._order.insert(0, ('code', 'ASC')) - def default_active(self, cursor, user, context=None): + def default_active(self): return True - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] - return False + def default_company(self): + return Transaction().context.get('company') or False - def on_change_with_currency_digits(self, cursor, user, vals, context=None): + def on_change_with_currency_digits(self, vals): company_obj = self.pool.get('company.company') if vals.get('company'): - company = company_obj.browse(cursor, user, vals['company'], - context=context) + company = company_obj.browse(vals['company']) return company.currency.digits return 2 - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for code in self.browse(cursor, user, ids, context=context): + for code in self.browse(ids): res[code.id] = code.company.currency.digits return res - def get_sum(self, cursor, user, ids, name, context=None): + def get_sum(self, ids, name): + cursor = Transaction().cursor res = {} move_line_obj = self.pool.get('account.move.line') currency_obj = self.pool.get('currency.currency') - child_ids = self.search(cursor, user, [('parent', 'child_of', ids)], - context=context) + child_ids = self.search([('parent', 'child_of', ids)]) all_ids = {}.fromkeys(ids + child_ids).keys() - line_query, _ = move_line_obj.query_get(cursor, user, context=context) + line_query, _ = move_line_obj.query_get() cursor.execute('SELECT c.id, ' \ 'SUM(tl.amount) ' \ 'FROM account_tax_code c, ' \ @@ -229,55 +215,48 @@ class Code(ModelSQL, ModelView): sum = Decimal(str(sum)) code_sum[code_id] = sum - for code in self.browse(cursor, user, ids, context=context): + for code in self.browse(ids): res.setdefault(code.id, Decimal('0.0')) - child_ids = self.search(cursor, user, [ + child_ids = self.search([ ('parent', 'child_of', [code.id]), - ], context=context) + ]) for child_id in child_ids: - res[code.id] += currency_obj.round(cursor, user, - code.company.currency, + res[code.id] += currency_obj.round(code.company.currency, code_sum.get(child_id, Decimal('0.0'))) - res[code.id] = currency_obj.round(cursor, user, - code.company.currency, res[code.id]) + res[code.id] = currency_obj.round(code.company.currency, + res[code.id]) return res - def get_rec_name(self, cursor, user, ids, name, context=None): + def get_rec_name(self, ids, name): if not ids: return {} res = {} - for code in self.browse(cursor, user, ids, context=context): + for code in self.browse(ids): if code.code: res[code.id] = code.code + ' - ' + code.name else: res[code.id] = code.name return res - def search_rec_name(self, cursor, user, name, clause, context=None): - ids = self.search(cursor, user, [('code',) + clause[1:]], limit=1, - order=[], context=context) + def search_rec_name(self, name, clause): + ids = self.search([('code',) + clause[1:]], limit=1, order=[]) if ids: return [('code',) + clause[1:]] return [('name',) + clause[1:]] - def delete(self, cursor, user, ids, context=None): + def delete(self, ids): if isinstance(ids, (int, long)): ids = [ids] - code_ids = self.search(cursor, user, [ + code_ids = self.search([ ('parent', 'child_of', ids), - ], context=context) - return super(Code, self).delete(cursor, user, code_ids, - context=context) + ]) + return super(Code, self).delete(code_ids) - def update_tax_code(self, cursor, user, code, context=None, - template2tax_code=None): + def update_tax_code(self, code, template2tax_code=None): ''' Update recursively tax code based on template. - :param cursor: the database cursor - :param user: the user id :param code: a code id or the BrowseRecord of the code - :param context: the context :param template2tax_code: a dictionary with tax code template id as key and tax code id as value, used to convert template id into tax code. The dictionary is filled with new tax codes @@ -289,25 +268,21 @@ class Code(ModelSQL, ModelView): template2tax_code = {} if isinstance(code, (int, long)): - code = self.browse(cursor, user, code, context=context) + code = self.browse(code) if code.template: - vals = template_obj._get_tax_code_value(cursor, user, - code.template, context=context, code=code) + vals = template_obj._get_tax_code_value(code.template, code=code) if vals: - self.write(cursor, user, code.id, vals, context=context) + self.write(code.id, vals) prev_lang = code._context.get('language') or 'en_US' prev_data = {} for field_name, field in code.template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = code.template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang code.setLang(lang) data = {} for field_name, field in code.template._columns.iteritems(): @@ -316,13 +291,13 @@ class Code(ModelSQL, ModelView): prev_data[field_name]): data[field_name] = code.template[field_name] if data: - self.write(cursor, user, code.id, data, context=ctx) + with Transaction().set_context(language=lang): + self.write(code.id, data) code.setLang(prev_lang) template2tax_code[code.template.id] = code.id for child in code.childs: - self.update_tax_code(cursor, user, child, context=context, - template2tax_code=template2tax_code) + self.update_tax_code(child, template2tax_code=template2tax_code) Code() @@ -346,7 +321,7 @@ class OpenChartCodeInit(ModelView): 'invisible': Not(Equal(Eval('method'), 'periods')), }, depends=['method']) - def default_method(self, cursor, user, context=None): + def default_method(self): return 'periods' OpenChartCodeInit() @@ -375,12 +350,11 @@ class OpenChartCode(Wizard): }, } - def _action_open_chart(self, cursor, user, data, context=None): + def _action_open_chart(self, data): model_data_obj = self.pool.get('ir.model.data') act_window_obj = self.pool.get('ir.action.act_window') - act_window_id = model_data_obj.get_id(cursor, user, 'account', - 'act_tax_code_tree2', context=context) - res = act_window_obj.read(cursor, user, act_window_id, context=context) + act_window_id = model_data_obj.get_id('account', 'act_tax_code_tree2') + res = act_window_obj.read(act_window_id) if data['form']['method'] == 'fiscalyear': res['pyson_context'] = PYSONEncoder().encode({ 'fiscalyear': data['form']['fiscalyear'], @@ -436,40 +410,37 @@ class TaxTemplate(ModelSQL, ModelView): self._order.insert(0, ('sequence', 'ASC')) self._order.insert(0, ('account', 'ASC')) - def init(self, cursor, module_name): - super(TaxTemplate, self).init(cursor, module_name) + def init(self, module_name): + super(TaxTemplate, self).init(module_name) + cursor = Transaction().cursor table = TableHandler(cursor, self, module_name) # Migration from 1.0 group is no more required table.not_null_action('group', action='remove') - def default_type(self, cursor, user, context=None): + def default_type(self): return 'percentage' - def default_include_base_amount(self, cursor, user, context=None): + def default_include_base_amount(self): return False - def default_invoice_base_sign(self, cursor, user, context=None): + def default_invoice_base_sign(self): return Decimal('1') - def default_invoice_tax_sign(self, cursor, user, context=None): + def default_invoice_tax_sign(self): return Decimal('1') - def default_credit_note_base_sign(self, cursor, user, context=None): + def default_credit_note_base_sign(self): return Decimal('1') - def default_credit_note_tax_sign(self, cursor, user, context=None): + def default_credit_note_tax_sign(self): return Decimal('1') - def _get_tax_value(self, cursor, user, template, context=None, - tax=None): + def _get_tax_value(self, template, tax=None): ''' Set values for tax creation. - :param cursor: the database cursor - :param user: the user id :param template: the BrowseRecord of the template - :param context: the context :param tax: the BrowseRecord of the tax to update :return: a dictionary with account fields as key and values as value ''' @@ -486,14 +457,11 @@ class TaxTemplate(ModelSQL, ModelView): res['template'] = template.id return res - def create_tax(self, cursor, user, template, company_id, - template2tax_code, template2account, context=None, - template2tax=None, parent_id=False): + def create_tax(self, template, company_id, template2tax_code, + template2account, template2tax=None, parent_id=False): ''' Create recursively taxes based on template. - :param cursor: the database cursor - :param user: the user id :param template: the template id or the BrowseRecord of template used for tax creation :param company_id: the id of the company for which taxes are created @@ -503,7 +471,6 @@ class TaxTemplate(ModelSQL, ModelView): :param template2account: a dictionary with account template id as key and account id as value, used to convert account template into account code - :param context: the context :param template2tax: a dictionary with tax template id as key and tax id as value, used to convert template id into tax. The dictionary is filled with new taxes @@ -518,10 +485,10 @@ class TaxTemplate(ModelSQL, ModelView): template2tax = {} if isinstance(template, (int, long)): - template = self.browse(cursor, user, template, context=context) + template = self.browse(template) if template.id not in template2tax: - vals = self._get_tax_value(cursor, user, template, context=context) + vals = self._get_tax_value(template) vals['company'] = company_id vals['parent'] = parent_id if template.invoice_account: @@ -555,19 +522,16 @@ class TaxTemplate(ModelSQL, ModelView): else: vals['credit_note_tax_code'] = False - new_id = tax_obj.create(cursor, user, vals, context=context) + new_id = tax_obj.create(vals) prev_lang = template._context.get('language') or 'en_US' prev_data = {} for field_name, field in template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang template.setLang(lang) data = {} for field_name, field in template._columns.iteritems(): @@ -575,7 +539,8 @@ class TaxTemplate(ModelSQL, ModelView): and template[field_name] != prev_data[field_name]: data[field_name] = template[field_name] if data: - tax_obj.write(cursor, user, new_id, data, context=ctx) + with Transaction().set_context(language=lang): + tax_obj.write(new_id, data) template.setLang(prev_lang) template2tax[template.id] = new_id else: @@ -583,9 +548,9 @@ class TaxTemplate(ModelSQL, ModelView): new_childs = [] for child in template.childs: - new_childs.append(self.create_tax(cursor, user, child, - company_id, template2tax_code, template2account, - context=context, template2tax=template2tax, parent_id=new_id)) + new_childs.append(self.create_tax(child, company_id, + template2tax_code, template2account, template2tax=template2tax, + parent_id=new_id)) return new_id TaxTemplate() @@ -704,56 +669,52 @@ class Tax(ModelSQL, ModelView): super(Tax, self).__init__() self._order.insert(0, ('sequence', 'ASC')) - def init(self, cursor, module_name): - super(Tax, self).init(cursor, module_name) + def init(self, module_name): + super(Tax, self).init(module_name) + cursor = Transaction().cursor table = TableHandler(cursor, self, module_name) # Migration from 1.0 group is no more required table.not_null_action('group', action='remove') - def default_active(self, cursor, user, context=None): + def default_active(self): return True - def default_type(self, cursor, user, context=None): + def default_type(self): return 'percentage' - def default_include_base_amount(self, cursor, user, context=None): + def default_include_base_amount(self): return False - def default_invoice_base_sign(self, cursor, user, context=None): + def default_invoice_base_sign(self): return Decimal('1') - def default_invoice_tax_sign(self, cursor, user, context=None): + def default_invoice_tax_sign(self): return Decimal('1') - def default_credit_note_base_sign(self, cursor, user, context=None): + def default_credit_note_base_sign(self): return Decimal('1') - def default_credit_note_tax_sign(self, cursor, user, context=None): + def default_credit_note_tax_sign(self): return Decimal('1') - def default_company(self, cursor, user, context=None): - if context is None: - context = {} - if context.get('company'): - return context['company'] - return False + def default_company(self): + return Transaction().context.get('company') or False - def on_change_with_currency_digits(self, cursor, user, vals, context=None): + def on_change_with_currency_digits(self, vals): company_obj = self.pool.get('company.company') if vals.get('company'): - company = company_obj.browse(cursor, user, vals['company'], - context=context) + company = company_obj.browse(vals['company']) return company.currency.digits return 2 - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for tax in self.browse(cursor, user, ids, context=context): + for tax in self.browse(ids): res[tax.id] = tax.company.currency.digits return res - def _process_tax(self, cursor, user, tax, price_unit, context=None): + def _process_tax(self, tax, price_unit): if tax.type == 'percentage': amount = price_unit * tax.percentage / Decimal('100') return { @@ -769,48 +730,43 @@ class Tax(ModelSQL, ModelView): 'tax': tax, } - def _unit_compute(self, cursor, user, taxes, price_unit, context=None): + def _unit_compute(self, taxes, price_unit): res = [] for tax in taxes: if tax.type != 'none': - res.append(self._process_tax(cursor, user, tax, price_unit, - context=context)) + res.append(self._process_tax(tax, price_unit)) if len(tax.childs): - res.extend(self._unit_compute(cursor, user, tax.childs, - price_unit, context=context)) + res.extend(self._unit_compute(tax.childs, price_unit)) return res - def delete(self, cursor, user, ids, context=None): + def delete(self, ids): # Restart the cache - self.sort_taxes(cursor.dbname) - return super(Tax, self).delete(cursor, user, ids, context=context) + self.sort_taxes.reset() + return super(Tax, self).delete(ids) - def create(self, cursor, user, vals, context=None): + def create(self, vals): # Restart the cache - self.sort_taxes(cursor.dbname) - return super(Tax, self).create(cursor, user, vals, context=context) + self.sort_taxes.reset() + return super(Tax, self).create(vals) - def write(self, cursor, user, ids, vals, context=None): + def write(self, ids, vals): # Restart the cache - self.sort_taxes(cursor.dbname) - return super(Tax, self).write(cursor, user, ids, vals, context=context) + self.sort_taxes.reset() + return super(Tax, self).write(ids, vals) @Cache('account_tax.sort_taxes') - def sort_taxes(self, cursor, user, ids, context=None): + def sort_taxes(self, ids): ''' Return a list of taxe ids sorted - :param cursor: the database cursor - :param user: the user id :param ids: a list of tax ids - :param context: the context :return: a list of tax ids sorted ''' - return self.search(cursor, user, [ + return self.search([ ('id', 'in', ids), - ], order=[('sequence', 'ASC'), ('id', 'ASC')], context=context) + ], order=[('sequence', 'ASC'), ('id', 'ASC')]) - def compute(self, cursor, user, ids, price_unit, quantity, context=None): + def compute(self, ids, price_unit, quantity): ''' Compute taxes for price_unit and quantity. Return list of dict for each taxes and their childs with: @@ -818,23 +774,20 @@ class Tax(ModelSQL, ModelView): amount tax ''' - ids = self.sort_taxes(cursor, user, ids, context=context) - taxes = self.browse(cursor, user, ids, context=context) - res = self._unit_compute(cursor, user, taxes, price_unit, - context=context) + ids = self.sort_taxes(ids) + taxes = self.browse(ids) + res = self._unit_compute(taxes, price_unit) quantity = Decimal(str(quantity or 0.0)) for row in res: row['base'] *= quantity row['amount'] *= quantity return res - def update_tax(self, cursor, user, tax, template2tax_code, - template2account, context=None, template2tax=None): + def update_tax(self, tax, template2tax_code, template2account, + template2tax=None): ''' Update recursively taxes based on template. - :param cursor: the database cursor - :param user: the user id :param tax: a tax id or the BrowseRecord of the tax :param template2tax_code: a dictionary with tax code template id as key and tax code id as value, used to convert tax code template into @@ -842,7 +795,6 @@ class Tax(ModelSQL, ModelView): :param template2account: a dictionary with account template id as key and account id as value, used to convert account template into account code - :param context: the context :param template2tax: a dictionary with tax template id as key and tax id as value, used to convert template id into tax. The dictionary is filled with new taxes @@ -854,11 +806,10 @@ class Tax(ModelSQL, ModelView): template2tax = {} if isinstance(tax, (int, long)): - tax = self.browse(cursor, user, tax, context=context) + tax = self.browse(tax) if tax.template: - vals = template_obj._get_tax_value(cursor, user, tax.template, - context=context, tax=tax) + vals = template_obj._get_tax_value(tax.template, tax=tax) if tax.template.invoice_account \ and tax.invoice_account.id != \ template2account.get(tax.template.invoice_account.id, @@ -921,19 +872,16 @@ class Tax(ModelSQL, ModelView): vals['credit_note_tax_code'] = False if vals: - self.write(cursor, user, tax.id, vals, context=context) + self.write(tax.id, vals) prev_lang = tax._context.get('language') or 'en_US' prev_data = {} for field_name, field in tax.template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = tax.template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang tax.setLang(lang) data = {} for field_name, field in tax.template._columns.iteritems(): @@ -942,13 +890,14 @@ class Tax(ModelSQL, ModelView): prev_data[field_name]): data[field_name] = tax.template[field_name] if data: - self.write(cursor, user, tax.id, data, context=ctx) + with Transaction().set_context(language=lang): + self.write(tax.id, data) tax.setLang(prev_lang) template2tax[tax.template.id] = tax.id for child in tax.childs: - self.update_tax(cursor, user, child, template2tax_code, - template2account, context=context, template2tax=template2tax) + self.update_tax(child, template2tax_code, template2account, + template2tax=template2tax) Tax() @@ -969,21 +918,20 @@ class Line(ModelSQL, ModelView): move_line = fields.Many2One('account.move.line', 'Move Line', required=True, select=1, ondelete='CASCADE') - def on_change_with_currency_digits(self, cursor, user, vals, context=None): + def on_change_with_currency_digits(self, vals): move_line_obj = self.pool.get('account.move.line') if vals.get('move_line'): - move_line = move_line_obj.browse(cursor, user, vals['move_line'], - context=context) + move_line = move_line_obj.browse(vals['move_line']) return move_line.currency_digits return 2 - def get_currency_digits(self, cursor, user, ids, name, context=None): + def get_currency_digits(self, ids, name): res = {} - for line in self.browse(cursor, user, ids, context=context): + for line in self.browse(ids): res[line.id] = line.move_line.currency_digits return res - def on_change_tax(self, cursor, user, vals, context=None): + def on_change_tax(self, vals): res = { 'code': False, } @@ -1001,15 +949,11 @@ class RuleTemplate(ModelSQL, ModelView): account = fields.Many2One('account.account.template', 'Account Template', domain=[('parent', '=', False)], required=True) - def _get_tax_rule_value(self, cursor, user, template, context=None, - rule=None): + def _get_tax_rule_value(self, template, rule=None): ''' Set values for tax rule creation. - :param cursor: the database cursor - :param user: the user id :param template: the BrowseRecord of the template - :param context: the context :param rule: the BrowseRecord of the rule to update :return: a dictionary with rule fields as key and values as value ''' @@ -1020,18 +964,14 @@ class RuleTemplate(ModelSQL, ModelView): res['template'] = template.id return res - def create_rule(self, cursor, user, template, company_id, context=None, - template2rule=None): + def create_rule(self, template, company_id, template2rule=None): ''' Create tax rule based on template. - :param cursor: the database cursor - :param user: the user id :param template: the template id or the BrowseRecord of template used for tax rule creation :param company_id: the id of the company for which tax rules are created - :param context: the context :param template2rule: a dictionary with tax rule template id as key and tax rule id as value, used to convert template id into tax rule. The dictionary is filled with new tax rules @@ -1044,25 +984,21 @@ class RuleTemplate(ModelSQL, ModelView): template2rule = {} if isinstance(template, (int, long)): - template = self.browse(cursor, user, template, context=context) + template = self.browse(template) if template.id not in template2rule: - vals = self._get_tax_rule_value(cursor, user, template, - context=context) + vals = self._get_tax_rule_value(template) vals['company'] = company_id - new_id = rule_obj.create(cursor, user, vals, context=context) + new_id = rule_obj.create(vals) prev_lang = template._context.get('language') or 'en_US' prev_data = {} for field_name, field in template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang template.setLang(lang) data = {} for field_name, field in template._columns.iteritems(): @@ -1070,7 +1006,8 @@ class RuleTemplate(ModelSQL, ModelView): and template[field_name] != prev_data[field_name]: data[field_name] = template[field_name] if data: - rule_obj.write(cursor, user, new_id, data, context=ctx) + with Transaction().set_context(language=lang): + rule_obj.write(new_id, data) template.setLang(prev_lang) template2rule[template.id] = new_id else: @@ -1093,47 +1030,39 @@ class Rule(ModelSQL, ModelView): lines = fields.One2Many('account.tax.rule.line', 'rule', 'Lines') template = fields.Many2One('account.tax.rule.template', 'Template') - def apply(self, cursor, user, rule, tax, pattern, context=None): + def apply(self, rule, tax, pattern): ''' Apply rule on tax - :param cursor: the database cursor - :param user: the user id :param rule: a rule id or the BrowseRecord of the rule :param tax: a tax id or the BrowseRecord of the tax :param pattern: a dictonary with rule line field as key and match value as value - :param context: the context :return: a list of the tax id to use or False ''' tax_obj = self.pool.get('account.tax') rule_line_obj = self.pool.get('account.tax.rule.line') if isinstance(rule, (int, long)) and rule: - rule = self.browse(cursor, user, rule, context=context) + rule = self.browse(rule) if isinstance(tax, (int, long)) and tax: - tax = tax_obj.browse(cursor, user, tax, context=context) + tax = tax_obj.browse(tax) pattern = pattern.copy() pattern['group'] = tax and tax.group.id or False pattern['origin_tax'] = tax and tax.id or False for line in rule.lines: - if rule_line_obj.match(cursor, user, line, pattern, - context=context): - return rule_line_obj.get_taxes(cursor, user, line, - context=context) + if rule_line_obj.match(line, pattern): + return rule_line_obj.get_taxes(line) return tax and [tax.id] or False - def update_rule(self, cursor, user, rule, context=None, template2rule=None): + def update_rule(self, rule, template2rule=None): ''' Update tax rule based on template. - :param cursor: the database cursor - :param user: the user id :param rule: a rule id or the BrowseRecord of the rule - :param context: the context :param template2rule: a dictionary with tax rule template id as key and tax rule id as value, used to convert template id into tax rule. The dictionary is filled with new tax rules @@ -1145,25 +1074,21 @@ class Rule(ModelSQL, ModelView): template2rule = {} if isinstance(rule, (int, long)): - rule = self.browse(cursor, user, rule, context=context) + rule = self.browse(rule) if rule.template: - vals = template_obj._get_tax_rule_value(cursor, user, - rule.template, context=context, rule=rule) + vals = template_obj._get_tax_rule_value(rule.template, rule=rule) if vals: - self.write(cursor, user, rule.id, vals, context=context) + self.write(rule.id, vals) prev_lang = rule._context.get('language') or 'en_US' prev_data = {} for field_name, field in rule.template._columns.iteritems(): if getattr(field, 'translate', False): prev_data[field_name] = rule.template[field_name] - ctx = context.copy() - for lang in lang_obj.get_translatable_languages(cursor, user, - context=context): + for lang in lang_obj.get_translatable_languages(): if lang == prev_lang: continue - ctx['language'] = lang rule.setLang(lang) data = {} for field_name, field in rule.template._columns.iteritems(): @@ -1172,7 +1097,8 @@ class Rule(ModelSQL, ModelView): prev_data[field_name]): data[field_name] = rule.template[field_name] if data: - self.write(cursor, user, rule.id, data, context=ctx) + with Transaction().set_context(language=lang): + self.write(rule.id, data) rule.setLang(prev_lang) template2rule[rule.template.id] = rule.id @@ -1205,15 +1131,11 @@ class RuleLineTemplate(ModelSQL, ModelView): self._order.insert(0, ('rule', 'ASC')) self._order.insert(0, ('sequence', 'ASC')) - def _get_tax_rule_line_value(self, cursor, user, template, context=None, - rule_line=None): + def _get_tax_rule_line_value(self, template, rule_line=None): ''' Set values for tax rule line creation. - :param cursor: the database cursor - :param user: the user id :param template: the BrowseRecord of the template - :param context: the context :param rule_line: the BrowseRecord of the rule line to update :return: a dictionary with rule line fields as key and values as value ''' @@ -1228,13 +1150,11 @@ class RuleLineTemplate(ModelSQL, ModelView): res['template'] = template.id return res - def create_rule_line(self, cursor, user, template, template2tax, - template2rule, context=None, template2rule_line=None): + def create_rule_line(self, template, template2tax, template2rule, + template2rule_line=None): ''' Create tax rule line based on template. - :param cursor: the database cursor - :param user: the user id :param template: the template id or the BrowseRecord of template used for tax rule line creation :param template2tax: a dictionary with tax template id as key @@ -1243,7 +1163,6 @@ class RuleLineTemplate(ModelSQL, ModelView): :param template2rule: a dictionary with tax rule template id as key and tax rule id as value, used to convert template id into tax rule. - :param context: the context :param template2rule_line: a dictionary with tax rule line template id as key and tax rule line id as value, used to convert template id into tax rule line. The dictionary is filled with new @@ -1256,11 +1175,10 @@ class RuleLineTemplate(ModelSQL, ModelView): template2rule_line = {} if isinstance(template, (int, long)): - template = self.browse(cursor, user, template, context=context) + template = self.browse(template) if template.id not in template2rule_line: - vals = self._get_tax_rule_line_value(cursor, user, template, - context=context) + vals = self._get_tax_rule_line_value(template) vals['rule'] = template2rule[template.rule.id] if template.origin_tax: vals['origin_tax'] = template2tax[template.origin_tax.id] @@ -1270,7 +1188,7 @@ class RuleLineTemplate(ModelSQL, ModelView): vals['tax'] = template2tax[template.tax.id] else: vals['tax'] = False - new_id = rule_line_obj.create(cursor, user, vals, context=context) + new_id = rule_line_obj.create(vals) template2rule_line[template.id] = new_id else: new_id = template2rule_line[template.id] @@ -1307,16 +1225,13 @@ class RuleLine(ModelSQL, ModelView): self._order.insert(0, ('rule', 'ASC')) self._order.insert(0, ('sequence', 'ASC')) - def match(self, cursor, user, line, pattern, context=None): + def match(self, line, pattern): ''' Match line on pattern - :param cursor: the database cursor - :param user: the user id :param line: a BrowseRecord of rule line :param pattern: a dictonary with rule line field as key and match value as value - :param context: the context :return: a boolean ''' res = True @@ -1335,27 +1250,22 @@ class RuleLine(ModelSQL, ModelView): break return res - def get_taxes(self, cursor, user, line, context=None): + def get_taxes(self, line): ''' Return list of taxes for a line - :param cursor: the database cursor - :param user: the user id :param line: a BrowseRecord of rule line - :param context: the context :return: a list of tax id ''' if line.tax: return [line.tax.id] return False - def update_rule_line(self, cursor, user, rule_line, template2tax, - template2rule, context=None, template2rule_line=None): + def update_rule_line(self, rule_line, template2tax, template2rule, + template2rule_line=None): ''' Update tax rule line based on template. - :param cursor: the database cursor - :param user: the user id :param rule_line: a rule line id or the BrowseRecord of the rule line :param template2tax: a dictionary with tax template id as key and tax id as value, used to convert template id into @@ -1363,7 +1273,6 @@ class RuleLine(ModelSQL, ModelView): :param template2rule: a dictionary with tax rule template id as key and tax rule id as value, used to convert template id into tax rule. - :param context: the context :param template2rule_line: a dictionary with tax rule line template id as key and tax rule line id as value, used to convert template id into tax rule line. The dictionary is filled with new @@ -1375,11 +1284,11 @@ class RuleLine(ModelSQL, ModelView): template2rule_line = {} if isinstance(rule_line, (int, long)): - rule_line = self.browse(cursor, user, rule_line, context=context) + rule_line = self.browse(rule_line) if rule_line.template: - vals = template_obj._get_tax_rule_line_value(cursor, user, - rule_line.template, context=context, rule_line=rule_line) + vals = template_obj._get_tax_rule_line_value(rule_line.template, + rule_line=rule_line) if rule_line.rule.id != template2rule[rule_line.template.rule.id]: vals['rule'] = template2rule[rule_line.template.rule.id] if rule_line.origin_tax: @@ -1394,7 +1303,7 @@ class RuleLine(ModelSQL, ModelView): template2tax[rule_line.template.tax.id]: vals['tax'] = template2tax[rule_line.template.tax.id] if vals: - self.write(cursor, user, rule_line.id, vals, context=context) + self.write(rule_line.id, vals) template2rule_line[rule_line.template.id] = rule_line.id RuleLine() @@ -1413,39 +1322,36 @@ class OpenCode(Wizard): }, } - def _action_open_code(self, cursor, user, data, context=None): - if context is None: - context = {} + def _action_open_code(self, data): model_data_obj = self.pool.get('ir.model.data') act_window_obj = self.pool.get('ir.action.act_window') fiscalyear_obj = self.pool.get('account.fiscalyear') period_obj = self.pool.get('account.period') - if not context.get('fiscalyear'): - fiscalyear_ids = fiscalyear_obj.search(cursor, user, [ + if not Transaction().context.get('fiscalyear'): + fiscalyear_ids = fiscalyear_obj.search([ ('state', '=', 'open'), - ], context=context) + ]) else: - fiscalyear_ids = [context['fiscalyear']] + fiscalyear_ids = [Transaction().context['fiscalyear']] period_ids = [] - if not context.get('periods'): - period_ids = period_obj.search(cursor, user, [ + if not Transaction().context.get('periods'): + period_ids = period_obj.search([ ('fiscalyear', 'in', fiscalyear_ids), - ], context=context) + ]) else: - period_ids = context['periods'] + period_ids = Transaction().context['periods'] - act_window_id = model_data_obj.get_id(cursor, user, 'account', - 'act_tax_line_form', context=context) - res = act_window_obj.read(cursor, user, act_window_id, context=context) + act_window_id = model_data_obj.get_id('account', 'act_tax_line_form') + res = act_window_obj.read(act_window_id) res['pyson_domain'] = PYSONEncoder().encode([ ('move_line.period', 'in', period_ids), ('code', '=', data['id']), ]) - if context.get('fiscalyear'): + if Transaction().context.get('fiscalyear'): res['pyson_context'] = PYSONEncoder().encode({ - 'fiscalyear': context['fiscalyear'], + 'fiscalyear': Transaction().context['fiscalyear'], }) else: res['pyson_context'] = PYSONEncoder().encode({