diff --git a/go/cmd/server/main.go b/go/cmd/server/main.go index 284e75e5..1f126da3 100644 --- a/go/cmd/server/main.go +++ b/go/cmd/server/main.go @@ -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)))) diff --git a/php/app/controllers/documents_controller.php b/php/app/controllers/documents_controller.php index e8e67c4f..9ca90aa8 100755 --- a/php/app/controllers/documents_controller.php +++ b/php/app/controllers/documents_controller.php @@ -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; + } } diff --git a/php/app/views/documents/pdf_invoice.ctp b/php/app/views/documents/pdf_invoice.ctp index e5da9f87..df57026c 100755 --- a/php/app/views/documents/pdf_invoice.ctp +++ b/php/app/views/documents/pdf_invoice.ctp @@ -1,38 +1,99 @@ 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 = "
" . htmlspecialchars($response) . ""; + } +} ?> - - diff --git a/php/app/webroot/pdf/.pdf b/php/app/webroot/pdf/.pdf new file mode 100644 index 00000000..7acc2ce5 Binary files /dev/null and b/php/app/webroot/pdf/.pdf differ diff --git a/php/app/webroot/pdf/CMCPL10361.pdf b/php/app/webroot/pdf/CMCPL10361.pdf new file mode 100644 index 00000000..1536d621 Binary files /dev/null and b/php/app/webroot/pdf/CMCPL10361.pdf differ diff --git a/php/app/webroot/pdf/CMCPO5421-Rev1_1.pdf b/php/app/webroot/pdf/CMCPO5421-Rev1_1.pdf new file mode 100644 index 00000000..2e1c9c5b Binary files /dev/null and b/php/app/webroot/pdf/CMCPO5421-Rev1_1.pdf differ diff --git a/php/app/webroot/pdf/_3.pdf b/php/app/webroot/pdf/_3.pdf new file mode 100644 index 00000000..cb7faccb Binary files /dev/null and b/php/app/webroot/pdf/_3.pdf differ