Skip to content
Open
Show file tree
Hide file tree
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
107 changes: 84 additions & 23 deletions openupgrade_scripts/scripts/stock_account/19.0.1.1/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@
from openupgradelib import openupgrade


def product_value_product_id(env):
"""
Fill product.value#product_id from move_id.product_id
"""
env.cr.execute(
"""
UPDATE product_value
SET product_id=stock_move.product_id
FROM stock_move
WHERE product_value.move_id=stock_move.id
AND product_value.product_id IS NULL
"""
)


def stock_move_account_move_id(env):
"""
Fill stock.move#account_move_id from account.move#stock_move_id
Expand Down Expand Up @@ -64,40 +49,116 @@ def stock_location_valuation_account_id(env):
Set stock.location#valuation_account_id from valuation_in_account_id and
valuation_out_account_id if they are the same
"""
env.cr.execute(
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_location
SET valuation_account_id=valuation_in_account_id
WHERE
valuation_in_account_id=valuation_out_account_id
"""
""",
)


def stock_move_value(env):
"""
Set stock.move#value to sum of product.value#value for this move
"""
env.cr.execute(
openupgrade.logged_query(
env.cr,
"""
UPDATE stock_move
SET value=aggregated_values.agg_value
FROM (
SELECT
move_id, sum(value) AS agg_value
stock_move_id, sum(value) AS agg_value
FROM
product_value
GROUP BY move_id
stock_valuation_layer
GROUP BY stock_move_id
) aggregated_values
WHERE aggregated_values.move_id=stock_move.id
WHERE aggregated_values.stock_move_id=stock_move.id
""",
)


def product_value(env):
"""
Fill product.value with stock valuations not assigned to a move
(=manual valuations)
"""
env.cr.execute(
"""
ALTER TABLE product_value
ADD COLUMN IF NOT EXISTS stock_valuation_layer_id int
""",
)
# simple case: the valuation layer has unit_cost set
env.cr.execute(
"""
INSERT INTO product_value
(
create_uid, create_date, write_uid, write_date, date, lot_id,
product_id, user_id, value, company_id, description,
stock_valuation_layer_id
)
SELECT
create_uid, create_date, write_uid, write_date, create_date, lot_id,
product_id, create_uid, unit_cost, company_id, description,
id
FROM stock_valuation_layer
WHERE
stock_move_id IS NULL
AND
unit_cost IS NOT NULL
""",
)
# otherwise: compute unit cost from sum of all previous values/sum of quantities
env.cr.execute(
"""
INSERT INTO product_value
(
create_uid, create_date, write_uid, write_date,
date, lot_id, product_id, user_id,
company_id, description, value,
stock_valuation_layer_id
)
SELECT
svl1.create_uid, svl1.create_date, svl1.write_uid, svl1.write_date,
svl1.create_date, svl1.lot_id, svl1.product_id, svl1.create_uid,
svl1.company_id, svl1.description, SUM(svl2.value) / SUM(svl2.quantity),
svl1.id
FROM stock_valuation_layer svl1
JOIN stock_valuation_layer svl2
ON
svl1.product_id=svl2.product_id
AND
svl1.company_id=svl2.company_id
AND
(
svl1.lot_id=svl2.lot_id
OR svl1.lot_id IS NULL AND svl2.lot_id IS NULL
)
AND
svl1.create_date >= svl2.create_date
WHERE
svl1.stock_move_id IS NULL
AND
svl1.unit_cost IS NULL
AND
svl1.quantity = 0
GROUP BY
svl1.id
HAVING
SUM(svl2.quantity) <> 0
""",
)
openupgrade.lift_constraints(env.cr, "stock_valuation_layer", "id", cascade=True)


@openupgrade.migrate()
def migrate(env, version):
product_value_product_id(env)
stock_move_account_move_id(env)
product_category_property_valuation(env)
stock_location_valuation_account_id(env)
stock_move_value(env)
product_value(env)
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,6 @@

from openupgradelib import openupgrade

_renamed_models = [
("stock.valuation.layer", "product.value"),
]

_renamed_tables = [
("stock_valuation_layer", "product_value"),
]

_renamed_fields = [
("product.value", "product_value", "", ""),
]

_copied_columns = {
"product_value": [
("create_date", "date", None),
("create_uid", "user_id", None),
("stock_move_id", "move_id", None),
],
}

_deleted_xmlids = [
"stock_account.stock_valuation_layer_company_rule",
"stock_account.group_stock_accounting_automatic",
Expand Down Expand Up @@ -81,10 +61,6 @@ def stock_move_is_fields(env):

@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_models(env.cr, _renamed_models)
openupgrade.rename_tables(env.cr, _renamed_tables)
openupgrade.rename_fields(env, _renamed_fields)
openupgrade.copy_columns(env.cr, _copied_columns)
openupgrade.delete_records_safely_by_xml_id(env, _deleted_xmlids)
stock_lot_avg_cost(env)
stock_move_is_fields(env)
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---Models in module 'stock_account'---
obsolete model stock.valuation.layer

# DONE: renamed to product.value
# DONE: used to compute stock.move#value

obsolete model stock.valuation.layer.revaluation [transient]

# NOTHING TO DO

new model product.value

# DONE: renamed from stock.valuation.layer
# DONE: filled from stock.valuation.layer where appropriate

new model stock_account.stock.valuation.report [abstract]
new model stock.avco.report [abstract]
Expand Down Expand Up @@ -64,32 +64,23 @@ stock_account / product.template / valuation (selection) : selec

stock_account / product.value / company_id (many2one) : NEW relation: res.company, required, hasdefault: compute

# NOTHING TO DO
# DONE: set from stock.valuation.layer

stock_account / product.value / date (datetime) : NEW required, hasdefault: default

# DONE: set from create_date
# DONE: set from stock.valuation.layer#create_date

stock_account / product.value / description (char) : NEW
stock_account / product.value / lot_id (many2one) : NEW relation: stock.lot

# NOTHING TO DO

stock_account / product.value / move_id (many2one) : NEW relation: stock.move

# DONE: renamed from stock_move_id

stock_account / product.value / product_id (many2one) : NEW relation: product.product

# DONE: set from move_id

stock_account / product.value / user_id (many2one) : NEW relation: res.users, required, hasdefault: default

# DONE: set from create_uid
# DONE: set from stock.valuation.layer

stock_account / product.value / value (float) : NEW required

# NOTHING TO DO
# DONE: set from stock.valuation.layer#unit_cost

stock_account / res.company / account_stock_journal_id (many2one): NEW relation: account.journal
stock_account / res.company / account_stock_valuation_id (many2one): NEW relation: account.account
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
env = locals().get("env")

category_fifo = env["product.category"].create(
{
"name": "FIFO category",
"property_valuation": "real_time",
"property_cost_method": "fifo",
}
)
category_avg = env["product.category"].create(
{
"name": "AVG category",
"property_valuation": "real_time",
"property_cost_method": "average",
}
)

product_fifo = env["product.product"].create(
{
"name": "FIFO product",
"categ_id": category_fifo.id,
"standard_price": 10,
"is_storable": True,
}
)

product_avg = env["product.product"].create(
{
"name": "AVG product",
"categ_id": category_avg.id,
"standard_price": 10,
"is_storable": True,
}
)

location_customer = env.ref("stock.stock_location_customers")
location_supplier = env.ref("stock.stock_location_suppliers")
location_stock = env.ref("stock.stock_location_stock")


def buy(product, **kwargs):
in_move = env["stock.move"].create(
dict(
{
"name": "/",
"product_id": product.id,
"location_id": location_supplier.id,
"location_dest_id": location_stock.id,
},
**kwargs,
)
)
in_move._action_confirm()
in_move.picked = True
in_move._action_done()
return in_move


# fifo product
buy(product_fifo, price_unit=20, product_uom_qty=2)
buy(product_fifo, price_unit=22, product_uom_qty=2)
product_fifo_out_move = env["stock.move"].create(
{
"name": "OUT FIFO",
"product_id": product_fifo.id,
"location_id": location_stock.id,
"location_dest_id": location_customer.id,
"product_uom_qty": 2,
}
)
product_fifo_out_move._action_confirm()
product_fifo_out_move.picked = True
product_fifo_out_move._action_assign(force_qty=2)
product_fifo_out_move._action_done()
assert product_fifo.standard_price == 22
# avg product
buy(product_avg, price_unit=20, product_uom_qty=2)
buy(product_avg, price_unit=22, product_uom_qty=2)
assert product_avg.standard_price == 21
# setting the price must happen in its own transaction, otherwise the create date
# of the resulting stock valuation layer is equal to the previous or following ones
env.cr.commit()
product_avg.standard_price = 22
env.cr.commit()
Loading
Loading