cmc-sales/php/app/views/documents/pdf_invoice.ctp

100 lines
4.4 KiB
Plaintext
Raw Normal View History

2011-08-15 17:36:48 -07:00
<?php
2026-01-12 03:00:48 -08:00
// Generate the Invoice PDF by calling the Go service instead of TCPDF.
$goBaseUrl = AppController::getGoBaseUrlOrFail();
2026-01-16 04:26:56 -08:00
$goEndpoint = $goBaseUrl . '/go/api/pdf/generate-invoice-html';
2026-01-12 03:00:48 -08:00
$outputDir = Configure::read('pdf_directory');
$lineItems = array();
foreach ($document['LineItem'] as $li) {
$lineItems[] = array(
'item_number' => $li['item_number'],
'quantity' => $li['quantity'],
'title' => $li['title'],
'description' => isset($li['description']) ? $li['description'] : '',
2026-01-16 04:26:56 -08:00
'is_html' => true, // Description is always HTML
2026-01-12 03:00:48 -08:00
'unit_price' => floatval($li['gross_unit_price']),
'total_price' => floatval($li['gross_price']),
'net_unit_price' => floatval($li['net_unit_price']),
'net_price' => floatval($li['net_price']),
'discount_percent' => floatval($li['discount_percent']),
'discount_amount_unit' => floatval($li['discount_amount_unit']),
'discount_amount_total' => floatval($li['discount_amount_total']),
'option' => intval($li['option']),
'has_text_prices' => isset($li['has_text_prices']) ? (bool)$li['has_text_prices'] : false,
'unit_price_string' => isset($li['unit_price_string']) ? $li['unit_price_string'] : '',
'gross_price_string' => isset($li['gross_price_string']) ? $li['gross_price_string'] : ''
2026-01-12 03:00:48 -08:00
);
}
2026-01-16 04:26:56 -08:00
$invoiceNumber = '';
if (!empty($document['Document']['cmc_reference'])) {
$invoiceNumber = $document['Document']['cmc_reference'];
} elseif (!empty($document['Invoice']['title'])) {
// Fallback so the Go service always receives a number-like value
$invoiceNumber = $document['Invoice']['title'];
}
2026-01-12 03:00:48 -08:00
$payload = array(
'document_id' => intval($document['Document']['id']),
2026-01-16 04:26:56 -08:00
'invoice_number' => $invoiceNumber,
2026-01-12 03:00:48 -08:00
'invoice_title' => $document['Invoice']['title'],
'customer_name' => $enquiry['Customer']['name'],
'contact_email' => $enquiry['Contact']['email'],
'contact_name' => $enquiry['Contact']['first_name'].' '.$enquiry['Contact']['last_name'],
'user_first_name' => $enquiry['User']['first_name'],
'user_last_name' => $enquiry['User']['last_name'],
'user_email' => $enquiry['User']['email'],
'your_reference' => isset($enquiry['Enquiry']['customer_reference']) ? $enquiry['Enquiry']['customer_reference'] : ('Enquiry on '.date('j M Y', strtotime($enquiry['Enquiry']['created']))),
'ship_via' => $document['Invoice']['ship_via'],
'fob' => $fob,
2026-01-12 03:00:48 -08:00
'issue_date' => $document['Invoice']['issue_date'], // expects YYYY-MM-DD
'issue_date_string' => $issue_date_string,
2026-01-12 03:00:48 -08:00
'currency_symbol' => $currencySymbol,
'currency_code' => $currencyCode,
2026-01-12 03:00:48 -08:00
'show_gst' => (bool)$gst,
'bill_to' => isset($document['Document']['bill_to']) ? $document['Document']['bill_to'] : '',
'ship_to' => isset($document['Document']['ship_to']) ? $document['Document']['ship_to'] : '',
'shipping_details' => isset($document['Document']['shipping_details']) ? $document['Document']['shipping_details'] : '',
'customer_order_number' => isset($job['Job']['customer_order_number']) ? $job['Job']['customer_order_number'] : '',
'job_title' => isset($job['Job']['title']) ? $job['Job']['title'] : '',
'payment_terms' => isset($job['Customer']['payment_terms']) ? $job['Customer']['payment_terms'] : '',
'customer_abn' => isset($job['Customer']['abn']) ? $job['Customer']['abn'] : '',
'subtotal' => $totals['subtotal'],
'gst_amount' => $totals['gst'],
'total' => $totals['total'],
2026-01-12 03:00:48 -08:00
'line_items' => $lineItems,
'output_dir' => $outputDir
);
$ch = curl_init($goEndpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlErr = curl_error($ch);
curl_close($ch);
if ($httpCode < 200 || $httpCode >= 300) {
2026-01-13 03:56:08 -08:00
echo "<p>Failed to generate PDF via Go service (HTTP $httpCode).<br>";
echo "Endpoint: " . htmlspecialchars($goEndpoint) . "<br>";
2026-01-12 03:00:48 -08:00
if ($curlErr) {
2026-01-13 03:56:08 -08:00
echo "Error: " . htmlspecialchars($curlErr) . "<br>";
}
if (!empty($response)) {
echo "Response: " . htmlspecialchars($response) . "<br>";
2026-01-12 03:00:48 -08:00
}
echo "</p>";
exit;
}
2011-08-15 17:36:48 -07:00
?>
<script type="text/javascript">
window.location.replace("/documents/view/<?=$document['Document']['id']?>");
2011-08-15 17:36:48 -07:00
</script>