new-document-modal.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. class NewDocumentModal {
  2. constructor() {
  3. this.$modal = $("#new-document-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.$ticketIdField = this.getField("ticket_id");
  12. this.$companyDropdown = this.getField("company_id");
  13. this.$locationDropdown = this.getField("location_id");
  14. this.$supplierDropdown = this.getField("supplier_id");
  15. this.$supplierFormGroup = this.$supplierDropdown.closest(".mb-3");
  16. this.$aruSection = $("#new-document-aru-section");
  17. this.$itemDescriptionField = this.getField("item_description");
  18. this.$itemSerialNumberField = this.getField("item_serial_number");
  19. this.$productLookupDropdown = $("#new-document-product-lookup");
  20. this.$productLookupDropdown.select2({
  21. theme: "bootstrap-5",
  22. dropdownParent: this.$modal
  23. });
  24. this.$productLookupDropdown.on("change", () => {
  25. const $option = this.$productLookupDropdown.find("option:selected");
  26. this.$itemDescriptionField.val($option.data("name"));
  27. });
  28. this.$locationDropdown.select2({
  29. theme: "bootstrap-5",
  30. dropdownParent: this.$locationDropdown.closest("div")
  31. });
  32. const parameters = new URLSearchParams(window.location.search);
  33. const locationId = parameters.get("add_location_id");
  34. if(locationId) {
  35. const button = document.getElementById("new-document-button");
  36. button.click();
  37. }
  38. }
  39. getField(name) {
  40. return this.$form.find(`[name=${name}]`);
  41. }
  42. // Modal lifecycle
  43. onShow(event) {
  44. const $element = $(event.relatedTarget);
  45. const data = $element.data();
  46. const { type, locationId, ticketId } = data;
  47. this.$supplierFormGroup.show();
  48. this.$aruSection.show();
  49. switch(type) {
  50. case DocumentTypes.SalesOrder:
  51. this.$title.text("New Sales Order");
  52. this.baseUrl = "/sales-orders";
  53. this.$aruSection.hide();
  54. this.$supplierFormGroup.hide();
  55. break;
  56. case DocumentTypes.GoodsIn:
  57. this.$title.text("New Goods-In");
  58. this.baseUrl = "/goods-in";
  59. this.$aruSection.hide();
  60. break;
  61. case DocumentTypes.AdvancedReplacementUnit:
  62. this.$title.text("New ARU");
  63. this.baseUrl = "/arus";
  64. this.$supplierFormGroup.hide();
  65. break;
  66. }
  67. if(locationId) {
  68. this.$locationDropdown.val(locationId);
  69. }
  70. if(ticketId) {
  71. this.$ticketIdField.val(ticketId);
  72. }
  73. }
  74. onShown() {
  75. this.$companyDropdown.trigger("focus");
  76. }
  77. onHidden() {
  78. this.$form.get(0).reset();
  79. this.$ticketIdField.val(null);
  80. }
  81. onSubmit(event) {
  82. event.preventDefault();
  83. const data = this.$form.serialize();
  84. $.post(`${this.baseUrl}/create`, data).then(response => {
  85. this.$modal.modal("hide");
  86. window.location = response.url;
  87. }).catch(error => {
  88. console.log(error);
  89. });
  90. }
  91. }
  92. $(() => new NewDocumentModal());