From b4310b1d5584e309ddb238f35ad1d63e2a085f13 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Mon, 14 Oct 2024 10:15:26 +0200 Subject: [PATCH] revert: #6942 CustomerBalance changes --- src/pages/Customer/Card/CustomerBalance.vue | 137 +++++++++++++------- 1 file changed, 88 insertions(+), 49 deletions(-) diff --git a/src/pages/Customer/Card/CustomerBalance.vue b/src/pages/Customer/Card/CustomerBalance.vue index f148194f8..95f5f0981 100644 --- a/src/pages/Customer/Card/CustomerBalance.vue +++ b/src/pages/Customer/Card/CustomerBalance.vue @@ -5,27 +5,29 @@ import { useRoute } from 'vue-router'; import { useAcl } from 'src/composables/useAcl'; import axios from 'axios'; import { useQuasar } from 'quasar'; -import FetchData from 'components/FetchData.vue'; import { toCurrency, toDate, toDateHourMin } from 'src/filters'; import { useState } from 'composables/useState'; import { useStateStore } from 'stores/useStateStore'; import { usePrintService } from 'composables/usePrintService'; +import { useSession } from 'composables/useSession'; import { useVnConfirm } from 'composables/useVnConfirm'; import VnTable from 'components/VnTable/VnTable.vue'; import VnInput from 'components/common/VnInput.vue'; import VnSubToolbar from 'components/ui/VnSubToolbar.vue'; -import VnSelect from 'src/components/common/VnSelect.vue'; +import VnFilter from 'components/VnTable/VnFilter.vue'; import CustomerNewPayment from 'src/pages/Customer/components/CustomerNewPayment.vue'; import InvoiceOutDescriptorProxy from 'src/pages/InvoiceOut/Card/InvoiceOutDescriptorProxy.vue'; const { openConfirmationModal } = useVnConfirm(); -const { sendEmail, openReport } = usePrintService(); +const { sendEmail } = usePrintService(); const { t } = useI18n(); const { hasAny } = useAcl(); -const currentBalance = ref({}); + +const session = useSession(); +const tokenMultimedia = session.getTokenMultimedia(); const quasar = useQuasar(); const route = useRoute(); const state = useState(); @@ -33,9 +35,9 @@ const stateStore = useStateStore(); const user = state.getUser(); const clientRisk = ref([]); -const companies = ref([]); const tableRef = ref(); -const companyId = ref(user.value.companyFk); +const companyId = ref(); +const companyLastId = ref(user.value.companyFk); const balances = ref([]); const vnFilterRef = ref({}); const filter = computed(() => { @@ -45,16 +47,43 @@ const filter = computed(() => { }; }); +const companyFilterColumn = { + align: 'left', + name: 'companyId', + label: t('Company'), + component: 'select', + attrs: { + url: 'Companies', + optionLabel: 'code', + sortBy: 'code', + limit: 0, + }, + columnFilter: { + event: { + remove: () => (companyId.value = null), + 'update:modelValue': (newCompanyFk) => { + if (!newCompanyFk) return; + vnFilterRef.value.addFilter(newCompanyFk); + companyLastId.value = newCompanyFk; + }, + blur: () => + !companyId.value && + (companyId.value = companyLastId.value ?? user.value.companyFk), + }, + }, + visible: false, +}; + const columns = computed(() => [ { - align: 'right', + align: 'left', name: 'payed', label: t('Date'), format: ({ payed }) => toDate(payed), cardVisible: true, }, { - align: 'right', + align: 'left', name: 'created', label: t('Creation date'), format: ({ created }) => toDateHourMin(created), @@ -62,10 +91,16 @@ const columns = computed(() => [ }, { align: 'left', + name: 'workerFk', label: t('Employee'), columnField: { component: 'userLink', - attrs: ({ row }) => ({ workerId: row.workerFk, name: row.userName }), + attrs: ({ row }) => { + return { + workerId: row.workerFk, + name: row.userName, + }; + }, }, cardVisible: true, }, @@ -90,14 +125,14 @@ const columns = computed(() => [ isId: true, }, { - align: 'left', + align: 'right', name: 'credit', label: t('Havings'), format: ({ credit }) => credit && toCurrency(credit), cardVisible: true, }, { - align: 'left', + align: 'right', name: 'balance', label: t('Balance'), format: ({ balance }) => toCurrency(balance), @@ -136,20 +171,41 @@ const columns = computed(() => [ onBeforeMount(() => { stateStore.rightDrawer = true; + companyId.value = user.value.companyFk; }); -async function getCurrentBalance(data) { - currentBalance.value[companyId.value] = { - amount: 0, - code: companies.value.find((c) => c.id === companyId.value)?.code, - }; +async function getClientRisk() { + const { data } = await axios.get(`clientRisks`, { + params: { + filter: JSON.stringify({ + include: { relation: 'company', scope: { fields: ['code'] } }, + where: { clientFk: route.params.id, companyFk: user.value.companyFk }, + }), + }, + }); + clientRisk.value = data; + return clientRisk.value; +} - for (const balance of data) { - currentBalance.value[balance.companyFk] = { - code: balance.company.code, - amount: balance.amount, - }; +async function getCurrentBalance() { + const currentBalance = (await getClientRisk()).find((balance) => { + return balance.companyFk === companyId.value; + }); + return currentBalance && currentBalance.amount; +} + +async function onFetch(data) { + balances.value = []; + for (const [index, balance] of data.entries()) { + if (index === 0) { + balance.balance = await getCurrentBalance(); + continue; + } + const previousBalance = data[index - 1]; + balance.balance = + previousBalance?.balance - (previousBalance?.debit - previousBalance?.credit); } + balances.value = data; } const showNewPaymentDialog = () => { @@ -164,48 +220,31 @@ const showNewPaymentDialog = () => { }; const showBalancePdf = ({ id }) => { - openReport(`InvoiceOuts/${id}/download`, {}, '_blank'); + const url = `api/InvoiceOuts/${id}/download?access_token=${tokenMultimedia}`; + window.open(url, '_blank'); };