Update: 2026-05-04 17:59:11
This commit is contained in:
@@ -40,32 +40,39 @@ try {
|
||||
$invoice['items'] = $stmtLines->fetchAll();
|
||||
|
||||
// 3. Decrypt Company Keys for JoFotara
|
||||
$clientId = \App\Core\Encryption::decrypt($invoice['jofotara_client_id_encrypted']);
|
||||
$secretKey = \App\Core\Encryption::decrypt($invoice['jofotara_secret_key_encrypted']);
|
||||
$clientId = \App\Core\Encryption::decrypt($invoice['jofotara_client_id_encrypted'] ?? '');
|
||||
$secretKey = \App\Core\Encryption::decrypt($invoice['jofotara_secret_key_encrypted'] ?? '');
|
||||
|
||||
if (!$clientId || !$secretKey) {
|
||||
throw new \Exception("JoFotara credentials missing for company: " . $invoice['company_name']);
|
||||
$jofotara = new JoFotara();
|
||||
$apiResponse = ['success' => false];
|
||||
$xmlContent = null;
|
||||
|
||||
// 4. Try JoFotara Submission if credentials exist
|
||||
if ($clientId && $secretKey) {
|
||||
$companyData = [
|
||||
'name' => $invoice['company_name'],
|
||||
'tax_identification_number' => $invoice['company_tin'],
|
||||
'address' => $invoice['company_address']
|
||||
];
|
||||
|
||||
// Decrypt Buyer Info for XML
|
||||
$invoice['buyer_name'] = \App\Core\Encryption::decrypt($invoice['buyer_name'] ?? '') ?: ($invoice['buyer_name'] ?? '');
|
||||
$invoice['buyer_tin'] = \App\Core\Encryption::decrypt($invoice['buyer_tin'] ?? '') ?: ($invoice['buyer_tin'] ?? '');
|
||||
|
||||
$xmlContent = $jofotara->generateXML($invoice, $companyData);
|
||||
$apiResponse = $jofotara->submitInvoice($xmlContent, $clientId, $secretKey);
|
||||
}
|
||||
|
||||
// Decrypt Buyer Info
|
||||
$invoice['buyer_name'] = \App\Core\Encryption::decrypt($invoice['buyer_name']) ?: '';
|
||||
$invoice['buyer_tin'] = \App\Core\Encryption::decrypt($invoice['buyer_tin']) ?: '';
|
||||
// 5. Fallback: Generate Local QR if API failed or no credentials
|
||||
$qrCode = $apiResponse['qrCode'] ?? $jofotara->generateQRCode([
|
||||
'supplier_name' => $invoice['company_name'],
|
||||
'supplier_tin' => $invoice['company_tin'],
|
||||
'invoice_date' => $invoice['invoice_date'],
|
||||
'grand_total' => $invoice['grand_total'],
|
||||
'tax_amount' => $invoice['tax_amount']
|
||||
]);
|
||||
|
||||
// 4. Initialize JoFotara Service
|
||||
$jofotara = new JoFotara();
|
||||
|
||||
// 5. Generate UBL 2.1 XML
|
||||
$companyData = [
|
||||
'name' => $invoice['company_name'],
|
||||
'tax_identification_number' => $invoice['company_tin'],
|
||||
'address' => $invoice['company_address']
|
||||
];
|
||||
$xmlContent = $jofotara->generateXML($invoice, $companyData);
|
||||
|
||||
// 6. Submit to JoFotara API
|
||||
$apiResponse = $jofotara->submitInvoice($xmlContent, $clientId, $secretKey);
|
||||
|
||||
// 7. Record Submission (Audit Log)
|
||||
// 6. Record Submission (Audit Log)
|
||||
$submissionId = \App\Core\Database::generateUuid();
|
||||
$stmtSub = $db->prepare("
|
||||
INSERT INTO jofotara_submissions
|
||||
@@ -74,41 +81,38 @@ try {
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())
|
||||
");
|
||||
|
||||
$status = $apiResponse['success'] ? 'accepted' : 'rejected';
|
||||
$status = $apiResponse['success'] ? 'accepted' : 'error';
|
||||
$stmtSub->execute([
|
||||
$submissionId,
|
||||
$id,
|
||||
$invoice['company_id'],
|
||||
$invoice['tenant_id'],
|
||||
$xmlContent,
|
||||
hash('sha256', $xmlContent),
|
||||
$xmlContent ? hash('sha256', $xmlContent) : null,
|
||||
$apiResponse['uuid'] ?? null,
|
||||
$apiResponse['qrCode'] ?? null,
|
||||
$qrCode,
|
||||
$apiResponse['_http_code'] ?? '0',
|
||||
json_encode($apiResponse['raw'] ?? []),
|
||||
json_encode($apiResponse['raw'] ?? ['info' => 'Local approval / No credentials']),
|
||||
$status
|
||||
]);
|
||||
|
||||
if (!$apiResponse['success']) {
|
||||
throw new \Exception("JoFotara Rejection: " . ($apiResponse['error'] ?? 'Unknown Error'));
|
||||
}
|
||||
|
||||
// 8. Update Invoice
|
||||
// 7. Update Invoice
|
||||
$updateStmt = $db->prepare("
|
||||
UPDATE invoices SET status = 'approved', jofotara_uuid = ?, qr_code = ?, updated_at = NOW() WHERE id = ?
|
||||
");
|
||||
$updateStmt->execute([$apiResponse['uuid'], $apiResponse['qrCode'], $id]);
|
||||
$updateStmt->execute([$apiResponse['uuid'] ?? null, $qrCode, $id]);
|
||||
|
||||
$db->commit();
|
||||
|
||||
json_success([
|
||||
'message' => 'Approved and submitted to JoFotara.',
|
||||
'uuid' => $apiResponse['uuid'],
|
||||
'qr_code' => $apiResponse['qrCode']
|
||||
'message' => $apiResponse['success'] ? 'تم الاعتماد والإرسال إلى جوفوترة بنجاح' : 'تم الاعتماد محلياً (نظام جوفوترة غير متصل)',
|
||||
'uuid' => $apiResponse['uuid'] ?? null,
|
||||
'qr_code' => $qrCode,
|
||||
'is_api_success' => $apiResponse['success']
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$db->rollBack();
|
||||
if ($db->inTransaction()) $db->rollBack();
|
||||
error_log("JoFotara Approve Error: " . $e->getMessage());
|
||||
json_error('Failed to approve invoice: ' . $e->getMessage(), 500);
|
||||
json_error('خطأ غير متوقع: ' . $e->getMessage(), 500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user