|
|
@@ -13,15 +13,22 @@ class SaleItemModal {
|
|
|
this.$form.on("submit", event => this.onSubmit(event));
|
|
|
this.form = this.$form.get(0);
|
|
|
|
|
|
- this.$saleItemIdField = this.$form.find("[name=sale_item_id]");
|
|
|
+ this.$saleItemIdField = this.getField("sale_item_id");
|
|
|
|
|
|
- this.$productDropdown = this.$form.find("[name=product_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.$quantityField = this.$form.find("[name=quantity]");
|
|
|
+ this.$quantityField = this.getField("quantity");
|
|
|
+
|
|
|
+ this.$unitPriceField = this.getField("unit_price");
|
|
|
|
|
|
- this.$unitPriceField = this.$form.find("[name=unit_price]");
|
|
|
+ }
|
|
|
|
|
|
+ getField(name) {
|
|
|
+ return this.$form.find(`[name=${name}]`);
|
|
|
}
|
|
|
|
|
|
// Modal lifecycle
|
|
|
@@ -29,7 +36,7 @@ class SaleItemModal {
|
|
|
const $element = $(event.relatedTarget);
|
|
|
const data = $element.data();
|
|
|
|
|
|
- const { saleNumber, item } = data;
|
|
|
+ const { saleNumber, supplierId, item } = data;
|
|
|
|
|
|
if(!saleNumber) {
|
|
|
console.error("saleNumber not provided.");
|
|
|
@@ -38,19 +45,54 @@ class SaleItemModal {
|
|
|
|
|
|
this.saleNumber = data.saleNumber;
|
|
|
|
|
|
- if(data.item) {
|
|
|
- const { item } = data;
|
|
|
+ if(item) {
|
|
|
this.$title.text("Update Sale Item");
|
|
|
|
|
|
this.$saleItemIdField.val(item.id);
|
|
|
- this.$productDropdown.val(item.productId);
|
|
|
+ this.$supplierDropdown.val(supplierId);
|
|
|
+
|
|
|
+ this.onSupplierChange().then(() => {
|
|
|
+ this.$productDropdown.val(item.productId);
|
|
|
+ });
|
|
|
+
|
|
|
this.$quantityField.val(item.quantity);
|
|
|
this.$unitPriceField.val(item.unitPrice);
|
|
|
}
|
|
|
else {
|
|
|
this.$title.text("Add Sale Item");
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ 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() {
|
|
|
@@ -59,12 +101,13 @@ class SaleItemModal {
|
|
|
}
|
|
|
|
|
|
onShown() {
|
|
|
- this.$productDropdown.trigger("focus");
|
|
|
+ this.$supplierDropdown.trigger("focus");
|
|
|
}
|
|
|
|
|
|
onHidden() {
|
|
|
this.$form.get(0).reset();
|
|
|
this.$saleItemIdField.val(null);
|
|
|
+ this.clearProducts();
|
|
|
}
|
|
|
|
|
|
onSubmit(event) {
|