From 5adf7086aac01b4c0a2ed9a0dc7d756f9cadeb16 Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Fri, 6 Aug 2021 22:02:30 -0500 Subject: [PATCH] Agregar soporte para rangos de fechas --- source/cfdi-descarga.py | 27 +++++++++++++++++++------ source/sat/sat_web.py | 1 + source/sat/util.py | 44 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/source/cfdi-descarga.py b/source/cfdi-descarga.py index 2c5d48c..c62d548 100755 --- a/source/cfdi-descarga.py +++ b/source/cfdi-descarga.py @@ -41,13 +41,26 @@ def _process_command_line_arguments(): parser.add_argument('-fn', '--fiel-nombre', help=help, default='fiel') help = "Descargar por Tipo: t=todos(default), e=emitidas, r=recibidas" - parser.add_argument('-t', '--tipo', help=help, dest='type', default='t', choices=['t', 'e', 'r']) + parser.add_argument('-t', '--tipo', help=help, + dest='type', default='t', choices=['t', 'e', 'r']) help = "Año de la descarga entre 2014 y el año actual (predeterminado)." - parser.add_argument('-a', '--año', help=help, dest='year', default=year, type=int, choices=range(2014, year+1)) + parser.add_argument('-a', '--año', help=help, + dest='year', default=year, type=int, choices=range(2014, year+1)) help = "Mes de la descarga, el mes actual es el predeterminado" - parser.add_argument('-m', '--mes', help=help, dest='month', default=0, type=int, choices=range(13)) + parser.add_argument('-m', '--mes', help=help, + dest='month', default=0, type=int, choices=range(13)) help = "Día de la descarga, de forma predeterminada no se usa" - parser.add_argument('-d', '--dia', help=help, dest='day', default=0, type=int, choices=range(32)) + parser.add_argument('-d', '--dia', help=help, + dest='day', default=0, type=int, choices=range(32)) + help = "Intervalo de días a partir de la fecha actual y hacia a atras" + parser.add_argument('-ud', '--ultimos-dias', help=help, + dest='last_days', default=0, type=int, choices=range(30)) + help = "Fecha inicial AAAA-MM-DD" + parser.add_argument('-fi', '--fecha-inicial', help=help, + dest='date_start', default='') + help = "Fecha final AAAA-MM-DD" + parser.add_argument('-ff', '--fecha-final', help=help, + dest='date_end', default='') help = 'Solicitar descarga' parser.add_argument('-sd', '--solicitar-descarga', help=help, @@ -59,9 +72,11 @@ def _process_command_line_arguments(): parser.add_argument('-da', '--descargar-archivos', help=help, action='store_true', default=False, required=False) help = 'ID de solicitud' - parser.add_argument('-id', '--id-solicitud', dest='id_request', help=help, default='') + parser.add_argument('-id', '--id-solicitud', dest='id_request', + help=help, default='') help = 'ID archivo' - parser.add_argument('-ida', '--id-archivo', dest='id_file', help=help, default='') + parser.add_argument('-ida', '--id-archivo', dest='id_file', + help=help, default='') help = 'Ruta de descarga de archivos' parser.add_argument('-dd', '--directorio-descargas', dest='path_download', help=help, default='') diff --git a/source/sat/sat_web.py b/source/sat/sat_web.py index 1f5015f..2df7737 100644 --- a/source/sat/sat_web.py +++ b/source/sat/sat_web.py @@ -131,6 +131,7 @@ class SATWebService(): # ~ soap = ET.tostring(root, pretty_print=True, encoding='utf-8') soap = ET.tostring(root) + return soap def _get_token(self): diff --git a/source/sat/util.py b/source/sat/util.py index 6a1ef70..9acfcd8 100644 --- a/source/sat/util.py +++ b/source/sat/util.py @@ -3,7 +3,7 @@ import getpass import uuid from calendar import monthrange -from datetime import datetime +from datetime import datetime, timedelta from pathlib import Path from time import sleep @@ -126,6 +126,21 @@ def _get_cert(data): return cert +def _to_date(str_date, end=False): + error = '' + dt = None + try: + parts = str_date.split('-') + if end: + dt = datetime(int(parts[0]), int(parts[1]), int(parts[2]), 23, 59, 59) + else: + dt = datetime(int(parts[0]), int(parts[1]), int(parts[2]), 0, 0, 0) + except Exception as e: + error = 'Fecha inválida' + + return error, dt + + def _validate_requests_args(args): result, data = _validate_fiel_args(args) if not result: @@ -149,14 +164,37 @@ def _validate_requests_args(args): data['month'] = args.month data['day'] = args.day + now = today() + + if args.last_days: + date_start = now.replace(hour=0, minute=0, second=0, microsecond=0) \ + - timedelta(days=args.last_days) + date_end = now.replace(hour=23, minute=59, second=59, microsecond=0) + data['date_start'] = date_start + data['date_end'] = date_end + return True, data + + if args.date_start and args.date_end: + error, date_start = _to_date(args.date_start) + if error: + log.error(error) + return False, {} + + error, date_end = _to_date(args.date_end, True) + if error: + log.error(error) + return False, {} + + data['date_start'] = date_start + data['date_end'] = date_end + return True, data + if data['day']: if not validate_date(data['year'], data['month'], data['day']): msg = 'Fecha inválida' log.error(msg) return False, {} - now = today() - month1 = month2 = data['month'] if month1 == 0: month1 = 1