Ver Fonte

Add ability to archive customers

Alan Lam há 2 anos atrás
pai
commit
a54e0e2064

+ 1 - 0
controllers/customer_add.php

@@ -34,6 +34,7 @@ if($submit) {
 		$customer->email_locations_after_test = (int) http::value("email_locations_after_test", "int");
 		$customer->year_cycle_month = http::value("year_cycle_month", "int");
 		$customer->show_in_map_overview = (int) http::value("show_in_map_overview", "int");
+		$customer->archived = (int) http::value("archived", "int");
 
 		$cc_email_addresses = HTTP::value("cc_email_addresses", "trim");
 		if($cc_email_addresses) {

+ 1 - 0
controllers/customer_edit.php

@@ -33,6 +33,7 @@ if($submit) {
 		$customer->email_locations_after_test = http::value("email_locations_after_test", "int");
 		$customer->year_cycle_month = http::value("year_cycle_month", "int");
 		$customer->show_in_map_overview = (int) http::value("show_in_map_overview", "int");
+		$customer->archived = (int) http::value("archived", "int");
 		$customer->config_json = http::value("config_json", "trim");
 
 		$cc_email_addresses = HTTP::value("cc_email_addresses", "trim");

+ 10 - 2
controllers/customers.php

@@ -1,11 +1,19 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 $page_title = "Customers";
 
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/customers", $page_title);
 
-$customers = Customer::query([], ["sort" => "name"]);
+$nvp = [];
+
+$include_archived = HTTP::value("include_archived");
+
+if(!$include_archived) {
+	$nvp["archived"] = 0;
+}
+
+$customers = Customer::query($nvp, ["sort" => "name"]);
 
 View::includeOnce("header", compact("page_title"));
 View::include("customers", compact("customers"));

+ 1 - 0
controllers/map_overview.php

@@ -10,6 +10,7 @@ Core::addBreadcrumb("/account/map-overview", $page_title);
 $today = gmdate("Y-m-d") . " 00:00:00";
 
 $customers = Customer::query([
+	"archived" => 0,
 	"show_in_map_overview" => "1"
 ], ["sort" => "name"]);
 

+ 1 - 1
controllers/projects.php

@@ -16,7 +16,7 @@ if(Core::$user->isAdmin() && $customer_id === "ALL") {
 
 	$nvp = [];
 
-	$customer = new Customer;
+	$customer = new Customer();
 	$customer->id = "ALL";
 	$customer->name = "All";
 }

+ 11 - 0
db/migrations/20241031150415_add_archived_field_to_customer_table.php

@@ -0,0 +1,11 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddArchivedFieldToCustomerTable extends AbstractMigration {
+	public function change() {
+		$this->table("customer")
+			->addColumn("archived", "boolean", ["signed" => false, "default" => 0, "after" => "show_in_map_overview"])
+			->update();
+	}
+}

+ 19 - 3
engine/customer.php

@@ -22,6 +22,7 @@ use Carbon\CarbonImmutable;
  * @property string $area_manager_field_name
  * @property int $year_cycle_month
  * @property int $show_in_map_overview
+ * @property int $archived
  * @property string $config_json
  */
 
@@ -36,6 +37,7 @@ class Customer extends Model {
 		"internal_id_field_name", "region_field_name", "area_manager_field_name",
 		"year_cycle_month",
 		"show_in_map_overview",
+		"archived",
 		"config_json",
 	];
 
@@ -49,9 +51,19 @@ class Customer extends Model {
 
 		$user = Core::$user;
 
-		$customers = $user->getCustomers();
-		if(count($customers) == 0) throw new Exception("At least one customer must exist before this page can be accessed.");
-		else if(count($customers) == 1) {
+		$nvp = [];
+
+		$include_archived = HTTP::value("include_archived");
+		if(!$include_archived) {
+			$nvp["archived"] = 0;
+		}
+
+		$customers = $user->getCustomers($nvp);
+		if(count($customers) === 0) {
+			throw new RuntimeException("At least one customer must exist before this page can be accessed.");
+		}
+
+		if(count($customers) === 1) {
 			$customer = array_shift($customers);
 		}
 		else {
@@ -325,4 +337,8 @@ class Customer extends Model {
 		return !empty($config->billingEnabled);
 	}
 
+	public function isArchived(): bool {
+		return (bool) $this->archived;
+	}
+
 }

+ 7 - 5
engine/user.php

@@ -606,17 +606,19 @@ class User extends Model {
 	 * @return Customer[]
 	 * @throws Exception
 	 */
-	public function getCustomers() {
-
+	public function getCustomers(array $nvp = []) {
 		if($this->canAccessCustomers()) {
-			return Customer::query([], ["sort" => "name"]);
+			return Customer::query($nvp, ["sort" => "name"]);
 		}
 
 		if($this->isCustomer()) {
 			$links = UserCustomer::query(["user_id" => $this->id]);
-			return Customer::query([
+
+			$nvp = array_merge($nvp, [
 				"id" => array_column($links, "customer_id")
-			], ["sort" => "name"]);
+			]);
+
+			return Customer::query($nvp, ["sort" => "name"]);
 		}
 
 		return [];

+ 19 - 3
views/customer.php

@@ -1,8 +1,24 @@
-
+<?php
+/**
+ * @var Customer $customer
+ * @var object[] $icons
+ */
+?>
 <div class="container mt-3 py-4">
 	<div class="row">
 		<div class="col">
-			<h2><?=htmlspecialchars($customer->name)?></h2>
+			<h2>
+				<?=htmlspecialchars($customer->name)?>
+				<?php
+				if($customer->isArchived()) {
+					?>
+					<span class="badge badge-secondary">
+						<?=htmlspecialchars(__("Archived"))?>
+					</span>
+					<?php
+				}
+				?>
+			</h2>
 		</div>
 		<div class="col-auto">
 			<a class="btn btn-outline-primary" href="/account/customers/<?=$customer->id?>/edit">Edit</a>
@@ -39,4 +55,4 @@
 	?>
 	</div>
 
-</div>
+</div>

+ 4 - 0
views/customer_form.php

@@ -119,6 +119,10 @@
 
 		<div class="row my-3">
 			<div class="col-sm offset-sm-4">
+				<div class="form-check">
+					<input class="form-check-input" type="checkbox" name="archived" id="archived" value="1" <?=Form::checked($customer->archived)?> />
+					<label for="archived">Archived</label>
+				</div>
 				<div class="form-check">
 					<input class="form-check-input" type="checkbox" name="show_in_map_overview" id="show-in-map-overview" value="1" <?=Form::checked($customer->show_in_map_overview)?> />
 					<label for="show-in-map-overview">Show in Map Overview</label>

+ 6 - 1
views/customers.php

@@ -1,3 +1,8 @@
+<?php
+/**
+ * @var Customer[] $customers
+ */
+?>
 <div class="container mt-3 py-4">
 	<div class="row">
 		<div class="col">
@@ -33,4 +38,4 @@
 	}
 	?>
 
-</div>
+</div>