import exchange rate

This commit is contained in:
2025-11-01 16:36:49 +01:00
parent ededa6d947
commit 77340d2cf6
10 changed files with 2353 additions and 2318 deletions

View File

@@ -30,7 +30,7 @@ create extension if not exists pgtap schema pgtap;
-- ----------------------------------------------------------------------
create schema geo;
select 'Pays ------------------' as msg;
select 'Pays -------------------' as msg;
create table geo.pays (
code2 text not null,
@@ -99,7 +99,7 @@ drop table pays_tmp;
-- langues
-- ----------------------------------------------------------------------
select 'Langues ---------------' as msg;
select 'Langues ----------------' as msg;
create table geo.langues (
code3 char(3) not null,
@@ -275,6 +275,28 @@ create table categorie (
categorie text not null
);
CREATE TABLE employees (
"EmployeeID" smallint NOT NULL,
"LastName" character varying(20) NOT NULL,
"FirstName" character varying(10) NOT NULL,
"Title" character varying(30),
"TitleOfCourtesy" character varying(25),
"BirthDate" date,
"HireDate" date,
"Address" character varying(60),
"City" character varying(15),
"Region" character varying(15),
"PostalCode" character varying(10),
"Country" character varying(15),
"HomePhone" character varying(24),
"Extension" character varying(4),
"Photo" bytea,
"Notes" text,
"ReportsTo" smallint,
"PhotoPath" character varying(255)
);
create table region (
id int primary key,
region text not null
@@ -293,11 +315,13 @@ create table personne (
);
create table societe (
id bigint generated always as identity,
id bigint primary key generated always as identity,
societe text
)
);
insert into societe OVERRIDING SYSTEM VALUE values
insert into societe
OVERRIDING SYSTEM VALUE
values
(1, 'Supérette'),
(2, 'Boulangerie Lagarde'),
(3, 'Pharmacie Martin'),
@@ -327,15 +351,16 @@ insert into societe OVERRIDING SYSTEM VALUE values
(27, 'Banque de l''Étoile'),
(28, 'Pizzeria Geppetto');
/*
CREATE TABLE emplois (
id bigint generated always as identity,
id_personne int NOT NULL,
id_societe int NOT NULL,
dates daterange,
temps_travail decimal(5,2) DEFAULT 151,67 CHECK(temps_travail > 0 AND temps_travail <= 400),
temps_travail decimal(5,2) DEFAULT 151.67 CHECK(temps_travail > 0 AND temps_travail <= 400),
salaire_mensuel decimal(10,2) NOT NULL,
poste text,
FOREIGN KEY (id_personne) REFERENCES personnes(id_personne),
FOREIGN KEY (id_personne) REFERENCES personne(id),
FOREIGN KEY (id_societe) REFERENCES societe(id)
);
*/
@@ -372,7 +397,7 @@ $$ LANGUAGE plpgsql;
-- Devises (currencies)
-- ----------------------------------------------------------------------
create table currency (
create table banque.currency (
code text not null,
num4217 integer default null,
symbole character varying(5) default null,
@@ -383,7 +408,7 @@ create table currency (
minors text default null
);
alter table currency
alter table banque.currency
add check (code ~ '^[A-Z]{3}$');
create table pays_devises (
@@ -399,13 +424,13 @@ alter table pays_devises
add check (devise_code ~ '^[A-Z]{3}$');
create unique index currency_pk
on currency
on banque.currency
using btree (code);
alter table currency
alter table banque.currency
add primary key using index currency_pk;
\copy currency from '/tmp/geo/devises.csv' (FORMAT CSV, header, delimiter ',', ENCODING 'UTF8');
\copy banque.currency from '/tmp/geo/devises.csv' (FORMAT CSV, header, delimiter ',', ENCODING 'UTF8');
\copy pays_devises from '/tmp/geo/devises_pays.csv' (FORMAT CSV, header, delimiter ',', ENCODING 'UTF8');
-- pays_devises -> pays
@@ -416,16 +441,59 @@ alter table only pays_devises
-- pays_devises -> devises
alter table only pays_devises
add foreign key (devise_code)
references currency (code);
references banque.currency (code);
CREATE TABLE banque.exchange_rate (
from_currency CHAR(3) references currency(code),
to_currency CHAR(3) references currency(code),
rate DECIMAL(12,6) NOT NULL,
fee_percent DECIMAL(5,2) DEFAULT 0, -- frais en %
last_updated TIMESTAMP DEFAULT NOW(),
PRIMARY KEY (from_currency, to_currency)
from_currency CHAR(3) references banque.currency(code),
to_currency CHAR(3) references banque.currency(code),
rate DECIMAL(12,6) NOT NULL,
fee_percent DECIMAL(5,2) DEFAULT 0,
last_updated TIMESTAMP DEFAULT NOW(),
PRIMARY KEY (from_currency, to_currency, last_updated)
);
create temporary table exchange (
jour date,
rate decimal(12,6)
);
-- CHF
DO $$
DECLARE
rec RECORD;
path TEXT;
BEGIN
-- Liste des devises à importer
FOR rec IN
SELECT code FROM banque.currency WHERE code <> 'EUR'
LOOP
path := format('/tmp/webstat/Webstat_Export_fr_EXR.M.%s.EUR.SP00.E.csv', rec.code);
-- Import CSV temporaire
EXECUTE format(
$sql$
copy exchange FROM %L (FORMAT CSV, HEADER, DELIMITER ';', ENCODING 'UTF8')
$sql$,
path
);
-- Insertion dans la table principale
EXECUTE format(
$sql$
INSERT INTO banque.exchange_rate (from_currency, to_currency, rate, fee_percent, last_updated)
SELECT 'EUR', %L, rate, 0, jour FROM exchange
$sql$,
rec.code
);
-- Nettoyage
TRUNCATE TABLE exchange;
END LOOP;
END $$;
DROP table exchange;
-- ----------------------------------------------------------------------
CREATE TABLE banque.account (
@@ -518,9 +586,9 @@ BEGIN
ELSE
SELECT rate, fee_percent INTO rate, fee
FROM banque.exchange_rate
WHERE from_currency = from_currency AND to_currency = to_currency;
WHERE from_currency = from_currency AND to_currency = to_currency ORDER BY last_updated desc LIMIT 1;
converted_amount := amount * rate * (1 - fee/100);
converted_amount := amount * rate * (1 - fee / 100);
END IF;
-- Débit
@@ -533,7 +601,7 @@ BEGIN
-- Mise à jour des soldes
UPDATE banque.account SET balance = balance - amount WHERE id = from_account_id;
UPDATE bansue.account SET balance = balance + converted_amount WHERE id = to_account_id;
UPDATE banque.account SET balance = balance + converted_amount WHERE id = to_account_id;
-- Génération du hash blockchain
SELECT encode(digest(concat(tx_id, description, prev_hash, NOW()::text), 'sha256'), 'hex') INTO new_hash;

View File

@@ -15,3 +15,5 @@ truncate table fournisseur;
\COPY marque FROM '/tmp/marque.csv' (FORMAT CSV, header, ENCODING 'UTF8');
\COPY fournisseur FROM '/tmp/fournisseur.csv' (FORMAT CSV, header, ENCODING 'UTF8');
\COPY produit FROM '/tmp/produits/cereales_petitdejeuner.csv' (FORMAT CSV, header, ENCODING 'UTF8');
\COPY personne FROM '/tmp/personne.csv' (FORMAT CSV, header, ENCODING 'UTF8');