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

+ 2 - 0
boot.php

@@ -34,3 +34,5 @@ catch(Exception $e) {
 	View::include("footer");
 	View::include("footer");
 	exit();
 	exit();
 }
 }
+
+chdir($_ENV["ROOT_DIR"]);

+ 26 - 0
controllers/arus.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace App;
+
+use App\Enums\DocumentTypes;
+
+$core = Core::getInstance();
+
+$user = $core->getUser();
+$user->requireAdmin();
+
+$core->addBreadcrumb("/arus", __("ARUs"));
+
+$nvp = [
+	"type" => DocumentTypes::ADVANCE_REPLACEMENT_UNIT,
+	"dts_deleted" => null
+];
+
+$documents = Document::query($nvp, ["sort" => "id", "order" => "desc"]);
+
+$core->addScript("/js/new-document-modal.js");
+
+View::include("header");
+View::include("arus", compact("documents"));
+View::include("modals/new_document");
+View::include("footer");

+ 77 - 37
controllers/document.php

@@ -106,17 +106,26 @@ $controller = new class($document_type, $document_number) {
 		if($document->isInvoice() || $document->isDeliveryNote()) {
 		if($document->isInvoice() || $document->isDeliveryNote()) {
 			/** @var Sale $sale */
 			/** @var Sale $sale */
 			$sale = $document->getSale();
 			$sale = $document->getSale();
-			$core->addBreadcrumb("/sales", __("Sales"));
-			$core->addBreadcrumb($sale->getUrl(), __("Sale %d", $sale->getNumber()));
-
-			if($document->isInvoice()) {
-				$core->addBreadcrumb($document->getUrl(), __("Invoice %d", $document->number));
+			if($sale) {
+				$core->addBreadcrumb("/sales", __("Sales"));
+				$core->addBreadcrumb($sale->getUrl(), __("Sale %d", $sale->getNumber()));
 			}
 			}
-			if($document->isDeliveryNote()) {
-				$core->addBreadcrumb($document->getUrl(), __("Delivery Note %d", $document->number));
+
+			if($document->isPartOfAru()) {
+				/** @var Document $aru */
+				$aru = $document->getRelatedDocument();
+				$core->addBreadcrumb("/arus", __("ARUs"));
+				$core->addBreadcrumb($aru->getUrl(), $aru->getTitle());
 			}
 			}
 		}
 		}
+		else if($document->isGoodsIn()) {
+			$core->addBreadcrumb("/goods-in", __("Goods-In"));
+		}
+		else if($document->isAru()) {
+			$core->addBreadcrumb("/arus", __("ARUs"));
+		}
 
 
+		$core->addBreadcrumb($document->getUrl(), $document->getTitle());
 
 
 		$core->addScript("/js/document-item-modal.js");
 		$core->addScript("/js/document-item-modal.js");
 		$core->addScript("/js/document-page.js");
 		$core->addScript("/js/document-page.js");
@@ -136,16 +145,16 @@ $controller = new class($document_type, $document_number) {
 		$supplier_id = $this->request->int("supplier_id");
 		$supplier_id = $this->request->int("supplier_id");
 		$supplier = $supplier_id ? $this->requireSupplier($supplier_id) : null;
 		$supplier = $supplier_id ? $this->requireSupplier($supplier_id) : null;
 
 
-//		$company_id = $this->request->int("company_id");
-//		$company = $company_id ? $this->requireCompany($company_id) : null;
+		$company_id = $this->request->int("company_id");
+		$company = $company_id ? $this->requireCompany($company_id) : null;
 
 
 		$location_id = $this->request->int("location_id");
 		$location_id = $this->request->int("location_id");
 		$location = $location_id ? $this->requireLocation($location_id) : null;
 		$location = $location_id ? $this->requireLocation($location_id) : null;
 
 
 		$document = new Document();
 		$document = new Document();
+		$document->company_id = $company->id ?? null;
 		$document->user_id = $this->user->id;
 		$document->user_id = $this->user->id;
 		$document->supplier_id = $supplier->id ?? null;
 		$document->supplier_id = $supplier->id ?? null;
-//		$document->company_id = $company->id ?? null;
 		$document->location_id = $location->id ?? null;
 		$document->location_id = $location->id ?? null;
 		$document->type = $type;
 		$document->type = $type;
 		$document->number = Document::getNextNumber($type);
 		$document->number = Document::getNextNumber($type);
@@ -161,6 +170,29 @@ $controller = new class($document_type, $document_number) {
 
 
 		$document->commit();
 		$document->commit();
 
 
+		$item_description = $this->request->string("item_description");
+		$item_serial_number = $this->request->string("item_serial_number");
+
+		if($item_description) {
+			$item = new DocumentItem();
+			$item->document_id = $document->id;
+			$item->description = $item_description;
+			$item->serial_number = $item_serial_number;
+			$item->quantity = 1;
+			$item->unit_price = "0.00";
+			$item->dts_created = Database::dts();
+			$item->commit();
+		}
+
+		if($document->hasCosts() && $location) {
+			$address = $location->getAddress();
+			$document->currency = "GBP";
+			$document->tax_rate = "20.00";
+			$document->billing_address = (string) $address;
+			$document->delivery_address = (string) $address;
+			$document->commit();
+		}
+
 		JSON::success([
 		JSON::success([
 			"url" => $document->getUrl()
 			"url" => $document->getUrl()
 		]);
 		]);
@@ -252,44 +284,47 @@ $controller = new class($document_type, $document_number) {
 		Http::redirect($document->getUrl());
 		Http::redirect($document->getUrl());
 	}
 	}
 
 
-	public function createDeliveryNote(): void {
+	public function createDocumentWithItems(int $document_type): void {
 		$document_item_ids = $this->request->int("document_item_ids");
 		$document_item_ids = $this->request->int("document_item_ids");
 		if(!$document_item_ids || !is_array($document_item_ids)) {
 		if(!$document_item_ids || !is_array($document_item_ids)) {
 			throw new RuntimeException(__("No items selected."));
 			throw new RuntimeException(__("No items selected."));
 		}
 		}
 
 
-		$invoice = $this->requireDocument();
+		$source_document = $this->requireDocument();
 
 
 		$items = $this->validateItemIds($document_item_ids);
 		$items = $this->validateItemIds($document_item_ids);
 
 
-		$delivery_note = new Document();
-		$delivery_note->user_id = $this->user->id;
-		$delivery_note->sale_id = $invoice->sale_id;
-		$delivery_note->location_id = $invoice->location_id;
-		$delivery_note->related_document_id = $invoice->id;
-		$delivery_note->type = DocumentTypes::DELIVERY_NOTE;
-		$delivery_note->billing_address = $invoice->billing_address;
-		$delivery_note->delivery_address = $invoice->delivery_address;
-		$delivery_note->number = Document::getNextNumber(DocumentTypes::DELIVERY_NOTE);
-		$delivery_note->date = date("Y-m-d");
-		$delivery_note->delivery_company = $invoice->delivery_company;
-		$delivery_note->tracking_number = $invoice->tracking_number;
-		$delivery_note->dts_created = Database::dts();
-		$delivery_note->commit();
+		$document = new Document();
+		$document->company_id = $source_document->company_id;
+		$document->user_id = $this->user->id;
+		$document->sale_id = $source_document->sale_id;
+		$document->location_id = $source_document->location_id;
+		$document->related_document_id = $source_document->id;
+		$document->type = $document_type;
+		$document->currency = $source_document->currency;
+		$document->tax_rate = $source_document->tax_rate;
+		$document->billing_address = $source_document->billing_address;
+		$document->delivery_address = $source_document->delivery_address;
+		$document->number = Document::getNextNumber($document_type);
+		$document->date = date("Y-m-d");
+		$document->delivery_company = $source_document->delivery_company;
+		$document->tracking_number = $source_document->tracking_number;
+		$document->dts_created = Database::dts();
+		$document->commit();
 
 
 		foreach($items as $item) {
 		foreach($items as $item) {
-			$delivery_note_item = new DocumentItem();
-			$delivery_note_item->document_id = $delivery_note->id;
-			$delivery_note_item->description = $item->description;
-			$delivery_note_item->serial_number = $item->serial_number;
-			$delivery_note_item->quantity = $item->quantity;
-			$delivery_note_item->unit_price = $item->unit_price;
-			$delivery_note_item->dts_created = Database::dts();
-			$delivery_note_item->commit();
+			$document_item = new DocumentItem();
+			$document_item->document_id = $document->id;
+			$document_item->description = $item->description;
+			$document_item->serial_number = $item->serial_number;
+			$document_item->quantity = $item->quantity;
+			$document_item->unit_price = $item->unit_price;
+			$document_item->dts_created = Database::dts();
+			$document_item->commit();
 		}
 		}
 
 
-		Session::addAlert("success", __("Delivery note created successfully."));
-		Http::redirect($delivery_note->getUrl());
+		Session::addAlert("success", __("%s created successfully.", $document->getDisplayType()));
+		Http::redirect($document->getUrl());
 	}
 	}
 
 
 	/**
 	/**
@@ -357,6 +392,7 @@ $controller = new class($document_type, $document_number) {
 switch($action) {
 switch($action) {
 	case "create":
 	case "create":
 		$controller->create();
 		$controller->create();
+		break;
 
 
 	case "submit":
 	case "submit":
 		$controller->update();
 		$controller->update();
@@ -379,8 +415,12 @@ switch($action) {
 				$controller->deleteItems();
 				$controller->deleteItems();
 				break;
 				break;
 
 
+			case "create-invoice":
+				$controller->createDocumentWithItems(DocumentTypes::INVOICE);
+				break;
+
 			case "create-delivery-note":
 			case "create-delivery-note":
-				$controller->createDeliveryNote();
+				$controller->createDocumentWithItems(DocumentTypes::DELIVERY_NOTE);
 				break;
 				break;
 
 
 			default:
 			default:

+ 1 - 0
controllers/sale.php

@@ -257,6 +257,7 @@ $controller = new class($number) {
 
 
 		// OK
 		// OK
 		$invoice = new Document();
 		$invoice = new Document();
+		$invoice->company_id = $sale->company_id;
 		$invoice->user_id = $this->user->id;
 		$invoice->user_id = $this->user->id;
 		$invoice->sale_id = $sale->id;
 		$invoice->sale_id = $sale->id;
 		$invoice->location_id = $location->id;
 		$invoice->location_id = $location->id;

+ 13 - 0
db/migrations/20221221235212_add_company_id_field_to_document_table.php

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

+ 78 - 3
engine/Document.php

@@ -12,6 +12,7 @@ use RuntimeException;
 
 
 /**
 /**
  * @property int $id
  * @property int $id
+ * @property int $company_id Company issuing this document
  * @property int $user_id User who created this document
  * @property int $user_id User who created this document
  * @property int $sale_id Sale that this document relates to
  * @property int $sale_id Sale that this document relates to
  * @property int $supplier_id Used for goods-in documents
  * @property int $supplier_id Used for goods-in documents
@@ -39,7 +40,7 @@ class Document extends Model {
 	protected static $table = "document";
 	protected static $table = "document";
 	protected static $idField = "id";
 	protected static $idField = "id";
 	protected static $fillableFields = [
 	protected static $fillableFields = [
-		"user_id", "sale_id", "supplier_id", "location_id", "related_document_id",
+		"company_id", "user_id", "sale_id", "supplier_id", "location_id", "related_document_id",
 		"type",
 		"type",
 		"invoice_status",
 		"invoice_status",
 		"currency", "tax_rate",
 		"currency", "tax_rate",
@@ -72,6 +73,14 @@ class Document extends Model {
 
 
 	// Object methods
 	// Object methods
 
 
+	/**
+	 * @return Company|null
+	 * @throws Exception
+	 */
+	public function getCompany(): ?Company {
+		return Company::get($this->company_id);
+	}
+
 	/**
 	/**
 	 * @return Sale|null
 	 * @return Sale|null
 	 * @throws Exception
 	 * @throws Exception
@@ -102,20 +111,48 @@ class Document extends Model {
 	 */
 	 */
 	public function getUrl(): string {
 	public function getUrl(): string {
 		if($this->isInvoice()) {
 		if($this->isInvoice()) {
+
+			// Sale Invoice
+
 			/** @var Sale $sale */
 			/** @var Sale $sale */
 			$sale = $this->getSale();
 			$sale = $this->getSale();
-			return sprintf("%s/invoices/%s", $sale->getUrl(), $this->number);
+			if($sale) {
+				return sprintf("%s/invoices/%s", $sale->getUrl(), $this->number);
+			}
+
+			// ARU invoice
+			$document = $this->getRelatedDocument();
+			if($document) {
+				if($document->isAru()) {
+					return sprintf("%s/invoices/%s", $document->getUrl(), $this->number);
+				}
+			}
 		}
 		}
 
 
 		if($this->isDeliveryNote()) {
 		if($this->isDeliveryNote()) {
+			// Sale delivery note
 			$sale = $this->getSale();
 			$sale = $this->getSale();
-			return sprintf("%s/delivery-notes/%s", $sale->getUrl(), $this->number);
+			if($sale) {
+				return sprintf("%s/delivery-notes/%s", $sale->getUrl(), $this->number);
+			}
+
+			// ARU delivery note
+			$document = $this->getRelatedDocument();
+			if($document) {
+				if($document->isAru()) {
+					return sprintf("%s/delivery-notes/%s", $document->getUrl(), $this->number);
+				}
+			}
 		}
 		}
 
 
 		if($this->isGoodsIn()) {
 		if($this->isGoodsIn()) {
 			return sprintf("/goods-in/%s", $this->number);
 			return sprintf("/goods-in/%s", $this->number);
 		}
 		}
 
 
+		if($this->isAru()) {
+			return sprintf("/arus/%s", $this->number);
+		}
+
 		throw new RuntimeException(__("Unimplemented getUrl for document type: $this->type"));
 		throw new RuntimeException(__("Unimplemented getUrl for document type: $this->type"));
 	}
 	}
 
 
@@ -223,4 +260,42 @@ class Document extends Model {
 		return $this->getType() === DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
 		return $this->getType() === DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
 	}
 	}
 
 
+	public function hasCosts(): bool {
+		return ($this->isInvoice() || $this->isAru());
+	}
+
+	/**
+	 * Get delivery notes related to this invoice or ARU
+	 * @return Document[]
+	 */
+	public function getDeliveryNotes(): array {
+		return self::query([
+			"type" => DocumentTypes::DELIVERY_NOTE,
+			"related_document_id" => $this->id,
+			"dts_deleted" => null
+		]);
+	}
+
+	/**
+	 * Get invoices related to this ARU
+	 * @return array
+	 */
+	public function getInvoices(): array {
+		return self::query([
+			"type" => DocumentTypes::INVOICE,
+			"related_document_id" => $this->id,
+			"dts_deleted" => null
+		]);
+	}
+
+	/**
+	 * @return bool
+	 * @throws Exception
+	 */
+	public function isPartOfAru(): bool {
+		$related = $this->getRelatedDocument();
+		return $related && $related->isAru();
+	}
+
+
 }
 }

