Skip to content
Snippets Groups Projects
Commit fabc8bae authored by Caty Jeanne's avatar Caty Jeanne
Browse files

7

parent 34a03948
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
import sqlite3
# Connect to the database
conn = sqlite3.connect('company.db')
# Create a cursor object
cursor = conn.cursor()
print("Connected to company.db")
```
%% Output
Connected to company.db
%% Cell type:code id: tags:
``` python
import pandas as pd
from datetime import datetime, timedelta
chrismas_day = datetime(2024, 12, 25)
cursor.execute("SELECT NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT FROM EMPLOYE")
employees = cursor.fetchall()
# Create a list to store the data
prime_totale = []
prime_partielle=[]
# Calculate the prime de noel for each employee
for employee in employees:
NUMERO_EMPLOYE, NOM, PRENOM,CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT = employee
start_date = datetime.strptime(DATE_DEBUT, '%Y-%m-%d')
delta = chrismas_day - start_date
if delta.days >= 365:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
prime_totale.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
prime_totale.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
else:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= max(0, salaire_base * delta.days / 365 )
prime_partielle.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel=max(0, salaire_base*100 * delta.days / 365 )
prime_partielle.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
# Create the dataframe
prime_totale = pd.DataFrame(prime_totale, columns=['NUMERO_EMPLOYE','NOM', 'PRENOM' ,'DATE_DEBUT', 'PRIME_NOEL'])
prime_partielle = pd.DataFrame(prime_partielle, columns=['NUMERO_EMPLOYE','NOM', 'PRENOM' , 'DATE_DEBUT', 'PRIME_NOEL'])
prime_totale.head()
```
%% Output
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_3000\1129747334.py in <module>
50
51 chrismas_day = datetime(2024, 12, 25)
---> 52 cursor.execute("SELECT NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT FROM EMPLOYE")
53 employees = cursor.fetchall()
54 # Create a list to store the data
ProgrammingError: Cannot operate on a closed database.
%% Cell type:code id: tags:
``` python
import sqlite3
# Connexion à la base de données
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
# Création de la table LUNDIS si elle n'existe pas déjà
cursor.execute("""
CREATE TABLE IF NOT EXISTS LUNDIS (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date_lundi TEXT UNIQUE
)
""")
# SQL avec WITH RECURSIVE pour générer les lundis
recursive_sql = """
WITH RECURSIVE lundis(date_lundi) AS (
SELECT DATE('2024-12-02') -- Premier lundi
UNION ALL
SELECT DATE(date_lundi, '+7 days')
FROM lundis
WHERE date_lundi < '2025-12-02' -- Limite à 1 an
)
INSERT INTO LUNDIS (date_lundi)
SELECT date_lundi
FROM lundis
WHERE date_lundi NOT IN (SELECT date_lundi FROM LUNDIS); -- Évite les doublons
"""
# Exécution de la requête
cursor.executescript(recursive_sql)
# Vérification des résultats
cursor.execute("SELECT * FROM LUNDIS ORDER BY date_lundi;")
lundis = cursor.fetchall()
# Affichage des lundis insérés
for lundi in lundis:
print(lundi)
# Sauvegarde des changements
conn.commit()
# Fermeture de la connexion
conn.close()
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import sqlite3 import sqlite3
# Connect to the database # Connect to the database
conn = sqlite3.connect('company.db') conn = sqlite3.connect('company.db')
# Create a cursor object # Create a cursor object
cursor = conn.cursor() cursor = conn.cursor()
print("Connected to company.db") print("Connected to company.db")
``` ```
%% Output %% Output
Connected to company.db Connected to company.db
%% Cell type:code id: tags:
``` python
import pandas as pd
from datetime import datetime, timedelta
chrismas_day = datetime(2024, 12, 25)
cursor.execute("SELECT NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT FROM EMPLOYE")
employees = cursor.fetchall()
# Create a list to store the data
prime_totale = []
prime_partielle=[]
def generate_mondays(start_date):
"""
Génère une liste de lundis à partir d'une date donnée jusqu'à un an après.
:param start_date: Date de début au format 'YYYY-MM-DD'
:return: Liste de lundis au format 'YYYY-MM-DD'
"""
# Convertir la date de départ en objet datetime
start = datetime.strptime(start_date, '%Y-%m-%d')
end_date = start + timedelta(days=365)
# Trouver le premier lundi (si la date donnée n'est pas un lundi)
if start.weekday() != 0: # Si ce n'est pas un lundi
start += timedelta(days=(7 - start.weekday())) # Aller au prochain lundi
# Générer les lundis
mondays = []
current_date = start
while current_date <= end_date:
mondays.append(current_date.strftime('%Y-%m-%d'))
current_date += timedelta(weeks=1) # Passer au lundi suivant
return mondays
mondays=generate_mondays('2024-12-04')
```
%% Output
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_3000\983263414.py in <module>
5
6 chrismas_day = datetime(2024, 12, 25)
----> 7 cursor.execute("SELECT NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT FROM EMPLOYE")
8 employees = cursor.fetchall()
9 # Create a list to store the data
ProgrammingError: Cannot operate on a closed database.
%% Cell type:code id: tags:
``` python
# Calculate the prime de noel for each employee
for employee in employees:
NUMERO_EMPLOYE, NOM, PRENOM,CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT = employee
start_date = datetime.strptime(DATE_DEBUT, '%Y-%m-%d')
delta = chrismas_day - start_date
for
if delta.days >= 365:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
prime_totale.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
prime_totale.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
else:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= max(0, salaire_base * delta.days / 365 )
prime_partielle.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel=max(0, salaire_base*100 * delta.days / 365 )
prime_partielle.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
# Create the dataframe
prime_totale = pd.DataFrame(prime_totale, columns=['NUMERO_EMPLOYE','NOM', 'PRENOM' ,'DATE_DEBUT', 'PRIME_NOEL'])
prime_partielle = pd.DataFrame(prime_partielle, columns=['NUMERO_EMPLOYE','NOM', 'PRENOM' , 'DATE_DEBUT', 'PRIME_NOEL'])
prime_totale.head()
```
%% Cell type:code id: tags:
``` python
import sqlite3
# Connexion à la base de données
conn = sqlite3.connect('company.db')
cursor = conn.cursor()
# Création de la table LUNDIS si elle n'existe pas déjà
cursor.execute("""
CREATE TABLE IF NOT EXISTS LUNDIS (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date_lundi TEXT UNIQUE
)
""")
# SQL avec WITH RECURSIVE pour générer les lundis
recursive_sql = """
WITH RECURSIVE lundis(date_lundi) AS (
SELECT DATE('2024-12-02') -- Premier lundi
UNION ALL
SELECT DATE(date_lundi, '+7 days')
FROM lundis
WHERE date_lundi < '2026-12-02' -- Limite à 1 an
)
INSERT INTO LUNDIS (DATE_LUNDI)
SELECT date_lundi
FROM lundis
WHERE date_lundi NOT IN (SELECT DATE_LUNDI FROM LUNDIS); -- Évite les doublons
"""
# Exécution de la requête
cursor.executescript(recursive_sql)
# Vérification des résultats
cursor.execute("SELECT * FROM LUNDIS ORDER BY DATE_LUNDI;")
lundis = cursor.fetchall()
# Affichage des lundis insérés
for lundi in lundis:
print(lundi)
# Sauvegarde des changements
conn.commit()
# Fermeture de la connexion
conn.close()
```
%% Cell type:code id: tags:
``` python
import sqlite3
import pandas as pd
from datetime import datetime, timedelta
# Fonction pour générer les lundis
def generate_mondays(start_date):
"""
Génère une liste de lundis à partir d'une date donnée jusqu'à un an après.
:param start_date: Date de début au format 'YYYY-MM-DD'
:return: Liste de lundis au format 'YYYY-MM-DD'
"""
start = datetime.strptime(start_date, '%Y-%m-%d')
end_date = start + timedelta(days=365)
# Ajuster au premier lundi
if start.weekday() != 0:
start += timedelta(days=(7 - start.weekday()))
mondays = []
current_date = start
while current_date <= end_date:
mondays.append(current_date.strftime('%Y-%m-%d'))
current_date += timedelta(weeks=1)
return mondays
# Utilisation de SQLite
try:
# Connexion à la base de données
conn = sqlite3.connect('company.db')
cursor = conn.cursor()
print("Connected to company.db")
# Génération des lundis
mondays = generate_mondays(datetime.now().strftime('%Y-%m-%d'))
# Exemple d'opérations avec les employés
chrismas_day = datetime(2024, 12, 25)
cursor.execute("SELECT NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION, CS_JOURNEE, DATE_DEBUT FROM EMPLOYE")
employees = cursor.fetchall()
prime_totale = []
prime_partielle = []
Bonus=[]
for employee in employees:
NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION, CS_JOURNEE, DATE_DEBUT = employee
start_date = datetime.strptime(DATE_DEBUT, '%Y-%m-%d')
for LUNDI in mondays:
lundi = datetime.strptime(LUNDI, '%Y-%m-%d')
delta = lundi - start_date
delta_days = delta.days
delta_noel =lundi - chrismas_day
delta_noel_days=delta_noel.days
prime_noel=0
prime_anniversaire=0
if 0 <= delta_noel_days%365<= 6:
if delta.days >= 365:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
else:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= max(0, salaire_base * delta.days / 365 )
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel=max(0, salaire_base*100 * delta.days / 365 )
if 0 <= abs(delta_days%365)<= 6:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_anniversaire= salaire_base
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_anniversaire= salaire_base
Bonus.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, LUNDI, prime_noel+prime_anniversaire))
# Sauvegarde des lundis dans une table de la base
cursor.executemany("INSERT OR IGNORE INTO LUNDIS (date_lundi) VALUES (?)", [(monday,) for monday in mondays])
conn.commit()
print("Mondays added to the database.")
except sqlite3.Error as e:
print(f"SQLite error: {e}")
```
%% Output
Connected to company.db
Mondays added to the database.
%% Cell type:code id: tags:
``` python
Bonus = pd.DataFrame(Bonus, columns=['NUMERO_EMPLOYE','NOM', 'PRENOM' ,'DATE_DEBUT', 'LUNDI','PRIMES'])
Bonus.head()
```
%% Output
NUMERO_EMPLOYE NOM PRENOM DATE_DEBUT LUNDI PRIMES
0 1 Dubois Michel 2020-11-13 2024-12-09 0.0
1 1 Dubois Michel 2020-11-13 2024-12-16 0.0
2 1 Dubois Michel 2020-11-13 2024-12-23 0.0
3 1 Dubois Michel 2020-11-13 2024-12-30 2160.0
4 1 Dubois Michel 2020-11-13 2025-01-06 0.0
%% Cell type:code id: tags:
``` python
datetime.now().strftime('%Y-%m-%d')
```
%% Output
'2024-12-04'
%% Cell type:code id: tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment