Sfoglia il codice sorgente

Add tables to customer view

Alan Lam 3 anni fa
parent
commit
c99c333c17

+ 17 - 1
controllers/customer.php

@@ -20,6 +20,22 @@ $core->addBreadcrumb($customer->getUrl(), $customer->name);
 $user = $core->getUser();
 $user->requireAdmin();
 
+$core->addScript("/js/ticket-modal.js");
+
+$location_ids = array_column($customer->getLocations(), "id");
+
+$sales = Sale::query([
+	"location_id" => $location_ids,
+	"dts_deleted" => null
+]);
+
+$tickets = Ticket::query([
+	"location_id" => $location_ids,
+	"dts_deleted" => null
+]);
+
 View::include("header");
-View::include("customer", compact("customer"));
+View::include("customer", compact("customer", "sales", "tickets"));
+View::include("modals/new_sale", compact("customer"));
+View::include("modals/ticket", compact("customer"));
 View::include("footer");

+ 16 - 11
views/customer.php

@@ -4,15 +4,15 @@ namespace App;
 
 /**
  * @var Customer $customer
+ * @var Sale[] $sales
+ * @var Ticket[] $tickets
  */
 
 $locations = $customer->getLocations();
-$sales = [];
-$tickets = [];
 
 ?>
 
-<div class="row mb-2">
+<div class="row mb-3">
 	<div class="col">
 		<h3><?=htmlspecialchars($customer->name)?></h3>
 	</div>
@@ -24,7 +24,7 @@ $tickets = [];
 	</div>
 </div>
 
-<div class="row mb-2">
+<div class="row mb-3">
 	<div class="col">
 		<h4><?=htmlspecialchars(__("Locations"))?></h4>
 	</div>
@@ -50,15 +50,15 @@ else {
 }
 ?>
 
-<div class="row mb-2">
+<div class="row mb-3">
 	<div class="col">
 		<h4><?=htmlspecialchars(__("Sales"))?></h4>
 	</div>
 	<div class="col-auto">
-		<a class="btn btn-success" href="<?=htmlspecialchars($customer->getUrl())?>/sales/add">
+		<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#new-sale-modal">
 			<i class="fa fa-fw fa-plus"></i>
 			<?=htmlspecialchars(__("Add Sale"))?>
-		</a>
+		</button>
 	</div>
 </div>
 
@@ -71,17 +71,20 @@ if(empty($sales)) {
 	</div>
 	<?php
 }
+else {
+	View::include("sales_table", compact("sales"));
+}
 ?>
 
-<div class="row mb-2">
+<div class="row mb-3">
 	<div class="col">
 		<h4><?=htmlspecialchars(__("Tickets"))?></h4>
 	</div>
 	<div class="col-auto">
-		<a class="btn btn-success" href="<?=htmlspecialchars($customer->getUrl())?>/tickets/add">
+		<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#ticket-modal">
 			<i class="fa fa-fw fa-plus"></i>
 			<?=htmlspecialchars(__("Add Ticket"))?>
-		</a>
+		</button>
 	</div>
 </div>
 
@@ -94,4 +97,6 @@ if(empty($tickets)) {
 	</div>
 	<?php
 }
-?>
+else {
+	View::include("tickets_table", compact("tickets"));
+}

+ 7 - 1
views/modals/new_sale.php

@@ -1,6 +1,12 @@
 <?php
 namespace App;
 
+/**
+ * @var Customer $customer
+ */
+
+$customers = isset($customer) ? [$customer] : Customer::getAll();
+
 ?>
 <div class="modal fade" role="dialog" tabindex="-1" data-backdrop="static" id="new-sale-modal">
 	<div class="modal-dialog modal-lg">
@@ -30,7 +36,7 @@ namespace App;
 					<label class="form-label" for="location-id"><?= htmlspecialchars(__("Location")) ?></label>
 					<select class="form-control" name="location_id" id="location-id">
 						<?php
