ソースを参照

Autofill SKU when adding a product

Alan Lam 2 年 前
親
コミット
066a3e12c9

+ 46 - 0
controllers/api.php

@@ -2,7 +2,9 @@
 
 namespace App;
 
+use App\Constraint\Constraint;
 use App\Constraint\HasTextConstraint;
+use App\Constraint\StartsWithConstraint;
 use Exception;
 use RuntimeException;
 
@@ -17,6 +19,50 @@ header("Content-Type: application/json");
 try {
 	switch($action) {
 
+		case "suggest-sku":
+			$supplier_id = $request->int("supplier_id");
+			$product_type = $request->int("product_type");
+
+			$supplier = Supplier::require($supplier_id);
+			$product_prefix = Product::getSkuPrefix($product_type);
+
+			if(!$supplier->sku_prefix) {
+				JSON::fail("The supplier has no SKU prefix set.");
+			}
+
+			$products = Product::query([
+				"sku" => (new StartsWithConstraint($supplier->sku_prefix . $product_prefix))
+			]);
+
+			$sku_flags = [];
+
+			foreach($products as $product) {
+				if($product->sku) {
+					$sku_flags[$product->sku] = true;
+				}
+			}
+
+			$found_sku = null;
+
+			foreach(range(1, 999) as $number) {
+				$sku = $supplier->sku_prefix . $product_prefix . str_pad($number, 3, "0", STR_PAD_LEFT);
+				if(isset($sku_flags[$sku])) {
+					continue;
+				}
+
+				$found_sku = $sku;
+				break;
+			}
+
+			if($found_sku) {
+				JSON::success(["sku" => $found_sku]);
+			}
+			else {
+				JSON::fail("An available SKU could not be found.");
+			}
+
+			break;
+
 		case "get-markers":
 			$customer_id = $request->int("customer_id");
 			$customer = Customer::require($customer_id);

+ 2 - 0
controllers/product_form.php

@@ -86,6 +86,8 @@ if($submit) {
 	}
 }
 
+$core->addScript("/js/product-form.js");
+
 View::include("header");
 View::include("product_form", compact("title", "product"));
 View::include("footer");

+ 14 - 0
engine/Constraint/StartsWithConstraint.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Constraint;
+
+use App\Database;
+
+class StartsWithConstraint extends Constraint {
+
+	public function getString(): string {
+		$value = Database::escape($this->values[0]);
+		return "LIKE '$value%'";
+	}
+
+}

+ 11 - 0
engine/Product.php

@@ -46,6 +46,17 @@ class Product extends Model implements \JsonSerializable {
 		];
 	}
 
+	public static function getSkuPrefix(int $type): string {
+		$map = [
+			self::TYPE_MACHINE => "M",
+			self::TYPE_SPARE_PART => "S",
+			self::TYPE_CONSUMABLE => "C",
+			self::TYPE_DELIVERY_CHARGE => "D",
+		];
+
+		return $map[$type] ?? "";
+	}
+
 	// Object methods
 
 	/**

+ 39 - 0
public/js/product-form.js

@@ -0,0 +1,39 @@
+class ProductForm {
+
+	constructor() {
+
+		this.$supplierField = $("#supplier-id");
+		this.$typeField = $("#type");
+		this.$skuField = $("#sku");
+
+		this.$supplierField.on("change", () => this.autoFillSku());
+		this.$typeField.on("change", () => this.autoFillSku());
+
+		this.autoFillSku();
+	}
+
+	get isNew() {
+		return $("#new").val() === "1";
+	}
+
+	autoFillSku() {
+		if(this.$skuField.val() && !this.isNew) {
+			return;
+		}
+
+		$.post("/api/suggest-sku", {
+			supplier_id: this.$supplierField.val(),
+			product_type: this.$typeField.val(),
+		}).then(response => {
+			if(response.sku) {
+				this.$skuField.val(response.sku);
+			}
+		}).catch(error => {
+			this.$skuField.val("");
+		});
+	}
+
+
+}
+
+$(() => new ProductForm());

+ 30 - 21
views/product_form.php

@@ -12,14 +12,15 @@ use App\Helpers\HtmlHelper;
 $core = Core::getInstance();
 $user = $core->getUser();
 
-$suppliers = Supplier::getAll();
 $request = Request::fromRequest();
 
 if($product->isNew()) {
 	$supplier_id = $request->int("supplier_id");
+	$new = 1;
 }
 else {
 	$supplier_id = $product->supplier_id;
+	$new = 0;
 }
 
 ?>
@@ -30,28 +31,21 @@ else {
 	$user->insertCsrfToken();
 	?>
 
-	<div class="mb-3">
-		<label class="form-label" for="supplier-id"><?=htmlspecialchars(__("Supplier"))?></label>
-		<select class="form-select" name="supplier_id" id="supplier-id" required>
-			<option value=""><?=htmlspecialchars(__("Select Supplier"))?></option>
-			<?php
-			HtmlHelper::renderOptions(Supplier::getOptions(), $supplier_id);
-			?>
-		</select>
-	</div>
-
-	<div class="mb-3">
-		<label class="form-label" for="name"><?=htmlspecialchars(__("Name"))?></label>
-		<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 class="form-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>
+	<input type="hidden" id="new" value="<?=$new?>" />
 
 	<div class="row">
-		<div class="col-6">
+		<div class="col-sm-6">
+			<div class="mb-3">
+				<label class="form-label" for="supplier-id"><?=htmlspecialchars(__("Supplier"))?></label>
+				<select class="form-select" name="supplier_id" id="supplier-id" required>
+					<option value=""><?=htmlspecialchars(__("Select Supplier"))?></option>
+					<?php
+					HtmlHelper::renderOptions(Supplier::getOptions(), $supplier_id);
+					?>
+				</select>
+			</div>
+		</div>
+		<div class="col-sm-6">
 			<div class="mb-3">
 				<label class="form-label" for="type"><?=htmlspecialchars(__("Type"))?></label>
 				<select class="form-select" name="type" id="type" required>
@@ -67,6 +61,21 @@ else {
 				</select>
 			</div>
 		</div>
+	</div>
+
+
+	<div class="mb-3">
+		<label class="form-label" for="name"><?=htmlspecialchars(__("Name"))?></label>
+		<input class="form-control" type="text" id="name" name="name" placeholder="<?=htmlspecialchars(__("Type name"))?>" value="<?=htmlspecialchars($product->name)?>" maxlength="255" required />
+	</div>
+
+	<div class="row">
+		<div class="col-6">
+			<div class="mb-3">
+				<label class="form-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>
 		<div class="col-6">
 			<div class="mb-3">
 				<label class="form-label" for="price"><?=htmlspecialchars(__("Price"))?></label>