-- DentalCare 0.5.9.8.12
-- Cashier Register compatibility repair for partially upgraded databases.
-- The in-app updater performs the same checks column by column.

ALTER TABLE `{{prefix}}queue_entries`
  ADD COLUMN IF NOT EXISTS `billing_at` DATETIME NULL;

ALTER TABLE `{{prefix}}clinical_encounters`
  ADD COLUMN IF NOT EXISTS `branch_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `queue_entry_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `finalized_at` DATETIME NULL;

ALTER TABLE `{{prefix}}procedures`
  ADD COLUMN IF NOT EXISTS `branch_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `service_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `encounter_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `treatment_plan_item_id` BIGINT UNSIGNED NULL;

ALTER TABLE `{{prefix}}treatment_plan_items`
  ADD COLUMN IF NOT EXISTS `estimated_fee` DECIMAL(12,2) NOT NULL DEFAULT 0.00;

ALTER TABLE `{{prefix}}invoices`
  ADD COLUMN IF NOT EXISTS `branch_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `encounter_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `queue_entry_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `paid_amount` DECIMAL(12,2) NOT NULL DEFAULT 0.00;

ALTER TABLE `{{prefix}}invoice_items`
  ADD COLUMN IF NOT EXISTS `procedure_id` BIGINT UNSIGNED NULL,
  ADD COLUMN IF NOT EXISTS `clinical_usage_item_id` BIGINT UNSIGNED NULL;

ALTER TABLE `{{prefix}}payments`
  ADD COLUMN IF NOT EXISTS `payer_name` VARCHAR(190) NULL,
  ADD COLUMN IF NOT EXISTS `payer_relationship` VARCHAR(40) NULL;
