SET @has_qty_need_input := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'qty_need_input'
);
SET @sql_qty_need_input := IF(
  @has_qty_need_input = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN qty_need_input INT UNSIGNED NOT NULL DEFAULT 0 AFTER qty_product',
  'SELECT 1'
);
PREPARE stmt_qty_need_input FROM @sql_qty_need_input;
EXECUTE stmt_qty_need_input;
DEALLOCATE PREPARE stmt_qty_need_input;

SET @has_qty_need_total := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'qty_need_total'
);
SET @sql_qty_need_total := IF(
  @has_qty_need_total = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN qty_need_total INT UNSIGNED NOT NULL DEFAULT 0 AFTER qty_need_input',
  'SELECT 1'
);
PREPARE stmt_qty_need_total FROM @sql_qty_need_total;
EXECUTE stmt_qty_need_total;
DEALLOCATE PREPARE stmt_qty_need_total;

SET @has_total_plates := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'total_plates'
);
SET @sql_total_plates := IF(
  @has_total_plates = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN total_plates INT UNSIGNED NOT NULL DEFAULT 0 AFTER total_plano',
  'SELECT 1'
);
PREPARE stmt_total_plates FROM @sql_total_plates;
EXECUTE stmt_total_plates;
DEALLOCATE PREPARE stmt_total_plates;

SET @has_min_charge_qty := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'min_charge_qty'
);
SET @sql_min_charge_qty := IF(
  @has_min_charge_qty = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN min_charge_qty INT UNSIGNED NOT NULL DEFAULT 0 AFTER total_plates',
  'SELECT 1'
);
PREPARE stmt_min_charge_qty FROM @sql_min_charge_qty;
EXECUTE stmt_min_charge_qty;
DEALLOCATE PREPARE stmt_min_charge_qty;

SET @has_excess_qty := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'excess_qty'
);
SET @sql_excess_qty := IF(
  @has_excess_qty = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN excess_qty INT UNSIGNED NOT NULL DEFAULT 0 AFTER min_charge_qty',
  'SELECT 1'
);
PREPARE stmt_excess_qty FROM @sql_excess_qty;
EXECUTE stmt_excess_qty;
DEALLOCATE PREPARE stmt_excess_qty;

SET @has_oplah_qty := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'oplah_qty'
);
SET @sql_oplah_qty := IF(
  @has_oplah_qty = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN oplah_qty INT UNSIGNED NOT NULL DEFAULT 0 AFTER excess_qty',
  'SELECT 1'
);
PREPARE stmt_oplah_qty FROM @sql_oplah_qty;
EXECUTE stmt_oplah_qty;
DEALLOCATE PREPARE stmt_oplah_qty;

