document-page.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. const confirmed = confirm("Are you sure you want to delete this item?");
  25. if(confirmed) {
  26. $.post(window.location + "/delete-item", {
  27. document_item_id: documentItemId
  28. }).then(response => {
  29. toastr.success("Item deleted successfully.");
  30. $("#document-items-section").html(response.html);
  31. DocumentPage.InitItemsSection();
  32. });
  33. }
  34. });
  35. }
  36. static InitPdfActions() {
  37. const $elements = $(".pdf-action");
  38. $elements.on("click", event => {
  39. event.preventDefault();
  40. const $element = $(event.currentTarget);
  41. DocumentPage.SaveForm().then(() => {
  42. window.open($element.attr("href"));
  43. });
  44. });
  45. const $emailActions = $(".email-pdf-action");
  46. $emailActions.on("click", event => {
  47. const $element = $(event.currentTarget);
  48. const { variant } = $element.data();
  49. DocumentPage.SaveForm().then(() => {
  50. toastr.info("Emailing PDF...");
  51. $.post(window.location + "/email-pdf", {
  52. variant: variant
  53. }).then(() => {
  54. toastr.success("Email sent successfully.");
  55. }).catch(error => {
  56. const { responseJSON } = error;
  57. if(responseJSON && responseJSON.message) {
  58. toastr.error(responseJSON.message);
  59. }
  60. });
  61. });
  62. });
  63. }
  64. static SaveForm() {
  65. const $form = $("#document-form");
  66. const action = $form.get(0).action;
  67. const data = $form.serialize();
  68. const $submitElements = $form.find("[type=submit]");
  69. $submitElements.prop("disabled", true);
  70. return $.post(action, data).always(() => {
  71. $submitElements.prop("disabled", false);
  72. }).then(() => {
  73. toastr.success("Sales Order saved successfully.");
  74. });
  75. }
  76. }
  77. $(() => {
  78. DocumentPage.InitForm();
  79. DocumentPage.InitItemsSection();
  80. DocumentPage.InitPdfActions();
  81. });