feat(depreciation): apply Fanavaran tables and age eligibility

Use the supplied part, severity and age codes for depreciation. Exclude vehicles older than ten from calculated and manual amounts, preserve historical assessments, and send zero depreciation to Fanavaran for ineligible vehicles.
This commit is contained in:
SepehrYahyaee
2026-11-23 11:10:55 +03:30
parent 3bf6fcc8b0
commit c74b29337e
14 changed files with 1424 additions and 208 deletions
+107
View File
@@ -0,0 +1,107 @@
# Fanavaran depreciation tables
The calculation now uses [ضرایب افت قیمت.xlsx](<../ضرایب افت قیمت.xlsx>) and [افت قیمت سن.png](<../افت قیمت سن.png>). Their reference data lives in `src/static/fanavaran-price-drop.ts`; the previous fixed production-year table and part coefficients are retired.
## Part and severity codes
These codes identify depreciation categories, **not** tenant car-component IDs. The original component ID remains `partId`. The expert can explicitly choose a category when a tenant's component caption is ambiguous. Clear names receive a suggestion; numeric IDs are never assumed to mean the same thing across insurers.
The depreciation path preserves saved tenant captions even when their component IDs collide with the legacy global fallback snapshot. It does not replace a tenant's saved roof caption with another tenant's trunk-floor-tray caption merely because both use ID 52.
| Category code | Caption | Minor / Moderate / Severe codes | Coefficients |
| --- | --- | --- | --- |
| 1 | سقف | 1 / 2 / 3 | 3 / 5 / 7 |
| 2 | کلاف | 4 / 5 / 6 | 2 / 3 / 4 |
| 3 | ستون | 7 / 8 / 9 | 2 / 3 / 4 |
| 4 | درب موتور | 10 / 11 / 12 | 2 / 3 / 4 |
| 5 | سینی جلو | 13 / 14 / 15 | 1 / 2 / 3 |
| 6 | شاسی جلو | 16 / 17 / 18 | 3 / 5 / 7 |
| 7 | گلگیر جلو | 19 / 20 / 21 | 1 / 2 / 3 |
| 8 | درب جلو | 22 / 23 / 24 | 1 / 2 / 3 |
| 9 | درب عقب | 25 / 26 / 27 | 1 / 2 / 3 |
| 10 | رکاب | 28 / 29 / 30 | 1 / 2 / 3 |
| 11 | گلگیر عقب | 31 / 32 / 33 | 2 / 3 / 5 |
| 12 | درب صندوق | 34 / 35 / 36 | 1 / 3 / 5 |
| 13 | سینی عقب | 37 / 38 / 39 | 1 / 2 / 3 |
| 14 | سینی کف صندوق | 40 / 41 / 42 | 2 / 4 / 5 |
| 15 | شاسی عقب | 43 / 44 / 45 | 2 / 4 / 6 |
| 16 | کف اتاق | 46 / 47 / 48 | 4 / 6 / 8 |
| 17 | بلوک سیلندر | Severe only: 49 | Severe only: 3 |
Front/rear bumpers are absent from the supplied table and no longer receive an automatic coefficient. Rear tray and trunk-floor tray are separate. Glass, pillar trim and roof accessories are not mapped to a door/pillar/roof just because that word occurs in their caption. A submitted part must exist among the case's selected damaged parts, and each part may occur only once per calculation. Non-catalog internal rows without a component ID remain available through the existing manual-total option.
## Age codes
| Age at accident | Code | Coefficient |
| --- | --- | --- |
| 0 | 1 | 3 |
| 1 | 2 | 2.9 |
| 2 | 3 | 2.8 |
| 3 | 4 | 2.7 |
| 4 | 5 | 2.6 |
| 5 | 6 | 2.5 |
| 6 | 7 | 2.4 |
| 7 | 8 | 2.3 |
| 8 | 9 | 2.2 |
| 9 | 10 | 2.1 |
| 10 | 11 | 2.05 |
Suggested age is the Jalali accident year minus the **damaged vehicle's** Jalali model year. It does not use the current year or the guilty party's vehicle, even when an expert or the guilty party owns the claim record. Experts can confirm/correct age directly. A Gregorian model year without month/day is not converted speculatively; use explicit age instead.
The screenshot marks these age rows effective from `1404/05/28`. Cases with a known earlier accident date cannot use automatic calculation from this table. Cars **older than ten at the accident are not eligible for depreciation**. Their calculated and manual depreciation must be zero, with no invented age/coefficient code. Exactly ten remains eligible. Positive manual totals require a known or explicitly entered eligible age. The UI offers an over-ten option, explains the exclusion and only lets the expert save zero. For pre-table accident dates, the manual-total path remains subject to the same age rule.
## Formula and currency
The supplied documents define reference rows, not a replacement formula. The existing formula is retained with the new coefficients:
```text
Depreciation = vehicle market price × age coefficient × sum(part severity coefficients) / 400
```
All amounts are **Rials**. Integer arithmetic in hundredths of the coefficient prevents floating-point rounding drift; the result is rounded to whole Rials. Example: a 400,000,000 Rial car aged 3 with moderate trunk-floor-tray damage produces `400000000 × 2.7 × 4 / 400 = 10800000` Rials.
## Existing API routes
```text
GET /v2/expert-claim/request/:claimRequestId/price-drop
PUT /v2/expert-claim/request/:claimRequestId/price-drop
```
The context includes `priceDropCatalog` with `fanavaranPartId`, `severities` and `severityCodes`; `ageCatalog`; `suggestedVehicleAge`; `accidentYear`; `calculationVersion`; `ageTableEffectiveFrom`; and `calculationUnavailableReason` when the accident predates the table.
Example calculation, where `9876` is an illustrative component ID actually selected on that case:
```json
{
"carPrice": 400000000,
"vehicleAge": 3,
"partSeverities": [
{ "partId": 9876, "fanavaranPartId": 14, "severity": "Moderate" }
]
}
```
`fanavaranPartId` is optional when a safe name mapping exists. The older `carModelYear` input remains supported when an accident date is available. Clients never choose coefficients directly: the API derives them from the static source.
`evaluation.priceDrop` stores version `fanavaran-v1`, age, `fanavaranAgeId` and the coefficient. Each saved part line includes the actual component `partId`, category `fanavaranPartId` and `fanavaranSeverityId`. The response and strict Mongoose schema retain these fields.
For eligible ages, the manual path remains (zero is also allowed without an age):
```json
{ "manualPriceDrop": 0 }
```
Calculated requests must omit `manualPriceDrop`; its presence explicitly chooses the manual path. The expert UI separates these writes so a previously saved total cannot silently override recalculation. Manual entry also has an optional `carPrice` field for Fanavaran's market-value data. Manual writes preserve an existing market price when omitted and clear obsolete calculated rows. `manualOverride` now survives schema casting.
## Fanavaran writes and rollout
The existing documented write contracts still receive the final total in third-party `DropAmountAdditionsDeductions` or car-body `DropAmount`. Category/severity/age codes are calculation/audit metadata; they are not inserted into undocumented outgoing fields. GEN.13 is read-only in the currently supplied API contract.
Saved historical assessments are never automatically recalculated. When a known over-ten vehicle has an old positive total, a new Fanavaran submission uses zero; the stored historical assessment is preserved until explicitly corrected. The UI identifies results without the new calculation version and lets the expert explicitly recalculate them. Deploy the API first, then the expert UI. No new environment variables or database migration are required.
## Verification
- All 17 category codes/captions and all 49 severity codes/coefficients were compared directly with the supplied workbook.
- API regression tests cover age boundaries, date validity, separate severity codes, tenant-ID collisions, explicit mappings, real schema persistence, manual zero, duplicate/unselected parts, vehicle-party isolation and failed writes preserving previous results.
- Expert UI regression tests cover request construction and rendered controls. Browser checks at 1440×1100 and 390×844 cover labels, keyboard, overflow, age zero, severe-only cylinder block, HTTP-error retry and separate calculated/manual requests. Browser API calls used synthetic intercepted responses.
- The full API suite additionally reports ten failures in the unchanged `fanavaran-submit-overrides.spec.ts` fixture, reproduced on an archive of the unmodified commit. These fail at the existing vehicle-review prerequisite before reaching their intended assertions; they are unrelated to depreciation.