-						foreach(Customer::getAll() as $customer) {
+						foreach($customers as $customer) {
 							?>
 							<optgroup label="<?=htmlspecialchars($customer->name)?>">
 								<?php

+ 9 - 4
views/modals/ticket.php

@@ -1,13 +1,18 @@
 <?php
 namespace App;
+
+/**
+ * @var Customer $customer
+ */
+
+$default_customer_id = $customer->id ?? null;
+
+use App\Helpers\HtmlHelper;
 ?>
 <div class="modal fade" role="dialog" tabindex="-1" data-bs-backdrop="static" id="ticket-modal">
 	<div class="modal-dialog modal-lg">
 		<form class="modal-content" method="post">
 			<?php
-
-			use App\Helpers\HtmlHelper;
-
 			View::include("modals/header", ["title" => __("New Ticket")]);
 			?>
 			<div class="modal-body">
@@ -42,7 +47,7 @@ namespace App;
 							<select class="form-control" name="customer_id" id="ticket-customer-id">
 								<option value=""><?=htmlspecialchars(__("Select Customer"))?></option>
 								<?php
-								HtmlHelper::renderOptions(Customer::getOptions());
+								HtmlHelper::renderOptions(Customer::getOptions(), $default_customer_id);
 								?>
 							</select>
 						</div>

+ 1 - 39
views/sales.php

@@ -96,44 +96,6 @@ if(empty($sales)) {
 	<?php
 }
 else {
-	?>
-	<table class="table table-bordered table-hover mb-3">
-		<thead>
-		<tr>
-			<th><?=htmlspecialchars(__("Number"))?></th>
-			<th><?=htmlspecialchars(__("Location"))?></th>
-			<th><?=htmlspecialchars(__("Status"))?></th>
-			<th><?=htmlspecialchars(__("Actions"))?></th>
-		</tr>
-		</thead>
-		<tbody>
-		<?php
-		foreach($sales as $sale) {
-			$location = $sale->getLocation();
-			?>
-			<tr>
-				<td class="text-end">
-					<a href="<?=htmlspecialchars($sale->getUrl())?>"><?=htmlspecialchars($sale->getNumber())?></a>
-				</td>
-				<td>
-					<a href="<?=htmlspecialchars($location->getUrl())?>">
-						<?=htmlspecialchars($location->getDisplayName())?>
-					</a>
-				</td>
-				<td>
-					<?=htmlspecialchars($sale->getDisplayStatus())?>
-				</td>
-				<td class="p-1">
-					<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($sale->getUrl())?>">
-						<?=htmlspecialchars(__("View"))?>
-					</a>
-				</td>
-			</tr>
-			<?php
-		}
-		?>
-		</tbody>
-	</table>
-	<?php
+	View::include("sales_table", compact("sales"));
 	$pagination->render();
 }

+ 44 - 0
views/sales_table.php

@@ -0,0 +1,44 @@
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+namespace App;
+
+/**
+ * @var Sale[] $sales
+ */
+?>
+<table class="table table-bordered table-hover mb-3">
+	<thead>
+	<tr>
+		<th><?=htmlspecialchars(__("Number"))?></th>
+		<th><?=htmlspecialchars(__("Location"))?></th>
+		<th><?=htmlspecialchars(__("Status"))?></th>
+		<th><?=htmlspecialchars(__("Actions"))?></th>
+	</tr>
+	</thead>
+	<tbody>
+	<?php
+	foreach($sales as $sale) {
+		$location = $sale->getLocation();
+		?>
+		<tr>
+			<td class="text-end">
+				<a href="<?=htmlspecialchars($sale->getUrl())?>"><?=htmlspecialchars($sale->getNumber())?></a>
+			</td>
+			<td>
+				<a href="<?=htmlspecialchars($location->getUrl())?>">
+					<?=htmlspecialchars($location->getDisplayName())?>
+				</a>
+			</td>
+			<td>
+				<?=htmlspecialchars($sale->getDisplayStatus())?>
+			</td>
+			<td class="p-1">
+				<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($sale->getUrl())?>">
+					<?=htmlspecialchars(__("View"))?>
+				</a>
+			</td>
+		</tr>
+		<?php
+	}
+	?>
+	</tbody>
+</table>

+ 4 - 51
views/tickets.php

@@ -108,58 +108,11 @@ if(!$tickets) {
 else {
 	?>
 	<form method="post">
-		<table class="table table-bordered table-hover mb-3">
-			<colgroup>
-				<col style="width: 1%;" />
-				<col style="width: 1%;" />
-				<col style="width: 30%;" />
-				<col style="width: 15%;" />
-				<col style="width: 15%;" />
-				<col style="width: 15%;" />
-				<col style="width: 15%;" />
-			</colgroup>
-			<thead>
-			<tr>
-				<th></th>
-				<th><?=htmlspecialchars("#")?></th>
-				<th><?=htmlspecialchars(__("Subject / Location"))?></th>
-				<th><?=htmlspecialchars(__("Status"))?></th>
-				<th><?=htmlspecialchars(__("Assigned To"))?></th>
-				<th><?=htmlspecialchars(__("Created"))?></th>
-				<th><?=htmlspecialchars(__("Updated"))?></th>
-			</tr>
-			</thead>
-			<tbody>
-			<?php
-			foreach($tickets as $ticket) {
-				$location = $ticket->getLocation();
-				?>
-				<tr>
-					<td>
-						<div class="form-check">
-							<label for="ticket-checkbox-<?=$ticket->id?>" class="form-check-label"></label>
-							<input class="form-check-input" type="checkbox" id="ticket-checkbox-<?=$ticket->id?>" name="ticket_ids[]" value="<?=$ticket->id?>" />
-						</div>
-					</td>
-					<td class="text-end"><?=$ticket->id?></td>
-					<td>
-						<strong>
-							<a href="/tickets/<?=$ticket->id?>"><?=htmlspecialchars($ticket->subject)?></a>
-						</strong><br />
-						<?=htmlspecialchars($location->name)?>
-					</td>
-					<td><?=htmlspecialchars($ticket->getDisplayStatus())?></td>
-					<td><?=htmlspecialchars($ticket->getDisplayAssignedTo())?></td>
-					<td><?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeCreated()))?></td>
-					<td><?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeUpdated()))?></td>
-				</tr>
-				<?php
-			}
-			?>
-			</tbody>
-		</table>
-
 		<?php
