-- =====================================================================
-- MINI 8 -- Lab Invoice Detail + Finalization + Invoice Lock
-- =====================================================================
-- Safe to run any number of times, on any environment, in any order
-- relative to the earlier upgrades: every statement below only ADDS a
-- column/index if it is genuinely missing (checked via
-- information_schema first, exactly like accounting_billing_upgrade.sql
-- already does). Never resets, drops, truncates, or deletes anything.
--
-- WHY THIS FILE EXISTS: Mini 8 does not introduce any new table or
-- column of its own -- `invoice.finalized_at` / `invoice.finalized_by`
-- were already added by DATABASE FILE/accounting_billing_upgrade.sql
-- (the Mini 7 / Accountant-Billing package). This file is included so
-- Mini 8 has its own migration entry, matching the project's existing
-- "one file per Mini" convention, and so a database that somehow does
-- NOT already have these two columns (e.g. accounting_billing_upgrade.sql
-- was skipped) still gets exactly what Lab Invoice Finalization needs.
--
-- On a database where accounting_billing_upgrade.sql has already run,
-- every statement below is a guaranteed no-op ('SELECT 1' branch).
-- =====================================================================

SET @db := DATABASE();

-- invoice.finalized_at / invoice.finalized_by -- same two columns
-- Financial_service::finalize() already writes for an Accountant Draft
-- invoice; Mini 8 extends that same function to also stamp these for a
-- Lab (POSTED/UNPAID/PARTIALLY_PAID/PAID) invoice, without ever
-- changing invoice.billing_status/status.
SET @q:=IF((SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@db AND table_name='invoice' AND column_name='finalized_at')=0,'ALTER TABLE invoice ADD COLUMN finalized_at DATETIME NULL','SELECT 1'); PREPARE s FROM @q; EXECUTE s; DEALLOCATE PREPARE s;
SET @q:=IF((SELECT COUNT(*) FROM information_schema.columns WHERE table_schema=@db AND table_name='invoice' AND column_name='finalized_by')=0,'ALTER TABLE invoice ADD COLUMN finalized_by INT NULL','SELECT 1'); PREPARE s FROM @q; EXECUTE s; DEALLOCATE PREPARE s;

-- Lookup index for the Lab Invoice Detail screen's "already finalized?"
-- check (Financial_service::finalize() / apply_opd_discount() /
-- save_invoice() guards) -- purely a read-performance aid, never
-- required for correctness. Added only if missing.
SET @q:=IF((SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema=@db AND table_name='invoice' AND index_name='invoice_finalized_at_idx')=0,'ALTER TABLE invoice ADD KEY invoice_finalized_at_idx (finalized_at)','SELECT 1'); PREPARE s FROM @q; EXECUTE s; DEALLOCATE PREPARE s;

-- README:
-- * No FK added, matching this schema's existing style (no FKs anywhere).
-- * billing_invoice_item, print_history, financial_audit_log,
--   patient_encounter, lab_orders.encounter_id -- all already exist from
--   earlier migrations (step5_complete_billing_upgrade.sql,
--   accounting_billing_upgrade.sql, mini3_lab_order_encounter_upgrade.sql /
--   step6_clinical_ipd_upgrade.sql) and are only READ by Mini 8's new
--   Lab Invoice Detail / Print screens -- nothing further to add for them.
-- * No data is reset, dropped, truncated, or deleted by this file.
