# Libro de Ventas v1 - EVENTSUD - SOK

**Identificador del Set SII:** `4841001`
**Tipo de Set:** SET LIBRO DE VENTAS - Version 1
**RUT Emisor:** `78073481-7`
**RUT Firmante (RutEnvia):** `16658678-K`
**Track ID:** `249682786`
**FolioNotificacion:** `4841001`
**Fecha:** 2026-05-26
**Estado SII envio:** OK (firma valida + XSD valida).
**Estado SII revision del set:** `SOK - SET DE PRUEBA CORRECTO`

## Contenido del libro

Libro **TipoLibro=ESPECIAL, TipoEnvio=TOTAL** generado a partir de los 8 DTEs
del set basico EVENTSUD (track 0249675518):

| TpoDoc | Folio | TasaImp | FchDoc | RUTDoc | TpoDocRef | FolioDocRef | MntExe | MntNeto | MntIVA | MntTotal |
|---|---|---|---|---|---|---|---|---|---|---|
| 33 | 13 | 19 | 2026-05-26 | 78014612-5 | — | — | 0 | 1.445.256 | 274.599 | 1.719.855 |
| 33 | 14 | 19 | 2026-05-26 | 78014612-5 | — | — | 0 | 10.708.967 | 2.034.704 | 12.743.671 |
| 33 | 25 | 19 | 2026-05-26 | 78014612-5 | — | — | 35.535 | 2.076.362 | 394.509 | 2.506.406 |
| 33 | 26 | 19 | 2026-05-26 | 78014612-5 | — | — | 13.720 | 4.598.448 | 873.705 | 5.485.873 |
| 61 | 11 | 19 | 2026-05-26 | 78014612-5 | **33** | **13** | 0 | 0 | 0 | 0 |
| 61 | 18 | 19 | 2026-05-26 | 78014612-5 | **33** | **14** | 0 | 5.252.557 | 997.986 | 6.250.543 |
| 61 | 19 | 19 | 2026-05-26 | 78014612-5 | **33** | **25** | 35.535 | 2.076.362 | 394.509 | 2.506.406 |
| 56 | 6 | 19 | 2026-05-26 | 78014612-5 | **61** | **11** | 0 | 0 | 0 | 0 |

**Resumen periodo:**
- TpoDoc=33: TotDoc=4, TotMntExe=49.255, TotMntNeto=18.829.033, TotMntIVA=3.577.517, TotMntTotal=22.455.805
- TpoDoc=56: TotDoc=1, TotMntExe=0, TotMntNeto=0, TotMntIVA=0, TotMntTotal=0
- TpoDoc=61: TotDoc=3, TotMntExe=35.535, TotMntNeto=7.328.919, TotMntIVA=1.392.495, TotMntTotal=8.756.949

## Fixes incorporados al motor para este caso

1. **fix(dte): firmar libro construyendo SignedInfo dentro del DOM** (commit `f44525a`)
   - Inclusive C14N hereda `xmlns:xsi` del root al SignedInfo cuando se
     verifica. Mi version aislada no lo tenia → LRF. Fix: construir Signature
     con createElementNS dentro del DOM del libro antes de calcular C14N.

2. **fix(dte): caratula libros** (commit `c97d47a`)
   - `RutEnvia` = RUT firmante (cert) = `16658678-K`, no RUT emisor.
   - `TipoLibro=ESPECIAL` para sets de certificación (no MENSUAL).
   - `FolioNotificacion` solo se emite cuando TipoLibro=ESPECIAL.
   - `TpoDocRef`+`FolioDocRef` en Detalle de NC/ND, apuntando a la factura
     referenciada (extraido del XML del DTE).

## Como reproducir

```bash
php artisan tinker --execute='
use App\Models\Dte\DteDocumento;
use App\Models\Parametros\ParEmpresa;
use App\Services\FacturacionElectronica\Services\CertificateService;
use App\Services\FacturacionElectronica\Services\EnvelopeService;
use App\Services\FacturacionElectronica\Services\LibroBuilder;

$emp = ParEmpresa::find(1);
$cert = app(CertificateService::class)->loadCertificate(1);
$rutEnvia = $cert->getId();
$dtes = DteDocumento::where("track_id", "0249675518")
    ->whereIn("tipo_dte", [33,39,41,56,61])
    ->orderBy("tipo_dte")->orderBy("folio")->get();
$r = app(LibroBuilder::class)->buildVentas($emp, "2026-05", $dtes, 4841001, $rutEnvia, "ESPECIAL");
$xml = app(EnvelopeService::class)->firmarLibro($r["xml"], $cert, $r["id"]);
file_put_contents("libro.xml", $xml);
'
```

## Notas operacionales

- **TipoLibro=ESPECIAL es obligatorio en sets de certificación.** MENSUAL
  rechaza con LBR-3 si lleva FolioNotificacion.
- **FolioNotificacion debe incrementarse en cada reintento del mismo set**
  (ver memoria feedback-sii-libros-folio-notificacion).
- **RutEnvia = RUT del firmante del cert** (no del emisor). Coincide con
  `rutSender` del multipart upload — si difieren el SII rechaza con
  CRT-3 "RUT Envia Diferente al registrado en Upload".
