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

Add ability to create sales orders without a lead

Alan Lam 3 лет назад
Родитель
Сommit
36c432db69
6 измененных файлов с 40 добавлено и 57 удалено
  1. 4 21
      controllers/document.php
  2. 2 29
      engine/Document.php
  3. 18 0
      engine/Enums/DocumentTypes.php
  4. 11 2
      public/js/new-document-modal.js
  5. 4 4
      routes/default.php
  6. 1 1
      views/documents.php

+ 4 - 21
controllers/document.php

@@ -16,27 +16,7 @@ use RuntimeException;
 
 
 $action = $action ?? null;
 $action = $action ?? null;
 $document_number = $document_number ?? null;
 $document_number = $document_number ?? null;
-
-switch($document_type_string) {
-	case "sales-orders":
-		$document_type = DocumentTypes::SALES_ORDER;
-		break;
-
-	case "delivery-notes":
-		$document_type = DocumentTypes::DELIVERY_NOTE;
-		break;
-
-	case "goods-in":
-		$document_type = DocumentTypes::GOODS_IN;
-		break;
-
-	case "arus":
-		$document_type = DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
-		break;
-
-	default:
-		throw new RuntimeException(__("Invalid document type."));
-}
+$document_type = DocumentTypes::fromRouteName($document_type_string);
 
 
 $request = Request::fromRequest();
 $request = Request::fromRequest();
 
 
