فهرست منبع

Add ability to search entities via references

Alan Lam 3 سال پیش
والد
کامیت
225f216a5b
5فایلهای تغییر یافته به همراه78 افزوده شده و 4 حذف شده
  1. 55 0
      controllers/search.php
  2. 15 0
      engine/Document.php
  3. 4 0
      engine/Sale.php
  4. 2 2
      views/documents.php
  5. 2 2
      views/sales_table.php

+ 55 - 0
controllers/search.php

@@ -2,6 +2,8 @@
 
 namespace App;
 
+use App\Enums\DocumentTypes;
+
 $core = Core::getInstance();
 
 $user = $core->getUser();
@@ -21,6 +23,59 @@ if($term) {
 	}
 	else {
 
+		// References
+		if(preg_match("/^([LGASD])(\d+)$/i", $term, $matches)) {
+			$type = strtoupper($matches[1]);
+			$number = $matches[2];
+
+			$document_type = null;
+
+			switch($type) {
+				case "L":
+					break;
+				case "G":
+					$document_type = DocumentTypes::GOODS_IN;
+					break;
+				case "A":
+					$document_type = DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
+					break;
+				case "S":
+					$document_type = DocumentTypes::SALES_ORDER;
+					break;
+				case "D":
+					$document_type = DocumentTypes::DELIVERY_NOTE;
+					break;
+			}
+
+			if($document_type === null) {
+				$sale = Sale::getByNumber($number);
+				if($sale) {
+					$results[] = [
+						"type" => __("Lead"),
+						"match_reason" => __("Lead reference match."),
+						"name" => $sale->getTitle(),
+						"url" => $sale->getUrl()
+					];
+				}
+			}
+			else {
+				$document = Document::queryOne([
+					"number" => $number,
+					"type" => $document_type,
+					"dts_deleted" => null
+				]);
+				if($document) {
+					$results[] = [
+						"type" => $document->getDisplayType(),
+						"match_reason" => __("%s reference match.", $document->getDisplayType()),
+						"name" => $document->getTitle(),
+						"url" => $document->getUrl()
+					];
+				}
+			}
+
+		}
+
 		// Search sale and document numbers
 		if(is_numeric($term)) {
 			$documents = Document::query([

+ 15 - 0
engine/Document.php

@@ -129,6 +129,21 @@ class Document extends Model {
 		return Location::get($this->location_id);
 	}
 
+	public function getReference(): string {
+		switch($this->getType()) {
+			case DocumentTypes::GOODS_IN:
+				return "G" . $this->number;
+			case DocumentTypes::ADVANCE_REPLACEMENT_UNIT:
+				return "A" . $this->number;
+			case DocumentTypes::SALES_ORDER:
+				return "S" . $this->number;
+			case DocumentTypes::DELIVERY_NOTE:
+				return "D" . $this->number;
+			default:
+				return $this->number;
+		}
+	}
+
 	/**
 	 * @return string
 	 * @throws Exception

+ 4 - 0
engine/Sale.php

@@ -66,6 +66,10 @@ class Sale extends Model {
 		return $this->id + 999;
 	}
 
+	public function getReference(): string {
+		return "L" . $this->getNumber();
+	}
+
 	public function getDisplayStatus(): string {
 		return SaleStatuses::enumToText($this->status);
 	}

+ 2 - 2
views/documents.php

@@ -176,7 +176,7 @@ else {
 		</colgroup>
 		<thead>
 		<tr>
-			<th><?=htmlspecialchars(__("No."))?></th>
+			<th><?=htmlspecialchars(__("Ref."))?></th>
 			<th><?=htmlspecialchars(__("Date"))?></th>
 			<?php
 			if(!$status_options) {
@@ -210,7 +210,7 @@ else {
 			?>
 			<tr>
 				<td class="text-end">
-					<a href="<?=htmlspecialchars($document->getUrl())?>"><?=$document->number?></a>
+					<a href="<?=htmlspecialchars($document->getUrl())?>"><?=$document->getReference()?></a>
 				</td>
 				<td>
 					<?=htmlspecialchars($document->getDisplayDate())?>

+ 2 - 2
views/sales_table.php

@@ -21,7 +21,7 @@ $user = $core->getUser();
 	</colgroup>
 	<thead>
 	<tr>
-		<th><?=htmlspecialchars(__("No."))?></th>
+		<th><?=htmlspecialchars(__("Ref."))?></th>
 		<th><?=htmlspecialchars(__("Date"))?></th>
 		<th><?=htmlspecialchars(__("Location"))?></th>
 		<th><?=htmlspecialchars(__("Status"))?></th>
@@ -37,7 +37,7 @@ $user = $core->getUser();
 		?>
 		<tr>
 			<td class="text-end">
-				<a href="<?=htmlspecialchars($sale->getUrl())?>"><?=htmlspecialchars($sale->getNumber())?></a>
+				<a href="<?=htmlspecialchars($sale->getUrl())?>"><?=htmlspecialchars($sale->getReference())?></a>
 			</td>
 			<td>
 				<?=htmlspecialchars($user->formatDate($sale->getDateTimeCreated()))?>