2011-05-24 02:11:07 -07:00
|
|
|
<?php
|
2026-01-12 03:00:48 -08:00
|
|
|
// Generate the Quote PDF by calling the Go service instead of TCPDF/FPDI.
|
|
|
|
|
|
|
|
|
|
$goBaseUrl = AppController::getGoBaseUrlOrFail();
|
2026-01-16 20:41:24 -08:00
|
|
|
$goEndpoint = $goBaseUrl . '/go/api/pdf/generate-quote';
|
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'],
|
|
|
|
|
'unit_price' => floatval($li['gross_unit_price']),
|
|
|
|
|
'total_price' => floatval($li['gross_price'])
|
|
|
|
|
);
|
2011-06-20 18:18:15 -07:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 20:41:24 -08:00
|
|
|
// Prepare fields with fallbacks
|
|
|
|
|
$cmcReference = '';
|
|
|
|
|
if (!empty($enquiry['Enquiry']['title'])) {
|
|
|
|
|
$cmcReference = $enquiry['Enquiry']['title'];
|
|
|
|
|
} elseif (!empty($document['Document']['cmc_reference'])) {
|
|
|
|
|
$cmcReference = $document['Document']['cmc_reference'];
|
|
|
|
|
} else {
|
|
|
|
|
$cmcReference = 'Quote-' . $document['Document']['id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$customerName = '';
|
|
|
|
|
if (!empty($enquiry['Customer']['name'])) {
|
|
|
|
|
$customerName = $enquiry['Customer']['name'];
|
|
|
|
|
} else {
|
|
|
|
|
$customerName = 'Customer';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contactEmail = !empty($enquiry['Contact']['email']) ? $enquiry['Contact']['email'] : '';
|
|
|
|
|
$contactName = !empty($enquiry['Contact']['first_name']) ? $enquiry['Contact']['first_name'].' '.$enquiry['Contact']['last_name'] : '';
|
|
|
|
|
$userFirstName = !empty($enquiry['User']['first_name']) ? $enquiry['User']['first_name'] : '';
|
|
|
|
|
$userLastName = !empty($enquiry['User']['last_name']) ? $enquiry['User']['last_name'] : '';
|
|
|
|
|
$userEmail = !empty($enquiry['User']['email']) ? $enquiry['User']['email'] : '';
|
|
|
|
|
$createdDate = !empty($enquiry['Enquiry']['created']) ? $enquiry['Enquiry']['created'] : date('Y-m-d');
|
|
|
|
|
|
2026-01-12 03:00:48 -08:00
|
|
|
$payload = array(
|
|
|
|
|
'document_id' => intval($document['Document']['id']),
|
2026-01-16 20:41:24 -08:00
|
|
|
'cmc_reference' => $cmcReference,
|
2026-01-12 03:00:48 -08:00
|
|
|
'revision' => intval($document['Document']['revision']),
|
2026-01-16 20:41:24 -08:00
|
|
|
'created_date' => date('Y-m-d', strtotime($createdDate)),
|
|
|
|
|
'created_date_string' => date('j M Y', strtotime($createdDate)),
|
|
|
|
|
'customer_name' => $customerName,
|
|
|
|
|
'contact_email' => $contactEmail,
|
|
|
|
|
'contact_name' => $contactName,
|
|
|
|
|
'user_first_name' => $userFirstName,
|
|
|
|
|
'user_last_name' => $userLastName,
|
|
|
|
|
'user_email' => $userEmail,
|
2026-01-12 03:00:48 -08:00
|
|
|
'currency_symbol' => $currencySymbol,
|
2026-01-16 20:41:24 -08:00
|
|
|
'currency_code' => isset($currencyCode) ? $currencyCode : 'AUD',
|
2026-01-12 03:00:48 -08:00
|
|
|
'show_gst' => (bool)$gst,
|
|
|
|
|
'commercial_comments' => isset($document['Quote']['commercial_comments']) ? $document['Quote']['commercial_comments'] : '',
|
|
|
|
|
'line_items' => $lineItems,
|
|
|
|
|
'pages' => array_map(function($p) { return $p['content']; }, $document['DocPage']),
|
|
|
|
|
'output_dir' => $outputDir
|
|
|
|
|
);
|
|
|
|
|
|
2026-01-16 20:41:24 -08:00
|
|
|
// Debug: Write payload to file for debugging
|
|
|
|
|
file_put_contents($outputDir . '/quote_payload_debug.txt',
|
|
|
|
|
"=== PAYLOAD ===\n" . print_r($payload, true) .
|
|
|
|
|
"\n\n=== ENQUIRY ===\n" . print_r($enquiry, true) .
|
|
|
|
|
"\n\n=== DOCUMENT ===\n" . print_r($document, true));
|
|
|
|
|
|
2026-01-12 03:00:48 -08:00
|
|
|
$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 Quote PDF via Go service (HTTP $httpCode).";
|
|
|
|
|
if ($curlErr) {
|
|
|
|
|
echo " Error: $curlErr";
|
2011-05-24 02:11:07 -07:00
|
|
|
}
|
2026-01-16 20:41:24 -08:00
|
|
|
if (!empty($response)) {
|
|
|
|
|
echo "<br>Response: " . htmlspecialchars($response);
|
|
|
|
|
}
|
|
|
|
|
echo "<br><br>Payload sent:<br><pre>" . htmlspecialchars(json_encode($payload, JSON_PRETTY_PRINT)) . "</pre>";
|
2026-01-12 03:00:48 -08:00
|
|
|
echo "</p>";
|
|
|
|
|
exit;
|
2011-05-24 02:11:07 -07:00
|
|
|
}
|
2026-01-16 20:41:24 -08:00
|
|
|
|
2026-01-18 22:52:57 -08:00
|
|
|
// PDF generated successfully - now save metadata and count pages
|
2026-01-16 20:41:24 -08:00
|
|
|
$result = json_decode($response, true);
|
|
|
|
|
if (isset($result['filename'])) {
|
2026-01-18 22:52:57 -08:00
|
|
|
// Build path, removing any double slashes
|
2026-01-16 20:41:24 -08:00
|
|
|
$pdfPath = $outputDir . '/' . $result['filename'];
|
2026-01-18 22:52:57 -08:00
|
|
|
$pdfPath = preg_replace('#/+#', '/', $pdfPath);
|
|
|
|
|
|
|
|
|
|
// Update database with PDF metadata
|
|
|
|
|
$Document = ClassRegistry::init('Document');
|
|
|
|
|
$Document->id = $document['Document']['id'];
|
|
|
|
|
$Document->saveField('pdf_filename', $result['filename']);
|
|
|
|
|
$Document->saveField('pdf_created_at', date('Y-m-d H:i:s'));
|
|
|
|
|
|
|
|
|
|
// Get user ID safely (match invoice logic)
|
|
|
|
|
$userId = null;
|
|
|
|
|
if (isset($this->Session)) {
|
|
|
|
|
$userId = $this->Session->read('Auth.User.id');
|
|
|
|
|
}
|
|
|
|
|
if (!$userId && isset($_SESSION['Auth']['User']['id'])) {
|
|
|
|
|
$userId = $_SESSION['Auth']['User']['id'];
|
|
|
|
|
}
|
|
|
|
|
if ($userId) {
|
|
|
|
|
$Document->saveField('pdf_created_by_user_id', $userId);
|
|
|
|
|
}
|
2026-01-16 20:41:24 -08:00
|
|
|
|
|
|
|
|
// Count pages using the Go service
|
|
|
|
|
App::import('Vendor','pagecounter');
|
|
|
|
|
$pageCounter = new PageCounter();
|
|
|
|
|
$pageCount = $pageCounter->count($pdfPath);
|
|
|
|
|
|
|
|
|
|
if ($pageCount > 0) {
|
|
|
|
|
$Document->saveField('doc_page_count', $pageCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-09 01:54:10 -07:00
|
|
|
?>
|
2026-01-18 22:52:57 -08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta http-equiv="refresh" content="0;url=/documents/view/<?=$document['Document']['id']?>" />
|
|
|
|
|
<title>Redirecting...</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<p>PDF generated successfully. Redirecting back to quote...</p>
|
|
|
|
|
<p><a href="/documents/view/<?=$document['Document']['id']?>">Click here if not redirected</a></p>
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
window.location.replace("/documents/view/<?=$document['Document']['id']?>");
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|