SET @has_calc_offset_margin_mode := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'margin_mode'
);
SET @sql_calc_offset_margin_mode := IF(
  @has_calc_offset_margin_mode = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN margin_mode ENUM(''PERCENT'',''FIXED'') NOT NULL DEFAULT ''PERCENT'' AFTER modal_final_per_pcs',
  'SELECT 1'
);
PREPARE stmt_calc_offset_margin_mode FROM @sql_calc_offset_margin_mode;
EXECUTE stmt_calc_offset_margin_mode;
DEALLOCATE PREPARE stmt_calc_offset_margin_mode;

SET @has_calc_offset_margin_fixed := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'margin_fixed'
);
SET @sql_calc_offset_margin_fixed := IF(
  @has_calc_offset_margin_fixed = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN margin_fixed DECIMAL(15,2) NOT NULL DEFAULT 0.00 AFTER margin_pct',
  'SELECT 1'
);
PREPARE stmt_calc_offset_margin_fixed FROM @sql_calc_offset_margin_fixed;
EXECUTE stmt_calc_offset_margin_fixed;
DEALLOCATE PREPARE stmt_calc_offset_margin_fixed;
