Update: 2026-05-05 01:19:43

This commit is contained in:
Hamza-Ayed
2026-05-05 01:19:43 +03:00
parent ed525250c2
commit b2ed480d93
3 changed files with 88 additions and 2 deletions

View File

@@ -80,6 +80,24 @@ if (move_uploaded_file($_FILES['invoice']['tmp_name'], $targetFile)) {
json_success(['id' => $db->lastInsertId()], 'تم رفع الفاتورة ولكن فشل استخراج البيانات تلقائياً');
}
// 5.5 Duplicate Prevention Check
$supplierTin = $extracted['supplier']['tin'] ?? '';
$invoiceNum = $extracted['invoice_number'] ?? '';
$invoiceDate = $extracted['invoice_date'] ?? '';
// Only hash if we have the critical data to avoid false duplicate collisions
$invoiceHash = null;
if ($supplierTin && $invoiceNum && $invoiceDate) {
$rawHashString = $companyId . '_' . $supplierTin . '_' . $invoiceNum . '_' . $invoiceDate;
$invoiceHash = hash('sha256', strtolower($rawHashString));
$checkStmt = $db->prepare("SELECT id FROM invoices WHERE company_id = ? AND invoice_hash = ? AND deleted_at IS NULL");
$checkStmt->execute([$companyId, $invoiceHash]);
if ($checkStmt->fetch()) {
json_error('هذه الفاتورة تم رفعها مسبقاً لهذه الشركة (رقم الفاتورة مكرر لنفس المورد والتاريخ).', 409);
}
}
// 6. Save Extracted Data with Encryption
try {
$db->beginTransaction();
@@ -97,11 +115,14 @@ if (move_uploaded_file($_FILES['invoice']['tmp_name'], $targetFile)) {
supplier_tin, supplier_name, supplier_address,
buyer_tin, buyer_name, buyer_national_id,
subtotal, tax_amount, discount_total, grand_total, currency_code,
invoice_hash, validation_warnings,
created_at
) VALUES (
:id, :tenant_id, :company_id, :uploaded_by, :path, 'extracted',
:num, :date, :type, :cat, :s_tin, :s_name, :s_addr, :b_tin, :b_name, :b_nid,
:sub, :tax, :disc, :total, :cur, NOW()
:sub, :tax, :disc, :total, :cur,
:hash, :warnings,
NOW()
)
");
@@ -125,7 +146,9 @@ if (move_uploaded_file($_FILES['invoice']['tmp_name'], $targetFile)) {
'tax' => $extracted['tax_amount'] ?? 0,
'disc' => $extracted['discount_total'] ?? 0,
'total' => $extracted['grand_total'] ?? 0,
'cur' => $extracted['currency_code'] ?? 'JOD'
'cur' => $extracted['currency_code'] ?? 'JOD',
'hash' => $invoiceHash,
'warnings' => isset($extracted['validation_warnings']) && !empty($extracted['validation_warnings']) ? json_encode($extracted['validation_warnings']) : null
]);
// Save Line Items