84 lines
2.3 KiB
PHP
84 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* Finalize Batch Endpoint
|
|
* POST /v1/batches/finalize
|
|
*
|
|
* Marks a batch as ready for processing.
|
|
* Triggers background processing (or processes synchronously depending on setup).
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Core\Database;
|
|
use App\Middleware\AuthMiddleware;
|
|
use App\Core\Security;
|
|
use App\Services\InvoiceProcessor;
|
|
|
|
$decoded = AuthMiddleware::check();
|
|
$tenantId = $decoded['tenant_id'];
|
|
$userId = $decoded['user_id'];
|
|
|
|
$data = Security::sanitize(input());
|
|
$batchId = $data['batch_id'] ?? null;
|
|
|
|
if (!$batchId) {
|
|
json_error('معرّف الدفعة مطلوب', 422);
|
|
}
|
|
|
|
$db = Database::getInstance();
|
|
|
|
// 1. Verify batch
|
|
$stmt = $db->prepare("
|
|
SELECT id, tenant_id, status, total_images
|
|
FROM invoice_batches
|
|
WHERE id = ? AND uploaded_by = ?
|
|
");
|
|
$stmt->execute([$batchId, $userId]);
|
|
$batch = $stmt->fetch();
|
|
|
|
if (!$batch || ($decoded['role'] !== 'super_admin' && $batch['tenant_id'] !== $tenantId)) {
|
|
json_error('الدفعة غير موجودة', 404);
|
|
}
|
|
|
|
|
|
|
|
if ($batch['status'] !== 'uploading') {
|
|
json_error('تم إنهاء هذه الدفعة مسبقاً', 400);
|
|
}
|
|
|
|
if ($batch['total_images'] == 0) {
|
|
json_error('لا يمكن إنهاء دفعة فارغة', 400);
|
|
}
|
|
|
|
// 2. Mark as processing
|
|
$stmt = $db->prepare("
|
|
UPDATE invoice_batches
|
|
SET status = 'processing', updated_at = NOW()
|
|
WHERE id = ?
|
|
");
|
|
$stmt->execute([$batchId]);
|
|
|
|
// 3. If it's a single invoice, try processing it SYNCHRONOUSLY right now!
|
|
if ($batch['total_images'] == 1) {
|
|
// We need the queue ID for this batch
|
|
$queueStmt = $db->prepare("SELECT id FROM invoice_processing_queue WHERE batch_id = ? AND status = 'pending' LIMIT 1");
|
|
$queueStmt->execute([$batchId]);
|
|
$queueId = $queueStmt->fetchColumn();
|
|
|
|
if ($queueId) {
|
|
InvoiceProcessor::processQueueItem((int)$queueId);
|
|
}
|
|
} else {
|
|
// For multiple invoices, try triggering the worker in the background
|
|
$workerPath = ROOT_PATH . '/app/cron/process_batches.php';
|
|
$logPath = STORAGE_PATH . '/logs/cron.log';
|
|
// Mute exec since it might fail depending on server config
|
|
@exec("php " . escapeshellarg($workerPath) . " >> " . escapeshellarg($logPath) . " 2>&1 &");
|
|
}
|
|
|
|
json_success([
|
|
'batch_id' => $batchId,
|
|
'status' => 'processing',
|
|
'total_images' => $batch['total_images']
|
|
], 'تم إنهاء الدفعة بنجاح وإرسالها للمعالجة');
|