<commit_before># -*- coding: utf-8 -*-

from __future__ import division

from openfisca_france.model.base import *  # noqa analysis:ignore

from numpy import maximum as max_, round as round_, minimum as min_, logical_not as not_, where, select

class ppa_eligibilite(Variable):
    column = BoolCol
    entity_class = Familles
    label = u"Eligibilité à la PPA pour un mois"

    def function(self, simulation, period):
        period = period.this_month
        P = simulation.legislation_at(period.start)
        age_min = P.minim.ppa.age_min
        condition_age_individus = simulation.calculate('age', period) >= age_min
        condition_age = self.any_by_roles(condition_age_individus)
        elig = condition_age

        return period, elig

class ppa_eligibilite_etudiants(Variable):
    column = BoolCol
    entity_class = Familles
    label = u"Eligibilité à la PPA (condition sur tout le trimestre)"

    def function(self, simulation, period):
        P = simulation.legislation_at(period.start)
        period = period.this_month
        ppa_majoree_eligibilite = simulation.calculate('rsa_majore_eligibilite', period)

        # Pour un individu
        etudiant = simulation.calculate('etudiant', period) # individu
        plancher_ressource = 169 * P.cotsoc.gen.smic_h_b * P.prestations.prestations_familiales.af.seuil_rev_taux
        def condition_ressource(period):
            revenu_activite = simulation.calculate('ppa_revenu_activite_individu', period)
            return revenu_activite > plancher_ressource
        m_1 = period.offset(-1, 'month')
        m_2 = period.offset(-2, 'month')
        m_3 = period.offset(-3, 'month')
        condition_etudiant_i = condition_ressource(m_1) * condition_ressource(m_2) * condition_ressource(m_3)

        # Au moins une personne de la famille doit être non étudiant ou avoir des ressources > plancher
        condition_famille = self.any_by_roles(not_(etudiant) + condition_etudiant_i, roles = [CHEF, CONJ])
        return period, ppa_majoree_eligibilite + condition_famille

class ppa_montant_forfaitaire_familial_non_majore(Variable):
    column = FloatCol
    entity_class = Familles
    label = u"Montant forfaitaire familial (sans majoration)"

    def function(self, simulation, period):
        period = period.this_month
        nb_parents = simulation.calculate('nb_parents', period)
        nb_enfants = simulation.calculate('rsa_nb_enfants', period)
        ppa_majoree_eligibilite = simulation.calculate('rsa_majore_eligibilite', period)
        rmi = simulation.legislation_at(period.start).minim.rmi
        nb_personnes = nb_parents + nb_enfants

        taux_non_majore = (
            1 +
            (nb_personnes >= 2) * rmi.txp2 +
            (nb_personnes >= 3) * rmi.txp3 +
            (nb_personnes >= 4) * where(nb_parents == 1, rmi.txps, rmi.txp3) + # Si nb_parents == 1, pas de conjoint, la 4e personne est un enfant, donc le taux est de 40%.
            max_(nb_personnes - 4, 0) * rmi.txps
            )

        return period, rmi.rmi * taux_non_majore

class ppa_montant_forfaitaire_familial_majore(Variable):
    column = FloatCol
    entity_class = Familles
    label = u"Montant forfaitaire familial (avec majoration)"

    def function(self, simulation, period):
        nb_enfants = simulation.calculate('rsa_nb_enfants', period)
        rsa = simulation.legislation_at(period.start).minima_sociaux.rsa
        taux_majore = rmi.majo_rsa.pac0 + rmi.majo_rsa.pac_enf_sup * nb_enfants

        return period, rsa.montant_de_base_du_rsa * taux_majore

class ppa_revenu_activite(Variable):
    column = FloatCol
    entity_class = Familles
    label = u"Revenu d'activité pris en compte pour la PPA"

    def function(self, simulation, period):
        period = period.this_month
        ppa_revenu_activite_individus = simulation.compute('ppa_revenu_activite_individu', period)
        ppa_revenu_activite = self.sum_by_entity(ppa_revenu_activite_individus)

        return period, ppa_revenu_activite

class ppa_revenu_activite_individu(Variable):
    column = FloatCol
    entity_class = Individus<commit_msg>Correct error in ppa_montant_forfaitaire_familial_majore<commit_after># -*- coding: utf-8 -*-

from __future__ import division

