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(); $location = $test->getLocation(); $tester = $test->getTester(); $values = $test->getValues(true); $location_address = $location->name . ", " . $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]); $fee = floatval($test->test_fee) > 0 ? $test->test_fee : $project->default_tester_fee; $fee = (float) $fee; $play_spent = (float) ($values["play_spent"] ?? 0); $travel_cost = (float) http::value("travel_cost", "float"); $invoice_total = $fee + $play_spent + $travel_cost; $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."); $address_parts = array_filter([ $address->address_1, $address->address_2, $address->city, $address->county, $address->postal_code, str_repeat("-", 32), "Tel: " . $tester->phone, "Email: " . $tester->email, ]); $reference = "T" . str_pad($test->id, 3, "0", STR_PAD_LEFT) . "/L" . str_pad($location->id, 3, "0", STR_PAD_LEFT) . "/U" . str_pad($tester->id, 3, "0", STR_PAD_LEFT); $tiny_font_size = 8; $small_font_size = 10; $normal_font_size = 10; $header_font_size = 14; $cell_height = 5; $cell_margin = 2; 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 // Full Width = 210 - 20 = 190 // Half Width = (210 - 20 - 10) / 2 = 90 // Column 1 x = 10 // Column 2 x = 110 $pdf = new TestPDF("P", "mm", "A4"); $pdf->SetSubject("Test Invoice"); $pdf->SetCreator(APP_NAME); $pdf->SetAuthor(APP_NAME); $pdf->SetTitle("Invoice ($reference)"); $pdf->AddPage(); $pdf->SetFont("Arial"); $pdf->SetDrawColor(240, 240, 240); $pdf->SetFillColor(240); $pdf->SetAutoPageBreak(false); // Right title $pdf->SetLeftMargin(110); $pdf->SetFont("Arial", "", 30); $pdf->MultiCell(90, 8, "INVOICE", 0, "R"); $pdf->SetFontSize($normal_font_size); $pdf->MultiCell(90, 8, "Ref: $reference", 0, "R"); $pdf->MoveY(8); $pdf->SetTextColor(128); $pdf->Cell(45, 8, "Date:", 0, 0, "R"); $pdf->SetTextColor(0); $pdf->Cell(45, 8, date("d/m/Y"), 0, 1, "R"); $pdf->SetTextColor(128); $pdf->Cell(45, 8, "Due Date:", 0, 0, "R"); $pdf->SetTextColor(0); $pdf->Cell(45, 8, date("d/m/Y", time() + 2592000), 0, 1, "R"); $pdf->Rect(110, $pdf->GetY(), 90, 8, "F"); $pdf->Cell(45, 8, "Balance Due:", 0, 0, "R"); $pdf->SetTextColor(0); $pdf->Cell(45, 8, utf8_decode("£") . number_format($invoice_total, 2), 0, 1, "R"); // Left addresses $pdf->SetXY(10, 10); $pdf->SetLeftMargin(10); $pdf->SetTextColor(0); $pdf->SetFont("Arial", "B", $normal_font_size); $pdf->Cell(90, 4, $tester->getDisplayName(), 0, 1); $pdf->SetFont("Arial"); $pdf->MultiCell(90, 4, implode("\n", $address_parts)); $pdf->MoveY(8); $pdf->SetTextColor(128); $pdf->Cell(90, 4, "Bill To:", 0, 1); $pdf->SetFont("Arial", "B", $normal_font_size); $pdf->SetTextColor(0, 0, 0); $pdf->Cell(90, 4, APP_NAME, 0, 1); $pdf->SetFont("Arial"); $pdf->MultiCell(90, 4, implode("\n", ["8 Market Street", "Liskeard", "Cornwall", "PL14 3JJ", str_repeat("-", 32), "Tel: 07841 353775"])); // Header $pdf->MoveY(8); $pdf->SetFillColor(64); $pdf->Rect(10, $pdf->GetY(), 190, 8, "F"); $pdf->SetTextColor(255); $pdf->Cell(115, 8, "Item"); $pdf->Cell(15, 8, "Quantity"); $pdf->Cell(30, 8, "Rate", 0, 0, "R"); $pdf->Cell(30, 8, "Amount", 0, 0, "R"); $pdf->MoveY(10); // Item $pdf->SetTextColor(0); $pdf->SetFont("Arial", "", $normal_font_size); $y = $pdf->GetY(); $pdf->MultiCell(115, 5, "Age Verification Test\n$location_address\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC")), 0, "L"); $next_y = $pdf->GetY(); $pdf->SetXY(125, $y); $pdf->Cell(15, 5, 1, 0, 0, "R"); $pdf->Cell(30, 5, utf8_decode("£") . number_format($fee, 2), 0, 0, "R"); $pdf->Cell(30, 5, utf8_decode("£") . number_format($fee, 2), 0, 0, "R"); $pdf->SetXY(10, $next_y); if($play_spent) { $pdf->MoveY(5); $y = $pdf->GetY(); $pdf->MultiCell(115, 5, "Play Costs", 0, "L"); $next_y = $pdf->GetY(); $pdf->SetXY(125, $y); $pdf->Cell(15, 5, 1, 0, 0, "R"); $pdf->Cell(30, 5, utf8_decode("£") . number_format($play_spent, 2), 0, 0, "R"); $pdf->Cell(30, 5, utf8_decode("£") . number_format($play_spent, 2), 0, 0, "R"); $pdf->SetXY(10, $next_y); } if($travel_cost) { $pdf->MoveY(5); $y = $pdf->GetY(); $pdf->MultiCell(115, 5, "Travel Costs", 0, "L"); $next_y = $pdf->GetY(); $pdf->SetXY(125, $y); $pdf->Cell(15, 5, 1, 0, 0, "R"); $pdf->Cell(30, 5, utf8_decode("£") . number_format($travel_cost, 2), 0, 0, "R"); $pdf->Cell(30, 5, utf8_decode("£") . number_format($travel_cost, 2), 0, 0, "R"); $pdf->SetXY(10, $next_y); } $pdf->MoveY(10); $pdf->SetTextColor(128); $pdf->Cell(150, 5, "Total:", 0, 0, "R"); $pdf->SetTextColor(0); $pdf->Cell(40, 5, utf8_decode("£") . number_format($invoice_total, 2), 0, 1, "R"); $pdf->MoveY(10); $pdf->SetTextColor(128); $pdf->MultiCell(190, 6, "Pay To:"); $pdf->SetTextColor(0); $pdf->MultiCell(190, 6, $tester->getDisplayName()); switch($tester->preferred_payment_method) { case User::PAYMENT_METHOD_BANK_TRANSFER: { $pdf->SetFont("Arial", "B"); $pdf->MultiCell(190, 6, "Bank Transfer"); $pdf->SetFont("Arial"); $pdf->MultiCell(190, 6, "Bank Name: $tester->bank_name"); $pdf->MultiCell(190, 6, "Sort Code: $tester->bank_sort_code"); $pdf->MultiCell(190, 6, "Account Number: $tester->bank_account_number"); break; } case User::PAYMENT_METHOD_PAYPAL: { $pdf->SetFont("Arial", "B"); $pdf->MultiCell(190, 6, "PayPal"); $pdf->SetFont("Arial"); $pdf->MultiCell(190, 6, "PayPal Address: $tester->paypal_email"); break; } } // Output $pdf->Output(); ?>