/
document.py
769 lines (654 loc) · 32.6 KB
/
document.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# -*- coding: utf-8 -*-
###############################################################################
# #
# Copyright (C) 2013 Renato Lima - Akretion #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU Affero General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU Affero General Public License for more details. #
# #
# You should have received a copy of the GNU Affero General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
###############################################################################
from datetime import datetime
from openerp import pooler
from openerp.exceptions import Warning
from openerp.tools.translate import _
from openerp.addons.l10n_br_account.sped.document import FiscalDocument
from openerp.addons.l10n_br_base.tools.misc import punctuation_rm
class NFe200(FiscalDocument):
def __init__(self):
super(NFe200, self).__init__()
self.nfe = None
self.nfref = None
self.det = None
self.dup = None
def _serializer(self, cr, uid, ids, nfe_environment, context=None):
pool = pooler.get_pool(cr.dbname)
nfes = []
if not context:
context = {'lang': 'pt_BR'}
for invoice in pool.get('account.invoice').browse(cr, uid, ids,
context):
company = pool.get('res.partner').browse(
cr, uid, invoice.company_id.partner_id.id, context)
self.nfe = self.get_NFe()
self._nfe_identification(invoice, company, nfe_environment)
self._in_out_adress(invoice)
for inv_related in invoice.fiscal_document_related_ids:
self.nfref = self._get_NFRef()
self._nfe_references(inv_related)
self.nfe.infNFe.ide.NFref.append(self.nfref)
self._emmiter(invoice, company)
self._receiver(invoice, company, nfe_environment)
i = 0
for inv_line in invoice.invoice_line:
i += 1
self.det = self._get_Det()
self._details(invoice, inv_line, i)
for inv_di in inv_line.import_declaration_ids:
self.di = self._get_DI()
self._di(inv_di)
for inv_di_line in inv_di.line_ids:
self.di_line = self._get_Addition()
self._addition(inv_di_line)
self.di.adi.append(self.di_line)
self.det.prod.DI.append(self.di)
self.nfe.infNFe.det.append(self.det)
if invoice.journal_id.revenue_expense:
for move_line in invoice.move_line_receivable_id:
self.dup = self._get_Dup()
self._encashment_data(invoice, move_line)
self.nfe.infNFe.cobr.dup.append(self.dup)
try:
self._carrier_data(invoice)
except AttributeError:
pass
self.vol = self._get_Vol()
self._weight_data(invoice)
self.nfe.infNFe.transp.vol.append(self.vol)
self._additional_information(invoice)
self._total(invoice)
self._export(invoice)
# Gera Chave da NFe
self.nfe.gera_nova_chave()
nfes.append(self.nfe)
return nfes
def _nfe_identification(self, invoice, company, nfe_environment):
# Identificação da NF-e
#
self.nfe.infNFe.ide.cUF.valor = (company.state_id and
company.state_id.ibge_code or '')
self.nfe.infNFe.ide.cNF.valor = ''
self.nfe.infNFe.ide.natOp.valor = invoice.cfop_ids[0].small_name or ''
self.nfe.infNFe.ide.indPag.valor = (invoice.payment_term and
invoice.payment_term.indPag or '0')
self.nfe.infNFe.ide.mod.valor = invoice.fiscal_document_id.code or ''
self.nfe.infNFe.ide.serie.valor = invoice.document_serie_id.code or ''
self.nfe.infNFe.ide.nNF.valor = invoice.internal_number or ''
self.nfe.infNFe.ide.dEmi.valor = invoice.date_invoice or ''
self.nfe.infNFe.ide.dSaiEnt.valor = datetime.strptime(
invoice.date_in_out, '%Y-%m-%d %H:%M:%S').date() or ''
self.nfe.infNFe.ide.cMunFG.valor = ('%s%s') % (
company.state_id.ibge_code, company.l10n_br_city_id.ibge_code)
self.nfe.infNFe.ide.tpImp.valor = 1 # (1 - Retrato; 2 - Paisagem)
self.nfe.infNFe.ide.tpEmis.valor = 1
self.nfe.infNFe.ide.tpAmb.valor = nfe_environment
self.nfe.infNFe.ide.finNFe.valor = invoice.nfe_purpose
self.nfe.infNFe.ide.procEmi.valor = 0
self.nfe.infNFe.ide.verProc.valor = 'OpenERP Brasil v8'
if invoice.cfop_ids[0].type in ("input"):
self.nfe.infNFe.ide.tpNF.valor = 0
else:
self.nfe.infNFe.ide.tpNF.valor = 1
def _in_out_adress(self, invoice):
#
# Endereço de Entrega ou Retirada
#
if invoice.partner_shipping_id:
if invoice.partner_id.id != invoice.partner_shipping_id.id:
if self.nfe.infNFe.ide.tpNF.valor == 0:
self.nfe.infNFe.retirada.CNPJ.valor = punctuation_rm(
invoice.partner_shipping_id.cnpj_cpf)
self.nfe.infNFe.retirada.xLgr.valor = (
invoice.partner_shipping_id.street or '')
self.nfe.infNFe.retirada.nro.valor = (
invoice.partner_shipping_id.number or '')
self.nfe.infNFe.retirada.xCpl.valor = (
invoice.partner_shipping_id.street2 or '')
self.nfe.infNFe.retirada.xBairro.valor = (
invoice.partner_shipping_id.district or 'Sem Bairro')
self.nfe.infNFe.retirada.cMun.valor = '%s%s' % (
invoice.partner_shipping_id.state_id.ibge_code,
invoice.partner_shipping_id.l10n_br_city_id.ibge_code)
self.nfe.infNFe.retirada.xMun.valor = (
invoice.partner_shipping_id.l10n_br_city_id.name or '')
self.nfe.infNFe.retirada.UF.valor = (
invoice.partner_shipping_id.state_id.code or '')
else:
self.nfe.infNFe.entrega.CNPJ.valor = punctuation_rm(
invoice.partner_shipping_id.cnpj_cpf)
self.nfe.infNFe.entrega.xLgr.valor = (
invoice.partner_shipping_id.street or '')
self.nfe.infNFe.entrega.nro.valor = (
invoice.partner_shipping_id.number or '')
self.nfe.infNFe.entrega.xCpl.valor = (
invoice.partner_shipping_id.street2 or '')
self.nfe.infNFe.entrega.xBairro.valor = (
invoice.partner_shipping_id.district or 'Sem Bairro')
self.nfe.infNFe.entrega.cMun.valor = '%s%s' % (
invoice.partner_shipping_id.state_id.ibge_code,
invoice.partner_shipping_id.l10n_br_city_id.ibge_code)
self.nfe.infNFe.entrega.xMun.valor = (
invoice.partner_shipping_id.l10n_br_city_id.name or '')
self.nfe.infNFe.entrega.UF.valor = (
invoice.partner_shipping_id.state_id.code or '')
def _nfe_references(self, inv_related):
#
# Documentos referenciadas
#
if inv_related.document_type == 'nf':
self.nfref.refNF.cUF.valor = (
inv_related.state_id and
inv_related.state_id.ibge_code or '',)
self.nfref.refNF.AAMM.valor = datetime.strptime(
inv_related.date, '%Y-%m-%d').strftime('%y%m') or ''
self.nfref.refNF.CNPJ.valor = punctuation_rm(inv_related.cnpj_cpf)
self.nfref.refNF.mod.valor = (
inv_related.fiscal_document_id and
inv_related.fiscal_document_id.code or '')
self.nfref.refNF.serie.valor = inv_related.serie or ''
self.nfref.refNF.nNF.valor = inv_related.internal_number or ''
elif inv_related.document_type == 'nfrural':
self.nfref.refNFP.cUF.valor = (
inv_related.state_id and
inv_related.state_id.ibge_code or '',)
self.nfref.refNFP.AAMM.valor = datetime.strptime(
inv_related.date, '%Y-%m-%d').strftime('%y%m') or ''
self.nfref.refNFP.IE.valor = punctuation_rm(inv_related.inscr_est)
self.nfref.refNFP.mod.valor = (
inv_related.fiscal_document_id and
inv_related.fiscal_document_id.code or '')
self.nfref.refNFP.serie.valor = inv_related.serie or ''
self.nfref.refNFP.nNF.valor = inv_related.internal_number or ''
if inv_related.cpfcnpj_type == 'cnpj':
self.nfref.refNFP.CNPJ.valor = punctuation_rm(
inv_related.cnpj_cpf)
else:
self.nfref.refNFP.CPF.valor = punctuation_rm(
inv_related.cnpj_cpf)
elif inv_related.document_type == 'nfe':
self.nfref.refNFe.valor = inv_related.access_key or ''
elif inv_related.document_type == 'cte':
self.nfref.refCTe.valor = inv_related.access_key or ''
elif inv_related.document_type == 'cf':
self.nfref.refECF.mod.valor = (
inv_related.fiscal_document_id and
inv_related.fiscal_document_id.code or '')
self.nfref.refECF.nECF.valor = inv_related.internal_number
self.nfref.refECF.nCOO.valor = inv_related.serie
def _emmiter(self, invoice, company):
"""Emitente"""
self.nfe.infNFe.emit.CNPJ.valor = punctuation_rm(
invoice.company_id.partner_id.cnpj_cpf)
self.nfe.infNFe.emit.xNome.valor = (
invoice.company_id.partner_id.legal_name)
self.nfe.infNFe.emit.xFant.valor = invoice.company_id.partner_id.name
self.nfe.infNFe.emit.enderEmit.xLgr.valor = company.street or ''
self.nfe.infNFe.emit.enderEmit.nro.valor = company.number or ''
self.nfe.infNFe.emit.enderEmit.xCpl.valor = company.street2 or ''
self.nfe.infNFe.emit.enderEmit.xBairro.valor = (
company.district or 'Sem Bairro')
self.nfe.infNFe.emit.enderEmit.cMun.valor = '%s%s' % (
company.state_id.ibge_code, company.l10n_br_city_id.ibge_code)
self.nfe.infNFe.emit.enderEmit.xMun.valor = (
company.l10n_br_city_id.name or '')
self.nfe.infNFe.emit.enderEmit.UF.valor = company.state_id.code or ''
self.nfe.infNFe.emit.enderEmit.CEP.valor = punctuation_rm(
company.zip or '')
self.nfe.infNFe.emit.enderEmit.cPais.valor = (
company.country_id.bc_code[1:])
self.nfe.infNFe.emit.enderEmit.xPais.valor = company.country_id.name
self.nfe.infNFe.emit.enderEmit.fone.valor = punctuation_rm(
str(company.phone or '').replace(' ', ''))
self.nfe.infNFe.emit.IE.valor = punctuation_rm(
invoice.company_id.partner_id.inscr_est)
self.nfe.infNFe.emit.IEST.valor = ''
self.nfe.infNFe.emit.IM.valor = punctuation_rm(
invoice.company_id.partner_id.inscr_mun or '')
self.nfe.infNFe.emit.CRT.valor = invoice.company_id.fiscal_type or ''
if invoice.company_id.partner_id.inscr_mun:
self.nfe.infNFe.emit.CNAE.valor = punctuation_rm(
invoice.company_id.cnae_main_id.code or '')
def _receiver(self, invoice, company, nfe_environment):
"""Destinatário"""
partner_bc_code = ''
address_invoice_state_code = ''
address_invoice_city = ''
partner_cep = ''
if invoice.partner_id.country_id.bc_code:
partner_bc_code = invoice.partner_id.country_id.bc_code[1:]
if invoice.partner_id.country_id.id != \
invoice.company_id.country_id.id:
address_invoice_state_code = 'EX'
address_invoice_city = 'Exterior'
address_invoice_city_code = '9999999'
else:
address_invoice_state_code = invoice.partner_id.state_id.code
address_invoice_city = (
invoice.partner_id.l10n_br_city_id.name or '')
address_invoice_city_code = ('%s%s') % (
invoice.partner_id.state_id.ibge_code,
invoice.partner_id.l10n_br_city_id.ibge_code)
partner_cep = punctuation_rm(invoice.partner_id.zip)
# Se o ambiente for de teste deve ser
# escrito na razão do destinatário
if nfe_environment == '2':
self.nfe.infNFe.dest.xNome.valor = (
'NF-E EMITIDA EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL')
else:
self.nfe.infNFe.dest.xNome.valor = (
invoice.partner_id.legal_name or '')
if invoice.partner_id.is_company:
self.nfe.infNFe.dest.CNPJ.valor = punctuation_rm(
invoice.partner_id.cnpj_cpf)
self.nfe.infNFe.dest.IE.valor = punctuation_rm(
invoice.partner_id.inscr_est)
else:
self.nfe.infNFe.dest.CPF.valor = punctuation_rm(
invoice.partner_id.cnpj_cpf)
self.nfe.infNFe.dest.enderDest.xLgr.valor = (
invoice.partner_id.street or '')
self.nfe.infNFe.dest.enderDest.nro.valor = (
invoice.partner_id.number or '')
self.nfe.infNFe.dest.enderDest.xCpl.valor = (
invoice.partner_id.street2 or '')
self.nfe.infNFe.dest.enderDest.xBairro.valor = (
invoice.partner_id.district or 'Sem Bairro')
self.nfe.infNFe.dest.enderDest.cMun.valor = address_invoice_city_code
self.nfe.infNFe.dest.enderDest.xMun.valor = address_invoice_city
self.nfe.infNFe.dest.enderDest.UF.valor = address_invoice_state_code
self.nfe.infNFe.dest.enderDest.CEP.valor = partner_cep
self.nfe.infNFe.dest.enderDest.cPais.valor = partner_bc_code
self.nfe.infNFe.dest.enderDest.xPais.valor = (
invoice.partner_id.country_id.name or '')
self.nfe.infNFe.dest.enderDest.fone.valor = punctuation_rm(
invoice.partner_id.phone or '').replace(' ', '')
self.nfe.infNFe.dest.email.valor = invoice.partner_id.email or ''
def _details(self, invoice, invoice_line, index):
"""Detalhe"""
self.det.nItem.valor = index
self.det.prod.cProd.valor = invoice_line.product_id.code or ''
self.det.prod.cEAN.valor = invoice_line.product_id.ean13 or ''
self.det.prod.xProd.valor = invoice_line.product_id.name or ''
self.det.prod.NCM.valor = punctuation_rm(
invoice_line.fiscal_classification_id.name or '')
self.det.prod.EXTIPI.valor = ''
self.det.prod.nFCI.valor = invoice_line.fci or ''
self.det.prod.CFOP.valor = invoice_line.cfop_id.code
self.det.prod.uCom.valor = invoice_line.uos_id.name or ''
self.det.prod.qCom.valor = str("%.4f" % invoice_line.quantity)
self.det.prod.vUnCom.valor = str("%.7f" % invoice_line.price_unit)
self.det.prod.vProd.valor = str("%.2f" % invoice_line.price_gross)
self.det.prod.cEANTrib.valor = invoice_line.product_id.ean13 or ''
self.det.prod.uTrib.valor = self.det.prod.uCom.valor
self.det.prod.qTrib.valor = self.det.prod.qCom.valor
self.det.prod.vUnTrib.valor = self.det.prod.vUnCom.valor
self.det.prod.vFrete.valor = str("%.2f" % invoice_line.freight_value)
self.det.prod.vSeg.valor = str("%.2f" % invoice_line.insurance_value)
self.det.prod.vDesc.valor = str("%.2f" % invoice_line.discount_value)
self.det.prod.vOutro.valor = str(
"%.2f" % invoice_line.other_costs_value)
self.det.infAdProd.valor = invoice_line.fiscal_comment or ''
#
# Produto entra no total da NF-e
#
self.det.prod.indTot.valor = 1
if invoice_line.product_type == 'product':
# ICMS
if invoice_line.icms_cst_id.code > 100:
self.det.imposto.ICMS.CSOSN.valor = (
invoice_line.icms_cst_id.code)
self.det.imposto.ICMS.pCredSN.valor = str(
"%.2f" % invoice_line.icms_percent)
self.det.imposto.ICMS.vCredICMSSN.valor = str(
"%.2f" % invoice_line.icms_value)
self.det.imposto.ICMS.orig.valor = invoice_line.icms_origin or ''
self.det.imposto.ICMS.CST.valor = invoice_line.icms_cst_id.code
self.det.imposto.ICMS.modBC.valor = invoice_line.icms_base_type
self.det.imposto.ICMS.vBC.valor = str(
"%.2f" % invoice_line.icms_base)
self.det.imposto.ICMS.pRedBC.valor = str(
"%.2f" % invoice_line.icms_percent_reduction)
self.det.imposto.ICMS.pICMS.valor = str(
"%.2f" % invoice_line.icms_percent)
self.det.imposto.ICMS.vICMS.valor = str(
"%.2f" % invoice_line.icms_value)
# ICMS ST
self.det.imposto.ICMS.modBCST.valor = (
invoice_line.icms_st_base_type)
self.det.imposto.ICMS.pMVAST.valor = str(
"%.2f" % invoice_line.icms_st_mva)
self.det.imposto.ICMS.pRedBCST.valor = str(
"%.2f" % invoice_line.icms_st_percent_reduction)
self.det.imposto.ICMS.vBCST.valor = str(
"%.2f" % invoice_line.icms_st_base)
self.det.imposto.ICMS.pICMSST.valor = str(
"%.2f" % invoice_line.icms_st_percent)
self.det.imposto.ICMS.vICMSST.valor = str(
"%.2f" % invoice_line.icms_st_value)
# IPI
self.det.imposto.IPI.CST.valor = invoice_line.ipi_cst_id.code
if invoice_line.ipi_type == 'percent' or '':
self.det.imposto.IPI.vBC.valor = str(
"%.2f" % invoice_line.ipi_base)
self.det.imposto.IPI.pIPI.valor = str(
"%.2f" % invoice_line.ipi_percent)
if invoice_line.ipi_type == 'quantity':
pesol = 0
if invoice_line.product_id:
pesol = invoice_line.product_id.weight_net
self.det.imposto.IPI.qUnid.valor = str(
"%.2f" % invoice_line.quantity * pesol)
self.det.imposto.IPI.vUnid.valor = str(
"%.2f" % invoice_line.ipi_percent)
self.det.imposto.IPI.vIPI.valor = str(
"%.2f" % invoice_line.ipi_value)
else:
# ISSQN
self.det.imposto.ISSQN.vBC.valor = str(
"%.2f" % invoice_line.issqn_base)
self.det.imposto.ISSQN.vAliq.valor = str(
"%.2f" % invoice_line.issqn_percent)
self.det.imposto.ISSQN.vISSQN.valor = str(
"%.2f" % invoice_line.issqn_value)
self.det.imposto.ISSQN.cMunFG.valor = ('%s%s') % (
invoice.partner_id.state_id.ibge_code,
invoice.partner_id.l10n_br_city_id.ibge_code)
self.det.imposto.ISSQN.cListServ.valor = punctuation_rm(
invoice_line.service_type_id.code or '')
self.det.imposto.ISSQN.cSitTrib.valor = invoice_line.issqn_type
# PIS
self.det.imposto.PIS.CST.valor = invoice_line.pis_cst_id.code
self.det.imposto.PIS.vBC.valor = str("%.2f" % invoice_line.pis_base)
self.det.imposto.PIS.pPIS.valor = str(
"%.2f" % invoice_line.pis_percent)
self.det.imposto.PIS.vPIS.valor = str("%.2f" % invoice_line.pis_value)
# PISST
self.det.imposto.PISST.vBC.valor = str(
"%.2f" % invoice_line.pis_st_base)
self.det.imposto.PISST.pPIS.valor = str(
"%.2f" % invoice_line.pis_st_percent)
self.det.imposto.PISST.qBCProd.valor = ''
self.det.imposto.PISST.vAliqProd.valor = ''
self.det.imposto.PISST.vPIS.valor = str(
"%.2f" % invoice_line.pis_st_value)
# COFINS
self.det.imposto.COFINS.CST.valor = invoice_line.cofins_cst_id.code
self.det.imposto.COFINS.vBC.valor = str(
"%.2f" % invoice_line.cofins_base)
self.det.imposto.COFINS.pCOFINS.valor = str(
"%.2f" % invoice_line.cofins_percent)
self.det.imposto.COFINS.vCOFINS.valor = str(
"%.2f" % invoice_line.cofins_value)
# COFINSST
self.det.imposto.COFINSST.vBC.valor = str(
"%.2f" % invoice_line.cofins_st_base)
self.det.imposto.COFINSST.pCOFINS.valor = str(
"%.2f" % invoice_line.cofins_st_percent)
self.det.imposto.COFINSST.qBCProd.valor = ''
self.det.imposto.COFINSST.vAliqProd.valor = ''
self.det.imposto.COFINSST.vCOFINS.valor = str(
"%.2f" % invoice_line.cofins_st_value)
# II
self.det.imposto.II.vBC.valor = str("%.2f" % invoice_line.ii_base)
self.det.imposto.II.vDespAdu.valor = str(
"%.2f" % invoice_line.ii_customhouse_charges)
self.det.imposto.II.vII.valor = str("%.2f" % invoice_line.ii_value)
self.det.imposto.II.vIOF.valor = str("%.2f" % invoice_line.ii_iof)
self.det.imposto.vTotTrib.valor = str(
"%.2f" % invoice_line.total_taxes)
def _di(self, invoice_line_di):
self.di.nDI.valor = invoice_line_di.name
self.di.dDI.valor = invoice_line_di.date_registration or ''
self.di.xLocDesemb.valor = invoice_line_di.location
self.di.UFDesemb.valor = invoice_line_di.state_id.code or ''
self.di.dDesemb.valor = invoice_line_di.date_release or ''
self.di.cExportador.valor = invoice_line_di.exporting_code
def _addition(self, invoice_line_di):
self.di_line.nAdicao.valor = invoice_line_di.name
self.di_line.nSeqAdic.valor = invoice_line_di.sequence
self.di_line.cFabricante.valor = invoice_line_di.manufacturer_code
self.di_line.vDescDI.valor = str(
"%.2f" % invoice_line_di.amount_discount)
def _encashment_data(self, invoice, move_line):
"""Dados de Cobrança"""
if invoice.type in ('out_invoice', 'in_refund'):
value = move_line.debit
else:
value = move_line.credit
self.dup.nDup.valor = move_line.name
self.dup.dVenc.valor = (move_line.date_maturity or
invoice.date_due or
invoice.date_invoice)
self.dup.vDup.valor = str("%.2f" % value)
def _carrier_data(self, invoice):
"""Dados da Transportadora e veiculo"""
self.nfe.infNFe.transp.modFrete.valor = (
invoice.incoterm and
invoice.incoterm.freight_responsibility or '9')
if invoice.carrier_id:
if invoice.carrier_id.partner_id.is_company:
self.nfe.infNFe.transp.transporta.CNPJ.valor = \
punctuation_rm(
invoice.carrier_id.partner_id.cnpj_cpf or '')
else:
self.nfe.infNFe.transp.transporta.CPF.valor = \
punctuation_rm(
invoice.carrier_id.partner_id.cnpj_cpf or '')
self.nfe.infNFe.transp.transporta.xNome.valor = (
invoice.carrier_id.partner_id.legal_name or '')
self.nfe.infNFe.transp.transporta.IE.valor = punctuation_rm(
invoice.carrier_id.partner_id.inscr_est)
self.nfe.infNFe.transp.transporta.xEnder.valor = (
invoice.carrier_id.partner_id.street or '')
self.nfe.infNFe.transp.transporta.xMun.valor = (
invoice.carrier_id.partner_id.l10n_br_city_id.name or '')
self.nfe.infNFe.transp.transporta.UF.valor = (
invoice.carrier_id.partner_id.state_id.code or '')
if invoice.vehicle_id:
self.nfe.infNFe.transp.veicTransp.placa.valor = (
invoice.vehicle_id.plate or '')
self.nfe.infNFe.transp.veicTransp.UF.valor = (
invoice.vehicle_id.plate.state_id.code or '')
self.nfe.infNFe.transp.veicTransp.RNTC.valor = (
invoice.vehicle_id.rntc_code or '')
def _weight_data(self, invoice):
"""Campos do Transporte da NF-e Bloco 381"""
self.vol.qVol.valor = invoice.number_of_packages
self.vol.esp.valor = invoice.kind_of_packages or ''
self.vol.marca.valor = invoice.brand_of_packages or ''
self.vol.nVol.valor = invoice.notation_of_packages or ''
self.vol.pesoL.valor = str("%.2f" % invoice.weight_net)
self.vol.pesoB.valor = str("%.2f" % invoice.weight)
def _additional_information(self, invoice):
"""Informações adicionais"""
self.nfe.infNFe.infAdic.infAdFisco.valor = invoice.fiscal_comment or ''
self.nfe.infNFe.infAdic.infCpl.valor = invoice.comment or ''
def _total(self, invoice):
"""Totais"""
self.nfe.infNFe.total.ICMSTot.vBC.valor = str(
"%.2f" % invoice.icms_base)
self.nfe.infNFe.total.ICMSTot.vICMS.valor = str(
"%.2f" % invoice.icms_value)
self.nfe.infNFe.total.ICMSTot.vBCST.valor = str(
"%.2f" % invoice.icms_st_base)
self.nfe.infNFe.total.ICMSTot.vST.valor = str(
"%.2f" % invoice.icms_st_value)
self.nfe.infNFe.total.ICMSTot.vProd.valor = str(
"%.2f" % invoice.amount_gross)
self.nfe.infNFe.total.ICMSTot.vFrete.valor = str(
"%.2f" % invoice.amount_freight)
self.nfe.infNFe.total.ICMSTot.vSeg.valor = str(
"%.2f" % invoice.amount_insurance)
self.nfe.infNFe.total.ICMSTot.vDesc.valor = str(
"%.2f" % invoice.amount_discount)
self.nfe.infNFe.total.ICMSTot.vII.valor = str(
"%.2f" % invoice.ii_value)
self.nfe.infNFe.total.ICMSTot.vIPI.valor = str(
"%.2f" % invoice.ipi_value)
self.nfe.infNFe.total.ICMSTot.vPIS.valor = str(
"%.2f" % invoice.pis_value)
self.nfe.infNFe.total.ICMSTot.vCOFINS.valor = str(
"%.2f" % invoice.cofins_value)
self.nfe.infNFe.total.ICMSTot.vOutro.valor = str(
"%.2f" % invoice.amount_costs)
self.nfe.infNFe.total.ICMSTot.vNF.valor = str(
"%.2f" % invoice.amount_total)
self.nfe.infNFe.total.ICMSTot.vTotTrib.valor = str(
"%.2f" % invoice.amount_total_taxes)
def _export(self, invoice):
"Informações de exportação"
self.nfe.infNFe.exporta.UFEmbarq.valor = (
invoice.shipping_state_id.code or '')
self.nfe.infNFe.exporta.xLocEmbarq.valor = (
invoice.shipping_location or '')
def get_NFe(self):
try:
from pysped.nfe.leiaute import NFe_200
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return NFe_200()
def _get_NFRef(self):
try:
from pysped.nfe.leiaute import NFRef_200
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return NFRef_200()
def _get_Det(self):
try:
from pysped.nfe.leiaute import Det_200
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return Det_200()
def _get_DI(self):
try:
from pysped.nfe.leiaute import DI_200
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return DI_200()
def _get_Addition(self):
try:
from pysped.nfe.leiaute import Adi_200
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return Adi_200()
def _get_Vol(self):
try:
from pysped.nfe.leiaute import Vol_200
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return Vol_200()
def _get_Dup(self):
try:
from pysped.nfe.leiaute import Dup_200
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return Dup_200()
def get_xml(self, cr, uid, ids, nfe_environment, context=None):
""""""
result = []
for nfe in self._serializer(cr, uid, ids, nfe_environment, context):
result.append({'key': nfe.infNFe.Id.valor, 'nfe': nfe.get_xml()})
return result
def set_xml(self, nfe_string, context=None):
""""""
nfe = self.get_NFe()
nfe.set_xml(nfe_string)
return nfe
class NFe310(NFe200):
def __init__(self):
super(NFe310, self).__init__()
def _nfe_identification(self, invoice, company, nfe_environment):
super(NFe310, self)._nfe_identification(
invoice, company, nfe_environment)
self.nfe.infNFe.ide.idDest.valor = (
invoice.fiscal_position.cfop_id.id_dest or '')
self.nfe.infNFe.ide.indFinal.valor = invoice.ind_final or ''
self.nfe.infNFe.ide.indPres.valor = invoice.ind_pres or ''
self.nfe.infNFe.ide.dhEmi.valor = datetime.strptime(
invoice.date_hour_invoice, '%Y-%m-%d %H:%M:%S')
self.nfe.infNFe.ide.dhSaiEnt.valor = datetime.strptime(
invoice.date_in_out, '%Y-%m-%d %H:%M:%S')
def _receiver(self, invoice, company, nfe_environment):
super(NFe310, self)._receiver(
invoice, company, nfe_environment)
if invoice.partner_id.country_id.id != \
invoice.company_id.country_id.id:
self.nfe.infNFe.dest.idEstrangeiro.valor = punctuation_rm(
invoice.partner_id.cnpj_cpf)
self.nfe.infNFe.dest.CNPJ.valor = None
self.nfe.infNFe.dest.CPF.valor = None
def _di(self, invoice_line_di):
super(NFe310, self)._di(invoice_line_di)
self.di.tpViaTransp.valor = invoice_line_di.type_transportation or ''
self.di.vAFRMM.valor = str("%.2f" % invoice_line_di.afrmm_value)
self.di.tpIntermedio.valor = invoice_line_di.type_import or ''
self.di.CNPJ.valor = invoice_line_di.exporting_code or ''
self.di.UFTerceiro.valor = (
invoice_line_di.thirdparty_state_id.code or '')
def _export(self, invoice):
"Informações de exportação"
self.nfe.infNFe.exporta.UFSaidaPais.valor = (
invoice.shipping_state_id.code or '')
self.nfe.infNFe.exporta.xLocExporta.valor = (
invoice.shipping_location or '')
self.nfe.infNFe.exporta.xLocDespacho.valor = '' # TODO
def get_NFe(self):
try:
from pysped.nfe.leiaute import NFe_310
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return NFe_310()
def _get_NFRef(self):
try:
from pysped.nfe.leiaute import NFRef_310
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return NFRef_310()
def _get_Det(self):
try:
from pysped.nfe.leiaute import Det_310
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return Det_310()
def _get_Dup(self):
try:
from pysped.nfe.leiaute import Dup_310
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return Dup_310()
def _get_DI(self):
try:
from pysped.nfe.leiaute import DI_310
except ImportError:
raise Warning(
_(u'Erro!'), _(u"Biblioteca PySPED não instalada!"))
return DI_310()