Uecko_ERP_FactuGES_sync/app/db/sql_sentences.py
david 1490d8baf2 Recuperar de la BD el número de factura completo, con ceros delante si los tuviera.
Increment version to 0.3.3 and update SQL queries and environment variables for tax settings
2026-09-01 18:50:52 +02:00

218 lines
12 KiB
Python

# =========================
# MYSQL (constantes)
# =========================
SELECT_PROFORMAS_DELETED = (
"SELECT ci.id "
"FROM proformas as ci "
"WHERE "
"(ci.deleted_at is not null) "
)
SELECT_CUSTOMER_BY_FACTUGES = (
"SELECT customers.id FROM customers WHERE customers.factuges_id=%s"
)
SELECT_PAYMENT_METHOD_BY_FACTUGES = (
"SELECT payment_methods.id FROM payment_methods WHERE payment_methods.factuges_id=%s"
)
INSERT_CUSTOMER = (
"INSERT INTO customers (id, name, tin, street, city, province, postal_code, country, language_code, "
"phone_primary, phone_secondary, mobile_primary, mobile_secondary, "
"email_primary, email_secondary, website, factuges_id, company_id, is_company, "
"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,%s,%s,'EUR','active',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)"
)
INSERT_PAYMENT_METHOD = (
"INSERT INTO payment_methods (id, description, factuges_id, created_at, updated_at) "
"VALUES (%s,%s,%s,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)"
)
INSERT_INVOICE = (
"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, "
"subtotal_amount_scale, discount_amount_scale, discount_percentage_scale, taxable_amount_scale, taxes_amount_scale, total_amount_scale, "
"language_code, currency_code, created_at, updated_at) "
"SELECT "
"%s AS id, "
"%s AS company_id, "
"LPAD((COALESCE(MAX(invoice_number), 0) + 1), 3, '0') 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 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, "
"2,2,2,2,2,2, 'es','EUR', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP "
"FROM customer_invoices "
"WHERE company_id = %s "
"AND is_proforma = %s "
"AND deleted_at is null "
"AND series = %s"
)
INSERT_VERIFACTU_RECORD = (
"INSERT INTO verifactu_records (id, invoice_id, estado, url, qr, uuid, created_at, updated_at) "
"VALUES (%s, %s, %s, '', '', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)"
)
INSERT_INVOICE_BAK = (
"INSERT INTO customer_invoices (id, company_id, invoice_number, status, series, reference, invoice_date, operation_date, description, "
"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, "
"subtotal_amount_scale, discount_amount_scale, discount_percentage_scale, taxable_amount_scale, taxes_amount_scale, total_amount_scale, "
"language_code, currency_code, created_at, updated_at) "
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,2,2,2,2,2,2,'es','EUR',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)"
)
INSERT_INVOICE_ITEM = (
"INSERT INTO customer_invoice_items "
"(item_id, invoice_id, position, description, quantity_value, unit_amount_value, subtotal_amount_value, "
"discount_percentage_value, discount_amount_value, global_discount_percentage_value, global_discount_amount_value, total_discount_amount_value, "
" taxable_amount_value, total_amount_value, "
"iva_code, iva_percentage_value, iva_amount_value, "
"rec_code, rec_percentage_value, rec_amount_value, taxes_amount_value, "
"quantity_scale, unit_amount_scale, subtotal_amount_scale, "
"discount_percentage_scale, discount_amount_scale, global_discount_percentage_scale, global_discount_amount_scale, total_discount_amount_scale, taxable_amount_scale, total_amount_scale, "
"iva_percentage_scale, iva_amount_scale, rec_percentage_scale, rec_amount_scale, taxes_amount_scale, retention_percentage_scale, retention_amount_scale, "
"created_at, updated_at) "
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,2,4,4,2,4,2,4,4,4,4,2,4,2,4,4,2,4,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)"
)
INSERT_INVOICE_TAX = (
"INSERT INTO customer_invoice_taxes (tax_id, invoice_id, taxable_amount_value, iva_code, iva_percentage_value, iva_amount_value, "
"rec_code, rec_percentage_value, rec_amount_value, retention_code, retention_percentage_value, retention_amount_value, taxes_amount_value, "
"taxable_amount_scale, iva_percentage_scale, iva_amount_scale, rec_percentage_scale, rec_amount_scale, retention_percentage_scale, retention_amount_scale, taxes_amount_scale, "
"created_at, updated_at) "
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,2,2,2,2,2,2,2,2,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)"
)
# INSERT_INVOICE_ITEM_TAX = (
# "INSERT INTO customer_invoice_item_taxes "
# "(tax_id, item_id, tax_code, taxable_amount_value, taxes_amount_value, taxable_amount_scale, taxes_amount_scale, created_at, updated_at) "
# "VALUES (%s,%s,%s,%s,%s,4,2,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)"
# )
UPDATE_CUSTOMER = (
"UPDATE customers SET name=%s, tin=%s, street=%s, city=%s, province=%s, postal_code=%s, country=%s, language_code=%s, is_company=%s, "
"phone_primary=%s, phone_secondary=%s, mobile_primary=%s, mobile_secondary=%s, "
"email_primary=%s, email_secondary=%s, website=%s, updated_at=CURRENT_TIMESTAMP "
"WHERE (id=%s)"
)
# =========================
# FIREBIRD (constantes)
# =========================
SELECT_FACTUGES_FACTURAS_CLIENTE = (
"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, facdet.POSICION"
)
SELECT_FACTUGES_FACTURAS_CLIENTE_CAB = (
"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 "
"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 "
"WHERE "
"(fac.VERIFACTU = 1) "
"AND (fac.ID_VERIFACTU is null)"
"ORDER BY fac.ID"
)
SELECT_FACTUGES_FACTURAS_CLIENTE_DET = (
"SELECT 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_DETALLES AS facdet "
"WHERE "
"(facdet.ID = ?) "
"ORDER BY facdet.POSICION"
)
UPDATE_FACTUGES_LINK = (
"UPDATE FACTURAS_CLIENTE "
"SET VERIFACTU=?, "
"ID_VERIFACTU=?, "
"VERIFACTU_NOTES =? "
"WHERE ID=?"
)
LIMPIAR_FACTUGES_LINK = (
"UPDATE FACTURAS_CLIENTE "
"SET ID_VERIFACTU = NULL, "
"VERIFACTU = 0 "
"WHERE (ID_VERIFACTU = ?)"
)
# =========================
# SENTENCIAS PARA VERIFACTI
# =========================
# OPCION A SACAMOS EL RESUMEN DE LA TAXES DE LA CABECERA
consulta_sql_customer_invoices_issue = (
"SELECT ci.id, ci.invoice_series_code as series, SUBSTRING(ci.invoice_reference, LENGTH(ci.invoice_series_code) + 1) AS 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.iva_code, cit.taxable_amount_value, cit.taxes_amount_value, "
"vr.id as vrId, vr.uuid, vr.estado "
"FROM issued_invoices as ci "
"LEFT JOIN issued_invoice_taxes cit on (ci.id = cit.issued_invoice_id) "
"LEFT JOIN verifactu_records vr on (ci.id = vr.issued_invoice_id) "
"WHERE (ci.status= 'issued') "
"AND (company_id = %s) "
"AND (vr.estado <> 'Correcto') "
"order by reference"
)
# OPCION B SACAMOS LOS IVAS DE LOS DETALLES DE LOS ITEM
# SELECT ci.id, ci.series, ci.invoice_number, ci.invoice_date, ci.description, ci.customer_tin, ci.customer_name, ci.total_amount_value,
# ciit.tax_code, sum(ciit.taxable_amount_value), sum(ciit.taxes_amount_value)
# FROM customer_invoices as ci
# LEFT JOIN customer_invoice_items cii on (ci.id = cii.invoice_id)
# LEFT JOIN customer_invoice_item_taxes ciit on (cii.item_id = ciit.item_id)
# WHERE (ci.is_proforma = 0) AND (ci.status= 'issued')
# group by 1,2,3,4,5,6,7,8,9
update_verifactu_records_with_invoiceId = ("UPDATE verifactu_records "
"set estado = %s, "
"uuid = %s, "
"url = %s, "
"qr = %s, "
"updated_at = CURRENT_TIMESTAMP "
"WHERE issued_invoice_id = %s"
)
update_verifactu_records_with_uuid = ("UPDATE verifactu_records "
"set estado = %s,"
"operacion = %s, "
"updated_at = CURRENT_TIMESTAMP "
"WHERE uuid = %s "
)