@@ -171,6 +151,9 @@ $controller = new class($document_type, $document_number) {
 		$document->date = date("Y-m-d");
 		$document->date = date("Y-m-d");
 		$document->dts_created = Database::dts();
 		$document->dts_created = Database::dts();
 
 
+		if($document->isSalesOrder() && !$document->location_id) {
+			throw new RuntimeException(__("Location must be selected for a Sales Order document."));
+		}
 		if($document->isGoodsIn() && !$document->supplier_id && !$document->location_id) {
 		if($document->isGoodsIn() && !$document->supplier_id && !$document->location_id) {
 			throw new RuntimeException(__("Supplier or location must be selected for a Goods-In document."));
 			throw new RuntimeException(__("Supplier or location must be selected for a Goods-In document."));
 		}
 		}

+ 2 - 29
engine/Document.php

@@ -150,38 +150,11 @@ class Document extends Model {
 	 */
 	 */
 	public function getUrl(): string {
 	public function getUrl(): string {
 		if($this->isSalesOrder()) {
 		if($this->isSalesOrder()) {
-
-			// Lead Sales Order
-
-			/** @var Sale $sale */
-			$sale = $this->getSale();
-			if($sale) {
-				return sprintf("%s/sales-orders/%s", $sale->getUrl(), $this->number);
-			}
-
-			// ARU Sales Order
-			$document = $this->getRelatedDocument();
-			if($document) {
-				if($document->isAru()) {
-					return sprintf("%s/sales-orders/%s", $document->getUrl(), $this->number);
-				}
-			}
+			return sprintf("/sales-orders/%s", $this->number);
 		}
 		}
 
 
 		if($this->isDeliveryNote()) {
 		if($this->isDeliveryNote()) {
-			// Sale delivery note
-			$sale = $this->getSale();
-			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);
-				}
-			}
+			return sprintf("/delivery-notes/%s", $this->number);
 		}
 		}
 
 
 		if($this->isGoodsIn()) {
 		if($this->isGoodsIn()) {

+ 18 - 0
engine/Enums/DocumentTypes.php

@@ -2,6 +2,8 @@
 
 
 namespace App\Enums;
 namespace App\Enums;
 
 
+use RuntimeException;
+
 class DocumentTypes extends AbstractEnum {
 class DocumentTypes extends AbstractEnum {
 
 
 	public const SALES_ORDER = 0;
 	public const SALES_ORDER = 0;
@@ -9,6 +11,7 @@ class DocumentTypes extends AbstractEnum {
 	public const GOODS_IN = 2;
 	public const GOODS_IN = 2;
 	public const ADVANCE_REPLACEMENT_UNIT = 3;
 	public const ADVANCE_REPLACEMENT_UNIT = 3;
 	public const CREDIT_NOTE = 4;
 	public const CREDIT_NOTE = 4;
+
 	public static function options(): array {
 	public static function options(): array {
 		return [
 		return [
 			self::SALES_ORDER => __("Sales Order"),
 			self::SALES_ORDER => __("Sales Order"),
@@ -18,4 +21,19 @@ class DocumentTypes extends AbstractEnum {
 			self::CREDIT_NOTE => __("Credit Note"),
 			self::CREDIT_NOTE => __("Credit Note"),
 		];
 		];
 	}
 	}
+
+	public static function fromRouteName(string $name): string {
+		$map = [
+			"sales-orders" => self::SALES_ORDER,
+			"delivery-notes" => self::DELIVERY_NOTE,
+			"goods-in" => self::GOODS_IN,
+			"arus" => self::ADVANCE_REPLACEMENT_UNIT,
+		];
+
+		if(!isset($map[$name])) {
+			throw new RuntimeException(__("Invalid document type."));
+		}
+
+		return $map[$name];
+	}
 }
 }

+ 11 - 2
public/js/new-document-modal.js

@@ -16,7 +16,9 @@ class NewDocumentModal {
 
 
 		this.$companyDropdown = this.getField("company_id");
 		this.$companyDropdown = this.getField("company_id");
 		this.$locationDropdown = this.getField("location_id");
 		this.$locationDropdown = this.getField("location_id");
+
 		this.$supplierDropdown = this.getField("supplier_id");
 		this.$supplierDropdown = this.getField("supplier_id");
+		this.$supplierFormGroup = this.$supplierDropdown.closest(".mb-3");
 
 
 		this.$aruSection = $("#new-document-aru-section");
 		this.$aruSection = $("#new-document-aru-section");
 		this.$itemDescriptionField = this.getField("item_description");
 		this.$itemDescriptionField = this.getField("item_description");
@@ -39,10 +41,17 @@ class NewDocumentModal {
 
 
 		const { type, locationId, ticketId } = data;
 		const { type, locationId, ticketId } = data;
 
 
-		this.$supplierDropdown.closest(".mb-3").show();
+		this.$supplierFormGroup.show();
 		this.$aruSection.show();
 		this.$aruSection.show();
 
 
 		switch(type) {
 		switch(type) {
+			case DocumentTypes.SalesOrder:
+				this.$title.text("New Sales Order");
+				this.baseUrl = "/sales-orders";
+				this.$aruSection.hide();
+				this.$supplierFormGroup.hide();
+				break;
+
 			case DocumentTypes.GoodsIn:
 			case DocumentTypes.GoodsIn:
 				this.$title.text("New Goods-In");
 				this.$title.text("New Goods-In");
 				this.baseUrl = "/goods-in";
 				this.baseUrl = "/goods-in";
@@ -52,7 +61,7 @@ class NewDocumentModal {
 			case DocumentTypes.AdvancedReplacementUnit:
 			case DocumentTypes.AdvancedReplacementUnit:
 				this.$title.text("New ARU");
 				this.$title.text("New ARU");
 				this.baseUrl = "/arus";
 				this.baseUrl = "/arus";
-				this.$supplierDropdown.closest(".mb-3").hide();
+				this.$supplierFormGroup.hide();
 				break;
 				break;
 		}
 		}
 
 

+ 4 - 4
routes/default.php

@@ -57,15 +57,15 @@ $this->addRoute("/^leads\/(submit)$/", "sale", ["action"], ["requireUser" => tru
 $this->addRoute("/^leads\/(\d+)$/", "sale", ["number"], ["requireUser" => true]);
 $this->addRoute("/^leads\/(\d+)$/", "sale", ["number"], ["requireUser" => true]);
 $this->addRoute("/^leads\/(\d+)\/(submit|submit-note|delete-note|submit-item|delete-item|items-batch-action)$/", "sale", ["number", "action"], ["requireUser" => true]);
 $this->addRoute("/^leads\/(\d+)\/(submit|submit-note|delete-note|submit-item|delete-item|items-batch-action)$/", "sale", ["number", "action"], ["requireUser" => true]);
 
 
-$this->addRoute("/^(?:leads|arus)\/\d+\/(sales-orders|delivery-notes)\/(\d+)$/", "document", ["document_type_string", "document_number"], ["requireUser" => true]);
-$this->addRoute("/^(?:leads|arus)\/\d+\/(sales-orders|delivery-notes)\/(\d+)\/(submit|submit-item|delete-item|items-batch-action|download-pdf|email-pdf)$/", "document", ["document_type_string", "document_number", "action"], ["requireUser" => true]);
-$this->addRoute("/^(?:leads|arus)\/\d+\/(sales-orders|delivery-notes)\/(\d+)\/(render-pdf)$/", "document", ["document_type_string", "document_number", "action"]);
+$this->addRoute("/^(sales-orders|delivery-notes)\/(\d+)$/", "document", ["document_type_string", "document_number"], ["requireUser" => true]);
+$this->addRoute("/^(sales-orders|delivery-notes)\/(\d+)\/(submit|submit-item|delete-item|items-batch-action|download-pdf|email-pdf)$/", "document", ["document_type_string", "document_number", "action"], ["requireUser" => true]);
+$this->addRoute("/^(sales-orders|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]);
 $this->addRoute("/^tickets\/(\d+)$/", "ticket", ["id"], ["requireUser" => true]);
 $this->addRoute("/^tickets\/(\d+)$/", "ticket", ["id"], ["requireUser" => true]);
 
 
-$this->addRoute("/^(goods-in|arus)\/(create)$/", "document", ["document_type_string", "action"], ["requireUser" => true]);
+$this->addRoute("/^(sales-orders|goods-in|arus)\/(create)$/", "document", ["document_type_string", "action"], ["requireUser" => true]);
 
 
 $this->addRoute("/^(goods-in|arus|sales-orders|delivery-notes)$/", "documents", ["type_string"], ["requireUser" => true]);
 $this->addRoute("/^(goods-in|arus|sales-orders|delivery-notes)$/", "documents", ["type_string"], ["requireUser" => true]);
 
 

+ 1 - 1
views/documents.php

@@ -51,7 +51,7 @@ if($is_aru) {
 		<h3><?=htmlspecialchars($title)?></h3>
 		<h3><?=htmlspecialchars($title)?></h3>
 	</div>
 	</div>
 	<?php
 	<?php
-	if($is_goods_in || $is_aru) {
+	if($is_sale_order || $is_goods_in || $is_aru) {
 		?>
 		?>
 		<div class="col-auto">
 		<div class="col-auto">
 			<button type="button" id="new-document-button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#new-document-modal" data-type="<?=$type?>">
 			<button type="button" id="new-document-button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#new-document-modal" data-type="<?=$type?>">