Przeglądaj źródła

Add ability to archive products

Alan Lam 1 rok temu
rodzic
commit
b0d28429d8

+ 3 - 2
controllers/product_form.php

@@ -58,6 +58,8 @@ if($submit) {
 			throw new RuntimeException(__("Type is required."));
 		}
 
+		$product->archived = (int) (bool) $request->int("archived");
+
 		$product->commit();
 
 		$delete_brochure = $request->int("delete_brochure");
@@ -77,9 +79,8 @@ if($submit) {
 			}
 		}
 
-
 		Session::addAlert("success", __("Product saved successfully."));
-		Http::redirect($product->getUrl());
+		Http::redirect($product->getUpdateUrl());
 	}
 	catch(RuntimeException $e) {
 		Session::addAlert("danger", $e->getMessage());

+ 11 - 1
controllers/products.php

@@ -36,6 +36,11 @@ if($type) {
 	$nvp["type"] = $type;
 }
 
+$archived = $request->int("archived");
+if($archived === 0) {
+	$nvp["archived"] = $archived;
+}
+
 $sort_options = [
 	"id" => __("ID"),
 	"name" => __("Name"),
@@ -64,6 +69,11 @@ switch($sort) {
 		break;
 }
 
+$archived_options = [
+	"0" => __("Hide"),
+	"1" => __("Show"),
+];
+
 $count = Product::count($nvp);
 
 $pagination = new Pagination($count);
@@ -77,5 +87,5 @@ $products = Product::query($nvp, [
 ]);
 
 View::include("header");
-View::include("products", compact("products", "pagination", "sort_options", "product_id", "supplier_id", "name", "sku", "type", "sort"));
+View::include("products", compact("products", "pagination", "archived_options", "sort_options", "product_id", "supplier_id", "name", "sku", "type", "archived", "sort"));
 View::include("footer");

+ 12 - 0
db/migrations/20250825104903_add_archived_field_to_product_table.php

@@ -0,0 +1,12 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddArchivedFieldToProductTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("product")
+			->addColumn("archived", "boolean", ["signed" => false, "default" => 0, "after" => "url"])
+			->update();
+	}
+}

+ 4 - 0
engine/Helpers/HtmlHelper.php

@@ -39,6 +39,10 @@ class HtmlHelper {
 		return $value ? "readonly" : "";
 	}
 
+	public static function checked($value): string {
+		return $value ? "checked" : "";
+	}
+
 	public static function hidden($value): string {
 		return $value ? "hidden" : "";
 	}

+ 11 - 1
engine/Product.php

@@ -15,6 +15,7 @@ use Exception;
  * @property int $type
  * @property string $price
  * @property string $url
+ * @property int $archived
  */
 
 class Product extends Model implements \JsonSerializable {
@@ -23,7 +24,7 @@ class Product extends Model implements \JsonSerializable {
 	protected static $idField = "id";
 	protected static $fillableFields = [
 		"supplier_id",
-		"name", "type", "sku", "price", "url",
+		"name", "type", "sku", "price", "url", "archived",
 		"dts_created", "dts_updated", "dts_deleted"
 	];
 
@@ -75,6 +76,10 @@ class Product extends Model implements \JsonSerializable {
 		return sprintf("/products/%s", $this->getSlug());
 	}
 
+	public function getUpdateUrl(): string {
+		return sprintf("/products/%s/update", $this->getSlug());
+	}
+
 	public function getWebsiteUrl(): ?string {
 		return $this->url;
 	}
@@ -95,6 +100,7 @@ class Product extends Model implements \JsonSerializable {
 			"sku" => $this->sku,
 			"type" => (int) $this->type,
 			"price" => $this->price,
+			"archived" => $this->isArchived(),
 		];
 	}
 
@@ -109,4 +115,8 @@ class Product extends Model implements \JsonSerializable {
 	public function deleteBrochure(): bool {
 		return !$this->hasBrochure() || unlink($this->getBrochurePath());
 	}
+
+	public function isArchived(): bool {
+		return (bool) $this->archived;
+	}
 }

+ 5 - 3
engine/Supplier.php

@@ -61,11 +61,13 @@ class Supplier extends Model {
 	/**
 	 * @return Product[]
 	 */
-	public function getProducts(): array {
-		return Product::query([
+	public function getProducts(array $nvp = []): array {
+		$nvp = array_merge($nvp, [
 			"supplier_id" => $this->id,
 			"dts_deleted" => null
-		], ["sort" => "name"]);
+		]);
+
+		return Product::query($nvp, ["sort" => "name"]);
 	}
 
 }

+ 1 - 0
public/js/document-item-modal.js

@@ -52,6 +52,7 @@ class DocumentItemModal {
 				option.textContent = product.name;
 				option.value = product.id;
 				option.dataset.price = product.price;
+				option.hidden = product.archived;
 				return option;
 			});
 			this.$productDropdown.append(options);

+ 1 - 0
public/js/sale-item-modal.js

@@ -81,6 +81,7 @@ class SaleItemModal {
 				option.textContent = product.name;
 				option.value = product.id;
 				option.dataset.price = product.price;
+				option.hidden = product.archived;
 				return option;
 			});
 			this.$productDropdown.append(options);