+ 1 - 1
engine/Enums/DocumentTypes.php

@@ -14,7 +14,7 @@ class DocumentTypes extends AbstractEnum {
 			self::INVOICE => __("Invoice"),
 			self::INVOICE => __("Invoice"),
 			self::DELIVERY_NOTE => __("Delivery Note"),
 			self::DELIVERY_NOTE => __("Delivery Note"),
 			self::GOODS_IN => __("Goods-In"),
 			self::GOODS_IN => __("Goods-In"),
-			self::ADVANCE_REPLACEMENT_UNIT => __("Advance Replacement Unit (ARU)"),
+			self::ADVANCE_REPLACEMENT_UNIT => __("ARU"),
 			self::CREDIT_NOTE => __("Credit Note"),
 			self::CREDIT_NOTE => __("Credit Note"),
 		];
 		];
 	}
 	}

+ 4 - 2
public/index.php

@@ -1,4 +1,6 @@
 <?php
 <?php
 
 
-require "../boot.php";
-require "../router.php";
+chdir("..");
+
+require "boot.php";
+require "router.php";

+ 2 - 3
public/js/document-item-modal.js

@@ -31,7 +31,7 @@ class DocumentItemModal {
 		const { item } = data;
 		const { item } = data;
 
 
 		if(item) {
 		if(item) {
-			this.$title.text("Update Invoice Item");
+			this.$title.text("Update Item");
 
 
 			this.$documentItemIdField.val(item.id);
 			this.$documentItemIdField.val(item.id);
 			this.$descriptionField.val(item.description);
 			this.$descriptionField.val(item.description);
@@ -40,9 +40,8 @@ class DocumentItemModal {
 			this.$unitPriceField.val(item.unitPrice);
 			this.$unitPriceField.val(item.unitPrice);
 		}
 		}
 		else {
 		else {
-			this.$title.text("Add Invoice Item");
+			this.$title.text("Add Item");
 		}
 		}
-
 	}
 	}
 
 
 	onShown() {
 	onShown() {

+ 9 - 0
public/js/new-document-modal.js

@@ -16,6 +16,10 @@ class NewDocumentModal {
 		this.$locationDropdown = this.$form.find("[name=location_id]");
 		this.$locationDropdown = this.$form.find("[name=location_id]");
 		this.$supplierDropdown = this.$form.find("[name=supplier_id]");
 		this.$supplierDropdown = this.$form.find("[name=supplier_id]");
 
 
+		this.$aruSection = $("#new-document-aru-section");
+		this.$itemDescriptionField = this.$form.find("[name=item_description]");
+		this.$itemSerialNumberField = this.$form.find("[name=item_serial_number]");
+
 	}
 	}
 
 
 	// Modal lifecycle
 	// Modal lifecycle
@@ -25,15 +29,20 @@ class NewDocumentModal {
 
 
 		const { type } = data;
 		const { type } = data;
 
 
+		this.$supplierDropdown.closest(".mb-3").show();
+		this.$aruSection.show();
+
 		switch(type) {
 		switch(type) {
 			case 2: // Goods-In
 			case 2: // Goods-In
 				this.$title.text("New Goods-In");
 				this.$title.text("New Goods-In");
 				this.baseUrl = "/goods-in";
 				this.baseUrl = "/goods-in";
+				this.$aruSection.hide();
 				break;
 				break;
 
 
 			case 3: // ARU
 			case 3: // ARU
 				this.$title.text("New ARU");
 				this.$title.text("New ARU");
 				this.baseUrl = "/arus";
 				this.baseUrl = "/arus";
+				this.$supplierDropdown.closest(".mb-3").hide();
 				break;
 				break;
 		}
 		}
 	}
 	}

