Update: 2026-05-07 01:14:37
This commit is contained in:
@@ -11,6 +11,9 @@ declare(strict_types=1);
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Middleware\AuthMiddleware;
|
||||
use App\Core\AI;
|
||||
use App\Core\Encryption;
|
||||
use App\Middleware\QuotaMiddleware;
|
||||
|
||||
$decoded = AuthMiddleware::check();
|
||||
$tenantId = $decoded['tenant_id'];
|
||||
@@ -71,23 +74,77 @@ if (!move_uploaded_file($_FILES['image']['tmp_name'], $targetPath)) {
|
||||
json_error('فشل في حفظ الصورة', 500);
|
||||
}
|
||||
|
||||
// 6. Add to processing queue
|
||||
$stmt = $db->prepare("
|
||||
INSERT INTO invoice_processing_queue (batch_id, tenant_id, company_id, image_path, image_order, status)
|
||||
VALUES (?, ?, ?, ?, ?, 'pending')
|
||||
");
|
||||
$stmt->execute([$batchId, $tenantId, $companyId, $targetPath, $imageOrder]);
|
||||
// 6. Run AI Extraction
|
||||
$fileContent = file_get_contents($targetPath);
|
||||
$base64Data = base64_encode($fileContent);
|
||||
$extracted = AI::extractInvoiceData($base64Data, $mimeType);
|
||||
|
||||
if (!$extracted) {
|
||||
// Save as raw upload if AI fails
|
||||
$invoiceId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
|
||||
$stmt = $db->prepare("INSERT INTO invoices (id, tenant_id, company_id, uploaded_by, original_file_path, status, created_at) VALUES (?, ?, ?, ?, ?, 'uploaded', NOW())");
|
||||
$stmt->execute([$invoiceId, $tenantId, $companyId, $userId, $targetPath]);
|
||||
} else {
|
||||
// Save Extracted Data
|
||||
$db->beginTransaction();
|
||||
try {
|
||||
$supplierTin = $extracted['supplier']['tin'] ?? '';
|
||||
$invoiceNum = $extracted['invoice_number'] ?? '';
|
||||
$invoiceDate = $extracted['invoice_date'] ?? '';
|
||||
$validDate = (!empty($invoiceDate) && strtotime($invoiceDate)) ? $invoiceDate : null;
|
||||
|
||||
$invoiceId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
|
||||
|
||||
$stmt = $db->prepare("
|
||||
INSERT INTO invoices (
|
||||
id, tenant_id, company_id, uploaded_by, original_file_path, status,
|
||||
invoice_number, invoice_date, invoice_type, invoice_category,
|
||||
supplier_tin, supplier_name, supplier_address,
|
||||
buyer_tin, buyer_name, buyer_national_id,
|
||||
subtotal, tax_amount, discount_total, grand_total, currency_code,
|
||||
created_at
|
||||
) VALUES (
|
||||
?, ?, ?, ?, ?, 'extracted',
|
||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()
|
||||
)
|
||||
");
|
||||
|
||||
$stmt->execute([
|
||||
$invoiceId, $tenantId, $companyId, $userId, $targetPath,
|
||||
$invoiceNum, $validDate, $extracted['invoice_type'] ?? 'cash', $extracted['invoice_category'] ?? 'simplified',
|
||||
Encryption::encrypt($supplierTin), Encryption::encrypt($extracted['supplier']['name'] ?? ''), Encryption::encrypt($extracted['supplier']['address'] ?? ''),
|
||||
Encryption::encrypt($extracted['buyer']['tin'] ?? ''), Encryption::encrypt($extracted['buyer']['name'] ?? ''), Encryption::encrypt($extracted['buyer']['national_id'] ?? ''),
|
||||
$extracted['subtotal'] ?? 0, $extracted['tax_amount'] ?? 0, $extracted['discount_total'] ?? 0, $extracted['grand_total'] ?? 0, $extracted['currency_code'] ?? 'JOD'
|
||||
]);
|
||||
|
||||
if (!empty($extracted['lines']) && is_array($extracted['lines'])) {
|
||||
$lineStmt = $db->prepare("INSERT INTO invoice_lines (id, invoice_id, line_number, description, quantity, unit_price, tax_rate, line_total) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
foreach ($extracted['lines'] as $index => $item) {
|
||||
$lineId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
|
||||
$lineStmt->execute([
|
||||
$lineId, $invoiceId, $item['line_number'] ?? ($index + 1), $item['description'] ?? 'بدون وصف',
|
||||
$item['quantity'] ?? 1, $item['unit_price'] ?? 0, $item['tax_rate'] ?? 0.16, $item['line_total'] ?? 0
|
||||
]);
|
||||
}
|
||||
}
|
||||
$db->commit();
|
||||
QuotaMiddleware::incrementInvoiceUsage($tenantId);
|
||||
} catch (Exception $e) {
|
||||
$db->rollBack();
|
||||
error_log("Batch Upload Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 7. Update batch image count
|
||||
$stmt = $db->prepare("
|
||||
UPDATE invoice_batches
|
||||
SET total_images = total_images + 1, updated_at = NOW()
|
||||
SET total_images = total_images + 1, processed_images = processed_images + 1, updated_at = NOW()
|
||||
WHERE id = ?
|
||||
");
|
||||
$stmt->execute([$batchId]);
|
||||
|
||||
// Count uploaded so far
|
||||
$stmt = $db->prepare("SELECT COUNT(*) FROM invoice_processing_queue WHERE batch_id = ?");
|
||||
// Count processed so far in batch
|
||||
$stmt = $db->prepare("SELECT processed_images FROM invoice_batches WHERE id = ?");
|
||||
$stmt->execute([$batchId]);
|
||||
$uploadedCount = (int)$stmt->fetchColumn();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user