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

Add product lookup to document item modal

Alan Lam 3 лет назад
Родитель
Сommit
b65bf31765
2 измененных файлов с 64 добавлено и 0 удалено
  1. 44 0
      public/js/document-item-modal.js
  2. 20 0
      views/modals/document_item.php

+ 44 - 0
public/js/document-item-modal.js

@@ -15,6 +15,12 @@ class DocumentItemModal {
 
 
 		this.$documentItemIdField = this.getField("document_item_id");
 		this.$documentItemIdField = this.getField("document_item_id");
 
 
+		this.$supplierDropdown = this.getField("supplier_id");
+		this.$supplierDropdown.on("change", () => this.onSupplierChange());
+
+		this.$productDropdown = this.getField("product_id");
+		this.$productDropdown.on("change", event => this.onProductChange());
+
 		this.$descriptionField = this.getField("description");
 		this.$descriptionField = this.getField("description");
 		this.$serialNumberField = this.getField("serial_number");
 		this.$serialNumberField = this.getField("serial_number");
 
 
@@ -28,6 +34,44 @@ class DocumentItemModal {
 		return this.$form.find(`[name=${name}]`);
 		return this.$form.find(`[name=${name}]`);
 	}
 	}
 
 
+	onSupplierChange() {
+		const supplierId = this.$supplierDropdown.val();
+
+		this.clearProducts();
+
+		const deferred = $.Deferred();
+
+		if(!supplierId) {
+			deferred.resolve();
+			return deferred;
+		}
+
+		return $.get(`/suppliers/${supplierId}/get-products`).then(response => {
+			const options = response.products.map(product => {
+				const option = document.createElement("option");
+				option.textContent = product.name;
+				option.value = product.id;
+				option.dataset.price = product.price;
+				return option;
+			});
+			this.$productDropdown.append(options);
+		}).catch(error => {
+			if(error.responseJSON && error.responseJSON.message) {
+				toastr.error(error.responseJSON.message);
+			}
+		});
+	}
+
+	clearProducts() {
+		this.$productDropdown.find("option[value!='']").remove();
+	}
+
+	onProductChange() {
+		const $option = this.$productDropdown.find("option:selected");
+		this.$unitPriceField.val($option.data("price"));
+		this.$descriptionField.val($.trim($option.text()));
+	}
+
 	// Modal lifecycle
 	// Modal lifecycle
 	onShow(event) {
 	onShow(event) {
 	    const $element = $(event.relatedTarget);
 	    const $element = $(event.relatedTarget);

+ 20 - 0
views/modals/document_item.php

@@ -1,15 +1,35 @@
 <?php
 <?php
 namespace App;
 namespace App;
+
+use App\Helpers\HtmlHelper;
 ?>
 ?>
 <div class="modal fade" role="dialog" tabindex="-1" data-backdrop="static" id="document-item-modal">
 <div class="modal fade" role="dialog" tabindex="-1" data-backdrop="static" id="document-item-modal">
 	<div class="modal-dialog modal-lg">
 	<div class="modal-dialog modal-lg">
 		<form class="modal-content" method="post">
 		<form class="modal-content" method="post">
 			<?php
 			<?php
+
 			View::include("modals/header", ["title" => __("Document Item")]);
 			View::include("modals/header", ["title" => __("Document Item")]);
 			?>
 			?>
 			<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-3">
+					<label class="form-label" for="document-item-supplier-id">
+						<?=htmlspecialchars(__("Supplier"))?>
+					</label>
+					<select class="form-select" name="supplier_id" id="document-item-supplier-id">
+						<option value=""><?=htmlspecialchars(__("Select Supplier"))?></option>
+						<?php
+						HtmlHelper::renderOptions(Supplier::getOptions());
+						?>
+					</select>
+				</div>
+				<div class="mb-3">
+					<label for="document-item-product-id" class="form-label"><?=htmlspecialchars(__("Product"))?></label>
+					<select class="form-select" name="product_id" id="document-item-product-id">
+						<option value=""><?=htmlspecialchars(__("Select Product"))?></option>
+					</select>
+				</div>
 				<div class="mb-3">
 				<div class="mb-3">
 					<label for="document-item-description" class="form-label">
 					<label for="document-item-description" class="form-label">
 						<?=htmlspecialchars(__("Description"))?>
 						<?=htmlspecialchars(__("Description"))?>