
export const useSum = () => {

    const supabase = useSupabaseClient()

    const getIncomingInvoiceSum = (invoice) => {
        let sum = 0
        invoice.accounts.forEach(account => {
             console.log(account)


            sum += account.amountTax
            sum += account.amountNet



        })
        return sum.toFixed(2)
    }

    const getCreatedDocumentSum = (createddocument,createddocuments = []) => {
        let totalNet = 0
        let total19 = 0
        let total7 = 0

        /*let usedadvanceinvoices = []
        if(createddocument.usedAdvanceInvoices.length > 0) {
            console.log(createddocument)
            console.log(createddocument.usedAdvanceInvoices)



            console.log((await supabase.from("createddocuments").select().in("id", createddocument.usedAdvanceInvoices)))

            usedadvanceinvoices = (await supabase.from("createddocuments").select().in("id", createddocument.usedAdvanceInvoices)).data
            console.log(usedadvanceinvoices)

        }*/


        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.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 Number(sumToPay.toFixed(2))
    }

    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}

}