Skip to content

Commit

Permalink
[OU-FIX] loyalty_criteria_multi_product: column type + condition + cr…
Browse files Browse the repository at this point in the history
…iteria + repeat_product

- It should be VARCHAR, not CHAR.
- The selection value is 'multi_product', not 'multi_gift'.
- Do the product m2m handle.
- Do the repeat_product handlement by SQL + fix it.
  • Loading branch information
pedrobaeza committed Jul 13, 2024
1 parent 819119e commit 5c36ffa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
from openupgradelib import openupgrade


def _copy_multi_product_criteria(env):
"""Copy multi-product criteria to the new m2m table. Done this way for avoiding to
rename columns, indexes, FKs, etc on the old m2m table, being this table not very
big for sure.
"""
openupgrade.logged_query(
env.cr,
"""
INSERT INTO loyalty_criteria_product_product_rel
(loyalty_criteria_id, product_product_id)
SELECT coupon_criteria_id, product_product_id
FROM coupon_criteria_product_product_rel
""",
)


def adapt_rules_with_repeat_product(env):
"""Adapt rules with the 'Repeat' option to new promotion rules.
The functionality of the 'Repeat' option has been integrated into the rules
Expand All @@ -11,17 +27,33 @@ def adapt_rules_with_repeat_product(env):
For each loyalty criterion with 'Repeat' enabled, the corresponding loyalty rule
is updated with the minimum quantity and product information. After the update,
the loyalty criterion is unlinked."""
env.cr.execute("SELECT id FROM loyalty_criteria WHERE repeat_product = true")
results = env.cr.fetchall()
loyalty_criteria_ids = [result[0] for result in results]
for loyalty_criteria in env["loyalty.criteria"].browse(loyalty_criteria_ids):
rule = env["loyalty.rule"].browse(loyalty_criteria.rule_id)
rule.minimum_qty = loyalty_criteria.rule_min_quantity
rule.product_ids = rule.product_ids
loyalty_criteria.unlink()
env.cr.commit()
openupgrade.logged_query(
env.cr,
"""
UPDATE loyalty_rule lr
SET minimum_qty = lc.rule_min_quantity
FROM loyalty_criteria lc
WHERE lc.repeat_product
AND lc.rule_id = lr.id
""",
)
openupgrade.logged_query(
env.cr,
"""
INSERT INTO loyalty_rule_product_product_rel rel
(loyalty_rule_id, product_product_id)
SELECT lc.rule_id, rel2.product_product_id
FROM loyalty_criteria_product_product_rel rel2
JOIN loyalty_criteria lc ON lc.id = rel2.loyalty_criteria_id
WHERE lc.repeat_product
""",
)
openupgrade.logged_query(
env.cr, "DELETE FROM loyalty_criteria WHERE repeat_product"
)


@openupgrade.migrate()
def migrate(env, version):
_copy_multi_product_criteria(env)
adapt_rules_with_repeat_product(env)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def move_coupon_criteria_to_rule(env):
env.cr,
"""
ALTER TABLE loyalty_rule
ADD COLUMN IF NOT EXISTS loyalty_criteria CHAR
ADD COLUMN IF NOT EXISTS loyalty_criteria VARCHAR
""",
)
# Set the value of "lotalty_rule.loyalty_criteria" previously defined in
Expand Down Expand Up @@ -41,7 +41,7 @@ def move_coupon_criteria_to_rule(env):
FROM loyalty_rule
WHERE
loyalty_rule.program_id = loyalty_criteria.program_id
AND loyalty_rule.loyalty_criteria = 'multi_gift'
AND loyalty_rule.loyalty_criteria = 'multi_product'
""",
)

Expand Down

0 comments on commit 5c36ffa

Please sign in to comment.