-- Copyright (C) 2026 Eduard Wisch
--
-- Haupttabelle für Verwaltung von Rechnungs-, Angebots- und Auftragszeilen
-- Diese Datei wird beim Modul-Upgrade ausgeführt
--

-- Prüfen ob Spalten existieren und hinzufügen falls nicht
SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND column_name = 'document_type');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD COLUMN document_type VARCHAR(20) DEFAULT ''invoice'' NOT NULL AFTER fk_facture',
    'SELECT ''Column document_type already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- fk_propal hinzufügen
SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND column_name = 'fk_propal');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD COLUMN fk_propal INT(11) DEFAULT NULL AFTER document_type',
    'SELECT ''Column fk_propal already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- fk_commande hinzufügen
SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND column_name = 'fk_commande');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD COLUMN fk_commande INT(11) DEFAULT NULL AFTER fk_propal',
    'SELECT ''Column fk_commande already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- fk_propaldet hinzufügen
SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND column_name = 'fk_propaldet');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD COLUMN fk_propaldet INT(11) DEFAULT NULL AFTER fk_facturedet',
    'SELECT ''Column fk_propaldet already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- fk_commandedet hinzufügen
SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND column_name = 'fk_commandedet');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD COLUMN fk_commandedet INT(11) DEFAULT NULL AFTER fk_propaldet',
    'SELECT ''Column fk_commandedet already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Bestehende Daten aktualisieren
UPDATE llx_facture_lines_manager
SET document_type = 'invoice'
WHERE fk_facture IS NOT NULL AND (document_type IS NULL OR document_type = '');

-- Indizes hinzufügen falls sie nicht existieren
SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND index_name = 'idx_fk_propal');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD INDEX idx_fk_propal (fk_propal)',
    'SELECT ''Index idx_fk_propal already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND index_name = 'idx_fk_commande');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD INDEX idx_fk_commande (fk_commande)',
    'SELECT ''Index idx_fk_commande already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND index_name = 'idx_fk_propaldet');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD INDEX idx_fk_propaldet (fk_propaldet)',
    'SELECT ''Index idx_fk_propaldet already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND index_name = 'idx_fk_commandedet');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD INDEX idx_fk_commandedet (fk_commandedet)',
    'SELECT ''Index idx_fk_commandedet already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @exist := (SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
    WHERE table_schema = DATABASE()
    AND table_name = 'llx_facture_lines_manager'
    AND index_name = 'idx_document_type');

SET @sqlstmt := IF(@exist = 0,
    'ALTER TABLE llx_facture_lines_manager ADD INDEX idx_document_type (document_type)',
    'SELECT ''Index idx_document_type already exists'' as msg');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
