-- Migration V16: Melhorias no Módulo de Repasses (Helper Cols)
-- Data: 2026-02-17
-- Objetivo: Adicionar colunas para permitir acresimos e detalhamento de repasse
-- Dependencia: Requer master_financeiro_repasses criada na V10

-- 1. Adicionar colunas na tabela master_financeiro_repasses
SET @dbname = DATABASE();
SET @tablename = "master_financeiro_repasses";

-- Coluna valor_acrescimo
SET @columnname = "valor_acrescimo";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (table_name = @tablename)
      AND (table_schema = @dbname)
      AND (column_name = @columnname)
  ) > 0,
  "SELECT 1",
  "ALTER TABLE master_financeiro_repasses ADD COLUMN valor_acrescimo DECIMAL(10,2) DEFAULT 0.00 AFTER valor_impostos"
));
PREPARE stmt FROM @preparedStatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Coluna observacao_acrescimo
SET @columnname = "observacao_acrescimo";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (table_name = @tablename)
      AND (table_schema = @dbname)
      AND (column_name = @columnname)
  ) > 0,
  "SELECT 1",
  "ALTER TABLE master_financeiro_repasses ADD COLUMN observacao_acrescimo TEXT AFTER valor_acrescimo"
));
PREPARE stmt FROM @preparedStatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Coluna valor_particular (informativo para facilitar relatorios)
SET @columnname = "valor_particular";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (table_name = @tablename)
      AND (table_schema = @dbname)
      AND (column_name = @columnname)
  ) > 0,
  "SELECT 1",
  "ALTER TABLE master_financeiro_repasses ADD COLUMN valor_particular DECIMAL(10,2) DEFAULT 0.00 AFTER valor_total_producao"
));
PREPARE stmt FROM @preparedStatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- Coluna valor_convenio (informativo para facilitar relatorios)
SET @columnname = "valor_convenio";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (table_name = @tablename)
      AND (table_schema = @dbname)
      AND (column_name = @columnname)
  ) > 0,
  "SELECT 1",
  "ALTER TABLE master_financeiro_repasses ADD COLUMN valor_convenio DECIMAL(10,2) DEFAULT 0.00 AFTER valor_particular"
));
PREPARE stmt FROM @preparedStatement;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 2. Atualizar permissoes para incluir novos recursos se necessario
-- (Opcional, se precisar de permissao especifica para adicionar acrescimo, pode user repasse_manage ja existente)
