@treeunfe/danfe 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/NFCEGerarDanfe/NFCEGerarDanfe.d.ts +58 -0
  2. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/NFCEGerarDanfe/NFCEGerarDanfe.js +495 -0
  3. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/NFCEGerarDanfe/NFCEGerarDanfe.js.map +1 -0
  4. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/NFEGerarDanfe/NFEGerarDanfe.d.ts +39 -0
  5. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/NFEGerarDanfe/NFEGerarDanfe.js +1097 -0
  6. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/NFEGerarDanfe/NFEGerarDanfe.js.map +1 -0
  7. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/index.d.ts +2 -0
  8. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/index.js +19 -0
  9. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/src/index.js.map +1 -0
  10. package/.rollup.cache/home/runner/work/treeunfe-dfe/treeunfe-dfe/packages/danfe/dist/tsconfig.tsbuildinfo +1 -0
  11. package/.turbo/turbo-build.log +15 -0
  12. package/.turbo/turbo-typecheck.log +4 -0
  13. package/CHANGELOG.md +27 -0
  14. package/LICENSE.txt +221 -0
  15. package/dist/index.cjs +1591 -0
  16. package/dist/index.cjs.map +1 -0
  17. package/dist/index.mjs +1587 -0
  18. package/dist/index.mjs.map +1 -0
  19. package/dist/src/NFCEGerarDanfe/NFCEGerarDanfe.d.ts +58 -0
  20. package/dist/src/NFEGerarDanfe/NFEGerarDanfe.d.ts +39 -0
  21. package/dist/src/index.d.ts +2 -0
  22. package/dist/tsconfig.tsbuildinfo +1 -0
  23. package/package.json +63 -0
  24. package/rollup.config.js +94 -0
  25. package/src/NFCEGerarDanfe/NFCEGerarDanfe.ts +598 -0
  26. package/src/NFEGerarDanfe/NFEGerarDanfe.ts +1223 -0
  27. package/src/bwip-js.d.ts +16 -0
  28. package/src/index.ts +19 -0
  29. package/tsconfig.json +16 -0
