zaz-compaqi/source/pythonpath/zcompaqi.py

216 lines
5.3 KiB
Python

#!/usr/bin/env python3
import easymacro as app
IS_HEADER = 'P'
IS_CARGO = '0'
CLASE = '1'
ORIGEN = '11'
PRINT = '0'
AJUSTE = '0'
FILE_TARGET = 'polizas.txt'
def main(args):
if args == 'import_polizas':
_import_polizas()
return
if args == 'export_polizas':
_export_polizas()
return
def _get_header(row):
header = ('P |{poliza_date:%Y%m%d}|{poliza_type:>4}|{poliza_num:>9}|{clase}|'
'{poliza_id:<10}|{poliza_description:<100}|{origen}|{impresa}|{ajuste} '
)
poliza_num = row[12]
poliza_id = row[10].strip() or '0'
poliza_date = app.dates.calc_to_date(row[11])
poliza_type = row[13]
poliza_description = row[14]
data = dict(
poliza_date = poliza_date,
poliza_type = poliza_type,
poliza_num = poliza_num,
clase = CLASE,
poliza_id = poliza_id,
poliza_description = poliza_description,
origen = ORIGEN,
impresa = PRINT,
ajuste = AJUSTE,
)
line = header.format(**data).replace('|', ' ')
return line
def _get_mov(row):
template = ('M |{num_cuenta:<30}|{reference:<10}|{type_mov}|{importe:<20}|'
'{diario:<10}|{importe_e:<20}|{description:<100}| '
)
num_cuenta = row[0]
reference = row[6][:10]
type_mov = '0'
importe = row[2]
importe_e = row[4]
if row[3]:
type_mov = '1'
importe = row[3]
importe_e = row[5]
if not importe_e:
importe_e = 0.0
diario = row[8].strip() or '0'
description = row[7]
uuid = row[15].strip()
data = dict(
num_cuenta = num_cuenta,
reference = reference,
type_mov = type_mov,
importe = importe,
diario = diario,
importe_e = importe_e,
description = description,
)
line = template.format(**data).replace('|', ' ')
return line, uuid
def _export_polizas():
doc = app.active
if not 'POLIZA' in doc:
message = 'No se encontró la hoja: POLIZA'
app.error(message)
app.errorbox(message)
return
message = (
'¿Estás seguro de generar el archivo TXT?\n\n'
'Todos los datos serán tomados de la hoja POLIZA\n\n'
)
if not app.question(message):
return
path = app.paths.join(doc.dir, FILE_TARGET)
source = doc['POLIZA']
data = source['A1'].current_region.data
lines = []
uuids = set()
num_current = ''
for row in data[1:]:
poliza_num = row[12]
if num_current != poliza_num:
if uuids:
for u in uuids:
line = f'AD {u}'
lines.append(line)
uuids = set()
num_current = poliza_num
line = _get_header(row)
lines.append(line)
line, uuid = _get_mov(row)
lines.append(line)
if uuid:
uuids.add(uuid)
line = f'AM {uuid}'
lines.append(line)
if uuids:
for u in uuids:
line = f'AD {u}'
lines.append(line)
app.paths.save(path, '\n'.join(lines))
message = f'Archivo generado correctamente en:\n\n{path}'
app.msgbox(message)
return
@app.catch_exception
def _import_polizas():
if not _validate_sheets():
return
message = (
'¿Estás seguro de procesar este archivo?\n\n'
'Todos los datos de la hoja POLIZA serán reemplazados\n\n'
'ESTA ACCIÓN NO SE PUEDE DESHACER'
)
if not app.question(message):
return
doc = app.active
sheet_catalogo = doc['Catalogo']
target = doc['POLIZA']
data = sheet_catalogo['A1'].current_region.data
catalog = {str(int(r[1])): r[2] for r in data}
path_txt = app.paths.get_file(filters='txt')
try:
data = app.paths.read(path_txt, True)
except UnicodeDecodeError:
data = app.paths.read(path_txt, True, 'ISO-8859-1')
rows = []
for r in data:
header = r[:2].strip()
if header == IS_HEADER:
date = app.dates.str_to_date(r[3:11].strip(), '%Y%m%d', True)
number = r[22:26].strip()
type_poliza = r[15:16]
description = r[40:141].strip()
poliza = (date, number, type_poliza, description)
# ~ print(poliza)
else:
account = r[3:13].strip()
name = catalog.get(account, 'NO EXISTE')
ca = r[55:56].strip()
importe = r[57:69].strip()
if ca == IS_CARGO:
cargo = importe
abono = ''
else:
cargo = ''
abono = importe
reference = r[34:55].strip()
description = r[110:211].strip()
movement = (account, name, cargo, abono, '', '', reference,
description, '', '', '') + poliza
rows.append(movement)
target['A2'].data = rows
message = 'Archivo procesado correctamente'
app.msgbox(message)
return
def _validate_sheets():
doc = app.active
if not 'Catalogo' in doc:
message = 'No se encontró la hoja: Catalogo'
app.error(message)
app.errorbox(message)
return False
if not 'POLIZA' in doc:
message = 'No se encontró la hoja: POLIZA'
app.error(message)
app.errorbox(message)
return False
return True