from openfisca_france.model.base import *  # noqa analysis:ignore

from numpy import maximum as max_, round as round_, minimum as min_, logical_not as not_, where, select

class ppa_eligibilite(Variable):
    column = BoolCol
    entity_class = Familles
    label = u"Eligibilité à la PPA pour un mois"

    def function(self, simulation, period):
        period = period.this_month
        P = simulation.legislation_at(period.start)
        age_min = P.minim.ppa.age_min
        condition_age_individus = simulation.calculate('age', period) >= age_min
        condition_age = self.any_by_roles(condition_age_individus)
        elig = condition_age

        return period, elig

class ppa_eligibilite_etudiants(Variable):
    column = BoolCol
    entity_class = Familles
    label = u"Eligibilité à la PPA (condition sur tout le trimestre)"

    def function(self, simulation, period):
        P = simulation.legislation_at(period.start)
        period = period.this_month
        ppa_majoree_eligibilite = simulation.calculate('rsa_majore_eligibilite', period)

        # Pour un individu
        etudiant = simulation.calculate('etudiant', period) # individu
        plancher_ressource = 169 * P.cotsoc.gen.smic_h_b * P.prestations.prestations_familiales.af.seuil_rev_taux
        def condition_ressource(period):
            revenu_activite = simulation.calculate('ppa_revenu_activite_individu', period)
            return revenu_activite > plancher_ressource
        m_1 = period.offset(-1, 'month')
        m_2 = period.offset(-2, 'month')
        m_3 = period.offset(-3, 'month')
        condition_etudiant_i = condition_ressource(m_1) * condition_ressource(m_2) * condition_ressource(m_3)

        # Au moins une personne de la famille doit être non étudiant ou avoir des ressources > plancher
        condition_famille = self.any_by_roles(not_(etudiant) + condition_etudiant_i, roles = [CHEF, CONJ])
        return period, ppa_majoree_eligibilite + condition_famille

class ppa_montant_forfaitaire_familial_non_majore(Variable):
    column = FloatCol
    entity_class = Familles
    label = u"Montant forfaitaire familial (sans majoration)"

    def function(self, simulation, period):
        period = period.this_month
        nb_parents = simulation.calculate('nb_parents', period)
        nb_enfants = simulation.calculate('rsa_nb_enfants', period)
        ppa_majoree_eligibilite = simulation.calculate('rsa_majore_eligibilite', period)
        rmi = simulation.legislation_at(period.start).minim.rmi
        nb_personnes = nb_parents + nb_enfants

        taux_non_majore = (
            1 +
            (nb_personnes >= 2) * rmi.txp2 +
            (nb_personnes >= 3) * rmi.txp3 +
            (nb_personnes >= 4) * where(nb_parents == 1, rmi.txps, rmi.txp3) + # Si nb_parents == 1, pas de conjoint, la 4e personne est un enfant, donc le taux est de 40%.
            max_(nb_personnes - 4, 0) * rmi.txps
            )

        return period, rmi.rmi * taux_non_majore

class ppa_montant_forfaitaire_familial_majore(Variable):
    column = FloatCol
    entity_class = Familles
    label = u"Montant forfaitaire familial (avec majoration)"

    def function(self, simulation, period):
        nb_enfants = simulation.calculate('rsa_nb_enfants', period)
        rsa = simulation.legislation_at(period.start).prestations.minima_sociaux.rsa
        taux_majore = rmi.majo_rsa.pac0 + rmi.majo_rsa.pac_enf_sup * nb_enfants

        return period, rsa.montant_de_base_du_rsa * taux_majore

class ppa_revenu_activite(Variable):
    column = FloatCol
    entity_class = Familles
    label = u"Revenu d'activité pris en compte pour la PPA"

    def function(self, simulation, period):
        period = period.this_month
        ppa_revenu_activite_individus = simulation.compute('ppa_revenu_activite_individu', period)
        ppa_revenu_activite = self.sum_by_entity(ppa_revenu_activite_individus)

        return period, ppa_revenu_activite

class ppa_revenu_activite_individu(Variable):
    column = FloatCol
    entity_class = Individus
{'fraction_non_alphanumeric': 0.08143322475570032, 'fraction_numerical': 0.006755941609361805, 'mean_word_length': 3.4166222695791157, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6342557', 'n_tokens_mistral': 2878, 'n_tokens_neox': 2786, 'n_words': 572}