-- 1/3 : Création de la table temporaire
DROP TABLE IF EXISTS w_dbsetup
;

CREATE TEMP TABLE w_dbsetup AS
    SELECT oid,
    	name,
    	texte,
    	select_cmd,
    	code  
    FROM eco.t_listes_tables
    ORDER BY code LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    name,
    texte,
    select_cmd,
    code
)
VALUES
(1, 't_articles', 'Articles', 'SELECT t_articles.oid, t_articles.code, t_articles.texte, t_types_articles.texte::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_articles
JOIN eco.t_types_articles ON type_id = t_types_articles.oid
JOIN eco.p_oids ON (t_articles.oid = p_oids.oid AND code_table = ''articles'')
WHERE t_articles.oid > 0 
ORDER BY t_articles.code', 'ART'),
(1, 't_categories_articles', 'Catégories articles', 'SELECT t_categories_articles.oid, t_categories_articles.code, t_categories_articles.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_categories_articles LEFT JOIN eco.p_oids ON (t_categories_articles.oid = p_oids.oid AND code_table = ''categories_articles'')
WHERE t_categories_articles.oid > 0 
ORDER BY t_categories_articles.code', 'CAA'),
(1, 't_compte', 'Comptes', 'SELECT t_compte.oid, t_compte.code, t_compte.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_compte LEFT JOIN eco.p_oids ON (t_compte.oid = p_oids.oid AND code_table = ''compte'')
WHERE t_compte.oid > 0 
ORDER BY t_compte.code', 'CPT'),
(1, 't_centres_responsabilite', 'Centres responsabilité', 'SELECT t_centres_responsabilites.oid, t_centres_responsabilites.code, t_centres_responsabilites.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_centres_responsabilites LEFT JOIN eco.p_oids ON (t_centres_responsabilites.oid = p_oids.oid AND code_table = ''centres_responsabilites'')
WHERE t_centres_responsabilites.oid > 0 
ORDER BY t_centres_responsabilites.code', 'CRE'),
(1, 't_familles_articles', 'Familles d''articles', 'SELECT t_familles_articles.oid, t_familles_articles.code, t_familles_articles.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_familles_articles LEFT JOIN eco.p_oids ON (t_familles_articles.oid = p_oids.oid AND code_table = ''familles_articles'')
WHERE t_familles_articles.oid > 0 
ORDER BY t_familles_articles.code', 'FAA'),
(1, 't_fournisseurs', 'Fournisseurs', 'SELECT t_fournisseurs.oid, t_fournisseurs.code, t_fournisseurs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_fournisseurs LEFT JOIN eco.p_oids ON (t_fournisseurs.oid = p_oids.oid AND code_table = ''fournisseurs'')
WHERE t_fournisseurs.oid > 0 
ORDER BY t_fournisseurs.code', 'FOU'),
(1, 't_gestionnaires', 'Gestionnaires', 'SELECT t_gestionnaires.oid, t_gestionnaires.code, t_gestionnaires.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_gestionnaires LEFT JOIN eco.p_oids ON (t_gestionnaires.oid = p_oids.oid AND code_table = ''gestionnaires'')
WHERE t_gestionnaires.oid > 0 
ORDER BY t_gestionnaires.code', 'GES'),
(1, 't_lieux', 'Lieux', 'SELECT t_lieux.oid, t_lieux.code, t_lieux.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_lieux LEFT JOIN eco.p_oids ON (t_lieux.oid = p_oids.oid AND code_table = ''lieux'')
WHERE t_lieux.oid > 0 
ORDER BY t_lieux.code', 'LIE'),
(1, 't_lpp_c', 'LPP', 'SELECT t_lpp_c.oid, t_lpp_c.code, t_lpp_c.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_lpp_c LEFT JOIN eco.p_oids ON (t_lpp_c.oid = p_oids.oid AND code_table = ''lpp'')
WHERE t_lpp_c.oid > 0 
ORDER BY t_lpp_c.code', 'LPP'),
(1, 't_sous_categories_articles', 'Sous catégories articles', 'SELECT t_sous_categories_articles.oid, t_sous_categories_articles.code, t_sous_categories_articles.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_sous_categories_articles LEFT JOIN eco.p_oids ON (t_sous_categories_articles.oid = p_oids.oid AND code_table = ''sous_categories_articles'')
WHERE t_sous_categories_articles.oid > 0 
ORDER BY t_sous_categories_articles.code', 'SCA'),
(1, 't_sous_familles_articles', 'Sous familles articles', 'SELECT t_sous_familles_articles.oid, t_sous_familles_articles.code, t_sous_familles_articles.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_sous_familles_articles LEFT JOIN eco.p_oids ON (t_sous_familles_articles.oid = p_oids.oid AND code_table = ''sous_familles_articles'')
WHERE t_sous_familles_articles.oid > 0 
ORDER BY t_sous_familles_articles.code', 'SFA'),
(1, 't_type_article_cti', 'Types d''articles CTI', 'SELECT t_type_article_cti.oid, t_type_article_cti.code, t_type_article_cti.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_type_article_cti LEFT JOIN eco.p_oids ON (t_type_article_cti.oid = p_oids.oid AND code_table = ''type_article_cti'')
ORDER BY t_type_article_cti.code', 'TAC'),
(1, 't_types_articles', 'Types d''articles', 'SELECT t_types_articles.oid, t_types_articles.code, t_types_articles.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_types_articles LEFT JOIN eco.p_oids ON (t_types_articles.oid = p_oids.oid AND code_table = ''types_articles'')
WHERE t_types_articles.oid > 0 
ORDER BY t_types_articles.code', 'TYA'),
(1, 't_types_fournisseurs', 'Types de fournisseurs', 'SELECT t_types_fournisseurs.oid, t_types_fournisseurs.code, t_types_fournisseurs.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_types_fournisseurs LEFT JOIN eco.p_oids ON (t_types_fournisseurs.oid = p_oids.oid AND code_table = ''types_fournisseurs'')
WHERE t_types_fournisseurs.oid > 0 
ORDER BY t_types_fournisseurs.code', 'TYF'),
(1, 't_types_mouvements', 'Types de mouvements', 'SELECT t_types_mouvements.oid, t_types_mouvements.code, t_types_mouvements.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_types_mouvements LEFT JOIN eco.p_oids ON (t_types_mouvements.oid = p_oids.oid AND code_table = ''types_mouvements'')
WHERE t_types_mouvements.oid > 0 
ORDER BY t_types_mouvements.code', 'TYM'),
(1, 't_ucd_c', 'UCD', 'SELECT t_ucd_c.oid, t_ucd_c.code, t_ucd_c.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_ucd_c LEFT JOIN eco.p_oids ON (t_ucd_c.oid = p_oids.oid AND code_table = ''ucd'')
WHERE t_ucd_c.oid > 0 
ORDER BY t_ucd_c.code', 'UCD'),
(1, 't_unites_fonctionnelles', 'Unités fonctionnelles', 'SELECT t_unites_fonctionnelles.oid, t_unites_fonctionnelles.code, t_unites_fonctionnelles.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_unites_fonctionnelles LEFT JOIN eco.p_oids ON (t_unites_fonctionnelles.oid = p_oids.oid AND code_table = ''unites_fonctionnelles'')
WHERE t_unites_fonctionnelles.oid > 0 
ORDER BY t_unites_fonctionnelles.code', 'UFO'),
(1, 't_unites', 'Unités', 'SELECT t_unites.oid, t_unites.code, t_unites.texte, ''''::text as complement, CASE WHEN p_oids.oid IS NOT NULL THEN ''O'' ELSE ''N'' END as utilise 
FROM eco.t_unites LEFT JOIN eco.p_oids ON (t_unites.oid = p_oids.oid AND code_table = ''unites_stockage_articles'')
WHERE t_unites.oid > 0 
ORDER BY t_unites.code', 'UNI')
;

-- 3/3 : Màj de la table iCTI
UPDATE eco.t_listes_tables SET 
				name = w_dbsetup.name,
				texte = w_dbsetup.texte, 
				select_cmd = w_dbsetup.select_cmd
			FROM w_dbsetup
			WHERE t_listes_tables.code = w_dbsetup.code;

			INSERT INTO eco.t_listes_tables(oid, code, name, texte, select_cmd)
				SELECT oid, code, name, texte, select_cmd FROM w_dbsetup WHERE oid = 0 AND oid NOT IN (SELECT oid FROM eco.t_listes_tables);
			
			INSERT INTO eco.t_listes_tables(code, name, texte, select_cmd)
				SELECT code, name, texte, select_cmd FROM w_dbsetup WHERE oid <> 0 AND code NOT IN (SELECT code FROM eco.t_listes_tables);