100 lines
4.4 KiB
PHP
Executable file
100 lines
4.4 KiB
PHP
Executable file
<?php
|
|
// Generate the Invoice PDF by calling the Go service instead of TCPDF.
|
|
|
|
$goBaseUrl = AppController::getGoBaseUrlOrFail();
|
|
$goEndpoint = $goBaseUrl . '/go/api/pdf/generate-invoice';
|
|
|
|
$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'] : '',
|
|
'is_html' => true, // Description is always HTML
|
|
'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'] : ''
|
|
);
|
|
}
|
|
|
|
$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'];
|
|
}
|
|
|
|
$payload = array(
|
|
'document_id' => intval($document['Document']['id']),
|
|
'invoice_number' => $invoiceNumber,
|
|
'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,
|
|
'issue_date' => $document['Invoice']['issue_date'], // expects YYYY-MM-DD
|
|
'issue_date_string' => $issue_date_string,
|
|
'currency_symbol' => $currencySymbol,
|
|
'currency_code' => $currencyCode,
|
|
'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'],
|
|
'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) {
|
|
echo "<p>Failed to generate PDF via Go service (HTTP $httpCode).<br>";
|
|
echo "Endpoint: " . htmlspecialchars($goEndpoint) . "<br>";
|
|
if ($curlErr) {
|
|
echo "Error: " . htmlspecialchars($curlErr) . "<br>";
|
|
}
|
|
if (!empty($response)) {
|
|
echo "Response: " . htmlspecialchars($response) . "<br>";
|
|
}
|
|
echo "</p>";
|
|
exit;
|
|
}
|
|
?>
|
|
|
|
<script type="text/javascript">
|
|
window.location.replace("/documents/view/<?=$document['Document']['id']?>");
|
|
</script>
|
|
|