sale-item-modal.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. class SaleItemModal {
  2. constructor() {
  3. this.$modal = $("#sale-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.$saleItemIdField = this.getField("sale_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.$quantityField = this.getField("quantity");
  18. this.$unitPriceField = this.getField("unit_price");
  19. }
  20. getField(name) {
  21. return this.$form.find(`[name=${name}]`);
  22. }
  23. // Modal lifecycle
  24. onShow(event) {
  25. const $element = $(event.relatedTarget);
  26. const data = $element.data();
  27. const { saleNumber, supplierId, item } = data;
  28. if(!saleNumber) {
  29. console.error("saleNumber not provided.");
  30. return false;
  31. }
  32. this.saleNumber = data.saleNumber;
  33. if(item) {
  34. this.$title.text("Update Lead Item");
  35. this.$saleItemIdField.val(item.id);
  36. this.$supplierDropdown.val(supplierId);
  37. this.onSupplierChange().then(() => {
  38. this.$productDropdown.val(item.productId);
  39. });
  40. this.$quantityField.val(item.quantity);
  41. this.$unitPriceField.val(item.unitPrice);
  42. }
  43. else {
  44. this.$title.text("Add Lead Item");
  45. }
  46. }
  47. onSupplierChange() {
  48. const supplierId = this.$supplierDropdown.val();
  49. this.clearProducts();
  50. const deferred = $.Deferred();
  51. if(!supplierId) {
  52. deferred.resolve();
  53. return deferred;
  54. }
  55. return $.get(`/suppliers/${supplierId}/get-products`).then(response => {
  56. const options = response.products.map(product => {
  57. const option = document.createElement("option");
  58. option.textContent = product.name;
  59. option.value = product.id;
  60. option.dataset.price = product.price;
  61. option.hidden = product.archived;
  62. return option;
  63. });
  64. this.$productDropdown.append(options);
  65. }).catch(error => {
  66. if(error.responseJSON && error.responseJSON.message) {
  67. toastr.error(error.responseJSON.message);
  68. }
  69. });
  70. }
  71. clearProducts() {
  72. this.$productDropdown.find("option[value!='']").remove();
  73. }
  74. onProductChange() {
  75. const $option = this.$productDropdown.find("option:selected");
  76. this.$unitPriceField.val($option.data("price"));
  77. }
  78. onShown() {
  79. this.$supplierDropdown.trigger("focus");
  80. }
  81. onHidden() {
  82. this.$form.get(0).reset();
  83. this.$saleItemIdField.val(null);
  84. this.clearProducts();
  85. }
  86. onSubmit(event) {
  87. event.preventDefault();
  88. $.post(`/leads/${this.saleNumber}/submit-item`, this.$form.serialize()).then(response => {
  89. this.$modal.modal("hide");
  90. $("#sale-items-section").html(response.html);
  91. toastr.success("Item saved successfully.");
  92. SalePage.InitSaleItemSection();
  93. });
  94. }
  95. }
  96. $(() => new SaleItemModal());