<commit_before>##############################################################################
#
#    Copyright (C) 2018 Compassion CH (http://www.compassion.ch)
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import uuid
import logging
import base64

from datetime import date

from odoo import api, models, fields, _, SUPERUSER_ID
from odoo.addons.website.models.website import slugify as slug
from odoo.tools import file_open

from odoo.addons.partner_compassion.models.partner_compassion import get_file_type

_logger = logging.getLogger(__name__)

# kanban colors
RED = 2
GREEN = 5


class Event(models.Model):
    _inherit = [
        "website.published.mixin",
        "website.seo.metadata",
        "event.registration",
        "mail.activity.mixin",
        "website.multi.mixin"
    ]
    _name = "event.registration"
    _description = "Event registration"
    _order = "create_date desc"

    ##########################################################################
    #                                 FIELDS                                 #
    ##########################################################################
    user_id = fields.Many2one(
        "res.users",
        "Responsible",
        domain=[("share", "=", False)],
        track_visibility="onchange",
        readonly=False,
    )
    stage_id = fields.Many2one(
        "event.registration.stage",
        "Stage",
        track_visibility="onchange",
        index=True,
        copy=False,
        domain="['|', ('event_type_ids', '=', False),"
               "      ('event_type_ids', '=', event_type_id)]",
        group_expand="_read_group_stage_ids",
        default=lambda r: r._default_stage(),
        readonly=False,
    )
    stage_date = fields.Date(default=fields.Date.today, copy=False)
    stage_task_ids = fields.Many2many(
        "event.registration.task",
        "event_registration_stage_tasks",
        compute="_compute_stage_tasks",
        readonly=False,
    )
    incomplete_task_ids = fields.Many2many(
        "event.registration.task",
        "event_registration_incomplete_tasks",
        string="Incomplete tasks",
        compute="_compute_stage_tasks",
        readonly=False,
    )
    complete_stage_task_ids = fields.Many2many(
        "event.registration.task",
        "event_registration_stage_complete_tasks",
        string="Completed tasks",
        compute="_compute_stage_tasks",
        help="This shows all tasks that the participant completed for the "
             "current stage of his registration.",
        readonly=False,
    )
    completed_task_ids = fields.Many2many(
        "event.registration.task",
        "event_registration_completed_tasks",
        string="Completed tasks",
        copy=False,
        help="This shows all tasks that the participant completed for his "
             "registration.",
        readonly=False,
    )
    color = fields.Integer(compute="_compute_kanban_color")
    compassion_event_id = fields.Many2one(
        "crm.event.compassion", related="event_id.compassion_event_id", readonly=True
    )
    advocate_details_id = fields.Many2one(
        "advocate.details", related="partner_id.advocate_details_id", readonly=True
    )
    fundraising = fields.Boolean(related="event_id.fundraising")
    amount_objective = fields.Integer("Raise objective")
    amount_raised = fields.Float(readonly=True, compute="_compute_amount_raised")
    amount_raised_percents = fields.Integer(
        readonly=True, compute="_compute_amount_raised_percent"
    )
    is_published = fields.Boolean(compute="_compute_is_published", store=True)
    website_url = fields.Char(compute="_compute_website_url")
    host_url = fields.Char(compute="_compute_host_url")
    wordpress_host = fields.Char(compute="_compute_wordpress_host")
    event_name = fields.Char(related="event_id.name", track_visibility="onchange")
    uuid = fields.Char(default=lambda self: self._get_uuid(), copy=False)
    include_flight = fields.Boolean()
    double_room_person = fields.Char("Double room with")

    # The following fields avoid giving read access to the public on the
    # res.partner participating in the event
    partner_id_id = fields.Integer(related="partner_id.id", readonly=True)
    partner_display_name = fields.Char(compute="_compute_partner_display_name")
    partner_preferred_name = fields.Char(
        related="partner_id.preferred_name", readonly=True
    )
    partner_name = fields.Char(related="partner_id.name", readonly=True)
    ambassador_picture_1 = fields.Binary(related="partner_id.image")
    ambassador_quote = fields.Text(related="partner_id.advocate_details_id.quote")
    partner_firstname = fields.Char(related="partner_id.firstname", readonly=True)
    partner_lastname = fields.Char(related="partner_id.lastname", readonly=True)
    partner_gender = fields.Selection(related="partner_id.title.gender", readonly=True)
    comments = fields.Text(track_visibility="onchange")

    has_signed_travel_contract = fields.Boolean(compute="_compute_step2_tasks")
    has_signed_child_protection = fields.Boolean(compute="_compute_step2_tasks")
    passport_uploaded = fields.Boolean(compute="_compute_step2_tasks")
    emergency_ok = fields.Boolean(compute="_compute_step2_tasks")
    criminal_record_uploaded = fields.Boolean(compute="_compute_step2_tasks")
    criminal_record = fields.Binary(
        related="partner_id.criminal_record", readonly=False)
    medical_discharge = fields.Binary(attachment=True, copy=False)
    medical_survey_id = fields.Many2one(
        "survey.user_input", "Medical survey", copy=False, readonly=False
    )
    feedback_survey_id = fields.Many2one(
        "survey.user_input", "Feedback survey", copy=False, readonly=False
    )
    requires_medical_discharge = fields.Boolean(
        compute="_compute_requires_medical_discharge", store=True, copy=False
    )

    # Travel info
    #############
    emergency_name = fields.Char("Emergency contact name")
    emergency_phone = fields.Char("Emergency contact phone number")
    emergency_relation_type = fields.Selection(
        [
            ("husband", "Husband"),
            ("wife", "Wife"),
            ("father", "Father"),
            ("mother", "Mother"),
            ("brother", "Brother"),
            ("sister", "Sister"),
            ("son", "Son"),
            ("daughter", "Daughter"),
            ("friend", "Friend"),
            ("other", "Other"),
        ],
        string="Emergency contact relation type",
    )
    birth_name = fields.Char()
    passport = fields.Binary(compute="_compute_passport", inverse="_inverse_passport")
    passport_number = fields.Char()
    passport_expiration_date = fields.Date()
    flight_ids = fields.One2many(
        "event.flight", "registration_id", "Flights", readonly=False
    )

    # Payment fields
    ################
    down_payment_id = fields.Many2one(
        "account.invoice", "Down payment", copy=False, readonly=False
    )
    group_visit_invoice_id = fields.Many2one(
        "account.invoice", "Trip invoice", copy=False, readonly=False
    )

    survey_count = fields.Integer(compute="_compute_survey_count")
    invoice_count = fields.Integer(compute="_compute_invoice_count")
    website_id = fields.Many2one(
        "website", related="compassion_event_id.website_id", store=True)

    ##########################################################################
    #                             FIELDS METHODS                             #
    ##########################################################################
    def _get_uuid(self):
        return str(uuid.uuid4())

    @api.multi
    def _compute_website_url(self):
        for registration in self:
            registration.website_url = "/event/{}/{}".format(
                slug(registration.compassion_event_id), slug(registration)
            )

    def _compute_amount_raised_percent(self):
        for registration in self:
            objective = (
                registration.amount_objective
                or registration.event_id.participants_amount_objective
            )
            if objective:
                registration.amount_raised_percents = int(
                    registration.amount_raised * 100 // objective
                )

    def _compute_amount_raised(self):
        for registration in self:
            partner = registration.partner_id
            compassion_event = registration.compassion_event_id
            invoice_lines = (
                self.env["account.invoice.line"]
                    .sudo()
                    .search(
                    [
                        ("user_id", "=", partner.id),
                        ("state", "in", ("draft", "open", "paid")),
                        ("event_id", "=", compassion_event.id),
                    ]
                )
            )
            amount_raised = sum(invoice_lines.mapped("price_subtotal_signed"))
            s_value = registration.event_id.sponsorship_donation_value
            if s_value:
                nb_sponsorships = (
                    self.env["recurring.contract"]
                        .sudo()
                        .search_count(
                        [
                            ("user_id", "=", partner.id),
                            ("origin_id.event_id", "=", compassion_event.id),
                            ("state", "!=", "cancelled")
                        ]
                    )
                )
                amount_raised += nb_sponsorships * s_value
            registration.amount_raised = amount_raised<commit_msg>Only take the paid invoices<commit_after>##############################################################################
