añadido product_secuence y product_code_unique para crear secuencias de productos por categoría

related 6
This commit is contained in:
santiky 2021-08-08 14:53:02 +02:00
parent 46d66821ec
commit 4fe2e3d368
Signed by: snt
GPG key ID: A9FD34930EADBE71
73 changed files with 5643 additions and 0 deletions

View file

@ -0,0 +1,5 @@
from . import res_company
from . import res_config_settings
from . import ir_sequence
from . import product_product
from . import product_category

View file

@ -0,0 +1,16 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# (http://www.eficent.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models
class IrSequence(models.Model):
_inherit = 'ir.sequence'
@api.model
def get_category_sequence_id(self, category=False):
if self.env.user.company_id.use_parent_categories_to_determine_prefix:
while not category.sequence_id and category.parent_id:
category = category.parent_id
return category.sequence_id or self.env.ref('product_sequence.seq_product_auto')

View file

@ -0,0 +1,58 @@
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# (http://www.eficent.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
class ProductCategory(models.Model):
_inherit = 'product.category'
code_prefix = fields.Char(
string="Prefix for Product Internal Reference",
help="Prefix used to generate the internal reference for products "
"created with this category. If blank the "
"default sequence will be used.",
)
sequence_id = fields.Many2one(
comodel_name="ir.sequence", string="Product Sequence",
help="This field contains the information related to the numbering "
"of the journal entries of this journal.",
copy=False, readonly=True,
)
@api.model
def _prepare_ir_sequence(self, prefix):
"""Prepare the vals for creating the sequence
:param prefix: a string with the prefix of the sequence.
:return: a dict with the values.
"""
vals = {
"name": "Product " + prefix,
"code": "product.product - " + prefix,
"padding": 5,
"prefix": prefix,
"company_id": False,
}
return vals
@api.multi
def write(self, vals):
prefix = vals.get("code_prefix")
if prefix:
for rec in self:
if rec.sequence_id:
rec.sudo().sequence_id.prefix = prefix
else:
seq_vals = self._prepare_ir_sequence(prefix)
rec.sequence_id = self.env["ir.sequence"].create(seq_vals)
return super().write(vals)
@api.model
def create(self, vals):
prefix = vals.get("code_prefix")
if prefix:
seq_vals = self._prepare_ir_sequence(prefix)
sequence = self.env["ir.sequence"].create(seq_vals)
vals["sequence_id"] = sequence.id
return super().create(vals)

View file

@ -0,0 +1,66 @@
# Copyright 2004 Tiny SPRL
# Copyright 2016 Sodexis
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# (http://www.eficent.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
class ProductProduct(models.Model):
_inherit = 'product.product'
default_code = fields.Char(
required=True,
default='/',
track_visibility='onchange',
help="Set to '/' and save if you want a new internal reference "
"to be proposed."
)
@api.model
def create(self, vals):
if 'default_code' not in vals or vals['default_code'] == '/':
categ_id = vals.get("categ_id")
template_id = vals.get("product_tmpl_id")
category = self.env['product.category']
if categ_id:
# Created as a product.product
category = category.browse(categ_id)
elif template_id:
# Created from a product.template
template = self.env["product.template"].browse(template_id)
category = template.categ_id
sequence = self.env["ir.sequence"].get_category_sequence_id(category)
vals['default_code'] = sequence.next_by_id()
return super().create(vals)
@api.multi
def write(self, vals):
"""To assign a new internal reference, just write '/' on the field.
Note this is up to the user, if the product category is changed,
she/he will need to write '/' on the internal reference to force the
re-assignment."""
if vals.get('default_code', '') == '/':
product_category_obj = self.env['product.category']
for product in self:
category_id = vals.get('categ_id', product.categ_id.id)
category = product_category_obj.browse(category_id)
sequence = self.env["ir.sequence"].get_category_sequence_id(category)
ref = sequence.next_by_id()
vals['default_code'] = ref
if len(product.product_tmpl_id.product_variant_ids) == 1:
product.product_tmpl_id.write({'default_code': ref})
super(ProductProduct, product).write(vals)
return True
return super().write(vals)
@api.multi
def copy(self, default=None):
if default is None:
default = {}
if self.default_code and 'default_code' not in default:
default.update({
'default_code': self.default_code + _('-copy'),
})
return super().copy(default)

View file

@ -0,0 +1,17 @@
# Copyright 2004 Tiny SPRL
# Copyright 2016 Sodexis
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# (http://www.eficent.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models
class Company(models.Model):
_inherit = 'res.company'
use_parent_categories_to_determine_prefix = fields.Boolean(
string="Use parent categories to determine the prefix",
help="Use parent categories to determine the prefix "
"if the category has no settings for the prefix.",
)

View file

@ -0,0 +1,16 @@
# Copyright 2004 Tiny SPRL
# Copyright 2016 Sodexis
# Copyright 2018 Eficent Business and IT Consulting Services S.L.
# (http://www.eficent.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
use_parent_categories_to_determine_prefix = fields.Boolean(
related="company_id.use_parent_categories_to_determine_prefix",
readonly=False,
)