<commit_before>##############################################################################
#
#    Copyright (C) 2016 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import logging

from datetime import datetime

from dateutil.relativedelta import relativedelta

from odoo import api, models
from odoo.addons.queue_job.job import job, related_action

logger = logging.getLogger(__name__)


class AccountInvoice(models.Model):
    _inherit = "account.invoice"

    @api.model
    def thankyou_summary_cron(self):
        """
        Sends a summary each month of the donations
        :return: True
        """
        comm_obj = self.env["partner.communication.job"]
        first = datetime.today().replace(day=1)
        last_month = first - relativedelta(months=1)
        partners = (
            self.env["res.users"]
                .search(
                [
                    "|",
                    "|",
                    ("name", "like", "<NAME>"),
                    ("name", "like", "<NAME>"),
                    ("name", "like", "<NAME>"),
                ]
            )
            .mapped("partner_id")
        )
        invoices = self.search(
            [
                ("type", "=", "out_invoice"),
                ("invoice_category", "!=", "sponsorship"),
                ("state", "=", "paid"),
                ("last_payment", ">=", last_month),
                ("last_payment", "<", first),
            ]
        )
        config = self.env.ref("thankyou_letters.config_thankyou_summary")
        for partner in partners:
            comm_obj.create(
                {
                    "config_id": config.id,
                    "partner_id": partner.id,
                    "object_ids": invoices.ids,
                }
            )
        return True

    @api.multi
    def generate_thank_you(self):
        """
        Creates a thank you letter communication separating events thank you
        and regular thank you.
        override thankyou_letters.generate_thank_you()
        """
        partners = self.mapped("partner_id").filtered(
            lambda p: p.thankyou_preference != "none"
        )
        gift_category = self.env.ref("sponsorship_compassion.product_category_gift")
        for partner in partners:
            invoice_lines = self.mapped("invoice_line_ids").filtered(
                lambda l: l.partner_id == partner
            )
            event_thank = invoice_lines.filtered("event_id")
            other_thank = invoice_lines - event_thank
            for event in event_thank.mapped("event_id"):
                event_thank.filtered(lambda l: l.event_id == event).generate_thank_you()
            if other_thank:
                other_thank.generate_thank_you()

        # Send confirmation to ambassadors
        ambassadors = self.mapped("invoice_line_ids.user_id")
        for ambassador in ambassadors:
            # Filter only donations not for made for himself and filter
            # gifts that are thanked but not directly for ambassador.
            ambassador_lines = self.mapped("invoice_line_ids").filtered(
                lambda l: l.user_id == ambassador
                and l.partner_id != ambassador
                and l.product_id.categ_id != gift_category
            )
            if ambassador_lines:
                ambassador_lines.send_receipt_to_ambassador()

    @api.multi
    def _filter_invoice_to_thank(self):
        """
        Given a recordset of paid invoices, return only those that have
        to be thanked.
        :return: account.invoice recordset
        override thankyou_letters._filter_invoice_to_thanks()
        """
        return self.filtered(
            lambda i: i.type == "out_invoice"
            and not i.avoid_thankyou_letter
            and (
                not i.communication_id
                or i.communication_id.state in ("call", "pending")
            )
            and i.invoice_category != "sponsorship"
            and (
                not i.mapped("invoice_line_ids.contract_id")
                or (
                    i.invoice_category == "gift" and i.origin != "Automatic birthday gift")
            )
        )

    @job(default_channel="root.group_reconcile")
    @related_action(action="related_action_invoices")
    def group_or_split_reconcile(self):
        """Reconcile given invoices with partner open payments.
        """
        super().group_or_split_reconcile()
        # Find if a communication with payment slips is pending and
        # regenerate it.
        jobs = self.env["partner.communication.job"].search(<commit_msg>CO-3614 look for 'G' contract in thank you filter<commit_after>##############################################################################
#
#    Copyright (C) 2016 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import logging

from datetime import datetime

from dateutil.relativedelta import relativedelta

from odoo import api, models
from odoo.addons.queue_job.job import job, related_action

logger = logging.getLogger(__name__)


class AccountInvoice(models.Model):
    _inherit = "account.invoice"

    @api.model
    def thankyou_summary_cron(self):
        """
        Sends a summary each month of the donations
        :return: True
        """
        comm_obj = self.env["partner.communication.job"]
        first = datetime.today().replace(day=1)
        last_month = first - relativedelta(months=1)
        partners = (
            self.env["res.users"]
                .search(
                [
                    "|",
                    "|",
                    ("name", "like", "<NAME>"),
                    ("name", "like", "<NAME>"),
                    ("name", "like", "<NAME>"),
                ]
            )
            .mapped("partner_id")
        )
        invoices = self.search(
            [
                ("type", "=", "out_invoice"),
                ("invoice_category", "!=", "sponsorship"),
                ("state", "=", "paid"),
                ("last_payment", ">=", last_month),
                ("last_payment", "<", first),
            ]
        )
        config = self.env.ref("thankyou_letters.config_thankyou_summary")
        for partner in partners:
            comm_obj.create(
                {
                    "config_id": config.id,
                    "partner_id": partner.id,
                    "object_ids": invoices.ids,
                }
            )
        return True

    @api.multi
    def generate_thank_you(self):
        """
        Creates a thank you letter communication separating events thank you
        and regular thank you.
        override thankyou_letters.generate_thank_you()
        """
        partners = self.mapped("partner_id").filtered(
            lambda p: p.thankyou_preference != "none"
        )
        gift_category = self.env.ref("sponsorship_compassion.product_category_gift")
        for partner in partners:
            invoice_lines = self.mapped("invoice_line_ids").filtered(
                lambda l: l.partner_id == partner
            )
            event_thank = invoice_lines.filtered("event_id")
            other_thank = invoice_lines - event_thank
            for event in event_thank.mapped("event_id"):
                event_thank.filtered(lambda l: l.event_id == event).generate_thank_you()
            if other_thank:
                other_thank.generate_thank_you()

        # Send confirmation to ambassadors
        ambassadors = self.mapped("invoice_line_ids.user_id")
        for ambassador in ambassadors:
            # Filter only donations not for made for himself and filter
            # gifts that are thanked but not directly for ambassador.
            ambassador_lines = self.mapped("invoice_line_ids").filtered(
                lambda l: l.user_id == ambassador
                and l.partner_id != ambassador
                and l.product_id.categ_id != gift_category
            )
            if ambassador_lines:
                ambassador_lines.send_receipt_to_ambassador()

    @api.multi
    def _filter_invoice_to_thank(self):
        """
        Given a recordset of paid invoices, return only those that have
        to be thanked.
        :return: account.invoice recordset
        override thankyou_letters._filter_invoice_to_thanks()
        """
        return self.filtered(
            lambda i: i.type == "out_invoice"
                      and not i.avoid_thankyou_letter
                      and (
                              not i.communication_id
                              or i.communication_id.state in ("call", "pending")
                      )
                      and i.invoice_category != "sponsorship"
                      and (
                              not i.mapped("invoice_line_ids.contract_id")
                              or (
                                      i.invoice_category == "gift"
                                      and i.origin != "Automatic birthday gift"
                                      and "G" not in i.mapped("invoice_line_ids.contract_id.type")
                              )
                      )
        )

    @job(default_channel="root.group_reconcile")
    @related_action(action="related_action_invoices")
    def group_or_split_reconcile(self):
        """Reconcile given invoices with partner open payments.
        """
        super().group_or_split_reconcile()
        # Find if a communication with payment slips is pending and
        # regenerate it.
        jobs = self.env["partner.communication.job"].search(
{'fraction_non_alphanumeric': 0.12560289389067525, 'fraction_numerical': 0.001607717041800643, 'mean_word_length': 1.7902999719652368, 'pattern_counts': {'":': 6, '<': 17, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5281496', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2563, 'n_words': 584}