If applied, this commit will fix the following bug by adding some restrictions on the creation of analytic default and the uses of tags in account.move Steps to reproduce: 1- install account.accountant, account_analytic_default 2- Accounting --> Configuration --> Analytic Tags, analytic accounting 3- Create an Analytic Tag, with no Analytic Distribution. 4- Accounting --> Configuration --> Analytic Default Rules. Create an Analytic Default Rule, using the Analytic Tag above and select a Partner 5- Create a Vendor Bill on that partner. The Analytic Tag is added to the invoice, as expected 6- However when selecting Accounting --> Reporting --> Analytic Report, the report is empty and does not contain the Analytic Tag. Adding the Analytic Tag to the filter has no effect. Bug/Issue: An analytic tag is meant to be an additional dimension of analysis. Dimension which is only worth/valid when an analytic account is specified. An analytic default rule should never be applied without - either an analytic account - or analytic tag used for distribution Fixes: 1- changing the error condition on creating analytic default 2- changing the error message accordingly to better explain the issue 3- raise an error when the user creates an account.move with analytic tags without (analytic tags + analytic account) or (analytic tags with analytic distrubtion) Please refer to the ticket for the whole discussion on this OPW-2766749 closes odoo/odoo#91271 X-original-commit: 99c50b9d48448bff54fc5a7c76bbec300bd25d25 Signed-off-by: Mohamed Megahed Abbas Megahed SALLAM (mome) <mome@odoo.com> Signed-off-by: William André (wan) <wan@odoo.com>
69 lines
4.1 KiB
Python
69 lines
4.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models, _
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
class AccountAnalyticDefault(models.Model):
|
|
_name = "account.analytic.default"
|
|
_description = "Analytic Distribution"
|
|
_rec_name = "analytic_id"
|
|
_order = "sequence"
|
|
|
|
sequence = fields.Integer(string='Sequence', help="Gives the sequence order when displaying a list of analytic distribution")
|
|
analytic_id = fields.Many2one('account.analytic.account', string='Analytic Account')
|
|
analytic_tag_ids = fields.Many2many('account.analytic.tag', string='Analytic Tags')
|
|
product_id = fields.Many2one('product.product', string='Product', ondelete='cascade', help="Select a product which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this product, it will automatically take this as an analytic account)")
|
|
partner_id = fields.Many2one('res.partner', string='Partner', ondelete='cascade', help="Select a partner which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this partner, it will automatically take this as an analytic account)")
|
|
account_id = fields.Many2one('account.account', string='Account', ondelete='cascade', help="Select an accounting account which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this account, it will automatically take this as an analytic account)")
|
|
user_id = fields.Many2one('res.users', string='User', ondelete='cascade', help="Select a user which will use analytic account specified in analytic default.")
|
|
company_id = fields.Many2one('res.company', string='Company', ondelete='cascade', help="Select a company which will use analytic account specified in analytic default (e.g. create new customer invoice or Sales order if we select this company, it will automatically take this as an analytic account)")
|
|
date_start = fields.Date(string='Start Date', help="Default start date for this Analytic Account.")
|
|
date_stop = fields.Date(string='End Date', help="Default end date for this Analytic Account.")
|
|
|
|
@api.constrains('analytic_id', 'analytic_tag_ids')
|
|
def _check_account_or_tags(self):
|
|
if any(not default.analytic_id
|
|
and not any(tag.analytic_distribution_ids for tag in default.analytic_tag_ids)
|
|
for default in self
|
|
):
|
|
raise ValidationError(_('An analytic default requires an analytic account or an analytic tag used for analytic distribution.'))
|
|
|
|
@api.model
|
|
def account_get(self, product_id=None, partner_id=None, account_id=None, user_id=None, date=None, company_id=None):
|
|
domain = []
|
|
if product_id:
|
|
domain += ['|', ('product_id', '=', product_id)]
|
|
domain += [('product_id', '=', False)]
|
|
if partner_id:
|
|
domain += ['|', ('partner_id', '=', partner_id)]
|
|
domain += [('partner_id', '=', False)]
|
|
if account_id:
|
|
domain += ['|', ('account_id', '=', account_id)]
|
|
domain += [('account_id', '=', False)]
|
|
if company_id:
|
|
domain += ['|', ('company_id', '=', company_id)]
|
|
domain += [('company_id', '=', False)]
|
|
if user_id:
|
|
domain += ['|', ('user_id', '=', user_id)]
|
|
domain += [('user_id', '=', False)]
|
|
if date:
|
|
domain += ['|', ('date_start', '<=', date), ('date_start', '=', False)]
|
|
domain += ['|', ('date_stop', '>=', date), ('date_stop', '=', False)]
|
|
best_index = -1
|
|
res = self.env['account.analytic.default']
|
|
for rec in self.search(domain):
|
|
index = 0
|
|
if rec.product_id: index += 1
|
|
if rec.partner_id: index += 1
|
|
if rec.account_id: index += 1
|
|
if rec.company_id: index += 1
|
|
if rec.user_id: index += 1
|
|
if rec.date_start: index += 1
|
|
if rec.date_stop: index += 1
|
|
if index > best_index:
|
|
res = rec
|
|
best_index = index
|
|
return res
|