diff --git a/app/db/normalizations.py b/app/db/normalizations.py index 6ccde4b..ee7dd55 100644 --- a/app/db/normalizations.py +++ b/app/db/normalizations.py @@ -1,12 +1,23 @@ -from app.config import logger -from datetime import date -from app.config import load_config import textwrap -from typing import Dict, Any, Optional -from decimal import Decimal, ROUND_HALF_UP -from app.utils import limpiar_cadena, normalizar_telefono_con_plus, corregir_y_validar_email, normalizar_url_para_insert, map_tax_code, cents, cents4, money_round, tax_fraction_from_code +from datetime import date +from decimal import Decimal +from typing import Any, Dict, Optional + from striprtf.striprtf import rtf_to_text +from app.config import load_config +from app.utils import ( + cents, + cents4, + corregir_y_validar_email, + limpiar_cadena, + map_tax_code, + money_round, + normalizar_telefono_con_plus, + normalizar_url_para_insert, + tax_fraction_from_code, +) + def rtf_a_texto_plano(rtf: str) -> str: """ @@ -72,7 +83,7 @@ def normalize_header_invoice_fields(fd: Dict[str, Any]) -> Dict[str, Any]: "operation_date": str(fd['FECHA_FACTURA']), "description": textwrap.shorten( f"{str(fd['REFERENCIA'])} - {str(fd.get('NOMBRE')) or ''}", width=50, placeholder="…"), - + "notes": str(fd["OBSERVACIONES"]), # siempre tendrán 2 decimales 'subtotal_amount_value': cents(fd.get('IMPORTE_NETO')), 'discount_amount_value': cents(fd.get('IMPORTE_DESCUENTO')), diff --git a/app/db/sql_sentences.py b/app/db/sql_sentences.py index 02f14b4..daa35dd 100644 --- a/app/db/sql_sentences.py +++ b/app/db/sql_sentences.py @@ -31,7 +31,7 @@ INSERT_PAYMENT_METHOD = ( ) INSERT_INVOICE = ( - "INSERT INTO customer_invoices (id, company_id, invoice_number, status, is_proforma, series, reference, invoice_date, operation_date, description, " + "INSERT INTO customer_invoices (id, company_id, invoice_number, status, is_proforma, series, reference, invoice_date, operation_date, description, notes, " "subtotal_amount_value, discount_amount_value, discount_percentage_value, taxable_amount_value, taxes_amount_value, total_amount_value, " "customer_id, customer_tin, customer_name, customer_street, customer_city, customer_province, customer_postal_code, customer_country, " "payment_method_id, payment_method_description, factuges_id, " @@ -41,7 +41,7 @@ INSERT_INVOICE = ( "%s AS id, " "%s AS company_id, " "COALESCE(MAX(invoice_number + 0),0)+1 AS invoice_number, " - "%s AS status, %s AS is_proforma, %s AS series, %s AS reference, %s AS invoice_date, %s AS operation_date, %s AS description, " + "%s AS status, %s AS is_proforma, %s AS series, %s AS reference, %s AS invoice_date, %s AS operation_date, %s AS description, %s AS notes, " "%s AS subtotal_amount_value, %s AS discount_amount_value, %s AS discount_percentage_value, %s AS taxable_amount_value, %s AS taxes_amount_value, %s AS total_amount_value, " "%s AS customer_id, %s AS customer_tin, %s AS customer_name, %s AS customer_street, %s AS customer_city, %s AS customer_province, %s AS customer_postal_code, %s AS customer_country, " "%s AS payment_method_id, %s AS payment_method_description, %s AS factuges_id, " @@ -107,23 +107,23 @@ UPDATE_CUSTOMER = ( # ========================= SELECT_FACTUGES_FACTURAS_CLIENTE = ( - f"SELECT fac.VERIFACTU, fac.ID_VERIFACTU, fac.ID || '' AS ID, fac.ID_EMPRESA || '' AS ID_EMPRESA, fac.REFERENCIA, fac.FECHA_FACTURA, fac.ID_CLIENTE || '' as ID_CLIENTE, fac.NIF_CIF, fac.NOMBRE, " - f"fac.CALLE, fac.POBLACION, fac.PROVINCIA, fac.CODIGO_POSTAL, fac.FECHA_ALTA, " - f"fac.IMPORTE_NETO, fac.DESCUENTO, fac.IMPORTE_DESCUENTO, fac.BASE_IMPONIBLE, fac.IVA, fac.IMPORTE_IVA, fac.IMPORTE_TOTAL, " - f"fac.ID_FORMA_PAGO, fp.DESCRIPCION as DES_FORMA_PAGO, fac.ID_TIPO_IVA, ti.REFERENCIA as DES_TIPO_IVA, fac.RECARGO_EQUIVALENCIA, fac.RE, fac.IMPORTE_RE, " - f"fac.ID_CLIENTE, fac.NIF_CIF, fac.NOMBRE, fac.CALLE, fac.POBLACION, fac.PROVINCIA, fac.CODIGO_POSTAL, " - f"cc.TELEFONO_1, cc.TELEFONO_2, cc.MOVIL_1, cc.MOVIL_2, cc.EMAIL_1, cc.EMAIL_2, cc.PAGINA_WEB, " - f"facdet.ID || '' as ID_DET, facdet.ID_FACTURA, facdet.POSICION, facdet.TIPO_DETALLE, facdet.ID_ARTICULO, facdet.CONCEPTO, facdet.CANTIDAD, " - f"facdet.IMPORTE_UNIDAD, facdet.DESCUENTO as DESCUENTO_DET, facdet.IMPORTE_TOTAL as IMPORTE_TOTAL_DET, facdet.VISIBLE, facdet.FECHA_ALTA as FECHA_ALTA_DET, facdet.FECHA_MODIFICACION as FECHA_MODIFICACION_DET " - f"FROM FACTURAS_CLIENTE AS fac " - f"LEFT JOIN CONTACTOS AS cc ON fac.ID_CLIENTE = cc.ID " - f"LEFT JOIN FORMAS_PAGO AS fp ON fac.ID_FORMA_PAGO = fp.ID " - f"LEFT JOIN TIPOS_IVA AS ti ON fac.ID_TIPO_IVA = ti.ID " - f"LEFT JOIN FACTURAS_CLIENTE_DETALLES as facdet ON fac.ID = facdet.ID_FACTURA " - f"WHERE " - f"(fac.VERIFACTU = 1) " - f"AND (fac.ID_VERIFACTU is null)" - f"ORDER BY (fac.ID)" + "SELECT fac.VERIFACTU, fac.ID_VERIFACTU, fac.ID || '' AS ID, fac.ID_EMPRESA || '' AS ID_EMPRESA, fac.REFERENCIA, fac.FECHA_FACTURA, fac.ID_CLIENTE || '' as ID_CLIENTE, fac.NIF_CIF, fac.NOMBRE, " + "fac.CALLE, fac.POBLACION, fac.PROVINCIA, fac.CODIGO_POSTAL, fac.FECHA_ALTA, " + "fac.IMPORTE_NETO, fac.DESCUENTO, fac.IMPORTE_DESCUENTO, fac.BASE_IMPONIBLE, fac.IVA, fac.IMPORTE_IVA, fac.IMPORTE_TOTAL, " + "fac.OBSERVACIONES, fac.ID_FORMA_PAGO, fp.DESCRIPCION as DES_FORMA_PAGO, fac.ID_TIPO_IVA, ti.REFERENCIA as DES_TIPO_IVA, fac.RECARGO_EQUIVALENCIA, fac.RE, fac.IMPORTE_RE, " + "fac.ID_CLIENTE, fac.NIF_CIF, fac.NOMBRE, fac.CALLE, fac.POBLACION, fac.PROVINCIA, fac.CODIGO_POSTAL, " + "cc.TELEFONO_1, cc.TELEFONO_2, cc.MOVIL_1, cc.MOVIL_2, cc.EMAIL_1, cc.EMAIL_2, cc.PAGINA_WEB, " + "facdet.ID || '' as ID_DET, facdet.ID_FACTURA, facdet.POSICION, facdet.TIPO_DETALLE, facdet.ID_ARTICULO, facdet.CONCEPTO, facdet.CANTIDAD, " + "facdet.IMPORTE_UNIDAD, facdet.DESCUENTO as DESCUENTO_DET, facdet.IMPORTE_TOTAL as IMPORTE_TOTAL_DET, facdet.VISIBLE, facdet.FECHA_ALTA as FECHA_ALTA_DET, facdet.FECHA_MODIFICACION as FECHA_MODIFICACION_DET " + "FROM FACTURAS_CLIENTE AS fac " + "LEFT JOIN CONTACTOS AS cc ON fac.ID_CLIENTE = cc.ID " + "LEFT JOIN FORMAS_PAGO AS fp ON fac.ID_FORMA_PAGO = fp.ID " + "LEFT JOIN TIPOS_IVA AS ti ON fac.ID_TIPO_IVA = ti.ID " + "LEFT JOIN FACTURAS_CLIENTE_DETALLES as facdet ON fac.ID = facdet.ID_FACTURA " + "WHERE " + "(fac.VERIFACTU = 1) " + "AND (fac.ID_VERIFACTU is null)" + "ORDER BY (fac.ID)" ) UPDATE_FACTUGES_LINK = ( @@ -148,15 +148,15 @@ LIMPIAR_FACTUGES_LINK = ( # OPCION A SACAMOS EL RESUMEN DE LA TAXES DE LA CABECERA consulta_sql_customer_invoices_issue = ( - f"SELECT ci.id, ci.series, ci.invoice_number, ci.invoice_date, ci.description, ci.customer_tin, ci.customer_name, ci.total_amount_value, ci.total_amount_scale, ci.reference, " - f"cit.taxable_amount_scale, cit.taxes_amount_scale, cit.tax_code, cit.taxable_amount_value, cit.taxes_amount_value, " - f"vr.id as vrId, vr.uuid, vr.estado " - f"FROM customer_invoices as ci " - f"LEFT JOIN customer_invoice_taxes cit on (ci.id = cit.invoice_id) " - f"LEFT JOIN verifactu_records vr on (ci.id = vr.invoice_id) " - f"WHERE (ci.is_proforma = 0) AND (ci.status= 'issued') " - f"AND (vr.estado <> 'Correcto') " - f"order by reference" + "SELECT ci.id, ci.series, ci.invoice_number, ci.invoice_date, ci.description, ci.customer_tin, ci.customer_name, ci.total_amount_value, ci.total_amount_scale, ci.reference, " + "cit.taxable_amount_scale, cit.taxes_amount_scale, cit.tax_code, cit.taxable_amount_value, cit.taxes_amount_value, " + "vr.id as vrId, vr.uuid, vr.estado " + "FROM customer_invoices as ci " + "LEFT JOIN customer_invoice_taxes cit on (ci.id = cit.invoice_id) " + "LEFT JOIN verifactu_records vr on (ci.id = vr.invoice_id) " + "WHERE (ci.is_proforma = 0) AND (ci.status= 'issued') " + "AND (vr.estado <> 'Correcto') " + "order by reference" ) # OPCION B SACAMOS LOS IVAS DE LOS DETALLES DE LOS ITEM diff --git a/app/db/sync_invoices_factuges.py b/app/db/sync_invoices_factuges.py index b23bb74..a6cb4a2 100644 --- a/app/db/sync_invoices_factuges.py +++ b/app/db/sync_invoices_factuges.py @@ -343,6 +343,7 @@ def insert_invoice_header( hif.get("invoice_date"), hif.get("operation_date"), hif.get("description"), + hif.get("notes"), hif.get("subtotal_amount_value"), hif.get("discount_amount_value"), hif.get("discount_percentage_val"),