Update: 2026-05-04 14:40:41

This commit is contained in:
Hamza-Ayed
2026-05-04 14:40:41 +03:00
parent ebb70e657e
commit 863dabc069
7 changed files with 448 additions and 134 deletions

View File

@@ -108,41 +108,34 @@ if (move_uploaded_file($_FILES['invoice']['tmp_name'], $targetFile)) {
'date' => $extracted['invoice_date'] ?? null,
'type' => $extracted['invoice_type'] ?? 'cash',
'cat' => $extracted['invoice_category'] ?? 'simplified',
's_tin' => \App\Core\Encryption::encrypt($extracted['supplier_tin'] ?? ''),
's_name' => \App\Core\Encryption::encrypt($extracted['supplier_name'] ?? ''),
's_addr' => \App\Core\Encryption::encrypt($extracted['supplier_address'] ?? ''),
'b_tin' => \App\Core\Encryption::encrypt($extracted['buyer_tin'] ?? ''),
'b_name' => \App\Core\Encryption::encrypt($extracted['buyer_name'] ?? ''),
'b_nid' => \App\Core\Encryption::encrypt($extracted['buyer_national_id'] ?? ''),
's_tin' => \App\Core\Encryption::encrypt($extracted['supplier']['tin'] ?? ''),
's_name' => \App\Core\Encryption::encrypt($extracted['supplier']['name'] ?? ''),
's_addr' => \App\Core\Encryption::encrypt($extracted['supplier']['address'] ?? ''),
'b_tin' => \App\Core\Encryption::encrypt($extracted['buyer']['tin'] ?? ''),
'b_name' => \App\Core\Encryption::encrypt($extracted['buyer']['name'] ?? ''),
'b_nid' => \App\Core\Encryption::encrypt($extracted['buyer']['national_id'] ?? ''),
'sub' => $extracted['subtotal'] ?? 0,
'tax' => $extracted['tax_amount'] ?? 0,
'disc' => $extracted['discount_total'] ?? 0,
'total' => $extracted['grand_total'] ?? 0,
'cur' => $extracted['currency'] ?? 'JOD'
'cur' => $extracted['currency_code'] ?? 'JOD'
]);
// Save Line Items
if (!empty($extracted['items'])) {
if (!empty($extracted['lines'])) {
$lineStmt = $db->prepare("
INSERT INTO invoice_lines (invoice_id, line_number, description, quantity, unit_price, tax_rate, line_total)
VALUES (?, ?, ?, ?, ?, ?, ?)
");
$lineNo = 1;
foreach ($extracted['items'] as $item) {
// Calculate tax rate if not provided (fallback to 0.16 for Jordan)
$taxRate = 0.16;
if (!empty($item['unit_price']) && !empty($item['tax_amount'])) {
$taxRate = round($item['tax_amount'] / ($item['unit_price'] * ($item['quantity'] ?: 1)), 4);
}
foreach ($extracted['lines'] as $item) {
$lineStmt->execute([
$invoiceId,
$lineNo++,
$item['line_number'] ?? 1,
$item['description'] ?? 'N/A',
$item['quantity'] ?? 1,
$item['unit_price'] ?? 0,
$taxRate,
$item['total'] ?? 0
$item['tax_rate'] ?? 0.16,
$item['line_total'] ?? 0
]);
}
}