| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- class DocumentItemModal {
- constructor() {
- this.$modal = $("#document-item-modal");
- this.modal = bootstrap.Modal.getInstance(this.$modal.get(0));
- this.$modal.on("show.bs.modal", event => this.onShow(event));
- this.$modal.on("shown.bs.modal", () => this.onShown());
- this.$modal.on("hidden.bs.modal", () => this.onHidden());
- this.$title = this.$modal.find(".modal-title");
- this.$form = this.$modal.find("form");
- this.$form.on("submit", event => this.onSubmit(event));
- this.form = this.$form.get(0);
- 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");
- this.$quantityField = this.getField("quantity");
- this.$unitPriceField = this.getField("unit_price");
- this.$insuranceValueField = this.getField("insurance_value");
- }
- getField(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;
- option.hidden = product.archived;
- 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);
- const data = $element.data();
- const { item, documentType } = data;
- this.$unitPriceField.closest("div").hide();
- this.$insuranceValueField.closest("div").hide();
- switch(documentType) {
- case DocumentTypes.DeliveryNote:
- this.$insuranceValueField.closest("div").show();
- break;
- case DocumentTypes.AdvancedReplacementUnit:
- case DocumentTypes.SalesOrder:
- this.$unitPriceField.closest("div").show();
- break;
- }
- if(item) {
- this.$title.text("Update Item");
- this.$documentItemIdField.val(item.id);
- this.$descriptionField.val(item.description);
- this.$serialNumberField.val(item.serialNumber);
- this.$quantityField.val(item.quantity);
- this.$unitPriceField.val(item.unitPrice);
- if(item.insuranceValue) {
- this.$insuranceValueField.val(item.insuranceValue);
- }
- }
- else {
- this.$title.text("Add Item");
- }
- }
- onShown() {
- this.$descriptionField.trigger("focus");
- }
- onHidden() {
- this.$form.get(0).reset();
- this.$documentItemIdField.val(null);
- }
- onSubmit(event) {
- event.preventDefault();
- $.post(window.location + `/submit-item`, this.$form.serialize()).then(response => {
- this.$modal.modal("hide");
- $("#document-items-section").html(response.html);
- toastr.success("Item saved successfully.");
- DocumentPage.InitItemsSection();
- });
- }
- }
- $(() => new DocumentItemModal());
|