feat: refs #6943 #6943 improve CustomerUnpaid

This commit is contained in:
Javier Segarra 2024-11-25 22:52:54 +01:00
parent e04c7d843e
commit 93cd111dc5
1 changed files with 59 additions and 143 deletions

View File

@ -1,164 +1,80 @@
<script setup> <script setup>
import { computed, onBeforeMount, ref, watch, nextTick } from 'vue'; import { ref } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { useRoute } from 'vue-router'; import { useRoute } from 'vue-router';
import VnInputDate from 'components/common/VnInputDate.vue'; import VnInputDate from 'components/common/VnInputDate.vue';
import VnInput from 'src/components/common/VnInput.vue'; import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue'; import FetchData from 'components/FetchData.vue';
import axios from 'axios'; import VnInputNumber from 'src/components/common/VnInputNumber.vue';
import useNotify from 'src/composables/useNotify'; const formModelRef = ref(false);
import { useStateStore } from 'stores/useStateStore';
const { t } = useI18n(); const { t } = useI18n();
const route = useRoute(); const route = useRoute();
const { notify } = useNotify();
const stateStore = useStateStore();
const amountInputRef = ref(null); const amountInputRef = ref(null);
const initialDated = Date.vnNew();
const unpaidClient = ref(false);
const isLoading = ref(false);
const amount = ref(null);
const dated = ref(initialDated);
const initialData = ref({ const initialData = ref({
dated: initialDated, dated: Date.vnNew(),
amount: null,
}); });
const hasChanged = computed(() => { const filterClientFindOne = {
return ( fields: ['unpaid', 'dated', 'amount'],
initialData.value.dated !== dated.value || where: {
initialData.value.amount !== amount.value
);
});
onBeforeMount(() => {
getData(route.params.id);
});
watch(
() => route.params.id,
(newValue) => {
if (!newValue) return;
getData(newValue);
}
);
const getData = async (id) => {
const filter = { where: { clientFk: id } };
try {
const { data } = await axios.get('ClientUnpaids', {
params: { filter: JSON.stringify(filter) },
});
if (data.length) {
setValues(data[0]);
} else {
defaultValues();
}
} catch (error) {
defaultValues();
}
};
const setValues = (data) => {
unpaidClient.value = true;
amount.value = data.amount;
dated.value = data.dated;
initialData.value = data;
};
const defaultValues = () => {
unpaidClient.value = false;
initialData.value.amount = null;
setInitialData();
};
const setInitialData = () => {
amount.value = initialData.value.amount;
dated.value = initialData.value.dated;
};
const onSubmit = async () => {
isLoading.value = true;
const payload = {
amount: amount.value,
clientFk: route.params.id, clientFk: route.params.id,
dated: dated.value, },
};
try {
await axios.patch('ClientUnpaids', payload);
notify('globals.dataSaved', 'positive');
unpaidClient.value = true;
} catch (error) {
notify('errors.writeRequest', 'negative');
} finally {
isLoading.value = false;
}
}; };
watch(
() => unpaidClient.value,
async (val) => {
await nextTick();
if (val) amountInputRef.value.focus();
}
);
</script> </script>
<template> <template>
<Teleport v-if="stateStore?.isSubToolbarShown()" to="#st-actions"> <FetchData
<QBtnGroup push class="q-gutter-x-sm"> :filter="filterClientFindOne"
<QBtn auto-load
:disabled="!hasChanged" url="ClientUnpaids"
:label="t('globals.reset')" @on-fetch="
:loading="isLoading" (data) => {
@click="setInitialData" const unpaid = data.length == 1;
color="primary" initialData = { ...data[0], unpaid };
flat }
icon="restart_alt" "
type="reset" />
/> <FormModel
<QBtn v-if="'unpaid' in initialData"
:disabled="!hasChanged" :observe-form-changes="false"
:label="t('globals.save')" ref="formModelRef"
:loading="isLoading" model="unpaid"
@click="onSubmit" url-update="ClientUnpaids"
color="primary" :mapper="(formData) => ({ ...formData, clientFk: route.params.id })"
icon="save" :form-initial-data="initialData"
/> >
</QBtnGroup> <template #form="{ data }">
</Teleport> <VnRow>
<QCheckbox :label="t('Unpaid client')" v-model="data.unpaid" />
</VnRow>
<div class="full-width flex justify-center"> <VnRow class="row q-gutter-md q-mb-md" v-show="data.unpaid">
<QCard class="card-width q-pa-lg"> <div class="col">
<QForm> <VnInputDate
<VnRow> data-cy="customerUnpaidDate"
<div class="col"> :label="t('Date')"
<QCheckbox :label="t('Unpaid client')" v-model="unpaidClient" /> v-model="data.dated"
</div> />
</VnRow> </div>
<div class="col">
<VnRow class="row q-gutter-md q-mb-md" v-show="unpaidClient"> <VnInputNumber
<div class="col"> data-cy="customerUnpaidAmount"
<VnInputDate :label="t('Date')" v-model="dated" /> ref="amountInputRef"
</div> :label="t('Amount')"
<div class="col"> clearable
<VnInput v-model="data.amount"
ref="amountInputRef" autofocus
:label="t('Amount')" >
clearable <template #append></template></VnInputNumber
type="number" >
v-model="amount" </div>
autofocus </VnRow>
> </template>
<template #append></template></VnInput </FormModel>
>
</div>
</VnRow>
</QForm>
</QCard>
</div>
</template> </template>
<i18n> <i18n>