4 decimal places in STOC.PRET caused FACT-008 errors during invoicing because pack_facturare.descarca_gestiune does exact price matching. Also add pack_facturare flow analysis documentation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
4.2 KiB
4.2 KiB
pack_facturare — Invoicing Flow Analysis
Call chain
initializeaza_date_factura(...)— setsntip,nluna,nan,nid_sucursala, etc.adauga_articol_factura(...)— inserts intoVANZARI_DETALII_TEMPscrie_factura2(...)— readsVANZARI_DETALII_TEMP, loops articles, callscontabilizeaza_articolcontabilizeaza_articol(detalii_articol)— for ntip<=20 (facturi), callsdescarca_gestiunedescarca_gestiune(...)— looks up STOC and decrements
Key parameter mapping (adauga_articol_factura -> descarca_gestiune)
adauga_articol_factura stores into VANZARI_DETALII_TEMP, then contabilizeaza_articol passes to descarca_gestiune:
| descarca_gestiune param | Source in VANZARI_DETALII_TEMP | adauga_articol_factura param |
|---|---|---|
| V_ID_ARTICOL | id_articol | V_ID_ARTICOL (param 2) |
| V_SERIE | serie | V_SERIE (param 3) |
| V_PRET_ACHIZITIE | pret_achizitie | V_PRET_ACHIZITIE_TEMP (param 7) |
| V_PRETD | pretd | V_PRETD (param 8) |
| V_ID_VALUTAD | id_valutad | V_ID_VALUTAD (param 9) |
| V_PRETV_ALES | pretv_orig | V_PRETV_ORIG (param 22) |
| V_PRET_UNITAR | pret | V_PRET_TEMP (param 10) |
| V_PROC_TVAV | proc_tvav | calculated from JTVA_COLOANE |
| V_CANTE | cantitate | V_CANTITATE (param 14) |
| V_DISCOUNT | discount_unitar | V_DISCOUNT_UNITAR (param 15) |
| V_ID_GESTIUNE | id_gestiune | V_ID_GESTIUNE (param 6) |
| V_CONT | cont | V_CONT (param 16) |
descarca_gestiune STOC lookup (ELSE branch, normal invoice ntip=1)
File: api/database-scripts/08_PACK_FACTURARE.pck, body around line 8326-8457.
The ELSE branch (default for ntip=1 factura simpla) queries STOC with exact match on ALL these:
WHERE A.ID_ARTICOL = V_ID_ARTICOL
AND A.ID_GESTIUNE = V_ID_GESTIUNE
AND NVL(A.CONT, 'XXXX') = V_CONT -- e.g. '371'
AND A.PRET = V_PRET_ACHIZITIE -- EXACT match on acquisition price
AND A.PRETD = V_PRETD
AND NVL(A.ID_VALUTA, 0) = DECODE(V_ID_VALUTAD, -99, 0, NVL(V_ID_VALUTAD, 0))
AND A.PRETV = V_PRETV_ALES -- sale price (0 for PA gestiuni)
AND NVL(A.SERIE, '+_') = NVL(V_SERIE, '+_')
AND A.LUNA = pack_facturare.nluna
AND A.AN = pack_facturare.nan
AND A.CANTS + A.CANT + nvl(b.cant, 0) > a.cante + nvl(b.cante, 0)
AND NVL(A.ID_PART_REZ, 0) = NVL(V_ID_PART_REZ, 0)
AND NVL(A.ID_LUCRARE_REZ, 0) = NVL(V_ID_LUCRARE_REZ, 0)
If no rows found -> FACT-008 error ("Articolul X nu mai e in stoc!").
Common FACT-008 causes
- Price precision mismatch — STOC.PRET has different decimal places than what facturare sends. Oracle compares with
=, so29.915 != 29.92. Always use 2 decimals for PRET in STOC/RUL. - PRETV mismatch — For gestiuni la pret de achizitie (PA), STOC.PRETV should be 0. If non-zero, won't match.
- Wrong LUNA/AN — Stock exists but for a different month/year than the invoice session.
- Wrong CONT — e.g. stock has CONT='345' but invoice expects '371'.
- Wrong ID_GESTIUNE — stock in gestiune 2 but invoicing from gestiune 1.
- No available quantity —
CANTS + CANT <= CANTE(already fully sold).
CASE branches in descarca_gestiune
| Condition | Source table | Use case |
|---|---|---|
| ntip IN (8,9) | RUL (returns) | Factura de retur |
| ntip = 24 | RUL (returns) | Aviz de retur |
| ntip = nTipFacturaHotel | STOC (no cont/pret filter) | Hotel invoice |
| ntip IN (nTipFacturaRestaurant, nTipNotaPlata) | STOC + RUL_TEMP | Restaurant |
| V_CANTE < 0 with clistaid containing ':' | RUL + STOC | Mixed return+sale |
| ELSE (default, ntip=1) | STOC | Normal invoice |
lnFacturareFaraStoc option
If RF_FACTURARE_FARA_STOC = 1 in firma options, the ELSE branch includes a UNION ALL with TIP=3 from NOM_ARTICOLE — allowing invoicing without stock. Otherwise, FACT-008 is raised.
Important: scripts inserting into STOC/RUL
When creating inventory notes or any stock entries programmatically, ensure:
- PRET (acquisition price): 2 decimals — must match exactly what facturare will send
- PRETV (sale price): 0 for gestiuni la pret de achizitie (PA)
- PRETD: match expected value (usually 0 for RON)
- CONT/ACONT: must match the gestiune configuration
- LUNA/AN: must match the invoicing period