Updates to call go

This commit is contained in:
Finley Ghosh 2026-01-13 22:48:09 +11:00
parent 2673886033
commit 1159991b00
7 changed files with 109 additions and 31 deletions

View file

@ -82,7 +82,7 @@ func main() {
goRouter.HandleFunc("/attachments/{id}", attachmentHandler.Get).Methods("GET")
goRouter.HandleFunc("/attachments/{id}", attachmentHandler.Delete).Methods("DELETE")
// PDF generation routes
// PDF generation routes (must come BEFORE the file server PathPrefix handler)
goRouter.HandleFunc("/pdf/generate-invoice", handlers.GenerateInvoicePDF).Methods("POST")
goRouter.HandleFunc("/pdf/generate-quote", handlers.GenerateQuotePDF).Methods("POST")
goRouter.HandleFunc("/pdf/generate-po", handlers.GeneratePurchaseOrderPDF).Methods("POST")
@ -90,10 +90,9 @@ func main() {
goRouter.HandleFunc("/pdf/generate-orderack", handlers.GenerateOrderAckPDF).Methods("POST")
goRouter.HandleFunc("/pdf/count-pages", handlers.CountPages).Methods("POST")
// Serve generated PDFs
// Serve generated PDFs (must come AFTER specific routes)
pdfDir := os.Getenv("PDF_OUTPUT_DIR")
if pdfDir == "" {
g
pdfDir = "webroot/pdf"
}
goRouter.PathPrefix("/pdf/").Handler(http.StripPrefix("/go/pdf/", http.FileServer(http.Dir(pdfDir))))

View file

@ -1554,4 +1554,22 @@ ENDINSTRUCTIONS;
}
return $newDoc[$model];
}
/**
* Calculate invoice totals (subtotal, GST, total)
* Used by the Go PDF endpoint
*/
function calculateInvoiceTotals($documentId) {
$document = $this->Document->find('first', array(
'conditions' => array('Document.id' => $documentId),
'recursive' => 1
));
if (empty($document)) {
return array('subtotal' => 0, 'gst' => 0, 'total' => 0);
}
$totals = $this->calculateTotals($document, $document['Invoice']['gst']);
return $totals;
}
}

View file

