AddTreasuryLaunchDialog.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <q-dialog ref="dialogRef" @hide="onDialogHide">
  3. <q-card
  4. class="q-dialog-plugin overflow-hidden"
  5. style="width: 520px; max-width: 90vw"
  6. >
  7. <DefaultDialogHeader :title="title" @close="onDialogCancel" />
  8. <DefaultForm ref="formRef" @submit="onOKClick">
  9. <q-scroll-area ref="scrollAreaRef" class="form-dialog-scroll-area">
  10. <q-card-section class="row q-col-gutter-md q-pt-none">
  11. <DefaultSelect
  12. v-model="form.transaction_type"
  13. v-model:error="validationErrors.transaction_type"
  14. class="col-12"
  15. emit-value
  16. label="Tipo"
  17. map-options
  18. placeholder="Entrada ou Saída"
  19. :error-message="validationErrors.transaction_type"
  20. :options="typeOptions"
  21. :rules="[inputRules.required]"
  22. />
  23. <DefaultInput
  24. v-model="form.description"
  25. v-model:error="validationErrors.description"
  26. class="col-12"
  27. label="Descrição"
  28. placeholder="Descreva a movimentação"
  29. :error-message="validationErrors.description"
  30. :rules="[inputRules.required]"
  31. />
  32. <DefaultSelect
  33. v-model="form.financial_plan_account_id"
  34. v-model:error="validationErrors.financial_plan_account_id"
  35. class="col-12"
  36. emit-value
  37. input-debounce="0"
  38. label="Plano de Contas"
  39. map-options
  40. use-input
  41. :error-message="validationErrors.financial_plan_account_id"
  42. :loading="loadingPlanAccounts"
  43. :options="planAccountOptions"
  44. @filter="filterPlanAccounts"
  45. >
  46. <template #no-option>
  47. <q-item>
  48. <q-item-section class="text-grey">
  49. {{ $t("http.errors.no_records_found") }}
  50. </q-item-section>
  51. </q-item>
  52. </template>
  53. </DefaultSelect>
  54. <DefaultCurrencyInput
  55. v-model="form.amount"
  56. v-model:error="validationErrors.amount"
  57. class="col-12"
  58. label="Valor"
  59. :error-message="validationErrors.amount"
  60. :rules="[inputRules.requiredNumber, inputRules.minValue(0.01)]"
  61. />
  62. </q-card-section>
  63. </q-scroll-area>
  64. <q-separator />
  65. <q-card-actions align="right" class="q-pa-md q-gutter-sm">
  66. <q-btn
  67. color="negative"
  68. label="Cancelar"
  69. no-caps
  70. outline
  71. @click="onDialogCancel"
  72. />
  73. <q-btn
  74. color="primary"
  75. label="Salvar"
  76. no-caps
  77. type="submit"
  78. unelevated
  79. :loading="loading"
  80. />
  81. </q-card-actions>
  82. </DefaultForm>
  83. </q-card>
  84. </q-dialog>
  85. </template>
  86. <script setup>
  87. import {
  88. createTreasuryLaunch,
  89. updateTreasuryLaunch,
  90. } from "src/api/treasury_launch";
  91. import { getFinancialPlanAccountsForSelect } from "src/api/financial_plan_account";
  92. import { onMounted, ref } from "vue";
  93. import { useDialogPluginComponent } from "quasar";
  94. import { useForm } from "src/composables/useForm";
  95. import { useInputRules } from "src/composables/useInputRules";
  96. import { useScroll } from "src/composables/useScroll";
  97. import { useSubmitHandler } from "src/composables/useSubmitHandler";
  98. import DefaultCurrencyInput from "src/components/defaults/DefaultCurrencyInput.vue";
  99. import DefaultDialogHeader from "src/components/defaults/DefaultDialogHeader.vue";
  100. import DefaultInput from "src/components/defaults/DefaultInput.vue";
  101. import DefaultSelect from "src/components/defaults/DefaultSelect.vue";
  102. defineEmits([...useDialogPluginComponent.emits]);
  103. const { account, launch } = defineProps({
  104. account: {
  105. required: true,
  106. type: Object,
  107. },
  108. launch: {
  109. default: null,
  110. type: Object,
  111. },
  112. });
  113. const { inputRules } = useInputRules();
  114. const { dialogRef, onDialogCancel, onDialogHide, onDialogOK } =
  115. useDialogPluginComponent();
  116. const formRef = ref(null);
  117. const scrollAreaRef = ref(null);
  118. const { scrollToComponent } = useScroll();
  119. const { form, getUpdatedFields } = useForm(
  120. {
  121. amount: launch?.amount ?? 0,
  122. description: launch?.description ?? "",
  123. financial_plan_account_id: launch?.financial_plan_account_id ?? null,
  124. transaction_type: launch?.transaction_type ?? null,
  125. },
  126. { containerRef: scrollAreaRef },
  127. );
  128. const {
  129. loading,
  130. validationErrors,
  131. execute: submitForm,
  132. } = useSubmitHandler({
  133. containerRef: scrollAreaRef,
  134. formRef,
  135. onSuccess: (response) => onDialogOK(response),
  136. scrollFn: scrollToComponent,
  137. });
  138. const allPlanAccounts = ref([]);
  139. const loadingPlanAccounts = ref(false);
  140. const planAccountOptions = ref([]);
  141. const isEdit = !!launch;
  142. const title = `${isEdit ? "Editar" : "Nova"} movimentação — ${account.name}`;
  143. const typeOptions = [
  144. { label: "Entrada", value: "entrada" },
  145. { label: "Saída", value: "saida" },
  146. ];
  147. const filterPlanAccounts = (needle, update) => {
  148. update(() => {
  149. const term = needle.toLowerCase();
  150. planAccountOptions.value = term
  151. ? allPlanAccounts.value.filter((option) =>
  152. option.label.toLowerCase().includes(term),
  153. )
  154. : allPlanAccounts.value;
  155. });
  156. };
  157. const loadPlanAccounts = async () => {
  158. loadingPlanAccounts.value = true;
  159. try {
  160. const accounts = await getFinancialPlanAccountsForSelect();
  161. allPlanAccounts.value = accounts.map((account) => ({
  162. label: `${account.code} - ${account.description}`,
  163. value: account.id,
  164. }));
  165. planAccountOptions.value = allPlanAccounts.value;
  166. } finally {
  167. loadingPlanAccounts.value = false;
  168. }
  169. };
  170. const onOKClick = async () => {
  171. const payload = {
  172. account_id: account.id,
  173. amount: form.amount,
  174. description: form.description,
  175. financial_plan_account_id: form.financial_plan_account_id,
  176. transaction_type: form.transaction_type,
  177. };
  178. await submitForm(() =>
  179. isEdit
  180. ? updateTreasuryLaunch(launch.id, getUpdatedFields.value)
  181. : createTreasuryLaunch(payload),
  182. );
  183. };
  184. onMounted(loadPlanAccounts);
  185. </script>