status != Test::STATUS_COMPLETED) throw new Exception(__("An invoice cannot be generated for this test yet.")); if(Core::$user->id != $test->tester_id && !Core::$user->canAdmin()) 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 = floatval($test->test_fee) > 0 ? $test->test_fee : $project->default_tester_fee; $fee = (float) $fee; $travel_cost = (float) http::value("travel_cost", "float"); $description = http::value("description", "trim"); $photo = (object) $_FILES["photo"]; $has_photo = false; if($travel_cost > 0) { if(!$description) throw new Exception(__("Description cannot be empty.")); switch($photo->error) { case UPLOAD_ERR_OK: $is_jpeg = Image::isJPEG($photo->tmp_name); if(!$is_jpeg) throw new Exception(__("The file uploaded was not a JPEG. Please try again.")); $has_photo = true; break; case UPLOAD_ERR_NO_FILE: throw new Exception(__("No file was uploaded. Please try again.")); break; default: // Upload error throw new Exception(__("An upload error has occurred. Please try again.")); break; } } $test->test_travel_cost = $travel_cost; $test->test_travel_cost_description = $description; $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 = $directory . "/tester_invoice.pdf"; 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(); ?>
getDisplayName())?>

Tel: phone)?>
Email: email)?>


Bill To:

Tel:
INVOICE
Ref:

Date:
Due Date:
Balance Due: £

name] ?? 0; if(!$value) continue; ?>
Item Quantity Rate Amount
name)?> Test
dts_start . " UTC"))))?>
1 £ £
label)?> 1 £ £
Travel Costs 1 £ £
Total: £

Pay To:
getDisplayName())?>
preferred_payment_method) { case User::PAYMENT_METHOD_BANK_TRANSFER: ?> Bank Transfer
Bank Name: bank_name)?> Sort Code: bank_sort_code)?> Account Number: bank_account_number)?> PayPal
PayPal Address: paypal_email)?>
WriteHTML($html); // Save PDF to directory $pdf_string = $mpdf->Output("", "S"); file_put_contents($pdf_filepath, $pdf_string); // Mark invoice generated $test->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PENDING; $test->dts_tester_invoice_generated = Util::getDateTime(); $test->commit(); // Move photo to folder if($has_photo) { $dst = $test->getDirectory() . "/travel_costs.jpg"; move_uploaded_file($photo->tmp_name, $dst); } // Output to browser $mpdf->Output($download_filename, "D");