PHP code example of djasoft / sunat-comprobantes

1. Go to this page and download the library: Download djasoft/sunat-comprobantes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

djasoft / sunat-comprobantes example snippets


use Djasoft\Sunat\Calculator;

$calc = new Calculator;

// buildDetail recibe el precio unitario CON IGV, que es el que ve el cliente
// y el que sueles tener guardado. Él calcula el valor sin IGV.
$data = ['details' => [
    $calc->buildDetail('SKU-001', 'NIU', 'Teclado mecánico', 2, 118.00),
    $calc->buildDetail('SKU-002', 'NIU', 'Libro', 1, 50.00, Calculator::EXONERADO),
]];

$calc->calculate($data);

$data['mtoOperGravadas'];   // 200.00
$data['mtoOperExoneradas']; //  50.00
$data['mtoIGV'];            //  36.00
$data['valorVenta'];        // 250.00
$data['subTotal'];          // 286.00
$data['mtoImpVenta'];       // 286.00  (redondeado al décimo inferior)
$data['redondeo'];          //   0.00
$data['legends'][0]['value']; // "DOSCIENTOS OCHENTA Y SEIS CON 00/100 SOLES"

use Djasoft\Sunat\QrContent;

$contenido = (new QrContent)->build(
    ruc:            '20123456789',
    tipoDocumento:  QrContent::FACTURA,
    serie:          'F001',
    correlativo:    '00000123',
    igv:            18.00,
    total:          118.00,
    fechaEmision:   '2026-08-15',
    tipoDocCliente: '6',
    numDocCliente:  '20987654321',
    hash:           $digestDelXmlFirmado,
);

// 20123456789|01|F001|00000123|18.00|118.00|2026-08-15|6|20987654321|abc…|

use BaconQrCode\Renderer\{ImageRenderer, RendererStyle\RendererStyle};
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
use BaconQrCode\Writer;

$svg = (new Writer(new ImageRenderer(new RendererStyle(220, 1), new SvgImageBackEnd)))
    ->writeString($contenido, 'UTF-8');