Просмотр исходного кода

Add ability to link tickets to sales and ARUs

Alan Lam 3 лет назад
Родитель
Сommit
6d36d2d100

+ 43 - 1
controllers/dashboard.php

@@ -2,9 +2,51 @@
 
 namespace App;
 
+use App\Constraint\Constraint;
+use App\Constraint\NotEqualsConstraint;
+use App\Constraint\NotInConstraint;
+use App\Enums\AruStatuses;
+use App\Enums\DocumentTypes;
+use App\Enums\InvoiceStatuses;
+use App\Enums\SaleStatuses;
+use App\Enums\TicketStatuses;
+
 $core = Core::getInstance();
 $core->addBreadcrumb("/dashboard", __("Dashboard"));
 
+$cards = [];
+
+$open_ticket_count = Ticket::count([
+	"status" => new NotEqualsConstraint(TicketStatuses::CLOSED),
+	"dts_deleted" => null
+]);
+
+$cards[] = ["title" => "Tickets Open", "count" => $open_ticket_count];
+
+$open_sale_count = Sale::count([
+	"status" => new NotEqualsConstraint(SaleStatuses::COMPLETED),
+	"dts_deleted" => null
+]);
+
+$cards[] = ["title" => "Sales Open", "count" => $open_sale_count];
+
+$open_aru_count = Document::count([
+	"type" => DocumentTypes::ADVANCE_REPLACEMENT_UNIT,
+	"aru_status" => new NotInConstraint(AruStatuses::COMPLETED, AruStatuses::CANCELLED),
+	"dts_deleted" => null
+]);
+
+$cards[] = ["title" => "ARUs Open", "count" => $open_aru_count];
+
+$open_invoice_count = Document::count([
+	"type" => DocumentTypes::INVOICE,
+	"invoice_status" => new NotInConstraint(InvoiceStatuses::VOID, InvoiceStatuses::PAID),
+	"dts_deleted" => null
+]);
+
+
+$cards[] = ["title" => "Invoices Open", "count" => $open_invoice_count];
+
 View::include("header");
-View::include("dashboard");
+View::include("dashboard", compact("cards"));
 View::include("footer");

+ 16 - 0
controllers/document.php

@@ -89,6 +89,14 @@ $controller = new class($document_type, $document_number) {
 		return $location;
 	}
 