SET @has_oplah_basis := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'oplah_basis'
);
SET @sql_oplah_basis := IF(
  @has_oplah_basis = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN oplah_basis VARCHAR(60) NOT NULL DEFAULT '''' AFTER oplah_qty',
  'SELECT 1'
);
PREPARE stmt_oplah_basis FROM @sql_oplah_basis;
EXECUTE stmt_oplah_basis;
DEALLOCATE PREPARE stmt_oplah_basis;

SET @has_oplah_price := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'oplah_price'
);
SET @sql_oplah_price := IF(
  @has_oplah_price = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN oplah_price DECIMAL(15,4) NOT NULL DEFAULT 0.0000 AFTER oplah_basis',
  'SELECT 1'
);
PREPARE stmt_oplah_price FROM @sql_oplah_price;
EXECUTE stmt_oplah_price;
DEALLOCATE PREPARE stmt_oplah_price;

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

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

SET @has_print_charge_units := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'print_charge_units'
);
SET @sql_print_charge_units := IF(
  @has_print_charge_units = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN print_charge_units INT UNSIGNED NOT NULL DEFAULT 0 AFTER print_price',
  'SELECT 1'
);
PREPARE stmt_print_charge_units FROM @sql_print_charge_units;
EXECUTE stmt_print_charge_units;
DEALLOCATE PREPARE stmt_print_charge_units;

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

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

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

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

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

SET @has_material_name_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'material_name_snapshot'
);
SET @sql_material_name_snapshot := IF(
  @has_material_name_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN material_name_snapshot VARCHAR(160) NULL AFTER lamination_id',
  'SELECT 1'
);
PREPARE stmt_material_name_snapshot FROM @sql_material_name_snapshot;
EXECUTE stmt_material_name_snapshot;
DEALLOCATE PREPARE stmt_material_name_snapshot;

SET @has_material_gsm_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'material_gsm_snapshot'
);
SET @sql_material_gsm_snapshot := IF(
  @has_material_gsm_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN material_gsm_snapshot INT UNSIGNED NOT NULL DEFAULT 0 AFTER material_name_snapshot',
  'SELECT 1'
);
PREPARE stmt_material_gsm_snapshot FROM @sql_material_gsm_snapshot;
EXECUTE stmt_material_gsm_snapshot;
DEALLOCATE PREPARE stmt_material_gsm_snapshot;

SET @has_material_plano_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'material_plano_snapshot'
);
SET @sql_material_plano_snapshot := IF(
  @has_material_plano_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN material_plano_snapshot VARCHAR(80) NULL AFTER material_gsm_snapshot',
  'SELECT 1'
);
PREPARE stmt_material_plano_snapshot FROM @sql_material_plano_snapshot;
EXECUTE stmt_material_plano_snapshot;
DEALLOCATE PREPARE stmt_material_plano_snapshot;

SET @has_material_plano_p_cm_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'material_plano_p_cm_snapshot'
);
SET @sql_material_plano_p_cm_snapshot := IF(
  @has_material_plano_p_cm_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN material_plano_p_cm_snapshot DECIMAL(10,2) NOT NULL DEFAULT 0.00 AFTER material_plano_snapshot',
  'SELECT 1'
);
PREPARE stmt_material_plano_p_cm_snapshot FROM @sql_material_plano_p_cm_snapshot;
EXECUTE stmt_material_plano_p_cm_snapshot;
DEALLOCATE PREPARE stmt_material_plano_p_cm_snapshot;

SET @has_material_plano_l_cm_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'material_plano_l_cm_snapshot'
);
SET @sql_material_plano_l_cm_snapshot := IF(
  @has_material_plano_l_cm_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN material_plano_l_cm_snapshot DECIMAL(10,2) NOT NULL DEFAULT 0.00 AFTER material_plano_p_cm_snapshot',
  'SELECT 1'
);
PREPARE stmt_material_plano_l_cm_snapshot FROM @sql_material_plano_l_cm_snapshot;
EXECUTE stmt_material_plano_l_cm_snapshot;
DEALLOCATE PREPARE stmt_material_plano_l_cm_snapshot;

SET @has_machine_name_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'machine_name_snapshot'
);
SET @sql_machine_name_snapshot := IF(
  @has_machine_name_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN machine_name_snapshot VARCHAR(160) NULL AFTER material_plano_l_cm_snapshot',
  'SELECT 1'
);
PREPARE stmt_machine_name_snapshot FROM @sql_machine_name_snapshot;
EXECUTE stmt_machine_name_snapshot;
DEALLOCATE PREPARE stmt_machine_name_snapshot;

SET @has_machine_code_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'machine_code_snapshot'
);
SET @sql_machine_code_snapshot := IF(
  @has_machine_code_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN machine_code_snapshot VARCHAR(80) NULL AFTER machine_name_snapshot',
  'SELECT 1'
);
PREPARE stmt_machine_code_snapshot FROM @sql_machine_code_snapshot;
EXECUTE stmt_machine_code_snapshot;
DEALLOCATE PREPARE stmt_machine_code_snapshot;

SET @has_tariff_name_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'tariff_name_snapshot'
);
SET @sql_tariff_name_snapshot := IF(
  @has_tariff_name_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN tariff_name_snapshot VARCHAR(160) NULL AFTER machine_code_snapshot',
  'SELECT 1'
);
PREPARE stmt_tariff_name_snapshot FROM @sql_tariff_name_snapshot;
EXECUTE stmt_tariff_name_snapshot;
DEALLOCATE PREPARE stmt_tariff_name_snapshot;

SET @has_lamination_name_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'lamination_name_snapshot'
);
SET @sql_lamination_name_snapshot := IF(
  @has_lamination_name_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN lamination_name_snapshot VARCHAR(160) NULL AFTER tariff_name_snapshot',
  'SELECT 1'
);
PREPARE stmt_lamination_name_snapshot FROM @sql_lamination_name_snapshot;
EXECUTE stmt_lamination_name_snapshot;
DEALLOCATE PREPARE stmt_lamination_name_snapshot;

SET @has_model_name_snapshot := (
  SELECT COUNT(*)
  FROM information_schema.columns
  WHERE table_schema = DATABASE()
    AND table_name = 'calc_offset_records'
    AND column_name = 'model_name_snapshot'
);
SET @sql_model_name_snapshot := IF(
  @has_model_name_snapshot = 0,
  'ALTER TABLE calc_offset_records ADD COLUMN model_name_snapshot VARCHAR(120) NULL AFTER lamination_name_snapshot',
  'SELECT 1'
);
PREPARE stmt_model_name_snapshot FROM @sql_model_name_snapshot;
EXECUTE stmt_model_name_snapshot;
DEALLOCATE PREPARE stmt_model_name_snapshot;
