100 lines
4.7 KiB
PHP
Executable file
100 lines
4.7 KiB
PHP
Executable file
<?php
|
|
// 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';
|
|
|
|
// 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'] : '';
|
|
|
|
// 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
|
|
);
|
|
}
|
|
|
|
$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'
|
|
);
|
|
|
|
// 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);
|
|
|
|
$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>";
|
|
}
|
|
}
|
|
?>
|
|
|