-- Copyright (C) 2026 Eduard Wisch
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <https://www.gnu.org/licenses/>.

--
-- Tabelle für Verwaltung von Rechnungs-, Angebots- und Auftragszeilen
--

CREATE TABLE IF NOT EXISTS llx_facture_lines_manager (
  rowid INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,

  -- Dokumenttyp und Referenzen
  document_type VARCHAR(20) DEFAULT 'invoice' NOT NULL,
  fk_facture INT(11) DEFAULT NULL,
  fk_propal INT(11) DEFAULT NULL,
  fk_commande INT(11) DEFAULT NULL,

  -- Zeilentyp: 'section', 'text', 'subtotal', 'product'
  line_type VARCHAR(20) NOT NULL,

  -- Referenzen auf Detailzeilen
  fk_facturedet INT(11) DEFAULT NULL,
  fk_propaldet INT(11) DEFAULT NULL,
  fk_commandedet INT(11) DEFAULT NULL,

  -- Section-Informationen
  parent_section INT(11) DEFAULT NULL,
  title VARCHAR(255) DEFAULT NULL,
  line_order INT(11) DEFAULT 0,
  show_subtotal TINYINT(1) DEFAULT 0,
  collapsed TINYINT(1) DEFAULT 0,
  in_facturedet TINYINT(1) DEFAULT 0,

  -- Timestamps
  date_creation DATETIME NOT NULL,
  tms TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  -- Indizes
  INDEX idx_fk_facture (fk_facture),
  INDEX idx_fk_propal (fk_propal),
  INDEX idx_fk_commande (fk_commande),
  INDEX idx_fk_facturedet (fk_facturedet),
  INDEX idx_fk_propaldet (fk_propaldet),
  INDEX idx_fk_commandedet (fk_commandedet),
  INDEX idx_document_type (document_type),
  INDEX idx_line_type (line_type),
  INDEX idx_parent_section (parent_section),
  INDEX idx_line_order (line_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
