Skip to content

Commit

Permalink
[ADD] product_internal_reference_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasProgrammer committed Oct 26, 2023
1 parent 544d165 commit bab2b14
Show file tree
Hide file tree
Showing 19 changed files with 703 additions and 0 deletions.
Empty file.
1 change: 1 addition & 0 deletions product_internal_reference_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
18 changes: 18 additions & 0 deletions product_internal_reference_generator/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Product Internal Reference Generator",
"summary": """Product template and variant reference based on sequence""",
"author": "Ilyas, Ooops, Odoo Community Association (OCA)",
"license": "LGPL-3",
"website": "https://github.com/OCA/product-variant",
"category": "Sale",
"version": "14.0.1.0.0",
"depends": ["stock"],
"data": [
"security/ir.model.access.csv",
"views/product.xml",
"security/groups.xml",
],
"demo": ["demo/demo.xml"],
"installable": True,
"application": False,
}
34 changes: 34 additions & 0 deletions product_internal_reference_generator/demo/demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version='1.0' encoding='utf-8' ?>
<odoo>

<record model="ir.sequence" id="demo_pcs_seq_1">
<field name="name">Good products sequence</field>
<field name="prefix">GOOD</field>
<field name="padding">4</field>
</record>

<record model="ir.sequence" id="demo_pcs_seq_2">
<field name="name">Bad products sequence</field>
<field name="prefix">BAD</field>
<field name="padding">5</field>
</record>

<record model="product.code.sequence" id="demo_pcs_1">
<field name="name">Good products</field>
<field name="variant_reference_numbers">3</field>
<field
name="sequence_id"
ref="product_internal_reference_generator.demo_pcs_seq_1"
/>
</record>

<record model="product.code.sequence" id="demo_pcs_2">
<field name="name">Bad products</field>
<field name="variant_reference_numbers">4</field>
<field
name="sequence_id"
ref="product_internal_reference_generator.demo_pcs_seq_2"
/>
</record>

</odoo>
3 changes: 3 additions & 0 deletions product_internal_reference_generator/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import product_code_sequence
from . import product_template
from . import product_product
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from odoo import fields, models


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

name = fields.Char(required=True)
sequence_id = fields.Many2one("ir.sequence", required=True)
variant_reference_numbers = fields.Integer("Digits", default=3, required=True)
17 changes: 17 additions & 0 deletions product_internal_reference_generator/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from odoo import api, models


class ProductProduct(models.Model):
_inherit = "product.product"

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get("product_tmpl_id"):
pt = self.env["product.template"].browse(vals["product_tmpl_id"])
if pt.variants_sequence_id:
vals["default_code"] = (
pt.variants_prefix + pt.variants_sequence_id.next_by_id()
)
res = super().create(vals_list)
return res
30 changes: 30 additions & 0 deletions product_internal_reference_generator/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from odoo import api, fields, models


class ProductTemplate(models.Model):
_inherit = "product.template"

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

@api.onchange("int_ref_template_id")
def onchange_int_ref_template_id(self):
self.variants_prefix = False

def btn_generate_sequence(self):
self.ensure_one()
self.variants_prefix = self.int_ref_template_id.sequence_id.get_next_char(0)
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,
}
)
self.variants_sequence_id = var_seq
self.default_code = int_ref_next_val + var_seq.get_next_char(0)
Empty file.
Empty file.
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions product_internal_reference_generator/security/groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?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="category_id" ref="base.module_category_hidden" />
</record>
</odoo>
Original file line number Diff line number Diff line change
@@ -0,0 +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,base.group_system,1,1,1,1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit bab2b14

Please sign in to comment.