Kaynağa Gözat

Add "Related Items" section

Alan Lam 3 yıl önce
ebeveyn
işleme
20ece1b2fc
6 değiştirilmiş dosya ile 161 ekleme ve 43 silme
  1. 25 0
      engine/Document.php
  2. 10 0
      engine/Model.php
  3. 13 1
      engine/Sale.php
  4. 10 40
      views/document.php
  5. 94 0
      views/related_items_section.php
  6. 9 2
      views/sale.php

+ 25 - 0
engine/Document.php

@@ -395,4 +395,29 @@ class Document extends Model {
 		return $output_path;
 	}
 
+	/**
+	 * @return $this
+	 * @throws Exception
+	 */
+	public function getRootDocument(): Document {
+		$related_document = $this->getRelatedDocument();
+		if($related_document) {
+			return $related_document->getRootDocument();
+		}
+
+		return $this;
+	}
+
+	/**
+	 * @return Document[]
+	 */
+	public function getChildDocuments(): array {
+		return self::query([
+			"related_document_id" => $this->id,
+			"dts_deleted" => null
+		], [
+			"sort" => "type",
+		]);
+	}
+
 }

+ 10 - 0
engine/Model.php

@@ -656,6 +656,10 @@ abstract class Model {
 		return clone $this->modelData;
 	}
 
+	final public function getTable(): string {
+		return static::$table;
+	}
+
 	/**
 	 * @param Model|string $id_or_obj
 	 * @return bool
@@ -666,6 +670,12 @@ abstract class Model {
 			return $id_string === $id_or_obj;
 		}
 
+		$model = $id_or_obj;
+
+		if($model->getTable() !== $this->getTable()) {
+			return false;
+		}
+
 		return ($id_string === $id_or_obj->getIdString());
 	}
 

+ 13 - 1
engine/Sale.php

@@ -129,7 +129,7 @@ class Sale extends Model {
 	}
 
 	public function getTitle(): string {
-		return __("Lead %d - %s", $this->getNumber(), $this->getLocation()->getDisplayName());
+		return __("Lead %d", $this->getNumber());
 	}
 
 	public function getDisplayPredictedSoldDate(string $format = "d/m/Y"): string {
@@ -140,4 +140,16 @@ class Sale extends Model {
 		return CarbonImmutable::parse($this->date_predicted_sold)->format($format);
 	}
 
+	/**
+	 * @return Document[]
+	 */
+	public function getChildDocuments(): array {
+		return Document::query([
+			"sale_id" => $this->id,
+			"type" => DocumentTypes::SALES_ORDER
+		], [
+			"sort" => "type",
+		]);
+	}
+
 }

+ 10 - 40
views/document.php

@@ -215,6 +215,16 @@ $location = $document->getLocation();
 
 			</div>
 			<div class="col-xl-4">
+				<?php
+				$root_document = $document->getRootDocument();
+
+				View::include("related_items_section", [
+					"sale" => $root_document->getSale(),
+					"document" => $root_document,
+					"current" => $document
+				]);
+				?>
+
 				<h4><?=htmlspecialchars(__("Additional Details"))?></h4>
 
 				<?php
@@ -337,46 +347,6 @@ $location = $document->getLocation();
 						</dd>
 						<?php
 					}
-
-					if($document->hasCosts()) {
-						$delivery_notes = $document->getDeliveryNotes();
-
-						if($delivery_notes) {
-							?>
-							<dt><?=htmlspecialchars(__("Delivery Notes"))?></dt>
-							<dd>
-								<?php
-								foreach($delivery_notes as $delivery_note) {
-									?>
-									<a href="<?=htmlspecialchars($delivery_note->getUrl())?>">
-										<?=htmlspecialchars($delivery_note->getTitle())?>
-									</a><br />
-									<?php
-								}
-								?>
-							</dd>
-							<?php
-						}
-
-						$sales_orders = $document->getSalesOrders();
-
-						if($sales_orders) {
-							?>
-							<dt><?=htmlspecialchars(__("Sales Orders"))?></dt>
-							<dd>
-								<?php
-								foreach($sales_orders as $sales_order) {
-									?>
-									<a href="<?=htmlspecialchars($sales_order->getUrl())?>">
-										<?=htmlspecialchars($sales_order->getTitle())?>
-									</a><br />
-									<?php
-								}
-								?>
-							</dd>
-							<?php
-						}
-					}
 					?>
 				</dl>
 			</div>

+ 94 - 0
views/related_items_section.php

@@ -0,0 +1,94 @@
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+namespace App;
+
+/**
+ * @var Sale $sale
+ * @var Document $document
+ * @var Model $current
+ */
+
+if(isset($sale)) {
+	$title = $sale->getTitle();
+	$child_documents = $sale->getChildDocuments();
+	$is_current = $sale->is($current);
+	$url = $sale->getUrl();
+}
+else if(isset($document)) {
+	$title = $document->getTitle();
+	$child_documents = $document->getChildDocuments();
+	$is_current = $document->is($current);
+	$url = $document->getUrl();
+}
+else {
+	return;
+}
+
+?>
+
+<section class="mb-4">
+
+	<h4><?=htmlspecialchars(__("Related Items"))?></h4>
+
+	<?php
+	/**
+	 * @param Document[] $documents
+	 * @return void
+	 * @throws \Exception
+	 */
+	function renderDocumentList(array $documents, Model $current): void {
+		if(!$documents) {
+			return;
+		}
+		?>
+		<ul>
+			<?php
+			foreach($documents as $document) {
+				?>
+				<li>
+					<?php
+					if($document->is($current)) {
+						?>
+						<?=htmlspecialchars($document->getTitle())?>
+						<span class="badge text-bg-primary"><?=htmlspecialchars(__("Current"))?></span>
+						<?php
+					}
+					else {
+						?>
+						<a href="<?=htmlspecialchars($document->getUrl())?>"><?=htmlspecialchars($document->getTitle())?></a>
+						<?php
+					}
+					?>
+					<?php
+					renderDocumentList($document->getChildDocuments(), $current);
+					?>
+				</li>
+				<?php
+			}
+			?>
+		</ul>
+		<?php
+	}
+	?>
+
+	<ul>
+		<li>
+			<?php
+			if($is_current) {
+				?>
+				<?=htmlspecialchars($title)?>
+				<span class="badge text-bg-primary"><?=htmlspecialchars(__("Current"))?></span>
+				<?php
+			}
+			else {
+				?>
+				<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars($title)?></a>
+				<?php
+			}
+			?>
+			<?php
+			renderDocumentList($child_documents, $current);
+			?>
+		</li>
+	</ul>
+</section>

+ 9 - 2
views/sale.php

@@ -24,7 +24,7 @@ $location = $sale->getLocation();
 
 		<div class="row">
 
-			<div class="col-xl-6">
+			<div class="col-xl-8">
 
 				<h4><?=htmlspecialchars(__("Main Details"))?></h4>
 
@@ -69,7 +69,14 @@ $location = $sale->getLocation();
 					</div>
 				</div>
 			</div>
-			<div class="col-xl-6">
+			<div class="col-xl-4">
+
+				<?php
+				View::include("related_items_section", [
+					"sale" => $sale,
+					"current" => $sale
+				]);
+				?>
 
 				<h4><?=htmlspecialchars(__("Additional Details"))?></h4>