Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Dto/DpsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ public function __construct(
/** Nome / Razão Social do tomador. */
public string $nomeTomador = '',

/** Código IBGE do município do tomador (7 digits). */
public string $tomadorCodigoMunicipio = '',

/** CEP do tomador (8 digits). */
public string $tomadorCep = '',

/** Logradouro do tomador. */
public string $tomadorLogradouro = '',

/** Número do tomador. */
public string $tomadorNumero = '',

/** Complemento do endereço do tomador. */
public string $tomadorComplemento = '',

/** Bairro do tomador. */
public string $tomadorBairro = '',

/** Inscrição municipal do tomador. */
public string $tomadorInscricaoMunicipal = '',

/** Telefone do tomador. */
public string $tomadorTelefone = '',

/** E-mail do tomador. */
public string $tomadorEmail = '',

/** Whether the provider opts into Simples Nacional. */
public int $opcaoSimplesNacional = 1,

Expand Down
44 changes: 44 additions & 0 deletions src/Xml/XmlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,53 @@ private function buildToma(\DOMDocument $doc, DpsData $dps): \DOMElement
$toma->appendChild($doc->createElement('xNome', htmlspecialchars($dps->nomeTomador, ENT_XML1)));
}

if ($dps->tomadorInscricaoMunicipal !== '') {
$toma->appendChild($doc->createElement('IM', $dps->tomadorInscricaoMunicipal));
}

if ($this->hasTomadorAddress($dps)) {
$end = $doc->createElement('end');
$endNac = $doc->createElement('endNac');
$endNac->appendChild($doc->createElement('cMun', $dps->tomadorCodigoMunicipio));
$endNac->appendChild($doc->createElement('CEP', $dps->tomadorCep));
$end->appendChild($endNac);

if ($dps->tomadorLogradouro !== '') {
$end->appendChild($doc->createElement('xLgr', htmlspecialchars($dps->tomadorLogradouro, ENT_XML1)));
}

if ($dps->tomadorNumero !== '') {
$end->appendChild($doc->createElement('nro', htmlspecialchars($dps->tomadorNumero, ENT_XML1)));
}

if ($dps->tomadorComplemento !== '') {
$end->appendChild($doc->createElement('xCpl', htmlspecialchars($dps->tomadorComplemento, ENT_XML1)));
}

if ($dps->tomadorBairro !== '') {
$end->appendChild($doc->createElement('xBairro', htmlspecialchars($dps->tomadorBairro, ENT_XML1)));
}

$toma->appendChild($end);
}

if ($dps->tomadorTelefone !== '') {
$toma->appendChild($doc->createElement('fone', $dps->tomadorTelefone));
}

if ($dps->tomadorEmail !== '') {
$toma->appendChild($doc->createElement('email', htmlspecialchars($dps->tomadorEmail, ENT_XML1)));
}

return $toma;
}

private function hasTomadorAddress(DpsData $dps): bool
{
return $dps->tomadorCodigoMunicipio !== ''
&& $dps->tomadorCep !== '';
}

private function buildTribFederal(\DOMDocument $doc, DpsData $dps): \DOMElement
{
$tribFed = $doc->createElement('tribFed');
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Xml/XmlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ public function testTomadorCpfBlockIsIncludedWhenDocumentHas11Digits(): void
self::assertSame('12345678901', $nodes->item(0)->textContent);
}

public function testTomadorAddressPhoneAndEmailAreIncludedWhenProvided(): void
{
$dps = $this->makeDps(
documentoTomador: '12345678000195',
nomeTomador: 'Empresa Tomadora S.A.',
tomadorCodigoMunicipio: '3303302',
tomadorCep: '24020077',
tomadorLogradouro: 'Avenida Rio Branco',
tomadorTelefone: '21988887777',
tomadorEmail: 'financeiro@example.test',
);

$xml = $this->builder->buildDps($dps);

self::assertStringContainsString('<toma>', $xml);
self::assertStringContainsString('<end><endNac><cMun>3303302</cMun><CEP>24020077</CEP></endNac><xLgr>Avenida Rio Branco</xLgr></end>', str_replace(["\n", ' '], '', $xml));
self::assertStringContainsString('<fone>21988887777</fone>', $xml);
self::assertStringContainsString('<email>financeiro@example.test</email>', $xml);
}

public function testTomadorBlockIsAbsentWhenDocumentoTomadorIsEmpty(): void
{
$dps = $this->makeDps(documentoTomador: '');
Expand Down Expand Up @@ -350,6 +370,15 @@ private function makeDps(
bool $issRetido = false,
string $documentoTomador = '',
string $nomeTomador = '',
string $tomadorCodigoMunicipio = '',
string $tomadorCep = '',
string $tomadorLogradouro = '',
string $tomadorNumero = '',
string $tomadorComplemento = '',
string $tomadorBairro = '',
string $tomadorInscricaoMunicipal = '',
string $tomadorTelefone = '',
string $tomadorEmail = '',
int $regimeEspecialTributacao = 0,
int $tipoRetencaoIss = 1,
int $opcaoSimplesNacional = 1,
Expand Down Expand Up @@ -380,6 +409,15 @@ private function makeDps(
codigoTributacaoNacional: $codigoTributacaoNacional,
documentoTomador: $documentoTomador,
nomeTomador: $nomeTomador,
tomadorCodigoMunicipio: $tomadorCodigoMunicipio,
tomadorCep: $tomadorCep,
tomadorLogradouro: $tomadorLogradouro,
tomadorNumero: $tomadorNumero,
tomadorComplemento: $tomadorComplemento,
tomadorBairro: $tomadorBairro,
tomadorInscricaoMunicipal: $tomadorInscricaoMunicipal,
tomadorTelefone: $tomadorTelefone,
tomadorEmail: $tomadorEmail,
regimeEspecialTributacao: $regimeEspecialTributacao,
tipoRetencaoIss: $tipoRetencaoIss,
issRetido: $issRetido,
Expand Down
Loading