You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*Your plan here*
SELECT
*
FROM (
SELECT
sm.id as sm_id,
pt.id AS pid,
COALESCE(pt.default_code, 'Тодорхойгүй') AS pcode,
CASE
WHEN ptrans.id IS NOT NULL THEN ptrans.value
WHEN pt.name IS NOT NULL THEN pt.name
ELSE 'Тодорхойгүй'
END AS pname,
uom.id AS uom_id,
uom.name AS uom_name,
COALESCE(property.value_float, 0) AS ip_price,
coalesce(SUM(CASE
WHEN sml.id IS null THEN sm.product_qty
WHEN sml.qty_done is null or sml.qty_done = 0 THEN sml.product_uom_qty
ELSE sml.qty_done
END),0) AS quantity,
CASE
WHEN loc.usage = 'internal'
AND dest.usage = 'internal' THEN 'internal'
WHEN sm.inventory_id IS NOT NULL
AND dest.usage = 'internal' THEN 'inventory_income'
WHEN sm.inventory_id IS NOT NULL
AND loc.usage = 'internal' THEN 'inventory_expense'
WHEN sm.scrapped = true
AND sm.expense_line_id IS NULL THEN 'scrap'
WHEN sm.sale_line_id IS NOT NULL
AND loc.usage = 'internal' THEN 'sale'
WHEN sm.sale_line_id IS NOT NULL
AND dest.usage = 'internal' THEN 'sale_refund'
WHEN sm.purchase_line_id IS NOT NULL
AND dest.usage = 'internal' THEN 'purchase'
WHEN sm.purchase_line_id IS NOT NULL
AND loc.usage = 'internal' THEN 'purchase_refund'
WHEN sm.transit_line_id IS NOT NULL
AND loc.usage = 'internal' THEN 'transit_expense'
WHEN sm.transit_line_id IS NOT NULL
AND dest.usage = 'internal' THEN 'transit_income'
ELSE 'znone'
END AS type,
loc.id AS loc_id,
dest.id AS dest_id,
loc.usage AS loc_usage,
dest.usage AS dest_usage,
COALESCE(CASE WHEN soll.price_unit >0 THEN soll.price_unit ELSE sm.sale_price END,0) as sale_price,
COALESCE(CASE WHEN SOLL.discount_reward_move_line_amount !=0 THEN SOLL.discount_reward_move_line_amount else 0 end) as add_disc_price,
COALESCE(sm.sale_price, 0) AS price,
(sm.date at time zone 'utc' AT TIME ZONE 'ULAT') ::TIMESTAMP::DATE AS date,
sp.id as picking_id,
sm.state AS state,
swm.id,
swm.name as sw_name,
COALESCE(psp.price, property.value_float, 0) as price_start,
COALESCE(plp.price, property.value_float, 0) as price_end,
COALESCE(CASE WHEN soll.discount != 0 THEN ((soll.price_unit * soll.product_uom_qty) * (soll.discount/100)) ELSE 0 END) AS sol_discount,
COALESCE(CASE WHEN soll.price_unit < 0 and soll.is_reward_line = true THEN abs(sum(soll.price_unit * soll.product_uom_qty)) ELSE 0 END, 0) AS sol_reward,
COALESCE(CASE WHEN soll.is_reward_line = true and pt.type = 'product' THEN (abs(soll.price_unit * soll.product_uom_qty)) ELSE 0 END) AS sol_discount2,
soo.is_loan_payment as is_loan,
COALESCE(soll.discount, 0) as discount
FROM
stock_move sm
INNER JOIN stock_location loc ON loc.id = sm.location_id
INNER JOIN stock_location dest ON dest.id = sm.location_dest_id
INNER JOIN product_product pp ON pp.id = sm.product_id
INNER JOIN (SELECT * FROM product_template WHERE ID > 0) pt ON pt.id = pp.product_tmpl_id
INNER JOIN uom_uom uom ON uom.id = pt.uom_id
LEFT JOIN stock_move_line sml ON sml.move_id = sm.id
LEFT JOIN stock_picking sp ON sp.id = sm.picking_id
LEFT JOIN sale_order soo ON soo.id = sp.sale_id
LEFT JOIN sale_order_line soll ON soll.order_id = soo.id AND sm.sale_line_id = soll.id
LEFT JOIN stock_inventory si ON si.id = sm.inventory_id
INNER JOIN stock_warehouse swm ON swm.id = sm.warehouse_id
LEFT JOIN ir_property property ON property.res_id = CONCAT('product.template,', pt.id)
AND property.company_id = sm.company_id
AND property.type = 'float'
AND property.name = swm.price_type
LEFT JOIN ir_translation ptrans ON pt.id = ptrans.res_id
AND ptrans.name = 'product.template,name'
AND ptrans.lang = 'mn_MN'
LEFT JOIN stock_transit_order_line stol ON stol.id = sm.transit_line_id
LEFT JOIN stock_transit_order sto ON sto.id = stol.order_id
LEFT JOIN stock_warehouse sw ON sw.id = sto.supplied_wh_id
LEFT JOIN stock_warehouse sww ON sww.id = sto.supplier_wh_id
LEFT JOIN (
SELECT price_type, product_id,
(case when price_type = 'price' then price else wholesale_price end) as price,
row_number() OVER (PARTITION BY price_type, product_id ORDER BY "datetime" DESC) as distance
FROM product_price_history
WHERE datetime <= '2024-10-26 16:00:00' AND company_id=1 AND price_type IN ('wholesale_price', 'price')
) psp on pp.id = psp.product_id AND REPLACE(swm.price_type, 'list_price', 'price') = psp.price_type AND psp.distance=1
LEFT JOIN (
SELECT price_type, product_id,
(case when price_type = 'price' then price else wholesale_price end) as price,
row_number() OVER (PARTITION BY price_type, product_id ORDER BY "datetime" DESC) as distance
FROM product_price_history
WHERE datetime <= '2024-10-27 15:59:59' AND company_id=1 AND price_type IN ('wholesale_price', 'price')
) plp on pp.id = plp.product_id AND REPLACE(swm.price_type, 'list_price', 'price') = plp.price_type AND plp.distance=1
WHERE
sm.company_id = 1
AND sm.state = 'done'
AND sm.date <= '2024-10-27 15:59:59'
AND swm.id = 1745
AND ( loc.id = ANY (ARRAY[13996])
OR dest.id = ANY (ARRAY[13996])
)
GROUP BY
sm.id,
loc.id,
dest.id,
pp.id,
pt.id,
pt.default_code,
pt.name,
pt.type,
property.id,
ptrans.id,
uom.id,
stol.id,
sp.id,
sw.id,
sto.id,
sww.id,
soll.id,
swm.id,
psp.price,
plp.price,
soo.is_loan_payment
UNION
SELECT
0 as sm_id,
pt.id AS pid,
COALESCE(pt.default_code, 'Тодорхойгүй') AS pcode,
CASE
WHEN ptrans.id IS NOT NULL THEN ptrans.value
WHEN pt.name IS NOT NULL THEN pt.name
ELSE 'Тодорхойгүй'
END AS pname,
uom.id AS uom_id,
uom.name AS uom_name,
0 AS ip_price,
coalesce(soll.product_uom_qty,0) AS quantity,
'sale' AS type,
null AS loc_id,
null AS dest_id,
'' AS loc_usage,
'' AS dest_usage,
0 as sale_price,
COALESCE(CASE WHEN SOLL.discount_reward_move_line_amount !=0 THEN SOLL.discount_reward_move_line_amount else 0 end) as add_disc_price,
COALESCE(soll.price_unit, 0) AS price,
(soo.date_order at time zone 'utc' AT TIME ZONE 'ULAT') ::TIMESTAMP::DATE AS date,
null as picking_id,
soo.state AS state,
swm.id,
swm.name as sw_name,
0 as price_start,
0 as price_end,
COALESCE(CASE WHEN soll.discount != 0 THEN ((soll.price_unit * soll.product_uom_qty) * (soll.discount/ 100)) ELSE 0 END) AS sol_discount,
COALESCE(CASE WHEN soll.price_unit < 0 and soll.is_reward_line = true THEN abs(sum(soll.price_unit * soll.product_uom_qty)) ELSE 0 END, 0) AS sol_reward,
COALESCE(CASE WHEN soll.is_reward_line = true and pt.type = 'product' THEN (abs(soll.price_unit * soll.product_uom_qty)) ELSE 0 END) AS sol_discount2,
soo.is_loan_payment as is_loan,
COALESCE(soll.discount, 0) as discount
FROM
sale_order_line soll
INNER JOIN product_product pp ON pp.id = soll.product_id
INNER JOIN (SELECT * FROM product_template WHERE ID > 0) pt ON pt.id = pp.product_tmpl_id
LEFT JOIN ir_translation ptrans ON pt.id = ptrans.res_id
AND ptrans.name = 'product.template,name'
AND ptrans.lang = 'mn_MN'
INNER JOIN uom_uom uom ON uom.id = pt.uom_id
LEFT JOIN sale_order soo ON soo.id = soll.order_id
INNER JOIN stock_warehouse swm ON swm.id = soo.warehouse_id
WHERE
soo.company_id = 1
AND soo.state = 'done'
AND soo.date_order <= '2024-10-27 15:59:59'
AND swm.id = 1745
and pt.type ='service'
GROUP BY
soll.id,
pp.id,
pt.id,
pt.default_code,
pt.name,
pt.type,
uom.id,
ptrans.id,
swm.id,
soo.id
UNION
select 0 as sm_id,
pt.id AS pid,
COALESCE(pt.default_code, 'Тодорхойгүй') AS pcode,
CASE
WHEN ptrans.id IS NOT NULL THEN ptrans.value
WHEN pt.name IS NOT NULL THEN pt.name
ELSE 'Тодорхойгүй'
END AS pname,
uom.id AS uom_id,
uom.name AS uom_name,
COALESCE(property.value_float, 0) AS ip_price,
aml.quantity AS quantity,
'sale_refund_discount' AS type,
0 AS loc_id,
0 AS dest_id,
'customer' AS loc_usage,
'internal' AS dest_usage,
aml.price_unit as sale_price,
aml.price_unit as add_disc_price,
aml.price_unit AS price,
am.date AS date,
0 as picking_id,
'done' AS state,
swm.id,
swm.name as sw_name,
COALESCE(psp.price, property.value_float, 0) as price_start,
COALESCE(plp.price, property.value_float, 0) as price_end,
aml.price_unit AS sol_discount,
1 AS sol_reward,
0 AS sol_discount2,
am.is_loan_payment as is_loan,
0 as discount
from account_move_line aml
left join account_move am on am.id = aml.move_id
INNER JOIN product_product pp ON pp.id = aml.product_id
INNER JOIN (SELECT * FROM product_template WHERE ID > 0) pt ON pt.id = pp.product_tmpl_id
INNER JOIN uom_uom uom ON uom.id = pt.uom_id
INNER JOIN stock_warehouse swm ON swm.id = am.warehouse_id
LEFT JOIN ir_property property ON property.res_id = CONCAT('product.template,', pt.id)
AND property.company_id = am.company_id
AND property.type = 'float'
AND property.name = swm.price_type
LEFT JOIN ir_translation ptrans ON pt.id = ptrans.res_id
AND ptrans.name = 'product.template,name'
AND ptrans.lang = 'mn_MN'
LEFT JOIN (
SELECT price_type, product_id,
(case when price_type = 'price' then price else wholesale_price end) as price,
row_number() OVER (PARTITION BY price_type, product_id ORDER BY "datetime" DESC) as distance
FROM product_price_history
WHERE datetime <= '2023-05-31 15:59:59' AND company_id=1 AND price_type IN ('wholesale_price', 'price')
) psp on pp.id = psp.product_id AND REPLACE(swm.price_type, 'list_price', 'price') = psp.price_type AND psp.distance=1
LEFT JOIN (
SELECT price_type, product_id,
(case when price_type = 'price' then price else wholesale_price end) as price,
row_number() OVER (PARTITION BY price_type, product_id ORDER BY "datetime" DESC) as distance
FROM product_price_history
WHERE datetime <= '2023-06-30 16:00:00' AND company_id=1 AND price_type IN ('wholesale_price', 'price')
) plp on pp.id = plp.product_id AND REPLACE(swm.price_type, 'list_price', 'price') = plp.price_type AND plp.distance=1
where am.state = 'posted' and am.date <= '2024-10-27 15:59:59' and aml.is_sales_discount_line is null
and am.type = 'out_refund' and aml.exclude_from_invoice_tab = false and pt.type!='product'
AND swm.id = 1745 and am.company_id = 1
) AS moves
WHERE
moves.type IN ( 'purchase', 'purchase_refund',
'sale', 'sale_refund', 'sale_refund_discount', 'transit_income',
'transit_expense', 'inventory_income',
'inventory_expense', 'internal_expense',
'internal_expense_refund', 'approval_request',
'scrap', 'internal', 'znone')
and state ='done'
The text was updated successfully, but these errors were encountered:
Version of PEV2: xxxx
Copy the failing plan
The text was updated successfully, but these errors were encountered: