Skip to content

Commit

Permalink
[IMP] product_expiry_configurable: Add migration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
rousseldenis committed Apr 7, 2023
1 parent e70d132 commit 916d963
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
46 changes: 46 additions & 0 deletions product_expiry_configurable/migrations/16.0.1.0.0/post-migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade


def _fill_in_columns(env):
"""
Create columns to fill in the previous module 'specific' fields
"""
if openupgrade.column_exists(
env.cr, "product_category", "specific_expiration_time"
):
query = """
UPDATE product_category
SET expiration_time = specific_expiration_time
"""
openupgrade.logged_query(env.cr, query)
if openupgrade.column_exists(env.cr, "product_category", "specific_use_time"):
query = """
UPDATE product_category
SET use_time = specific_use_time
"""
openupgrade.logged_query(env.cr, query)
if openupgrade.column_exists(env.cr, "product_category", "specific_use_time"):
query = """
UPDATE product_category
SET use_time = specific_use_time
"""
openupgrade.logged_query(env.cr, query)
if openupgrade.column_exists(env.cr, "product_category", "specific_removal_time"):
query = """
UPDATE product_category
SET removal_time = specific_removal_time
"""
openupgrade.logged_query(env.cr, query)
if openupgrade.column_exists(env.cr, "product_category", "specific_alert_time"):
query = """
UPDATE product_category
SET alert_time = specific_alert_time
"""
openupgrade.logged_query(env.cr, query)


@openupgrade.migrate()
def migrate(env, version):
_fill_in_columns(env)
50 changes: 50 additions & 0 deletions product_expiry_configurable/migrations/16.0.1.0.0/pre-migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade


def _create_columns(env):
"""
Create columns to fill in the previous module 'specific' fields
"""
field_names = [
(
"expiration_time",
"product.category",
"product_category",
"integer",
"integer",
"product_expiry_configurable",
),
(
"use_time",
"product.category",
"product_category",
"integer",
"integer",
"product_expiry_configurable",
),
(
"removal_time",
"product.category",
"product_category",
"integer",
"integer",
"product_expiry_configurable",
),
(
"alert_time",
"product.category",
"product_category",
"integer",
"integer",
"product_expiry_configurable",
),
]
if not openupgrade.column_exists(env.cr, "product_category", "expiration_time"):
openupgrade.add_fields(env, field_names)


@openupgrade.migrate()
def migrate(env, version):
_create_columns(env)
1 change: 0 additions & 1 deletion product_expiry_configurable/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class ProductTemplate(models.Model):
use_expiration_date = fields.Boolean(
compute="_compute_use_expiration_date", readonly=False, store=True
)

expiration_time = fields.Integer(
compute="_compute_date_fields",
store=True,
Expand Down
10 changes: 7 additions & 3 deletions product_expiry_configurable/models/stock_lot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ def _search_quants_domain(self, expiry_lots):
("location_id.scrap_location", "=", False),
]

def _get_expired_lots_domain_for_remind(self, date_field, date_reminded_field):
return [
(date_field, "<=", fields.Date.today()),
(date_reminded_field, "=", False),
]

@api.model
def _expiry_date_exceeded(self, date_field=False):
"""Log an activity on internally stored lots whose "date" field has been reached.
No further activity will be generated on lots whose "date"
has already been reached (even if the "date" is changed).
"""

date_reminded_field = "%s_reminded" % date_field

expiry_lots = self.env["stock.lot"].search(
[(date_field, "<=", fields.Date.today()), (date_reminded_field, "=", False)]
self._get_expired_lots_domain_for_remind(date_field, date_reminded_field)
)

lot_stock_quants = self.env["stock.quant"].search(
Expand Down

0 comments on commit 916d963

Please sign in to comment.