-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlBuilder.php
More file actions
136 lines (104 loc) · 4.74 KB
/
XmlBuilder.php
File metadata and controls
136 lines (104 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
// SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace LibreCodeCoop\NfsePHP\Xml;
use LibreCodeCoop\NfsePHP\Dto\DpsData;
/**
* Builds the DPS (Documento Padrão de Serviço) XML payload.
* Spec: ABRASF 2.04 / SEFIN Nacional 1.0.
*/
class XmlBuilder
{
private const XSD_NAMESPACE = 'http://www.sped.fazenda.gov.br/nfse';
private const XSD_SCHEMA = 'http://www.sped.fazenda.gov.br/nfse tiDPS_v1.00.xsd';
private const DPS_VERSION = '1.01';
public function buildDps(DpsData $dps): string
{
$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$root = $doc->createElementNS(self::XSD_NAMESPACE, 'DPS');
$root->setAttribute('versao', self::DPS_VERSION);
$root->setAttribute('xsi:schemaLocation', self::XSD_SCHEMA);
$root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$doc->appendChild($root);
$infDps = $doc->createElement('infDPS');
$infDps->setAttribute('Id', $this->buildIdentifier($dps));
$root->appendChild($infDps);
// Municipality
$cMun = $doc->createElement('cMun', $dps->municipioIbge);
$infDps->appendChild($cMun);
$infDps->appendChild($doc->createElement('cLocEmi', $dps->municipioIbge));
// Prestador
$prest = $doc->createElement('prest');
$cnpj = $doc->createElement('CNPJ', $dps->cnpjPrestador);
$prest->appendChild($cnpj);
$prest->appendChild($this->buildRegTrib($doc, $dps));
$infDps->appendChild($prest);
// Service block
$serv = $doc->createElement('serv');
$itemListaServico = $doc->createElement('cServ');
$itemListaServico->appendChild($doc->createElement('cTribNac', $dps->codigoTributacaoNacional));
$serv->appendChild($itemListaServico);
$serv->appendChild($doc->createElement('xDescServ', htmlspecialchars($dps->discriminacao, ENT_XML1)));
$infDps->appendChild($serv);
// Tomador (optional — absent for foreign buyers with no document)
if ($dps->documentoTomador !== '') {
$infDps->appendChild($this->buildToma($doc, $dps));
}
// Values
$valores = $doc->createElement('valores');
$vServPrest = $doc->createElement('vServPrest');
$vServPrest->appendChild($doc->createElement('vServ', $dps->valorServico));
$valores->appendChild($vServPrest);
$valores->appendChild($this->buildTotTrib($doc, $dps));
$infDps->appendChild($valores);
return $doc->saveXML() ?: '';
}
private function buildIdentifier(DpsData $dps): string
{
return 'DPS'
. $dps->municipioIbge
. $dps->tipoAmbiente
. $dps->cnpjPrestador
. str_pad($dps->serie, 5, '0', STR_PAD_LEFT)
. str_pad($dps->numeroDps, 15, '0', STR_PAD_LEFT);
}
private function buildTotTrib(\DOMDocument $doc, DpsData $dps): \DOMElement
{
$totTrib = $doc->createElement('totTrib');
// tribMun contains ISS and conditional pAliq
$tribMun = $doc->createElement('tribMun');
$tribMun->appendChild($doc->createElement('tribISSQN', $dps->issRetido ? '2' : '1'));
$tribMun->appendChild($doc->createElement('tpRetISSQN', (string) $dps->tipoRetencaoIss));
// E0617: For não optante (opSimpNac=1), pAliq must NOT be present
if ($dps->opcaoSimplesNacional !== 1) {
$tribMun->appendChild($doc->createElement('pAliq', $dps->aliquota));
}
$totTrib->appendChild($tribMun);
// E0715: indTotTrib is ALWAYS included to avoid schema validation errors
$totTrib->appendChild($doc->createElement('indTotTrib', (string) $dps->indicadorTributacao));
return $totTrib;
}
private function buildRegTrib(\DOMDocument $doc, DpsData $dps): \DOMElement
{
$regTrib = $doc->createElement('regTrib');
$regTrib->appendChild($doc->createElement('regEspTrib', (string) $dps->regimeEspecialTributacao));
return $regTrib;
}
private function buildToma(\DOMDocument $doc, DpsData $dps): \DOMElement
{
$toma = $doc->createElement('toma');
$docLen = strlen($dps->documentoTomador);
if ($docLen === 14) {
$toma->appendChild($doc->createElement('CNPJ', $dps->documentoTomador));
} elseif ($docLen === 11) {
$toma->appendChild($doc->createElement('CPF', $dps->documentoTomador));
}
if ($dps->nomeTomador !== '') {
$toma->appendChild($doc->createElement('xNome', htmlspecialchars($dps->nomeTomador, ENT_XML1)));
}
return $toma;
}
}