<commit_before>﻿##############################################################################
#
#    Copyright (C) 2022 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
from datetime import datetime, timedelta
from enum import Enum
from postfinancecheckout import Configuration
from postfinancecheckout.api.transaction_service_api import TransactionServiceApi

from odoo import api, models, fields, _
from odoo.tools import ormcache


class Provider(Enum):
    WORLDLINE = "WORLDLINE SCHWEIZ AG"
    PF_CARD = "POSTFINANCE CARD"
    TWINT = "TWINT"
    E_FINANCE = "POSTFINANCE E-FINANCE"


PF_MAPPING = {
    Provider.WORLDLINE: "SIX Acquiring",
    Provider.PF_CARD: "PostFinance Acquiring - PostFinance Card",
    Provider.E_FINANCE: "PostFinance Acquiring - PostFinance E-Finance",
    Provider.TWINT: "TWINT - TWINT Connector"
}


class ReconcileFundWizard(models.TransientModel):

    _name = "reconcile.1015.wizard"
    _description = "Wizard reconcile 1015 account"

    account_id = fields.Many2one(
        "account.account", "Reconcile account",
        default=lambda s: s.env["account.account"].search([
            ("code", "=", "1015")], limit=1)
    )
    full_reconcile_line_ids = fields.Many2many(
        "account.move.line", "reconcile_1015_reconciled", string="Reconciled lines",
        readonly=True
    )
    partial_reconcile_line_ids = fields.Many2many(
        "account.move.line", "reconcile_1015_partial_reconciled",
        string="Partial reconciled lines", readonly=True
    )
    missing_donation_line_ids = fields.Many2many(
        "account.move.line", "reconcile_1015_missing_invoice",
        string="Leftover donations",
        readonly=True
    )

    def reconcile_1015(self):
        mvl_obj = self.env["account.move.line"]
        credit_lines = mvl_obj.search([
            ("account_id", "=", self.account_id.id),
            ("full_reconcile_id", "=", False),
            ("credit", ">", 0),
            ("date", ">=", "2022-02-09")  # Date of activation of pf_checkout
        ])
        for cl in credit_lines:
            self.reconcile_using_pf_checkout(cl)

        # Compute results
        oldest_credit = min(self.partial_reconcile_line_ids.mapped("date"))
        self.missing_donation_line_ids = mvl_obj.search([
            ("account_id", "=", self.account_id.id),
            ("full_reconcile_id", "=", False),
            ("debit", ">", 0),
            ("date", "<=", fields.Date.to_string(oldest_credit)),
            ("date", ">=", "2022-02-09")  # Date of activation of pf_checkout
        ]).filtered(lambda m: not m.matched_credit_ids)
        self.env.user.notify_success(
            message=_("Successfully reconciled %s entries")
            % len(self.full_reconcile_line_ids))
        return {
            "type": "ir.actions.act_window",
            "view_mode": "tree,form",
            "view_type": "form",
            "res_model": "account.move.line",
            "target": "current",
            "context": self.env.context,
            "domain": [("id", "in", (self.partial_reconcile_line_ids +
                                     self.missing_donation_line_ids).ids)]
        }

    @api.multi
    def reconcile_using_pf_checkout(self, move_line):
        # Transactions are grouped by dates
        date_position = -1
        date_length = 8
        search_days_delta = 0<commit_msg>CO-3909 FIX possible outcome messages<commit_after>﻿##############################################################################
#
#    Copyright (C) 2022 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
from datetime import datetime, timedelta
from enum import Enum
from postfinancecheckout import Configuration
from postfinancecheckout.api.transaction_service_api import TransactionServiceApi

from odoo import api, models, fields, _
from odoo.tools import ormcache


class Provider(Enum):
    WORLDLINE = "WORLDLINE SCHWEIZ AG"
    PF_CARD = "POSTFINANCE CARD"
    TWINT = "TWINT"
    E_FINANCE = "POSTFINANCE E-FINANCE"


PF_MAPPING = {
    Provider.WORLDLINE: "SIX Acquiring",
    Provider.PF_CARD: "PostFinance Acquiring - PostFinance Card",
    Provider.E_FINANCE: "PostFinance Acquiring - PostFinance E-Finance",
    Provider.TWINT: "TWINT - TWINT Connector"
}


class ReconcileFundWizard(models.TransientModel):

    _name = "reconcile.1015.wizard"
    _description = "Wizard reconcile 1015 account"

    account_id = fields.Many2one(
        "account.account", "Reconcile account",
        default=lambda s: s.env["account.account"].search([
            ("code", "=", "1015")], limit=1)
    )
    full_reconcile_line_ids = fields.Many2many(
        "account.move.line", "reconcile_1015_reconciled", string="Reconciled lines",
        readonly=True
    )
    partial_reconcile_line_ids = fields.Many2many(
        "account.move.line", "reconcile_1015_partial_reconciled",
        string="Partial reconciled lines", readonly=True
    )
    missing_donation_line_ids = fields.Many2many(
        "account.move.line", "reconcile_1015_missing_invoice",
        string="Leftover donations",
        readonly=True
    )

    def reconcile_1015(self):
        mvl_obj = self.env["account.move.line"]
        credit_lines = mvl_obj.search([
            ("account_id", "=", self.account_id.id),
            ("full_reconcile_id", "=", False),
            ("credit", ">", 0),
            ("date", ">=", "2022-02-09")  # Date of activation of pf_checkout
        ])
        for cl in credit_lines:
            self.reconcile_using_pf_checkout(cl)

        # Compute results
        if self.partial_reconcile_line_ids:
            oldest_credit = min(self.partial_reconcile_line_ids.mapped("date"))
            self.missing_donation_line_ids = mvl_obj.search([
                ("account_id", "=", self.account_id.id),
                ("full_reconcile_id", "=", False),
                ("debit", ">", 0),
                ("date", "<=", fields.Date.to_string(oldest_credit)),
                ("date", ">=", "2022-02-09")  # Date of activation of pf_checkout
            ]).filtered(lambda m: not m.matched_credit_ids)
        if self.full_reconcile_line_ids:
            self.env.user.notify_success(
                message=_("Successfully reconciled %s entries")
                % len(self.full_reconcile_line_ids),
                sticky=True
            )
        else:
            if not credit_lines:
                self.env.user.notify_success(
                    message=_("Every credit entry is reconciled"))
            else:
                self.env.user.notify_warning(
                    message=_("0 credit entry could be fully reconciled"))
        return {
            "type": "ir.actions.act_window",
            "view_mode": "tree,form",
            "view_type": "form",
            "res_model": "account.move.line",
            "target": "current",
            "context": self.env.context,
            "domain": [("id", "in", (self.partial_reconcile_line_ids +
                                     self.missing_donation_line_ids).ids)]
        }

    @api.multi
    def reconcile_using_pf_checkout(self, move_line):
        # Transactions are grouped by dates
        date_position = -1
        date_length = 8
        search_days_delta = 0
{'fraction_non_alphanumeric': 0.15461215932914046, 'fraction_numerical': 0.015854297693920334, 'mean_word_length': 2.699951526902569, 'pattern_counts': {'":': 14, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2494942', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2290, 'n_words': 453}