Skip to main content

O quê / quando

Monte a prestação tipada e transmita ao autorizador. Use homologação até ter evidência de homologação e schemas verificados.

Exemplo

use Suot\Fiscal\Core\Exception\InfrastructureException;
use Suot\Fiscal\Core\Result\RetryMode;
use Suot\Fiscal\Core\ValueObject\Uf;
use Suot\Fiscal\Cte\Data\CteAddress;
use Suot\Fiscal\Cte\Data\CteCargo;
use Suot\Fiscal\Cte\Data\CteEmitter;
use Suot\Fiscal\Cte\Data\CteFreight;
use Suot\Fiscal\Cte\Data\CteLinkedDocument;
use Suot\Fiscal\Cte\Data\CteMunicipality;
use Suot\Fiscal\Cte\Data\CteParty;
use Suot\Fiscal\Cte\Data\CteTax;

$dispatch = new CteMunicipality('4314902', 'Porto Alegre', 'RS');
$origin = $dispatch;
$destination = new CteMunicipality('3550308', 'Sao Paulo', 'SP');

try {
    $result = $cte->issue()
        ->forOperation('ot-123-issue')
        ->inHomologation()
        ->numbered('1', '1') // série, número
        ->issuedIn(new Uf('RS'))
        ->withOperation('5353', 'PRESTACAO DE SERVICO DE TRANSPORTE')
        ->dispatchedFrom($dispatch)
        ->issuedBy(new CteEmitter(
            cnpj: '60708000114',
            ie: '0961234567',
            name: 'EMPRESA EMITENTE HOMOLOGACAO',
            tradeName: 'EMITENTE HOMOLOG',
            address: new CteAddress(
                street: 'Rua Teste',
                number: '100',
                district: 'Centro',
                zip: '90000000',
                municipality: $dispatch,
            ),
        ))
        ->from($origin)
        ->to($destination)
        ->forShipper(new CteParty(/* … */))
        ->deliverTo(new CteParty(/* … */))
        ->carrying(new CteCargo(value: '1000.00', description: 'MERCADORIAS DIVERSAS', weight: '100.0000'))
        ->charging(new CteFreight(totalValue: '100.00'))
        ->taxedBy(new CteTax(icmsValue: '12.00'))
        ->linkedToDocument(new CteLinkedDocument(nfeKey: $chaveNfe55Digitos))
        ->underRntrc('12345678')
        ->handle();
} catch (InfrastructureException $e) {
    $failure = $e->toFailure('issue');
    // Host: alertar; filar retry conforme $failure->retryPolicy (timeout ambíguo → QueryBeforeRetry)
    throw $e;
}

if ($result->isAuthorized()) {
    // Host: persistir accessKey, protocolNumber, submittedDocumentXml, responseXml, receivedAt, operationId
}

if ($result->isRejected()) {
    $failure = $result->failure();
    $policy = $result->retryPolicy();
    match ($policy?->mode) {
        RetryMode::AfterCorrection => /* remontar issue() com dados corrigidos + novo forOperation */,
        RetryMode::QueryBeforeRetry => /* byAccessKey() antes de reemitir */,
        RetryMode::AfterDelay, RetryMode::Immediate => /* filar conforme política */,
        RetryMode::Never, null => /* definitivo; não reenviar */,
    };
}

if ($result->isProcessing()) {
    // Host: consultar por chave/protocolo; não emitir de novo às cegas
}
Prefira linkedToDocument(new CteLinkedDocument(…)). Existe também linkedToNfe(string $nfeKey) como atalho de string.

Result

Ver Resultados da emissão: isAuthorized(), isRejected(), failure(), accessKey, protocolNumber, XMLs. Trilha: Avaliar.