+ 8 - 6
routes/default.php

@@ -50,9 +50,9 @@ $this->addRoute("/^sales\/(submit)$/", "sale", ["action"], ["requireUser" => tru
 $this->addRoute("/^sales\/(\d+)$/", "sale", ["number"], ["requireUser" => true]);
 $this->addRoute("/^sales\/(\d+)$/", "sale", ["number"], ["requireUser" => true]);
 $this->addRoute("/^sales\/(\d+)\/(submit|submit-note|delete-note|submit-item|delete-item|items-batch-action)$/", "sale", ["number", "action"], ["requireUser" => true]);
 $this->addRoute("/^sales\/(\d+)\/(submit|submit-note|delete-note|submit-item|delete-item|items-batch-action)$/", "sale", ["number", "action"], ["requireUser" => true]);
 
 
-$this->addRoute("/^sales\/\d+\/(invoices|delivery-notes)\/(\d+)$/", "document", ["document_type_string", "document_number"], ["requireUser" => true]);
-$this->addRoute("/^sales\/\d+\/(invoices|delivery-notes)\/(\d+)\/(submit|submit-item|delete-item|items-batch-action|download-pdf)$/", "document", ["document_type_string", "document_number", "action"], ["requireUser" => true]);
-$this->addRoute("/^sales\/\d+\/(invoices|delivery-notes)\/(\d+)\/(render-pdf)$/", "document", ["document_type_string", "document_number", "action"]);
+$this->addRoute("/^(?:sales|arus)\/\d+\/(invoices|delivery-notes)\/(\d+)$/", "document", ["document_type_string", "document_number"], ["requireUser" => true]);
+$this->addRoute("/^(?:sales|arus)\/\d+\/(invoices|delivery-notes)\/(\d+)\/(submit|submit-item|delete-item|items-batch-action|download-pdf)$/", "document", ["document_type_string", "document_number", "action"], ["requireUser" => true]);
+$this->addRoute("/^(?:sales|arus)\/\d+\/(invoices|delivery-notes)\/(\d+)\/(render-pdf)$/", "document", ["document_type_string", "document_number", "action"]);
 
 
 $this->addRoute("/^tickets$/", "tickets", [], ["requireUser" => true]);
 $this->addRoute("/^tickets$/", "tickets", [], ["requireUser" => true]);
 $this->addRoute("/^tickets\/(create)$/", "tickets_ajax", ["action"], ["requireUser" => true]);
 $this->addRoute("/^tickets\/(create)$/", "tickets_ajax", ["action"], ["requireUser" => true]);
@@ -61,9 +61,11 @@ $this->addRoute("/^tickets\/(\d+)$/", "ticket", ["id"], ["requireUser" => true])
 $this->addRoute("/^(goods-in|arus)\/(create)$/", "document", ["document_type_string", "action"], ["requireUser" => true]);
 $this->addRoute("/^(goods-in|arus)\/(create)$/", "document", ["document_type_string", "action"], ["requireUser" => true]);
 
 
 $this->addRoute("/^goods-in$/", "goods_in", [], ["requireUser" => true]);
 $this->addRoute("/^goods-in$/", "goods_in", [], ["requireUser" => true]);
-$this->addRoute("/^(goods-in)\/(\d+)$/", "document", ["document_type_string", "document_number"], ["requireUser" => true]);
-$this->addRoute("/^(goods-in)\/(\d+)\/(submit|submit-item|delete-item|items-batch-action|download-pdf)$/", "document", ["document_type_string", "document_number", "action"], ["requireUser" => true]);
-$this->addRoute("/^(goods-in)\/(\d+)\/(render-pdf)$/", "document", ["document_type_string", "document_number", "action"]);
+$this->addRoute("/^arus$/", "arus", [], ["requireUser" => true]);
+
+$this->addRoute("/^(goods-in|arus)\/(\d+)$/", "document", ["document_type_string", "document_number"], ["requireUser" => true]);
+$this->addRoute("/^(goods-in|arus)\/(\d+)\/(submit|submit-item|delete-item|items-batch-action|download-pdf)$/", "document", ["document_type_string", "document_number", "action"], ["requireUser" => true]);
+$this->addRoute("/^(goods-in|arus)\/(\d+)\/(render-pdf)$/", "document", ["document_type_string", "document_number", "action"]);
 
 
 $this->addRoute("/^download-file\/(ticket-comment-attachment)\/(\d+)$/", "download_file", ["type", "id"], ["requireUser" => true]);
 $this->addRoute("/^download-file\/(ticket-comment-attachment)\/(\d+)$/", "download_file", ["type", "id"], ["requireUser" => true]);
 
 

+ 74 - 0
views/arus.php

@@ -0,0 +1,74 @@
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+namespace App;
+
+use App\Enums\DocumentTypes;
+
+/**
+ * @var Document[] $documents
+ */
+
+?>
+<div class="row mb-3">
+	<div class="col">
+		<h3><?=htmlspecialchars(__("ARUs"))?></h3>
+	</div>
+	<div class="col-auto">
+		<a class="btn btn-success" data-bs-toggle="modal" data-bs-target="#new-document-modal" data-type="<?=DocumentTypes::ADVANCE_REPLACEMENT_UNIT?>">
+			<i class="fa fa-fw fa-plus"></i>
+			<?=htmlspecialchars(__("Add"))?>
+		</a>
+	</div>
+</div>
+
+<?php
+
+if(empty($documents)) {
+	?>
+	<div class="alert alert-info">
+		<i class="fa fa-fw fa-info-circle"></i>
+		<?=htmlspecialchars(__("No ARU documents to display."))?>
+	</div>
+	<?php
+}
+else {
+	?>
+	<table class="table table-bordered table-hover">
+		<thead>
+		<tr>
+			<th><?=htmlspecialchars(__("Number"))?></th>
+			<th><?=htmlspecialchars(__("Location"))?></th>
+			<th><?=htmlspecialchars(__("Items"))?></th>
+			<th><?=htmlspecialchars(__("Actions"))?></th>
+		</tr>
+		</thead>
+		<tbody>
+		<?php
+		foreach($documents as $document) {
+			$location = $document->getLocation();
+			?>
+			<tr>
+				<td class="text-right">
+					<?=htmlspecialchars($document->number)?>
+				</td>
+				<td>
+					<a href="<?=htmlspecialchars($location->getUrl())?>">
+						<?=htmlspecialchars($location->getDisplayName())?>
+					</a>
+				</td>
+				<td>
+					Items
+				</td>
+				<td class="p-1">
+					<a class="btn btn-sm btn-primary" href="<?=htmlspecialchars($document->getUrl())?>">
+						<?=htmlspecialchars(__("View"))?>
+					</a>
+				</td>
+			</tr>
+			<?php
+		}
+		?>
+		</tbody>
+	</table>
+	<?php
+}

+ 46 - 18
views/document.php

@@ -16,12 +16,25 @@ use NumberFormatter;
 $core = Core::getInstance();
 $core = Core::getInstance();
 $user = $core->getUser();
 $user = $core->getUser();
 
 
+$company = $document->getCompany();
 $sale = $document->getSale();
 $sale = $document->getSale();
 $related_document = $document->getRelatedDocument();
 $related_document = $document->getRelatedDocument();
 
 
 ?>
 ?>
 <section class="mb-4">
 <section class="mb-4">
-	<h3><?=htmlspecialchars($document->getTitle())?></h3>
+	<h3>
+		<?php
+		if($related_document) {
+			echo __("%s for %s",
+				htmlspecialchars($document->getTitle()),
+				"<a href=\"" . htmlspecialchars($related_document->getUrl()) . "\">" . htmlspecialchars($related_document->getTitle()) . "</a>"
+			);
+		}
+		else {
+			echo htmlspecialchars($document->getTitle());
+		}
+		?>
+	</h3>
 	<form action="<?=htmlspecialchars($document->getUrl())?>/submit" method="post">
 	<form action="<?=htmlspecialchars($document->getUrl())?>/submit" method="post">
 
 
 		<div class="row">
 		<div class="row">
@@ -63,7 +76,7 @@ $related_document = $document->getRelatedDocument();
 				</div>
 				</div>
 
 
 				<?php
 				<?php
-				if($document->isInvoice()) {
+				if($document->hasCosts()) {
 					?>
 					?>
 					<div class="row">
 					<div class="row">
 						<div class="col">
 						<div class="col">
@@ -90,7 +103,7 @@ $related_document = $document->getRelatedDocument();
 					<?php
 					<?php
 				}
 				}
 
 
-				if($document->isInvoice() || $document->isDeliveryNote()) {
+				if($document->hasCosts() || $document->isDeliveryNote()) {
 					?>
 					?>
 					<div class="row">
 					<div class="row">
 						<div class="col-sm-6">
 						<div class="col-sm-6">
@@ -189,6 +202,15 @@ $related_document = $document->getRelatedDocument();
 				</div>
 				</div>
 
 
 				<dl>
 				<dl>
+					<?php
+					if($company) {
+						?>
+						<dt><?=htmlspecialchars(__("Company"))?></dt>
+						<dd><?=htmlspecialchars($company->name)?></dd>
+						<?php
+					}
+					?>
+
 					<dt><?=htmlspecialchars(__("Created By"))?></dt>
 					<dt><?=htmlspecialchars(__("Created By"))?></dt>
 					<dd><?=htmlspecialchars($document->getUser()->getDisplayName())?></dd>
 					<dd><?=htmlspecialchars($document->getUser()->getDisplayName())?></dd>
 
 
@@ -207,22 +229,9 @@ $related_document = $document->getRelatedDocument();
 						<?php
 						<?php
 					}
 					}
 
 
-					if($related_document) {
-						?>
-						<dt><?=htmlspecialchars(__("Related Document"))?></dt>
-						<dd>
-							<a href="<?=htmlspecialchars($related_document->getUrl())?>">
-								<?=htmlspecialchars($related_document->getTitle())?>
-							</a>
-						</dd>
-						<?php
-					}
+					if($document->hasCosts()) {
+						$delivery_notes = $document->getDeliveryNotes();
 
 
-					if($document->isInvoice()) {
-						$delivery_notes = Document::query([
-							"type" => DocumentTypes::DELIVERY_NOTE,
-							"related_document_id" => $document->id
-						]);
 						if($delivery_notes) {
 						if($delivery_notes) {
 							?>
 							?>
 							<dt><?=htmlspecialchars(__("Delivery Notes"))?></dt>
 							<dt><?=htmlspecialchars(__("Delivery Notes"))?></dt>
@@ -239,6 +248,25 @@ $related_document = $document->getRelatedDocument();
 							</dd>
 							</dd>
 							<?php
 							<?php
 						}
 						}
+
+						$invoices = $document->getInvoices();
+
+						if($invoices) {
+							?>
+							<dt><?=htmlspecialchars(__("Invoices"))?></dt>
+							<dd>
+								<?php
+								foreach($invoices as $invoice) {
+									?>
+									<a href="<?=htmlspecialchars($invoice->getUrl())?>">
+										<?=htmlspecialchars($invoice->getTitle())?>
+									</a><br />
+									<?php
+								}
+								?>
+							</dd>
+							<?php
+						}
 					}
 					}
 					?>
 					?>
 				</dl>
 				</dl>

+ 27 - 6
views/document_items_section.php

@@ -10,12 +10,24 @@ use NumberFormatter;
 
 
 $items = $document->getItems();
 $items = $document->getItems();
 
 
+$title = __("Items");
+
+if($document->isInvoice()) {
+	$title = __("Invoice Items");
+}
+if($document->isDeliveryNote()) {
+	$title = __("Delivery Items");
+}
+if($document->isAru()) {
+	$title = __("Return Items");
+}
+
 ?>
 ?>
 <section class="mb-4" id="document-items-section">
 <section class="mb-4" id="document-items-section">
 
 
 	<div class="row mb-3">
 	<div class="row mb-3">
 		<div class="col">
 		<div class="col">
-			<h3><?=htmlspecialchars(__("Items"))?></h3>
+			<h3><?=htmlspecialchars($title)?></h3>
 		</div>
 		</div>
 		<div class="col-auto">
 		<div class="col-auto">
 			<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#document-item-modal">
 			<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#document-item-modal">
@@ -40,7 +52,7 @@ $items = $document->getItems();
 					<col style="width: 20%"/>
 					<col style="width: 20%"/>
 					<col style="width: 15%"/>
 					<col style="width: 15%"/>
 					<?php
 					<?php
-					if($document->isInvoice()) {
+					if($document->hasCosts()) {
 						?>
 						?>
 						<col style="width: 15%"/>
 						<col style="width: 15%"/>
 						<col style="width: 15%"/>
 						<col style="width: 15%"/>
@@ -59,7 +71,7 @@ $items = $document->getItems();
 					<th><?=htmlspecialchars(__("Serial Number"))?></th>
 					<th><?=htmlspecialchars(__("Serial Number"))?></th>
 					<th><?=htmlspecialchars(__("Quantity"))?></th>
 					<th><?=htmlspecialchars(__("Quantity"))?></th>
 					<?php
 					<?php
-					if($document->isInvoice()) {
+					if($document->hasCosts()) {
 						?>
 						?>
 						<th><?=htmlspecialchars(__("Unit Price"))?></th>
 						<th><?=htmlspecialchars(__("Unit Price"))?></th>
 						<th><?=htmlspecialchars(__("Amount"))?></th>
 						<th><?=htmlspecialchars(__("Amount"))?></th>
@@ -92,7 +104,7 @@ $items = $document->getItems();
 							<?=$item->quantity?>
 							<?=$item->quantity?>
 						</td>
 						</td>
 						<?php
 						<?php
-						if($document->isInvoice()) {
+						if($document->hasCosts()) {
 							?>
 							?>
 							<td class="text-end">
 							<td class="text-end">
 								<?=htmlspecialchars($formatter->formatCurrency($item->unit_price, $currency))?>
 								<?=htmlspecialchars($formatter->formatCurrency($item->unit_price, $currency))?>
@@ -118,7 +130,7 @@ $items = $document->getItems();
 				?>
 				?>
 				</tbody>
 				</tbody>
 				<?php
 				<?php
-				if($document->isInvoice()) {
+				if($document->hasCosts()) {
 					$tax = bcmul($subtotal, $document->tax_rate / 100, 2);
 					$tax = bcmul($subtotal, $document->tax_rate / 100, 2);
 					$total = bcadd($subtotal, $tax, 2);
 					$total = bcadd($subtotal, $tax, 2);
 					?>
 					?>
@@ -160,7 +172,16 @@ $items = $document->getItems();
 			</div>
 			</div>
 
 
 			<?php
 			<?php
-			if($document->isInvoice()) {
+			if($document->isAru()) {
+				?>
+				<button type="submit" class="btn btn-primary" name="batch_action" value="create-invoice">
+					<i class="fa fa-fw fa-file-invoice"></i>
+					<?=htmlspecialchars(__("Create Invoice"))?>
+				</button>
+				<?php
+			}
+
+			if($document->hasCosts() && !$document->isPartOfAru()) {
 				?>
 				?>
 				<button type="submit" class="btn btn-primary" name="batch_action" value="create-delivery-note">
 				<button type="submit" class="btn btn-primary" name="batch_action" value="create-delivery-note">
 					<i class="fa fa-fw fa-file-invoice"></i>
 					<i class="fa fa-fw fa-file-invoice"></i>

+ 24 - 8
views/document_pdf.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 namespace App;
 namespace App;
 
 
 use App\Enums\DocumentTypes;
 use App\Enums\DocumentTypes;
@@ -46,8 +46,14 @@ if($document->isGoodsIn()) {
 	$background_colour = "#ff7043";
 	$background_colour = "#ff7043";
 }
 }
 
 
+$company = $document->getCompany();
 $sale = $document->getSale();
 $sale = $document->getSale();
-$company = $sale ? $sale->getCompany() : null;
+
+if(!$company && $sale) {
+	$company = $sale->getCompany();
+}
+
+$is_part_of_aru = $document->isPartOfAru();
 
 
 ?>
 ?>
 <!doctype html>
 <!doctype html>
@@ -208,17 +214,27 @@ $company = $sale ? $sale->getCompany() : null;
 						$render_card("Terms", $document->terms ?? " ");
 						$render_card("Terms", $document->terms ?? " ");
 					}
 					}
 
 
-					if($document->isInvoice() || $document->isDeliveryNote()) {
-						$render_card("Client PO No.", $document->po_number);
+					if($is_part_of_aru) {
+						$aru = $document->getRelatedDocument();
+						$render_card("ARU No.", $aru->number);
 					}
 					}
 
 
-					if($document->isInvoice()) {
+					if(!$is_part_of_aru) {
+						if($document->isInvoice() || $document->isDeliveryNote()) {
+							$render_card("Client PO No.", $document->po_number);
+						}
+					}
+
+					if($document->isInvoice() && !$is_part_of_aru) {
 						$render_card("Delivery Note No.", $delivery_note_number);
 						$render_card("Delivery Note No.", $delivery_note_number);
 					}
 					}
 
 
 					if($document->isInvoice()) {
 					if($document->isInvoice()) {
 						$render_card("Currency", $document->currency);
 						$render_card("Currency", $document->currency);
-						$render_card("VAT No.", $company->vat_number);
+
+						if($company) {
+							$render_card("VAT No.", $company->vat_number);
+						}
 					}
 					}
 
 
 					?>
 					?>
@@ -300,7 +316,7 @@ $company = $sale ? $sale->getCompany() : null;
 				$tax = bcmul($subtotal, $document->tax_rate / 100, 2);
 				$tax = bcmul($subtotal, $document->tax_rate / 100, 2);
 				$total = bcadd($subtotal, $tax, 2);
 				$total = bcadd($subtotal, $tax, 2);
 
 
-				$bank_details = $company->getBankDetailsForCurrency($document->currency);
+				$bank_details = $company ? $company->getBankDetailsForCurrency($document->currency) : null;
 
 
 				?>
 				?>
 				<tr>
 				<tr>
@@ -330,7 +346,7 @@ $company = $sale ? $sale->getCompany() : null;
 							<strong>IBAN:</strong> <?=htmlspecialchars($bank_details->bank_account_iban ?? "")?>
 							<strong>IBAN:</strong> <?=htmlspecialchars($bank_details->bank_account_iban ?? "")?>
 						</div>
 						</div>
 						<div>
 						<div>
-							<strong>Company Reg. No.:</strong> <?=htmlspecialchars($company->company_number)?>
+							<strong>Company Reg. No.:</strong> <?=htmlspecialchars($company->company_number ?? "")?>
 						</div>
 						</div>
 					</td>
 					</td>
 					<td class="text-uppercase" colspan="2">Subtotal</td>
 					<td class="text-uppercase" colspan="2">Subtotal</td>

+ 9 - 5
views/header.php

@@ -32,14 +32,18 @@ $google_api_key = $_ENV["GOOGLE_API_KEY"] ?? null;
 		<script src="https://maps.googleapis.com/maps/api/js?key=<?=htmlspecialchars($google_api_key)?>"></script>
 		<script src="https://maps.googleapis.com/maps/api/js?key=<?=htmlspecialchars($google_api_key)?>"></script>
 		<?php
 		<?php
 	}
 	}
-	?>
 
 
-	<script src="/js/app.js"></script>
+	$scripts = $core->getGlobalScripts();
+	array_unshift($scripts, "/js/app.js");
 
 
-	<?php
-	foreach($core->getGlobalScripts() as $script) {
+	foreach($scripts as $script) {
+		$time_modified = 0;
+		$is_relative = strpos($script, "/") === 0;
+		if($is_relative) {
+			$time_modified = filemtime("public" . $script);
+		}
 		?>
 		?>
-		<script src="<?=htmlspecialchars($script)?>"></script>
+		<script src="<?=htmlspecialchars($script)?>?t=<?=$time_modified?>"></script>
 		<?php
 		<?php
 	}
 	}
 	?>
 	?>

+ 5 - 5
views/modals/document_item.php

@@ -10,27 +10,27 @@ namespace App;
 			<input type="hidden" name="document_item_id" value="" />
 			<input type="hidden" name="document_item_id" value="" />
 
 
 			<div class="modal-body">
 			<div class="modal-body">
-				<div class="mb-2">
+				<div class="mb-3">
 					<label for="document-item-description" class="form-label">
 					<label for="document-item-description" class="form-label">
 						<?=htmlspecialchars(__("Description"))?>
 						<?=htmlspecialchars(__("Description"))?>
 					</label>
 					</label>
 
 
 					<input class="form-control" type="text" name="description" id="document-item-description" placeholder="<?=htmlspecialchars(__("Type description"))?>" maxlength="255" required />
 					<input class="form-control" type="text" name="description" id="document-item-description" placeholder="<?=htmlspecialchars(__("Type description"))?>" maxlength="255" required />
 				</div>
 				</div>
-				<div class="mb-2">
+				<div class="mb-3">
 					<label for="document-item-serial-number" class="form-label">
 					<label for="document-item-serial-number" class="form-label">
-						<?=htmlspecialchars(__("Description"))?>
+						<?=htmlspecialchars(__("Serial Number"))?>
 					</label>
 					</label>
 
 
 					<input class="form-control" type="text" name="serial_number" id="document-item-serial-number" placeholder="<?=htmlspecialchars(__("Type serial number"))?>" maxlength="255" />
 					<input class="form-control" type="text" name="serial_number" id="document-item-serial-number" placeholder="<?=htmlspecialchars(__("Type serial number"))?>" maxlength="255" />
 				</div>
 				</div>
-				<div class="mb-2">
+				<div class="mb-3">
 					<label for="sale-item-quantity" class="form-label">
 					<label for="sale-item-quantity" class="form-label">
 						<?=htmlspecialchars(__("Quantity"))?>
 						<?=htmlspecialchars(__("Quantity"))?>
 					</label>
 					</label>
 					<input class="form-control" type="number" name="quantity" id="sale-item-quantity" min="0" value="1" placeholder="<?=htmlspecialchars(__("Type quantity"))?>" required />
 					<input class="form-control" type="number" name="quantity" id="sale-item-quantity" min="0" value="1" placeholder="<?=htmlspecialchars(__("Type quantity"))?>" required />
 				</div>
 				</div>
-				<div class="mb-2">
+				<div class="mb-3">
 					<label for="sale-item-unit-price" class="form-label">
 					<label for="sale-item-unit-price" class="form-label">
 						<?=htmlspecialchars(__("Unit Price"))?>
 						<?=htmlspecialchars(__("Unit Price"))?>
 					</label>
 					</label>

+ 39 - 2
views/modals/new_document.php

@@ -10,6 +10,22 @@ namespace App;
 			?>
 			?>
 			<input type="hidden" name="type" value="" />
 			<input type="hidden" name="type" value="" />
 			<div class="modal-body">
 			<div class="modal-body">
+				<div class="mb-3">
+					<label class="form-label" for="new-document-company-id"><?= htmlspecialchars(__("Company")) ?></label>
+					<select class="form-control" name="company_id" id="new-document-company-id">
+						<option value=""><?=htmlspecialchars(__("Select Company"))?></option>
+						<?php
+						foreach(Company::getAll() as $company) {
+							?>
+							<option value="<?=$company->id?>">
+								<?=htmlspecialchars($company->name)?>
+							</option>
+							<?php
+						}
+						?>
+					</select>
+				</div>
+
 				<div class="mb-3">
 				<div class="mb-3">
 					<label class="form-label" for="new-document-supplier-id"><?= htmlspecialchars(__("Supplier")) ?></label>
 					<label class="form-label" for="new-document-supplier-id"><?= htmlspecialchars(__("Supplier")) ?></label>
 					<select class="form-control" name="supplier_id" id="new-document-supplier-id">
 					<select class="form-control" name="supplier_id" id="new-document-supplier-id">
@@ -27,8 +43,8 @@ namespace App;
 				</div>
 				</div>
 
 
 				<div class="mb-3">
 				<div class="mb-3">
-					<label class="form-label" for="location-id"><?= htmlspecialchars(__("Location")) ?></label>
-					<select class="form-control" name="location_id" id="location-id">
+					<label class="form-label" for="new-document-location-id"><?= htmlspecialchars(__("Location")) ?></label>
+					<select class="form-control" name="location_id" id="new-document-location-id">
 						<option value=""><?=htmlspecialchars(__("Select Location"))?></option>
 						<option value=""><?=htmlspecialchars(__("Select Location"))?></option>
 						<?php
 						<?php
 						foreach(Customer::getAll() as $customer) {
 						foreach(Customer::getAll() as $customer) {
@@ -50,6 +66,27 @@ namespace App;
 					</select>
 					</select>
 				</div>
 				</div>
 
 
+				<section id="new-document-aru-section">
+					<h5><?=htmlspecialchars(__("ARU Item"))?></h5>
+
+					<p><?=htmlspecialchars(__("Enter details of the part that is being returned if known."))?></p>
+
+					<div class="mb-3">
+						<label class="form-label" for="new-document-item-description">
+							<?=htmlspecialchars(__("Description"))?>
+						</label>
+						<input class="form-control" type="text" name="item_description" id="new-document-item-description" maxlength="255" placeholder="<?=htmlspecialchars(__("Type description"))?>" />
+					</div>
+
+					<div class="mb-3">
+						<label class="form-label" for="new-document-item-serial-number">
+							<?=htmlspecialchars(__("Serial Number"))?>
+						</label>
+						<input class="form-control" type="text" name="item_serial_number" id="new-document-item-serial-number" maxlength="255" placeholder="<?=htmlspecialchars(__("Type serial number"))?>" />
+					</div>
+
+				</section>
+
 			</div>
 			</div>
 			<div class="modal-footer">
 			<div class="modal-footer">
 				<button type="submit" class="btn btn-success">
 				<button type="submit" class="btn btn-success">