tener en cuenta en los detalles si viene null dejar el null en cantidad, importes...

This commit is contained in:
David Arranz 2025-09-18 12:41:06 +02:00
parent f9c374449f
commit 0b5fac15af

View File

@ -22,9 +22,11 @@ def sync_invoices(conn_factuges, conn_mysql, last_execution_date):
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, facdet.VISIBLE, facdet.FECHA_ALTA as FECHA_ALTA_DET, facdet.FECHA_MODIFICACION as FECHA_MODIFICACION_DET "
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 "
@ -160,10 +162,9 @@ def eliminar_datos(conn_factuges, ids_verifactu_deleted, config):
def insertar_datos(conn_mysql, filas, conn_factuges, config):
cte_company_id = '5e4dc5b3-96b9-4968-9490-14bd032fec5f'
insert_customer_query = (
"INSERT INTO customers (id, name, tin, street, city, province, postal_code, country, factuges_id, company_id, "
# "email, phone, fax, website, legal_record, default_tax,"
"is_company, language_code, currency_code, status, created_at, updated_at ) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 1, 'es', 'EUR', 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) "
"INSERT INTO customers (id, name, tin, street, city, province, postal_code, country, phone_primary, phone_secondary, mobile_primary, mobile_secondary, "
"email_primary, email_secondary, website, factuges_id, company_id, is_company, language_code, currency_code, status, created_at, updated_at ) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 1, 'es', 'EUR', 'active', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) "
)
insert_customer_invoices_query = (
@ -225,10 +226,24 @@ def insertar_datos(conn_mysql, filas, conn_factuges, config):
discount_amount_value = (
factura_detalle['IMPORTE_DESCUENTO'] or 0)*100
discount_percentage_value = (
factura_detalle['DESCUENTO'] or 0)*100 if (factura_detalle['DESCUENTO']) is not None else None
factura_detalle['DESCUENTO'] or 0)*100 if (factura_detalle['DESCUENTO']) is not None else 0
taxable_amount_value = (factura_detalle['BASE_IMPONIBLE'] or 0)*100
# Preparamos el tipo de IVA, en FactuGES es único
tax_code = str(factura_detalle['DES_TIPO_IVA'])
tax_amount_value = (factura_detalle['IMPORTE_IVA'] or 0)*100
if tax_code == 'IVA21':
tax_code = 'iva_21'
elif tax_code == 'IVA18':
tax_code = 'iva_18'
elif tax_code == 'IVA16':
tax_code = 'iva_16'
elif tax_code == 'IVA10':
tax_code = 'iva_10'
else:
tax_code = ''
# La cuota de impuestos es el IVA + RE
tax_amount_value = (
(factura_detalle['IMPORTE_IVA'] or 0) + (factura_detalle['IMPORTE_RE'] or 0))*100
total_amount_value = (factura_detalle['IMPORTE_TOTAL'] or 0)*100
payment_method_id = str(factura_detalle['ID_FORMA_PAGO'])
payment_method_description = str(factura_detalle['DES_FORMA_PAGO'])
@ -241,25 +256,33 @@ def insertar_datos(conn_mysql, filas, conn_factuges, config):
customer_city = str(factura_detalle['POBLACION'])
customer_province = str(factura_detalle['PROVINCIA'])
customer_postal_code = str(factura_detalle['CODIGO_POSTAL'])
customer_phone_primary = factura_detalle['TELEFONO_1']
customer_phone_secondary = factura_detalle['TELEFONO_2']
customer_mobile_primary = factura_detalle['MOVIL_1']
customer_mobile_secondary = factura_detalle['MOVIL_2']
customer_email_primary = factura_detalle['EMAIL_1']
customer_email_secondary = factura_detalle['EMAIL_2']
customer_webside = str(factura_detalle['PAGINA_WEB'])
customer_country = 'es'
item_position = int(factura_detalle['POSICION'])
item_description = str(factura_detalle['CONCEPTO'])
item_quantity_value = (factura_detalle['CANTIDAD'] or 0)*100
item_unit_amount_value = (
item_quantity_value = None if factura_detalle['CANTIDAD'] is None else (
factura_detalle['CANTIDAD'] or 0)*100
item_unit_amount_value = None if factura_detalle['IMPORTE_UNIDAD'] is None else (
factura_detalle['IMPORTE_UNIDAD'] or 0)*10000
item_discount_percentage_value = (
factura_detalle['DESCUENTO'] or 0)*100
item_discount_amount = None
# item_discount_amount = (
# (factura_detalle['IMPORTE_UNIDAD'] or 0)*((factura_detalle['DESCUENTO'] or 0)/100))*100
item_total_amount = (factura_detalle['IMPORTE_TOTAL'] or 0)*100
item_total_amount = None if factura_detalle['IMPORTE_TOTAL_DET'] is None else (
factura_detalle['IMPORTE_TOTAL_DET'] or 0)*100
# campos pendiente de revisar en un futuro
# xxxxxxx = str(factura_detalle['ID_EMPRESA'])
# xxxxxxx = str(factura_detalle['ID_FORMA_PAGO']) según este id se debe de guardar en la factura los vencimiento asociados a la forma de pago
# xxxxxxx = str(factura_detalle['OBSERVACIONES'])
# RE, IMPORTE_RE >> en el caso que este relleno debo trasladarlo a los detalles de la factura
# Comprobamos si existe el cliente del primer item de la factura
if factuges_id_anterior is None or factuges_id_anterior != factuges_id:
@ -272,7 +295,8 @@ def insertar_datos(conn_mysql, filas, conn_factuges, config):
logging.info(
f"Inserting customer {factuges_customer_id} {customer_tin} {customer_name}")
cursorMySQL.execute(insert_customer_query, (customer_id, customer_name, customer_tin, customer_street, customer_city, customer_province,
customer_postal_code, customer_country, factuges_customer_id, cte_company_id))
customer_postal_code, customer_country, customer_phone_primary, customer_phone_secondary, customer_mobile_primary,
customer_mobile_secondary, customer_email_primary, customer_email_secondary, customer_webside, factuges_customer_id, cte_company_id))
else:
# Si ya exite ponemos el id del customer correspondiente
customer_id = str(row[0])
@ -290,14 +314,18 @@ def insertar_datos(conn_mysql, filas, conn_factuges, config):
# payment_method_id, payment_method_description,
customer_id, customer_tin, customer_name, customer_street, customer_city, customer_province, customer_postal_code, customer_country))
# Insertamos el IVA
cursorMySQL.execute(insert_customer_invoices_taxes_query, (str(uuid4()),
id_customer_invoice, tax_code, taxable_amount_value, tax_amount_value))
# Insertamos el IVA y RE si viene
if (factura_detalle['IVA'] > 0):
taxable_amount_value = (
factura_detalle['BASE_IMPONIBLE'])*100
tax_amount_value = (factura_detalle['IMPORTE_IVA'])*100
cursorMySQL.execute(insert_customer_invoices_taxes_query, (str(uuid4()),
id_customer_invoice, tax_code, taxable_amount_value, tax_amount_value))
# Insertamos el RE si viene
if (factura_detalle['RECARGO_EQUIVALENCIA'] > 0):
tax_code = (factura_detalle['RE'])*100
taxable_amount_value = (factura_detalle['RE'])*100
tax_code = 're_5_2'
taxable_amount_value = (
factura_detalle['BASE_IMPONIBLE'])*100
tax_amount_value = (factura_detalle['IMPORTE_RE'])*100
cursorMySQL.execute(insert_customer_invoices_taxes_query, (str(uuid4()),
id_customer_invoice, tax_code, taxable_amount_value, tax_amount_value))