+		View::include("tickets_table", [
+			"tickets" => $tickets,
+			"editable" => true
+		]);
 		$pagination->render();
 		?>
 

+ 82 - 0
views/tickets_table.php

@@ -0,0 +1,82 @@
+<?php
+namespace App;
+
+/**
+ * @var Ticket[] $tickets
+ * @var bool $editable
+ */
+
+$core = Core::getInstance();
+$user = $core->getUser();
+
+$editable = $editable ?? false;
+
+?>
+<table class="table table-bordered table-hover mb-3">
+	<colgroup>
+		<?php
+		if($editable) {
+			?>
+			<col style="width: 1%;" />
+			<?php
+		}
+		?>
+		<col style="width: 1%;" />
+		<col style="width: 30%;" />
+		<col style="width: 15%;" />
+		<col style="width: 15%;" />
+		<col style="width: 15%;" />
+		<col style="width: 15%;" />
+	</colgroup>
+	<thead>
+	<tr>
+		<?php
+		if($editable) {
+			?>
+			<th></th>
+			<?php
+		}
+		?>
+		<th><?=htmlspecialchars("#")?></th>
+		<th><?=htmlspecialchars(__("Subject / Location"))?></th>
+		<th><?=htmlspecialchars(__("Status"))?></th>
+		<th><?=htmlspecialchars(__("Assigned To"))?></th>
+		<th><?=htmlspecialchars(__("Created"))?></th>
+		<th><?=htmlspecialchars(__("Updated"))?></th>
+	</tr>
+	</thead>
+	<tbody>
+	<?php
+	foreach($tickets as $ticket) {
+		$location = $ticket->getLocation();
+		?>
+		<tr>
+			<?php
+			if($editable) {
+				?>
+				<td>
+					<div class="form-check">
+						<label for="ticket-checkbox-<?=$ticket->id?>" class="form-check-label"></label>
+						<input class="form-check-input" type="checkbox" id="ticket-checkbox-<?=$ticket->id?>" name="ticket_ids[]" value="<?=$ticket->id?>" />
+					</div>
+				</td>
+				<?php
+			}
+			?>
+			<td class="text-end"><?=$ticket->id?></td>
+			<td>
+				<strong>
+					<a href="/tickets/<?=$ticket->id?>"><?=htmlspecialchars($ticket->subject)?></a>
+				</strong><br />
+				<?=htmlspecialchars($location->name)?>
+			</td>
+			<td><?=htmlspecialchars($ticket->getDisplayStatus())?></td>
+			<td><?=htmlspecialchars($ticket->getDisplayAssignedTo())?></td>
+			<td><?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeCreated()))?></td>
+			<td><?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeUpdated()))?></td>
+		</tr>
+		<?php
+	}
+	?>
+	</tbody>
+</table>