requestAction('/documents/calculateInvoiceTotals/'.$document['Document']['id']);
$subtotal = isset($totals['subtotal']) ? $totals['subtotal'] : 0;
$gstAmount = isset($totals['gst']) ? $totals['gst'] : 0;
$total = isset($totals['total']) ? $totals['total'] : 0;
// Prepare line items
$lineItems = array();
foreach ($document['LineItem'] as $item) {
$lineItems[] = array(
'item_number' => $item['item_number'],
'quantity' => $item['quantity'],
'title' => $item['title'],
'description' => isset($item['description']) ? $item['description'] : '',
'unit_price' => isset($item['unit_price_string']) ? floatval($item['unit_price_string']) : 0,
'total_price' => isset($item['gross_price_string']) ? floatval($item['gross_price_string']) : 0
);
}
$invoicePayload = array(
'document_id' => $document['Document']['id'],
'invoice_title' => $document['Invoice']['title'],
'customer_name' => $document['Customer']['name'],
'contact_email' => isset($document['Contact']['email']) ? $document['Contact']['email'] : '',
'contact_name' => isset($document['Contact']['first_name']) ? $document['Contact']['first_name'] . ' ' . $document['Contact']['last_name'] : '',
'user_first_name' => isset($user['User']['first_name']) ? $user['User']['first_name'] : '',
'user_last_name' => isset($user['User']['last_name']) ? $user['User']['last_name'] : '',
'user_email' => isset($user['User']['email']) ? $user['User']['email'] : '',
'your_reference' => isset($document['Document']['your_reference']) ? $document['Document']['your_reference'] : '',
'ship_via' => isset($ship_via) ? $ship_via : '',
'fob' => isset($fob) ? $fob : '',
'issue_date' => $document['Invoice']['issue_date'],
'issue_date_string' => isset($issue_date_string) ? $issue_date_string : date('d F Y', strtotime($document['Invoice']['issue_date'])),
'currency_symbol' => isset($document['Currency']['symbol']) ? $document['Currency']['symbol'] : '$',
'currency_code' => isset($document['Currency']['code']) ? $document['Currency']['code'] : 'AUD',
'show_gst' => $document['Invoice']['gst'],
'bill_to' => $billTo,
'ship_to' => $shipTo,
'shipping_details' => $shippingDetails,
'customer_order_number' => $customerOrderNumber,
'job_title' => isset($job['Job']['title']) ? $job['Job']['title'] : '',
'payment_terms' => $paymentTerms,
'customer_abn' => $customerABN,
'subtotal' => $subtotal,
'gst_amount' => $gstAmount,
'total' => $total,
'line_items' => $lineItems,
'output_dir' => '../php/app/webroot/pdf'
);
// Call Go API
$ch = curl_init($goApiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($invoicePayload));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$curlError = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$result = json_decode($response, true);
if (isset($result['filename'])) {
echo "";
} else {
echo "Error: Invalid response from PDF generator";
}
} else {
echo "Error: PDF generation failed (HTTP {$httpCode})
";
echo "API URL: " . htmlspecialchars($goApiUrl) . "
";
if (!empty($curlError)) {
echo "Curl Error: " . htmlspecialchars($curlError) . "
";
}
if (!empty($response)) {
echo "
" . htmlspecialchars($response) . ""; } } ?>