document-page.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. swal.fire({
  73. icon: "question",
  74. title: "Emailing PDF",
  75. html: $p1.get(0).outerHTML + $p2.get(0).outerHTML,
  76. confirmButtonText: "Send Email",
  77. showCancelButton: true,
  78. preConfirm: () => {
  79. return $.post(window.location + "/email-pdf", {
  80. variant: variant
  81. }).then(() => {
  82. toastr.success("Email sent successfully.");
  83. }).catch(error => {
  84. const { responseJSON } = error;
  85. if(responseJSON && responseJSON.message) {
  86. toastr.error(responseJSON.message);
  87. }
  88. });
  89. }
  90. });
  91. });
  92. });
  93. }
  94. static SaveForm() {
  95. const $form = $("#document-form");
  96. const action = $form.get(0).action;
  97. const data = $form.serialize();
  98. const $submitElements = $form.find("[type=submit]");
  99. $submitElements.prop("disabled", true);
  100. return $.post(action, data).always(() => {
  101. $submitElements.prop("disabled", false);
  102. }).then(() => {
  103. toastr.success("Sales Order saved successfully.");
  104. });
  105. }
  106. }
  107. $(() => {
  108. DocumentPage.InitForm();
  109. DocumentPage.InitItemsSection();
  110. DocumentPage.InitPdfActions();
  111. });