status != Test::STATUS_COMPLETED) { http_response_code(403); throw new Exception(__("An invoice cannot be generated for this test yet.")); } if(Core::$user->id != $test->tester_id && !Core::$user->canAdmin()) { http_response_code(403); throw new Exception(__("You do not have permission to access this resource.")); } $project = $test->getProject(); $template = $project->getTemplate(); $location = $test->getLocation(); $tester = $test->getTester(); $values = $test->getValues(true); $location_address = $location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]); // Base cost $fee = $test->getTestFee(); $travel_cost = $test->getTravelCosts(); $invoice_total = $fee + $travel_cost; // Additional tester invoice fields $tester_invoice_fields = $template->getTesterInvoiceFields(); foreach($tester_invoice_fields as $field) { $invoice_total += (float) $values[$field->name] ?? 0; } // Get invoice address $address = $tester->getInvoiceAddress(); if(!$address) throw new Exception(__("Invoice address not found. Please set an address as the invoice address then try again.")); if(!$tester->hasValidPaymentMethod()) throw new Exception(__("No valid payment method found. Please go to the Settings page and make sure all respective fields are filled in and then try again.")); // Filename $reference = $test->getTesterInvoiceReference(); $download_filename = str_replace("/", "_", $reference) . " - " . $tester->getDisplayName() . " - Invoice.pdf"; // Check for existing PDF $directory = $test->getDirectory(); $pdf_filepath = $test->getTesterInvoicePDFPath(); if($test->hasTesterInvoicePDF()) { http::setDownloadHeaders($download_filename); header("Content-Type: application/pdf"); readfile($pdf_filepath); exit; } // Initialise for PDF generation $address_parts = array_filter([ $address->address_1, $address->address_2, $address->city, $address->county, $address->postal_code, ]); $bill_to_address_parts = explode("\n", ADDRESS); list($date_start, $time_start) = explode(" ", $test->dts_start); list($date_end, $time_end) = explode(" ", $test->dts_end); $dt_start = new DateTime($test->dts_start); $dt_end = new DateTime($test->dts_end); $diff = $dt_start->diff($dt_end, true); $date_start = date("d/m/Y", strtotime($test->dts_start)); // A4 size: 210x297mm $mpdf = new Mpdf(); $mpdf->SetSubject("Test Invoice"); $mpdf->SetCreator(APP_NAME); $mpdf->SetAuthor(APP_NAME); $mpdf->SetTitle("Invoice ($reference)"); $mpdf->useSubstitutions = true; ob_start(); ?>
| Date: | =date("d/m/Y")?> |
| Due Date: | =date("d/m/Y", time() + 2592000)?> |
| Balance Due: | £=number_format($invoice_total, 2)?> |
| Item | Quantity | Rate | Amount |
|
=htmlspecialchars($template->name)?> Test =nl2br(htmlspecialchars("$location_address\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC"))))?> |
1 | £=number_format($fee, 2)?> | £=number_format($fee, 2)?> |
| =htmlspecialchars($field->label)?> | 1 | £=number_format($value, 2)?> | £=number_format($value, 2)?> |
| Travel Costs | 1 | £=number_format($travel_cost, 2)?> | £=number_format($travel_cost, 2)?> |
| Total: | £=number_format($invoice_total, 2)?> | ||