浏览代码

Add batch action to mark multiple leads as completed

Alan Lam 3 年之前
父节点
当前提交
b856e27baa
共有 2 个文件被更改,包括 119 次插入 和 64 次删除
  1. 25 0
      controllers/sales.php
  2. 94 64
      views/sales_table.php

+ 25 - 0
controllers/sales.php

@@ -4,6 +4,7 @@ namespace App;
 
 
 use App\Constraint\NotEqualsConstraint;
 use App\Constraint\NotEqualsConstraint;
 use App\Enums\SaleStatuses;
 use App\Enums\SaleStatuses;
+use RuntimeException;
 
 
 $core = Core::getInstance();
 $core = Core::getInstance();
 
 
@@ -16,6 +17,30 @@ $nvp = ["dts_deleted" => null];
 
 
 $request = Request::fromRequest();
 $request = Request::fromRequest();
 
 
+// Batch actions
+$batch_action = $request->string("batch_action");
+$sale_ids = $request->int("sale_ids");
+
+if($batch_action && $sale_ids && $user->isAdmin()) {
+	$sales = Sale::query(["id" => $sale_ids]);
+
+	$user->requireValidCsrfToken();
+
+	switch($batch_action) {
+		case "mark-completed":
+			foreach($sales as $sale) {
+				$sale->status = SaleStatuses::COMPLETED;
+				$sale->commit();
+			}
+			Session::addAlert("success", __("Leads marked as completed successfully."));
+			Http::redirect($_SERVER["REQUEST_URI"]);
+			break;
+		default:
+			throw new RuntimeException(__("Invalid batch action."));
+	}
+}
+
+// Filters
 $customer_id = $request->int("customer_id");
 $customer_id = $request->int("customer_id");
 if($customer_id) {
 if($customer_id) {
 	$customer = Customer::get($customer_id);
 	$customer = Customer::get($customer_id);

+ 94 - 64
views/sales_table.php

@@ -9,71 +9,101 @@ $core = Core::getInstance();
 $user = $core->getUser();
 $user = $core->getUser();
 
 
 ?>
 ?>
-<table class="table table-bordered table-hover mb-3">
-	<thead>
-	<tr>
-		<th><?=htmlspecialchars(__("Ref."))?></th>
-		<th><?=htmlspecialchars(__("Date"))?></th>
-		<th><?=htmlspecialchars(__("Location"))?></th>
-		<th><?=htmlspecialchars(__("Status"))?></th>
-		<th><?=htmlspecialchars(__("Items"))?></th>
-		<th><?=htmlspecialchars(__("Value"))?></th>
-		<th><?=htmlspecialchars(__("Predicted Sold Date"))?></th>
-		<th><?=htmlspecialchars(__("Last Updated"))?></th>
-		<th><?=htmlspecialchars(__("Actions"))?></th>
-	</tr>
-	</thead>
-	<tbody>
+<form method="post">
 	<?php
 	<?php
-	foreach($sales as $sale) {
-		$location = $sale->getLocation();
-		?>
+	$user->insertCsrfToken();
+	?>
+
+	<table class="table table-bordered table-hover mb-3">
+		<thead>
 		<tr>
 		<tr>
-			<td class="text-end">
-				<a href="<?=htmlspecialchars($sale->getUrl())?>"><?=htmlspecialchars($sale->getReference())?></a>
-			</td>
-			<td>
-				<?=htmlspecialchars($user->formatDate($sale->getDateTimeCreated()))?>
-			</td>
-			<td>
-				<a href="<?=htmlspecialchars($location->getUrl())?>">
-					<?=htmlspecialchars($location->getDisplayName())?>
-				</a>
-			</td>
-			<td>
-				<?=htmlspecialchars($sale->getDisplayStatus())?>
-			</td>
-			<td>
-				<?php
-				foreach($sale->getItems() as $item) {
-					?>
-					<?=htmlspecialchars($item->getProduct()->name)?> &times; <?=$item->quantity?><br />
-					<?php
-				}
-				?>
-			</td>
-			<td class="text-end">
-				<?=htmlspecialchars($sale->getDisplayAmount())?>
-			</td>
-			<td>
-				<?=htmlspecialchars($sale->getDisplayPredictedSoldDate())?>
-			</td>
-			<td>
-				<?php
-				$dt_updated = $sale->getDateTimeUpdated();
-				if($dt_updated) {
-					echo $dt_updated->diffForHumans();
-				}
-				?>
-			</td>
-			<td class="p-1">
-				<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($sale->getUrl())?>">
-					<?=htmlspecialchars(__("View"))?>
-				</a>
-			</td>
+			<th></th>
+			<th><?=htmlspecialchars(__("Ref."))?></th>
+			<th><?=htmlspecialchars(__("Date"))?></th>
+			<th><?=htmlspecialchars(__("Location"))?></th>
+			<th><?=htmlspecialchars(__("Status"))?></th>
+			<th><?=htmlspecialchars(__("Items"))?></th>
+			<th><?=htmlspecialchars(__("Value"))?></th>
+			<th><?=htmlspecialchars(__("Predicted Sold Date"))?></th>
+			<th><?=htmlspecialchars(__("Last Updated"))?></th>
+			<th><?=htmlspecialchars(__("Actions"))?></th>
 		</tr>
 		</tr>
+		</thead>
+		<tbody>
 		<?php
 		<?php
-	}
-	?>
-	</tbody>
-</table>
+		foreach($sales as $sale) {
+			$location = $sale->getLocation();
+			?>
+			<tr>
+				<td>
+					<input type="checkbox" name="sale_ids[]" value="<?=$sale->id?>" />
+				</td>
+				<td class="text-end">
+					<a href="<?=htmlspecialchars($sale->getUrl())?>"><?=htmlspecialchars($sale->getReference())?></a>
+				</td>
+				<td>
+					<?=htmlspecialchars($user->formatDate($sale->getDateTimeCreated()))?>
+				</td>
+				<td>
+					<a href="<?=htmlspecialchars($location->getUrl())?>">
+						<?=htmlspecialchars($location->getDisplayName())?>
+					</a>
+				</td>
+				<td>
+					<?=htmlspecialchars($sale->getDisplayStatus())?>
+				</td>
+				<td>
+					<?php
+					foreach($sale->getItems() as $item) {
+						?>
+						<?=htmlspecialchars($item->getProduct()->name)?> &times; <?=$item->quantity?><br />
+						<?php
+					}
+					?>
+				</td>
+				<td class="text-end">
+					<?=htmlspecialchars($sale->getDisplayAmount())?>
+				</td>
+				<td>
+					<?=htmlspecialchars($sale->getDisplayPredictedSoldDate())?>
+				</td>
+				<td>
+					<?php
+					$dt_updated = $sale->getDateTimeUpdated();
+					if($dt_updated) {
+						echo $dt_updated->diffForHumans();
+					}
+					?>
+				</td>
+				<td class="p-1">
+					<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($sale->getUrl())?>">
+						<?=htmlspecialchars(__("View"))?>
+					</a>
+				</td>
+			</tr>
+			<?php
+		}
+		?>
+		</tbody>
+	</table>
+
+	<div class="card mb-3">
+		<div class="card-header">
+			<?=htmlspecialchars(__("Batch Actions"))?>
+		</div>
+		<div class="card-body">
+			<label class="sr-only" for="batch-action"><?=htmlspecialchars(__("Batch Action"))?></label>
+			<div class="input-group">
+				<select class="form-control" name="batch_action" id="batch-action" required>
+					<option value=""><?=htmlspecialchars(__("Select Batch Action"))?></option>
+					<option value="mark-completed"><?=htmlspecialchars(__("Mark As Completed"))?></option>
+				</select>
+				<button type="submit" class="btn btn-primary">
+					<?=htmlspecialchars(__("Submit"))?>
+				</button>
+			</div>
+		</div>
+	</div>
+
+</form>
+