@ -1,38 +1,99 @@
<?php
App::import('Vendor','pdfdoc');
// Call the Go PDF generation endpoint instead of using TCPDF
// Use environment variable for Go service URL, default to Docker DNS name and port
$goHost = getenv('GO_APP_HOST') ?: 'cmc-go:8080';
$goApiUrl = 'http://' . $goHost . '/go/pdf/generate-invoice';
$pdfdoc = new PDFDOC();
// Prepare invoice data for Go API
$billTo = isset($document['Document']['bill_to']) ? $document['Document']['bill_to'] : '';
$shipTo = isset($document['Document']['ship_to']) ? $document['Document']['ship_to'] : '';
$shippingDetails = isset($document['Document']['shipping_details']) ? $document['Document']['shipping_details'] : 'TBA';
$customerOrderNumber = isset($document['Document']['customer_order_number']) ? $document['Document']['customer_order_number'] : '';
$paymentTerms = isset($document['Document']['payment_terms']) ? $document['Document']['payment_terms'] : '';
$customerABN = isset($document['Document']['customer_abn']) ? $document['Document']['customer_abn'] : '';
$pdfdoc->SetPrintHeader(false);
$pdfdoc->SetPrintFooter(false);
// Calculate totals
$totals = $this->requestAction('/documents/calculateInvoiceTotals/'.$document['Document']['id']);
$subtotal = isset($totals['subtotal']) ? $totals['subtotal'] : 0;
$gstAmount = isset($totals['gst']) ? $totals['gst'] : 0;
$total = isset($totals['total']) ? $totals['total'] : 0;
// Prepare line items
$lineItems = array();
foreach ($document['LineItem'] as $item) {
$lineItems[] = array(
'item_number' => $item['item_number'],
'quantity' => $item['quantity'],
'title' => $item['title'],
'description' => isset($item['description']) ? $item['description'] : '',
'unit_price' => isset($item['unit_price_string']) ? floatval($item['unit_price_string']) : 0,
'total_price' => isset($item['gross_price_string']) ? floatval($item['gross_price_string']) : 0
);
}
$pdfdoc->AddPage();
$pdfdoc->Page1Header();
$invoicePayload = array(
'document_id' => $document['Document']['id'],
'invoice_title' => $document['Invoice']['title'],
'customer_name' => $document['Customer']['name'],
'contact_email' => isset($document['Contact']['email']) ? $document['Contact']['email'] : '',
'contact_name' => isset($document['Contact']['first_name']) ? $document['Contact']['first_name'] . ' ' . $document['Contact']['last_name'] : '',
'user_first_name' => isset($user['User']['first_name']) ? $user['User']['first_name'] : '',
'user_last_name' => isset($user['User']['last_name']) ? $user['User']['last_name'] : '',
'user_email' => isset($user['User']['email']) ? $user['User']['email'] : '',
'your_reference' => isset($document['Document']['your_reference']) ? $document['Document']['your_reference'] : '',
'ship_via' => isset($ship_via) ? $ship_via : '',
'fob' => isset($fob) ? $fob : '',
'issue_date' => $document['Invoice']['issue_date'],
'issue_date_string' => isset($issue_date_string) ? $issue_date_string : date('d F Y', strtotime($document['Invoice']['issue_date'])),
'currency_symbol' => isset($document['Currency']['symbol']) ? $document['Currency']['symbol'] : '$',
'currency_code' => isset($document['Currency']['code']) ? $document['Currency']['code'] : 'AUD',
'show_gst' => $document['Invoice']['gst'],
'bill_to' => $billTo,
'ship_to' => $shipTo,
'shipping_details' => $shippingDetails,
'customer_order_number' => $customerOrderNumber,
'job_title' => isset($job['Job']['title']) ? $job['Job']['title'] : '',
'payment_terms' => $paymentTerms,
'customer_abn' => $customerABN,
'subtotal' => $subtotal,
'gst_amount' => $gstAmount,
'total' => $total,
'line_items' => $lineItems,
'output_dir' => '../php/app/webroot/pdf'
);
$pageTitle = "<h1>TAX INVOICE</h1>";
$pdfdoc->writeHTML($pageTitle, true, false, false, false, 'C');
// Call Go API
$ch = curl_init($goApiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($invoicePayload));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$pdfdoc->SetTextColor(0);
$pageNo = $pdfdoc->PageNoFormatted();
$totalCount = $pdfdoc->getAliasNbPages();
$shippingBillingBox = $this->element('pdf_shipping_billing_box', array('pageNo'=>$pageNo, 'totalCount'=>$totalCount));
$pdfdoc->writeHTML($shippingBillingBox, false);
$LineItemTable = $this->element('line_items_table_with_shipping');
$pdfdoc->SetPrintHeader(true);
$pdfdoc->pageContent($LineItemTable);
$this->element('pdf_output', array('pdfdoc'=>$pdfdoc));
$response = curl_exec($ch);
$curlError = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$result = json_decode($response, true);
if (isset($result['filename'])) {
echo "<script type=\"text/javascript\">
window.location.replace(\"/documents/view/{$document['Document']['id']}\");
</script>";
} else {
echo "Error: Invalid response from PDF generator";
}
} else {
echo "Error: PDF generation failed (HTTP {$httpCode})<br>";
echo "API URL: " . htmlspecialchars($goApiUrl) . "<br>";
if (!empty($curlError)) {
echo "Curl Error: " . htmlspecialchars($curlError) . "<br>";
}
if (!empty($response)) {
echo "<pre>" . htmlspecialchars($response) . "</pre>";
}
}
?>
<script type="text/javascript">
window.location.replace("/documents/view/<?=$document['Document']['id']?>");
</script>

BIN
php/app/webroot/pdf/.pdf Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
php/app/webroot/pdf/_3.pdf Normal file

Binary file not shown.