[FIX] account: wrong computation smart button on analytic account

The smart buttons were computing the number of aml instead of the number
of invoices/bills with the account.

We now compute back the number of moves.
We also changed it so it is consistent with the view,
so it computes the number of moves with move lines that have the distribution
instead of the analytic lines with the account.

X-original-commit: 84f3ee28156721cea0b530b92ede36fad58c5db8
Part-of: odoo/odoo#104933
This commit is contained in:
gawa-odoo
2022-11-04 10:54:56 +01:00
parent 2ad702f239
commit 972171968d
@@ -18,26 +18,52 @@ class AccountAnalyticAccount(models.Model):
@api.depends('line_ids')
def _compute_invoice_count(self):
sale_types = self.env['account.move'].get_sale_types(include_receipts=True)
domain = [
('move_line_id.move_id.move_type', 'in', sale_types),
('account_id', 'in', self.ids)
]
groups = self.env['account.analytic.line']._read_group(domain, ['move_line_id.move_id:count_distinct'], ['account_id'])
moves_count_mapping = dict((g['account_id'][0], g['account_id_count']) for g in groups)
query = self.env['account.move.line']._search([
('move_id.state', '=', 'posted'),
('move_id.move_type', 'in', sale_types),
])
query.add_where(
'account_move_line.analytic_distribution ?| array[%s]',
[str(account_id) for account_id in self.ids],
)
query.order = None
query_string, query_param = query.select(
'jsonb_object_keys(account_move_line.analytic_distribution) as account_id',
'COUNT(DISTINCT(move_id)) as move_count',
)
query_string = f"{query_string} GROUP BY jsonb_object_keys(account_move_line.analytic_distribution)"
self._cr.execute(query_string, query_param)
data = {int(record.get('account_id')): record.get('move_count') for record in self._cr.dictfetchall()}
for account in self:
account.invoice_count = moves_count_mapping.get(account.id, 0)
account.invoice_count = data.get(account.id, 0)
@api.depends('line_ids')
def _compute_vendor_bill_count(self):
purchase_types = self.env['account.move'].get_purchase_types(include_receipts=True)
domain = [
('move_line_id.move_id.move_type', 'in', purchase_types),
('account_id', 'in', self.ids)
]
groups = self.env['account.analytic.line']._read_group(domain, ['move_line_id.move_id:count_distinct'], ['account_id'])
moves_count_mapping = dict((g['account_id'][0], g['account_id_count']) for g in groups)
query = self.env['account.move.line']._search([
('move_id.state', '=', 'posted'),
('move_id.move_type', 'in', purchase_types),
])
query.add_where(
'account_move_line.analytic_distribution ?| array[%s]',
[str(account_id) for account_id in self.ids],
)
query.order = None
query_string, query_param = query.select(
'jsonb_object_keys(account_move_line.analytic_distribution) as account_id',
'COUNT(DISTINCT(move_id)) as move_count',
)
query_string = f"{query_string} GROUP BY jsonb_object_keys(account_move_line.analytic_distribution)"
self._cr.execute(query_string, query_param)
data = {int(record.get('account_id')): record.get('move_count') for record in self._cr.dictfetchall()}
for account in self:
account.vendor_bill_count = moves_count_mapping.get(account.id, 0)
account.vendor_bill_count = data.get(account.id, 0)
def action_view_invoice(self):
self.ensure_one()