2025-11-05 17:43:40 +00:00
# =========================
2025-11-06 19:18:37 +00:00
# MYSQL (constantes)
2025-11-05 17:43:40 +00:00
# =========================
2025-11-20 18:51:03 +00:00
SELECT_INVOICES_DELETED = (
" SELECT ci.id "
" FROM customer_invoices as ci "
" WHERE "
" (ci.deleted_at is not null) "
2025-11-05 17:43:40 +00:00
)
2025-11-20 18:51:03 +00:00
SELECT_CUSTOMER_BY_FACTUGES = (
" SELECT customers.id FROM customers WHERE customers.factuges_id= %s "
2025-11-05 17:43:40 +00:00
)
SELECT_PAYMENT_METHOD_BY_FACTUGES = (
" SELECT payment_methods.id FROM payment_methods WHERE payment_methods.factuges_id= %s "
)
2025-11-20 18:51:03 +00:00
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) "
)
2025-11-05 17:43:40 +00:00
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 = (
2025-11-20 18:51:03 +00:00
" INSERT INTO customer_invoices (id, company_id, invoice_number, status, is_proforma, series, reference, invoice_date, operation_date, description, "
2025-11-06 19:18:37 +00:00
" 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, "
" COALESCE(MAX(invoice_number + 0),0)+1 AS invoice_number, "
2025-11-20 18:51:03 +00:00
" %s AS status, %s AS is_proforma, %s AS series, %s AS reference, %s AS invoice_date, %s AS operation_date, %s AS description, "
2025-11-06 19:18:37 +00:00
" %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 "
2025-11-20 18:51:03 +00:00
" AND is_proforma = %s "
2025-11-06 19:18:37 +00:00
" AND deleted_at is null "
)
2025-11-20 18:51:03 +00:00
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) "
)
2025-11-06 19:18:37 +00:00
INSERT_INVOICE_BAK = (
2025-11-05 17:43:40 +00:00
" 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, "
" discount_percentage_value, discount_amount_value, total_amount_value, "
" quantity_scale, unit_amount_scale, discount_amount_scale, total_amount_scale, discount_percentage_scale, created_at, updated_at) "
" VALUES ( %s , %s , %s , %s , %s , %s , %s , %s , %s ,2,4,2,4,2,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP) "
)
INSERT_INVOICE_TAX = (
" INSERT INTO customer_invoice_taxes (tax_id, invoice_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 ,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) "
)
2025-11-20 18:51:03 +00:00
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 ) "
2025-11-06 19:18:37 +00:00
)
# =========================
# FIREBIRD (constantes)
# =========================
2025-11-20 18:51:03 +00:00
2025-11-06 19:18:37 +00:00
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 > 0) "
f " AND (fac.ID_VERIFACTU is null) "
f " ORDER BY (fac.ID) "
)
2025-11-05 17:43:40 +00:00
UPDATE_FACTUGES_LINK = (
" UPDATE FACTURAS_CLIENTE "
" SET ID_VERIFACTU=? "
" WHERE ID=? "
)
LIMPIAR_FACTUGES_LINK = (
" UPDATE FACTURAS_CLIENTE "
" SET ID_VERIFACTU = NULL, "
" VERIFACTU = 0 "
" WHERE (ID_VERIFACTU = ?) "
2025-11-06 19:18:37 +00:00
)
2025-11-20 18:51:03 +00:00
# =========================
# SENTENCIAS PARA VERIFACTI
# =========================
# 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, "
2025-11-21 18:42:03 +00:00
f " cit.taxable_amount_scale, cit.taxes_amount_scale, cit.tax_code, cit.taxable_amount_value, cit.taxes_amount_value, "
2025-11-20 18:51:03 +00:00
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 "
)
# 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 invoice_id = %s "
)
update_verifactu_records_with_uuid = ( " UPDATE verifactu_records "
" set estado = %s , "
" operacion = %s , "
" updated_at = CURRENT_TIMESTAMP "
" WHERE uuid = %s "
)