|
|
@@ -15,6 +15,12 @@ class DocumentItemModal {
|
|
|
|
|
|
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.$serialNumberField = this.getField("serial_number");
|
|
|
|
|
|
@@ -28,6 +34,44 @@ class DocumentItemModal {
|
|
|
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
|
|
|
onShow(event) {
|
|
|
const $element = $(event.relatedTarget);
|