-- Migration 19: Caixa e fechamento
-- Source: backend/db/dump/19052026_nextbar.sql

-- -------------------------------------------------------- 19. Caixa e fechamento
--
-- Estrutura para tabela `sessoes_caixa`
--

CREATE TABLE `sessoes_caixa` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  event_id INT NULL,
  establishment_id INT NULL,
  sector_id INT NOT NULL,
  device_id INT NULL,
  responsible_user_id INT NOT NULL,
  opened_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  closed_at TIMESTAMP NULL,
  opening_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  closing_amount DECIMAL(10,2) NULL,
  observations TEXT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (tenant_id) REFERENCES tenants(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id),
  FOREIGN KEY (establishment_id) REFERENCES estabelecimentos(id),
  FOREIGN KEY (sector_id) REFERENCES setores(id),
  FOREIGN KEY (device_id) REFERENCES dispositivos(id),
  FOREIGN KEY (responsible_user_id) REFERENCES usuarios(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `totais_sessao_caixa`
--

CREATE TABLE `totais_sessao_caixa` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  cash_session_id INT NOT NULL,
  payment_method VARCHAR(100) NOT NULL,
  gross_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  canceled_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  refunded_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  net_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  sales_count INT(11) NOT NULL DEFAULT 0,
  cancellations_count INT(11) NOT NULL DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (cash_session_id) REFERENCES sessoes_caixa(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `fechamentos_evento`
--

CREATE TABLE `fechamentos_evento` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  tenant_id INT NOT NULL,
  event_id INT NOT NULL,
  gross_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  canceled_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  refunded_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  net_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  total_sales_count INT(11) NOT NULL DEFAULT 0,
  pending_sales_count INT(11) NOT NULL DEFAULT 0,
  closed_by INT NULL,
  closed_at TIMESTAMP NULL,
  report_url VARCHAR(500) NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (tenant_id) REFERENCES tenants(id),
  FOREIGN KEY (event_id) REFERENCES eventos(id),
  FOREIGN KEY (closed_by) REFERENCES usuarios(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `resumos_setor_fechamento_evento`
--

CREATE TABLE `resumos_setor_fechamento_evento` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  event_closing_id INT NOT NULL,
  sector_id INT NOT NULL,
  gross_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  canceled_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  refunded_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  net_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  sales_count INT(11) NOT NULL DEFAULT 0,
  pending_sales_count INT(11) NOT NULL DEFAULT 0,
  printed_tickets_count INT(11) NOT NULL DEFAULT 0,
  reprinted_tickets_count INT(11) NOT NULL DEFAULT 0,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (event_closing_id) REFERENCES fechamentos_evento(id),
  FOREIGN KEY (sector_id) REFERENCES setores(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------
--
-- Estrutura para tabela `resumos_produto_fechamento_evento`
--

CREATE TABLE `resumos_produto_fechamento_evento` (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  event_closing_id INT NOT NULL,
  product_tenant_id INT NOT NULL,
  product_concept_id INT NULL,
  product_variant_id INT NULL,
  sold_quantity INT(11) NOT NULL DEFAULT 0,
  gross_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  canceled_quantity INT(11) NOT NULL DEFAULT 0,
  canceled_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  status TINYINT(1) NOT NULL DEFAULT 1,
  trash TINYINT(1) NOT NULL DEFAULT 0,
  FOREIGN KEY (event_closing_id) REFERENCES fechamentos_evento(id),
  FOREIGN KEY (product_tenant_id) REFERENCES produtos_tenant(id),
  FOREIGN KEY (product_concept_id) REFERENCES conceitos_produtos(id),
  FOREIGN KEY (product_variant_id) REFERENCES variantes_produtos(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
