Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
1 Balas
4441 Tampilan

I made this SQL query and got sale orders that I needed, but I still need to join this sale orders with their invoices and filter only those sale orders that have invoices with state != 'draft'.

but can't find a way to join with sale orders with invoices.

I think there is table called sale_order_invoice_rel but can't figure out how to join and filter correctly

SELECT DISTINCT so.id

FROM stock_picking sp

INNER JOIN stock_move sm

ON sp.id = sm.picking_id

INNER JOIN procurement_order po

ON sm.procurement_id = po.id

INNER JOIN sale_order_line sol

ON po.sale_line_id = sol.id

INNER JOIN sale_order so

ON sol.order_id = so.id

WHERE so.invoice_status = 'to invoice' and sp.state = 'done'
ORDER BY so.id ASC
Avatar
Buang
Jawaban Terbai
Could something like this work for you? (I know this is an old thread - but when solving this for my own needs I came upon it)

SELECT so.name as orderno,  p.name as partner_name, so.client_order_ref, so.amount_untaxed, so.amount_total, aml.name, aml.move_name as invoice

FROM public.sale_order so
LEFT JOIN public.sale_order_line sol on aml.id = olr.invoice_line_id
WHERE so.state = 'sale' and so.invoice_status = 'invoiced' 
ORDER BY p.name ASC, so.amount_total DESC, aml.name ASC

Avatar
Buang

Hi grf,
Can you try this way,

SELECT DISTINCT so.id
FROM stock_picking sp
INNER JOIN stock_move sm
ON sp.id = sm.picking_id
INNER JOIN procurement_order po
ON sm.procurement_id = po.id
INNER JOIN sale_order_line sol
ON po.sale_line_id = sol.id
INNER JOIN sale_order so
ON sol.order_id = so.id
INNER JOIN sale_order_invoice_rel rel
ON so.id = rel.order_id
INNER JOIN account_invoice ai
ON rel.invoice_id = ai.id
WHERE so.invoice_status = 'to invoice'
AND sp.state = 'done'
AND ai.state != 'draft'
ORDER BY so.id ASC

Post Terkait Replies Tampilan Aktivitas
1
Nov 22
5853
1
Okt 21
3410
2
Nov 18
10322
4
Mar 15
5379
1
Mar 15
6126