import { string } from 'alga-js' import refreshTokenHelper from "../../utils/refreshTokenHelper" import forceLogoutHelper from "../../utils/forceLogoutHelper" import errorHandlingHelper from "../../utils/errorHandlingHelper" const handleFunc = async (event: any, authToken: any = null) => { let data: any = {} const config = useRuntimeConfig() const token = authToken ?? await getTokenHelper(event) const organizationId = getCookie(event, 'organizationId') // Fetch invoices with related order information const res: any = await event.context.fetch(`models/c_invoice?$filter=${string.urlEncode("isSOTrx eq true AND (DocStatus eq 'CO' OR DocStatus eq 'CL' OR DocStatus eq 'DR')")}&$select=C_Invoice_ID,C_Invoice_UU,IsActive,DocumentNo,Description,IsApproved,IsPaid,IsPrinted,IsTransferred,DateOrdered,DateInvoiced,DateAcct,TotalLines,GrandTotal,ChargeAmt,Processed,IsSOTrx,IsDiscountPrinted,IsTaxIncluded,SendEMail,IsSelfService,ProcessedOn,IsPayScheduleValid,IsInDispute,IsFixedAssetInvoice,IsOverrideCurrencyRate,DocStatus,C_DocType_ID,C_DocTypeTarget_ID,DocBaseType,SalesRep_ID,AD_User_ID,C_PaymentTerm_ID,C_Currency_ID,PaymentRule,M_PriceList_ID,C_BPartner_ID,C_BPartner_Location_ID,AD_Org_ID,AD_Client_ID,C_Order_ID,C_Payment_ID,isUploadToPayJoe,UploadDatePayJoe,isUploadToLexoffice,UploadDateLexoffice&$expand=C_Order_ID($select=Report_Strapi_Reference),C_Payment_ID($select=PayAmt),C_PaymentTerm_ID($select=NetDays)&orderby=c_invoice_${string.urlEncode('id desc')}`, 'GET', token, null) // Fetch allocation lines to get total paid amount for each invoice if (res?.records?.length > 0) { const invoiceIds = res.records.map((inv: any) => inv.id) // Fetch all allocation lines for these invoices try { const allocRes: any = await event.context.fetch(`models/c_allocationline?$filter=${string.urlEncode(`C_Invoice_ID in (${invoiceIds.join(',')})`)}&$select=C_Invoice_ID,Amount`, 'GET', token, null) if (allocRes?.records?.length > 0) { // Sum up allocations per invoice const allocationSums: Record = {} for (const alloc of allocRes.records) { const invoiceId = alloc.C_Invoice_ID?.id if (invoiceId) { allocationSums[invoiceId] = (allocationSums[invoiceId] || 0) + Math.abs(alloc.Amount || 0) } } // Add total paid amount to each invoice record for (const invoice of res.records) { invoice.TotalPaidAmt = allocationSums[invoice.id] || 0 } } } catch (err) { console.warn('Could not fetch allocation lines:', err) } } // Fetch org's business partner to check isAllowFeeReportDownload permission let isAllowFeeReportDownload = false if (organizationId) { try { const orgRes: any = await event.context.fetch(`models/ad_org/${organizationId}?$select=C_BPartner_ID`, 'GET', token, null) const bpartnerId = orgRes?.C_BPartner_ID?.id if (bpartnerId) { const bpartnerRes: any = await event.context.fetch(`models/c_bpartner/${bpartnerId}?$select=isAllowFeeReportDownload`, 'GET', token, null) isAllowFeeReportDownload = bpartnerRes?.isAllowFeeReportDownload === 'Y' } } catch (err) { console.warn('Could not fetch org business partner permission:', err) } } if(res) { data = { ...res, isAllowFeeReportDownload } } return data } export default defineEventHandler(async (event) => { let data: any = {} try { data = await handleFunc(event) } catch(err: any) { try { let authToken: any = await refreshTokenHelper(event) data = await handleFunc(event, authToken) } catch(error) { data = errorHandlingHelper(err?.data ?? err, error?.data ?? error) forceLogoutHelper(event, data) } } return data })