Skip to content

Commit

Permalink
[FIX] product_internal_reference_generator: create sequence as sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasProgrammer committed Dec 18, 2023
1 parent 7b79aee commit 57b7366
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 20 deletions.
19 changes: 16 additions & 3 deletions product_internal_reference_generator/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ Internal Reference Prefix + progressive number for variant, eg: “Main0001001

"001" the variant identifier.


Every time a new variant is created, a new internal reference is automatically assigned with progressive variant code.

A general setting allows to set the default Internal Reference Template for new products.

Two specific access rights allow specific users to:

- generate an internal reference in product

- create internal reference templates, change internal reference template for a product template once an internal reference has been generated, as well as editing existing internal references.

A specific access rights allows specific users to change internal reference template for a product template once an internal reference has been generated, as well as editing existing ones.

**Table of contents**

Expand All @@ -52,7 +57,7 @@ A specific access rights allows specific users to change internal reference temp
Usage
=====

Go to inventory > Configuration > Internal Reference Templates and set:
Go to Inventory > Configuration > Internal Reference Templates and set:

the sequence to be used for Internal Reference Prefix generation

Expand All @@ -62,6 +67,14 @@ Now go to product template, select an Internal Reference Template and generate a

Each time a new variant is created for this product template, an internal reference is automatically assigned.

In settings > inventory > Default Internal Reference Template, select which template will be loaded automatically each time a new product template is created.

In user, enable extra rights:

- "Generate internal reference" to display "Generate" button in product template.

- "Internal reference template manager" to manage Internal Reference Templates in inventory > configuration menu and edit internal reference templates already assigned to a product template.

Known issues / Roadmap
======================

Expand Down
1 change: 1 addition & 0 deletions product_internal_reference_generator/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"security/groups.xml",
"security/ir.model.access.csv",
"views/product.xml",
"views/res_config_settings.xml",
],
"demo": ["demo/product_code_seq_demo.xml"],
"installable": True,
Expand Down
2 changes: 2 additions & 0 deletions product_internal_reference_generator/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from . import product_code_sequence
from . import product_template
from . import product_product
from . import res_company
from . import res_config_settings
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
# Copyright 2023 Ooops - Ilyas
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models
from odoo import _, exceptions, fields, models


class ProductCodeSequence(models.Model):
_name = "product.code.sequence"
_description = "Internal Reference Template"

active = fields.Boolean(default=True)
name = fields.Char(required=True)
sequence_id = fields.Many2one("ir.sequence", required=True)
variant_reference_numbers = fields.Integer("Digits", default=3, required=True)

def unlink(self):
for rec in self:
products = self.env["product.template"].search(
[("int_ref_template_id", "=", rec.id)]
)
if products:
raise exceptions.ValidationError(
_(
"You can't delete %s template because there is products "
"related to it. You can archive it instead."
" Products: %s" % (rec.name, products.mapped("display_name"))
)
)
return super().unlink()
21 changes: 13 additions & 8 deletions product_internal_reference_generator/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class ProductTemplate(models.Model):
_inherit = "product.template"

int_ref_template_id = fields.Many2one(
"product.code.sequence", "Internal Reference Template"
"product.code.sequence", "Internal Reference Template", copy=True
)
variants_sequence_id = fields.Many2one("ir.sequence")
variants_sequence_id = fields.Many2one("ir.sequence", copy=False)
variants_prefix = fields.Char(
"Internal Reference Prefix", readonly=True, tracking=True
"Internal Reference Prefix", readonly=True, tracking=True, copy=False
)
default_code = fields.Char(copy=False)

