AddTreasuryLaunchDialog.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <q-dialog ref="dialogRef" @hide="onDialogHide">
  3. <q-card class="q-dialog-plugin overflow-hidden" style="width: 520px; max-width: 90vw">
  4. <DefaultDialogHeader :title="title" @close="onDialogCancel" />
  5. <q-form ref="formRef" @submit="onOKClick">
  6. <q-card-section class="row q-col-gutter-sm q-pt-none">
  7. <DefaultSelect
  8. v-model="form.transaction_type"
  9. v-model:error="validationErrors.transaction_type"
  10. :options="typeOptions"
  11. emit-value
  12. map-options
  13. :rules="[inputRules.required]"
  14. label="Tipo"
  15. placeholder="Entrada ou Saída"
  16. class="col-12"
  17. />
  18. <DefaultInput
  19. v-model="form.description"
  20. v-model:error="validationErrors.description"
  21. :rules="[inputRules.required]"
  22. label="Descrição"
  23. placeholder="Descreva a movimentação"
  24. class="col-12"
  25. />
  26. <DefaultSelect
  27. v-model="form.financial_plan_account_id"
  28. v-model:error="validationErrors.financial_plan_account_id"
  29. :options="planAccountOptions"
  30. :loading="loadingPlanAccounts"
  31. emit-value
  32. map-options
  33. use-input
  34. input-debounce="0"
  35. :rules="[inputRules.required]"
  36. label="Plano de Contas"
  37. placeholder="Selecione a conta"
  38. class="col-12"
  39. @filter="filterPlanAccounts"
  40. />
  41. <DefaultCurrencyInput
  42. v-model="form.amount"
  43. v-model:error="validationErrors.amount"
  44. label="Valor"
  45. class="col-12"
  46. />
  47. </q-card-section>
  48. <q-card-actions align="right">
  49. <q-btn outline color="negative" label="Cancelar" @click="onDialogCancel" />
  50. <q-btn color="primary" label="Salvar" type="submit" :loading="loading" />
  51. </q-card-actions>
  52. </q-form>
  53. </q-card>
  54. </q-dialog>
  55. </template>
  56. <script setup>
  57. import { ref, onMounted } from "vue";
  58. import { useDialogPluginComponent } from "quasar";
  59. import { useInputRules } from "src/composables/useInputRules";
  60. import { useSubmitHandler } from "src/composables/useSubmitHandler";
  61. import { createTreasuryLaunch } from "src/api/treasury_launch";
  62. import { getFinancialPlanAccounts } from "src/api/financial_plan_account";
  63. import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
  64. import DefaultInput from "src/components/defaults/DefaultInput.vue";
  65. import DefaultSelect from "src/components/defaults/DefaultSelect.vue";
  66. import DefaultCurrencyInput from "src/components/defaults/DefaultCurrencyInput.vue";
  67. defineEmits([...useDialogPluginComponent.emits]);
  68. const { account } = defineProps({
  69. account: {
  70. type: Object,
  71. required: true,
  72. },
  73. });
  74. const { inputRules } = useInputRules();
  75. const { dialogRef, onDialogHide, onDialogOK, onDialogCancel } = useDialogPluginComponent();
  76. const formRef = ref(null);
  77. const title = `Nova movimentação — ${account.name}`;
  78. const typeOptions = [
  79. { label: "Entrada", value: "entrada" },
  80. { label: "Saída", value: "saida" },
  81. ];
  82. const form = ref({
  83. transaction_type: null,
  84. description: "",
  85. financial_plan_account_id: null,
  86. amount: 0,
  87. });
  88. const allPlanAccounts = ref([]);
  89. const planAccountOptions = ref([]);
  90. const loadingPlanAccounts = ref(false);
  91. const filterPlanAccounts = (needle, update) => {
  92. update(() => {
  93. const term = needle.toLowerCase();
  94. planAccountOptions.value = term
  95. ? allPlanAccounts.value.filter((o) => o.label.toLowerCase().includes(term))
  96. : allPlanAccounts.value;
  97. });
  98. };
  99. onMounted(async () => {
  100. loadingPlanAccounts.value = true;
  101. try {
  102. const accounts = await getFinancialPlanAccounts();
  103. allPlanAccounts.value = accounts.map((a) => ({
  104. label: `${a.code} - ${a.description}`,
  105. value: a.id,
  106. }));
  107. planAccountOptions.value = allPlanAccounts.value;
  108. } finally {
  109. loadingPlanAccounts.value = false;
  110. }
  111. });
  112. const {
  113. loading,
  114. validationErrors,
  115. execute: submitForm,
  116. } = useSubmitHandler({
  117. onSuccess: (response) => onDialogOK(response),
  118. formRef,
  119. });
  120. const onOKClick = async () => {
  121. await submitForm(() =>
  122. createTreasuryLaunch({
  123. account_id: account.id,
  124. transaction_type: form.value.transaction_type,
  125. description: form.value.description,
  126. financial_plan_account_id: form.value.financial_plan_account_id,
  127. amount: form.value.amount,
  128. }),
  129. );
  130. };
  131. </script>