forked from reingart/pyafipws
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_pyfepdf.py
More file actions
368 lines (308 loc) · 9.08 KB
/
test_pyfepdf.py
File metadata and controls
368 lines (308 loc) · 9.08 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
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
#!/usr/bin/python
# -*- coding: utf8 -*-
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, 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 MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
"""Test para FEPDF"""
__author__ = "Mariano Reingart <reingart@gmail.com>"
__copyright__ = "Copyright (C) 2010-2019 Mariano Reingart"
__license__ = "GPL 3.0"
import os
import sys
import datetime
import pytest
from pyafipws.wsaa import WSAA
from pyafipws.pyfepdf import FEPDF
from pyafipws.pyfepdf import main
from builtins import str
from pyafipws.utils import SafeConfigParser
import shutil
CERT = "reingart.crt"
PKEY = "reingart.key"
CONFIG_FILE = "rece.ini"
fepdf = FEPDF()
pytestmark = [pytest.mark.dontusefix]
shutil.copy("tests/facturas.json", "facturas.json")
def test_crear_factura():
"""Test de creación de una factura (Interna)."""
tipo_cbte = 19 if "--expo" in sys.argv else 201
punto_vta = 4000
fecha = datetime.datetime.now().strftime("%Y%m%d")
concepto = 3
tipo_doc = 80
nro_doc = "30000000007"
cbte_nro = 12345678
imp_total = "127.00"
imp_tot_conc = "3.00"
imp_neto = "100.00"
imp_iva = "21.00"
imp_trib = "1.00"
imp_op_ex = "2.00"
imp_subtotal = "105.00"
fecha_cbte = fecha
fecha_venc_pago = fecha
# Fechas del período del servicio facturado (solo si concepto> 1)
fecha_serv_desde = fecha
fecha_serv_hasta = fecha
# campos p/exportación (ej): DOL para USD, indicando cotización:
moneda_id = "DOL" if "--expo" in sys.argv else "PES"
moneda_ctz = 1 if moneda_id == "PES" else 14.90
incoterms = "FOB" # solo exportación
idioma_cbte = 1 # 1: es, 2: en, 3: pt
# datos adicionales del encabezado:
nombre_cliente = "Joao Da Silva"
domicilio_cliente = "Rua 76 km 34.5 Alagoas"
pais_dst_cmp = 212 # 200: Argentina, ver tabla
id_impositivo = "PJ54482221-l" # cat. iva (mercado interno)
forma_pago = "30 dias"
obs_generales = "Observaciones Generales<br/>linea2<br/>linea3"
obs_comerciales = "Observaciones Comerciales<br/>texto libre"
# datos devueltos por el webservice (WSFEv1, WSMTXCA, etc.):
motivo_obs = "Factura individual, DocTipo: 80, DocNro 30000000007 no se encuentra registrado en los padrones de AFIP."
cae = "61123022925855"
fch_venc_cae = "20110320"
fact = fepdf.CrearFactura(
concepto,
tipo_doc,
nro_doc,
tipo_cbte,
punto_vta,
cbte_nro,
imp_total,
imp_tot_conc,
imp_neto,
imp_iva,
imp_trib,
imp_op_ex,
fecha_cbte,
fecha_venc_pago,
fecha_serv_desde,
fecha_serv_hasta,
moneda_id,
moneda_ctz,
cae,
fch_venc_cae,
id_impositivo,
nombre_cliente,
domicilio_cliente,
pais_dst_cmp,
obs_comerciales,
obs_generales,
forma_pago,
incoterms,
idioma_cbte,
motivo_obs,
)
assert fact == True
def test_agregar_detalle_item():
"""Test de agregando un artículo a una factura (interna).."""
tipo_cbte = 19 if "--expo" in sys.argv else 201
test_crear_factura()
# detalle de artículos:
u_mtx = 123456
cod_mtx = 1234567890123
codigo = "P0001"
ds = "Descripcion del producto P0001\n" + "Lorem ipsum sit amet " * 10
qty = 1.00
umed = 7
if tipo_cbte in (1, 2, 3, 4, 5, 34, 39, 51, 52, 53, 54, 60, 64):
# discriminar IVA si es clase A / M
precio = 110.00
imp_iva = 23.10
else:
# no discriminar IVA si es clase B (importe final iva incluido)
precio = 133.10
imp_iva = None
bonif = 0.00
iva_id = 5
importe = 133.10
despacho = u"Nº 123456"
dato_a = "Dato A"
chk1 = fepdf.AgregarDetalleItem(
u_mtx,
cod_mtx,
codigo,
ds,
qty,
umed,
precio,
bonif,
iva_id,
imp_iva,
importe,
despacho,
dato_a,
)
# descuento general (a tasa 21%):
u_mtx = cod_mtx = codigo = None
ds = u"Bonificación/Descuento 10%"
qty = precio = bonif = None
umed = 99
iva_id = 5
if tipo_cbte in (1, 2, 3, 4, 5, 34, 39, 51, 52, 53, 54, 60, 64):
# discriminar IVA si es clase A / M
imp_iva = -2.21
else:
imp_iva = None
importe = -12.10
chk2 = fepdf.AgregarDetalleItem(
u_mtx,
cod_mtx,
codigo,
ds,
qty,
umed,
precio,
bonif,
iva_id,
imp_iva,
importe,
"",
)
# descripción (sin importes ni cantidad):
u_mtx = cod_mtx = codigo = None
qty = precio = bonif = iva_id = imp_iva = importe = None
umed = 0
ds = u"Descripción Ejemplo"
chk3 = fepdf.AgregarDetalleItem(
u_mtx,
cod_mtx,
codigo,
ds,
qty,
umed,
precio,
bonif,
iva_id,
imp_iva,
importe,
"",
)
assert chk1 == True
assert chk2 == True
assert chk3 == True
def test_agregar_iva():
iva_id = 5 # 21%
base_imp = 100
importe = 21
chk = fepdf.AgregarIva(iva_id, base_imp, importe)
assert chk == True
def test_agregar_tributo():
tributo_id = 99
desc = "Impuesto Municipal Matanza"
base_imp = "100.00"
alic = "1.00"
importe = "1.00"
chk = fepdf.AgregarTributo(tributo_id, desc, base_imp, alic, importe)
assert chk == True
def test_agregar_cmp_asoc():
tipo = 5
pto_vta = 2
nro = 1234
chk = fepdf.AgregarCmpAsoc(tipo, pto_vta, nro)
assert chk == True
def test_crear_plantilla():
sys.argv = []
config = SafeConfigParser()
config.read(CONFIG_FILE)
conf_fact = dict(config.items("FACTURA"))
fepdf.CrearPlantilla(
papel=conf_fact.get("papel", "legal"),
orientacion=conf_fact.get("orientacion", "portrait"),
)
def test_procesar_plantilla():
sys.argv = []
sys.argv.append("--debug")
config = SafeConfigParser()
config.read(CONFIG_FILE)
conf_fact = dict(config.items("FACTURA"))
chk = fepdf.ProcesarPlantilla(
num_copias=int(conf_fact.get("copias", 1)),
lineas_max=int(conf_fact.get("lineas_max", 24)),
qty_pos=conf_fact.get("cant_pos") or "izq",
)
assert chk == False
def test_generar_qr():
fepdf.CUIT = "30000000007"
url = fepdf.GenerarQR()
assert url.startswith("https://www.afip.gob.ar/fe/qr/")
def test_main_prueba():
sys.argv = []
sys.argv.append("--prueba")
sys.argv.append("--debug")
main()
def test_main_cargar():
sys.argv = []
sys.argv.append("--cargar")
sys.argv.append("--entrada")
sys.argv.append("tests/facturas.txt")
f = main()
assert f.factura.get("cbte_nro") == 12345678
assert f.factura.get("tipo_cbte") == 201
assert f.factura.get("tipo_doc") == 80
assert f.factura.get("nro_doc") == 30000000007
def test_main_cargar_json():
sys.argv = []
sys.argv.append("--cargar")
sys.argv.append("--json")
sys.argv.append("--entrada")
sys.argv.append("facturas.json")
f = main()
assert f.factura.get("cbte_nro") == '7'
assert f.factura.get("cbt_numero") == '7'
assert f.factura.get("cae") == '61233038185853'
def test_main_grabar():
sys.argv = []
sys.argv.append("--prueba")
sys.argv.append("--fecha_prueba")
sys.argv.append("--grabar")
main()
f1 = open("facturas.txt", "r")
f2 = open("tests/facturas.txt", "r")
d1 = f1.readlines()
d2 = f2.readlines()
if sys.version_info[0] < 3:
d2 = [d.replace("\xc2", "") for d in d2]
d2 = [d.replace("\xc3\xb3", "\xf3") for d in d2]
f1.close()
f2.close()
diff1 = [x for x in d2 if x not in d1]
assert diff1 == []
def test_main_grabar_json():
sys.argv = []
sys.argv.append("--prueba")
sys.argv.append("--grabar")
sys.argv.append("--json")
sys.argv.append("--debug")
main()
f1 = open("facturas.json", "r")
f2 = open("tests/facturas.json", "r")
d1 = f1.readlines()
d2 = f2.readlines()
f1.close()
f2.close()
diff = [x for x in d1 if x not in d2]
assert diff == []
os.remove('facturas.txt')
os.remove('facturas.json')
from test_wslsp import open_file
def new_mostrar_pdf(self, archivo, imprimir=False):
if sys.platform.startswith(("linux2", "java", "linux")):
os.system("evince " "%s" "" % archivo)
def test_mostrar_pdf(mocker):
sys.argv = []
mocker.patch("os.system")
mocker.patch("pyafipws.pyfepdf.FEPDF.MostrarPDF", new=new_mostrar_pdf)
config = SafeConfigParser()
config.read(CONFIG_FILE)
conf_fact = dict(config.items("FACTURA"))
salida = conf_fact.get("salida", "")
fepdf.MostrarPDF(archivo=salida)
if sys.platform.startswith("linux" or "linux2"):
os.system.assert_called_with("evince %s" % salida)