-- MSMEBiz v1.6. Apply AFTER migration_v1.5.sql. No destructive changes.
CREATE TABLE IF NOT EXISTS saas_billing_templates (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 title VARCHAR(100) NOT NULL, description VARCHAR(255) NOT NULL DEFAULT '',
 amount DECIMAL(12,2) NOT NULL DEFAULT 0.00,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS saas_billing_credits (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 invoice_id BIGINT UNSIGNED NOT NULL,
 org_id BIGINT UNSIGNED NOT NULL,
 credit_ref VARCHAR(64) UNIQUE NULL,
 amount DECIMAL(12,2) NOT NULL,
 reason VARCHAR(255) NOT NULL,
 entered_by BIGINT UNSIGNED NOT NULL,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 INDEX ix_credit_org_invoice(org_id,invoice_id),
 FOREIGN KEY(invoice_id) REFERENCES saas_billing_invoices(id),
 FOREIGN KEY(org_id) REFERENCES organizations(id),
 FOREIGN KEY(entered_by) REFERENCES users(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
