223 lines
10 KiB
PHP
Executable file
223 lines
10 KiB
PHP
Executable file
<?php
|
|
// Generate the Order Acknowledgement PDF by calling the Go service (similar to invoice)
|
|
error_log("=== pdf_orderack.ctp: Starting order ack PDF generation ===");
|
|
error_log("=== pdf_orderack.ctp: Document ID: " . (isset($document['Document']['id']) ? $document['Document']['id'] : 'UNDEFINED') . " ===");
|
|
|
|
if (!isset($document) || !isset($document['Document'])) {
|
|
error_log("=== pdf_orderack.ctp: ERROR - Document not set in view! ===");
|
|
echo "ERROR: Document data not available in view";
|
|
exit;
|
|
}
|
|
|
|
$goBaseUrl = AppController::getGoBaseUrlOrFail();
|
|
$goEndpoint = $goBaseUrl . '/go/document/generate/order-acknowledgement';
|
|
error_log("=== pdf_orderack.ctp: Go endpoint: " . $goEndpoint . " ===");
|
|
|
|
$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'] : ''
|
|
);
|
|
}
|
|
|
|
// Similar field extraction as invoice
|
|
$orderAckNumber = '';
|
|
if (!empty($document['Document']['cmc_reference'])) {
|
|
$orderAckNumber = $document['Document']['cmc_reference'];
|
|
} elseif (!empty($document['OrderAcknowledgement']['title'])) {
|
|
$orderAckNumber = $document['OrderAcknowledgement']['title'];
|
|
}
|
|
|
|
error_log("=== pdf_orderack.ctp: document['Document']['cmc_reference']='" . (isset($document['Document']['cmc_reference']) ? $document['Document']['cmc_reference'] : 'NOT SET') . "' ===");
|
|
error_log("=== pdf_orderack.ctp: document['Document'] keys: " . implode(', ', array_keys($document['Document'])) . " ===");
|
|
|
|
// Use enquiry title (reference) as cmc_reference if not already set
|
|
if (empty($document['Document']['cmc_reference'])) {
|
|
if (isset($enquiry['Enquiry']['title']) && !empty($enquiry['Enquiry']['title'])) {
|
|
$document['Document']['cmc_reference'] = $enquiry['Enquiry']['title'];
|
|
error_log("=== pdf_orderack.ctp: Set cmc_reference to enquiry title: " . $document['Document']['cmc_reference'] . " ===");
|
|
} else {
|
|
error_log("=== pdf_orderack.ctp: ERROR - Cannot determine cmc_reference: enquiry title not available ===");
|
|
echo "ERROR: Cannot determine CMC Reference - enquiry information missing.";
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$orderAckTitle = isset($document['OrderAcknowledgement']['title']) && !empty($document['OrderAcknowledgement']['title'])
|
|
? $document['OrderAcknowledgement']['title']
|
|
: 'OrderAck-' . $document['Document']['id'];
|
|
|
|
$customerName = 'Customer';
|
|
if (isset($enquiry['Customer']['name']) && !empty($enquiry['Customer']['name'])) {
|
|
$customerName = $enquiry['Customer']['name'];
|
|
} elseif (isset($document['Document']['cmc_reference']) && !empty($document['Document']['cmc_reference'])) {
|
|
$customerName = $document['Document']['cmc_reference'];
|
|
}
|
|
|
|
// Add fallback logic for all enquiry-dependent fields (same as invoice)
|
|
$contactEmail = isset($enquiry['Contact']['email']) ? $enquiry['Contact']['email'] : '';
|
|
$contactName = '';
|
|
if (isset($enquiry['Contact']['first_name']) && isset($enquiry['Contact']['last_name'])) {
|
|
$contactName = $enquiry['Contact']['first_name'] . ' ' . $enquiry['Contact']['last_name'];
|
|
}
|
|
|
|
$userFirstName = isset($enquiry['User']['first_name']) ? $enquiry['User']['first_name'] : '';
|
|
$userLastName = isset($enquiry['User']['last_name']) ? $enquiry['User']['last_name'] : '';
|
|
$userEmail = isset($enquiry['User']['email']) ? $enquiry['User']['email'] : '';
|
|
|
|
$yourReference = '';
|
|
if (isset($enquiry['Enquiry']['customer_reference']) && !empty($enquiry['Enquiry']['customer_reference'])) {
|
|
$yourReference = $enquiry['Enquiry']['customer_reference'];
|
|
} else if (isset($enquiry['Enquiry']['created'])) {
|
|
$yourReference = 'Enquiry on ' . date('j M Y', strtotime($enquiry['Enquiry']['created']));
|
|
} else {
|
|
$yourReference = 'Enquiry on ' . date('j M Y');
|
|
}
|
|
|
|
$payload = array(
|
|
'document_id' => intval($document['Document']['id']),
|
|
'title' => $orderAckTitle,
|
|
'customer_name' => $customerName,
|
|
'cmc_reference' => $document['Document']['cmc_reference'],
|
|
'email_to' => $contactEmail,
|
|
'attention' => $contactName,
|
|
'your_reference' => $yourReference,
|
|
'customer_order_number' => isset($job['Job']['customer_order_number']) ? $job['Job']['customer_order_number'] : '',
|
|
'job_title' => isset($job['Job']['title']) ? $job['Job']['title'] : '',
|
|
'issue_date' => isset($document['OrderAcknowledgement']['issue_date']) ? $document['OrderAcknowledgement']['issue_date'] : date('Y-m-d'),
|
|
'issue_date_string' => isset($issue_date_string) ? $issue_date_string : '',
|
|
'bill_to' => isset($document['Document']['bill_to']) ? $document['Document']['bill_to'] : '',
|
|
'ship_to' => isset($document['Document']['ship_to']) ? $document['Document']['ship_to'] : '',
|
|
'ship_via' => isset($document['OrderAcknowledgement']['ship_via']) ? $document['OrderAcknowledgement']['ship_via'] : '',
|
|
'fob' => isset($document['OrderAcknowledgement']['fob']) ? $document['OrderAcknowledgement']['fob'] : '',
|
|
'payment_terms' => isset($job['Customer']['payment_terms']) ? $job['Customer']['payment_terms'] : '',
|
|
'customer_abn' => isset($job['Customer']['abn']) ? $job['Customer']['abn'] : '',
|
|
'freight_details' => isset($document['Document']['shipping_details']) ? $document['Document']['shipping_details'] : '',
|
|
'estimated_delivery' => isset($document['OrderAcknowledgement']['estimated_delivery']) ? $document['OrderAcknowledgement']['estimated_delivery'] : '',
|
|
'currency_symbol' => $currencySymbol,
|
|
'currency_code' => $currencyCode,
|
|
'show_gst' => (bool)$gst,
|
|
'subtotal' => $totals['subtotal'],
|
|
'gst_amount' => $totals['gst'],
|
|
'total' => $totals['total'],
|
|
'line_items' => $lineItems,
|
|
'output_dir' => $outputDir
|
|
);
|
|
|
|
error_log("=== pdf_orderack.ctp: payload['cmc_reference']='" . $payload['cmc_reference'] . "' ===");
|
|
|
|
$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));
|
|
|
|
error_log("=== pdf_orderack.ctp: Making curl request to Go service ===");
|
|
$response = curl_exec($ch);
|
|
error_log("=== pdf_orderack.ctp: Curl response received: " . substr($response, 0, 200) . " ===");
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$curlErr = curl_error($ch);
|
|
error_log("=== pdf_orderack.ctp: HTTP Code: " . $httpCode . ", Curl Error: " . $curlErr . " ===");
|
|
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;
|
|
}
|
|
|
|
// PDF generated successfully - now save metadata and count pages
|
|
$result = json_decode($response, true);
|
|
if (isset($result['filename'])) {
|
|
// Build path, removing any double slashes
|
|
$pdfPath = $outputDir . '/' . $result['filename'];
|
|
$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 quote logic) with Basic Auth fallback
|
|
$userId = null;
|
|
$sessionUser = null;
|
|
if (isset($this->Session)) {
|
|
$sessionUser = $this->Session->read('Auth.User');
|
|
$userId = isset($sessionUser['id']) ? $sessionUser['id'] : null;
|
|
}
|
|
if (!$userId && isset($_SESSION['Auth']['User']['id'])) {
|
|
$userId = $_SESSION['Auth']['User']['id'];
|
|
}
|
|
// Fallback: try Basic Auth username/email -> User lookup
|
|
if (!$userId && !empty($_SERVER['PHP_AUTH_USER'])) {
|
|
$User = ClassRegistry::init('User');
|
|
$foundUser = $User->find('first', array(
|
|
'conditions' => array(
|
|
'OR' => array(
|
|
'User.email' => $_SERVER['PHP_AUTH_USER'],
|
|
'User.username' => $_SERVER['PHP_AUTH_USER'],
|
|
),
|
|
),
|
|
'fields' => array('User.id'),
|
|
'recursive' => -1,
|
|
'order' => 'User.id ASC'
|
|
));
|
|
if ($foundUser && isset($foundUser['User']['id'])) {
|
|
$userId = $foundUser['User']['id'];
|
|
}
|
|
}
|
|
|
|
if ($userId) {
|
|
$Document->saveField('pdf_created_by_user_id', $userId);
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
?>
|
|
<!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 order acknowledgement...</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>
|