<commit_before>from odoo import fields, models, _, api
from odoo.exceptions import UserError, ValidationError
from odoo.tools.misc import format_date
from odoo.osv import expression
import logging
_logger = logging.getLogger(__name__)


class AccountPayment(models.Model):

    _inherit = 'account.payment'

    # fix that should go in standard
    payment_method_line_id = fields.Many2one(copy=False)
    l10n_latam_check_id = fields.Many2one(
        'account.payment', string='Check', readonly=True,
        states={'draft': [('readonly', False)]}, copy=False)
    l10n_latam_check_current_journal_id = fields.Many2one(
        'account.journal', compute='_compute_l10n_latam_check_current_journal',
        string="Check Current Journal", store=True)
    l10n_latam_check_operation_ids = fields.One2many(
        'account.payment', 'l10n_latam_check_id', readonly=True, string='Check Operations')
    l10n_latam_check_bank_id = fields.Many2one(
        'res.bank', readonly=False, states={'cancel': [('readonly', True)], 'posted': [('readonly', True)]},
        compute='_compute_l10n_latam_check_data', store=True, string='Check Bank',)
    l10n_latam_check_issuer_vat = fields.Char(
        readonly=False, states={'cancel': [('readonly', True)], 'posted': [('readonly', True)]},
        compute='_compute_l10n_latam_check_data', store=True, string='Check Issuer VAT')
    l10n_latam_use_checkbooks = fields.Boolean(related='journal_id.l10n_latam_use_checkbooks')
    l10n_latam_checkbook_type = fields.Selection(related='l10n_latam_checkbook_id.type')
    l10n_latam_checkbook_id = fields.Many2one(
        'l10n_latam.checkbook', 'Checkbook', store=True,
        compute='_compute_l10n_latam_checkbook', readonly=True, states={'draft': [('readonly', False)]})
    l10n_latam_check_payment_date = fields.Date(
        string='Check Payment Date', readonly=True, states={'draft': [('readonly', False)]})
    l10n_latam_check_warning_msg = fields.Html(compute='_compute_l10n_latam_check_warning_msg')
    check_number = fields.Char(readonly=False)

    @api.depends('payment_method_line_id.code', 'journal_id.l10n_latam_use_checkbooks')
    def _compute_l10n_latam_checkbook(self):
        with_checkbooks = self.filtered(
            lambda x: x.payment_method_line_id.code == 'check_printing' and x.journal_id.l10n_latam_use_checkbooks)
        (self - with_checkbooks).l10n_latam_checkbook_id = False
        for rec in with_checkbooks:
            checkbooks = rec.journal_id.with_context(active_test=True).l10n_latam_checkbook_ids
            if rec.l10n_latam_checkbook_id and rec.l10n_latam_checkbook_id in checkbooks:
                continue
            rec.l10n_latam_checkbook_id = checkbooks and checkbooks[0] or False

    @api.depends('l10n_latam_checkbook_id')<commit_msg>[IMP] l10n_latam_check: fix check uniqueness<commit_after>from odoo import fields, models, _, api
from odoo.exceptions import UserError, ValidationError
from odoo.tools.misc import format_date
from odoo.osv import expression
import logging
_logger = logging.getLogger(__name__)


class AccountPayment(models.Model):

    _inherit = 'account.payment'

    # fix that should go in standard
    payment_method_line_id = fields.Many2one(copy=False)
    l10n_latam_check_id = fields.Many2one(
        'account.payment', string='Check', readonly=True,
        states={'draft': [('readonly', False)]}, copy=False)
    l10n_latam_check_current_journal_id = fields.Many2one(
        'account.journal', compute='_compute_l10n_latam_check_current_journal',
        string="Check Current Journal", store=True)
    l10n_latam_check_operation_ids = fields.One2many(
        'account.payment', 'l10n_latam_check_id', readonly=True, string='Check Operations')
    l10n_latam_check_bank_id = fields.Many2one(
        'res.bank', readonly=False, states={'cancel': [('readonly', True)], 'posted': [('readonly', True)]},
        compute='_compute_l10n_latam_check_data', store=True, string='Check Bank',)
    l10n_latam_check_issuer_vat = fields.Char(
        readonly=False, states={'cancel': [('readonly', True)], 'posted': [('readonly', True)]},
        compute='_compute_l10n_latam_check_data', store=True, string='Check Issuer VAT')
    l10n_latam_use_checkbooks = fields.Boolean(related='journal_id.l10n_latam_use_checkbooks')
    l10n_latam_checkbook_type = fields.Selection(related='l10n_latam_checkbook_id.type')
    l10n_latam_checkbook_id = fields.Many2one(
        'l10n_latam.checkbook', 'Checkbook', store=True,
        compute='_compute_l10n_latam_checkbook', readonly=True, states={'draft': [('readonly', False)]})
    l10n_latam_check_payment_date = fields.Date(
        string='Check Payment Date', readonly=True, states={'draft': [('readonly', False)]})
    l10n_latam_check_warning_msg = fields.Html(compute='_compute_l10n_latam_check_warning_msg')
    check_number = fields.Char(readonly=False)

    @api.constrains('check_number', 'journal_id')
    def _constrains_check_number(self):
        """ This odoo constraint should not check checks with checkbooks because when using checkbooks, number is
        unique by checkbook type (deferred, current, electronic) """
        checkbooks_checks = self.filtered('l10n_latam_checkbook_id')
        return super(AccountPayment, self - checkbooks_checks)._constrains_check_number

    @api.constrains('journal_id', 'check_number', 'l10n_latam_checkbook_id')
    def _l10n_latam_check_unique(self):
        """ The constraint _constrains_check_number is not usefull when using checkbooks for two reasons:
        1. it is protecting to use same number because it check only posted payments and it doesn't re-check when
        changing state
        2. when using checkbooks the uniqueness is pero checkbook type on a journal"""
        for rec in self.filtered('l10n_latam_checkbook_id'):
            same_checks = self.search([
                ('l10n_latam_checkbook_id.type', '=', rec.l10n_latam_checkbook_id.type),
                ('journal_id', '=', rec.journal_id.id),
                ('check_number', '=', rec.check_number),
                ('id', '!=', rec.id),
            ])
            if same_checks:
                raise ValidationError(_(
                    'Check Number (%s) must be unique per Checkbook type (current, deferred or electronic)!\n'
                    '* Check ids: %s') % (rec.check_number, same_checks.ids))

    @api.depends('payment_method_line_id.code', 'journal_id.l10n_latam_use_checkbooks')
    def _compute_l10n_latam_checkbook(self):
        with_checkbooks = self.filtered(
            lambda x: x.payment_method_line_id.code == 'check_printing' and x.journal_id.l10n_latam_use_checkbooks)
        (self - with_checkbooks).l10n_latam_checkbook_id = False
        for rec in with_checkbooks:
            checkbooks = rec.journal_id.with_context(active_test=True).l10n_latam_checkbook_ids
            if rec.l10n_latam_checkbook_id and rec.l10n_latam_checkbook_id in checkbooks:
                continue
            rec.l10n_latam_checkbook_id = checkbooks and checkbooks[0] or False

    @api.depends('l10n_latam_checkbook_id')
{'fraction_non_alphanumeric': 0.10875070343275182, 'fraction_numerical': 0.019977490151941474, 'mean_word_length': 4.285501858736059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6635140', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2284, 'n_words': 420}