PHP code example of crdesign8 / laravel-rtc-calculator

1. Go to this page and download the library: Download crdesign8/laravel-rtc-calculator 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/ */

    

crdesign8 / laravel-rtc-calculator example snippets


use Crdesign8\LaravelRtcCalculator\Facades\Rtc;
use Crdesign8\LaravelRtcCalculator\DTOs\ItemDTO;
use Crdesign8\LaravelRtcCalculator\Enums\UnidadeMedida;

$resultado = Rtc::make()
    ->paraFiscal(municipio: 4314902, uf: 'RS')
    ->emitidoEm('2027-01-01T03:00:00-03:00')
    ->addItem(
        ItemDTO::make(numero: 1)
            ->ncm('24021000')
            ->quantidade(222)
            ->unidade(UnidadeMedida::VN)
            ->cst('550')
            ->baseCalculo(1111.00)
            ->cClassTrib('550020')
    )
    ->calcular();

// Acessa os totais calculados
echo $resultado->getTotal()->getVIsTot();      // Imposto Seletivo total
echo $resultado->getTotal()->getVBcIbsCbs();  // Base de cálculo IBS+CBS
echo $resultado->getTotal()->getVCbsTot();    // CBS total

// Acessa um item específico pelo número
$item = $resultado->getItem(1);
echo $item->getCstIs();    // CST do Imposto Seletivo
echo $item->getVIs();      // Valor do IS

$resultado = Rtc::make()
    ->paraFiscal(municipio: 4314902, uf: 'RS')
    ->emitidoEm('2027-01-01T03:00:00-03:00')
    ->addItems([
        ItemDTO::make(1)->ncm('24021000')->quantidade(100)->unidade(UnidadeMedida::VN)
            ->cst('550')->baseCalculo(500.00)->cClassTrib('550020'),
        ItemDTO::make(2)->ncm('22030000')->quantidade(50)->unidade(UnidadeMedida::L)
            ->cst('200')->baseCalculo(200.00)->cClassTrib('200032'),
    ])
    ->calcular();

$xmlNfe = file_get_contents(storage_path('app/nfe-sem-rtc.xml'));

$resultado = Rtc::make()->calcularPorNfe(
    xmlNfe: $xmlNfe,
    rtcPorItem: [
        1 => [
            'cst'               => '200',
            'cClassTrib'        => '200032',
            'tributacaoRegular' => ['cst' => '200', 'cClassTrib' => '200032'],
        ],
        2 => [
            'cst'        => '550',
            'cClassTrib' => '550020',
            'impostoSeletivo' => [
                'cst'              => '000',
                'baseCalculo'      => 250.00,
                'cClassTrib'       => '000001',
                'unidade'          => 'VN',
                'quantidade'       => 10,
                'impostoInformado' => 0,
            ],
        ],
    ],
);

echo $resultado->getTotal()->getVBcIbsCbs(); // base total IBS+CBS
echo $resultado->getTotal()->getVIsTot();    // total IS

// Gera o XML com grupos RTC
$xmlRtc = Rtc::make()->gerarXml($resultado);

// Injeta na NFe existente
$nfeComRtc = Rtc::make()->injetarNfe(
    xmlRtc: $xmlRtc,
    xmlNfe: file_get_contents('nfe-sem-rtc.xml')
);

file_put_contents('nfe-com-rtc.xml', $nfeComRtc);

use Crdesign8\LaravelRtcCalculator\Events\RtcCalculated;

// Em qualquer EventServiceProvider ou via closure:
Event::listen(RtcCalculated::class, function (RtcCalculated $event) {
    Log::info('RTC calculado', [
        'municipio' => $event->dto->getMunicipio(),
        'itens'     => count($event->dto->getItens()),
        'vIsTot'    => $event->result->getTotal()->getVIsTot(),
        'vCbsTot'   => $event->result->getTotal()->getVCbsTot(),
    ]);
});

// config/rtc.php
return [
    'base_url'              => env('RTC_BASE_URL', 'http://localhost:8080'),
    'timeout'               => env('RTC_TIMEOUT', 30),
    'retry_times'           => env('RTC_RETRY_TIMES', 2),
    'retry_sleep_ms'        => env('RTC_RETRY_SLEEP_MS', 500),
    'default_tipo_documento'=> env('RTC_DEFAULT_TIPO_DOCUMENTO', 'NFe'),
    'versao'                => env('RTC_VERSAO', '1.0.0'),
    'logging' => [
        'enabled' => env('RTC_LOGGING_ENABLED', false),
        'channel' => env('RTC_LOGGING_CHANNEL', 'stack'),
    ],
];
bash
php artisan rtc:healthcheck
# Esperado: Calculadora RTC disponível em http://localhost:8080 ✔
bash
php artisan vendor:publish --tag=rtc-config
bash
php artisan rtc:calcular entrada.json

# Salvar o resultado em arquivo
php artisan rtc:calcular entrada.json --saida=resultado.json
bash
php artisan rtc:injetar nfe-sem-rtc.xml resultado.json nfe-com-rtc.xml

# Para CTe ou NFCe
php artisan rtc:injetar nota.xml resultado.json nota-com-rtc.xml --tipo=CTe
bash
php artisan rtc:healthcheck

# Testar uma URL diferente da configurada
php artisan rtc:healthcheck --url=http://meu-servidor:8080
bash
# Pré-requisito: calculadora Java em http://localhost:8080

# 1. Cálculo de tributos (IS + IBS + CBS) com tabela formatada
php examples/01-calcular-tributos.php

# 2. Geração do XML com grupos IS/IBSCBS/ISTot/IBSCBSTot
php examples/02-gerar-xml-rtc.php

# 3. Fluxo completo: calcular → gerar XML → injetar em uma NFe real
php examples/03-fluxo-completo-nfe.php