-- Phase 08.3
-- Link each user to a default cashbox for POS.

SET @db := DATABASE();

SET @has_cashbox_id := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA=@db AND TABLE_NAME='users' AND COLUMN_NAME='cashbox_id'
);
SET @sql := IF(@has_cashbox_id=0,
  'ALTER TABLE `users` ADD COLUMN `cashbox_id` BIGINT UNSIGNED NULL AFTER `is_active`',
  'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @has_idx := (
  SELECT COUNT(*) FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA=@db AND TABLE_NAME='users' AND INDEX_NAME='idx_users_cashbox'
);
SET @sql := IF(@has_idx=0,
  'ALTER TABLE `users` ADD INDEX `idx_users_cashbox` (`cashbox_id`)',
  'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
