$li['item_number'], 'quantity' => $li['quantity'], 'title' => $li['title'], 'unit_price' => floatval($li['gross_unit_price']), 'total_price' => floatval($li['gross_price']) ); } // 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'); $payload = array( 'document_id' => intval($document['Document']['id']), 'cmc_reference' => $cmcReference, 'revision' => intval($document['Document']['revision']), '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, 'currency_symbol' => $currencySymbol, 'currency_code' => isset($currencyCode) ? $currencyCode : 'AUD', '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 ); // 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)); $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 "

Failed to generate Quote PDF via Go service (HTTP $httpCode)."; if ($curlErr) { echo " Error: $curlErr"; } if (!empty($response)) { echo "
Response: " . htmlspecialchars($response); } echo "

Payload sent:

" . htmlspecialchars(json_encode($payload, JSON_PRETTY_PRINT)) . "
"; echo "

"; exit; } // PDF generated successfully - now count pages and update database $result = json_decode($response, true); if (isset($result['filename'])) { $pdfPath = $outputDir . '/' . $result['filename']; // Count pages using the Go service App::import('Vendor','pagecounter'); $pageCounter = new PageCounter(); $pageCount = $pageCounter->count($pdfPath); if ($pageCount > 0) { // Update the document with the page count $Document = ClassRegistry::init('Document'); $Document->id = $document['Document']['id']; $Document->saveField('doc_page_count', $pageCount); } } ?>