import { BillingLocation, BilledTo } from '@evidencija-rezija/shared-code'; /** * Checks if billFwdStatus should be updated to "pending" based on attachment status * @param location - The billing location containing the bill * @param currentBillId - The ID of the bill being updated (to exclude from check) * @param hasNewAttachment - Whether a new attachment is being added to the current bill * @returns true if billFwdStatus should be set to "pending" */ export const shouldUpdateBillFwdStatusWhenAttached = ( location: BillingLocation, currentBillId: string | undefined, hasNewAttachment: boolean ): boolean => { // Only proceed if a new attachment is being added if (!hasNewAttachment) { return false; } // Check billFwdEnabled is true if (location.billFwdEnabled !== true) { return false; } // Check billFwdStrategy is "when-attached" if (location.billFwdStrategy !== "when-attached") { return false; } // Check bills have already been sent or are pending -> don't sent them again if (location.billFwdStatus === "pending" || location.billFwdStatus === "sent") { return false; } // Check if ALL other bills have attachments const otherBills = location.bills.filter(bill => bill._id !== currentBillId); // filter only bills billed to tenant // because bills billed to landlord should not trigger forwarding const billsPayedByTenant = otherBills.filter(bill => bill.billedTo === BilledTo.Tenant); if(billsPayedByTenant.length === 0) { // No other bills billed to tenant exist, so do not trigger forwarding return false; } const allOtherBillsHaveAttachments = billsPayedByTenant.every(bill => bill.attachment !== null); return allOtherBillsHaveAttachments; }; /** * Checks if billFwdStatus should be updated to "pending" based on paid status * @param location - The billing location containing the bill * @param currentBillId - The ID of the bill being updated (to exclude from check) * @param isPaid - Whether the current bill is being marked as paid * @returns true if billFwdStatus should be set to "pending" */ export const shouldUpdateBillFwdStatusWhenPayed = ( location: BillingLocation, currentBillId: string | undefined, isPaid: boolean ): boolean => { // Only proceed if the bill is being marked as paid if (!isPaid) { return false; } // Check billFwdEnabled is true if (location.billFwdEnabled !== true) { return false; } // Check billFwdStrategy is "when-payed" if (location.billFwdStrategy !== "when-payed") { return false; } // Check bills have already been sent or are pending -> don't sent them again if (location.billFwdStatus === "pending" || location.billFwdStatus === "sent") { return false; } // Check if ALL other bills are paid const otherBills = location.bills.filter(bill => bill._id !== currentBillId); // filter only bills billed to tenant // because bills billed to landlord should not trigger forwarding const billsPayedByTenant = otherBills.filter(bill => bill.billedTo === BilledTo.Tenant); if(billsPayedByTenant.length === 0) { // No other bills billed to tenant exist, so do not trigger forwarding return false; } const allOtherBillsPaid = billsPayedByTenant.every(bill => bill.paid === true); return allOtherBillsPaid; };