| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- use \Mpdf\Mpdf;
- $test = Test::get($id);
- if(!$test) throw new Exception(__("Test not found."));
- if($test->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"];
- 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."));
- $dst = $test->getDirectory() . "/travel_costs.jpg";
- move_uploaded_file($photo->tmp_name, $dst);
- 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();
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- body {
- font-family: sans-serif;
- }
- .title {
- font-size: 3rem;
- }
- td {
- padding: 8px;
- }
- </style>
- </head>
- <body>
- <div>
- <div style="float: left; width: 49.5%; font-size: 0.875rem;">
- <div style="float: left; width: 49.5%;">
- <strong><?=htmlspecialchars($tester->getDisplayName())?></strong><br />
- <?=nl2br(htmlspecialchars(implode("\n", $address_parts)))?><br />
- </div>
- <div style="float: right; width: 49.5%;">
- Tel: <?=htmlspecialchars($tester->phone)?><br />
- Email: <?=htmlspecialchars($tester->email)?><br />
- </div>
- <br />
- <br />
- <span color="gray">Bill To:</span><br />
- <div style="float: left; width: 49.5%;">
- <strong><?=htmlspecialchars(COMPANY_NAME)?></strong><br />
- <?=nl2br(htmlspecialchars(implode("\n", $bill_to_address_parts)))?>
- </div>
- <div style="float: right; width: 49.5%;">
- Tel: <?=htmlspecialchars(PHONE)?>
- </div>
- </div>
- <div style="float: right; width: 49.5%; text-align: right;">
- <div class="title">
- INVOICE
- </div>
- Ref: <?=$reference?><br />
- <br />
- <table width="100%" cellspacing="0">
- <tr>
- <td width="50%" align="right" color="gray">Date:</td>
- <td width="50%" align="right"><?=date("d/m/Y")?></td>
- </tr>
- <tr>
- <td align="right" color="gray">Due Date:</td>
- <td align="right"><?=date("d/m/Y", time() + 2592000)?></td>
- </tr>
- <tr bgcolor="#eee">
- <td align="right">Balance Due:</td>
- <td align="right">£<?=number_format($invoice_total, 2)?></td>
- </tr>
- </table>
- </div>
- <div style="clear: both;"></div>
- <br />
- <table width="100%" cellspacing="0" border="1">
- <tr bgcolor="#444">
- <td color="white" width="55%">Item</td>
- <td color="white">Quantity</td>
- <td color="white">Rate</td>
- <td color="white">Amount</td>
- </tr>
- <tr>
- <td>
- <strong><?=htmlspecialchars($template->name)?> Test</strong><br />
- <?=nl2br(htmlspecialchars("$location_address\n" . date("d/m/Y H:i", strtotime($test->dts_start . " UTC"))))?>
- </td>
- <td align="right" valign="top">1</td>
- <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
- <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
- </tr>
- <?php
- foreach($tester_invoice_fields as $field) {
- $value = $values[$field->name] ?? 0;
- if(!$value) continue;
- ?>
- <tr>
- <td>
- <strong><?=htmlspecialchars($field->label)?></strong>
- </td>
- <td align="right" valign="top">1</td>
- <td align="right" valign="top">£<?=number_format($value, 2)?></td>
- <td align="right" valign="top">£<?=number_format($value, 2)?></td>
- </tr>
- <?php
- }
- if($travel_cost) {
- ?>
- <tr>
- <td>
- <strong>Travel Costs</strong>
- </td>
- <td align="right" valign="top">1</td>
- <td align="right" valign="top">£<?=number_format($travel_cost, 2)?></td>
- <td align="right" valign="top">£<?=number_format($travel_cost, 2)?></td>
- </tr>
- <?php
- }
- ?>
- <tr>
- <td colspan="3" align="right" valign="top" color="gray">Total:</td>
- <td align="right" valign="top">£<?=number_format($invoice_total, 2)?></td>
- </tr>
- </table>
- <br />
- <span color="gray">Pay To:</span><br />
- <?=htmlspecialchars($tester->getDisplayName())?><br />
- <?php
- switch($tester->preferred_payment_method) {
- case User::PAYMENT_METHOD_BANK_TRANSFER:
- ?>
- <strong>Bank Transfer</strong><br />
- Bank Name: <?=htmlspecialchars($tester->bank_name)?>
- Sort Code: <?=htmlspecialchars($tester->bank_sort_code)?>
- Account Number: <?=htmlspecialchars($tester->bank_account_number)?>
- <?php
- break;
- case User::PAYMENT_METHOD_PAYPAL:
- ?>
- <strong>PayPal</strong><br />
- PayPal Address: <?=htmlspecialchars($tester->paypal_email)?>
- <?php
- break;
- }
- ?>
- </div>
- </body>
- </html>
- <?php
- $html = ob_get_clean();
- $mpdf->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();
- // Output to browser
- $mpdf->Output($download_filename, "D");
|