From d83f31c3c28e0f920be915bebe6d949d2d1af030 Mon Sep 17 00:00:00 2001 From: florianfederspiel Date: Sun, 9 Mar 2025 18:09:20 +0100 Subject: [PATCH] Introduced central Sum for Createddocuments --- composables/useSum.js | 107 ++++++++++++++++++++- pages/banking/statements/[mode]/[[id]].vue | 37 +------ pages/createDocument/index.vue | 2 +- 3 files changed, 112 insertions(+), 34 deletions(-) diff --git a/composables/useSum.js b/composables/useSum.js index 48fb221..e68d140 100644 --- a/composables/useSum.js +++ b/composables/useSum.js @@ -16,6 +16,111 @@ export const useSum = () => { return sum.toFixed(2) } - return {getIncomingInvoiceSum} + const getCreatedDocumentSum = (createddocument) => { + let totalNet = 0 + let total19 = 0 + let total7 = 0 + + createddocument.rows.forEach(row => { + if(!['pagebreak','title','text'].includes(row.mode)){ + let rowPrice = Number(Number(row.quantity) * Number(row.price) * (1 - Number(row.discountPercent) /100) ).toFixed(3) + totalNet = totalNet + Number(rowPrice) + + if(row.taxPercent === 19) { + total19 = total19 + Number(rowPrice * 0.19) + } else if(row.taxPercent === 7) { + total7 = total7 + Number(rowPrice * 0.07) + } + } + }) + + + + + let totalGross = Number(totalNet.toFixed(2)) + Number(total19.toFixed(2)) + Number(total7.toFixed(2)) + + let totalGrossAlreadyPaid = 0 + + createddocument.usedAdvanceInvoices.forEach(advanceInvoiceId => { + let advanceInvoice = createddocuments.value.find(i => i.id === advanceInvoiceId) + + let priceNet = advanceInvoice.rows.find(i => i.advanceInvoiceData).price + + let partSum = priceNet * ((100 + advanceInvoice.rows.find(i => i.advanceInvoiceData).taxPercent) / 100) + + totalGrossAlreadyPaid += partSum + }) + + + let sumToPay = totalGross - totalGrossAlreadyPaid + + return sumToPay + } + + const getCreatedDocumentSumDetailed = (createddocument) => { + let totalNet = 0 + let total19 = 0 + let total7 = 0 + + createddocument.rows.forEach(row => { + if(!['pagebreak','title','text'].includes(row.mode)){ + let rowPrice = Number(Number(row.quantity) * Number(row.price) * (1 - Number(row.discountPercent) /100) ).toFixed(3) + totalNet = totalNet + Number(rowPrice) + + if(row.taxPercent === 19) { + total19 = total19 + Number(rowPrice * 0.19) + } else if(row.taxPercent === 7) { + total7 = total7 + Number(rowPrice * 0.07) + } + } + }) + + //Title Sum + + let titleSums = {} + let lastTitle = "" + + createddocument.rows.forEach(row => { + if(row.mode === 'title'){ + titleSums[`${row.pos} - ${row.text}`] = 0 + lastTitle = `${row.pos} - ${row.text}` + } else if(!['pagebreak','text'].includes(row.mode) && lastTitle !== ""){ + titleSums[lastTitle] = Number(titleSums[lastTitle]) + Number(Number(row.quantity) * Number(row.price) * (1 - Number(row.discountPercent) /100) ) + } + }) + + + + + let totalGross = Number(totalNet.toFixed(2)) + Number(total19.toFixed(2)) + Number(total7.toFixed(2)) + + let totalGrossAlreadyPaid = 0 + + createddocument.usedAdvanceInvoices.forEach(advanceInvoiceId => { + let advanceInvoice = createddocuments.value.find(i => i.id === advanceInvoiceId) + + let priceNet = advanceInvoice.rows.find(i => i.advanceInvoiceData).price + + let partSum = priceNet * ((100 + advanceInvoice.rows.find(i => i.advanceInvoiceData).taxPercent) / 100) + + totalGrossAlreadyPaid += partSum + }) + + console.log(totalGrossAlreadyPaid) + + let sumToPay = totalGross - totalGrossAlreadyPaid + + return { + titleSums: titleSums, + totalNet: totalNet, + total19: total19, + total7: total7, + totalGross: totalGross, + totalGrossAlreadyPaid: totalGrossAlreadyPaid, + totalSumToPay: sumToPay + } + } + + return {getIncomingInvoiceSum, getCreatedDocumentSum, getCreatedDocumentSumDetailed} } \ No newline at end of file diff --git a/pages/banking/statements/[mode]/[[id]].vue b/pages/banking/statements/[mode]/[[id]].vue index 88e7c0e..4dc1122 100644 --- a/pages/banking/statements/[mode]/[[id]].vue +++ b/pages/banking/statements/[mode]/[[id]].vue @@ -44,14 +44,14 @@ const setup = async () => { accounts.value = (await supabase.from("accounts").select()).data - openDocuments.value = documents.filter(i => i.statementallocations.reduce((n,{amount}) => n + amount, 0).toFixed(2) !== getDocumentSum(i).toFixed(2)) + openDocuments.value = documents.filter(i => i.statementallocations.reduce((n,{amount}) => n + amount, 0).toFixed(2) !== useSum().getCreatedDocumentSum(i).toFixed(2)) openDocuments.value = openDocuments.value.map(i => { return { ...i, - docTotal: getDocumentSum(i, i.usedAdvanceInvoices.map(i => getDocumentSum(documents.find(x => x.id === i)))), + docTotal: useSum().getCreatedDocumentSum(i), statementTotal: Number(i.statementallocations.reduce((n,{amount}) => n + amount, 0)), - openSum: (Number(getDocumentSum(i, i.usedAdvanceInvoices.map(i => getDocumentSum(documents.find(x => x.id === i))))) - Number(i.statementallocations.reduce((n,{amount}) => n + amount, 0))).toFixed(2) + openSum: (useSum().getCreatedDocumentSum(i) - Number(i.statementallocations.reduce((n,{amount}) => n + amount, 0))).toFixed(2) } }) @@ -82,33 +82,6 @@ const separateIBAN = (input = "") => { return separates.join(" ") } -const getDocumentSum = (doc,advanceInvoices = []) => { - let sum = 0 - - //console.log(advanceInvoices) - - doc.rows.forEach(row => { - if(row.mode === "normal" || row.mode === "service" || row.mode === "free") { - sum += row.quantity * row.price * (1 - row.discountPercent / 100) * (1 + row.taxPercent / 100) - } - }) - - //console.log(sum) - - if(advanceInvoices.length > 0) { - advanceInvoices.forEach(i => { - console.log(i) - sum -= i - }) - } - - //console.log(sum) - - - - return Number(sum.toFixed(2)) -} - const getInvoiceSum = (invoice) => { let sum = 0 invoice.accounts.forEach(account => { @@ -408,7 +381,7 @@ setup()