#
#    Copyright (C) 2018 Compassion CH (http://www.compassion.ch)
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import uuid
import logging
import base64

from datetime import date

from odoo import api, models, fields, _, SUPERUSER_ID
from odoo.addons.website.models.website import slugify as slug
from odoo.tools import file_open

from odoo.addons.partner_compassion.models.partner_compassion import get_file_type

_logger = logging.getLogger(__name__)

# kanban colors
RED = 2
GREEN = 5


class Event(models.Model):
    _inherit = [
        "website.published.mixin",
        "website.seo.metadata",
        "event.registration",
        "mail.activity.mixin",
        "website.multi.mixin"
    ]
    _name = "event.registration"
    _description = "Event registration"
    _order = "create_date desc"

    ##########################################################################
    #                                 FIELDS                                 #
    ##########################################################################
    user_id = fields.Many2one(
        "res.users",
        "Responsible",
        domain=[("share", "=", False)],
        track_visibility="onchange",
        readonly=False,
    )
    stage_id = fields.Many2one(
        "event.registration.stage",
        "Stage",
        track_visibility="onchange",
        index=True,
        copy=False,
        domain="['|', ('event_type_ids', '=', False),"
               "      ('event_type_ids', '=', event_type_id)]",
        group_expand="_read_group_stage_ids",
        default=lambda r: r._default_stage(),
        readonly=False,
    )
    stage_date = fields.Date(default=fields.Date.today, copy=False)
    stage_task_ids = fields.Many2many(
        "event.registration.task",
        "event_registration_stage_tasks",
        compute="_compute_stage_tasks",
        readonly=False,
    )
    incomplete_task_ids = fields.Many2many(
        "event.registration.task",
        "event_registration_incomplete_tasks",
        string="Incomplete tasks",
        compute="_compute_stage_tasks",
        readonly=False,
    )
    complete_stage_task_ids = fields.Many2many(
        "event.registration.task",
        "event_registration_stage_complete_tasks",
        string="Completed tasks",
        compute="_compute_stage_tasks",
        help="This shows all tasks that the participant completed for the "
             "current stage of his registration.",
        readonly=False,
    )
    completed_task_ids = fields.Many2many(
        "event.registration.task",
        "event_registration_completed_tasks",
        string="Completed tasks",
        copy=False,
        help="This shows all tasks that the participant completed for his "
             "registration.",
        readonly=False,
    )
    color = fields.Integer(compute="_compute_kanban_color")
    compassion_event_id = fields.Many2one(
        "crm.event.compassion", related="event_id.compassion_event_id", readonly=True
    )
    advocate_details_id = fields.Many2one(
        "advocate.details", related="partner_id.advocate_details_id", readonly=True
    )
    fundraising = fields.Boolean(related="event_id.fundraising")
    amount_objective = fields.Integer("Raise objective")
    amount_raised = fields.Float(readonly=True, compute="_compute_amount_raised")
    amount_raised_percents = fields.Integer(
        readonly=True, compute="_compute_amount_raised_percent"
    )
    is_published = fields.Boolean(compute="_compute_is_published", store=True)
    website_url = fields.Char(compute="_compute_website_url")
    host_url = fields.Char(compute="_compute_host_url")
    wordpress_host = fields.Char(compute="_compute_wordpress_host")
    event_name = fields.Char(related="event_id.name", track_visibility="onchange")
    uuid = fields.Char(default=lambda self: self._get_uuid(), copy=False)
    include_flight = fields.Boolean()
    double_room_person = fields.Char("Double room with")

    # The following fields avoid giving read access to the public on the
    # res.partner participating in the event
    partner_id_id = fields.Integer(related="partner_id.id", readonly=True)
    partner_display_name = fields.Char(compute="_compute_partner_display_name")
    partner_preferred_name = fields.Char(
        related="partner_id.preferred_name", readonly=True
    )
    partner_name = fields.Char(related="partner_id.name", readonly=True)
    ambassador_picture_1 = fields.Binary(related="partner_id.image")
    ambassador_quote = fields.Text(related="partner_id.advocate_details_id.quote")
    partner_firstname = fields.Char(related="partner_id.firstname", readonly=True)
    partner_lastname = fields.Char(related="partner_id.lastname", readonly=True)
    partner_gender = fields.Selection(related="partner_id.title.gender", readonly=True)
    comments = fields.Text(track_visibility="onchange")

    has_signed_travel_contract = fields.Boolean(compute="_compute_step2_tasks")
    has_signed_child_protection = fields.Boolean(compute="_compute_step2_tasks")
    passport_uploaded = fields.Boolean(compute="_compute_step2_tasks")
    emergency_ok = fields.Boolean(compute="_compute_step2_tasks")
    criminal_record_uploaded = fields.Boolean(compute="_compute_step2_tasks")
    criminal_record = fields.Binary(
        related="partner_id.criminal_record", readonly=False)
    medical_discharge = fields.Binary(attachment=True, copy=False)
    medical_survey_id = fields.Many2one(
        "survey.user_input", "Medical survey", copy=False, readonly=False
    )
    feedback_survey_id = fields.Many2one(
        "survey.user_input", "Feedback survey", copy=False, readonly=False
    )
    requires_medical_discharge = fields.Boolean(
        compute="_compute_requires_medical_discharge", store=True, copy=False
    )

    # Travel info
    #############
    emergency_name = fields.Char("Emergency contact name")
    emergency_phone = fields.Char("Emergency contact phone number")
    emergency_relation_type = fields.Selection(
        [
            ("husband", "Husband"),
            ("wife", "Wife"),
            ("father", "Father"),
            ("mother", "Mother"),
            ("brother", "Brother"),
            ("sister", "Sister"),
            ("son", "Son"),
            ("daughter", "Daughter"),
            ("friend", "Friend"),
            ("other", "Other"),
        ],
        string="Emergency contact relation type",
    )
    birth_name = fields.Char()
    passport = fields.Binary(compute="_compute_passport", inverse="_inverse_passport")
    passport_number = fields.Char()
    passport_expiration_date = fields.Date()
    flight_ids = fields.One2many(
        "event.flight", "registration_id", "Flights", readonly=False
    )

    # Payment fields
    ################
    down_payment_id = fields.Many2one(
        "account.invoice", "Down payment", copy=False, readonly=False
    )
    group_visit_invoice_id = fields.Many2one(
        "account.invoice", "Trip invoice", copy=False, readonly=False
    )

    survey_count = fields.Integer(compute="_compute_survey_count")
    invoice_count = fields.Integer(compute="_compute_invoice_count")
    website_id = fields.Many2one(
        "website", related="compassion_event_id.website_id", store=True)

    ##########################################################################
    #                             FIELDS METHODS                             #
    ##########################################################################
    def _get_uuid(self):
        return str(uuid.uuid4())

    @api.multi
    def _compute_website_url(self):
        for registration in self:
            registration.website_url = "/event/{}/{}".format(
                slug(registration.compassion_event_id), slug(registration)
            )

    def _compute_amount_raised_percent(self):
        for registration in self:
            objective = (
                registration.amount_objective
                or registration.event_id.participants_amount_objective
            )
            if objective:
                registration.amount_raised_percents = int(
                    registration.amount_raised * 100 // objective
                )

    def _compute_amount_raised(self):
        for registration in self:
            partner = registration.partner_id
            compassion_event = registration.compassion_event_id
            invoice_lines = (
                self.env["account.invoice.line"]
                    .sudo()
                    .search(
                    [
                        ("user_id", "=", partner.id),
                        ("state", "=", "paid"),
                        ("event_id", "=", compassion_event.id),
                    ]
                )
            )
            amount_raised = sum(invoice_lines.mapped("price_subtotal_signed"))
            s_value = registration.event_id.sponsorship_donation_value
            if s_value:
                nb_sponsorships = (
                    self.env["recurring.contract"]
                        .sudo()
                        .search_count(
                        [
                            ("user_id", "=", partner.id),
                            ("origin_id.event_id", "=", compassion_event.id),
                            ("state", "!=", "cancelled")
                        ]
                    )
                )
                amount_raised += nb_sponsorships * s_value
            registration.amount_raised = amount_raised
{'fraction_non_alphanumeric': 0.15133271585880415, 'fraction_numerical': 0.0032931974889369147, 'mean_word_length': 2.7224669603524227, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5321037', 'n_tokens_mistral': 5452, 'n_tokens_neox': 5182, 'n_words': 878}