document-page.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. class DocumentPage {
  2. static InitForm() {
  3. const $form = $("#document-form");
  4. $form.on("submit", event => {
  5. event.preventDefault();
  6. DocumentPage.SaveForm();
  7. })
  8. }
  9. static InitItemsSection() {
  10. const $table = $("#document-items-section table");
  11. const $checkboxes = $table.find("input[type=checkbox]");
  12. const $allCheckbox = $("#all-items-checkbox");
  13. const $batchActions = $(".batch-action");
  14. $allCheckbox.on("change", () => {
  15. const checked = $allCheckbox.prop("checked");
  16. $checkboxes.prop("checked", checked);
  17. });
  18. $checkboxes.on("change", () => {
  19. let batchActionsEnabled = $checkboxes.filter(":checked").length > 0;
  20. $batchActions.prop("disabled", !batchActionsEnabled);
  21. });
  22. $table.find(".btn-delete-document-item").on("click", event => {
  23. const documentItemId = $(event.currentTarget).data("document-item-id");
  24. swal.fire({
  25. icon: "question",
  26. title: "Deleting Item",
  27. text: "Are you sure you want to delete this item?",
  28. confirmButtonText: "Delete",
  29. showCancelButton: true
  30. }).then(result => {
  31. if(result.isConfirmed) {
  32. $.post(window.location + "/delete-item", {
  33. document_item_id: documentItemId
  34. }).then(response => {
  35. toastr.success("Item deleted successfully.");
  36. $("#document-items-section").html(response.html);
  37. DocumentPage.InitItemsSection();
  38. });
  39. }
  40. });
  41. });
  42. }
  43. static InitPdfActions() {
  44. const $elements = $(".pdf-action");
  45. $elements.on("click", event => {
  46. event.preventDefault();
  47. const $element = $(event.currentTarget);
  48. DocumentPage.SaveForm().then(() => {
  49. window.open($element.attr("href"));
  50. });
  51. });
  52. const $emailActions = $(".email-pdf-action");
  53. $emailActions.on("click", event => {
  54. const $element = $(event.currentTarget);
  55. const { variant } = $element.data();
  56. DocumentPage.SaveForm().then(() => {
  57. const showErrorAlert = (error) => {
  58. swal.fire("Error", error, "error");
  59. };
  60. const contactName = $("#contact-name").val();
  61. if(!contactName) {
  62. showErrorAlert("Contact Name must be set.")
  63. return;
  64. }
  65. const contactEmail = $("#contact-email").val();
  66. if(!contactEmail) {
  67. showErrorAlert("Contact Email must be set.")
  68. return;
  69. }
  70. const $p1 = $("<p>").text("Are you sure you want to email the PDF to:");
  71. const $p2 = $("<div>").text(`${contactName} <${contactEmail}>`);
  72. const checked = (variant === "commercial") ? 0 : 1;
  73. swal.fire({
  74. icon: "question",
  75. title: "Emailing PDF",
  76. html: $p1.get(0).outerHTML + $p2.get(0).outerHTML,
  77. confirmButtonText: "Send Email",
  78. showCancelButton: true,
  79. input: "checkbox",
  80. inputValue: checked,
  81. inputPlaceholder: "Attach brochures",
  82. preConfirm: value => {
  83. return $.post(window.location + "/email-pdf", {
  84. variant: variant,
  85. include_brochures: value
  86. }).then(() => {
  87. toastr.success("Email sent successfully.");
  88. }).catch(error => {
  89. const { responseJSON } = error;
  90. if(responseJSON && responseJSON.message) {
  91. toastr.error(responseJSON.message);
  92. }
  93. });
  94. }
  95. });
  96. });
  97. });
  98. }
  99. static SaveForm() {
  100. const $form = $("#document-form");
  101. const action = $form.get(0).action;
  102. const data = $form.serialize();
  103. const $submitElements = $form.find("[type=submit]");
  104. $submitElements.prop("disabled", true);
  105. return $.post(action, data).always(() => {
  106. $submitElements.prop("disabled", false);
  107. }).then(() => {
  108. toastr.success("Sales Order saved successfully.");
  109. });
  110. }
  111. }
  112. $(() => {
  113. DocumentPage.InitForm();
  114. DocumentPage.InitItemsSection();
  115. DocumentPage.InitPdfActions();
  116. });