Эх сурвалжийг харах

Add SKU field for products

Alan Lam 4 жил өмнө
parent
commit
0a3077a234

+ 1 - 0
controllers/product_form.php

@@ -48,6 +48,7 @@ if($submit) {
 
 		$product->supplier_id = $supplier_id;
 		$product->name = $request->string("name");
+		$product->sku = $request->string("sku");
 		$product->type = $request->int("type");
 		$product->price = $request->decimal("price", 2);
 

+ 12 - 0
db/migrations/20220627225033_add_sku_field_to_product_table.php

@@ -0,0 +1,12 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddSkuFieldToProductTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("product")
+			->addColumn("sku", "string", ["after" => "type"])
+			->update();
+	}
+}

+ 2 - 1
engine/Product.php

@@ -9,6 +9,7 @@ use Exception;
  * @property int $id
  * @property int $supplier_id
  * @property string $name
+ * @property string $sku
  * @property int $type
  * @property string $price
  * @property string $dts_created
@@ -20,7 +21,7 @@ class Product extends Model {
 
 	protected static $table = "product";
 	protected static $idField = "id";
-	protected static $fillableFields = ["supplier_id", "name", "type", "price", "dts_created", "dts_updated", "dts_deleted"];
+	protected static $fillableFields = ["supplier_id", "name", "type", "sku", "price", "dts_created", "dts_updated", "dts_deleted"];
 
 	public const TYPE_MACHINE = 1;
 	public const TYPE_SPARE_PART = 2;

+ 4 - 0
views/product.php

@@ -32,6 +32,10 @@ $supplier = $product->getSupplier();
 			<?=htmlspecialchars($supplier->name)?>
 		</a>
 	</div>
+	<div class="list-group-item d-flex justify-content-between">
+		<strong><?=htmlspecialchars(__("SKU"))?></strong>
+		<?=htmlspecialchars($product->sku)?>
+	</div>
 	<div class="list-group-item d-flex justify-content-between">
 		<strong><?=htmlspecialchars(__("Type"))?></strong>
 		<?=htmlspecialchars($product->getDisplayType())?>

+ 6 - 1
views/product_form.php

@@ -22,7 +22,7 @@ $suppliers = Supplier::getAll();
 
 	<div class="mb-3">
 		<label for="supplier-id"><?=htmlspecialchars(__("Supplier"))?></label>
-		<select class="form-select" name="supplier_id" id="supplier-id">
+		<select class="form-select" name="supplier_id" id="supplier-id" required>
 			<option value=""><?=htmlspecialchars(__("Select Supplier"))?></option>
 			<?php
 			foreach($suppliers as $supplier) {
@@ -41,6 +41,11 @@ $suppliers = Supplier::getAll();
 		<input class="form-control" type="text" id="name" name="name" placeholder="<?=htmlspecialchars(__("Type name"))?>" value="<?=htmlspecialchars($product->name)?>" maxlength="255" required />
 	</div>
 
+	<div class="mb-3">
+		<label for="sku"><?=htmlspecialchars(__("SKU"))?></label>
+		<input class="form-control" type="text" id="sku" name="sku" placeholder="<?=htmlspecialchars(__("Type SKU"))?>" value="<?=htmlspecialchars($product->sku)?>" maxlength="255" required />
+	</div>
+
 	<div class="row">
 		<div class="col-6">
 			<div class="mb-3">

+ 1 - 47
views/products.php

@@ -29,51 +29,5 @@ if(empty($products)) {
 	<?php
 }
 else {
-	?>
-	<table class="table table-bordered">
-		<thead>
-		<tr>
-			<th><?=htmlspecialchars(__("ID"))?></th>
-			<th><?=htmlspecialchars(__("Supplier"))?></th>
-			<th><?=htmlspecialchars(__("Name"))?></th>
-			<th><?=htmlspecialchars(__("Type"))?></th>
-			<th><?=htmlspecialchars(__("Price"))?></th>
-			<th><?=htmlspecialchars(__("Actions"))?></th>
-		</tr>
-		</thead>
-		<tbody>
-		<?php
-		foreach($products as $product) {
-			$supplier = $product->getSupplier();
-			?>
-			<tr>
-				<td class="text-end">
-					<?=$product->id?>
-				</td>
-				<td>
-					<a href="<?=htmlspecialchars($supplier->getUrl())?>">
-						<?=htmlspecialchars($supplier->name)?>
-					</a>
-				</td>
-				<td>
-					<?=htmlspecialchars($product->name)?>
-				</td>
-				<td>
-					<?=htmlspecialchars($product->getDisplayType())?>
-				</td>
-				<td class="text-end">
-					<?=htmlspecialchars($product->getDisplayPrice())?>
-				</td>
-				<td class="p-1">
-					<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($product->getUrl())?>/update">
-						<?=htmlspecialchars(__("Update"))?>
-					</a>
-				</td>
-			</tr>
-			<?php
-		}
-		?>
-		</tbody>
-	</table>
-	<?php
+	View::include("products_table", compact("products"));
 }

+ 54 - 0
views/products_table.php

@@ -0,0 +1,54 @@
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+namespace App;
+
+/**
+ * @var Product[] $products
+ */
+
+?>
+<table class="table table-bordered">
+	<thead>
+	<tr>
+		<th><?=htmlspecialchars(__("ID"))?></th>
+		<th><?=htmlspecialchars(__("Supplier"))?></th>
+		<th><?=htmlspecialchars(__("Name"))?></th>
+		<th><?=htmlspecialchars(__("Type"))?></th>
+		<th><?=htmlspecialchars(__("Price"))?></th>
+		<th><?=htmlspecialchars(__("Actions"))?></th>
+	</tr>
+	</thead>
+	<tbody>
+	<?php
+	foreach($products as $product) {
+		$supplier = $product->getSupplier();
+		?>
+		<tr>
+			<td class="text-end">
+				<?=$product->id?>
+			</td>
+			<td>
+				<a href="<?=htmlspecialchars($supplier->getUrl())?>">
+					<?=htmlspecialchars($supplier->name)?>
+				</a>
+			</td>
+			<td>
+				<?=htmlspecialchars($product->name)?>
+			</td>
+			<td>
+				<?=htmlspecialchars($product->getDisplayType())?>
+			</td>
+			<td class="text-end">
+				<?=htmlspecialchars($product->getDisplayPrice())?>
+			</td>
+			<td class="p-1">
+				<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($product->getUrl())?>/update">
+					<?=htmlspecialchars(__("Update"))?>
+				</a>
+			</td>
+		</tr>
+		<?php
+	}
+	?>
+	</tbody>
+</table>

+ 4 - 1
views/supplier.php

@@ -6,7 +6,7 @@ namespace App;
  * @var Supplier $supplier
  */
 
-$products = [];
+$products = $supplier->getProducts();
 
 ?>
 
@@ -33,3 +33,6 @@ if(empty($products)) {
 	</div>
 	<?php
 }
+else {
+	View::include("products_table", compact("products"));
+}