Skip to content

Commit

Permalink
Use script_ast generator
Browse files Browse the repository at this point in the history
  • Loading branch information
benjello committed Nov 16, 2024
1 parent 127d23b commit a4bd5c5
Show file tree
Hide file tree
Showing 16 changed files with 873 additions and 86 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* Move parameters from xml format to yaml files tree

### 1.0.0
* Renomme `nb_trim_val` en `trimestres_valides`
* Renomme `nb_trim_val` en `duree_assurance`
* Utilisation de noms longs pour différent paramètres

## 0.9.2
Expand Down
37 changes: 36 additions & 1 deletion openfisca_tunisia_pension/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
from openfisca_tunisia_pension.tunisia_pension_taxbenefitsystem import TunisiaPensionTaxBenefitSystem
'''OpenFisca Tunisia Pension tax-benefit system.'''


import logging
import os

from openfisca_core.taxbenefitsystems import TaxBenefitSystem

from openfisca_tunisia_pension import entities
from openfisca_tunisia_pension.scripts_ast import script_ast


COUNTRY_DIR = os.path.dirname(os.path.abspath(__file__))

logging.getLogger('numba.core.ssa').disabled = True
logging.getLogger('numba.core.byteflow').disabled = True
logging.getLogger('numba.core.interpreter').disabled = True

# Convert regimes classes to OpenFisca variables.
script_ast.main(verbose = False)


class TunisiaPensionTaxBenefitSystem(TaxBenefitSystem):
'''Tunisian pensions tax benefit system'''
CURRENCY = 'DT'

def __init__(self):
super(TunisiaPensionTaxBenefitSystem, self).__init__(entities.entities)

# We add to our tax and benefit system all the variables
self.add_variables_from_directory(os.path.join(COUNTRY_DIR, 'variables'))

# We add to our tax and benefit system all the legislation parameters defined in the parameters files
parameters_path = os.path.join(COUNTRY_DIR, 'parameters')
self.load_parameters(parameters_path)


CountryTaxBenefitSystem = TunisiaPensionTaxBenefitSystem
Empty file.
249 changes: 249 additions & 0 deletions openfisca_tunisia_pension/regimes/regime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
'''Abstract regimes definition.'''


from openfisca_core.model_api import *
# from openfisca_core.errors.variable_not_found_error import VariableNotFoundError

# Import the Entities specifically defined for this tax and benefit system
from openfisca_tunisia_pension.entities import Individu


class AbstractRegime(object):
name = None
variable_prefix = None
parameters = None

