recherche

This commit is contained in:
Bastien COIGNOUX
2026-05-04 21:52:51 +02:00
parent 432f8ce176
commit 2b8741de08
30 changed files with 2317 additions and 246 deletions

View File

@ -16,6 +16,8 @@ export type AnalyseFormInput = {
taxe_fonciere_annuelle?: number;
charges_copropriete_mensuelle?: number;
prix_revente_cible?: number;
/** Prix de revente estimé au m² (marché). */
prix_revente_m2?: number;
frais_agence_vente_pct?: number;
taux_impot?: number;
};
@ -98,6 +100,9 @@ function formToRecord(form: AnalyseFormInput, calc: AnalyseCalculated): Record<s
taxe_fonciere_annuelle: form.taxe_fonciere_annuelle,
charges_copropriete_mensuelle: form.charges_copropriete_mensuelle,
prix_revente_cible: form.prix_revente_cible,
...(form.prix_revente_m2 !== undefined && form.prix_revente_m2 !== null
? { prix_revente_m2: form.prix_revente_m2 }
: {}),
frais_agence_vente_pct: form.frais_agence_vente_pct,
taux_impot: form.taux_impot,
marge_brute: calc.marge_brute,
@ -153,6 +158,30 @@ export function useAnalyse(bienId: string | undefined) {
},
});
const patchMutation = useMutation({
mutationFn: async (patch: { prix_revente_m2?: number | null }) => {
if (!bienId || !uid) throw new Error('Données manquantes');
const res = await pb.collection('analyses_financieres').getList<AnalyseFinanciereRecord>(1, 1, {
filter: `bien="${bienId}" && user="${uid}"`,
sort: '-id',
});
const existing = res.items[0];
if (existing) {
return pb.collection('analyses_financieres').update<AnalyseFinanciereRecord>(existing.id, patch);
}
return pb.collection('analyses_financieres').create<AnalyseFinanciereRecord>({
user: uid,
bien: bienId,
type_bien_fiscal: 'ancien',
...patch,
});
},
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: ['analyse_financiere', bienId, uid] });
void queryClient.invalidateQueries({ queryKey: ['bien_detail', bienId] });
},
});
return {
analyse: query.data ?? null,
isLoading: query.isPending,
@ -161,6 +190,8 @@ export function useAnalyse(bienId: string | undefined) {
fetchAnalyse: query.refetch,
saveAnalyse: saveMutation.mutateAsync,
isSaving: saveMutation.isPending,
patchAnalyse: patchMutation.mutateAsync,
isPatching: patchMutation.isPending,
calculateResults,
};
}