Update: 2026-05-08 00:43:22

This commit is contained in:
Hamza-Ayed
2026-05-08 00:43:22 +03:00
parent 08e2a87c10
commit 522885d257
4 changed files with 79 additions and 34 deletions

View File

@@ -1,9 +1,8 @@
-- ════════════════════════════════════════════════════════════
-- مُصادَق — Comprehensive Phase 1 Migration
-- Tables for AI Extraction, JoFotara Integration, and Usage Logs
-- مُصادَق — Complete Phase 1 Migration (MySQL 8.0 Compatible)
-- ════════════════════════════════════════════════════════════
-- 1. Invoice Line Items (For AI extracted data)
-- 1. Invoice Line Items (AI extracted data)
CREATE TABLE IF NOT EXISTS invoice_lines (
id CHAR(36) PRIMARY KEY DEFAULT (UUID()),
invoice_id CHAR(36) NOT NULL,
@@ -29,7 +28,7 @@ CREATE TABLE IF NOT EXISTS jofotara_submissions (
jofotara_uuid VARCHAR(100) NULL,
xml_content LONGTEXT NULL,
status ENUM('accepted', 'rejected', 'pending') DEFAULT 'pending',
qr_code_raw TEXT NULL, -- Base64 QR from JoFotara
qr_code_raw TEXT NULL,
response_body JSON NULL,
submitted_at DATETIME NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
@@ -38,22 +37,7 @@ CREATE TABLE IF NOT EXISTS jofotara_submissions (
FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 3. Update Companies for JoFotara Credentials
ALTER TABLE companies
ADD COLUMN IF NOT EXISTS jofotara_client_id VARCHAR(255) NULL AFTER tax_identification_number,
ADD COLUMN IF NOT EXISTS jofotara_secret_key VARCHAR(255) NULL AFTER jofotara_client_id,
ADD COLUMN IF NOT EXISTS jofotara_status ENUM('active', 'inactive', 'pending') DEFAULT 'inactive' AFTER jofotara_secret_key;
-- 4. Update Invoices for AI and JoFotara metadata
ALTER TABLE invoices
ADD COLUMN IF NOT EXISTS invoice_category ENUM('simplified', 'standard') DEFAULT 'simplified' AFTER invoice_type,
ADD COLUMN IF NOT EXISTS ubl_type_code VARCHAR(10) DEFAULT '388' AFTER invoice_category,
ADD COLUMN IF NOT EXISTS payment_method_code VARCHAR(10) DEFAULT '013' AFTER ubl_type_code,
ADD COLUMN IF NOT EXISTS validation_warnings JSON NULL AFTER qr_code,
ADD COLUMN IF NOT EXISTS ai_confidence DECIMAL(5,2) DEFAULT 0 AFTER validation_warnings,
ADD COLUMN IF NOT EXISTS jofotara_uuid VARCHAR(100) NULL AFTER status;
-- 5. AI Usage Log (Cost & Token tracking)
-- 3. AI Usage Log
CREATE TABLE IF NOT EXISTS ai_usage_log (
id INT AUTO_INCREMENT PRIMARY KEY,
tenant_id CHAR(36) NOT NULL,
@@ -71,7 +55,7 @@ CREATE TABLE IF NOT EXISTS ai_usage_log (
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 6. Notifications Table
-- 4. Notifications
CREATE TABLE IF NOT EXISTS notifications (
id CHAR(36) PRIMARY KEY DEFAULT (UUID()),
tenant_id CHAR(36) NOT NULL,
@@ -85,3 +69,26 @@ CREATE TABLE IF NOT EXISTS notifications (
INDEX idx_user_read (user_id, is_read),
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ════════════════════════════════════════════════════════════
-- 5. Safe ALTER TABLE (MySQL 8 compatible — no IF NOT EXISTS)
-- Run each block separately. If column already exists,
-- MySQL will show "Duplicate column" error — just skip it.
-- ════════════════════════════════════════════════════════════
-- 5a. Companies: JoFotara credentials
-- Run these ONE BY ONE. Skip any that say "Duplicate column name"
ALTER TABLE companies ADD COLUMN jofotara_client_id VARCHAR(255) NULL;
ALTER TABLE companies ADD COLUMN jofotara_secret_key VARCHAR(255) NULL;
ALTER TABLE companies ADD COLUMN jofotara_status ENUM('active', 'inactive', 'pending') DEFAULT 'inactive';
-- 5b. Invoices: AI + JoFotara metadata
-- Run these ONE BY ONE. Skip any that say "Duplicate column name"
ALTER TABLE invoices ADD COLUMN invoice_category ENUM('simplified', 'standard') DEFAULT 'simplified';
ALTER TABLE invoices ADD COLUMN ubl_type_code VARCHAR(10) DEFAULT '388';
ALTER TABLE invoices ADD COLUMN payment_method_code VARCHAR(10) DEFAULT '013';
ALTER TABLE invoices ADD COLUMN validation_warnings JSON NULL;
ALTER TABLE invoices ADD COLUMN ai_confidence DECIMAL(5,2) DEFAULT 0;
ALTER TABLE invoices ADD COLUMN jofotara_uuid VARCHAR(100) NULL;

5
scripts/reset_queue.sql Normal file
View File

@@ -0,0 +1,5 @@
-- Reset failed queue items so the cron worker retries them
UPDATE invoice_processing_queue SET status = 'pending', error_message = NULL WHERE status = 'failed';
-- Verify current queue state
SELECT id, batch_id, status, error_message, created_at FROM invoice_processing_queue ORDER BY created_at;