import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); // Lundi de la semaine (en UTC) pour la date courante function mondayOfThisWeekUTC(now = new Date()) { const d = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())); const day = d.getUTCDay(); // 0=Dim, 1=Lun, ... const diffToMonday = (day + 6) % 7; // 0 si lundi d.setUTCDate(d.getUTCDate() - diffToMonday); d.setUTCHours(0, 0, 0, 0); return d; } // Retourne les 5 dates Lundi→Vendredi (UTC, à minuit) function currentWeekDates() { const monday = mondayOfThisWeekUTC(); return Array.from({ length: 5 }, (_, i) => { const d = new Date(monday); d.setUTCDate(monday.getUTCDate() + i); return d; }); } function rndInt(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1)) + min; } function rndAmount(minCents: number, maxCents: number) { const cents = rndInt(minCents, maxCents); return (cents / 100).toFixed(2); // string (ex: "123.45") } async function main() { // On veut explicitement G503 (mileage) et G517 (remboursement) const wanted = ['G57', 'G517'] as const; const codes = await prisma.bankCodes.findMany({ where: { bank_code: { in: wanted as unknown as string[] } }, select: { id: true, bank_code: true }, }); const map = new Map(codes.map(c => [c.bank_code, c.id])); for (const c of wanted) { if (!map.has(c)) throw new Error(`Bank code manquant: ${c}`); } const employees = await prisma.employees.findMany({ select: { id: true } }); if (!employees.length) { console.warn('Aucun employé — rien à insérer.'); return; } const weekDays = currentWeekDates(); // Règles: // - (index % 5) === 0 -> mileage G503 (km) // - (index % 5) === 1 -> remboursement G517 ($) // Les autres: pas de dépense // On met la dépense un des jours de la semaine (déterministe mais varié). let created = 0; for (let ei = 0; ei < employees.length; ei++) { const e = employees[ei]; const ts = await prisma.timesheets.findFirst({ where: { employee_id: e.id }, select: { id: true }, orderBy: { id: 'asc' }, // ajuste si tu préfères par date }); if (!ts) continue; const dayIdx = ei % 5; // 0..4 -> répartit sur la semaine const date = weekDays[dayIdx]; if (ei % 5 === 0) { // Mileage (G503) — amount = km const km = rndInt(10, 180); // 10..180 km await prisma.expenses.create({ data: { timesheet_id: ts.id, bank_code_id: map.get('G57')!, date, amount: km.toString(), // on stocke le nombre de km dans amount (si tu as un champ "quantity_km", remplace ici) attachement: null, description: `Mileage ${km} km (emp ${e.id})`, is_approved: Math.random() < 0.6, supervisor_comment: Math.random() < 0.2 ? 'OK' : null, }, }); created++; } else if (ei % 5 === 1) { // Remboursement (G517) — amount = $ const dollars = rndAmount(2000, 25000); // 20.00$..250.00$ await prisma.expenses.create({ data: { timesheet_id: ts.id, bank_code_id: map.get('G517')!, date, amount: dollars, attachement: null, description: `Remboursement ${dollars}$ (emp ${e.id})`, is_approved: Math.random() < 0.6, supervisor_comment: Math.random() < 0.2 ? 'OK' : null, }, }); created++; } } const total = await prisma.expenses.count(); console.log(`✓ Expenses: ${created} nouvelles lignes, ${total} total rows (semaine courante)`); } main().finally(() => prisma.$disconnect());