This question has been flagged

Hi friends,

I have written a sql query for return sale order records which are all in (type of on delivery order),they all are deliverd and invoiced.In report there must be date invoice fields also required against particular sale order(only when that sale order is in done state).I have descibed my sql query for your reference:


select sol.id,

so.name,

so.partner_id as partner_id,

so.date_order,

sol.deliver_date::date,

sp.date_done,

pt.name as product,

sol.product_uom_qty as qty,

so.state,

so.shipped,

(case when so.state='done' then ai.date_invoice end) as date_invoice

from sale_order_line sol

left join sale_order so on so.id=sol.order_id

left join product_product pp on pp.id=sol.product_id

left join product_template pt on pt.id = pp.product_tmpl_id

left join stock_picking sp on sp.sale_id=so.id

left join res_partner res on res.id=so.partner_id

left join account_invoice ai on ai.origin = so.name

where 1=1

and sp.type='out'

and so.state!='draft'


For sale order there is only link between sale order and account invoice is origin field.But it didnt worked properly.For some records the query didnt returned date invoice(only date invoice field is required from account_invoice table).So plese friends help me in this situation.Thanks in Advance.

 

Avatar
Discard
Best Answer

Hello,

You can fetch invoices from table sale_order_invoice_rel, this table contains 2 fields order_id (sale order ID) and invoice_id (Invoice ID).

Hope this will helps you.

Thanks.

Avatar
Discard

Thanks a lot. I had a feeling this was already present in Odoo. thanks for pointing it out! On another note. Sometimes you don't want to link an invoice with an so, but link a line of an so to an invoice, the table sale_order_line_invoice_rel is used for these cases.