Skip to content

Commit

Permalink
[FIX] Fixed performance problem when search tracking values by subtype (
Browse files Browse the repository at this point in the history
OCA#470)

id, instead search them by messages ids then filter them
fix OCA#469

[REF] account_invoice_change_currency: Sql queries in order to improve performance

[FIX] account_invoice_change_currency: Fixed tests
  • Loading branch information
hugho-ad authored and rolandojduartem committed May 13, 2022
1 parent 95b9888 commit 35be856
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
28 changes: 18 additions & 10 deletions account_invoice_change_currency/models/account_change_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,25 @@ def get_last_currency_id(self, skip_update_currency=False):
subtype_id = self.env.ref(
'account_invoice_change_currency.mt_currency_update')
subtype_create_id = self.env.ref('account.mt_invoice_created')
domain = [
('mail_message_id', 'in', self.message_ids.ids),
('field', '=', 'currency_id'),
]
query = """
SELECT mtv.old_value_integer, mtv.new_value_integer, mm.subtype_id
FROM mail_tracking_value as mtv INNER JOIN mail_message AS mm
ON mtv.mail_message_id = mm.id """
if skip_update_currency:
domain += [('mail_message_id.subtype_id', '!=', subtype_id.id)]
last_value = self.env['mail.tracking.value'].sudo().search(
domain, limit=1, order='write_date desc, id desc')
value = last_value.old_value_integer
if last_value.mail_message_id.subtype_id == subtype_create_id:
value = last_value.new_value_integer
query += " AND mm.subtype_id != %s "
params = (subtype_id.id, tuple(self.message_ids.ids))
else:
params = (tuple(self.message_ids.ids), )
query += """WHERE mtv.field = 'currency_id' AND
mtv.mail_message_id IN %s
ORDER BY mtv.write_date DESC, mtv.id DESC LIMIT 1"""
self.env.cr.execute(query, params)
res = self.env.cr.dictfetchone()
value = False
if res:
value = res['old_value_integer']
if res['subtype_id'] == subtype_create_id.id:
value = res['new_value_integer']
return self.currency_id.browse(value)

@api.multi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_change_invoice_currency(self):
before_amount, after_curr, inv.company_id, fields.Date.today())

self.assertEqual(
inv.amount_total, expected_value,
float_compare(inv.amount_total, expected_value, 1), 0,
'Total amount of invoice does not equal to expected value!!!')

def test_change_validated_invoice_currency(self):
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_custom_rate_zero_update_currency(self):
expected_value = before_curr._convert(
before_amount, usd, inv.company_id, fields.Date.today())
self.assertEqual(
inv.amount_total, expected_value,
float_compare(inv.amount_total, expected_value, 1), 0,
'Total amount of invoice does not equal to expected value!!!')
# Change currency and set custom rate 0
inv.write({'currency_id': eur.id, 'custom_rate': custom_rate})
Expand Down Expand Up @@ -237,12 +237,12 @@ def test_custom_rate_zero_update_currency(self):
expected_value = before_amount * rate / old_rate
# TODO: Check float comparation, 26179.05 vs 26179.07656
self.assertEqual(float_compare(
inv.amount_total, expected_value, self.precision), -1,
inv.amount_total, expected_value, 1), 0,
'Total amount of invoice does not equal to expected value!!!')
inv.action_account_change_currency()
# TODO: Check float comparation, 26179.05 vs 26179.07656
self.assertEqual(float_compare(
inv.amount_total, expected_value, self.precision), -1,
inv.amount_total, expected_value, 1), 0,
'Total amount of invoice does not equal to expected value!!!')

def test_force_custom_rate(self):
Expand Down

0 comments on commit 35be856

Please sign in to comment.