Jump to content

Consulta SQL para exportar múltiples pedidos con detalles


Eutanasio

Recommended Posts

Hola,

No estoy seguro de cómo abordar esto. Me gustaría una consulta SQL para PS 1.7 que permita exportar pedidos por Fecha y Estado del pedido. Necesito esto para que más tarde pueda usar otro programa para generar múltiples facturas.

La exportación debe contener, para cada pedido, todos los detalles posibles (detalles del pedido, pago, cliente, productos, totales, envío, descuentos, envoltura de regalo...).

¿Es posible? ¿O es demasiado complicado y complejo?

Link to comment
Share on other sites

Si es posible pero depende de que datos específicamente necesites de cada una de las entidades que mencionas. A groso modo sería algo como:

SELECT * FROM `ps_orders` o
INNER JOIN ps_order_detail od ON(o.id_order = od.id_order)
INNER JOIN ps_customer c ON(o.id_customer = c.id_customer)

Pero ya si quieres obtener los detalles de la dirección de facturación + envío y los cupones aplicados y las tasas de impuesto aplicadas, etc etc etc, entonces si se podría mucho mas complejo el asunto.

Suerte

  • Like 1
Link to comment
Share on other sites

Gracias ChatGPT!

SELECT
    o.id_order AS order_id,
    o.reference AS order_reference,
    c.firstname AS customer_firstname,
    c.lastname AS customer_lastname,
    a.address1,
    a.address2,
    a.postcode,
    a.city,
    cl.name AS country,
    s.name AS state,
    GROUP_CONCAT(CONCAT(od.product_name, ' (Qty: ', od.product_quantity, ', Price: ', od.product_price, ')') SEPARATOR '; ') AS product_details,
    o.total_shipping_tax_incl AS shipping_cost,
    o.total_wrapping_tax_incl AS wrapping_cost,
    payment.payment_details,
    o.total_paid_tax_excl,
    o.total_paid_tax_incl,
    (o.total_paid_tax_incl - o.total_products_wt - o.total_shipping_tax_incl - o.total_wrapping_tax_incl) AS order_discount,
    o.date_add AS order_date
FROM
    ps_orders o
JOIN
    ps_customer c ON o.id_customer = c.id_customer
JOIN
    ps_address a ON o.id_address_invoice = a.id_address
JOIN
    ps_country_lang cl ON a.id_country = cl.id_country AND o.id_lang = cl.id_lang
LEFT JOIN
    ps_state s ON a.id_state = s.id_state
JOIN
    ps_order_detail od ON o.id_order = od.id_order
LEFT JOIN
    (
        SELECT
            order_reference,
            GROUP_CONCAT(CONCAT(payment_method, ': ', amount) SEPARATOR '; ') AS payment_details
        FROM
            ps_order_payment
        GROUP BY
            order_reference
    ) AS payment ON o.reference = payment.order_reference
WHERE
    o.reference IN ('OrderID1', 'OrderID2', 'OrderID3')
GROUP BY
    o.id_order
ORDER BY
    o.id_order;

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...