|
|
@@ -7,55 +7,19 @@ if(!$project) throw new Exception(__("Project not found."));
|
|
|
if(!$project->dts_finished) throw new Exception(__("An invoice cannot be generated for this project as it has not been marked as finished."));
|
|
|
if(!Core::$user->canAdmin()) throw new Exception(__("You do not have permission to access this resource."));
|
|
|
|
|
|
-$customer = $project->getCustomer();
|
|
|
-$template = $project->getTemplate();
|
|
|
-$invoice_fields = $template->getCustomerInvoiceFields();
|
|
|
-
|
|
|
-// Calculation
|
|
|
-$default_customer_fee = $project->default_customer_fee;
|
|
|
-$tests = $project->getTests();
|
|
|
-
|
|
|
-$invoice_field_totals = [];
|
|
|
-$test_fee_invoice_counts = [];
|
|
|
-
|
|
|
-$test_costs = 0;
|
|
|
-foreach($tests as $test) {
|
|
|
-
|
|
|
- $values = $test->getValues(true);
|
|
|
-
|
|
|
- $fee = $test->customer_fee ?? $default_customer_fee;
|
|
|
- $test_costs += $fee;
|
|
|
-
|
|
|
- $count = $test_fee_invoice_counts[$fee] ?? 0;
|
|
|
- ++$count;
|
|
|
- $test_fee_invoice_counts[$fee] = $count;
|
|
|
+$invoice = $project->getCustomerInvoice();
|
|
|
|
|
|
- foreach($invoice_fields as $field) {
|
|
|
- if(!isset($invoice_field_totals[$field->name])) {
|
|
|
- $invoice_field_totals[$field->name] = 0;
|
|
|
- }
|
|
|
+$line_items = $invoice->getLineItems();
|
|
|
|
|
|
- $invoice_field_totals[$field->name] += (float) ($values[$field->name] ?? 0);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-$total_ex_vat = $test_costs + array_sum($invoice_field_totals);
|
|
|
-$vat = round($total_ex_vat * 0.20, 2);
|
|
|
-
|
|
|
-$total_inc_vat = $total_ex_vat + $vat;
|
|
|
+$customer = $project->getCustomer();
|
|
|
|
|
|
-// Processing
|
|
|
-usort($tests, function($a, $b) {
|
|
|
- return $a->dts_start <=> $b->dts_start;
|
|
|
-});
|
|
|
+$total_ex_vat = $invoice->getTotal();
|
|
|
+$vat = $invoice->getTax();
|
|
|
|
|
|
-Location::load(array_column($tests, "location_id"));
|
|
|
-Template::load(array_column($tests, "template_id"));
|
|
|
+$total_inc_vat = $invoice->getTotalIncTax();
|
|
|
|
|
|
// Filename
|
|
|
-$reference =
|
|
|
- "P" . str_pad($project->id, 3, "0", STR_PAD_LEFT) .
|
|
|
- "/C" . str_pad($customer->id, 3, "0", STR_PAD_LEFT);
|
|
|
+$reference = $invoice->getReference();
|
|
|
|
|
|
$download_filename = str_replace("/", "_", $reference) . " - " . $project->name . " - Invoice.pdf";
|
|
|
|
|
|
@@ -190,53 +154,18 @@ ob_start();
|
|
|
</tr>
|
|
|
|
|
|
<?php
|
|
|
- /*
|
|
|
- foreach($tests as $test) {
|
|
|
- $location = $test->getLocation();
|
|
|
- $location_address = $location->getDisplayAddress(["address_1", "address_2", "city", "county", "postal_code"]);
|
|
|
- ?>
|
|
|
-
|
|
|
- <tr>
|
|
|
- <td>
|
|
|
- <strong><?=htmlspecialchars($location->name)?></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($default_customer_fee, 2)?></td>
|
|
|
- <td align="right" valign="top">£<?=number_format($default_customer_fee, 2)?></td>
|
|
|
- </tr>
|
|
|
- <?php
|
|
|
- }
|
|
|
- */
|
|
|
-
|
|
|
- foreach($test_fee_invoice_counts as $fee => $count) {
|
|
|
- ?>
|
|
|
- <tr>
|
|
|
- <td>
|
|
|
- <strong><?=htmlspecialchars($template->name)?> Testing</strong>
|
|
|
- </td>
|
|
|
- <td align="right" valign="top"><?=number_format($count)?></td>
|
|
|
- <td align="right" valign="top">£<?=number_format($fee, 2)?></td>
|
|
|
- <td align="right" valign="top">£<?=number_format($fee * $count, 2)?></td>
|
|
|
- </tr>
|
|
|
- <?php
|
|
|
- }
|
|
|
-
|
|
|
- foreach($invoice_fields as $field) {
|
|
|
- $total = $invoice_field_totals[$field->name] ?? 0;
|
|
|
- if(!$total) continue;
|
|
|
+ foreach($line_items as $item) {
|
|
|
?>
|
|
|
<tr>
|
|
|
<td>
|
|
|
- <strong><?=htmlspecialchars($field->label)?></strong>
|
|
|
+ <strong><?=htmlspecialchars($item->getName())?></strong>
|
|
|
</td>
|
|
|
- <td align="center" valign="top">-</td>
|
|
|
- <td align="center" valign="top">-</td>
|
|
|
- <td align="right" valign="top">£<?=number_format($total, 2)?></td>
|
|
|
+ <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayQuantity())?></td>
|
|
|
+ <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayPrice())?></td>
|
|
|
+ <td align="right" valign="top"><?=htmlspecialchars($item->getDisplayTotal())?></td>
|
|
|
</tr>
|
|
|
<?php
|
|
|
}
|
|
|
-
|
|
|
?>
|
|
|
|
|
|
<tr>
|