document-item-modal.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. class DocumentItemModal {
  2. constructor() {
  3. this.$modal = $("#document-item-modal");
  4. this.modal = bootstrap.Modal.getInstance(this.$modal.get(0));
  5. this.$modal.on("show.bs.modal", event => this.onShow(event));
  6. this.$modal.on("shown.bs.modal", () => this.onShown());
  7. this.$modal.on("hidden.bs.modal", () => this.onHidden());
  8. this.$title = this.$modal.find(".modal-title");
  9. this.$form = this.$modal.find("form");
  10. this.$form.on("submit", event => this.onSubmit(event));
  11. this.form = this.$form.get(0);
  12. this.$documentItemIdField = this.getField("document_item_id");
  13. this.$supplierDropdown = this.getField("supplier_id");
  14. this.$supplierDropdown.on("change", () => this.onSupplierChange());
  15. this.$productDropdown = this.getField("product_id");
  16. this.$productDropdown.on("change", event => this.onProductChange());
  17. this.$descriptionField = this.getField("description");
  18. this.$serialNumberField = this.getField("serial_number");
  19. this.$quantityField = this.getField("quantity");
  20. this.$unitPriceField = this.getField("unit_price");
  21. this.$insuranceValueField = this.getField("insurance_value");
  22. }
  23. getField(name) {
  24. return this.$form.find(`[name=${name}]`);
  25. }
  26. onSupplierChange() {
  27. const supplierId = this.$supplierDropdown.val();
  28. this.clearProducts();
  29. const deferred = $.Deferred();
  30. if(!supplierId) {
  31. deferred.resolve();
  32. return deferred;
  33. }
  34. return $.get(`/suppliers/${supplierId}/get-products`).then(response => {
  35. const options = response.products.map(product => {
  36. const option = document.createElement("option");
  37. option.textContent = product.name;
  38. option.value = product.id;
  39. option.dataset.price = product.price;
  40. return option;
  41. });
  42. this.$productDropdown.append(options);
  43. }).catch(error => {
  44. if(error.responseJSON && error.responseJSON.message) {
  45. toastr.error(error.responseJSON.message);
  46. }
  47. });
  48. }
  49. clearProducts() {
  50. this.$productDropdown.find("option[value!='']").remove();
  51. }
  52. onProductChange() {
  53. const $option = this.$productDropdown.find("option:selected");
  54. this.$unitPriceField.val($option.data("price"));
  55. this.$descriptionField.val($.trim($option.text()));
  56. }
  57. // Modal lifecycle
  58. onShow(event) {
  59. const $element = $(event.relatedTarget);
  60. const data = $element.data();
  61. const { item, documentType } = data;
  62. this.$unitPriceField.closest("div").hide();
  63. this.$insuranceValueField.closest("div").hide();
  64. switch(documentType) {
  65. case DocumentTypes.DeliveryNote:
  66. this.$insuranceValueField.closest("div").show();
  67. break;
  68. case DocumentTypes.AdvancedReplacementUnit:
  69. case DocumentTypes.SalesOrder:
  70. this.$unitPriceField.closest("div").show();
  71. break;
  72. }
  73. if(item) {
  74. this.$title.text("Update Item");
  75. this.$documentItemIdField.val(item.id);
  76. this.$descriptionField.val(item.description);
  77. this.$serialNumberField.val(item.serialNumber);
  78. this.$quantityField.val(item.quantity);
  79. this.$unitPriceField.val(item.unitPrice);
  80. if(item.insuranceValue) {
  81. this.$insuranceValueField.val(item.insuranceValue);
  82. }
  83. }
  84. else {
  85. this.$title.text("Add Item");
  86. }
  87. }
  88. onShown() {
  89. this.$descriptionField.trigger("focus");
  90. }
  91. onHidden() {
  92. this.$form.get(0).reset();
  93. this.$documentItemIdField.val(null);
  94. }
  95. onSubmit(event) {
  96. event.preventDefault();
  97. $.post(window.location + `/submit-item`, this.$form.serialize()).then(response => {
  98. this.$modal.modal("hide");
  99. $("#document-items-section").html(response.html);
  100. toastr.success("Item saved successfully.");
  101. DocumentPage.InitItemsSection();
  102. });
  103. }
  104. }
  105. $(() => new DocumentItemModal());