Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] sale_manual_delivery: Improve init script performances #3477

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions sale_manual_delivery/hook.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
def pre_init_hook(cr):
# Fields are not configured with decimal precision - declare them as double precision
cr.execute(
"""
ALTER TABLE sale_order_line ADD COLUMN IF NOT EXISTS qty_procured numeric;
ALTER TABLE sale_order_line ADD COLUMN IF NOT EXISTS qty_procured
double precision DEFAULT 0;
COMMENT ON COLUMN sale_order_line.qty_procured IS 'Quantity Procured';
"""
)
cr.execute(
"""
ALTER TABLE sale_order_line ADD COLUMN IF NOT EXISTS qty_to_procure numeric;
ALTER TABLE sale_order_line ADD COLUMN IF NOT EXISTS qty_to_procure double precision;
COMMENT ON COLUMN sale_order_line.qty_to_procure IS 'Quantity to Procure"';
"""
)

cr.execute(
"""
update sale_order_line as sol set qty_procured = r.qty_procured,
qty_to_procure = sol.product_uom_qty - r.qty_procured
from (select sol.id, sum(
case
when (
sl.usage = 'customer'
and sm.origin_returned_move_id is null
or (
sm.origin_returned_move_id is not null and sm.to_refund
)) then
UPDATE sale_order_line
SET qty_to_procure = product_uom_qty
WHERE qty_delivered_method <> 'stock_move'
OR (qty_delivered_method = 'stock_move'
AND state IN ('draft', 'sent', 'cancel', 'done'))
"""
)

cr.execute(
"""
update sale_order_line as sol
set qty_procured = r.qty_procured, qty_to_procure = sol.product_uom_qty - r.qty_procured
from (select sol.id, sum(
case
when (
sl.usage = 'customer'
and sm.origin_returned_move_id is null
or (
sm.origin_returned_move_id is not null and sm.to_refund
)) then
ROUND(
((sm.product_uom_qty / sm_product_uom.factor) * sol_product_uom.factor),
SCALE(sol_product_uom.rounding)
)
when (
sl.usage != 'customer'
and sm.to_refund
) then
ROUND(
((sm.product_uom_qty / sm_product_uom.factor) * sol_product_uom.factor),
SCALE(sol_product_uom.rounding)
)
when (
sl.usage != 'customer'
and sm.to_refund
) then
ROUND(
((sm.product_uom_qty / sm_product_uom.factor) * sol_product_uom.factor),
SCALE(sol_product_uom.rounding)
) * -1
else 0
end)
AS qty_procured
((sm.product_uom_qty / sm_product_uom.factor) * sol_product_uom.factor),
SCALE(sol_product_uom.rounding)
) * -1
else 0
end)
AS qty_procured
from
sale_order_line as sol
inner join (
Expand All @@ -50,6 +62,8 @@ def pre_init_hook(cr):
and sol.product_id = sm.product_id
and sm.sale_line_id = sol.id
)
WHERE sol.qty_delivered_method = 'stock_move' AND sol.state = 'sale'

) as q on q.id = sol.id
left join stock_move as sm on sm.id = q.move_id
left join product_product as pp on pp.id = sol.product_id
Expand All @@ -60,5 +74,6 @@ def pre_init_hook(cr):
group by sol.id, sm.product_uom, sol.product_uom
) as r
where r.id = sol.id
AND sol.qty_delivered_method = 'stock_move' AND sol.state = 'sale'
"""
)
Loading