ERP: field code kategori & item_code produk (parent+child+id+name), migrasi existing data, update .gitignore untuk addons
This commit is contained in:
56
addons/kosbar_erp_ext/models/product_template.py
Normal file
56
addons/kosbar_erp_ext/models/product_template.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import re
|
||||
|
||||
from odoo import models, fields, api
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class ProductTemplate(models.Model):
|
||||
_inherit = 'product.template'
|
||||
|
||||
item_code = fields.Char(
|
||||
string='Item Code',
|
||||
index=True,
|
||||
help='Kombinasi: parent_kode + kategori_kode + id_produk(4digit) + nama_produk_tanpa_spasi(lower)',
|
||||
)
|
||||
|
||||
@api.constrains('item_code')
|
||||
def _check_item_code_unique(self):
|
||||
for rec in self:
|
||||
if not rec.item_code:
|
||||
continue
|
||||
dup = self.search([('item_code', '=', rec.item_code), ('id', '!=', rec.id)], limit=1)
|
||||
if dup:
|
||||
raise ValidationError(
|
||||
f'Item Code "{rec.item_code}" sudah dipakai oleh produk lain '
|
||||
f'({dup.name}). Item Code harus unik.'
|
||||
)
|
||||
|
||||
def _generate_item_code(self):
|
||||
"""Generate item_code: parent_code + cat_code + id:04d + name_slug"""
|
||||
for p in self:
|
||||
cat = p.categ_id
|
||||
parent_code = ''
|
||||
cat_code = '00'
|
||||
|
||||
if cat:
|
||||
# Pastikan kategori punya code (calculate jika belum ada)
|
||||
if not cat.code:
|
||||
cat.code = f'{cat.id:02d}'
|
||||
parent_code = cat.parent_id.code if cat.parent_id else ''
|
||||
cat_code = cat.code
|
||||
|
||||
# slug: lowercase, remove semua spasi
|
||||
name_slug = re.sub(r'\s+', '', (p.name or '').lower())
|
||||
p.item_code = f'{parent_code}{cat_code}{p.id:04d}{name_slug}'
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
records = super().create(vals_list)
|
||||
records._generate_item_code()
|
||||
return records
|
||||
|
||||
def write(self, vals):
|
||||
res = super().write(vals)
|
||||
if 'name' in vals or 'categ_id' in vals:
|
||||
self._generate_item_code()
|
||||
return res
|
||||
Reference in New Issue
Block a user