Skip to content

Commit ae8519f

Browse files
authored
Merge pull request #15 from LibreCodeCoop/feat/tomador-address-optional-fields
feat: include optional tomador address and contact in DPS XML
2 parents a7e8d62 + 1e71b2b commit ae8519f

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/Dto/DpsData.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,33 @@ public function __construct(
6060
/** Nome / Razão Social do tomador. */
6161
public string $nomeTomador = '',
6262

63+
/** Código IBGE do município do tomador (7 digits). */
64+
public string $tomadorCodigoMunicipio = '',
65+
66+
/** CEP do tomador (8 digits). */
67+
public string $tomadorCep = '',
68+
69+
/** Logradouro do tomador. */
70+
public string $tomadorLogradouro = '',
71+
72+
/** Número do tomador. */
73+
public string $tomadorNumero = '',
74+
75+
/** Complemento do endereço do tomador. */
76+
public string $tomadorComplemento = '',
77+
78+
/** Bairro do tomador. */
79+
public string $tomadorBairro = '',
80+
81+
/** Inscrição municipal do tomador. */
82+
public string $tomadorInscricaoMunicipal = '',
83+
84+
/** Telefone do tomador. */
85+
public string $tomadorTelefone = '',
86+
87+
/** E-mail do tomador. */
88+
public string $tomadorEmail = '',
89+
6390
/** Whether the provider opts into Simples Nacional. */
6491
public int $opcaoSimplesNacional = 1,
6592

src/Xml/XmlBuilder.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,53 @@ private function buildToma(\DOMDocument $doc, DpsData $dps): \DOMElement
178178
$toma->appendChild($doc->createElement('xNome', htmlspecialchars($dps->nomeTomador, ENT_XML1)));
179179
}
180180

181+
if ($dps->tomadorInscricaoMunicipal !== '') {
182+
$toma->appendChild($doc->createElement('IM', $dps->tomadorInscricaoMunicipal));
183+
}
184+
185+
if ($this->hasTomadorAddress($dps)) {
186+
$end = $doc->createElement('end');
187+
$endNac = $doc->createElement('endNac');
188+
$endNac->appendChild($doc->createElement('cMun', $dps->tomadorCodigoMunicipio));
189+
$endNac->appendChild($doc->createElement('CEP', $dps->tomadorCep));
190+
$end->appendChild($endNac);
191+
192+
if ($dps->tomadorLogradouro !== '') {
193+
$end->appendChild($doc->createElement('xLgr', htmlspecialchars($dps->tomadorLogradouro, ENT_XML1)));
194+
}
195+
196+
if ($dps->tomadorNumero !== '') {
197+
$end->appendChild($doc->createElement('nro', htmlspecialchars($dps->tomadorNumero, ENT_XML1)));
198+
}
199+
200+
if ($dps->tomadorComplemento !== '') {
201+
$end->appendChild($doc->createElement('xCpl', htmlspecialchars($dps->tomadorComplemento, ENT_XML1)));
202+
}
203+
204+
if ($dps->tomadorBairro !== '') {
205+
$end->appendChild($doc->createElement('xBairro', htmlspecialchars($dps->tomadorBairro, ENT_XML1)));
206+
}
207+
208+
$toma->appendChild($end);
209+
}
210+
211+
if ($dps->tomadorTelefone !== '') {
212+
$toma->appendChild($doc->createElement('fone', $dps->tomadorTelefone));
213+
}
214+
215+
if ($dps->tomadorEmail !== '') {
216+
$toma->appendChild($doc->createElement('email', htmlspecialchars($dps->tomadorEmail, ENT_XML1)));
217+
}
218+
181219
return $toma;
182220
}
183221

222+
private function hasTomadorAddress(DpsData $dps): bool
223+
{
224+
return $dps->tomadorCodigoMunicipio !== ''
225+
&& $dps->tomadorCep !== '';
226+
}
227+
184228
private function buildTribFederal(\DOMDocument $doc, DpsData $dps): \DOMElement
185229
{
186230
$tribFed = $doc->createElement('tribFed');

tests/Unit/Xml/XmlBuilderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ public function testTomadorCpfBlockIsIncludedWhenDocumentHas11Digits(): void
183183
self::assertSame('12345678901', $nodes->item(0)->textContent);
184184
}
185185

186+
public function testTomadorAddressPhoneAndEmailAreIncludedWhenProvided(): void
187+
{
188+
$dps = $this->makeDps(
189+
documentoTomador: '12345678000195',
190+
nomeTomador: 'Empresa Tomadora S.A.',
191+
tomadorCodigoMunicipio: '3303302',
192+
tomadorCep: '24020077',
193+
tomadorLogradouro: 'Avenida Rio Branco',
194+
tomadorTelefone: '21988887777',
195+
tomadorEmail: 'financeiro@example.test',
196+
);
197+
198+
$xml = $this->builder->buildDps($dps);
199+
200+
self::assertStringContainsString('<toma>', $xml);
201+
self::assertStringContainsString('<end><endNac><cMun>3303302</cMun><CEP>24020077</CEP></endNac><xLgr>Avenida Rio Branco</xLgr></end>', str_replace(["\n", ' '], '', $xml));
202+
self::assertStringContainsString('<fone>21988887777</fone>', $xml);
203+
self::assertStringContainsString('<email>financeiro@example.test</email>', $xml);
204+
}
205+
186206
public function testTomadorBlockIsAbsentWhenDocumentoTomadorIsEmpty(): void
187207
{
188208
$dps = $this->makeDps(documentoTomador: '');
@@ -350,6 +370,15 @@ private function makeDps(
350370
bool $issRetido = false,
351371
string $documentoTomador = '',
352372
string $nomeTomador = '',
373+
string $tomadorCodigoMunicipio = '',
374+
string $tomadorCep = '',
375+
string $tomadorLogradouro = '',
376+
string $tomadorNumero = '',
377+
string $tomadorComplemento = '',
378+
string $tomadorBairro = '',
379+
string $tomadorInscricaoMunicipal = '',
380+
string $tomadorTelefone = '',
381+
string $tomadorEmail = '',
353382
int $regimeEspecialTributacao = 0,
354383
int $tipoRetencaoIss = 1,
355384
int $opcaoSimplesNacional = 1,
@@ -380,6 +409,15 @@ private function makeDps(
380409
codigoTributacaoNacional: $codigoTributacaoNacional,
381410
documentoTomador: $documentoTomador,
382411
nomeTomador: $nomeTomador,
412+
tomadorCodigoMunicipio: $tomadorCodigoMunicipio,
413+
tomadorCep: $tomadorCep,
414+
tomadorLogradouro: $tomadorLogradouro,
415+
tomadorNumero: $tomadorNumero,
416+
tomadorComplemento: $tomadorComplemento,
417+
tomadorBairro: $tomadorBairro,
418+
tomadorInscricaoMunicipal: $tomadorInscricaoMunicipal,
419+
tomadorTelefone: $tomadorTelefone,
420+
tomadorEmail: $tomadorEmail,
383421
regimeEspecialTributacao: $regimeEspecialTributacao,
384422
tipoRetencaoIss: $tipoRetencaoIss,
385423
issRetido: $issRetido,

0 commit comments

Comments
 (0)