@api.onchange("int_ref_template_id")
def onchange_int_ref_template_id(self):
Expand All @@ -21,11 +22,15 @@ def onchange_int_ref_template_id(self):
def btn_generate_sequence(self):
self.ensure_one()
int_ref_next_val = self.int_ref_template_id.sequence_id.next_by_id()
var_seq = self.env["ir.sequence"].create(
{
"name": "variants " + int_ref_next_val,
"padding": self.int_ref_template_id.variant_reference_numbers,
}
var_seq = (
self.env["ir.sequence"]
.sudo()
.create(
{
"name": "variants " + int_ref_next_val,
"padding": self.int_ref_template_id.variant_reference_numbers,
}
)
)
self.write(
{
Expand Down
14 changes: 14 additions & 0 deletions product_internal_reference_generator/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 Ooops - Ilyas
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl)

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

default_int_ref_template_id = fields.Many2one(
"product.code.sequence",
default_model="product.template",
string="Default Internal Reference Template",
)
13 changes: 13 additions & 0 deletions product_internal_reference_generator/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2023 Ooops - Ilyas
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl)

from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

default_int_ref_template_id = fields.Many2one(
related="company_id.default_int_ref_template_id",
readonly=False,
)
9 changes: 7 additions & 2 deletions product_internal_reference_generator/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ Internal Reference Prefix + progressive number for variant, eg: “Main0001001

"001" the variant identifier.


Every time a new variant is created, a new internal reference is automatically assigned with progressive variant code.

A general setting allows to set the default Internal Reference Template for new products.

Two specific access rights allow specific users to:

- generate an internal reference in product

- create internal reference templates, change internal reference template for a product template once an internal reference has been generated, as well as editing existing internal references.

A specific access rights allows specific users to change internal reference template for a product template once an internal reference has been generated, as well as editing existing ones.
10 changes: 9 additions & 1 deletion product_internal_reference_generator/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Go to inventory > Configuration > Internal Reference Templates and set:
Go to Inventory > Configuration > Internal Reference Templates and set:

the sequence to be used for Internal Reference Prefix generation

Expand All @@ -7,3 +7,11 @@ the number of digits to be used for variants code (standard is 3)
Now go to product template, select an Internal Reference Template and generate an Internal Reference Prefix. Internal reference field is now read-only.

Each time a new variant is created for this product template, an internal reference is automatically assigned.

In settings > inventory > Default Internal Reference Template, select which template will be loaded automatically each time a new product template is created.

In user, enable extra rights:

- "Generate internal reference" to display "Generate" button in product template.

- "Internal reference template manager" to manage Internal Reference Templates in inventory > configuration menu and edit internal reference templates already assigned to a product template.
6 changes: 5 additions & 1 deletion product_internal_reference_generator/security/groups.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="group_int_ref_template_always_visible" model="res.groups">
<field name="name">Internal reference template always visible</field>
<field name="name">Internal reference template manager</field>
<field name="category_id" ref="base.module_category_hidden" />
</record>
<record id="group_int_ref_generate" model="res.groups">
<field name="name">Generate internal reference</field>
<field name="category_id" ref="base.module_category_hidden" />
</record>
</odoo>
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
pcs1,pcs1,model_product_code_sequence,base.group_user,1,1,0,0
pcs2,pcs2,model_product_code_sequence,stock.group_stock_manager,1,1,1,1
pcs1,pcs1,model_product_code_sequence,base.group_user,1,0,0,0
pcs2,pcs2,model_product_code_sequence,product_internal_reference_generator.group_int_ref_template_always_visible,1,1,1,1
15 changes: 13 additions & 2 deletions product_internal_reference_generator/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,12 @@ <h1 class="title">Product Internal Reference Generator</h1>
<p>“Main0001” is the prefix generated by sequence and assigned to product template, and</p>
<p>“001” the variant identifier.</p>
<p>Every time a new variant is created, a new internal reference is automatically assigned with progressive variant code.</p>
<p>A specific access rights allows specific users to change internal reference template for a product template once an internal reference has been generated, as well as editing existing ones.</p>
<p>A general setting allows to set the default Internal Reference Template for new products.</p>
<p>Two specific access rights allow specific users to:</p>
<ul class="simple">
<li>generate an internal reference in product</li>
<li>create internal reference templates, change internal reference template for a product template once an internal reference has been generated, as well as editing existing internal references.</li>
</ul>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand All @@ -393,11 +398,17 @@ <h1 class="title">Product Internal Reference Generator</h1>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>Go to inventory &gt; Configuration &gt; Internal Reference Templates and set:</p>
<p>Go to Inventory &gt; Configuration &gt; Internal Reference Templates and set:</p>
<p>the sequence to be used for Internal Reference Prefix generation</p>
<p>the number of digits to be used for variants code (standard is 3)</p>
<p>Now go to product template, select an Internal Reference Template and generate an Internal Reference Prefix. Internal reference field is now read-only.</p>
<p>Each time a new variant is created for this product template, an internal reference is automatically assigned.</p>
<p>In settings &gt; inventory &gt; Default Internal Reference Template, select which template will be loaded automatically each time a new product template is created.</p>
<p>In user, enable extra rights:</p>
<ul class="simple">
<li>“Generate internal reference” to display “Generate” button in product template.</li>
<li>“Internal reference template manager” to manage Internal Reference Templates in inventory &gt; configuration menu and edit internal reference templates already assigned to a product template.</li>
</ul>
</div>
<div class="section" id="known-issues-roadmap">
<h1><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2023 Ooops - Ilyas
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import exceptions
from odoo.tests import Form, SavepointCase


Expand Down Expand Up @@ -60,6 +61,16 @@ def test_all(self):
# no code if no sequence template
self.assertFalse(self.pt_plane.get_variant_next_default_code())

def test_unlink(self):
products = self.env["product.template"].search(
[("int_ref_template_id", "=", self.pt_seq.id)]
)
products.write({"int_ref_template_id": self.pt_seq.id})
with self.assertRaises(exception=exceptions.ValidationError):
self.pt_seq.unlink()
products.write({"int_ref_template_id": False})
self.pt_seq.unlink()

def _check_default_code(self, sequence_str, ind, pt):
self.assertIn(
str(self.pt_seq.sequence_id.number_next_actual - 1) + sequence_str,
Expand Down
8 changes: 8 additions & 0 deletions product_internal_reference_generator/views/product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
type="object"
string="Generate"
attrs="{'invisible': [('int_ref_template_id', '=', False)]}"
groups="product_internal_reference_generator.group_int_ref_generate"
confirm="Are you sure want to generate a new reference prefix?
If variants already exist for this product, the new internal
reference will be assigned only to those variants that
Expand Down Expand Up @@ -64,8 +65,15 @@
<field name="arch" type="xml">
<form>
<sheet>
<widget
name="web_ribbon"
title="Archived"
bg_color="bg-danger"
attrs="{'invisible': [('active', '=', True)]}"
/>
<group>
<group>
<field name="active" invisible="1" />
<field name="name" />
<field name="sequence_id" />
<field name="variant_reference_numbers" />
Expand Down
23 changes: 23 additions & 0 deletions product_internal_reference_generator/views/res_config_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="stock.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[@id='manage_product_packaging']" position="after">
<div
class="col-12 col-lg-6 o_setting_box"
id="int_ref_template_id_setting"
groups="product_internal_reference_generator.group_int_ref_template_always_visible,base.group_system"
>
<div class="o_setting_left_pane">
</div>
<div class="o_setting_right_pane">
<label for="default_int_ref_template_id" />
<field name="default_int_ref_template_id" />
</div>
</div>
</xpath>
</field>
</record>
</odoo>

0 comments on commit 57b7366

Please sign in to comment.