Pārlūkot izejas kodu

Update workflow for invoicing

Alan Lam 6 gadi atpakaļ
vecāks
revīzija
71dbdae7d3

+ 26 - 17
controllers/test_invoice.php

@@ -21,26 +21,29 @@ $fee = (float) $fee;
 
 
 $travel_cost = (float) http::value("travel_cost", "float");
 $travel_cost = (float) http::value("travel_cost", "float");
 $description = http::value("description", "trim");
 $description = http::value("description", "trim");
-
 $photo = (object) $_FILES["photo"];
 $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;
+$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 = $travel_cost;
 $test->test_travel_cost_description = $description;
 $test->test_travel_cost_description = $description;
 
 
@@ -277,5 +280,11 @@ $test->status_tester_invoice = Test::STATUS_TESTER_INVOICE_PENDING;
 $test->dts_tester_invoice_generated = Util::getDateTime();
 $test->dts_tester_invoice_generated = Util::getDateTime();
 $test->commit();
 $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
 // Output to browser
 $mpdf->Output($download_filename, "D");
 $mpdf->Output($download_filename, "D");

+ 0 - 2
controllers/tests_completed.php

@@ -23,5 +23,3 @@ View::include("test_list_completed", compact("tests", "can_invoice", "pagination
 View::include("modal_test_invoice");
 View::include("modal_test_invoice");
 View::includeOnce("modal_page_jump");
 View::includeOnce("modal_page_jump");
 View::includeOnce("footer");
 View::includeOnce("footer");
-
-?>

+ 22 - 0
controllers/tests_completed_ajax.php

@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * @var int $test_id
+ */
+
+$user = Core::$user;
+$tests = $user->getCompletedTests();
+$tests = array_values(array_filter($tests, function(Test $test) use ($test_id) { return $test->id == $test_id; }));
+
+if(!$tests) {
+	JSON::fail();
+}
+
+/** @var Test $test */
+$test = $tests[0];
+
+JSON::success([
+	"invoiceGenerated" => $test->hasTesterInvoicePDF(),
+	"travelCosts" => (float) $test->test_travel_cost,
+	"travelCostDescription" => $test->test_travel_cost_description
+]);

+ 4 - 0
engine/json.php

@@ -8,6 +8,8 @@ class JSON {
 
 
 	public static function success($array=array()) {
 	public static function success($array=array()) {
 
 
+		self::header();
+
 		$flags = PRODUCTION ? 0 : JSON_PRETTY_PRINT;
 		$flags = PRODUCTION ? 0 : JSON_PRETTY_PRINT;
 
 
 		$output = array(
 		$output = array(
@@ -22,6 +24,8 @@ class JSON {
 
 
 	public static function fail($array = []) {
 	public static function fail($array = []) {
 
 
+		self::header();
+
 		$flags = PRODUCTION ? 0 : JSON_PRETTY_PRINT;
 		$flags = PRODUCTION ? 0 : JSON_PRETTY_PRINT;
 
 
 		$output = array(
 		$output = array(

+ 48 - 4
public/js/app-completed-tests.js

@@ -1,19 +1,29 @@
 $(function() {
 $(function() {
 
 
+	const $pleaseWaitContainer = $("#please-wait-container");
 	const $travelCostsQuestionContainer = $("#travel-costs-question-container");
 	const $travelCostsQuestionContainer = $("#travel-costs-question-container");
 	const $travelCostsApprovedContainer = $("#travel-costs-approved-container");
 	const $travelCostsApprovedContainer = $("#travel-costs-approved-container");
-	const $travelCostsNotApprovedContainer = $("#travel-costs-not-approved-container");
+	const $downloadInvoiceContainer = $("#download-invoice-container");
+
+	const $travelCostsField = $("#travel-costs");
+	const $descriptionField = $("#travel-cost-description");
+	const $photoField = $("#travel-cost-photo");
 
 
 	const $approvedButton = $(".btn-costs-approved");
 	const $approvedButton = $(".btn-costs-approved");
 	$approvedButton.on("click", () => {
 	$approvedButton.on("click", () => {
 		$travelCostsQuestionContainer.collapse("hide");
 		$travelCostsQuestionContainer.collapse("hide");
 		$travelCostsApprovedContainer.collapse("show");
 		$travelCostsApprovedContainer.collapse("show");
+		$downloadInvoiceContainer.collapse("show");
+		$descriptionField.prop("required", true);
+		$photoField.prop("required", true);
 	});
 	});
 
 
 	const $notApprovedButton = $(".btn-costs-not-approved");
 	const $notApprovedButton = $(".btn-costs-not-approved");
 	$notApprovedButton.on("click", () => {
 	$notApprovedButton.on("click", () => {
 		$travelCostsQuestionContainer.collapse("hide");
 		$travelCostsQuestionContainer.collapse("hide");
-		$travelCostsNotApprovedContainer.collapse("show");
+		$downloadInvoiceContainer.collapse("show");
+		$descriptionField.prop("required", false);
+		$photoField.prop("required", false);
 	});
 	});
 
 
 	const $invoiceModal = $("#modal-test-invoice");
 	const $invoiceModal = $("#modal-test-invoice");
@@ -24,12 +34,46 @@ $(function() {
 
 
 		$(this).find("form").attr("action", "/account/tests/" + id + "/invoice");
 		$(this).find("form").attr("action", "/account/tests/" + id + "/invoice");
 		$(this).find("input[name=travel_cost]").val("0.00");
 		$(this).find("input[name=travel_cost]").val("0.00");
+
+		$.get(`/account/tests/completed/${id}`).then(
+			response => {
+
+				$pleaseWaitContainer.collapse("hide");
+
+				$travelCostsField.prop("disabled", response.invoiceGenerated);
+				$descriptionField.prop("disabled", response.invoiceGenerated);
+				$photoField.prop("disabled", response.invoiceGenerated);
+
+				if(response.invoiceGenerated) {
+					if(response.travelCosts > 0) {
+						$approvedButton.trigger("click");
+						$travelCostsField.val(response.travelCosts);
+						$descriptionField.val(response.travelCostDescription);
+					}
+					else {
+						$notApprovedButton.trigger("click");
+						console.log("nope");
+					}
+				}
+				else {
+					$travelCostsQuestionContainer.collapse("show");
+				}
+
+
+			}
+		);
+
 	});
 	});
 	$invoiceModal.on("hidden.bs.modal", () => {
 	$invoiceModal.on("hidden.bs.modal", () => {
-		$travelCostsQuestionContainer.collapse("show");
+		$pleaseWaitContainer.collapse("show");
+		$travelCostsQuestionContainer.collapse("hide");
 		$travelCostsApprovedContainer.collapse("hide");
 		$travelCostsApprovedContainer.collapse("hide");
-		$travelCostsNotApprovedContainer.collapse("hide");
+		$downloadInvoiceContainer.collapse("hide");
 		$invoiceModal.find("form").get(0).reset();
 		$invoiceModal.find("form").get(0).reset();
+
+		$travelCostsField.prop("disabled", false);
+		$descriptionField.prop("disabled", false);
+		$photoField.prop("disabled", false);
 	});
 	});
 
 
 });
 });

+ 1 - 0
routes.php

@@ -85,6 +85,7 @@ $routes = [
 	"/^account\/tests\/kml$/" => ["controller" => "tests_tester_kml", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/kml$/" => ["controller" => "tests_tester_kml", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/available$/" => ["controller" => "tests_available", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/available$/" => ["controller" => "tests_available", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/completed$/" => ["controller" => "tests_completed", "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/completed$/" => ["controller" => "tests_completed", "require_user_level" => User::LEVEL_TESTER],
+	"/^account\/tests\/completed\/{$id_pattern}$/" => ["controller" => "tests_completed_ajax", "params" => ["test_id"], "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/$id_pattern$/" => ["controller" => "test", "params" => ["id"], "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/$id_pattern$/" => ["controller" => "test", "params" => ["id"], "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/$id_pattern\/(selfie)$/" => ["controller" => "test_file", "params" => ["id", "name"], "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/$id_pattern\/(selfie)$/" => ["controller" => "test_file", "params" => ["id", "name"], "require_user_level" => User::LEVEL_TESTER],
 	"/^account\/tests\/$id_pattern\/files\/(pdf)$/" => ["controller" => "test_file", "params" => ["id", "name"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/tests\/$id_pattern\/files\/(pdf)$/" => ["controller" => "test_file", "params" => ["id", "name"], "require_user_level" => User::LEVEL_CUSTOMER],

+ 15 - 13
views/modal_test_invoice.php

@@ -11,7 +11,12 @@
 				</div>
 				</div>
 				<div class="modal-body">
 				<div class="modal-body">
 
 
-					<div id="travel-costs-question-container" class="collapse show text-center">
+					<div id="please-wait-container" class="alert alert-info text-center collapse show">
+						<i class="fa fa-fw fa-sync fa-pulse"></i>
+						<?=htmlspecialchars(__("Fetching invoice data..."))?>
+					</div>
+
+					<div id="travel-costs-question-container" class="collapse text-center">
 						<label><?=htmlspecialchars(__("Were travel costs pre-approved?"))?></label><br />
 						<label><?=htmlspecialchars(__("Were travel costs pre-approved?"))?></label><br />
 						<button type="button" class="btn btn-lg btn-success btn-costs-approved"><?=htmlspecialchars(__("Yes"))?></button>
 						<button type="button" class="btn btn-lg btn-success btn-costs-approved"><?=htmlspecialchars(__("Yes"))?></button>
 						<button type="button" class="btn btn-lg btn-danger btn-costs-not-approved"><?=htmlspecialchars(__("No"))?></button>
 						<button type="button" class="btn btn-lg btn-danger btn-costs-not-approved"><?=htmlspecialchars(__("No"))?></button>
@@ -19,14 +24,17 @@
 
 
 					<div id="travel-costs-approved-container" class="collapse">
 					<div id="travel-costs-approved-container" class="collapse">
 						<div class="form-group">
 						<div class="form-group">
-							<label for="travel-cost"><?=htmlspecialchars(__("Travel Costs"))?></label>
+							<label for="travel-costs"><?=htmlspecialchars(__("Travel Costs"))?></label>
 							<div class="input-group">
 							<div class="input-group">
 								<div class="input-group-prepend">
 								<div class="input-group-prepend">
 									<span class="input-group-text"><?=htmlspecialchars(__("£"))?></span>
 									<span class="input-group-text"><?=htmlspecialchars(__("£"))?></span>
 								</div>
 								</div>
-								<input class="form-control" type="number" step="0.01" min="0" name="travel_cost" id="travel-cost" value="0.00" />
+								<input class="form-control" type="number" step="0.01" min="0" name="travel_cost" id="travel-costs" value="0.00" />
 							</div>
 							</div>
-							<small class="form-text text-muted"><?=htmlspecialchars(__("You may be contacted for verification purposes."))?></small>
+							<small class="form-text text-muted">
+								<span class="text-danger"><?=htmlspecialchars(__("Travel costs are locked after downloading for the first time, please ensure it is correct!"))?></span>
+								<span class="text-muted"><?=htmlspecialchars(__("You may be contacted for verification purposes."))?></span>
+							</small>
 						</div>
 						</div>
 
 
 						<div class="form-group">
 						<div class="form-group">
@@ -40,22 +48,16 @@
 							<small class="form-text text-muted"><?=htmlspecialchars(__("JPEG photos only."))?></small>
 							<small class="form-text text-muted"><?=htmlspecialchars(__("JPEG photos only."))?></small>
 						</div>
 						</div>
 
 
-						<div class="form-group">
-							<button class="btn btn-primary" type="submit"><?=htmlspecialchars(__("Download Invoice"))?></button>
-							<small class="form-text text-danger"><?=htmlspecialchars(__("Travel costs are locked after downloading for the first time, please ensure it is correct!"))?></small>
-						</div>
 
 
 					</div>
 					</div>
 
 
-					<div id="travel-costs-not-approved-container" class="collapse">
-						<div class="alert alert-info">
-							<i class="fa fa-fw fa-info-circle"></i>
-							<?=htmlspecialchars(__("Please contact your tester manager before submitting invoice."))?>
+					<div id="download-invoice-container" class="collapse">
+						<div class="form-group">
+							<button class="btn btn-primary" type="submit"><?=htmlspecialchars(__("Download Invoice"))?></button>
 						</div>
 						</div>
 					</div>
 					</div>
 
 
 
 
-
 				</div>
 				</div>
 			</form>
 			</form>
 		</div>
 		</div>

+ 7 - 0
views/test_verify_form.php

@@ -61,6 +61,13 @@ if($tester) {
 		$tester_invoice_details["Travel Costs"] = "N/A";
 		$tester_invoice_details["Travel Costs"] = "N/A";
 	}
 	}
 
 
+	if($test->test_travel_cost_description) {
+		$tester_invoice_details["Travel Cost Description"] = htmlspecialchars($test->test_travel_cost_description);
+	}
+	else {
+		$tester_invoice_details["Travel Cost Description"] = "N/A";
+	}
+
 
 
 	if($test->hasTesterInvoicePDF()) {
 	if($test->hasTesterInvoicePDF()) {
 		$tester_invoice_details["PDF"] = "<a href=\"/account/tests/{$test->id}/invoice\" target=\"_blank\">Download Invoice</a>";
 		$tester_invoice_details["PDF"] = "<a href=\"/account/tests/{$test->id}/invoice\" target=\"_blank\">Download Invoice</a>";