init
This commit is contained in:
10
src/core/juge/index.ts
Normal file
10
src/core/juge/index.ts
Normal file
@ -0,0 +1,10 @@
|
||||
export {
|
||||
evaluateDeal,
|
||||
maxPurchaseForTargetNetMarginPct,
|
||||
} from './marginCalculator';
|
||||
export type { JugeInputs, JugeResult } from './marginCalculator';
|
||||
export {
|
||||
DEFAULT_VAT_ON_MARGIN_RATE,
|
||||
DVF_DISCOUNT_FLASH_PCT,
|
||||
MIN_NET_MARGIN_PCT,
|
||||
} from './thresholds';
|
||||
@ -1,16 +1,16 @@
|
||||
import type { DealTrafficLight } from '../../domain/dealSignals';
|
||||
import {
|
||||
DEFAULT_VAT_ON_MARGIN_RATE,
|
||||
DVF_DISCOUNT_FLASH_PCT,
|
||||
MIN_NET_MARGIN_PCT,
|
||||
} from './thresholds';
|
||||
|
||||
export type DealTrafficLight = 'red' | 'orange' | 'green' | 'green_flash_dvf';
|
||||
export type { DealTrafficLight };
|
||||
|
||||
export interface JugeInputs {
|
||||
purchasePrice: number;
|
||||
resalePrice: number;
|
||||
surfaceM2: number;
|
||||
/** Prix m² de référence marché (DVF / étude locale). */
|
||||
dvfReferencePriceM2?: number | null;
|
||||
worksTotal: number;
|
||||
notaryFeeRate: number;
|
||||
@ -20,11 +20,6 @@ export interface JugeInputs {
|
||||
carryingMonths: number;
|
||||
carryingAnnualRate: number;
|
||||
carryingPrincipal?: number | null;
|
||||
/**
|
||||
* TVA sur marge : approximation prudentielle pour outil terrain.
|
||||
* Base imposable = marge économique avant TVA (cash hors TVA collectée/déductible).
|
||||
* Ajustez avec votre expert-comptable selon votre assiette réelle.
|
||||
*/
|
||||
vatOnMarginRate?: number;
|
||||
}
|
||||
|
||||
@ -38,8 +33,9 @@ export interface JugeResult {
|
||||
breakEvenResalePrice: number;
|
||||
purchasePricePerM2: number;
|
||||
dvfDiscountPct: number | null;
|
||||
/** Sous-cotation vs DVF (prix / m² ≤ référence × (1 − 20 %)). */
|
||||
dvfUnderMarketFlash: boolean;
|
||||
trafficLight: DealTrafficLight;
|
||||
/** 0–100 : marge nette normalisée vs seuil + bonus sous-cotation DVF. */
|
||||
scoreDeal: number;
|
||||
}
|
||||
|
||||
@ -51,9 +47,6 @@ function carryingCostEUR(input: JugeInputs): number {
|
||||
return principal * input.carryingAnnualRate * (input.carryingMonths / 12);
|
||||
}
|
||||
|
||||
/**
|
||||
* Le Juge : synthèse financière go / no-go pour marchand de biens.
|
||||
*/
|
||||
export function evaluateDeal(input: JugeInputs): JugeResult {
|
||||
const vatRate = input.vatOnMarginRate ?? DEFAULT_VAT_ON_MARGIN_RATE;
|
||||
|
||||
@ -81,6 +74,7 @@ export function evaluateDeal(input: JugeInputs): JugeResult {
|
||||
input.surfaceM2 > 0 ? input.purchasePrice / input.surfaceM2 : 0;
|
||||
|
||||
let dvfDiscountPct: number | null = null;
|
||||
let dvfUnderMarketFlash = false;
|
||||
if (
|
||||
input.dvfReferencePriceM2 != null &&
|
||||
input.dvfReferencePriceM2 > 0 &&
|
||||
@ -89,13 +83,15 @@ export function evaluateDeal(input: JugeInputs): JugeResult {
|
||||
dvfDiscountPct =
|
||||
(input.dvfReferencePriceM2 - purchasePricePerM2) /
|
||||
input.dvfReferencePriceM2;
|
||||
dvfUnderMarketFlash =
|
||||
purchasePricePerM2 <=
|
||||
input.dvfReferencePriceM2 * (1 - DVF_DISCOUNT_FLASH_PCT);
|
||||
}
|
||||
|
||||
const trafficLight = resolveTrafficLight(
|
||||
netMarginPct,
|
||||
dvfUnderMarketFlash,
|
||||
dvfDiscountPct,
|
||||
purchasePricePerM2,
|
||||
input.dvfReferencePriceM2,
|
||||
);
|
||||
|
||||
const scoreDeal = computeScoreDeal(netMarginPct, dvfDiscountPct);
|
||||
@ -107,35 +103,34 @@ export function evaluateDeal(input: JugeInputs): JugeResult {
|
||||
vatOnMargin,
|
||||
netMarginAfterVat,
|
||||
netMarginPct,
|
||||
breakEvenResalePrice: computeBreakEvenResale(input, vatRate),
|
||||
breakEvenResalePrice: computeBreakEvenResale(input),
|
||||
purchasePricePerM2,
|
||||
dvfDiscountPct,
|
||||
dvfUnderMarketFlash,
|
||||
trafficLight,
|
||||
scoreDeal,
|
||||
};
|
||||
}
|
||||
|
||||
/** Marge : feu rouge sous 15 %. DVF : flash vert indépendant (signal d’achat). */
|
||||
function resolveTrafficLight(
|
||||
netMarginPct: number,
|
||||
dvfUnderMarketFlash: boolean,
|
||||
dvfDiscountPct: number | null,
|
||||
purchasePricePerM2: number,
|
||||
dvfReferencePriceM2?: number | null,
|
||||
): DealTrafficLight {
|
||||
const underDvfFlash =
|
||||
dvfReferencePriceM2 != null &&
|
||||
dvfReferencePriceM2 > 0 &&
|
||||
purchasePricePerM2 <= dvfReferencePriceM2 * (1 - DVF_DISCOUNT_FLASH_PCT);
|
||||
|
||||
if (netMarginPct < MIN_NET_MARGIN_PCT) {
|
||||
return underDvfFlash ? 'green_flash_dvf' : 'red';
|
||||
return 'red';
|
||||
}
|
||||
if (underDvfFlash) {
|
||||
if (dvfUnderMarketFlash) {
|
||||
return 'green_flash_dvf';
|
||||
}
|
||||
if (dvfDiscountPct != null && dvfDiscountPct > 0.1) {
|
||||
return 'green';
|
||||
}
|
||||
return 'orange';
|
||||
if (netMarginPct < 0.18) {
|
||||
return 'orange';
|
||||
}
|
||||
return 'green';
|
||||
}
|
||||
|
||||
function computeScoreDeal(
|
||||
@ -153,7 +148,7 @@ function computeScoreDeal(
|
||||
return Math.round(Math.min(100, marginScore + dvfBonus));
|
||||
}
|
||||
|
||||
function computeBreakEvenResale(input: JugeInputs, vatRate: number): number {
|
||||
function computeBreakEvenResale(input: JugeInputs): number {
|
||||
const notaryFees = input.purchasePrice * input.notaryFeeRate;
|
||||
const carrying = carryingCostEUR(input);
|
||||
const fixedCosts =
|
||||
@ -165,83 +160,25 @@ function computeBreakEvenResale(input: JugeInputs, vatRate: number): number {
|
||||
input.miscSaleCost;
|
||||
|
||||
const agencyRate = input.saleAgencyFeeRate;
|
||||
const effectiveCoeff = 1 - agencyRate - vatRate * (1 - agencyRate);
|
||||
if (effectiveCoeff <= 0) {
|
||||
const coeff = 1 - agencyRate;
|
||||
if (coeff <= 0) {
|
||||
return Number.POSITIVE_INFINITY;
|
||||
}
|
||||
return fixedCosts / effectiveCoeff;
|
||||
return fixedCosts / coeff;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prix d'achat maximum pour respecter une marge nette cible (linéaire sur coûts).
|
||||
* Utile quand la checklist visite augmente les travaux : recalcul offre max.
|
||||
* Prix d’achat max pour tenir une marge nette cible (ex. 15 % après TVA sur marge).
|
||||
* Recalcul instantané quand la checklist visite augmente les travaux.
|
||||
*/
|
||||
export function maxPurchaseForTargetNetMarginPct(
|
||||
input: Omit<JugeInputs, 'purchasePrice'>,
|
||||
targetNetMarginPct: number,
|
||||
): number {
|
||||
const vatRate = input.vatOnMarginRate ?? DEFAULT_VAT_ON_MARGIN_RATE;
|
||||
const carryingPrincipalFallback = 0;
|
||||
|
||||
const agency = input.saleAgencyFeeRate;
|
||||
const netResaleCoeff = 1 - agency;
|
||||
|
||||
const numerator =
|
||||
netResaleCoeff * input.resalePrice -
|
||||
input.miscSaleCost -
|
||||
input.worksTotal -
|
||||
input.miscAcquisitionCost -
|
||||
(1 + targetNetMarginPct + vatRate * (1 + targetNetMarginPct)) *
|
||||
carryingPrincipalFallback;
|
||||
|
||||
const denominator =
|
||||
(1 + input.notaryFeeRate) *
|
||||
(1 +
|
||||
targetNetMarginPct +
|
||||
vatRate * (1 + targetNetMarginPct) +
|
||||
input.carryingAnnualRate * (input.carryingMonths / 12));
|
||||
|
||||
if (denominator <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let maxPurchase = numerator / denominator;
|
||||
|
||||
const refine = (candidate: number): number => {
|
||||
const trial: JugeInputs = {
|
||||
...input,
|
||||
purchasePrice: candidate,
|
||||
carryingPrincipal: input.carryingPrincipal ?? candidate,
|
||||
};
|
||||
const { netMarginPct } = evaluateDeal(trial);
|
||||
return netMarginPct - targetNetMarginPct;
|
||||
};
|
||||
|
||||
maxPurchase = binarySearchPurchase(input, targetNetMarginPct, vatRate);
|
||||
|
||||
void refine;
|
||||
return Math.max(0, Math.round(maxPurchase));
|
||||
}
|
||||
|
||||
/**
|
||||
* Recherche dichotomique robuste (le lien marge ↔ prix d'achat est affine par morceaux
|
||||
* mais les arrondis / TVA max(0,·) peuvent créer des irrégularités légères).
|
||||
*/
|
||||
export function maxPurchaseForTargetNetMarginPctRobust(
|
||||
input: Omit<JugeInputs, 'purchasePrice'>,
|
||||
targetNetMarginPct: number,
|
||||
): number {
|
||||
return binarySearchPurchase(input, targetNetMarginPct);
|
||||
}
|
||||
|
||||
function binarySearchPurchase(
|
||||
input: Omit<JugeInputs, 'purchasePrice'>,
|
||||
targetNetMarginPct: number,
|
||||
vatRate: number = input.vatOnMarginRate ?? DEFAULT_VAT_ON_MARGIN_RATE,
|
||||
): number {
|
||||
let low = 0;
|
||||
let high = input.resalePrice * 1.5;
|
||||
for (let i = 0; i < 48; i++) {
|
||||
let high = Math.max(input.resalePrice * 1.5, 1);
|
||||
for (let i = 0; i < 56; i++) {
|
||||
const mid = (low + high) / 2;
|
||||
const { netMarginPct } = evaluateDeal({
|
||||
...input,
|
||||
|
||||
Reference in New Issue
Block a user