This commit is contained in:
2025-11-01 08:18:17 +01:00
parent 38c1028860
commit da27e8f869
15 changed files with 2772 additions and 163 deletions

View File

@@ -14,7 +14,7 @@ create schema ext;
create extension if not exists ltree schema ext;
create extension if not exists pgcrypto schema ext;
create extension if not exists vector schema ext;
create extension if not exists isn;
create extension if not exists isn schema ext;
create schema pgtap;
create extension if not exists pgtap schema pgtap;
@@ -208,7 +208,7 @@ create table fournisseur (
-- ----------------------------------------------------------------------
create table produit (
id bigint primary key,
ean13 EAN13 null,
ean13 ext.EAN13 null,
nom text not null,
marque text null,
categorie text null,
@@ -293,7 +293,15 @@ create table ligne (
);
alter table ligne
add column total decimal generated always as (prix_unitaire * quantite) stored;
add column total decimal
generated always as (prix_unitaire * quantite) stored;
create table prix_historique (
id int generated always as identity,
article_code text not null,
prix_unitaire decimal not null,
dates daterange not null
);
create table marque (
id int primary key,
@@ -334,3 +342,119 @@ INSERT INTO genres (genre_id, genre) VALUES
(12,'Biographie'),
(13,'Cyberpunk'),
(14,'Steampunk');
-- ----------------------------------------------------------------------
-- Banque
-- ----------------------------------------------------------------------
create table personne (
id bigint generated always as identity,
prenom text,
nom text,
telephone text,
ville text
);
create table societe (
id bigint generated always as identity,
societe text
)
insert into societe OVERRIDING SYSTEM VALUE values
(1, 'Supérette'),
(2, 'Boulangerie Lagarde'),
(3, 'Pharmacie Martin'),
(4, 'Diminutif'),
(5, 'Vélocité'),
(6, 'Café du Marché'),
(7, 'La Maison Fleurie'),
(8, 'Librairie des Tilleuls'),
(9, 'MétalTech SARL'),
(10, 'BoisDesign'),
(11, 'Les Délices du Terroir'),
(12, 'VitiVerte'),
(13, 'ÉlectroServ'),
(14, 'Ateliers du Moulin'),
(15, 'Comptexpert'),
(16, 'Assur O'' Poil'),
(17, 'Banque Régionale du Centre'),
(18, 'ImmoVilla'),
(19, 'ITLink Solutions'),
(20, 'Studio Graphica '),
(21, 'Mairie de Batz'),
(22, 'Hôtel du rivage'),
(23, 'Collège Marie Curie'),
(24, 'École primaire des Lilas'),
(25, 'Maison de retraite Les Acacias'),
(26, 'Cabinet Médical du Parc'),
(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),
salaire_mensuel decimal(10,2) NOT NULL,
poste text,
FOREIGN KEY (id_personne) REFERENCES personnes(id_personne),
FOREIGN KEY (id_societe) REFERENCES societe(id)
);
insert into emplois values
CREATE TABLE account (
id bigint generated always as identity,
account_number TEXT UNIQUE NOT NULL,
balance NUMERIC(18,2) NOT NULL DEFAULT 0,
currency CHAR(3) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);
CREATE TABLE "transaction" (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
reference TEXT,
amount NUMERIC(18,2) NOT NULL,
currency CHAR(3) NOT NULL,
from_account BIGINT NOT NULL REFERENCES account(id),
to_account BIGINT NOT NULL REFERENCES account(id),
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
processed BOOLEAN NOT NULL DEFAULT FALSE -- indique si ledger + soldes ont été appliqués
);
-- ledger (écritures comptables immuables) : append-only
CREATE TABLE ledger_entry (
id bigint generated always as identity,
transaction_id UUID NOT NULL REFERENCES "transaction"(id),
account_id BIGINT NOT NULL REFERENCES account(id),
amount NUMERIC(18,2) NOT NULL, -- convention: positif = crédit, négatif = débit (ici from = -amount, to = +amount)
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
description TEXT
);
-- index pour performance et idempotence par transaction/account
CREATE UNIQUE INDEX ux_ledger_tx_account ON ledger_entry(transaction_id, account_id);
-- outbox pour publisher reliable (pattern outbox)
CREATE TABLE outbox_event (
id bigint generated always as identity,
occurrenced_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
topic TEXT NOT NULL,
payload JSONB NOT NULL,
transaction_id UUID, -- lien optionnel
processed BOOLEAN NOT NULL DEFAULT FALSE,
processed_at TIMESTAMP WITH TIME ZONE NULL
);
-- table very simple de blockchain / chain d'audit
CREATE TABLE block_chain (
id bigint generated always as identity,
block_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
tx_id UUID NOT NULL, -- transaction incluse dans ce bloc (ou multiple selon choix)
previous_hash TEXT NULL,
block_hash TEXT NOT NULL,
block_data JSONB NOT NULL -- stockage lisible des éléments du bloc (pour audit)
);
CREATE INDEX idx_block_chain_txid ON block_chain(tx_id);

View File

@@ -29,3 +29,18 @@ join ligne l on l.ticket_id = t.id
join adherent h on h.id = t.adherent_id
join article a on a.code = l.article_code
group by t.id, h.id, l.article_code, a.famille_code;
/*
INSERT INTO prix_historique (article_code, prix_unitaire, dates)
SELECT
article_code,
prix_unitaire,
daterange(
date(MIN(t.date_ticket)),
date(LEAD(MIN(t.date_ticket)) OVER (PARTITION BY article_code ORDER BY MIN(t.date_ticket)))
) AS dates
FROM ligne l
JOIN ticket t ON t.id = l.ticket_id
GROUP BY article_code, prix_unitaire
ORDER BY article_code, MIN(t.date_ticket);
*/