This commit is contained in:
2026-09-11 07:00:39 +07:00
parent 85c0a9bb63
commit 4456e5b043
28 changed files with 37 additions and 5 deletions

View File

@@ -0,0 +1 @@
from . import models

View File

@@ -0,0 +1,16 @@
{
'name': 'Kos Barokah ERP Extension',
'version': '1.0',
'category': 'Sales',
'summary': 'Grup akses, Custom Sales/DO Sequence',
'depends': ['sale_management', 'sales_team', 'stock', 'purchase', 'crm'],
'data': [
'security/res_groups.xml',
'security/ir.model.access.csv',
'data/ir_sequence.xml',
'data/stock_picking_type.xml',
'views/product_views.xml',
],
'installable': True,
'application': False,
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<!-- Quotation Sequence: SQ/2026/00001 -->
<record id="seq_quotation" model="ir.sequence">
<field name="name">Quotation Kos Barokah</field>
<field name="code">sale.quotation.custom</field>
<field name="padding">5</field>
<field name="prefix">SQ/%(year)s/</field>
<field name="company_id" eval="False"/>
</record>
<!-- Sales Order Sequence: SO/2026/00001 -->
<record id="seq_sales_order" model="ir.sequence">
<field name="name">Sales Order Kos Barokah</field>
<field name="code">sale.order.custom</field>
<field name="padding">5</field>
<field name="prefix">SO/%(year)s/</field>
<field name="company_id" eval="False"/>
</record>
<!-- Delivery Order Sequence: DO/2026/00001 -->
<record id="seq_delivery_order" model="ir.sequence">
<field name="name">Delivery Order Kos Barokah</field>
<field name="code">stock.picking.do</field>
<field name="padding">5</field>
<field name="prefix">DO/%(year)s/</field>
<field name="company_id" eval="False"/>
</record>
</data>
</odoo>

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<!-- Delivery Orders memakai sequence DO/2026/00001 -->
<record id="stock.picking_type_out" model="stock.picking.type">
<field name="sequence_id" ref="kosbar_erp_ext.seq_delivery_order"/>
</record>
</data>
</odoo>

View File

@@ -0,0 +1,5 @@
from . import sale_order
from . import stock_picking
from . import product_category
from . import product_template
from . import product_product

View File

@@ -0,0 +1,19 @@
from odoo import models, fields, api
class ProductCategory(models.Model):
_inherit = 'product.category'
code = fields.Char(
string='Category Code',
size=10,
help='Kode 2-digit otomatis dari ID kategori (contoh ID 1 -> "01")',
)
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
for r in records:
if not r.code:
r.code = f'{r.id:02d}'
return records

View File

@@ -0,0 +1,13 @@
from odoo import models, fields, api
class ProductProduct(models.Model):
_inherit = 'product.product'
item_code = fields.Char(
string='Item Code',
related='product_tmpl_id.item_code',
store=True,
readonly=True,
help='Item Code dari template produk (variant menampilkan kode template).',
)

View File

@@ -0,0 +1,74 @@
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',
store=True,
readonly=False, # biar view bisa pakai; kita atur readonly di XML
help='Kombinasi: parent_kode + kategori_kode + id(+4 digit) + nama_tanpa_spasi(lower). '
'Read-only, dipakai sbg stock keeping unit code.',
)
@api.onchange('categ_id')
def _onchange_categ_preview(self):
"""Saat create belum simpan id -> tampilkan pratinjau gabungan kode kategori."""
if self.categ_id and not self.env.context.get('default_product_tmpl_id') \
and not self.id and not self.item_code:
cat = self.categ_id
if cat and not cat.code:
cat.code = f'{cat.id:02d}'
parent = cat.parent_id.code if cat.parent_id else ''
own = cat.code or ''
self.item_code = f'{parent}{own}'
@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 _category_prefix(self, cat):
"""Return gabungan kode kategori (parent+own), 4 digit atau 00."""
if not cat:
return '00'
if not cat.code:
cat.code = f'{cat.id:02d}'
parent_code = cat.parent_id.code if cat.parent_id else ''
own_code = cat.code or ''
return f'{parent_code}{own_code}'
def _name_slug(self, name):
slug = (name or '').lower().replace(',', '.')
return re.sub(r'\s+', '', slug)
def _full_item_code(self, obj):
"""Compute full item_code: prefix kategori + id:04d + name_slug."""
prefix = self._category_prefix(obj.categ_id)
oid = obj.id or 0
return f'{prefix}{oid:04d}{self._name_slug(obj.name)}'
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
for r in records:
r.item_code = r._full_item_code(r)
return records
def write(self, vals):
res = super().write(vals)
if 'name' in vals or 'categ_id' in vals:
for r in self:
r.item_code = r._full_item_code(r)
return res

View File

@@ -0,0 +1,32 @@
from odoo import models, fields, api
class SaleOrder(models.Model):
_inherit = 'sale.order'
def _get_quotation_sequence_id(self):
return self.env.ref('kosbar_erp_ext.seq_quotation').sudo()
def _get_sales_order_sequence_id(self):
return self.env.ref('kosbar_erp_ext.seq_sales_order').sudo()
def _get_delivery_sequence_id(self):
return self.env.ref('kosbar_erp_ext.seq_delivery_order').sudo()
def action_confirm(self):
"""Override confirmation to change number from SQ to SO."""
for order in self:
# Quotation (draft) should have SQ prefix; confirm -> SO
seq = self._get_sales_order_sequence_id()
order.name = seq.with_date(order.date_order or fields.Date.context_today(self)).next_by_code()
return super().action_confirm()
@api.model_create_multi
def create(self, vals_list):
"""When creating quotations (draft), use the SQ sequence."""
for vals in vals_list:
if vals.get('state') == 'draft' and not vals.get('name'):
seq = self._get_quotation_sequence_id()
vals['name'] = seq.with_date(
vals.get('date_order') or fields.Date.context_today(self)
).next_by_code()
return super().create(vals_list)

