From 6bc1aec6bb43d87abd2139363beea2bd88e57505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 5 Dec 2008 17:13:50 +0100 Subject: [PATCH] Add cached sort_taxes method to improve speed of compute taxes --- tax.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/tax.py b/tax.py index f3d63a501a..7b2aba5979 100644 --- a/tax.py +++ b/tax.py @@ -5,6 +5,7 @@ from trytond.osv import fields, OSV from trytond.wizard import Wizard, WizardOSV from decimal import Decimal +from trytond.tools import Cache class Group(OSV): @@ -642,6 +643,37 @@ class Tax(OSV): price_unit, context=context)) return res + def delete(self, cursor, user, ids, context=None): + # Restart the cache + self.sort_taxes(cursor.dbname) + return super(Tax, self).delete(cursor, user, ids, context=context) + + def create(self, cursor, user, vals, context=None): + # Restart the cache + self.sort_taxes(cursor.dbname) + return super(Tax, self).create(cursor, user, vals, context=context) + + def write(self, cursor, user, ids, vals, context=None): + # Restart the cache + self.sort_taxes(cursor.dbname) + return super(Tax, self).write(cursor, user, ids, vals, context=context) + + def sort_taxes(self, cursor, user, ids, context=None): + ''' + 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, [ + ('id', 'in', ids), + ], order=[('sequence', 'ASC'), ('id', 'ASC')], context=context) + + sort_taxes = Cache('account_tax.sort_taxes')(sort_taxes) + def compute(self, cursor, user, ids, price_unit, quantity, context=None): ''' Compute taxes for price_unit and quantity. @@ -650,9 +682,7 @@ class Tax(OSV): amount tax ''' - ids = self.search(cursor, user, [ - ('id', 'in', ids), - ], order=[('sequence', 'ASC'), ('id', 'ASC')], context=context) + 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) @@ -707,9 +737,7 @@ class Tax(OSV): amount tax ''' - ids = self.search(cursor, user, [ - ('id', 'in', ids), - ], order=[('sequence', 'ASC'), ('id', 'ASC')], context=context) + ids = self.sort_taxes(cursor, user, ids, context=context) taxes = self.browse(cursor, user, ids, context=context) taxes.reverse() res = self._unit_compute_inv(cursor, user, taxes, price_unit,