@@ -0,0 +1,1097 @@
1
+ /*
2
+ * This file is part of Treeunfe DFe.
3
+ *
4
+ * Treeunfe DFe is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU General Public License as published by
6
+ * the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * Treeunfe DFe is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with Treeunfe DFe. If not, see <https://www.gnu.org/licenses/>.
16
+ */
17
+ import { ValidaCPFCNPJ } from '@treeunfe/shared';
18
+ import bwipjs from 'bwip-js';
19
+ import { format } from 'date-fns';
20
+ import fs from 'fs';
21
+ import PDFDocument from 'pdfkit';
22
+ class NFEGerarDanfe {
23
+ constructor(props) {
24
+ this.barcodeBuffer = null;
25
+ const { data, chave, outputPath } = props;
26
+ this.data = data;
27
+ this.chave = chave.trim();
28
+ this.outputPath = outputPath;
29
+ this.enviada = false;
30
+ this.documento = new ValidaCPFCNPJ();
31
+ this.protNFe = data.protNFe;
32
+ const nfeData = Array.isArray(data.NFe) ? data.NFe[0] : data.NFe;
33
+ const { det, ide, emit, dest, total, transp, infAdic } = nfeData.infNFe;
34
+ this.det = det;
35
+ this.ide = ide;
36
+ this.emit = emit;
37
+ this.total = total;
38
+ this.transp = transp;
39
+ this.infAdic = infAdic;
40
+ if (dest)
41
+ this.dest = dest;
42
+ if (this.protNFe?.infProt.nProt) {
43
+ this.enviada = true;
44
+ }
45
+ // Área útil ignorando margem à direita (22.68) e esquerda (5.67) = 566.93
46
+ this.doc = new PDFDocument({
47
+ margins: { top: 22, right: 22.68, bottom: 12, left: 5.67 },
48
+ size: 'a4', // 595.28 * 841.89
49
+ bufferPages: true,
50
+ layout: 'portrait',
51
+ font: 'Times-Roman',
52
+ });
53
+ }
54
+ async generateBarcode(data) {
55
+ try {
56
+ const pngBuffer = await bwipjs.toBuffer({
57
+ bcid: 'code128',
58
+ text: data,
59
+ scaleX: 4,
60
+ height: 14,
61
+ includetext: false,
62
+ });
63
+ this.barcodeBuffer = pngBuffer; // Armazena o buffer na instância
64
+ return pngBuffer; // Retorna o buffer
65
+ }
66
+ catch (err) {
67
+ console.error('Erro ao gerar código de barras:', err);
68
+ this.barcodeBuffer = null;
69
+ return null;
70
+ }
71
+ }
72
+ setLineStyle(lineWidth, strokeColor) {
73
+ this.doc.lineWidth(lineWidth).strokeColor(strokeColor).fill('black');
74
+ }
75
+ drawHeader(isFirstPage) {
76
+ if (isFirstPage) {
77
+ this._buildGuia();
78
+ this._buildSeparator();
79
+ this._buildHeader(52);
80
+ }
81
+ else {
82
+ this._buildHeader(0);
83
+ }
84
+ // this.doc.moveDown();
85
+ }
86
+ drawFooter() {
87
+ this._buildFooter();
88
+ }
89
+ _buildGuia() {
90
+ const { top, left } = this.doc.page.margins;
91
+ this.setLineStyle(0.75, '#1c1c1c');
92
+ /** TOP LEFT */
93
+ this.doc.rect(left, top, 470, 21).stroke();
94
+ this.doc.fontSize(5).text(`RECEBEMOS DE ${this.emit.xNome} OS PRODUTOS / SERVIÇOS CONSTANTES DA NOTA FISCAL INDICADO AO LADO`, 10, 26, {
95
+ characterSpacing: 0.5
96
+ });
97
+ this.doc.fontSize(6).text(`EMISSÃO: ${format(new Date(this.ide.dhEmi), 'dd-MM-yyyy')} - DEST. / REM.: ${this.dest?.xNome || ''} - VALOR TOTAL: R$ ${parseFloat(String(this.total.ICMSTot.vNF)).toFixed(2)}`, 10, 33.5, {
98
+ characterSpacing: 0.5
99
+ });
100
+ /** RIGHT */
101
+ this.doc.rect(left + 470, top, 96.93, 42).stroke();
102
+ this.doc.fontSize(11).text(`NF-e`, 480, 27, {
103
+ characterSpacing: 1.5,
104
+ align: 'center'
105
+ });
106
+ this.doc.fontSize(8.8).font('Times-Bold').text(`Nº ${String(this.ide.nNF).padStart(2, '0')}`, 480, 40.5, {
107
+ characterSpacing: 1.5,
108
+ align: 'center',
109
+ });
110
+ this.doc.fontSize(8.5).font('Times-Roman').text(`SÉRIE ${this.ide.serie.padStart(3, '0')}`, 480, 53, {
111
+ characterSpacing: 1.5,
112
+ align: 'center',
113
+ });
114
+ /** BOTTON LEFT */
115
+ this.doc.rect(left, top + 21, 75, 21).stroke();
116
+ this.doc.fontSize(5).text(`DATA DE RECEBIMENTO`, 10, 46.5, {
117
+ characterSpacing: 0.5,
118
+ });
119
+ /** BOTTON RIGHT */
120
+ this.doc.rect(left + 75, top + 21, 395, 21).stroke();
121
+ this.doc.fontSize(5).text(`IDENTIFICAÇÃO E ASSINATURA DO RECEBEDOR`, 75 + 10, 46.5, {
122
+ characterSpacing: 0.5
123
+ });
124
+ }
125
+ _buildSeparator() {
126
+ const { left, right } = this.doc.page.margins;
127
+ const pageWidth = 595.28; // Largura da página A4 em pontos
128
+ // Calcule a largura da linha, considerando as margens esquerda e direita
129
+ const lineWidth = pageWidth - left - right;
130
+ // Desenhe a linha
131
+ this.doc.moveTo(left, 69) // Início da linha
132
+ .lineTo(left + lineWidth, 69) // Fim da linha
133
+ .lineWidth(1) // Espessura da linha
134
+ .dash(1.5, { space: 1.5 }) // Estilo pontilhado
135
+ .strokeColor('black') // Cor da linha
136
+ .stroke(); // Desenhar a linha
137
+ this.doc.undash();
138
+ }
139
+ _buildHeader(pos) {
140
+ const { top, left } = this.doc.page.margins;
141
+ const page = this.doc.bufferedPageRange();
142
+ const CNPJCPF = this.emit.CNPJCPF?.toString();
143
+ const CNPJ = this.emit.CNPJ?.toString();
144
+ const CPF = this.emit.CPF?.toString();
145
+ const documento = this.documento.mascaraCnpjCpf(CNPJCPF || CNPJ || CPF || '');
146
+ this.setLineStyle(0.75, '#1c1c1c');
147
+ const topIdentificacao_1 = top + pos;
148
+ const identificationJoined = `\nCEP: ${this.emit.enderEmit.CEP} - ${this.emit.enderEmit.xBairro} - ${this.emit.enderEmit.UF}\nTEL: ${this.emit.enderEmit.fone} - FAX: ${this.emit.enderEmit.fone}`;
149
+ /** IDENTIFICACAO EMITENTE */
150
+ const _buildIdentificacaoEmit = () => {
151
+ this.doc.rect(left, topIdentificacao_1, 197.5, 98).stroke();
152
+ this.doc.fontSize(5).text(`IDENTIFICAÇÃO DO EMITENTE`, 10, topIdentificacao_1 + 3.5, {
153
+ characterSpacing: 0.5,
154
+ });
155
+ this.doc.fontSize(8).font('Times-Bold').text(this.emit.xNome, 35, topIdentificacao_1 + 14, {
156
+ characterSpacing: 1,
157
+ width: 170,
158
+ lineBreak: true,
159
+ lineGap: 4
160
+ });
161
+ this.doc.fontSize(7).font('Times-Roman').text(`${this.emit.enderEmit.xLgr}, ${this.emit.enderEmit.nro} ${this.emit.xFant ? `- ${this.emit.xFant}` : ''}`, 10, topIdentificacao_1 + 44, {
162
+ characterSpacing: 1,
163
+ width: 181,
164
+ lineBreak: true,
165
+ lineGap: 2,
166
+ continued: true
167
+ }).text(identificationJoined);
168
+ };
169
+ /** IDENTIFICACAO NFe */
170
+ const _buildIdentificacaoDanfe = () => {
171
+ this.doc.rect(left + 197.5, topIdentificacao_1, 112.7, 98).stroke();
172
+ this.doc.fontSize(12).font('Times-Bold').text(`DANFE`, 203, topIdentificacao_1 + 8, {
173
+ characterSpacing: 1.5,
174
+ width: 112.7,
175
+ align: 'center'
176
+ });
177
+ this.doc.fontSize(6).text(`DOCUMENTO AUXILIAR DA NOTA FISCAL ELETRÔNICA`, 215, topIdentificacao_1 + 20, {
178
+ characterSpacing: 0.5,
179
+ width: 95,
180
+ align: 'left',
181
+ lineGap: 2,
182
+ });
183
+ this.doc.fontSize(8).font('Times-Roman').text(`0 - ENTRADA`, 217, topIdentificacao_1 + 40, {
184
+ characterSpacing: 0.5,
185
+ align: 'left',
186
+ });
187
+ this.doc.fontSize(8).text(`1 - SAÍDA`, 217, topIdentificacao_1 + 50, {
188
+ characterSpacing: 0.5,
189
+ align: 'left',
190
+ });
191
+ this.doc.rect(left + 275, topIdentificacao_1 + 38, 18, 18).stroke();
192
+ this.doc.fontSize(14).font('Times-Bold').text(String(this.ide.tpNF), left + 280, topIdentificacao_1 + 42.5);
193
+ this.doc.fontSize(8.8).text(`Nº ${String(this.ide.nNF).padStart(2, '0')}`, 208, topIdentificacao_1 + 65, {
194
+ characterSpacing: 1,
195
+ });
196
+ this.doc.fontSize(10).font('Times-Roman').text(`fl. ${page.start + 1}/${page.count}`, 240, topIdentificacao_1 + 65, {
197
+ characterSpacing: 1.5,
198
+ width: 112.7,
199
+ align: 'center'
200
+ });
201
+ this.doc.fontSize(8.8).text(`SÉRIE ${this.ide.serie.padStart(3, '0')}`, 208, topIdentificacao_1 + 78, {
202
+ characterSpacing: 1.5,
203
+ width: 112.7,
204
+ align: 'center'
205
+ });
206
+ };
207
+ /** IDENTIFICACAO NFe (Barcode e Chave) */
208
+ const _buildIdentificacaoNFe = () => {
209
+ this.doc.rect(left + 310.2, topIdentificacao_1, 256.73, 35).stroke();
210
+ if (this.barcodeBuffer) { // Verifica se o buffer do barcode existe
211
+ this.doc.image(this.barcodeBuffer, left + 323.5, topIdentificacao_1 + 3, { width: 230, height: 30 });
212
+ }
213
+ else {
214
+ this.doc.fontSize(8).fillColor('red').text('Erro ao carregar barcode', left + 316, topIdentificacao_1 + 12, {
215
+ width: 256.73, align: 'center'
216
+ });
217
+ }
218
+ if (Number(this.ide.tpAmb) !== 2 && !this.enviada && !this.data.forceTransmitida) {
219
+ this.doc.fontSize(14).font('Times-Bold').fillColor('red').text('NF-E NÃO ENVIADA PARA SEFAZ', left + 316, topIdentificacao_1 + 12, {
220
+ characterSpacing: 1,
221
+ width: 256.73,
222
+ });
223
+ }
224
+ this.setLineStyle(0.75, '#1c1c1c');
225
+ this.doc.rect(left + 310.2, topIdentificacao_1 + 35, 256.73, 23).stroke();
226
+ this.doc.fontSize(5).font('Times-Roman').text(`CHAVE DE ACESSO`, left + 314, topIdentificacao_1 + 38, {
227
+ characterSpacing: 0.5,
228
+ });
229
+ this.doc.fontSize(6).text(this.chave, left + 310.2, topIdentificacao_1 + 47, {
230
+ characterSpacing: 1,
231
+ width: 256.73,
232
+ align: 'center'
233
+ });
234
+ this.doc.rect(left + 310.2, topIdentificacao_1 + 58, 256.73, 40).stroke();
235
+ if (Number(this.ide.tpAmb) === 2 || this.enviada) {
236
+ this.doc.fontSize(6.5).text('Consulta de autenticidade no portal nacional da NF-e', left + 310.2, topIdentificacao_1 + 65, {
237
+ characterSpacing: 0.5,
238
+ width: 256.73,
239
+ align: 'center'
240
+ });
241
+ this.doc.fontSize(6.5).text('www.nfe.fazenda.gov.br/portal', left + 310.2, topIdentificacao_1 + 75, {
242
+ characterSpacing: 0.5,
243
+ width: 256.73,
244
+ align: 'center'
245
+ });
246
+ this.doc.fontSize(6.5).text('ou no site da Sefaz Autorizadora', left + 310.2, topIdentificacao_1 + 85, {
247
+ characterSpacing: 0.5,
248
+ width: 256.73,
249
+ align: 'center'
250
+ });
251
+ }
252
+ this.doc.rect(left, topIdentificacao_1 + 98, 310.2, 23).stroke();
253
+ this.doc.fontSize(5).text('NATUREZA DE OPERAÇÃO', left + 4, topIdentificacao_1 + 102, {
254
+ characterSpacing: 0.5,
255
+ });
256
+ this.doc.fontSize(8).text(this.ide.natOp, left + 4, topIdentificacao_1 + 112, {
257
+ characterSpacing: 1,
258
+ });
259
+ this.doc.rect(left + 310.2, topIdentificacao_1 + 98, 256.73, 23).stroke();
260
+ if (Number(this.ide.tpAmb) === 2 || this.enviada) {
261
+ let dhRecbto = '';
262
+ if (this.protNFe?.infProt.dhRecbto) {
263
+ dhRecbto = format(new Date(this.protNFe?.infProt.dhRecbto), 'dd/MM/yyyy HH:mm:ss');
264
+ }
265
+ const xProtNfe = `${String(this.protNFe?.infProt.nProt || '')} ${dhRecbto}`;
266
+ this.doc.fontSize(5).text('PROTOCOLO DE AUTORIZAÇÃO DE USO', left + 314.2, topIdentificacao_1 + 102, {
267
+ characterSpacing: 0.5,
268
+ });
269
+ this.doc.fontSize(8).text(xProtNfe, left + 314.2, topIdentificacao_1 + 112, {
270
+ characterSpacing: 1,
271
+ });
272
+ }
273
+ };
274
+ /** IDENTIFICACAO PESSOA */
275
+ const _buildIdentificacaoPessoa = () => {
276
+ this.doc.rect(left, topIdentificacao_1 + 121, 189.5, 23).stroke();
277
+ this.doc.fontSize(5).text('INSCRIÇÃO ESTADUAL', left + 4, topIdentificacao_1 + 125, {
278
+ characterSpacing: 0.5,
279
+ });
280
+ this.doc.fontSize(8).text(this.emit.IE, left + 5, topIdentificacao_1 + 135, {
281
+ characterSpacing: 1,
282
+ });
283
+ this.doc.rect(left + 189.5, topIdentificacao_1 + 121, 187.93, 23).stroke();
284
+ this.doc.fontSize(5).text('INSCRIÇÃO ESTADUAL DO SUBST. TRIB.', left + 193.5, topIdentificacao_1 + 125, {
285
+ characterSpacing: 0.5,
286
+ });
287
+ this.doc.fontSize(8).text(String(this.emit.IEST || ''), left + 194.5, topIdentificacao_1 + 135, {
288
+ characterSpacing: 1,
289
+ });
290
+ this.doc.rect(left + 377.43, topIdentificacao_1 + 121, 189.5, 23).stroke();
291
+ this.doc.fontSize(5).text('CNPJ / CPF', left + 381.43, topIdentificacao_1 + 125, {
292
+ characterSpacing: 0.5,
293
+ });
294
+ this.doc.fontSize(8).text(documento, left + 386.43, topIdentificacao_1 + 135, {
295
+ characterSpacing: 1,
296
+ });
297
+ };
298
+ _buildIdentificacaoEmit();
299
+ _buildIdentificacaoDanfe();
300
+ _buildIdentificacaoNFe();
301
+ _buildIdentificacaoPessoa();
302
+ }
303
+ _buildDestinatario() {
304
+ const { top, left } = this.doc.page.margins;
305
+ const docDest = this.documento.mascaraCnpjCpf(this.dest?.CNPJCPF || this.dest?.CNPJ || this.dest?.CPF || '');
306
+ this.setLineStyle(0.75, '#1c1c1c');
307
+ const topDestinatario = top + 90;
308
+ const _buildDestPessoa = () => {
309
+ this.doc.rect(left, topDestinatario + 120, 398, 23).stroke();
310
+ let xNome = 'NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL';
311
+ if (Number(this.ide.tpAmb) !== 2) {
312
+ xNome = String(this.dest?.xNome || '');
313
+ }
314
+ this.doc.fontSize(5).font('Times-Roman').text('NOME / RAZÃO SOCIAL', left + 4, topDestinatario + 125, {
315
+ characterSpacing: 0.5,
316
+ });
317
+ this.doc.fontSize(8).text(xNome, left + 5, topDestinatario + 135, {
318
+ characterSpacing: 1,
319
+ });
320
+ this.doc.rect(left + 398, topDestinatario + 120, 94, 23).stroke();
321
+ this.doc.fontSize(5).text('CNPJ / CPF', left + 402, topDestinatario + 125, {
322
+ characterSpacing: 0.5,
323
+ });
324
+ this.doc.fontSize(8).text(docDest, left + 403, topDestinatario + 135, {
325
+ characterSpacing: 1,
326
+ });
327
+ this.doc.rect(left + 492, topDestinatario + 120, 74.3, 23).stroke();
328
+ this.doc.fontSize(5).text('DATA DA EMISSÃO', left + 496, topDestinatario + 125, {
329
+ characterSpacing: 0.5,
330
+ });
331
+ this.doc.fontSize(8).text(format(new Date(this.ide.dhEmi), 'dd-MM-yyyy'), left + 497, topDestinatario + 135, {
332
+ characterSpacing: 1,
333
+ });
334
+ };
335
+ const _buildDestLogradouro = () => {
336
+ this.doc.rect(left, topDestinatario + 143, 320, 23).stroke();
337
+ this.doc.fontSize(5).text('ENDEREÇO', left + 4, topDestinatario + 148, {
338
+ characterSpacing: 0.5,
339
+ });
340
+ this.doc.fontSize(8).text(`${this.dest?.enderDest?.xLgr || ''}, ${this.dest?.enderDest?.nro || ''}`, left + 5, topDestinatario + 158, {
341
+ characterSpacing: 1,
342
+ });
343
+ this.doc.rect(left + 320, topDestinatario + 143, 118.5, 23).stroke();
344
+ this.doc.fontSize(5).text('BAIRRO / DISTRITO', left + 324, topDestinatario + 148, {
345
+ characterSpacing: 0.5,
346
+ });
347
+ const bairroText = String(this.dest?.enderDest?.xBairro || '');
348
+ const bairroCharLimit = 22;
349
+ const bairroMaxWidthForText = 110; // Largura útil para o texto dentro da célula
350
+ const defaultBairroFontSize = 8;
351
+ const reducedBairroFontSize = 5.5;
352
+ const defaultBairroCharSpacing = 1;
353
+ const reducedBairroCharSpacing = 0.25;
354
+ let fontSizeToUse = defaultBairroFontSize;
355
+ let charSpacingToUse = defaultBairroCharSpacing;
356
+ if (bairroText.length > bairroCharLimit) {
357
+ fontSizeToUse = reducedBairroFontSize;
358
+ charSpacingToUse = reducedBairroCharSpacing;
359
+ }
360
+ // Cálculo de Y para tentar alinhar (pode precisar de ajuste fino)
361
+ const textLineHeight = this.doc.currentLineHeight(); // Altura da linha com fontSizeToUse
362
+ const cellContentYBase = topDestinatario + 158; // Linha de base original
363
+ const cellHeightForText = 23 - 5 - 5; // Altura da célula - padding label - padding inferior
364
+ let yPosForBairro = cellContentYBase;
365
+ // Ajuste simples para tentar centralizar um pouco se a linha for mais baixa que o espaço
366
+ if (textLineHeight < cellHeightForText) {
367
+ yPosForBairro = cellContentYBase - ((cellHeightForText - textLineHeight) / 2) + (textLineHeight * 0.3); // ajuste
368
+ }
369
+ // Ajuste fino para alinhar com os outros campos na mesma linha Y
370
+ yPosForBairro = topDestinatario + 158; // Reset para a mesma linha Y dos outros campos
371
+ this.doc.font('Times-Roman').fontSize(fontSizeToUse);
372
+ this.doc.text(bairroText, left + 320 + 3, yPosForBairro, {
373
+ characterSpacing: charSpacingToUse,
374
+ width: bairroMaxWidthForText,
375
+ lineBreak: false,
376
+ ellipsis: true,
377
+ });
378
+ this.doc.font('Times-Roman').fontSize(8);
379
+ this.doc.rect(left + 438.5, topDestinatario + 143, 53.5, 23).stroke();
380
+ this.doc.fontSize(5).text('CEP', left + 442.5, topDestinatario + 148, {
381
+ characterSpacing: 0.5,
382
+ });
383
+ this.doc.fontSize(8).text(String(this.dest?.enderDest?.CEP || ''), left + 443.5, topDestinatario + 158, {
384
+ characterSpacing: 1,
385
+ });
386
+ this.doc.rect(left + 492, topDestinatario + 143, 74.3, 23).stroke();
387
+ this.doc.fontSize(5).text('DATA SAÍDA / ENTRADA', left + 496, topDestinatario + 148, {
388
+ characterSpacing: 0.5,
389
+ });
390
+ this.doc.fontSize(8).text(format(new Date(), 'dd-MM-yyyy'), left + 497, topDestinatario + 158, {
391
+ characterSpacing: 1,
392
+ });
393
+ };
394
+ const _buildDestEndereco = () => {
395
+ this.doc.rect(left, topDestinatario + 166, 246.5, 23).stroke();
396
+ this.doc.fontSize(5).text('MUNICÍPIO', left + 4, topDestinatario + 171, {
397
+ characterSpacing: 0.5,
398
+ });
399
+ this.doc.fontSize(8).text(String(this.dest?.enderDest?.xMun || ''), left + 5, topDestinatario + 181, {
400
+ characterSpacing: 1,
401
+ });
402
+ this.doc.rect(left + 246.5, topDestinatario + 166, 113, 23).stroke();
403
+ this.doc.fontSize(5).text('FONE / FAX', left + 250.5, topDestinatario + 171, {
404
+ characterSpacing: 0.5,
405
+ });
406
+ this.doc.fontSize(8).text(this.dest?.enderDest?.fone || '', left + 250.5, topDestinatario + 181, {
407
+ characterSpacing: 1,
408
+ });
409
+ this.doc.rect(left + 359.5, topDestinatario + 166, 40, 23).stroke();
410
+ this.doc.fontSize(5).text('UF', left + 363.5, topDestinatario + 171, {
411
+ characterSpacing: 0.5,
412
+ });
413
+ this.doc.fontSize(8).text(String(this.dest?.enderDest?.UF || ''), left + 363.5, topDestinatario + 181, {
414
+ characterSpacing: 1,
415
+ });
416
+ this.doc.rect(left + 399.5, topDestinatario + 166, 92.5, 23).stroke();
417
+ this.doc.fontSize(5).text('INSCRIÇÃO ESTADUAL', left + 403.5, topDestinatario + 171, {
418
+ characterSpacing: 0.5,
419
+ });
420
+ this.doc.fontSize(8).text(String(this.dest?.indIEDest || ''), left + 403.5, topDestinatario + 181, {
421
+ characterSpacing: 1,
422
+ });
423
+ this.doc.rect(left + 492, topDestinatario + 166, 74.3, 23).stroke();
424
+ this.doc.fontSize(5).text('HORA DA SAÍDA', left + 496, topDestinatario + 171, {
425
+ characterSpacing: 0.5,
426
+ });
427
+ this.doc.fontSize(8).text(format(new Date(), 'HH:mm'), left + 497, topDestinatario + 181, {
428
+ characterSpacing: 1,
429
+ });
430
+ };
431
+ this.doc.fontSize(6).font('Times-Bold').text('DESTINATÁRIO / REMETENTE', left, topDestinatario + 114, {
432
+ characterSpacing: 0.5,
433
+ });
434
+ _buildDestPessoa();
435
+ _buildDestLogradouro();
436
+ _buildDestEndereco();
437
+ }
438
+ _builCalculoImposto() {
439
+ const { top, left } = this.doc.page.margins;
440
+ this.setLineStyle(0.75, '#1c1c1c');
441
+ const topDestinatario = top + 173;
442
+ const _buildCalcImposto = () => {
443
+ /** LINHA 1 */
444
+ this.doc.rect(left, topDestinatario + 120, 86, 23).stroke();
445
+ this.doc.fontSize(5).font('Times-Roman').text('BASE DE CÁLCULO DO ICMS', left + 4, topDestinatario + 125, {
446
+ characterSpacing: 0.5,
447
+ });
448
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vBC)).toFixed(2), left - 8, topDestinatario + 135, {
449
+ characterSpacing: 1,
450
+ align: 'right',
451
+ width: 86
452
+ });
453
+ this.doc.rect(left + 86, topDestinatario + 120, 79, 23).stroke();
454
+ this.doc.fontSize(5).text('VALOR DO ICMS', left + 90, topDestinatario + 125, {
455
+ characterSpacing: 0.5,
456
+ });
457
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vICMS)).toFixed(2), left + 86 - 16, topDestinatario + 135, {
458
+ characterSpacing: 1,
459
+ align: 'right',
460
+ width: 86
461
+ });
462
+ this.doc.rect(left + 165, topDestinatario + 120, 79, 23).stroke();
463
+ this.doc.fontSize(5).text('BASE CÁLC. ICMS SUBST', left + 169, topDestinatario + 125, {
464
+ characterSpacing: 0.5,
465
+ });
466
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vBCST)).toFixed(2), left + 165 - 16, topDestinatario + 135, {
467
+ characterSpacing: 1,
468
+ align: 'right',
469
+ width: 86
470
+ });
471
+ this.doc.rect(left + 244, topDestinatario + 120, 79, 23).stroke();
472
+ this.doc.fontSize(5).text('VALOR DO ICMS SUBST.', left + 248, topDestinatario + 125, {
473
+ characterSpacing: 0.5,
474
+ });
475
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vST)).toFixed(2), left + 244 - 16, topDestinatario + 135, {
476
+ characterSpacing: 1,
477
+ align: 'right',
478
+ width: 86
479
+ });
480
+ this.doc.rect(left + 323, topDestinatario + 120, 91, 23).stroke();
481
+ this.doc.fontSize(5).text('VALOR APROX. DOS TRIBUTOS', left + 327, topDestinatario + 125, {
482
+ characterSpacing: 0.5,
483
+ });
484
+ this.doc.fontSize(8).text(parseFloat(this.total.ICMSTot.vTotTrib || '0').toFixed(2), left + 323 - 6, topDestinatario + 135, {
485
+ characterSpacing: 1,
486
+ align: 'right',
487
+ width: 86
488
+ });
489
+ this.doc.rect(left + 414, topDestinatario + 120, 152.93, 23).fillAndStroke('#DDDDDD', '#1c1c1c');
490
+ this.setLineStyle(0.75, '#1c1c1c');
491
+ this.doc.fontSize(5).text('VALOR TOTAL DOS PRODUTOS', left + 418, topDestinatario + 125, {
492
+ characterSpacing: 0.5,
493
+ });
494
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vProd)).toFixed(2), left + 480 - 8, topDestinatario + 135, {
495
+ characterSpacing: 1,
496
+ align: 'right',
497
+ width: 86
498
+ });
499
+ /** LINHA 2 */
500
+ this.doc.rect(left, topDestinatario + 143, 86, 23).stroke();
501
+ this.doc.fontSize(5).text('VALOR DO FRETE', left + 4, topDestinatario + 148, {
502
+ characterSpacing: 0.5,
503
+ });
504
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vFrete)).toFixed(2), left - 8, topDestinatario + 158, {
505
+ characterSpacing: 1,
506
+ align: 'right',
507
+ width: 86
508
+ });
509
+ this.doc.rect(left + 86, topDestinatario + 143, 79, 23).stroke();
510
+ this.doc.fontSize(5).text('VALOR DO SEGURO', left + 90, topDestinatario + 148, {
511
+ characterSpacing: 0.5,
512
+ });
513
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vSeg)).toFixed(2), left + 86 - 16, topDestinatario + 158, {
514
+ characterSpacing: 1,
515
+ align: 'right',
516
+ width: 86
517
+ });
518
+ this.doc.rect(left + 165, topDestinatario + 143, 79, 23).stroke();
519
+ this.doc.fontSize(5).text('DESCONTO', left + 169, topDestinatario + 148, {
520
+ characterSpacing: 0.5,
521
+ });
522
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vDesc)).toFixed(2), left + 165 - 16, topDestinatario + 158, {
523
+ characterSpacing: 1,
524
+ align: 'right',
525
+ width: 86
526
+ });
527
+ this.doc.rect(left + 244, topDestinatario + 143, 79, 23).stroke();
528
+ this.doc.fontSize(5).text('OUTRAS DESP. ACESS.', left + 248, topDestinatario + 148, {
529
+ characterSpacing: 0.5,
530
+ });
531
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vOutro)).toFixed(2), left + 244 - 16, topDestinatario + 158, {
532
+ characterSpacing: 1,
533
+ align: 'right',
534
+ width: 86
535
+ });
536
+ this.doc.rect(left + 323, topDestinatario + 143, 91, 23).stroke();
537
+ this.doc.fontSize(5).text('VALOR DO IPI', left + 327, topDestinatario + 148, {
538
+ characterSpacing: 0.5,
539
+ });
540
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vIPI)).toFixed(2), left + 323 - 6, topDestinatario + 158, {
541
+ characterSpacing: 1,
542
+ align: 'right',
543
+ width: 86
544
+ });
545
+ this.doc.rect(left + 414, topDestinatario + 143, 152.93, 23).fillAndStroke('#DDDDDD', '#1c1c1c');
546
+ this.setLineStyle(0.75, '#1c1c1c');
547
+ this.doc.fontSize(5).text('VALOR TOTAL DA NOTA', left + 418, topDestinatario + 148, {
548
+ characterSpacing: 0.5,
549
+ });
550
+ this.doc.fontSize(8).text(parseFloat(String(this.total.ICMSTot.vNF)).toFixed(2), left + 480 - 8, topDestinatario + 158, {
551
+ characterSpacing: 1,
552
+ align: 'right',
553
+ width: 86
554
+ });
555
+ };
556
+ this.doc.fontSize(6).font('Times-Bold').text('CÁLCULO DO IMPOSTO', left, topDestinatario + 114, {
557
+ characterSpacing: 0.5,
558
+ });
559
+ _buildCalcImposto();
560
+ }
561
+ _builTransporte() {
562
+ const { top, left } = this.doc.page.margins;
563
+ this.setLineStyle(0.75, '#1c1c1c');
564
+ const topDestinatario = top + 233;
565
+ const getModFrete = () => {
566
+ // * 0=Contratação do Frete por conta do Remetente (CIF)
567
+ // * 1=Contratação do Frete por conta do Destinatário (FOB)
568
+ // * 2=Contratação do Frete por conta de Terceiros
569
+ // * 3=Transporte Próprio por conta do Remetente
570
+ // * 4=Transporte Próprio por conta do Destinatário
571
+ // * 9=Sem Ocorrência de Transporte. (Atualizado na NT 2016/002)
572
+ const modFrete = parseInt(String(this.transp.modFrete));
573
+ switch (modFrete) {
574
+ case 0:
575
+ return `${modFrete} - REMETENTE`;
576
+ case 1:
577
+ return `${modFrete} - DESTINATÁRIO`;
578
+ case 2:
579
+ return `${modFrete} - TERCEIROS`;
580
+ case 3:
581
+ return `${modFrete} - REMETENTE`;
582
+ case 4:
583
+ return `${modFrete} - DESTINATÁRIO`;
584
+ case 9:
585
+ return `${modFrete} - SEM FRETE`;
586
+ default:
587
+ return '';
588
+ }
589
+ };
590
+ const _buildVolumeTransporte = () => {
591
+ if (this.transp.vol) {
592
+ /** Posição da primeira linha */
593
+ let topTrnasport = topDestinatario + 166;
594
+ let topTrnasportTitle = topDestinatario + 171;
595
+ let topTrnasportValue = topDestinatario + 181;
596
+ const createVolume = (vol) => {
597
+ this.doc.rect(left, topTrnasport, 68.5, 23).stroke();
598
+ this.doc.fontSize(5).text('QUANTIDADE', left + 4, topTrnasportTitle, {
599
+ characterSpacing: 0.5,
600
+ });
601
+ this.doc.fontSize(8).text(String(vol?.qVol || ''), left - 8, topTrnasportValue, {
602
+ characterSpacing: 1,
603
+ align: 'right',
604
+ width: 68.5
605
+ });
606
+ this.doc.rect(left + 68.5, topTrnasport, 100.5, 23).stroke();
607
+ this.doc.fontSize(5).text('ESPÉCIE', left + 72.5, topTrnasportTitle, {
608
+ characterSpacing: 0.5,
609
+ });
610
+ this.doc.fontSize(8).text(String(vol?.esp || ''), left + 73.5, topTrnasportValue, {
611
+ characterSpacing: 1,
612
+ });
613
+ this.doc.rect(left + 169, topTrnasport, 100.5, 23).stroke();
614
+ this.doc.fontSize(5).text('MARCA', left + 173, topTrnasportTitle, {
615
+ characterSpacing: 0.5,
616
+ });
617
+ this.doc.fontSize(8).text(String(vol?.marca || ''), left + 174, topTrnasportValue, {
618
+ characterSpacing: 1,
619
+ });
620
+ this.doc.rect(left + 269.5, topTrnasport, 100.5, 23).stroke();
621
+ this.doc.fontSize(5).text('NUMERAÇÃO', left + 273.5, topTrnasportTitle, {
622
+ characterSpacing: 0.5,
623
+ });
624
+ this.doc.fontSize(8).text(String(vol?.nVol || ''), left + 274.5, topTrnasportValue, {
625
+ characterSpacing: 1,
626
+ });
627
+ this.doc.rect(left + 370, topTrnasport, 102.6, 23).stroke();
628
+ this.doc.fontSize(5).text('PESO BRUTO', left + 374, topTrnasportTitle, {
629
+ characterSpacing: 0.5,
630
+ });
631
+ this.doc.fontSize(8).text(String(vol?.nVol || ''), left + 375, topTrnasportValue, {
632
+ characterSpacing: 1,
633
+ });
634
+ //96,43
635
+ this.doc.rect(left + 472.5, topTrnasport, 94.43, 23).stroke();
636
+ this.doc.fontSize(5).text('PESO LÍQUIDO', left + 474.5, topTrnasportTitle, {
637
+ characterSpacing: 0.5,
638
+ });
639
+ this.doc.fontSize(8).text(String(vol?.nVol || ''), left + 475.5, topTrnasportValue, {
640
+ characterSpacing: 1,
641
+ });
642
+ /** Define posição da nova linha */
643
+ topTrnasport = topTrnasport + 23;
644
+ topTrnasportTitle = topTrnasportTitle + 23;
645
+ topTrnasportValue = topTrnasportValue + 23;
646
+ };
647
+ if (this.transp.vol instanceof Array) {
648
+ for (let vol of this.transp.vol) {
649
+ createVolume(vol);
650
+ }
651
+ }
652
+ else {
653
+ createVolume(this.transp.vol);
654
+ }
655
+ }
656
+ };
657
+ const _buildCalcImposto = () => {
658
+ const documento = this.documento.mascaraCnpjCpf(this.transp.transporta?.CNPJCPF || this.transp.transporta?.CNPJ || this.transp.transporta?.CPF || '');
659
+ /** LINHA 1 */
660
+ this.doc.rect(left, topDestinatario + 120, 248.5, 23).stroke();
661
+ this.doc.fontSize(5).font('Times-Roman').text('RAZÃO SOCIAL', left + 4, topDestinatario + 125, {
662
+ characterSpacing: 0.5,
663
+ });
664
+ this.doc.fontSize(8).text(this.transp.transporta?.xNome || '', left + 5, topDestinatario + 135, {
665
+ characterSpacing: 1,
666
+ width: 246
667
+ });
668
+ this.doc.rect(left + 248.5, topDestinatario + 120, 90, 23).stroke();
669
+ this.doc.fontSize(5).text('FRETE POR CONTA', left + 252.5, topDestinatario + 125, {
670
+ characterSpacing: 0.5,
671
+ });
672
+ this.doc.fontSize(8).text(getModFrete(), left + 253.5, topDestinatario + 135, {
673
+ characterSpacing: 1,
674
+ });
675
+ this.doc.rect(left + 338.5, topDestinatario + 120, 50, 23).stroke();
676
+ this.doc.fontSize(5).text('CÓDIGO ANTT', left + 342.5, topDestinatario + 125, {
677
+ characterSpacing: 0.5,
678
+ });
679
+ this.doc.fontSize(8).text(this.transp?.veicTransp?.RNTC || '', left + 343.5, topDestinatario + 135, {
680
+ characterSpacing: 1,
681
+ });
682
+ this.doc.rect(left + 388.5, topDestinatario + 120, 62, 23).stroke();
683
+ this.doc.fontSize(5).text('PLACA DO VEÍCULO', left + 391.5, topDestinatario + 125, {
684
+ characterSpacing: 0.5,
685
+ });
686
+ this.doc.fontSize(8).text(this.transp?.veicTransp?.placa || '', left + 393.5, topDestinatario + 135, {
687
+ characterSpacing: 1,
688
+ });
689
+ this.doc.rect(left + 450.5, topDestinatario + 120, 22, 23).stroke();
690
+ this.doc.fontSize(5).text('UF', left + 454.5, topDestinatario + 125, {
691
+ characterSpacing: 0.5,
692
+ });
693
+ this.doc.fontSize(8).text(this.transp?.transporta?.UF || '', left + 455.5, topDestinatario + 135, {
694
+ characterSpacing: 1,
695
+ });
696
+ this.doc.rect(left + 472.5, topDestinatario + 120, 94.43, 23).stroke();
697
+ this.doc.fontSize(5).text('CNPJ / CPF', left + 476.5, topDestinatario + 125, {
698
+ characterSpacing: 0.5,
699
+ });
700
+ this.doc.fontSize(8).text(documento, left + 477.5, topDestinatario + 135, {
701
+ characterSpacing: 1,
702
+ });
703
+ /** LINHA 2 */
704
+ this.doc.rect(left, topDestinatario + 143, 338.5, 23).stroke();
705
+ this.doc.fontSize(5).text('ENDEREÇO', left + 4, topDestinatario + 148, {
706
+ characterSpacing: 0.5,
707
+ });
708
+ this.doc.fontSize(8).text(this.transp.transporta?.xEnder || '', left + 4, topDestinatario + 158, {
709
+ characterSpacing: 1,
710
+ });
711
+ this.doc.rect(left + 338.5, topDestinatario + 143, 112, 23).stroke();
712
+ this.doc.fontSize(5).text('MUNICÍPIO', left + 342.5, topDestinatario + 148, {
713
+ characterSpacing: 0.5,
714
+ });
715
+ this.doc.fontSize(8).text(this.transp.transporta?.xMun || '', left + 343.5, topDestinatario + 158, {
716
+ characterSpacing: 1,
717
+ });
718
+ this.doc.rect(left + 450.5, topDestinatario + 143, 22, 23).stroke();
719
+ this.doc.fontSize(5).text('UF', left + 454.5, topDestinatario + 148, {
720
+ characterSpacing: 0.5,
721
+ });
722
+ this.doc.fontSize(8).text(this.transp?.transporta?.UF || '', left + 455.5, topDestinatario + 158, {
723
+ characterSpacing: 1,
724
+ });
725
+ this.doc.rect(left + 472.5, topDestinatario + 143, 94.43, 23).stroke();
726
+ this.doc.fontSize(5).text('INSCRIÇÃO ESTADUAL', left + 476.5, topDestinatario + 148, {
727
+ characterSpacing: 0.5,
728
+ });
729
+ this.doc.fontSize(8).text(this.transp?.transporta?.IE || '', left + 476.5, topDestinatario + 158, {
730
+ characterSpacing: 1,
731
+ });
732
+ };
733
+ this.doc.fontSize(6).font('Times-Bold').text('TRANSPORTADOR / VOLUMES TRANSPORTADOS', left, topDestinatario + 114, {
734
+ characterSpacing: 0.5,
735
+ });
736
+ _buildCalcImposto();
737
+ _buildVolumeTransporte();
738
+ }
739
+ _buildProdutos() {
740
+ const { left } = this.doc.page.margins;
741
+ this.setLineStyle(0.75, '#1c1c1c');
742
+ this.doc.fontSize(6).font('Times-Bold').fillColor('black').text('DADOS DO PRODUTO / SERVIÇOS', left, 452, {
743
+ characterSpacing: 0.5,
744
+ });
745
+ const tableTop = 458;
746
+ const defaultItemHeight = 15;
747
+ let y = tableTop;
748
+ let currentPage = 0;
749
+ const header = (top) => {
750
+ this.doc.rect(left, top, 40, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
751
+ this.setLineStyle(0.75, '#1c1c1c');
752
+ this.doc.font('Times-Roman').fontSize(5.7).text('CÓDIGO DO PROD. / SERV', left, top + 2.8, {
753
+ width: 40,
754
+ align: 'center',
755
+ });
756
+ this.doc.rect(left + 40, top, 138, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
757
+ this.setLineStyle(0.75, '#1c1c1c');
758
+ this.doc.text('DESCRIÇÃO DO PRODUTO / SERVIÇO', left + 40, top + 5.9, {
759
+ width: 138,
760
+ align: 'center'
761
+ });
762
+ this.doc.rect(left + 178, top, 30, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
763
+ this.setLineStyle(0.75, '#1c1c1c');
764
+ this.doc.text('NCM / SH', left + 178, top + 5.9, {
765
+ width: 30,
766
+ align: 'center'
767
+ });
768
+ this.doc.rect(left + 208, top, 17, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
769
+ this.setLineStyle(0.75, '#1c1c1c');
770
+ this.doc.text('CST', left + 208, top + 5.9, {
771
+ width: 17,
772
+ align: 'center'
773
+ });
774
+ this.doc.rect(left + 225, top, 28, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
775
+ this.setLineStyle(0.75, '#1c1c1c');
776
+ this.doc.text('CFOP', left + 225, top + 5.9, {
777
+ width: 28,
778
+ align: 'center'
779
+ });
780
+ this.doc.rect(left + 253, top, 23, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
781
+ this.setLineStyle(0.75, '#1c1c1c');
782
+ this.doc.text('UNID.', left + 253, top + 5.9, {
783
+ width: 23,
784
+ align: 'center'
785
+ });
786
+ this.doc.rect(left + 276, top, 25, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
787
+ this.setLineStyle(0.75, '#1c1c1c');
788
+ this.doc.text('QUANT.', left + 276, top + 5.9, {
789
+ width: 25,
790
+ align: 'center'
791
+ });
792
+ this.doc.rect(left + 301, top, 37.23, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
793
+ this.setLineStyle(0.75, '#1c1c1c');
794
+ this.doc.text('VALOR UNITÁRIO', left + 301, top + 2.9, {
795
+ width: 37.23,
796
+ align: 'center'
797
+ });
798
+ this.doc.rect(left + 338.23, top, 39.23, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
799
+ this.setLineStyle(0.75, '#1c1c1c');
800
+ this.doc.text('VALOR DESCONTO', left + 338.23, top + 2.9, {
801
+ width: 39.23,
802
+ align: 'center'
803
+ });
804
+ this.doc.rect(left + 377.46, top, 33.23, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
805
+ this.setLineStyle(0.75, '#1c1c1c');
806
+ this.doc.text('VALOR LIQUIDO', left + 377.46, top + 2.9, {
807
+ width: 33.23,
808
+ align: 'center'
809
+ });
810
+ this.doc.rect(left + 410.69, top, 38, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
811
+ this.setLineStyle(0.75, '#1c1c1c');
812
+ this.doc.text('BASE CÁLC. ICMS', left + 413.8, top + 2.9, {
813
+ width: 32,
814
+ align: 'center'
815
+ });
816
+ this.doc.rect(left + 448.69, top, 35, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
817
+ this.setLineStyle(0.75, '#1c1c1c');
818
+ this.doc.text('VALOR I.C.M.S.', left + 448.69, top + 2.9, {
819
+ width: 35,
820
+ align: 'center'
821
+ });
822
+ this.doc.rect(left + 483.69, top, 38, defaultItemHeight).fillAndStroke('#DDDDDD', '#1c1c1c');
823
+ this.setLineStyle(0.75, '#1c1c1c');
824
+ this.doc.text('VALOR I.P.I.', left + 490.69, top + 2.9, {
825
+ width: 25,
826
+ align: 'center'
827
+ });
828
+ this.doc.rect(left + 521.69, top, 45.2, 7.5).fillAndStroke('#DDDDDD', '#1c1c1c');
829
+ this.setLineStyle(0.75, '#1c1c1c');
830
+ this.doc.text('ALÍQUOTAS', left + 521.69, top + 2, {
831
+ width: 45,
832
+ align: 'center'
833
+ });
834
+ this.doc.rect(left + 521.69, top + 7.5, 22.5, 7.5).fillAndStroke('#DDDDDD', '#1c1c1c');
835
+ this.setLineStyle(0.75, '#1c1c1c');
836
+ this.doc.text('ICMS', left + 521.69, top + 9.5, {
837
+ width: 22.5,
838
+ align: 'center'
839
+ });
840
+ this.doc.rect(left + 544.19, top + 7.5, 22.75, 7.5).fillAndStroke('#DDDDDD', '#1c1c1c');
841
+ this.setLineStyle(0.75, '#1c1c1c');
842
+ this.doc.text('IPI', left + 544.19, top + 9.5, {
843
+ width: 22.5,
844
+ align: 'center'
845
+ });
846
+ };
847
+ const row = (top, item) => {
848
+ function getCST(ICMS) {
849
+ const chavesICMS = Object.keys(ICMS);
850
+ const listaIcmsSemCST = [
851
+ 'ICMSSN101',
852
+ 'ICMSSN102',
853
+ 'ICMSSN201',
854
+ 'ICMSSN202',
855
+ 'ICMSSN500',
856
+ 'ICMSSN900'
857
+ ];
858
+ const icmsSemCST = listaIcmsSemCST.includes(chavesICMS[0]);
859
+ let CST = '';
860
+ if (chavesICMS.length > 0) {
861
+ const tipoICMS = chavesICMS[0];
862
+ if (!icmsSemCST) {
863
+ CST = ICMS[tipoICMS].CST;
864
+ }
865
+ }
866
+ return CST;
867
+ }
868
+ function getValoresItem(ICMS) {
869
+ const chavesICMS = Object.keys(ICMS);
870
+ const listaIcmsSemvBC = [
871
+ 'ICMS02',
872
+ 'ICMS15',
873
+ 'ICMS30',
874
+ 'ICMS40',
875
+ 'ICMS53',
876
+ 'ICMS60',
877
+ 'ICMS61',
878
+ 'ICMSST',
879
+ 'ICMSSN101',
880
+ 'ICMSSN102',
881
+ 'ICMSSN201',
882
+ 'ICMSSN202',
883
+ 'ICMSSN500'
884
+ ];
885
+ const icmsSemvBC = listaIcmsSemvBC.includes(chavesICMS[0]);
886
+ let vBC = '0,00';
887
+ let vICMS = '0,00';
888
+ let pICMS = '0,00';
889
+ if (chavesICMS.length > 0) {
890
+ const tipoICMS = chavesICMS[0];
891
+ if (!icmsSemvBC) {
892
+ vBC = ICMS[tipoICMS].vBC;
893
+ vICMS = ICMS[tipoICMS].vICMS;
894
+ pICMS = ICMS[tipoICMS].pICMS;
895
+ return {
896
+ vBC: parseFloat(vBC).toFixed(2),
897
+ vICMS: parseFloat(vBC).toFixed(2),
898
+ pICMS: parseFloat(pICMS).toFixed(2)
899
+ };
900
+ }
901
+ }
902
+ return { vBC, vICMS, pICMS };
903
+ }
904
+ function getValoresIPI(IPI) {
905
+ if (!IPI) {
906
+ return {
907
+ vIPI: '0,00',
908
+ pIPI: '0,00'
909
+ };
910
+ }
911
+ let vIPI = parseFloat(String(IPI.IPITrib.vIPI)).toFixed(2);
912
+ let pIPI = parseFloat(String(IPI.IPITrib.pIPI)).toFixed(2);
913
+ return { vIPI, pIPI };
914
+ }
915
+ const CST = getCST(item.imposto.ICMS);
916
+ const { vIPI, pIPI } = getValoresIPI(item.imposto.IPI);
917
+ const { vBC, vICMS, pICMS } = getValoresItem(item.imposto.ICMS);
918
+ const text = item.infAdProd ? `${item.prod.xProd || ''}\n${item.infAdProd}` : item.prod.xProd || '';
919
+ const textHeight = this.doc.heightOfString(text, {
920
+ width: 138,
921
+ align: 'center'
922
+ });
923
+ const itemHeight = Math.max(defaultItemHeight, textHeight + 10); // 10 is padding
924
+ this.setLineStyle(0.75, '#1c1c1c');
925
+ this.doc.rect(left, top, 40, itemHeight).stroke();
926
+ this.doc.font('Times-Roman').fontSize(6).text(item.prod.cProd || '', left + 2, top + 3, {
927
+ width: 40,
928
+ });
929
+ this.doc.rect(left + 40, top, 138, itemHeight).stroke();
930
+ this.doc.text(text, left + 43, top + 4, {
931
+ width: 137,
932
+ });
933
+ this.doc.rect(left + 178, top, 30, itemHeight).stroke();
934
+ this.doc.text(String(item.prod.NCM) || '', left + 178, top + 5.9, {
935
+ width: 30,
936
+ align: 'center'
937
+ });
938
+ this.doc.rect(left + 208, top, 17, itemHeight).stroke();
939
+ this.doc.text(CST, left + 208, top + 5.9, {
940
+ width: 17,
941
+ align: 'center'
942
+ });
943
+ this.doc.rect(left + 225, top, 28, itemHeight).stroke();
944
+ this.doc.text(String(item.prod.CFOP || ''), left + 225, top + 5.9, {
945
+ width: 28,
946
+ align: 'center'
947
+ });
948
+ this.doc.rect(left + 253, top, 23, itemHeight).stroke();
949
+ this.doc.text(item.prod.uCom || item.prod.uTrib || '', left + 256, top + 5.9, {
950
+ width: 23,
951
+ });
952
+ const quant = parseFloat(String(item.prod.qCom || item.prod.qTrib)).toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 3 });
953
+ this.doc.rect(left + 276, top, 25, itemHeight).stroke();
954
+ this.doc.text(String(quant || ''), left + 274.5, top + 5.9, {
955
+ width: 25,
956
+ align: 'right'
957
+ });
958
+ const valUnit = parseFloat(String(item.prod.vUnCom || item.prod.vUnTrib || '0')).toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
959
+ this.doc.rect(left + 301, top, 37.23, itemHeight).stroke();
960
+ this.doc.text(valUnit, left + 299.5, top + 5.9, {
961
+ width: 37.23,
962
+ align: 'right'
963
+ });
964
+ const valDesc = parseFloat(String(item.prod.vDesc || '0')).toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
965
+ this.doc.rect(left + 338.23, top, 39.23, itemHeight).stroke();
966
+ this.doc.text(valDesc, left + 336.73, top + 5.9, {
967
+ width: 39.23,
968
+ align: 'right'
969
+ });
970
+ const valLiq = parseFloat(String(item.prod.vProd || '0')).toLocaleString('pt-BR', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
971
+ this.doc.rect(left + 377.46, top, 33.23, itemHeight).stroke();
972
+ this.doc.text(valLiq, left + 375.96, top + 5.9, {
973
+ width: 33.23,
974
+ align: 'right'
975
+ });
976
+ this.doc.rect(left + 410.69, top, 38, itemHeight).stroke();
977
+ this.doc.text(vBC, left + 409.19, top + 5.9, {
978
+ width: 38,
979
+ align: 'right'
980
+ });
981
+ this.doc.rect(left + 448.69, top, 35, itemHeight).stroke();
982
+ this.doc.text(vICMS, left + 447.19, top + 5.9, {
983
+ width: 35,
984
+ align: 'right'
985
+ });
986
+ this.doc.rect(left + 483.69, top, 38, itemHeight).stroke();
987
+ this.doc.text(vIPI, left + 482.19, top + 5.9, {
988
+ width: 38,
989
+ align: 'right'
990
+ });
991
+ this.doc.rect(left + 521.69, top, 22.5, itemHeight).stroke();
992
+ this.doc.text(pICMS, left + 520.19, top + 5.9, {
993
+ width: 22.5,
994
+ align: 'right'
995
+ });
996
+ this.doc.rect(left + 544.19, top, 22.75, itemHeight).stroke();
997
+ this.doc.text(pIPI, left + 542.69, top + 5.9, {
998
+ width: 22.5,
999
+ align: 'right'
1000
+ });
1001
+ return itemHeight;
1002
+ };
1003
+ header(458);
1004
+ const createTable = (prod) => {
1005
+ if (y + defaultItemHeight > this.doc.page.height - 160) {
1006
+ this.doc.addPage();
1007
+ currentPage++;
1008
+ if (currentPage > 0) {
1009
+ y = 185;
1010
+ this.doc.fontSize(6).font('Times-Bold').fillColor('black').text('DADOS DO PRODUTO / SERVIÇOS', left, 175, {
1011
+ characterSpacing: 0.5,
1012
+ });
1013
+ header(185);
1014
+ }
1015
+ }
1016
+ const itemHeight = row(y + 15, prod);
1017
+ y += itemHeight;
1018
+ };
1019
+ // Adicionando itens da tabela
1020
+ if (this.det instanceof Array) {
1021
+ for (let i = 0; i < this.det.length; i++) {
1022
+ const prod = this.det[i];
1023
+ createTable(prod);
1024
+ }
1025
+ }
1026
+ else {
1027
+ createTable(this.det);
1028
+ }
1029
+ }
1030
+ _buildFooter() {
1031
+ const { left } = this.doc.page.margins;
1032
+ this.setLineStyle(0.75, '#1c1c1c');
1033
+ const topDestinatario = 820.45 - 88.5;
1034
+ this.doc.fontSize(6).font('Times-Bold').text('DADOS ADICIONAIS', left, topDestinatario - 5, {
1035
+ characterSpacing: 0.5,
1036
+ });
1037
+ this.doc.rect(left, topDestinatario, 408, 95).stroke();
1038
+ this.doc.fontSize(5).font('Times-Roman').text('INFORMAÇÕES COMPLEMENTARES', 10, topDestinatario + 4.5, {
1039
+ characterSpacing: 0.5
1040
+ });
1041
+ this.doc.fontSize(8).text(this.infAdic?.infCpl || '', 13, topDestinatario + 13, {
1042
+ characterSpacing: 1,
1043
+ width: 400,
1044
+ });
1045
+ this.doc.rect(left + 408, topDestinatario, 158.93, 95).stroke();
1046
+ this.doc.fontSize(5).text('RESERVADO AO FISCO', 408 + 10, topDestinatario + 4.5, {
1047
+ characterSpacing: 0.5
1048
+ });
1049
+ this.doc.fontSize(8).text(String(this.infAdic?.infAdFisco || ''), 13, topDestinatario + 13, {
1050
+ characterSpacing: 1,
1051
+ width: 400,
1052
+ });
1053
+ if (this.exibirMarcaDaguaDanfe) {
1054
+ const topPosition = Number(Number(this.ide.tpAmb)) !== 2 ? topDestinatario + 38 : topDestinatario + 58;
1055
+ const leftPosition = Number(Number(this.ide.tpAmb)) !== 2 ? left + 150 : left + 100;
1056
+ this.doc.fontSize(26).font('Times-Bold').fillColor('#c7c7c7').text('Treeunfe DFe', leftPosition, topPosition, {
1057
+ characterSpacing: 0.5,
1058
+ });
1059
+ }
1060
+ if (Number(Number(this.ide.tpAmb)) === 2) {
1061
+ this.doc.fontSize(14).font('Times-Bold').fillColor('grey').text('AMBIENTE DE HOMOLOGAÇÃO - NF-E SEM VALOR FISCAL', left + 100, topDestinatario + 45, {
1062
+ characterSpacing: 1
1063
+ });
1064
+ }
1065
+ // .rotate(45, { origin: [0, 0] })
1066
+ }
1067
+ async generatePDF(exibirMarcaDaguaDanfe) {
1068
+ try {
1069
+ this.exibirMarcaDaguaDanfe = exibirMarcaDaguaDanfe ?? true;
1070
+ const chave = this.chave;
1071
+ await this.generateBarcode(chave);
1072
+ this.doc.pipe(fs.createWriteStream(this.outputPath));
1073
+ this.drawHeader(true);
1074
+ this._buildDestinatario();
1075
+ this._builCalculoImposto();
1076
+ this._builTransporte();
1077
+ this.doc.on('pageAdded', () => {
1078
+ this.drawHeader(false);
1079
+ });
1080
+ this.drawFooter();
1081
+ this.doc.on('pageAdded', () => {
1082
+ this.drawFooter();
1083
+ });
1084
+ this._buildProdutos();
1085
+ this.doc.end();
1086
+ return {
1087
+ message: ` DANFE Gerada em '${this.outputPath}'`,
1088
+ success: true,
1089
+ };
1090
+ }
1091
+ catch (error) {
1092
+ throw new Error(`Erro ao gerar DANFE: ${error.message}`);
1093
+ }
1094
+ }
1095
+ }
1096
+ export default NFEGerarDanfe;
1097
+ //# sourceMappingURL=NFEGerarDanfe.js.map