Checkpoint: Ajout du bouton Validation BAP dans la page Factures BAP : nouveau champ bapValidated/bapValidatedAt dans le schéma, migration DB, route tRPC validateBAP avec vérification des critères (Score 100%, non exportée, isSubscription=0, service+typeAchat+ventilation remplis), bouton vert actif/désactivé avec tooltip explicatif, badge "Validé" pour les factures déjà validées

This commit is contained in:
Manus
2026-03-06 04:17:53 -05:00
parent f16efa4197
commit 9868a1afda
7 changed files with 1430 additions and 2 deletions

View File

@@ -375,6 +375,43 @@ export const appRouter = router({
await deleteInvoice(input.id);
return { success: true };
}),
validateBAP: protectedProcedure
.input(z.object({ id: z.number() }))
.mutation(async ({ input, ctx }) => {
const invoice = await getInvoiceById(input.id);
if (!invoice || invoice.userId !== ctx.user.id) {
throw new TRPCError({ code: "NOT_FOUND" });
}
// Vérifier les critères de validation BAP
const score = invoice.qualityScore || 0;
const isNotExported = invoice.exportStatus !== "exported";
const isNotSubscription = invoice.isSubscription === 0;
const hasService = !!invoice.serviceConcerne;
const hasTypeAchat = !!invoice.typeAchat;
const hasVentilation = !!invoice.ventilationComptable;
if (score < 100) {
throw new TRPCError({ code: "BAD_REQUEST", message: "Le score de qualité doit être à 100% pour valider" });
}
if (!isNotExported) {
throw new TRPCError({ code: "BAD_REQUEST", message: "La facture a déjà été exportée" });
}
if (!isNotSubscription) {
throw new TRPCError({ code: "BAD_REQUEST", message: "La facture est marquée comme abonnement" });
}
if (!hasService || !hasTypeAchat || !hasVentilation) {
throw new TRPCError({ code: "BAD_REQUEST", message: "Les champs Service, Type d'achat et Ventilation doivent être remplis" });
}
await updateInvoice(input.id, {
bapValidated: 1,
bapValidatedAt: new Date(),
});
return { success: true, validatedAt: new Date() };
}),
search: protectedProcedure
.input(z.object({ query: z.string() }))