Uecko_ERP_FactuGES_sync/app/db/sync_orders.py
2025-08-28 10:51:05 +02:00

217 lines
8.2 KiB
Python

import logging
from decimal import Decimal
from config import load_config
from utils import text_converter
def sync_orders(conn_factuges, conn_mysql):
config = load_config()
consulta_quotes_uecko = (
"SELECT quotes.id, quotes.date_sent, quotes.reference, quotes.customer_reference, "
"quotes.customer_information, quotes.dealer_id, dealers.id_contact, dealers.name "
"FROM quotes INNER JOIN dealers ON (dealers.id = quotes.dealer_id) "
"WHERE quotes.date_sent IS NOT NULL AND "
"quotes.id_contract IS NULL"
)
update_quotes_uecko = (
"UPDATE quotes SET "
"id_contract = %s "
"WHERE id = %s"
)
inserted_orders = []
cursor_MySQL = None
try:
cursor_MySQL = conn_mysql.cursor()
cursor_MySQL.execute(consulta_quotes_uecko)
quotes = cursor_MySQL.fetchall()
quote_columns = [desc[0] for desc in cursor_MySQL.description]
selected_quotes = []
for quote in quotes:
tupla = dict(zip(quote_columns, quote))
selected_quotes.append(tupla)
logging.info(f"Quotes rows to be processed: {len(selected_quotes)}")
if selected_quotes:
for quote in selected_quotes:
logging.info(f"Quote reference: {quote['reference']}")
if (quote['id_contact'] is None):
logging.info(
f"Error: Quote unprocesable (id_contact missing)")
continue
items = fetch_quote_items(conn_mysql, quote['id'])
id_contrato = insert_quote_to_factuges(
conn_factuges, quote, items, config)
cursor_MySQL.execute(update_quotes_uecko,
(int(id_contrato), str(quote['id'])))
inserted_orders.append({
"customer_reference": quote['customer_reference'],
"dealer_name": quote['name'],
})
cursor_MySQL.close()
return inserted_orders
except Exception as e:
# Escribir el error en el archivo de errores
logging.error(msg=e, stack_info=True)
raise e # Re-lanzar la excepción para detener el procesamiento
finally:
# Cerrar la conexión
if cursor_MySQL is not None:
cursor_MySQL.close()
def fetch_quote_items(conn_mysql, quote_id):
consulta_quotes_items_uecko = (
"SELECT quote_items.item_id, quote_items.id_article, quote_items.position, "
"quote_items.description, quote_items.quantity, quote_items.unit_price, "
"quote_items.discount, quote_items.total_price "
"FROM quote_items "
"WHERE quote_items.quote_id = %s "
"ORDER BY quote_items.position"
)
cursor_MySQL = None
try:
cursor_MySQL = conn_mysql.cursor()
cursor_MySQL.execute(consulta_quotes_items_uecko, (quote_id, ))
items = cursor_MySQL.fetchall()
items_columns = [desc[0] for desc in cursor_MySQL.description]
cursor_MySQL.close()
selected_items = []
for item in items:
tupla = dict(zip(items_columns, item))
selected_items.append(tupla)
return selected_items
except Exception as e:
# Escribir el error en el archivo de errores
logging.error(msg=e, stack_info=True)
raise e # Re-lanzar la excepción para detener el procesamiento
finally:
if cursor_MySQL is not None:
cursor_MySQL.close()
def insert_quote_to_factuges(conn_factuges, quote, items, config):
id_empresa = int(config['FACTUGES_ID_EMPRESA'])
situacion = str(config['FACTUGES_CONTRATO_SITUACION'])
id_tienda = int(config['FACTUGES_CONTRATO_ID_TIENDA'])
enviada_revisada = int(config['FACTUGES_CONTRATO_ENVIADA_REVISADA'])
id_cliente = int(quote['id_contact'])
# nombre_clliente = str(quote['name'])
fecha_presupuesto = quote['date_sent'].date()
persona_contacto = str(quote['customer_information'])
referencia_cliente = str(quote['customer_reference'])
select_gen_id_contrato_cliente = (
"select GEN_ID(GEN_CONTRATOS_CLI_ID, 1) from RDB$DATABASE"
)
select_gen_id_presupuesto_cliente = (
"select GEN_ID(GEN_PRESUPUESTOS_CLI_ID, 1) from RDB$DATABASE"
)
insert_contrato_cliente_data = (
"insert into CONTRATOS_CLIENTE ("
"ID, ID_EMPRESA, ID_TIENDA, ID_CLIENTE, NOMBRE, SITUACION, "
"NOTAS_ENVIO, REFERENCIA_CLIENTE, "
"ENVIADA_REVISADA, FECHA_CONTRATO "
") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
)
insert_presupuesto_cliente_data = (
"insert into PRESUPUESTOS_CLIENTE ("
"ID, ID_EMPRESA, ID_TIENDA, ID_CLIENTE, SITUACION, "
"OBSERVACIONES, REFERENCIA_CLIENTE, "
"ENVIADA_REVISADA, FECHA_PRESUPUESTO "
") values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
)
insert_contrato_cliente_detalles_data = (
"insert into CONTRATOS_CLIENTE_DETALLES ("
"ID, ID_CONTRATO, POSICION, ID_ARTICULO, TIPO_DETALLE, "
"CONCEPTO, CANTIDAD, IMPORTE_UNIDAD, "
"VALORADO, VISIBLE, FECHA_ALTA "
") values ("
"GEN_ID(GEN_CONTRATOS_CLI_DETALLE_ID, 1), ?, ?, ?, ?, "
"?, ?, ?, "
"1, 1, CURRENT_TIMESTAMP"
")"
)
insert_presupuesto_cliente_detalles_data = (
"insert into PRESUPUESTOS_CLIENTE_DETALLES ("
"ID, ID_PRESUPUESTO, POSICION, ID_ARTICULO, TIPO_DETALLE, "
"CONCEPTO, CANTIDAD, IMPORTE_UNIDAD, "
"VALORADO, VISIBLE, FECHA_ALTA "
") values ("
"GEN_ID(GEN_PRESUPUESTOS_CLI_DETALLE_ID, 1), ?, ?, ?, ?, "
"?, ?, ?, "
"1, 1, CURRENT_TIMESTAMP"
")"
)
cursor_FactuGES = None
try:
cursor_FactuGES = conn_factuges.cursor()
cursor_FactuGES.execute(select_gen_id_presupuesto_cliente)
id_presupuesto = int(cursor_FactuGES.fetchone()[0])
logging.info(
f"Inserting quote on FactuGES -> id_preupuesto = {str(id_presupuesto)}")
logging.info(insert_presupuesto_cliente_data)
logging.info((id_presupuesto, id_empresa, id_tienda, id_cliente,
situacion, fecha_presupuesto, persona_contacto,
referencia_cliente, enviada_revisada, fecha_presupuesto))
cursor_FactuGES.execute(insert_presupuesto_cliente_data,
(id_presupuesto, id_empresa, id_tienda, id_cliente,
situacion, persona_contacto,
referencia_cliente, enviada_revisada, fecha_presupuesto))
logging.info(
f"Inserting items. Quote items length to be processed: {len(items)}")
for item in items:
descripcion_iso = text_converter(
item['description'], charset_destino='ISO8859_1', longitud_maxima=2000)
quantity = Decimal(
int(item['quantity'])) / Decimal(100) if item['quantity'] is not None else None
unit_price = Decimal(int(
item['unit_price'])) / Decimal(100) if item['unit_price'] is not None else None
# total_price = item['total_price']
logging.info(str(insert_presupuesto_cliente_detalles_data))
logging.info((
id_presupuesto, item['position'], item['id_article'], config['FACTUGES_CONTRATO_TIPO_DETALLE'],
descripcion_iso, quantity, unit_price
))
cursor_FactuGES.execute(insert_presupuesto_cliente_detalles_data, (
id_presupuesto, item['position'], item['id_article'], config['FACTUGES_CONTRATO_TIPO_DETALLE'],
descripcion_iso, quantity, unit_price
))
cursor_FactuGES.close()
return id_presupuesto
except Exception as e:
# Escribir el error en el archivo de errores
logging.error(msg=e, stack_info=True)
raise e # Re-lanzar la excepción para detener el procesamiento
finally:
if cursor_FactuGES is not None:
cursor_FactuGES.close()