Update: 2026-07-30 02:27:45

This commit is contained in:
Hamza-Ayed
2026-07-30 02:27:45 +03:00
parent 5f62455113
commit ca4a7c2e70
56 changed files with 3391 additions and 709 deletions
+13 -10
View File
@@ -32,7 +32,9 @@ if ($errors) {
$companyId = $data['company_id'];
$source = $data['source'] ?? 'mobile_scan';
$expectedImages = (int)($data['expected_images'] ?? 0);
// The mobile client sends 'total_images'; older/web callers send 'expected_images'.
// Accept both so the expected count is never silently zero.
$expectedImages = (int)($data['expected_images'] ?? $data['total_images'] ?? 0);
// 2. Permission check
$db = Database::getInstance();
@@ -52,24 +54,25 @@ if ($decoded['role'] !== 'super_admin' && $company['tenant_id'] !== $tenantId) {
// Use the actual tenant of the company
$targetTenantId = $company['tenant_id'];
// 3. Check quota (preview — don't increment yet)
// 3. Reserve quota for the WHOLE batch up front, not just one invoice.
// checkInvoiceQuota() responds and exits by itself when there is no room, so the
// old try/catch wrapper never actually caught anything.
if ($decoded['role'] !== 'super_admin') {
try {
QuotaMiddleware::checkInvoiceQuota($targetTenantId);
} catch (\Exception $e) {
json_error('تم استنفاد رصيد الفواتير لهذا الشهر. قم بترقية باقتك.', 429);
}
QuotaMiddleware::checkInvoiceQuota($targetTenantId, max(1, $expectedImages));
}
// 4. Generate batch ID
$batchId = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));
// 5. Create batch record
// 5. Create batch record.
// total_images starts at 0 and is incremented by upload-image for each file that
// actually lands. Seeding it with the client's expected count here would double
// count and break the (processed + failed) >= total completion check.
$stmt = $db->prepare("
INSERT INTO invoice_batches (id, tenant_id, company_id, uploaded_by, total_images, source, status)
VALUES (?, ?, ?, ?, ?, ?, 'uploading')
VALUES (?, ?, ?, ?, 0, ?, 'uploading')
");
$stmt->execute([$batchId, $targetTenantId, $companyId, $userId, $expectedImages, $source]);
$stmt->execute([$batchId, $targetTenantId, $companyId, $userId, $source]);
// 6. Create upload directory
$uploadDir = STORAGE_PATH . '/invoices/' . $targetTenantId . '/' . $companyId . '/batches/' . $batchId;