View File

@@ -0,0 +1,10 @@
from odoo import models, api, fields
class StockPicking(models.Model):
_inherit = 'stock.picking'
@api.model
def create(self, vals):
"""Delivery order (outgoing) memakai sequence DO/2026/00001 dari
picking type sequence_id yang sudah diarahkan ke seq_delivery_order."""
return super().create(vals)

View File

@@ -0,0 +1,118 @@
# Custom Product Code & Item Code Automation
## Overview
Odoo 17/19 Community tidak punya field `code` standar untuk product category. Untuk kebutuhan stock keeping unit (SKU) yang unik, kita buat field `item_code` yang di‑generate otomatis dari:
```
item_code = parent_category_code (2 digit) + category_code (2 digit) + product_id (4 digit) + name_slug (lowercase, no space)
```
Contoh:
- Kategori anak: id=06, parent=01 → prefix = `0106`
- Produk: id=3, nama="Backup BRT 30 x 36 x 1,25 mm"
- Hasil: `01060003backupbrt30x36x1,25mm`
## Implementation Steps
### 1. Extend `product.category` → field `code`
```python
class ProductCategory(models.Model):
_inherit = 'product.category'
code = fields.Char(string='Category Code', compute='_compute_code', store=True, size=4)
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
for r in records:
r.code = f'{r.id:02d}'
return records
```
### 2. Extend `product.template` → field `item_code`
Field `item_code`:
- **Stored** (`store=True`) — dipakai untuk pencarian & unique index.
- **Read‑only di view** — tidak boleh ditulis manual.
- **On‑change preview** → tampilkan prefix kategori saat buat produk baru.
```python
class ProductTemplate(models.Model):
_inherit = 'product.template'
item_code = fields.Char(string='Item Code', store=True, readonly=False)
_sql_constraints = [
('item_code_unique', 'UNIQUE(item_code)', 'Item Code harus unik.'),
]
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
for r in records:
r.item_code = r._full_item_code(r)
return records
def write(self, vals):
res = super().write(vals)
if 'name' in vals or 'categ_id' in vals:
for r in self:
r.item_code = r._full_item_code(r)
return res
```
### 3. Extend `product.product` (varian)
Untuk produk ber‑varian, `item_code` diturunkan via related field ke template:
```python
class ProductProduct(models.Model):
_inherit = 'product.product'
item_code = fields.Char(
string='Item Code',
related='product_tmpl_id.item_code',
store=True,
readonly=True,
)
```
### 4. XML View — field read‑only
```xml
<!-- product_template_form_view inheritance -->
<field name="default_code" position="after">
<field name="item_code" readonly="1" placeholder="Terisi otomatis"/>
</field>
```
### 5. PostgreSQL — Unique Index (optional, backup)
Constraint `__sql_constraints` sudah menambah unique index via Odoo ORM. Untuk kecepatan lebih cepat di PostgreSQL level, dapat manual:
```sql
CREATE UNIQUE INDEX IF NOT EXISTS product_template_item_code_uniq
ON product_template (item_code) WHERE item_code IS NOT NULL;
```
### 6. Migration — isi data existing
```python
env.cr.execute("""
CREATE UNIQUE INDEX IF NOT EXISTS product_template_item_code_uniq
ON product_template (item_code) WHERE item_code IS NOT NULL
""")
cats = env['product.category'].search([])
for c in cats:
if not c.code:
c.code = f'{c.id:02d}'
prods = env['product.template'].search([])
for p in prods:
p.item_code = p._full_item_code(p)
```
## Pitfalls
- `@api.depends('id')` **not allowed** di Odoo 19. Namun `id` tersedia di `create()` setelah super() selesai.
- Field `code` di kategori **harus `size` ditentukan** ( Odoo 17+ lint error tanpa `size`)
- Product dengan `categ_id = False` → gunakan fallback `00`.
- **Item Code read‑only di view**: bila pakai `store=True`, field tetap dapat ditulis via ORM (API). Untuk mencegah manual edit, gunakan `readonly=True` + `@api.constrains` untuk validasi.
## References
- `models/product_template.py` — contoh implementasi lengkap (sudah ada di addon `kosbar_erp_ext`)
- `views/product_views.xml` — XML view inheritance

View File

@@ -0,0 +1 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
<!-- Group Sales Manager -->
<record id="group_sales_manager" model="res.groups">
<field name="name">Sales Manager</field>
<field name="implied_ids" eval="[(4, ref('sales_team.group_sale_salesman_all_leads'))]"/>
</record>
<!-- Group CFO -->
<record id="group_cfo" model="res.groups">
<field name="name">CFO / Finance Director</field>
<field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
</record>
</data>
</odoo>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Product Template form: Item Code read_only -->
<record id="view_product_template_item_code" model="ir.ui.view">
<field name="name">product.template.form.item.code</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='categ_id']" position="after">
<field name="item_code" readonly="1"
placeholder="Terisi otomatis setelah simpan"/>
</xpath>
</field>
</record>
<!-- Product Variant/Master form: Item Code read_only -->
<record id="view_product_product_item_code" model="ir.ui.view">
<field name="name">product.product.form.item.code</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='categ_id']" position="after">
<field name="item_code" readonly="1"/>
</xpath>
</field>
</record>
</odoo>