+ 9 - 0
views/product.php

@@ -13,6 +13,15 @@ $supplier = $product->getSupplier();
 	<div class="col">
 		<h3>
 			<?=htmlspecialchars($product->name)?>
+			<?php
+			if($product->isArchived()) {
+				?>
+				<span class="badge text-bg-secondary">
+					<?=htmlspecialchars(__("Archived"))?>
+				</span>
+				<?php
+			}
+			?>
 		</h3>
 	</div>
 	<div class="col-auto">

+ 7 - 0
views/product_form.php

@@ -107,6 +107,13 @@ else {
 		?>
 	</div>
 
+	<div class="mb-3">
+		<div class="form-check">
+			<input class="form-check-input" type="checkbox" id="archived" name="archived" value="1" <?=HtmlHelper::checked($product->archived)?> />
+			<label class="form-check-label" for="archived"><?=htmlspecialchars(__("Archived"))?></label>
+		</div>
+	</div>
+
 
 	<div class="mb-3">
 		<button class="btn btn-primary" type="submit" name="submit" value="1">

+ 18 - 8
views/products.php

@@ -7,12 +7,14 @@ use App\Helpers\HtmlHelper;
 /**
  * @var Product[] $products
  * @var Pagination $pagination
+ * @var array $archived_options
  * @var array $sort_options
  * @var int $product_id
  * @var int $supplier_id
  * @var string $name
  * @var string $sku
  * @var int $type
+ * @var int $archived
  * @var string $sort
  */
 
@@ -78,14 +80,22 @@ use App\Helpers\HtmlHelper;
 				</div>
 				<div class="col-sm-4">
 					<div class="mb-3">
-						<div class="mb-3">
-							<label class="form-label" for="sort"><?= htmlspecialchars(__("Sort")) ?></label>
-							<select class="form-select" name="sort" id="sort">
-								<?php
-								HtmlHelper::renderOptions($sort_options, $sort);
-								?>
-							</select>
-						</div>
+						<label class="form-label" for="archived"><?= htmlspecialchars(__("Archived")) ?></label>
+						<select class="form-select" name="archived" id="archived">
+							<?php
+							HtmlHelper::renderOptions($archived_options, $archived);
+							?>
+						</select>
+					</div>
+				</div>
+				<div class="col-sm-4">
+					<div class="mb-3">
+						<label class="form-label" for="sort"><?= htmlspecialchars(__("Sort")) ?></label>
+						<select class="form-select" name="sort" id="sort">
+							<?php
+							HtmlHelper::renderOptions($sort_options, $sort);
+							?>
+						</select>
 					</div>
 				</div>
 			</div>

+ 5 - 2
views/products_table.php

@@ -23,8 +23,9 @@ namespace App;
 	<?php
 	foreach($products as $product) {
 		$supplier = $product->getSupplier();
+		$row_class = $product->isArchived() ? "table-secondary" : "";
 		?>
-		<tr>
+		<tr class="<?=$row_class?>">
 			<td class="text-end">
 				<?=$product->id?>
 			</td>
@@ -34,7 +35,9 @@ namespace App;
 				</a>
 			</td>
 			<td>
-				<?=htmlspecialchars($product->name)?>
+				<a href="<?=htmlspecialchars($product->getUrl())?>">
+					<?=htmlspecialchars($product->name)?>
+				</a>
 			</td>
 			<td>
 				<?=htmlspecialchars($product->sku)?>