[FIX] account_fleet: Improve performances

Performances issues have been noticed in account_fleet when computing
the move_ids of vehicles.
This change has been made in order to fix this issue.

Task id #2700407

closes odoo/odoo#80755

X-original-commit: 2860051b3cbfa7bcd7df76d05b4e5770e9a4c2d0
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Nicolas (vin)
2021-12-02 13:43:19 +00:00
parent d832a04c68
commit a4061afe0d
2 changed files with 9 additions and 3 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ class AccountMove(models.Model):
class AccountMoveLine(models.Model):
_inherit = 'account.move.line'
vehicle_id = fields.Many2one('fleet.vehicle', string='Vehicle')
vehicle_id = fields.Many2one('fleet.vehicle', string='Vehicle', index=True)
need_vehicle = fields.Boolean(compute='_compute_need_vehicle',
help="Technical field to decide whether the vehicle_id field is editable")
+8 -2
View File
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models, fields
from odoo import Command, models, fields
class FleetVehicle(models.Model):
@@ -16,8 +16,14 @@ class FleetVehicle(models.Model):
self.bill_count = 0
return
moves = self.env['account.move.line'].read_group(
domain=[('vehicle_id', 'in', self.ids), ('move_id.state', '!=', 'cancel')],
fields=['vehicle_id', 'move_id:array_agg'],
groupby=['vehicle_id'],
)
vehicle_move_mapping = {move['vehicle_id'][0]: set(move['move_id']) for move in moves}
for vehicle in self:
vehicle.account_move_ids = self.env['account.move.line'].search([('vehicle_id', '=', vehicle.id), ('move_id.state', '!=', 'cancel')]).move_id
vehicle.account_move_ids = [Command.set(vehicle_move_mapping.get(vehicle.id, []))]
vehicle.bill_count = len(vehicle.account_move_ids)
def action_view_bills(self):