document-item-modal.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. option.hidden = product.archived;
  41. return option;
  42. });
  43. this.$productDropdown.append(options);
  44. }).catch(error => {
  45. if(error.responseJSON && error.responseJSON.message) {
  46. toastr.error(error.responseJSON.message);
  47. }
  48. });
  49. }
  50. clearProducts() {
  51. this.$productDropdown.find("option[value!='']").remove();
  52. }
  53. onProductChange() {
  54. const $option = this.$productDropdown.find("option:selected");
  55. this.$unitPriceField.val($option.data("price"));
  56. this.$descriptionField.val($.trim($option.text()));
  57. }
  58. // Modal lifecycle
  59. onShow(event) {
  60. const $element = $(event.relatedTarget);
  61. const data = $element.data();
  62. const { item, documentType } = data;
  63. this.$unitPriceField.closest("div").hide();
  64. this.$insuranceValueField.closest("div").hide();
  65. switch(documentType) {
  66. case DocumentTypes.DeliveryNote:
  67. this.$insuranceValueField.closest("div").show();
  68. break;
  69. case DocumentTypes.AdvancedReplacementUnit:
  70. case DocumentTypes.SalesOrder:
  71. this.$unitPriceField.closest("div").show();
  72. break;
  73. }
  74. if(item) {
  75. this.$title.text("Update Item");
  76. this.$documentItemIdField.val(item.id);
  77. this.$descriptionField.val(item.description);
  78. this.$serialNumberField.val(item.serialNumber);
  79. this.$quantityField.val(item.quantity);
  80. this.$unitPriceField.val(item.unitPrice);
  81. if(item.insuranceValue) {
  82. this.$insuranceValueField.val(item.insuranceValue);
  83. }
  84. }
  85. else {
  86. this.$title.text("Add Item");
  87. }
  88. }
  89. onShown() {
  90. this.$descriptionField.trigger("focus");
  91. }
  92. onHidden() {
  93. this.$form.get(0).reset();
  94. this.$documentItemIdField.val(null);
  95. }
  96. onSubmit(event) {
  97. event.preventDefault();
  98. $.post(window.location + `/submit-item`, this.$form.serialize()).then(response => {
  99. this.$modal.modal("hide");
  100. $("#document-items-section").html(response.html);
  101. toastr.success("Item saved successfully.");
  102. DocumentPage.InitItemsSection();
  103. });
  104. }
  105. }
  106. $(() => new DocumentItemModal());