+	private function requireTicket(int $id): Ticket {
+		$ticket = Ticket::get($id);
+		if(!$ticket) {
+			throw new RuntimeException(__("Ticket not found."));
+		}
+		return $ticket;
+	}
+
 	private function requireSupplier(int $id): Supplier {
 		$supplier = Supplier::get($id);
 		if(!$supplier) {
@@ -151,6 +159,9 @@ $controller = new class($document_type, $document_number) {
 		$location_id = $this->request->int("location_id");
 		$location = $location_id ? $this->requireLocation($location_id) : null;
 
+		$ticket_id = $this->request->int("ticket_id");
+		$ticket = $ticket_id ? $this->requireTicket($ticket_id) : null;
+
 		$document = new Document();
 		$document->company_id = $company->id ?? null;
 		$document->user_id = $this->user->id;
@@ -170,6 +181,11 @@ $controller = new class($document_type, $document_number) {
 
 		$document->commit();
 
+		if($document->isAru() && $ticket) {
+			$ticket->aru_document_id = $document->id;
+			$ticket->commit();
+		}
+
 		$item_description = $this->request->string("item_description");
 		$item_serial_number = $this->request->string("item_serial_number");
 

+ 2 - 0
controllers/sale.php

@@ -77,11 +77,13 @@ $controller = new class($number) {
 		$core->addScript("/js/sale-item-modal.js");
 		$core->addScript("/js/sale-note-modal.js");
 		$core->addScript("/js/sale-page.js");
+		$core->addScript("/js/ticket-modal.js");
 
 		View::include("header");
 		View::include("sale", compact("sale"));
 		View::include("modals/sale_item");
 		View::include("modals/sale_note");
+		View::include("modals/ticket");
 		View::include("footer");
 	}
 

+ 3 - 0
controllers/ticket.php

@@ -79,6 +79,9 @@ if($comment_submit) {
 	Http::redirect($ticket->getUrl());
 }
 
+$core->addScript("/js/new-document-modal.js");
+
 View::include("header");
 View::include("ticket", compact("ticket"));
+View::include("modals/new_document");
 View::include("footer");

+ 20 - 0
controllers/tickets_ajax.php

@@ -2,6 +2,7 @@
 
 namespace App;
 
+use App\Enums\DocumentTypes;
 use RuntimeException;
 
 /**
@@ -45,6 +46,23 @@ $controller = new class($action) {
 		$location_name = $request->string("location_name");
 		$postal_code = $request->string("postal_code");
 
+		$sale_number = $request->int("sale_number");
+		$aru_number = $request->int("aru_number");
+
+		if($sale_number) {
+			$sale = Sale::getByNumber($sale_number);
+			if(!$sale) {
+				throw new RuntimeException(__("Sale not found."));
+			}
+		}
+
+		if($aru_number) {
+			$aru = Document::getByNumber($aru_number, DocumentTypes::ADVANCE_REPLACEMENT_UNIT);
+			if(!$aru) {
+				throw new RuntimeException(__("ARU not found."));
+			}
+		}
+
 		$location = null;
 		if($location_id) {
 			$location = Location::get($location_id);
@@ -80,6 +98,8 @@ $controller = new class($action) {
 
 		$ticket = new Ticket();
 		$ticket->location_id = $location->id;
+		$ticket->sale_id = $sale->id ?? null;
+		$ticket->aru_document_id = $aru->id ?? null;
 		$ticket->creator_user_id = $this->user->id;
 		$ticket->assigned_user_id = $this->user->id;
 		$ticket->subject = $request->string("subject");

+ 13 - 0
db/migrations/20221223162954_add_sale_id_field_to_ticket_table.php

@@ -0,0 +1,13 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddSaleIdFieldToTicketTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("ticket")
+			->addColumn("sale_id", "integer", ["signed" => false, "null" => true, "after" => "location_id"])
+			->addForeignKey("sale_id", "sale", "id", ["update" => "CASCADE", "delete" => "SET NULL"])
+			->update();
+	}
+}

+ 13 - 0
db/migrations/20221223163100_add_aru_document_id_to_ticket_table.php

@@ -0,0 +1,13 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddAruDocumentIdToTicketTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("ticket")
+			->addColumn("aru_document_id", "integer", ["signed" => false, "null" => true, "after" => "sale_id"])
+			->addForeignKey("aru_document_id", "document", "id", ["update" => "CASCADE", "delete" => "SET NULL"])
+			->update();
+	}
+}

+ 21 - 0
engine/Constraint/NotInConstraint.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace App\Constraint;
+
+use App\Database;
+
+class NotInConstraint extends Constraint {
+
+	public function getString(): string {
+		if(!$this->values) {
+			return "1";
+		}
+
+		$values = array_map(static function($value) {
+			return "'" . Database::escape($value) . "'";
+		}, $this->values);
+
+		return sprintf("NOT IN (%s)", implode(", ", $values));
+	}
+
+}

+ 24 - 0
engine/Document.php

@@ -78,6 +78,20 @@ class Document extends Model {
 		return 1000;
 	}
 
+	/**
+	 * @param int $number
+	 * @param int $document_type
+	 * @return Document|null
+	 * @throws Exception
+	 */
+	public static function getByNumber(int $number, int $document_type): ?Document {
+		return self::queryOne([
+			"type" => $document_type,
+			"number" => $number,
+			"dts_deleted" => null
+		]);
+	}
+
 	// Object methods
 
 	/**
@@ -335,4 +349,14 @@ class Document extends Model {
 		return nl2br(htmlspecialchars($this->notes));
 	}
 
+	/**
+	 * @return Ticket[]
+	 */
+	public function getTickets(): array {
+		return Ticket::query([
+			"aru_document_id" => $this->id,
+			"dts_deleted" => null
+		]);
+	}
+
 }

+ 2 - 0
engine/Enums/AruStatuses.php

@@ -8,6 +8,7 @@ class AruStatuses extends AbstractEnum {
 	public const AWAITING_PARTS_FROM_SUPPLIER = 1;
 	public const AWAITING_CUSTOMER_RETURN = 2;
 	public const COMPLETED = 3;
+	public const CANCELLED = 4;
 
 	public static function options(): array {
 		return [
@@ -15,6 +16,7 @@ class AruStatuses extends AbstractEnum {
 			self::AWAITING_PARTS_FROM_SUPPLIER => __("Awaiting Parts From Supplier"),
 			self::AWAITING_CUSTOMER_RETURN => __("Awaiting Customer Return"),
 			self::COMPLETED => __("Completed"),
+			self::CANCELLED => __("Cancelled"),
 		];
 	}
 }

+ 207 - 0
engine/Pagination.php

@@ -0,0 +1,207 @@
+<?php
+
+namespace App;
+
+class Pagination {
+
+	protected $pageSize = 25;
+	protected $currentPage = 1;
+	protected $itemCount = 0;
+	protected $width = 3;
+
+	protected $single;
+	protected $plural;
+
+	protected $parameters = [];
+
+	protected $isJsHandled = false;
+
+	/**
+	 * Pagination constructor.
+	 * @param int $item_count
+	 */
+	public function __construct(int $item_count) {
+		$this->itemCount = $item_count;
+
+		$this->setTerms(__("%s result"), __("%s results"));
+	}
+
+	public function setItemCount(int $count): bool {
+		$this->itemCount = $count;
+		$this->currentPage = max(min($this->currentPage, $this->getPageCount()), 1);
+		return true;
+	}
+
+	public function setTerms(string $single, string $plural): bool {
+		$this->single = $single;
+		$this->plural = $plural;
+		return true;
+	}
+
+	public function getOffset(): int {
+		return $this->pageSize * ($this->currentPage - 1);
+	}
+
+	public function slice(array $array): array {
+		return array_slice($array, $this->getOffset(), $this->pageSize);
+	}
+
+	private function from(): int {
+		return $this->getOffset() + 1;
+	}
+
+	private function to(): int {
+		return min($this->getOffset() + $this->pageSize, $this->itemCount);
+	}
+
+	private function getCurrentPage(): int {
+		return $this->currentPage;
+	}
+
+	private function getPageCount(): int {
+		return (int) ceil($this->itemCount / $this->pageSize) ?: 1;
+	}
+
+	public function getPageText(): string {
+		return __("Page %d of %d", $this->getCurrentPage(), $this->getPageCount());
+	}
+
+	private function hasPreviousPage(): bool {
+		return $this->currentPage > 1;
+	}
+
+	private function hasNextPage(): bool {
+		return $this->currentPage < $this->getPageCount();
+	}
+
+	private function urlForPage(int $page_number): string {
+		$query = $_GET;
+		$query = array_merge($query, $this->parameters);
+		$query["page"] = $page_number;
+
+		return $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH) ."?". http_build_query($query);
+	}
+
+	public function getText(): string {
+		$count_text = n__($this->single, $this->plural, $this->itemCount, number_format($this->itemCount));
+
+		if($this->getPageCount() <= 1) {
+			return sprintf("Showing %s", $count_text);
+		}
+
+		return sprintf("Showing %s-%s of %s",
+			number_format($this->from()),
+			number_format($this->to()),
+			$count_text
+		);
+	}
+
+	public function requiresPagination(): bool {
+		return $this->getPageCount() > 1;
+	}
+
+	/**
+	 * @param array $options <b>container_class</b> Pagination container CSS class (default: pagination justify-content-center)<br />
+	 * <b>item_class</b> Item container CSS class (default: page-item)<br />
+	 * <b>next</b> Text for next link (default: Next)<br />
+	 * <b>previous</b> Text for previous link (default: Previous)
+	 * @return string
+	 */
+	public function build(array $options = []): string {
+
+		$container_class = $options["container_class"] ?? "pagination justify-content-center";
+		$item_class = $options["item_class"] ?? "page-item";
+
+		$first_text = '<i class="fa fa-angle-double-left"></i>';
+		$previous_text = '<i class="fa fa-angle-left"></i>';
+		$next_text = '<i class="fa fa-angle-right"></i>';
+		$last_text = '<i class="fa fa-angle-double-right"></i>';
+
+		$current_page = $this->getCurrentPage();
+
+		$min_page = max($current_page - $this->width, 1);
+		$max_page = min($current_page + $this->width, $this->getPageCount());
+
+		$previous_page = $current_page - 1;
+		$next_page = $current_page + 1;
+
+		$output = "";
+
+		// Page count is 1 or less
+		if(!$this->requiresPagination()) return "";
+
+		// Start building output
+
+		$get_page_link_html = function(string $text, int $page) use ($item_class) {
+			if($this->isJsHandled) {
+				return "<li class=\"{$item_class} position-relative\"><a class=\"page-link stretched-link\" data-page=\"{$page}\" style=\"cursor: pointer\">{$text}</a></li>";
+			}
+
+			return "<li class=\"{$item_class} position-relative\"><a class=\"page-link stretched-link\" href=\"{$this->urlForPage($page)}\" data-page=\"{$page}\" data-ajax-load>{$text}</a></li>";
+		};
+
+		$get_disabled_link_html = static function(string $text) use ($item_class) {
+			return "<li class=\"{$item_class} disabled\"><span class=\"page-link\">{$text}</span></li>";
+		};
+
+		$get_active_link_html = function() use ($item_class) {
+			return "<li class=\"{$item_class} active\"><span class=\"page-link\">{$this->getCurrentPage()}</span></li>";
+		};
+
+		$output .= "<ul class=\"{$container_class}\">";
+
+		if($this->hasPreviousPage()) {
+			$output .= $get_page_link_html($first_text, 1);
+			$output .= $get_page_link_html($previous_text, $previous_page);
+		}
+		else {
+			$output .= $get_disabled_link_html($previous_text);
+		}
+
+		for($i = $current_page - $this->width; $i <= $current_page + $this->width; $i++) {
+			if(($i < $min_page) || ($i > $max_page)) {
+				continue;
+			}
+
+			if($i === $this->getCurrentPage()) {
+				$output .= $get_active_link_html();
+			}
+			else {
+				$output .= $get_page_link_html($i, $i);
+			}
+		}
+
+		if($this->hasNextPage()) {
+			$output .= $get_page_link_html($next_text, $next_page);
+			$output .= $get_page_link_html($last_text, $this->getPageCount());
+		}
+		else {
+			$output .= $get_disabled_link_html($next_text);
+		}
+
+		$output .= "</ul>";
+
+		return $output;
+	}
+
+	public function addParameter(string $name, ?string $value) {
+		if(!$value) return;
+		$this->parameters[$name] = $value;
+	}
+
+	public function setPage(?int $page) {
+		if($page < 1) $page = 1;
+		$this->currentPage = $page;
+	}
+
+	public function setPageSize(int $size) {
+		$this->pageSize = $size;
+	}
+
+	/**
+	 * @param bool $js_handled
+	 */
+	public function setIsJsHandled(bool $js_handled): void {
+		$this->isJsHandled = $js_handled;
+	}
+}

+ 3 - 1
engine/Sale.php

@@ -108,8 +108,10 @@ class Sale extends Model {
 		return $this->getDocuments(["type" => DocumentTypes::INVOICE]);
 	}
 
+	/**
+	 * @return Ticket[]
+	 */
 	public function getTickets(): array {
-		return []; // TODO
 		return Ticket::query([
 			"sale_id" => $this->id
 		], [

+ 42 - 12
engine/Ticket.php

@@ -7,11 +7,14 @@ use App\Enums\TicketStatuses;
 use App\Traits\DateTimeTrait;
 use App\Traits\SoftDeleteTrait;
 use Carbon\CarbonImmutable;
+use Exception;
 
 /**
  * Class Ticket
  * @property int $id
  * @property int $location_id
+ * @property int $sale_id
+ * @property int $aru_document_id
  * @property int $creator_user_id
  * @property int $assigned_user_id
  * @property string $subject
@@ -27,7 +30,7 @@ class Ticket extends Model {
 	protected static $table = "ticket";
 	protected static $idField = "id";
 	protected static $fillableFields = [
-		"location_id", "creator_user_id", "assigned_user_id",
+		"location_id", "sale_id", "aru_document_id", "creator_user_id", "assigned_user_id",
 		"subject", "contact_name", "status", "priority", "rating",
 		"dts_due",
 		"dts_created", "dts_updated", "dts_deleted",
@@ -36,14 +39,26 @@ class Ticket extends Model {
 	use DateTimeTrait;
 	use SoftDeleteTrait;
 
+	/**
+	 * @return Location
+	 * @throws Exception
+	 */
 	public function getLocation(): Location {
 		return Location::get($this->location_id);
 	}
 
+	/**
+	 * @return User
+	 * @throws Exception
+	 */
 	public function getCreator(): User {
 		return User::get($this->creator_user_id);
 	}
 
+	/**
+	 * @return User|null
+	 * @throws Exception
+	 */
 	public function getAssignedTo(): ?User {
 		return User::get($this->assigned_user_id);
 	}
@@ -59,11 +74,19 @@ class Ticket extends Model {
 		return TicketComment::query(["ticket_id" => $this->id], ["sort" => "id"]);
 	}
 
+	/**
+	 * @return string
+	 * @throws Exception
+	 */
 	public function getDisplayCreator(): string {
 		$creator = $this->getCreator();
 		return $creator->getDisplayName() . " ($creator->email)";
 	}
 
+	/**
+	 * @return string
+	 * @throws Exception
+	 */
 	public function getDisplayAssignedTo(): string {
 		$assigned_to = $this->getAssignedTo();
 		return $assigned_to ? $assigned_to->getDisplayName() . " ($assigned_to->email)" : __("Unassigned");
@@ -77,15 +100,6 @@ class Ticket extends Model {
 		return TicketPriorities::getBadgeHtml($this->priority);
 	}
 
-	public function getDisplayCreatedDateTime(): string {
-		return $this->getDateTimeCreated()->format("d/m/Y H:i");
-	}
-
-	public function getDisplayUpdatedDateTime(): string {
-		$dt_updated = $this->getDateTimeUpdated();
-		return $dt_updated ? $dt_updated->format("d/m/Y H:i") : __("Never");
-	}
-
 	public function getUrl(): string {
 		return sprintf("/tickets/%d", $this->id);
 	}
@@ -93,7 +107,7 @@ class Ticket extends Model {
 	/**
 	 * @param User $user
 	 * @return bool
-	 * @throws \Exception
+	 * @throws Exception
 	 */
 	public function assign(User $user): bool {
 		$this->assigned_user_id = $user->id;
@@ -102,11 +116,27 @@ class Ticket extends Model {
 
 	/**
 	 * @return bool
-	 * @throws \Exception
+	 * @throws Exception
 	 */
 	public function unassign(): bool {
 		$this->assigned_user_id = null;
 		return $this->commit();
 	}
 
+	/**
+	 * @return Sale|null
+	 * @throws Exception
+	 */
+	public function getSale(): ?Sale {
+		return Sale::get($this->sale_id);
+	}
+
+	/**
+	 * @return Document|null
+	 * @throws Exception
+	 */
+	public function getAruDocument(): ?Document {
+		return Document::get($this->aru_document_id);
+	}
+
 }

+ 1 - 1
engine/User.php

@@ -155,7 +155,7 @@ class User extends Model {
 			return __("Never");
 		}
 
-		return CarbonImmutable::instance($dt)->setTimezone("Europe/London")->format("d/m/Y H:i");
+		return CarbonImmutable::instance($dt)->setTimezone("Europe/London")->format($format);
 	}
 
 	public function formatDate(?DateTimeInterface $dt): string {

+ 14 - 5
public/js/new-document-modal.js

@@ -12,7 +12,8 @@ class NewDocumentModal {
 		this.$form = this.$modal.find("form");
 		this.$form.on("submit", event => this.onSubmit(event));
 
-		this.$typeField = this.getField("type");
+		this.$ticketIdField = this.getField("ticket_id");
+
 		this.$companyDropdown = this.getField("company_id");
 		this.$locationDropdown = this.getField("location_id");
 		this.$supplierDropdown = this.getField("supplier_id");
@@ -32,24 +33,32 @@ class NewDocumentModal {
 	    const $element = $(event.relatedTarget);
 	    const data = $element.data();
 
-		const { type } = data;
+		const { type, locationId, ticketId } = data;
 
 		this.$supplierDropdown.closest(".mb-3").show();
 		this.$aruSection.show();
 
 		switch(type) {
-			case 2: // Goods-In
+			case DocumentTypes.GoodsIn:
 				this.$title.text("New Goods-In");
 				this.baseUrl = "/goods-in";
 				this.$aruSection.hide();
 				break;
 
-			case 3: // ARU
+			case DocumentTypes.AdvancedReplacementUnit:
 				this.$title.text("New ARU");
 				this.baseUrl = "/arus";
 				this.$supplierDropdown.closest(".mb-3").hide();
 				break;
 		}
+
+		if(locationId) {
+			this.$locationDropdown.val(locationId);
+		}
+
+		if(ticketId) {
+			this.$ticketIdField.val(ticketId);
+		}
 	}
 
 	onShown() {
@@ -58,7 +67,7 @@ class NewDocumentModal {
 
 	onHidden() {
 		this.$form.get(0).reset();
-		this.$typeField.val(null);
+		this.$ticketIdField.val(null);
 	}
 
 	onSubmit(event) {

+ 19 - 2
public/js/ticket-modal.js

@@ -12,10 +12,13 @@ class TicketModal {
 		this.$form = this.$modal.find("form");
 		this.$form.on("submit", event => this.onSubmit(event));
 
-		this.$subjectField = this.$form.find("[name=subject]");
+		this.$subjectField = this.getField("subject");
 
 		this.$locationRow = $("#location-row");
-		this.$locationDropdown = this.$form.find("[name=location_id]");
+		this.$locationDropdown = this.getField("location_id");
+
+		this.$saleNumberField = this.getField("sale_number");
+		this.$aruNumberField = this.getField("aru_number");
 
 		this.$noLocationAction = $("#no-location-action");
 		this.$noLocationAction.on("click", event => {
@@ -26,10 +29,21 @@ class TicketModal {
 		this.$noLocationRow = $("#no-location-row");
 	}
 
+	getField(name) {
+		return this.$form.find(`[name=${name}]`);
+	}
+
 	// Modal lifecycle
 	onShow(event) {
 	    const $element = $(event.relatedTarget);
 	    const data = $element.data();
+
+		const { locationId, saleNumber } = data;
+		this.locationId = locationId;
+
+		if(saleNumber) {
+			this.$saleNumberField.val(saleNumber);
+		}
 	}
 
 	onShown() {
@@ -43,6 +57,9 @@ class TicketModal {
 				return $option;
 			});
 			this.$locationDropdown.append($options);
+			if(this.locationId) {
+				this.$locationDropdown.val(this.locationId);
+			}
 		});
 	}
 

+ 31 - 4
views/dashboard.php

@@ -1,6 +1,33 @@
-<h3><?=htmlspecialchars(__("Dashboard"))?></h3>
+<?php
+/**
+ * @var array $cards
+ */
+?>
 
-<div class="alert alert-warning">
-	<i class="fa fa-fw fa-info-circle"></i>
-	<?=htmlspecialchars(__("Coming soon..."))?>
+<div class="row mb-3">
+	<div class="col">
+		<h3><?=htmlspecialchars(__("Dashboard"))?></h3>
+	</div>
 </div>
+
+<div class="row g-3">
+	<?php
+	foreach($cards as $card) {
+		?>
+		<div class="col-6 col-md-4 col-lg-3">
+			<div class="card">
+				<div class="card-body text-center">
+					<strong>
+						<?=htmlspecialchars($card["title"])?>
+					</strong><br />
+					<div class="fa-2x">
+						<?=htmlspecialchars(number_format($card["count"]))?>
+					</div>
+				</div>
+			</div>
+		</div>
+		<?php
+	}
+	?>
+</div>
+<?php

+ 18 - 0
views/document.php

@@ -249,6 +249,24 @@ $related_document = $document->getRelatedDocument();
 
 				<dl>
 					<?php
+					if($document->isAru()) {
+						$tickets = $document->getTickets();
+						if($tickets) {
+							?>
+							<dt><?=htmlspecialchars(__("Related Tickets"))?></dt>
+							<dd>
+								<?php
+								foreach($tickets as $ticket) {
+									?>
+									<a href="<?=htmlspecialchars($ticket->getUrl())?>"><?=htmlspecialchars(__("Ticket %d - %s", $ticket->id, $ticket->subject))?></a>
+									<?php
+								}
+								?>
+							</dd>
+							<?php
+						}
+					}
+
 					if($company) {
 						?>
 						<dt><?=htmlspecialchars(__("Company"))?></dt>

+ 1 - 1
views/modals/new_document.php

@@ -8,7 +8,7 @@ namespace App;
 			<?php
 			View::include("modals/header", ["title" => __("New Document")]);
 			?>
-			<input type="hidden" name="type" value="" />
+			<input type="hidden" name="ticket_id" value="" />
 			<div class="modal-body">
 				<div class="mb-3">
 					<label class="form-label" for="new-document-company-id"><?= htmlspecialchars(__("Company")) ?></label>

+ 24 - 8
views/modals/ticket.php

@@ -11,19 +11,19 @@ namespace App;
 			View::include("modals/header", ["title" => __("New Ticket")]);
 			?>
 			<div class="modal-body">
-				<div class="mb-2">
+				<div class="mb-3">
 					<label class="form-label" for="ticket-subject"><?=htmlspecialchars(__("Subject"))?></label>
 					<input class="form-control" type="text" name="subject" id="ticket-subject" maxlength="255" placeholder="<?=htmlspecialchars(__("Type subject"))?>" required />
 				</div>
-				<div class="mb-2">
+				<div class="mb-3">
 					<label class="form-label" for="ticket-content"><?=htmlspecialchars(__("Content"))?></label>
 					<textarea class="form-control" name="content" id="ticket-content" rows="8" placeholder="<?=htmlspecialchars(__("Type content"))?>" required></textarea>
 				</div>
-				<div class="mb-2">
+				<div class="mb-3">
 					<label class="form-label" for="ticket-contact-name"><?=htmlspecialchars(__("Contact Name"))?></label>
 					<input class="form-control" name="contact_name" id="ticket-contact-name" type="text" maxlength="255" placeholder="<?=htmlspecialchars(__("Type contact name"))?>" />
 				</div>
-				<div id="location-row" class="mb-2">
+				<div id="location-row" class="mb-3">
 					<label class="form-label" for="ticket-location">
 						<?=htmlspecialchars(__("Location"))?>
 					</label>
@@ -35,7 +35,7 @@ namespace App;
 
 				<div id="no-location-row" class="row" style="display: none;">
 					<div class="col-lg-4">
-						<div class="mb-2">
+						<div class="mb-3">
 							<label class="form-label" for="ticket-customer-id">
 								<?=htmlspecialchars(__("Customer"))?>
 							</label>
@@ -48,7 +48,7 @@ namespace App;
 						</div>
 					</div>
 					<div class="col-lg-4">
-						<div class="mb-2">
+						<div class="mb-3">
 							<label class="form-label" for="ticket-location-name">
 								<?=htmlspecialchars(__("Location Name"))?>
 							</label>
@@ -56,7 +56,7 @@ namespace App;
 						</div>
 					</div>
 					<div class="col-lg-4">
-						<div class="mb-2">
+						<div class="mb-3">
 							<label class="form-label" for="ticket-postal-code">
 								<?=htmlspecialchars(__("Location Postcode"))?>
 							</label>
@@ -65,10 +65,26 @@ namespace App;
 					</div>
 				</div>
 
-				<div class="mb-2">
+				<div class="mb-3">
 					<label class="form-label" for="ticket-attachments"><?=htmlspecialchars(__("Attachments"))?></label>
 					<input class="form-control" type="file" name="attachments[]" multiple />
 				</div>
+
+				<div class="row">
+					<div class="col-6">
+						<div class="mb-3">
+							<label class="form-label" for="ticket-sale-number"><?=htmlspecialchars(__("Sale No."))?></label>
+							<input class="form-control" type="number" name="sale_number" id="ticket-sale-number" min="0" placeholder="<?=htmlspecialchars(__("Type sale number"))?>" />
+						</div>
+					</div>
+					<div class="col-6">
+						<div class="mb-3">
+							<label class="form-label" for="ticket-aru-number"><?=htmlspecialchars(__("ARU No."))?></label>
+							<input class="form-control" type="number" name="aru_number" id="ticket-aru-number" min="0" placeholder="<?=htmlspecialchars(__("Type ARU number"))?>" />
+						</div>
+					</div>
+				</div>
+
 			</div>
 			<div class="modal-footer">
 				<button type="submit" class="btn btn-success">

+ 69 - 2
views/sale.php

@@ -10,6 +10,9 @@ use App\Helpers\HtmlHelper;
  * @var Sale $sale
  */
 
+$core = Core::getInstance();
+$user = $core->getUser();
+
 ?>
 
 <section class="mb-4">
@@ -85,14 +88,78 @@ View::include("sale_notes_section", compact("sale"));
 ?>
 
 <section class="mb-4">
-	<h3><?=htmlspecialchars(__("Tickets"))?></h3>
+	<div class="row mb-3">
+		<div class="col">
+			<h3><?=htmlspecialchars(__("Tickets"))?></h3>
+		</div>
+		<div class="col-auto">
+			<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#ticket-modal" data-location-id="<?=$sale->location_id?>" data-sale-number="<?=$sale->getNumber()?>">
+				<i class="fa fa-fw fa-plus"></i>
+				<?=htmlspecialchars(__("Add Ticket"))?>
+			</button>
+		</div>
+	</div>
 	<?php
 	$tickets = $sale->getTickets();
 	if(empty($tickets)) {
 		HtmlHelper::renderInfoAlert(__("No tickets to display."));
 	}
 	else {
-
+		?>
+		<table class="table table-bordered table-hover">
+			<colgroup>
+				<col style="width: 10%"/>
+				<col style="width: 10%"/>
+				<col style="width: 35%"/>
+				<col style="width: 10%"/>
+				<col style="width: 20%"/>
+				<col style="width: 15%"/>
+				<col style="width: 1%"/>
+			</colgroup>
+			<thead>
+			<tr>
+				<th><?=htmlspecialchars(__("ID"))?></th>
+				<th><?=htmlspecialchars(__("Date"))?></th>
+				<th><?=htmlspecialchars(__("Subject"))?></th>
+				<th><?=htmlspecialchars(__("Status"))?></th>
+				<th><?=htmlspecialchars(__("Assigned To"))?></th>
+				<th><?=htmlspecialchars(__("Last Updated"))?></th>
+				<th><?=htmlspecialchars(__("Actions"))?></th>
+			</tr>
+			</thead>
+			<tbody>
+			<?php
+			foreach($tickets as $ticket) {
+				?>
+				<tr>
+					<td class="text-end">
+						<a href="<?=htmlspecialchars($ticket->getUrl())?>"><?=htmlspecialchars($ticket->id)?></a>
+					</td>
+					<td>
+						<?=htmlspecialchars($user->formatDate($ticket->getDateTimeCreated()))?>
+					</td>
+					<td>
+						<?=htmlspecialchars($ticket->subject)?>
+					</td>
+					<td>
+						<?=htmlspecialchars($ticket->getDisplayStatus())?>
+					</td>
+					<td>
+						<?=htmlspecialchars($ticket->getDisplayAssignedTo())?>
+					</td>
+					<td>
+						<?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeUpdated()))?>
+					</td>
+					<td class="p-1">
+						<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($ticket->getUrl())?>"><?=htmlspecialchars(__("View"))?></a>
+					</td>
+				</tr>
+				<?php
+			}
+			?>
+			</tbody>
+		</table>
+		<?php
 	}
 	?>
 </section>

+ 1 - 1
views/sales.php

@@ -47,7 +47,7 @@ else {
 			?>
 			<tr>
 				<td class="text-end">
-					<?=htmlspecialchars($sale->getNumber())?>
+					<a href="<?=htmlspecialchars($sale->getUrl())?>"><?=htmlspecialchars($sale->getNumber())?></a>
 				</td>
 				<td>
 					<a href="<?=htmlspecialchars($location->getUrl())?>">

+ 31 - 1
views/ticket.php

@@ -1,7 +1,8 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
+use App\Enums\DocumentTypes;
 use App\Enums\TicketPriorities;
 use App\Enums\TicketStatuses;
 use App\Helpers\HtmlHelper;
@@ -18,6 +19,9 @@ $ticket_details = array_shift($comments);
 $attachments = $ticket_details->getAttachments();
 $creator = $ticket->getCreator();
 
+$sale = $ticket->getSale();
+$aru = $ticket->getAruDocument();
+
 $render_data_row = static function(string $title, string $html): void {
 	?>
 	<div class="row my-1">
@@ -82,6 +86,14 @@ $render_data_row = static function(string $title, string $html): void {
 			</div>
 			<div class="col-xl">
 				<?php
+
+				if($sale) {
+					$render_data_row(__("Sale No."), sprintf('<a href="%s">%s</a>', htmlspecialchars($sale->getUrl()), $sale->getNumber()));
+				}
+				if($aru) {
+					$render_data_row(__("ARU No."), sprintf('<a href="%s">%s</a>', htmlspecialchars($aru->getUrl()), $aru->number));
+				}
+
 				$render_data_row(__("Updated:"), $user->formatDateTime($ticket->getDateTimeUpdated()));
 				$render_data_row(__("Created:"), $user->formatDateTime($ticket->getDateTimeCreated()));
 				?>
@@ -119,6 +131,24 @@ $render_data_row = static function(string $title, string $html): void {
 				<i class="fa fa-fw fa-save"></i>
 				<?=htmlspecialchars(__("Update Ticket"))?>
 			</button>
+			<?php
+			if(!$aru) {
+				?>
+				<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#new-document-modal" data-type="<?=DocumentTypes::ADVANCE_REPLACEMENT_UNIT?>" data-location-id="<?=$ticket->location_id?>" data-ticket-id="<?=$ticket->id?>">
+					<i class="fa fa-fw fa-plus"></i>
+					<?=htmlspecialchars(__("Create ARU"))?>
+				</button>
+				<?php
+			}
+			else {
+				?>
+				<a class="btn btn-primary" href="<?=htmlspecialchars($aru->getUrl())?>">
+					<i class="fa fa-fw fa-magnifying-glass"></i>
+					<?=htmlspecialchars(__("View ARU"))?>
+				</a>
+				<?php
+			}
+			?>
 		</form>
 
 	</div>

+ 2 - 2
views/tickets.php

@@ -19,10 +19,10 @@ $user = $core->getUser();
 		<h3><?=htmlspecialchars(__("Tickets"))?></h3>
 	</div>
 	<div class="col-auto">
-		<a class="btn btn-success" href="/tickets/add" data-bs-toggle="modal" data-bs-target="#ticket-modal">
+		<button class="btn btn-success" type="button" data-bs-toggle="modal" data-bs-target="#ticket-modal">
 			<i class="fa fa-fw fa-plus"></i>
 			<?=htmlspecialchars(__("Add"))?>
-		</a>
+		</button>
 	</div>
 </div>