Skip to content

Commit

Permalink
Merge pull request #36 from Gedeelde-Weelde/bug/GED-64-product-withou…
Browse files Browse the repository at this point in the history
…t-plu-code

Bug/ged 64 product without plu code
  • Loading branch information
jurcello authored Jan 6, 2025
2 parents 5578aec + 7eeaf67 commit 9029ffa
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion product_digi_sync/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Check https://github.com/odoo/odoo/blob/16.0/odoo/addons/base/data/ir_module_category_data.xml
# for the full list
"category": "Sales/Point of Sale",
"version": "16.0.0.0.1",
"version": "16.0.0.1.0",
"data": [
"security/ir.model.access.csv",
"views/product_template_views.xml",
Expand Down
23 changes: 23 additions & 0 deletions product_digi_sync/migrations/16.0.0.1.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging

# ruff: noqa
_logger = logging.getLogger(__name__)


def migrate(cr, version):
_logger.info("Removing sql constraint shop_plucode_uniq")
# Safely drop the SQL constraint if it exists
cr.execute(
"""
DO $$ BEGIN
IF EXISTS (
SELECT 1
FROM information_schema.table_constraints
WHERE constraint_name = 'product_template_shop_plucode_uniq'
AND table_name = 'product_template'
) THEN
ALTER TABLE product_template DROP CONSTRAINT product_template_shop_plucode_uniq;
END IF;
END $$;
"""
)
31 changes: 21 additions & 10 deletions product_digi_sync/models/product_template.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import re

from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.tools import get_barcode_check_digit

from odoo.addons.queue_job.exception import RetryableJobError
Expand All @@ -14,7 +14,7 @@
class ProductTemplate(DigiSyncBaseModel, models.Model):
_inherit = "product.template"

shop_plucode = fields.Integer(string="Shop plucode", required=False)
shop_plucode = fields.Integer(string="Shop plucode", required=False, default=None)
send_to_scale = fields.Boolean(string="Send to scale", required=False)
is_weighted_article = fields.Boolean(
string="Weighted article", required=False, default=True
Expand All @@ -24,14 +24,6 @@ class ProductTemplate(DigiSyncBaseModel, models.Model):
string="Show packed date on label", required=False
)

_sql_constraints = [
(
"shop_plucode_uniq",
"unique(shop_plucode)",
"Plu code must be unique.",
),
]

def get_current_barcode_rule(self):
weighted_barcode_rule = self._get_barcode_rule("weighted_barcode_rule_id")
piece_barcode_rule = self._get_barcode_rule("piece_barcode_rule_id")
Expand Down Expand Up @@ -143,3 +135,22 @@ def send_quality_image_to_digi_directly(self):
client.send_product_quality_image_to_digi(self)
except Exception as e:
raise RetryableJobError(str(e), 5) from e

@api.constrains("shop_plucode")
def _check_unique_shop_plucode(self):
for record in self:
if record.shop_plucode:
existing_record = self.search(
[
("shop_plucode", "=", record.shop_plucode),
("id", "!=", record.id),
],
limit=1,
)
if existing_record:
raise models.ValidationError(
_(
"The Shop PLU code must be "
"unique! Code '{code}' is already used."
).format(code=record.shop_plucode)
)
21 changes: 21 additions & 0 deletions product_digi_sync/tests/test_product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from PIL import Image

from odoo.exceptions import ValidationError

from odoo.addons.product_digi_sync.models.digi_client import DigiClient
from odoo.addons.queue_job.models.base import Base as QueueJobBase

Expand Down Expand Up @@ -234,6 +236,25 @@ def test_it_sends_the_product_quality_image_to_digi_only_when_quality_is_changed
self.assertEqual(mock_send_product_image_to_digi.call_count, 1)
patched_get_param.stop()

def test_that_a_plucode_can_always_be_zero(self):
product1 = self.env["product.template"].create(
{"name": "Test Product 1", "shop_plucode": 0}
)
product2 = self.env["product.template"].create(
{"name": "Test Product 2", "shop_plucode": 0}
)
self.assertEqual(product1.shop_plucode, 0)
self.assertEqual(product2.shop_plucode, 0)

def test_that_a_plucode_cannot_have_the_same_value_when_unequal_to_zero(self):
with self.assertRaises(ValidationError):
self.env["product.template"].create(
{"name": "Test Product 1", "shop_plucode": 42}
)
self.env["product.template"].create(
{"name": "Test Product 2", "shop_plucode": 42}
)

def _create_product_with_image(self, name, shop_plucode):
product_with_image = self.env["product.template"].create(
{
Expand Down

0 comments on commit 9029ffa

Please sign in to comment.