Procházet zdrojové kódy

Add support for generating customer invoices

Alan Lam před 7 roky
rodič
revize
1864df7750

+ 6 - 0
config.template.php

@@ -9,6 +9,12 @@ define("ADDRESS", "Check Policy Limited\n8 Market Street, Liskeard\nCornwall, PL
 define("PHONE", "07841 353775");
 define("WHATSAPP", "447841353775");
 
+define("INVOICE_BANK_NAME", "Bank");
+define("INVOICE_ACCOUNT_NUMBER", "12345678");
+define("INVOICE_SORT_CODE", "12-34-56");
+define("INVOICE_VAT_NUMBER", "12345678");
+define("INVOICE_CONTACT_EMAIL", "noreply@check-policy.com");
+
 define("DB_HOST", "");
 define("DB_USERNAME", "");
 define("DB_PASSWORD", "");

+ 1 - 0
controllers/customer_edit.php

@@ -26,6 +26,7 @@ if($submit) {
 		$customer->country = http::value("country", "trim");
 		$customer->email = http::value("email", "email");
 		$customer->phone = http::value("phone", "trim");
+		$customer->invoice_contact_name = http::value("invoice_contact_name", "trim");
 		$customer->email_after_test = http::value("email_after_test", "int");
 		$customer->email_locations_after_test = http::value("email_locations_after_test", "int");
 

+ 2 - 2
controllers/project_edit.php

@@ -38,6 +38,7 @@ if($submit) {
 		$project->test_count = http::value("test_count", "int");
 		$project->min_age = http::value("min_age", "int");
 		$project->max_age = http::value("max_age", "int");
+		$project->default_customer_fee = http::value("default_customer_fee", "float");
 		$project->default_tester_fee = http::value("default_tester_fee", "float");
 		$project->time_of_day = http::value("time_of_day", "trim");
 		$project->tester_can_claim = http::value("tester_can_claim", "trim");
@@ -57,6 +58,7 @@ if($submit) {
 		if(empty($project->test_count)) $form_exception->addError("Test count is required.");
 		if(empty($project->min_age)) $form_exception->addError("Minimum age is required.");
 		if(empty($project->max_age)) $form_exception->addError("Maximum age is required.");
+		if(empty($project->default_customer_fee)) $form_exception->addError("Default customer fee is required.");
 		if(empty($project->default_tester_fee)) $form_exception->addError("Default tester fee is required.");
 		if(empty($project->time_of_day)) $form_exception->addError("Time of day is required.");
 		else if(!in_array($project->time_of_day, ["A", "D", "N"])) $form_exception->addError("Invalid time of day selected.");
@@ -131,5 +133,3 @@ $locations = $customer->getLocations();
 View::includeOnce("header", compact("page_title"));
 View::include("project_form", compact("project", "templates", "locations", "location_ids"));
 View::includeOnce("footer");
-
-?>

+ 290 - 0
controllers/project_invoice.php

@@ -0,0 +1,290 @@
+<?php
+
+use \Mpdf\Mpdf;
+
+$project = Project::get($id);
+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;
+
+	foreach($invoice_fields as $field) {
+		if(!isset($invoice_field_totals[$field->name])) {
+			$invoice_field_totals[$field->name] = 0;
+		}
+
+		$invoice_field_totals[$field->name] += (float) ($values[$field->name] ?? 0);
+	}
+}
+
+$total_ex_vat = $test_costs;
+$vat = round($total_ex_vat * 0.20, 2);
+
+$total_inc_vat = $total_ex_vat + $vat;
+
+// Processing
+usort($tests, function($a, $b) {
+	return $a->dts_start <=> $b->dts_start;
+});
+
+Location::load(array_column($tests, "location_id"));
+Template::load(array_column($tests, "template_id"));
+
+// Filename
+$reference =
+	"P" . str_pad($project->id, 3, "0", STR_PAD_LEFT) .
+	"/C" . str_pad($customer->id, 3, "0", STR_PAD_LEFT);
+
+$download_filename = str_replace("/", "_", $reference) . " - " . $project->name . " - Invoice.pdf";
+
+// Initialise for PDF generation
+$bill_to_address_parts = explode("\n", ADDRESS);
+
+$bill_from_address_parts = [
+	"8 Market Street",
+	"Liskeard",
+	"Cornwall",
+	"PL14 3JJ",
+];
+
+// A4 size: 210x297mm
+$mpdf = new Mpdf();
+
+$mpdf->SetSubject("Project 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;
+			}
+
+			table {
+				border-collapse: collapse;
+			}
+
+			td {
+				padding: 8px;
+				font-size: 0.875rem;
+			}
+
+			.text-center {
+				text-align: center;
+			}
+
+			.text-brand {
+				color: #269926;
+			}
+		</style>
+	</head>
+	<body>
+
+		<div>
+			<div style="float: left; width: 49.5%; font-size: 0.875rem;">
+				<img src="./public/img/logo.png" alt="<?=htmlspecialchars(COMPANY_NAME)?>" /><br />
+				<br />
+			</div>
+			<div style="float: right; width: 49.5%; text-align: right;">
+				<div class="title">
+					INVOICE
+				</div>
+			</div>
+		</div>
+
+		<div>
+			<div style="float: left; width: 49.5%; font-size: 0.875rem;">
+
+				<strong><?=htmlspecialchars(COMPANY_NAME)?></strong><br />
+				<?=nl2br(htmlspecialchars(implode("\n", $bill_from_address_parts)))?><br />
+				Tel: <?=htmlspecialchars(PHONE)?>
+
+				<br />
+				<br />
+
+				<span color="gray">Bill To:</span><br />
+
+				<strong><?=htmlspecialchars($customer->name)?></strong><br />
+				<?=htmlspecialchars($customer->invoice_contact_name)?><br />
+				<?=nl2br(htmlspecialchars(implode("\n", $bill_to_address_parts)))?><br />
+
+				<?php
+				if($customer->phone) {
+					?>
+					Tel: <?=htmlspecialchars($customer->phone)?><br />
+					<?php
+				}
+				if($customer->email) {
+					?>
+					Email: <?=htmlspecialchars($customer->email)?><br />
+					<?php
+				}
+				?>
+
+
+
+			</div>
+			<div style="float: right; width: 49.5%; text-align: right;">
+				Ref: <?=$reference?><br />
+				Name: <?=htmlspecialchars($project->name)?><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() + (30 * 86400))?></td>
+					</tr>
+					<tr bgcolor="#eee">
+						<td align="right">Balance Due:</td>
+						<td align="right">£<?=number_format($total_inc_vat, 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>
+
+				<?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;
+					?>
+					<tr>
+						<td>
+							<strong><?=htmlspecialchars($field->label)?></strong>
+						</td>
+						<td align="center" valign="top">-</td>
+						<td align="center" valign="top">-</td>
+						<td align="right" valign="top">£<?=number_format($total, 2)?></td>
+					</tr>
+					<?php
+				}
+
+				?>
+
+				<tr>
+					<td colspan="3" align="right" valign="top" color="gray">Total</td>
+					<td align="right" valign="top">£<?=number_format($total_ex_vat, 2)?></td>
+				</tr>
+
+				<tr>
+					<td colspan="3" align="right" valign="top" color="gray">VAT @ 20%</td>
+					<td align="right" valign="top">£<?=number_format($vat, 2)?></td>
+				</tr>
+
+				<tr>
+					<td colspan="3" align="right" valign="top" color="gray">Total (inc. VAT)</td>
+					<td align="right" valign="top">£<?=number_format($total_inc_vat, 2)?></td>
+				</tr>
+
+			</table>
+
+			<br />
+
+			<span color="gray">Pay To:</span><br />
+
+			<?=htmlspecialchars(COMPANY_NAME)?><br />
+			<?=htmlspecialchars(INVOICE_BANK_NAME)?><br />
+			Account Number: <?=htmlspecialchars(INVOICE_ACCOUNT_NUMBER)?><br />
+			Sort Code: <?=htmlspecialchars(INVOICE_SORT_CODE)?><br />
+
+			<p class="text-center">Payment is due within 30 days.</p>
+			<p class="text-center">If you have any questions concerning this invoice, contact: <?=htmlspecialchars(INVOICE_CONTACT_EMAIL)?>
+
+			<p class="text-brand text-center">
+				<strong>THANK YOU FOR YOUR BUSINESS!</strong>
+			</p>
+
+			<p class="text-brand text-center">
+				VAT Number: <?=htmlspecialchars(INVOICE_VAT_NUMBER)?>
+			</p>
+
+
+		</div>
+	</body>
+</html>
+<?php
+
+$html = ob_get_clean();
+
+$mpdf->WriteHTML($html);
+
+// Output to browser
+$mpdf->Output($download_filename, "D");

+ 7 - 1
controllers/test.php

@@ -305,9 +305,15 @@ else if(Core::$user->level >= User::LEVEL_ADMIN) {
 				$test->dts_end = Util::getDateTime($unix_end);
 			}
 
+			$test->customer_fee = null;
+			$customer_fee = http::value("customer_fee", "float");
+			if($customer_fee !== false && $customer_fee != $project->default_customer_fee) {
+				$test->customer_fee = $customer_fee;
+			}
+
 			$test->test_fee = null;
 			$tester_fee = http::value("tester_fee", "float");
-			if($tester_fee != $project->default_tester_fee) {
+			if($tester_fee !== false && $tester_fee != $project->default_tester_fee) {
 				$test->test_fee = $tester_fee;
 			}
 

+ 1 - 2
engine/customer.php

@@ -6,6 +6,7 @@ class Customer extends Model {
 	protected static $id_field = "id";
 	protected static $fillable_fields = ["name", "address_1", "address_2",
 		"city", "county", "postal_code", "country", "email", "phone",
+		"invoice_contact_name",
 		"email_after_test", "email_locations_after_test",
 		"internal_id_field_name", "region_field_name", "area_manager_field_name",
 	];
@@ -56,5 +57,3 @@ class Customer extends Model {
 	}
 
 }
-
-?>

+ 12 - 3
engine/project.php

@@ -4,7 +4,7 @@ class Project extends Model {
 
 	protected static $table = "project";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
+	protected static $fillable_fields = ["customer_id", "template_id", "name", "test_count", "min_age", "max_age", "default_customer_fee", "default_tester_fee", "time_of_day", "tester_can_claim", "template_overrides_json", "dts_start", "dts_deadline", "dts_created", "dts_updated", "dts_report", "dts_finished", "user_id_finished"];
 
 	protected static $field_map = [
 	];
@@ -87,6 +87,17 @@ class Project extends Model {
 		return "/account/projects/{$this->id}";
 	}
 
+	public function getDirectory() {
+		$directory = ROOT_DIR . "/files/projects/" . $this->id;
+		if(!file_exists($directory)) mkdir($directory, 0777, true);
+		return $directory;
+	}
+
+	public function hasInvoicePDF() {
+		$directory = $this->getDirectory();
+		return file_exists($directory . "/invoice.pdf");
+	}
+
 	public function isAnytime() {
 		return $this->time_of_day == "A";
 	}
@@ -121,5 +132,3 @@ class Project extends Model {
 	}
 
 }
-
-?>

+ 7 - 0
engine/template.php

@@ -52,6 +52,13 @@ class Template extends Model {
 		return $json->testerInvoiceFields ?? [];
 	}
 
+	public function getCustomerInvoiceFields() {
+		$json = json_decode($this->json);
+		if(!$json) throw new Exception("Failed to decode template JSON.");
+
+		return $json->customerInvoiceFields ?? [];
+	}
+
 	public function setOverrides($overrides) {
 		$this->overrides = $overrides;
 	}

+ 1 - 1
engine/test.php

@@ -4,7 +4,7 @@ class Test extends Model {
 
 	protected static $table = "test";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["project_id", "location_id", "tester_id", "status", "status_tester_invoice", "test_fee", "test_time", "test_travel_cost", "shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes", "dts_due", "dts_start", "dts_end", "dts_tester_invoice_generated", "dts_tester_invoice_paid", "verifier_id", "verifier_notes", "pass", "retest", "dts_created", "dts_updated", "dts_assigned", "dts_verified"];
+	protected static $fillable_fields = ["project_id", "location_id", "tester_id", "status", "status_tester_invoice", "customer_fee", "test_fee", "test_time", "test_travel_cost", "shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes", "dts_due", "dts_start", "dts_end", "dts_tester_invoice_generated", "dts_tester_invoice_paid", "verifier_id", "verifier_notes", "pass", "retest", "dts_created", "dts_updated", "dts_assigned", "dts_verified"];
 
 	const TEST_FIELDS = ["shop_left", "shop_right", "attire", "challenge_stage", "challenge_description", "challenger_name", "challenger_description", "challenger_gender", "challenger_eye_contact", "additional_notes"];
 

+ 3 - 0
json/age_verification.json

@@ -1,4 +1,7 @@
 {
+	"customerInvoiceFields": [
+		{"name": "play_spent", "label": "Money Spent by Testers"}
+	],
 	"testerInvoiceFields": [
 		{"name": "play_spent", "label": "Play Costs"}
 	],

+ 3 - 0
json/age_verification_mitchells_and_butlers.json

@@ -1,4 +1,7 @@
 {
+	"customerInvoiceFields": [
+		{"name": "play_spent", "label": "Money Spent by Testers"}
+	],
 	"testerInvoiceFields": [
 		{"name": "play_spent", "label": "Play Costs"},
 		{"name": "bar_expenditure", "label": "Bar Expenditure"}

+ 3 - 0
json/mystery_shop.json

@@ -1,4 +1,7 @@
 {
+	"customerInvoiceFields": [
+		{"name": "play_spent", "label": "Money Spent by Testers"}
+	],
 	"testerInvoiceFields": [
 		{"name": "play_spent", "label": "Play Costs"}
 	],

+ 1 - 3
routes.php

@@ -67,6 +67,7 @@ $routes = [
 	"/^account\/projects\/$id_pattern\/export-csv$/" => ["controller" => "project_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-customer-csv$/" => ["controller" => "project_customer_csv", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/export-kml$/" => ["controller" => "project_kml", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
+	"/^account\/projects\/$id_pattern\/invoice$/" => ["controller" => "project_invoice", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/(mark-finished|mark-unfinished)$/" => ["controller" => "project_mark_finished", "params" => ["id", "action"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/projects\/$id_pattern\/(get-locations|get-testers|create-test|assign-tester)$/" => ["controller" => "project_ajax", "params" => ["id", "action"], "type" => "json", "require_user_level" => User::LEVEL_ADMIN],
 
@@ -128,7 +129,4 @@ $routes = [
 	"/^account\/reports$/" => ["controller" => "reports", "require_user_level" => User::LEVEL_VENUE],
 	"/^account\/reports\/(export-odp|export-pptx)$/" => ["controller" => "reports", "params" => ["action"], "require_user_level" => User::LEVEL_VENUE],
 
-
 ];
-
-?>

+ 8 - 2
views/customer_form.php

@@ -47,17 +47,23 @@
 			</div>
 		</div>
 		<div class="row mb-1">
-			<label class="col-sm-4 col-form-label"for="email">Email Address</label>
+			<label class="col-sm-4 col-form-label" for="email">Email Address</label>
 			<div class="col-sm">
 				<input class="form-control" type="email" name="email" id="email" placeholder="Enter email address" value="<?=htmlspecialchars($customer->email)?>" />
 			</div>
 		</div>
 		<div class="row mb-1">
-			<label class="col-sm-4 col-form-label"for="phone">Contact Number</label>
+			<label class="col-sm-4 col-form-label" for="phone">Contact Number</label>
 			<div class="col-sm">
 				<input class="form-control" type="text" name="phone" id="phone" placeholder="Enter contact number" value="<?=htmlspecialchars($customer->phone)?>" />
 			</div>
 		</div>
+		<div class="row mb-1">
+			<label class="col-sm-4 col-form-label" for="invoice-contact-name">Invoice Contact Name</label>
+			<div class="col-sm">
+				<input class="form-control" type="text" name="invoice_contact_name" id="invoice-contact-name" placeholder="Enter invoice contact name" value="<?=htmlspecialchars($customer->invoice_contact_name)?>" />
+			</div>
+		</div>
 		<div class="row my-3">
 			<div class="col-sm offset-sm-4">
 				<div class="form-check">

+ 1 - 0
views/project.php

@@ -35,6 +35,7 @@ var projectID = <?=$project->id?>;
 					<?php
 					if($project->dts_finished) {
 						?>
+						<a class="dropdown-item" href="/account/projects/<?=$project->id?>/invoice">Generate Invoice</a>
 						<a class="dropdown-item" href="/account/projects/<?=$project->id?>/mark-unfinished">Mark Unfinished</a>
 						<?php
 					}

+ 11 - 0
views/project_form.php

@@ -60,6 +60,17 @@ $time_of_day_options = [
 						<input class="form-control" type="number" name="test_count" id="test-count" placeholder="Enter test count" value="<?=htmlspecialchars($project->test_count)?>" required />
 					</div>
 				</div>
+				<div class="row mb-1">
+					<label class="col-sm-4 col-form-label" for="default-customer-fee">Default Customer Fee</label>
+					<div class="col-sm">
+						<div class="input-group">
+							<div class="input-group-prepend">
+								<span class="input-group-text">£</span>
+							</div>
+							<input class="form-control" type="number" name="default_customer_fee" min=0" step="0.01" id="default-customer-fee" placeholder="Enter default customer fee" value="<?=htmlspecialchars($project->default_customer_fee)?>" required />
+						</div>
+					</div>
+				</div>
 				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label" for="default-tester-fee">Default Tester Fee</label>
 					<div class="col-sm">

+ 11 - 0
views/test_verify_form.php

@@ -329,6 +329,17 @@ function render_card_list_group($details) {
 								</select>
 							</div>
 						</div>
+						<div class="row mb-1">
+							<label class="col-sm-4 col-form-label" for="customer-fee">Customer Fee Override</label>
+							<div class="col-sm">
+								<div class="input-group">
+									<div class="input-group-prepend">
+										<span class="input-group-text">£</span>
+									</div>
+									<input class="form-control" type="number" step="0.01" min="0" name="customer_fee" id="customer-fee" placeholder="Enter customer fee override" value="<?=htmlspecialchars($test->customer_fee)?>" />
+								</div>
+							</div>
+						</div>
 						<div class="row mb-1">
 							<label class="col-sm-4 col-form-label" for="tester-fee">Tester Fee Override</label>
 							<div class="col-sm">