<commit_before># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging

from odoo import fields, models, _
from odoo.tools import float_compare

_logger = logging.getLogger(__name__)


class PaymentTransaction(models.Model):
    _inherit = 'payment.transaction'

    account_invoice_id = fields.Many2one('account.invoice', string='Invoice')

    def form_feedback(self, data, acquirer_name):
        """ Override to confirm the invoice, if defined, and if the transaction is done. """
        tx = None
        res = super(PaymentTransaction, self).form_feedback(data, acquirer_name)

        # fetch the tx
        tx_find_method_name = '_%s_form_get_tx_from_data' % acquirer_name
        if hasattr(self, tx_find_method_name):
            tx = getattr(self, tx_find_method_name)(data)

        if tx and tx.account_invoice_id:
            _logger.info(
                '<%s> transaction <%s> processing form feedback for invoice <%s>: tx ref:%s, tx amount: %s',
                acquirer_name, tx.id, tx.account_invoice_id.id, tx.reference, tx.amount)
            tx._confirm_invoice()

        return res

    def confirm_invoice_token(self):
        """ Confirm a transaction token and call SO confirmation if it is a success.

        :return: True if success; error string otherwise """
        self.ensure_one()
        if self.payment_token_id and self.partner_id == self.account_invoice_id.partner_id:
            try:
                s2s_result = self.s2s_do_transaction()
            except Exception as e:
                _logger.warning(
                    _("<%s> transaction (%s) failed : <%s>") %
                    (self.acquirer_id.provider, self.id, str(e)))
                return 'pay_invoice_tx_fail'

            valid_state = 'authorized' if self.acquirer_id.capture_manually else 'done'
            if not s2s_result or self.state != valid_state:
                _logger.warning(
                    _("<%s> transaction (%s) invalid state : %s") %
                    (self.acquirer_id.provider, self.id, self.state_mesage))
                return 'pay_invoice_tx_state'

            try:
                # Auto-confirm SO if necessary
                return self._confirm_invoice()
            except Exception as e:
                _logger.warning(
                    _("<%s>  transaction (%s) invoice confirmation failed : <%s>") %
                    (self.acquirer_id.provider, self.id, str(e)))
                return 'pay_invoice_tx_confirm'
        return 'pay_invoice_tx_token'

    def _confirm_invoice(self):
        """ Check tx state, confirm and pay potential invoice """
        self.ensure_one()
        # check tx state, confirm the potential SO
        if self.account_invoice_id.state != 'open':
            _logger.warning('<%s> transaction STATE INCORRECT for invoice %s (ID %s, state %s)', self.acquirer_id.provider, self.account_invoice_id.number, self.account_invoice_id.id, self.account_invoice_id.state)
            return 'pay_invoice_invalid_doc_state'
        if not float_compare(self.amount, self.account_invoice_id.amount_total, 2) == 0:
            _logger.warning(
                '<%s> transaction AMOUNT MISMATCH for invoice %s (ID %s): expected %r, got %r',
                self.acquirer_id.provider, self.account_invoice_id.number, self.account_invoice_id.id,
                self.account_invoice_id.amount_total, self.amount,
            )
            self.account_invoice_id.message_post(<commit_msg>[FIX] account_payment: use correct field<commit_after># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging

from odoo import fields, models, _
from odoo.tools import float_compare

_logger = logging.getLogger(__name__)


class PaymentTransaction(models.Model):
    _inherit = 'payment.transaction'

    account_invoice_id = fields.Many2one('account.invoice', string='Invoice')

    def form_feedback(self, data, acquirer_name):
        """ Override to confirm the invoice, if defined, and if the transaction is done. """
        tx = None
        res = super(PaymentTransaction, self).form_feedback(data, acquirer_name)

        # fetch the tx
        tx_find_method_name = '_%s_form_get_tx_from_data' % acquirer_name
        if hasattr(self, tx_find_method_name):
            tx = getattr(self, tx_find_method_name)(data)

        if tx and tx.account_invoice_id:
            _logger.info(
                '<%s> transaction <%s> processing form feedback for invoice <%s>: tx ref:%s, tx amount: %s',
                acquirer_name, tx.id, tx.account_invoice_id.id, tx.reference, tx.amount)
            tx._confirm_invoice()

        return res

    def confirm_invoice_token(self):
        """ Confirm a transaction token and call SO confirmation if it is a success.

        :return: True if success; error string otherwise """
        self.ensure_one()
        if self.payment_token_id and self.partner_id == self.account_invoice_id.partner_id:
            try:
                s2s_result = self.s2s_do_transaction()
            except Exception as e:
                _logger.warning(
                    _("<%s> transaction (%s) failed : <%s>") %
                    (self.acquirer_id.provider, self.id, str(e)))
                return 'pay_invoice_tx_fail'

            valid_state = 'authorized' if self.acquirer_id.capture_manually else 'done'
            if not s2s_result or self.state != valid_state:
                _logger.warning(
                    _("<%s> transaction (%s) invalid state : %s") %
                    (self.acquirer_id.provider, self.id, self.state_message))
                return 'pay_invoice_tx_state'

            try:
                # Auto-confirm SO if necessary
                return self._confirm_invoice()
            except Exception as e:
                _logger.warning(
                    _("<%s>  transaction (%s) invoice confirmation failed : <%s>") %
                    (self.acquirer_id.provider, self.id, str(e)))
                return 'pay_invoice_tx_confirm'
        return 'pay_invoice_tx_token'

    def _confirm_invoice(self):
        """ Check tx state, confirm and pay potential invoice """
        self.ensure_one()
        # check tx state, confirm the potential SO
        if self.account_invoice_id.state != 'open':
            _logger.warning('<%s> transaction STATE INCORRECT for invoice %s (ID %s, state %s)', self.acquirer_id.provider, self.account_invoice_id.number, self.account_invoice_id.id, self.account_invoice_id.state)
            return 'pay_invoice_invalid_doc_state'
        if not float_compare(self.amount, self.account_invoice_id.amount_total, 2) == 0:
            _logger.warning(
                '<%s> transaction AMOUNT MISMATCH for invoice %s (ID %s): expected %r, got %r',
                self.acquirer_id.provider, self.account_invoice_id.number, self.account_invoice_id.id,
                self.account_invoice_id.amount_total, self.amount,
            )
            self.account_invoice_id.message_post(
{'fraction_non_alphanumeric': 0.09703389830508474, 'fraction_numerical': 0.0019774011299435027, 'mean_word_length': 2.341670599339311, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '3211336', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2036, 'n_words': 517}