Updated all components to respect the billedToTenant flag: - LocationCard: Filter bills display and monthlyExpense calculation - ViewLocationCard: Filter bills display and monthlyExpense calculation - HomePage: Update monthlyExpense calculations for month grouping - printActions: Filter barcode print data to only include tenant bills All filtering uses (bill.billedToTenant ?? true) for backward compatibility with existing bills that don't have this property set. This ensures users only see and calculate expenses for bills that are the tenant's responsibility. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
'use server';
|
|
|
|
import { getDbClient } from '../dbClient';
|
|
import { BillingLocation, Bill } from '../db-types';
|
|
import { AuthenticatedUser } from '../types/next-auth';
|
|
import { withUser } from '../auth';
|
|
import { unstable_noStore as noStore } from 'next/cache';
|
|
|
|
export interface PrintBarcodeData {
|
|
locationName: string;
|
|
billName: string;
|
|
barcodeImage: string;
|
|
payedAmount?: number | null;
|
|
}
|
|
|
|
/**
|
|
* Fetches all bills with barcode images for a specific month for printing
|
|
* @param year - Year to fetch data for
|
|
* @param month - Month to fetch data for (1-12)
|
|
* @returns Array of barcode data for printing
|
|
*/
|
|
export const fetchBarcodeDataForPrint = withUser(async (user: AuthenticatedUser, year: number, month: number): Promise<PrintBarcodeData[]> => {
|
|
noStore();
|
|
|
|
const { id: userId } = user;
|
|
const dbClient = await getDbClient();
|
|
|
|
const yearMonth = `${year}-${month.toString().padStart(2, '0')}`;
|
|
|
|
// Fetch all locations for the specific month
|
|
const locations = await dbClient.collection<BillingLocation>("lokacije")
|
|
.find({
|
|
userId, // ensure data belongs to authenticated user
|
|
"yearMonth.year": year,
|
|
"yearMonth.month": month
|
|
})
|
|
.toArray();
|
|
|
|
// Extract and flatten barcode data
|
|
const printData: PrintBarcodeData[] = [];
|
|
|
|
for (const location of locations) {
|
|
for (const bill of location.bills) {
|
|
// Only include bills that are billed to tenant and have barcode images
|
|
if (bill.barcodeImage && bill.barcodeImage.trim() !== "" && (bill.billedToTenant ?? true)) {
|
|
printData.push({
|
|
locationName: location.name,
|
|
billName: bill.name,
|
|
barcodeImage: bill.barcodeImage,
|
|
payedAmount: bill.payedAmount
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Sort by location name, then by bill name for consistent ordering
|
|
printData.sort((a, b) => {
|
|
if (a.locationName !== b.locationName) {
|
|
return a.locationName.localeCompare(b.locationName);
|
|
}
|
|
return a.billName.localeCompare(b.billName);
|
|
});
|
|
|
|
return printData;
|
|
}); |