class cotisation(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'cotisation retraite employeur'

def formula(individu, period, parameters):
NotImplementedError

class duree_assurance(Variable):
value_type = int
entity = Individu
definition_period = YEAR
label = "Durée d'assurance (trimestres validés)"

# def formula(individu, period, parameters):
# duree_assurance_validee = individu("regime_name_duree_assurance_validee", period)
# annee_de_liquidation = individu('regime_name_liquidation_date', period).astype('datetime64[Y]').astype(int) + 1970
# majoration_duree_assurance = individu('regime_name_majoration_duree_assurance', period)
# return where(
# annee_de_liquidation == period.start.year,
# round_(duree_assurance_validee + majoration_duree_assurance), # On arrondi l'année de la liquidation
# duree_assurance_validee
# )

class liquidation_date(Variable):
value_type = date
entity = Individu
definition_period = ETERNITY
label = 'Date de liquidation'
default_value = date(2250, 12, 31)

class majoration_pension(Variable):
value_type = int
entity = Individu
definition_period = MONTH
label = 'Majoration de pension'

def formula(individu, period, parameters):
NotImplementedError

class pension(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension'

def formula(individu, period, parameters):
NotImplementedError

class pension_brute(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension brute'

def formula(individu, period, parameters):
NotImplementedError

class pension_servie(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension servie'

def formula(individu, period, parameters):
NotImplementedError


class AbstractRegimeEnAnnuites(AbstractRegime):
name = 'Régime en annuités'
variable_prefix = 'regime_en_annuites'
parameters = 'regime_en_annuites'

class duree_assurance_annuelle(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = "Durée d'assurance (en trimestres validés l'année considérée)"

class majoration_duree_assurance(Variable):
value_type = float
entity = Individu
definition_period = ETERNITY
label = "Majoration de durée d'assurance"

def formula(individu, period):
return (
individu('regime_name_majoration_duree_assurance_enfant', period)
+ individu('regime_name_majoration_duree_assurance_autre', period)
)

class majoration_duree_assurance_autre(Variable):
value_type = float
entity = Individu
definition_period = ETERNITY
label = "Majoration de durée d'assurance autre que celle attribuée au motif des enfants"

class majoration_pension(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Majoration de pension'

class majoration_pension_au_31_decembre(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Majoration de pension au 31 décembre'

def formula(individu, period, parameters):
annee_de_liquidation = individu('regime_name_liquidation_date', period).astype('datetime64[Y]').astype(int) + 1970
# Raccouci pour arrêter les calculs dans le passé quand toutes les liquidations ont lieu dans le futur
if all(annee_de_liquidation > period.start.year):
return individu.empty_array()
last_year = period.last_year
majoration_pension_au_31_decembre_annee_precedente = individu('regime_name_majoration_pension_au_31_decembre', last_year)
revalorisation = parameters(period).regime_name.revalorisation_pension_au_31_decembre
majoration_pension = individu('regime_name_majoration_pension', period)
return revalorise(
majoration_pension_au_31_decembre_annee_precedente,
majoration_pension,
annee_de_liquidation,
revalorisation,
period,
)

class pension(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension'

def formula(individu, period):
pension_brute = individu('regime_name_pension_brute', period)
majoration_pension = individu('regime_name_majoration_pension', period)
return pension_brute + majoration_pension

class pension_au_31_decembre(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension'

def formula(individu, period):
pension_brute_au_31_decembre = individu('regime_name_pension_brute_au_31_decembre', period)
majoration_pension_au_31_decembre = individu('regime_name_majoration_pension_au_31_decembre', period)
return pension_brute_au_31_decembre + majoration_pension_au_31_decembre

class pension_brute_au_31_decembre(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension brute au 31 décembre'

def formula(individu, period, parameters):
annee_de_liquidation = individu('regime_name_liquidation_date', period).astype('datetime64[Y]').astype(int) + 1970
# Raccouci pour arrêter les calculs dans le passé quand toutes les liquidations ont lieu dans le futur
if all(period.start.year < annee_de_liquidation):
return individu.empty_array()
last_year = period.last_year
pension_brute_au_31_decembre_annee_precedente = individu('regime_name_pension_brute_au_31_decembre', last_year)
revalorisation = parameters(period).regime_name.revalorisation_pension_au_31_decembre
pension_brute = individu('regime_name_pension_brute', period)
return revalorise(
pension_brute_au_31_decembre_annee_precedente,
pension_brute,
annee_de_liquidation,
revalorisation,
period,
)

class pension_servie(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Pension servie'

def formula(individu, period, parameters):
annee_de_liquidation = individu('regime_name_liquidation_date', period).astype('datetime64[Y]').astype(int) + 1970
# Raccouci pour arrêter les calculs dans le passé quand toutes les liquidations ont lieu dans le futur
if all(annee_de_liquidation > period.start.year):
return individu.empty_array()
last_year = period.last_year
pension_au_31_decembre_annee_precedente = individu('regime_name_pension_au_31_decembre', last_year)
revalorisation = parameters(period).regime_name.revalarisation_pension_servie
pension = individu('regime_name_pension_au_31_decembre', period)
return revalorise(
pension_au_31_decembre_annee_precedente,
pension,
annee_de_liquidation,
revalorisation,
period,
)

class salaire_de_base(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Salaire de base (salaire brut)'
set_input = set_input_divide_by_period

class salaire_de_reference(Variable):
value_type = float
entity = Individu
definition_period = ETERNITY
label = 'Salaire de référence'

class taux_de_liquidation(Variable):
value_type = float
entity = Individu
definition_period = YEAR
label = 'Taux de liquidation de la pension'

def formula(individu, period, parameters):
decote = individu('regime_name_decote', period)
surcote = individu('regime_name_surcote', period)
taux_plein = parameters(period).regime_name.taux_plein.taux_plein
return taux_plein * (1 - decote + surcote)


# def revalorise(variable_31_decembre_annee_precedente, variable_originale, annee_de_liquidation, revalorisation, period):
# return select(
# [
# annee_de_liquidation > period.start.year,
# annee_de_liquidation == period.start.year,
# annee_de_liquidation < period.start.year,
# ],
# [
# 0,
# variable_originale,
# variable_31_decembre_annee_precedente * revalorisation
# ]
# )
88 changes: 88 additions & 0 deletions openfisca_tunisia_pension/regimes/rsa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'''Régime des salariés agricoles.'''


from openfisca_core.model_api import *
from openfisca_core import periods

from openfisca_tunisia_pension.entities import Individu
from openfisca_tunisia_pension.regimes.regime import AbstractRegimeEnAnnuites
# from openfisca_tunisia_pension.tools import add_vectorial_timedelta, year_


import functools
from numpy import (
apply_along_axis,
logical_not as not_,
maximum as max_,
vstack,
)

from openfisca_tunisia_pension.variables.helpers import mean_over_k_largest, pension_generique


class RegimeRSA(AbstractRegimeEnAnnuites):
name = 'Régime des salariés agricoles'
variable_prefix = 'rsa'
parameters_prefix = 'rsa'

class salaire_reference(Variable):
value_type = float
entity = Individu
label = 'Salaires de référence du régime des salariés agricoles'
definition_period = YEAR

def formula(individu, period):
# TODO: gérer le nombre d'année
# TODO: plafonner les salaires à 2 fois le smag de l'année d'encaissement
base_declaration_rsa = 180
base_liquidation_rsa = 300

n = 3
mean_over_largest = functools.partial(mean_over_k_largest, k = n)
salaire = apply_along_axis(
mean_over_largest,
axis = 0,
arr = vstack([
individu('salaire', period = periods.period('year', year))
for year in range(period.start.year, period.start.year - n, -1)
]),
)
salaire_refererence = salaire * base_liquidation_rsa / base_declaration_rsa
return salaire_refererence


class pension(Variable):
value_type = float
entity = Individu
label = 'Salaires de référence du régime des salariés agricoles'
definition_period = YEAR

def formula(individu, period, parameters):
rsa = parameters(period).retraite.regime_name
taux_annuite_base = rsa.taux_annuite_base
taux_annuite_supplementaire = rsa.taux_annuite_supplementaire
duree_stage = rsa.stage_requis
age_elig = rsa.age_legal
periode_remplacement_base = rsa.periode_remplacement_base
plaf_taux_pension = rsa.plaf_taux_pension
smag = parameters(period).marche_travail.smag * 25
duree_stage_validee = duree_assurance > 4 * duree_stage
pension_min = rsa.pension_min
salaire_reference = individu('regime_name_salaire_reference', period)

montant = pension_generique(
duree_assurance,
sal_ref,
taux_annuite_base,
taux_annuite_supplementaire,
duree_stage,
age_elig,
periode_remplacement_base,
plaf_taux_pension
)

elig_age = age > age_elig
elig = duree_stage_validee * elig_age * (salaire_reference > 0)
montant_percu = max_(montant, pension_min * smag)
pension = elig * montant_percu
return pension
Loading

0 comments on commit a4bd5c5

Please sign in to comment.