diff --git a/app/modules_app/invoices/upload.php b/app/modules_app/invoices/upload.php index f1764c7..7f557ec 100644 --- a/app/modules_app/invoices/upload.php +++ b/app/modules_app/invoices/upload.php @@ -110,19 +110,27 @@ if (move_uploaded_file($_FILES['invoice']['tmp_name'], $targetFile)) { $invoiceId = $db->lastInsertId(); - // Save Line Items (No encryption for lines for now, usually not sensitive but can be) + // Save Line Items if (!empty($extracted['items'])) { $lineStmt = $db->prepare(" - INSERT INTO invoice_lines (invoice_id, description, quantity, unit_price, tax_amount, total) - VALUES (?, ?, ?, ?, ?, ?) + 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); + } + $lineStmt->execute([ $invoiceId, + $lineNo++, $item['description'] ?? 'N/A', $item['quantity'] ?? 1, $item['unit_price'] ?? 0, - $item['tax_amount'] ?? 0, + $taxRate, $item['total'] ?? 0 ]); } diff --git a/scripts/schema.sql b/scripts/schema.sql index af0eb90..9f47e46 100644 --- a/scripts/schema.sql +++ b/scripts/schema.sql @@ -95,12 +95,12 @@ CREATE TABLE invoices ( invoice_type ENUM('cash','credit') DEFAULT 'cash', ubl_type_code CHAR(3) DEFAULT '388', payment_method_code CHAR(3) DEFAULT '013', - supplier_tin VARCHAR(20) NULL, - supplier_name VARCHAR(255) NULL, + supplier_tin TEXT NULL, + supplier_name TEXT NULL, supplier_address TEXT NULL, - buyer_tin VARCHAR(20) NULL, - buyer_national_id VARCHAR(20) NULL, - buyer_name VARCHAR(255) NULL, + buyer_tin TEXT NULL, + buyer_national_id TEXT NULL, + buyer_name TEXT NULL, subtotal DECIMAL(15,3) DEFAULT 0, discount_total DECIMAL(15,3) DEFAULT 0, tax_amount DECIMAL(15,3) DEFAULT 0,