Przeglądaj źródła

Add custom year cycle support for customers

Alan Lam 7 lat temu
rodzic
commit
121099975a

+ 2 - 2
controllers/customer_add.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 $page_title = "Add Customer";
 
@@ -29,6 +29,7 @@ if($submit) {
 		$customer->phone = http::value("phone", "trim");
 		$customer->email_after_test = http::value("email_after_test", "int");
 		$customer->email_locations_after_test = http::value("email_locations_after_test", "int");
+		$customer->year_cycle_month = http::value("year_cycle_month", "int");
 
 		if(empty($customer->name)) $form_exception->addError("Name is required.");
 
@@ -51,4 +52,3 @@ View::includeOnce("header", compact("page_title"));
 View::include("customer_form", compact("page_title", "customer"));
 View::includeOnce("footer");
 
-?>

+ 2 - 2
controllers/customer_edit.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 $customer = Customer::get($id);
 if(!$customer) throw new Exception("Customer not found.");
@@ -29,6 +29,7 @@ if($submit) {
 		$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");
+		$customer->year_cycle_month = http::value("year_cycle_month", "int");
 
 		if(empty($customer->name)) $form_exception->addError("Name is required.");
 
@@ -51,4 +52,3 @@ View::includeOnce("header", compact("page_title"));
 View::include("customer_form", compact("page_title", "customer"));
 View::includeOnce("footer");
 
-?>

+ 1 - 4
controllers/map_overview.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 $page_title = "Map Overview";
 
@@ -20,9 +20,6 @@ Customer::load(array_column($projects, "customer_id"));
 
 $customers = Customer::query([], ["sort" => "name"]);
 
-
 View::includeOnce("header", compact("page_title"));
 View::include("map_overview", compact("customers", "projects"));
 View::includeOnce("footer");
-
-?>

+ 25 - 15
controllers/map_overview_ajax.php

@@ -1,4 +1,6 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+$date_format = "/^2[0-9]{3}\-[0-1][0-9]\-[0-3][0-9]$/";
 
 JSON::header();
 
@@ -14,32 +16,39 @@ if($submit) {
 			// Gets all locations for a customer
 			case "get-customer-locations": {
 
+				// Input
 				$customer_id = http::value("customer_id", "int");
+
+				$min_date = http::value("min_date");
+				$max_date = http::value("max_date");
+
+				// Get customer
 				$customer = Customer::get($customer_id);
 				if(!$customer) throw new Exception("Customer not found.");
 
+				// Get locations
 				$locations = $customer->getLocations();
 
-				$min_date = http::value("min_date");
-				$max_date = http::value("max_date");
-
+				// Get tests
 				$nvp = [
 					"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
 					"location_id" => array_column($locations, "id")
 				];
 
-				$date_format = "/^2[0-9]{3}\-[0-1][0-9]\-[0-3][0-9]$/";
+				if($min_date && $max_date) {
+					if(preg_match($date_format, $min_date) && preg_match($date_format, $max_date)) {
+						$min_dts = "{$min_date}";
+						$max_dts = "{$max_date}";
 
-				if($min_date && preg_match($date_format, $min_date)) {
-					$min_dts = "{$min_date} 00:00:00";
-					$nvp["dts_start"] = (new Constraint("MORE_THAN_OR_EQUAL"))->setValues($min_dts);
+						$nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues($min_dts, $max_dts);
+					}
 				}
-
-				if($max_date && preg_match($date_format, $max_date)) {
-					$max_dts = "{$max_date} 23:59:59";
-					$nvp["dts_start"] = (new Constraint("LESS_THAN_OR_EQUAL"))->setValues($max_dts);
+				else {
+					list($min_dts, $max_dts) = $customer->getYearCycleDates();
+					$nvp["dts_start"] = (new Constraint("BETWEEN"))->setValues($min_dts, $max_dts);
 				}
 
+
 				$tests = Test::query($nvp);
 
 				$location_test_array = [];
@@ -49,6 +58,7 @@ if($submit) {
 						$location_test_array[$test->location_id] = $test;
 					}
 					else {
+						/** @var Test $existing */
 						$existing = $location_test_array[$test->location_id];
 
 						$unix_start_existing = is_null($existing->dts_start) ? 0 : strtotime($existing->dts_start . " UTC");
@@ -71,6 +81,7 @@ if($submit) {
 
 					$customer_slug = str_replace("-", "_", Util::slug($customer->name));
 
+					/** @var User $tester */
 					$tester = null;
 					$formatted_start_dts = null;
 					$formatted_due_dts = null;
@@ -110,7 +121,6 @@ if($submit) {
 						}
 					}
 
-
 					if($icon_file && file_exists("public/img/markers/$icon_file")) {
 						$icon = $icon_file;
 					}
@@ -148,7 +158,8 @@ if($submit) {
 				}
 
 				JSON::success([
-					"locations" => $output
+					"locations" => $output,
+					"testPeriod" => sprintf("%s to %s", $min_dts, $max_dts)
 				]);
 
 				break;
@@ -252,4 +263,3 @@ if($submit) {
 
 JSON::fail("Invalid request.");
 
-?>

+ 13 - 0
db/migrations/20191110184617_add_year_cycle_month_field.php

@@ -0,0 +1,13 @@
+<?php
+
+use Phinx\Db\Adapter\MysqlAdapter;
+use Phinx\Migration\AbstractMigration;
+
+class AddYearCycleMonthField extends AbstractMigration {
+	public function change() {
+		$table = $this->table("customer");
+		$table
+			->addColumn("year_cycle_month", "integer", ["limit" => MysqlAdapter::INT_TINY, "signed" => false, "null" => true])
+			->update();
+	}
+}

+ 23 - 0
engine/customer.php

@@ -17,6 +17,7 @@
  * @property string $internal_id_field_name
  * @property string $region_field_name
  * @property string $area_manager_field_name
+ * @property int $year_cycle_month
  */
 
 class Customer extends Model {
@@ -28,6 +29,7 @@ class Customer extends Model {
 		"invoice_contact_name",
 		"email_after_test", "email_locations_after_test",
 		"internal_id_field_name", "region_field_name", "area_manager_field_name",
+		"year_cycle_month",
 	];
 
 	// Static methods
@@ -103,4 +105,25 @@ class Customer extends Model {
 		return $url;
 	}
 
+	public function getYearCycleDates(): array {
+
+		$month = $this->year_cycle_month;
+		if(!$month) $month = 1;
+
+		$current_year = date("Y");
+		$current_month = date("n");
+
+		if($current_month >= $month) {
+			$min_dts = sprintf("%d-%02d-01", $current_year, $month);
+			$max_dts = sprintf("%d-%02d-01", $current_year + 1, $month);
+		}
+		else {
+			$min_dts = sprintf("%d-%02d-01", $current_year - 1, $month);
+			$max_dts = sprintf("%d-%02d-01", $current_year, $month);
+		}
+
+		return [$min_dts, $max_dts];
+
+	}
+
 }

+ 3 - 0
public/js/app-map-overview.js

@@ -22,6 +22,7 @@ $(function() {
 		if(selected) {
 			$(this).data("selected", false);
 			$(this).find(".badge").removeClass("badge-success").text("");
+			$(this).find("small").text("");
 		}
 		else {
 			loadCustomerLocations(customerId);
@@ -97,6 +98,7 @@ function loadCustomerLocations(customerId) {
 	/**
 	 * @property {Object} response
 	 * @property {Array} response.locations
+	 * @property {String} response.testPeriod
 	 * @property {Boolean} response.success
 	 */
 
@@ -121,6 +123,7 @@ function loadCustomerLocations(customerId) {
 
 		$customerItem.data("selected", true);
 		$customerItem.find(".badge").addClass("badge-danger").text(response.locations.length);
+		$customerItem.find("small").text(response.testPeriod);
 
 	});
 

+ 33 - 11
views/customer_form.php

@@ -2,43 +2,43 @@
 	<h2><?=htmlspecialchars($page_title)?></h2>
 
 	<form method="post">
-		<div class="row mb-1">
+		<div class="row form-group">
 			<label class="col-sm-4 col-form-label" for="name">Name</label>
 			<div class="col-sm">
 				<input class="form-control" type="text" name="name" id="name" placeholder="Enter name" value="<?=htmlspecialchars($customer->name)?>" required />
 			</div>
 		</div>
-		<div class="row mb-1">
+		<div class="row form-group">
 			<label class="col-sm-4 col-form-label" for="address-1">Address Line 1</label>
 			<div class="col-sm">
 				<input class="form-control" type="text" name="address_1" id="address-1" placeholder="Enter address line 1" value="<?=htmlspecialchars($customer->address_1)?>" />
 			</div>
 		</div>
-		<div class="row mb-1">
+		<div class="row form-group">
 			<label class="col-sm-4 col-form-label" for="address-2">Address Line 2</label>
 			<div class="col-sm">
 				<input class="form-control" type="text" name="address_2" id="address-2" placeholder="Enter address line 2" value="<?=htmlspecialchars($customer->address_2)?>" />
 			</div>
 		</div>
-		<div class="row mb-1">
+		<div class="row form-group">
 			<label class="col-sm-4 col-form-label" for="city">City</label>
 			<div class="col-sm">
 				<input class="form-control" type="text" name="city" id="city" placeholder="Enter city" value="<?=htmlspecialchars($customer->city)?>" />
 			</div>
 		</div>
-		<div class="row mb-1">
+		<div class="row form-group">
 			<label class="col-sm-4 col-form-label" for="county">County</label>
 			<div class="col-sm">
 				<input class="form-control" type="text" name="county" id="county" placeholder="Enter county" value="<?=htmlspecialchars($customer->county)?>" />
 			</div>
 		</div>
-		<div class="row mb-1">
+		<div class="row form-group">
 			<label class="col-sm-4 col-form-label" for="postal-code">Postal Code</label>
 			<div class="col-sm">
 				<input class="form-control" type="text" name="postal_code" id="postal-code" placeholder="Enter postal code" value="<?=htmlspecialchars($customer->postal_code)?>" />
 			</div>
 		</div>
-		<div class="row mb-1">
+		<div class="row form-group">
 			<label class="col-sm-4 col-form-label" for="country">Country</label>
 			<div class="col-sm">
 				<select class="form-control" name="country" id="country">
@@ -46,24 +46,46 @@
 				</select>
 			</div>
 		</div>
-		<div class="row mb-1">
+		<div class="row form-group">
 			<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">
+		<div class="row form-group">
 			<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">
+		<div class="row form-group">
 			<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 form-group">
+            <label  class="col-sm-4 col-form-label" for="year-cycle-month">
+                Year Cycle Month
+            </label>
+            <div class="col-sm">
+                <select class="form-control" name="year_cycle_month" id="year-cycle-month">
+                    <option value="">Select Month</option>
+                    <?php
+                    foreach(range(1, 12) as $month) {
+                        $display_month = sprintf("%02d", $month);
+                        $selected = ($customer->year_cycle_month == $month) ? "selected" : "";
+                        ?>
+                        <option value="<?=$month?>" <?=$selected?>><?=$display_month?></option>
+                        <?php
+                    }
+                    ?>
+                </select>
+            </div>
+        </div>
+
+
 		<div class="row my-3">
 			<div class="col-sm offset-sm-4">
 				<div class="form-check">
@@ -78,4 +100,4 @@
 			</div>
 		</div>
 	</form>
-</div>
+</div>

+ 9 - 6
views/map_overview.php

@@ -17,13 +17,13 @@
 		<div class="col-3">
 			<div class="form-group">
 				<label for="min-date">Min Test Date</label>
-				<input type="date" class="form-control" id="min-date" placeholder="Type date" value="<?=date("Y")?>-01-01" />
+				<input type="date" class="form-control" id="min-date" placeholder="Type date" />
 			</div>
 		</div>
 		<div class="col-3">
 			<div class="form-group">
-				<label for="min-date">Max Test Date</label>
-				<input type="date" class="form-control" id="min-date" placeholder="Type date" value="<?=date("Y")?>-12-31" />
+				<label for="max-date">Max Test Date</label>
+				<input type="date" class="form-control" id="max-date" placeholder="Type date" />
 			</div>
 		</div>
 	</div>
@@ -50,8 +50,11 @@
 			foreach($customers as $customer) {
 				?>
 				<a class="list-group-item list-group-item-action d-flex justify-content-between" data-customer-id="<?=$customer->id?>" data-selected="false">
-					<div><?=htmlspecialchars($customer->name)?></div>
-					<span class="badge badge-pill badge-lg"></span>
+					<div>
+                        <?=htmlspecialchars($customer->name)?><br />
+                        <small></small>
+                    </div>
+					<span class="badge badge-pill badge-lg align-self-center"></span>
 				</a>
 				<?php
 			}
@@ -97,4 +100,4 @@
 		</div>
 	</div>
 
-</div>
+</div>