Skip to content

Commit 4df1856

Browse files
committed
fix(xml): restore DPS structure expected by validators
Signed-off-by: Vitor Mattos <vitor@php.rio>
1 parent 8ae9dc6 commit 4df1856

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/Xml/XmlBuilder.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class XmlBuilder
1717
{
1818
private const XSD_NAMESPACE = 'http://www.sped.fazenda.gov.br/nfse';
1919
private const XSD_SCHEMA = 'http://www.sped.fazenda.gov.br/nfse tiDPS_v1.00.xsd';
20+
private const DPS_VERSION = '1.01';
2021

2122
public function buildDps(DpsData $dps): string
2223
{
@@ -25,22 +26,25 @@ public function buildDps(DpsData $dps): string
2526
$doc->formatOutput = true;
2627

2728
$root = $doc->createElementNS(self::XSD_NAMESPACE, 'DPS');
29+
$root->setAttribute('versao', self::DPS_VERSION);
2830
$root->setAttribute('xsi:schemaLocation', self::XSD_SCHEMA);
2931
$root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
3032
$doc->appendChild($root);
3133

3234
$infDps = $doc->createElement('infDPS');
33-
$infDps->setAttribute('Id', 'DPS' . $dps->cnpjPrestador . date('YmdHis'));
35+
$infDps->setAttribute('Id', $this->buildIdentifier($dps));
3436
$root->appendChild($infDps);
3537

3638
// Municipality
3739
$cMun = $doc->createElement('cMun', $dps->municipioIbge);
3840
$infDps->appendChild($cMun);
41+
$infDps->appendChild($doc->createElement('cLocEmi', $dps->municipioIbge));
3942

4043
// Prestador
4144
$prest = $doc->createElement('prest');
4245
$cnpj = $doc->createElement('CNPJ', $dps->cnpjPrestador);
4346
$prest->appendChild($cnpj);
47+
$prest->appendChild($this->buildRegTrib($doc, $dps));
4448
$infDps->appendChild($prest);
4549

4650
// Service block
@@ -60,25 +64,35 @@ public function buildDps(DpsData $dps): string
6064

6165
// Values
6266
$valores = $doc->createElement('valores');
63-
$valores->appendChild($doc->createElement('vServ', $dps->valorServico));
67+
68+
$vServPrest = $doc->createElement('vServPrest');
69+
$vServPrest->appendChild($doc->createElement('vServ', $dps->valorServico));
70+
$valores->appendChild($vServPrest);
71+
6472
$valores->appendChild($this->buildTotTrib($doc, $dps));
6573
$infDps->appendChild($valores);
6674

67-
// Regime especial de tributação (optional)
68-
if ($dps->regimeEspecialTributacao !== null) {
69-
$infDps->appendChild($doc->createElement('regEspTrib', (string) $dps->regimeEspecialTributacao));
70-
}
71-
7275
return $doc->saveXML() ?: '';
7376
}
7477

78+
private function buildIdentifier(DpsData $dps): string
79+
{
80+
return 'DPS'
81+
. $dps->municipioIbge
82+
. $dps->tipoAmbiente
83+
. $dps->cnpjPrestador
84+
. str_pad($dps->serie, 5, '0', STR_PAD_LEFT)
85+
. str_pad($dps->numeroDps, 15, '0', STR_PAD_LEFT);
86+
}
87+
7588
private function buildTotTrib(\DOMDocument $doc, DpsData $dps): \DOMElement
7689
{
7790
$totTrib = $doc->createElement('totTrib');
7891

7992
// tribMun contains ISS and conditional pAliq
8093
$tribMun = $doc->createElement('tribMun');
8194
$tribMun->appendChild($doc->createElement('tribISSQN', $dps->issRetido ? '2' : '1'));
95+
$tribMun->appendChild($doc->createElement('tpRetISSQN', (string) $dps->tipoRetencaoIss));
8296

8397
// E0617: For não optante (opSimpNac=1), pAliq must NOT be present
8498
if ($dps->opcaoSimplesNacional !== 1) {
@@ -93,6 +107,14 @@ private function buildTotTrib(\DOMDocument $doc, DpsData $dps): \DOMElement
93107
return $totTrib;
94108
}
95109

110+
private function buildRegTrib(\DOMDocument $doc, DpsData $dps): \DOMElement
111+
{
112+
$regTrib = $doc->createElement('regTrib');
113+
$regTrib->appendChild($doc->createElement('regEspTrib', (string) $dps->regimeEspecialTributacao));
114+
115+
return $regTrib;
116+
}
117+
96118
private function buildToma(\DOMDocument $doc, DpsData $dps): \DOMElement
97119
{
98120
$toma = $doc->createElement('toma');

0 commit comments

Comments
 (0)