Checkpoint: Ajout de 3 champs métier aux factures (Service concerné, Type d'achat, Ventilation comptable) avec interface d'administration pour gérer les listes enrichissables. Les champs apparaissent dans la liste des factures et dans le formulaire d'édition.

This commit is contained in:
Manus
2026-02-11 08:03:30 -05:00
parent 280abba902
commit 4a8be36d00
12 changed files with 1665 additions and 4 deletions

View File

@@ -20,7 +20,13 @@ import {
LlmLog,
importSettings,
InsertImportSettings,
ImportSettings
ImportSettings,
departmentList,
InsertDepartment,
Department,
accountingAllocationList,
InsertAccountingAllocation,
AccountingAllocation
} from "../drizzle/schema";
import { ENV } from './_core/env';
@@ -480,3 +486,72 @@ export async function upsertImportSettings(data: InsertImportSettings): Promise<
return inserted[0]!;
}
}
// ============= DEPARTMENT LIST OPERATIONS =============
export async function getDepartmentsByUser(userId: number): Promise<Department[]> {
const db = await getDb();
if (!db) return [];
return await db.select().from(departmentList).where(eq(departmentList.userId, userId)).orderBy(departmentList.name);
}
export async function createDepartment(data: InsertDepartment): Promise<Department> {
const db = await getDb();
if (!db) throw new Error("Database not available");
const [department] = await db.insert(departmentList).values(data).$returningId();
return await db.select().from(departmentList).where(eq(departmentList.id, department.id)).then(rows => rows[0]);
}
export async function deleteDepartment(id: number): Promise<void> {
const db = await getDb();
if (!db) throw new Error("Database not available");
await db.delete(departmentList).where(eq(departmentList.id, id));
}
// ============= ACCOUNTING ALLOCATION LIST OPERATIONS =============
export async function getAccountingAllocationsByUser(userId: number): Promise<AccountingAllocation[]> {
const db = await getDb();
if (!db) return [];
return await db.select().from(accountingAllocationList).where(eq(accountingAllocationList.userId, userId)).orderBy(accountingAllocationList.name);
}
export async function createAccountingAllocation(data: InsertAccountingAllocation): Promise<AccountingAllocation> {
const db = await getDb();
if (!db) throw new Error("Database not available");
const [allocation] = await db.insert(accountingAllocationList).values(data).$returningId();
return await db.select().from(accountingAllocationList).where(eq(accountingAllocationList.id, allocation.id)).then(rows => rows[0]);
}
export async function deleteAccountingAllocation(id: number): Promise<void> {
const db = await getDb();
if (!db) throw new Error("Database not available");
await db.delete(accountingAllocationList).where(eq(accountingAllocationList.id, id));
}
// ============= INITIALIZE DEFAULT VALUES =============
export async function initializeDefaultLists(userId: number): Promise<void> {
const db = await getDb();
if (!db) return;
// Initialize default departments
const defaultDepartments = ["DSI", "TRAVAUX", "AUTRE"];
for (const name of defaultDepartments) {
try {
await db.insert(departmentList).values({ userId, name }).onDuplicateKeyUpdate({ set: { name } });
} catch (error) {
// Ignore duplicate errors
}
}
// Initialize default accounting allocations
const defaultAllocations = ["TOUS", "PA", "HEP", "SANITAIRE", "AUTRE"];
for (const name of defaultAllocations) {
try {
await db.insert(accountingAllocationList).values({ userId, name }).onDuplicateKeyUpdate({ set: { name } });
} catch (error) {
// Ignore duplicate errors
}
}
}