diff --git a/account_invoice_change_currency/models/account_change_currency.py b/account_invoice_change_currency/models/account_change_currency.py index e8df6250b7c..71452c72028 100644 --- a/account_invoice_change_currency/models/account_change_currency.py +++ b/account_invoice_change_currency/models/account_change_currency.py @@ -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 diff --git a/account_invoice_change_currency/tests/test_account_invoice_change_currency.py b/account_invoice_change_currency/tests/test_account_invoice_change_currency.py index 5f7c9fac832..78199522eeb 100644 --- a/account_invoice_change_currency/tests/test_account_invoice_change_currency.py +++ b/account_invoice_change_currency/tests/test_account_invoice_change_currency.py @@ -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): @@ -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}) @@ -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):