sale-item-modal.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. return option;
  62. });
  63. this.$productDropdown.append(options);
  64. }).catch(error => {
  65. if(error.responseJSON && error.responseJSON.message) {
  66. toastr.error(error.responseJSON.message);
  67. }
  68. });
  69. }
  70. clearProducts() {
  71. this.$productDropdown.find("option[value!='']").remove();
  72. }
  73. onProductChange() {
  74. const $option = this.$productDropdown.find("option:selected");
  75. this.$unitPriceField.val($option.data("price"));
  76. }
  77. onShown() {
  78. this.$supplierDropdown.trigger("focus");
  79. }
  80. onHidden() {
  81. this.$form.get(0).reset();
  82. this.$saleItemIdField.val(null);
  83. this.clearProducts();
  84. }
  85. onSubmit(event) {
  86. event.preventDefault();
  87. $.post(`/leads/${this.saleNumber}/submit-item`, this.$form.serialize()).then(response => {
  88. this.$modal.modal("hide");
  89. $("#sale-items-section").html(response.html);
  90. toastr.success("Item saved successfully.");
  91. SalePage.InitSaleItemSection();
  92. });
  93. }
  94. }
  95. $(() => new SaleItemModal());