Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1998 Lượt xem

Odoo 16.

The purpose as per the subject above:

Category | Nomor
A | A-01
A | A-02
A | A-03
B | B-01
B | B-02
B | B-03
B | B-04
...

So, I need to get the numbering in the above sample. The category came from Many2one selections. The Category is based on user input, so not defined before.


But the result as below:

Category | Nomor
A | A-01
A | A-02
A | A-03
B | B-04
B | B-05
B | B-06
B | B-07
...


The number was continuing from the previous category number.

There is any wrong method or another method?


My Code that I already tried it:



@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
condition = [('kode_kategori_tarif', 'like', vals.get('kode_kategori_tarif'))]
last_record = self.search(condition, order='id desc', limit=1)
for lr in last_record:
last_seq = lr.kode_sub_kategori_tarif
if last_seq:
last_no = int(last_seq.split('-')[-1]) + 1
seq = '{}-{:02d}'.format(lr.kode_kategori_tarif, last_no)
else: seq = '{}-01'.format(lr.kode_kategori_tarif)
vals['kode_sub_kategori_tarif'] = seq
record = super(BaseMD_SubKategoriTarif, self).create(vals_list)
return record

id_kode_kategori_tarif = fields.Many2one('thg_base_hospital.basemd_kategori_tarif', 'Kategori')
kode_kategori_tarif = fields.Char('Kode Tarif', related="id_kode_kategori_tarif.kode_kategori_tarif", store=True)
kode_sub_kategori_tarif = fields.Char('Kode Sub Kategori', readonly=True, help='Code will show after saved')
name = fields.Char('Nama Sub Kategori Tarif', required=True)
jasa_dokter = fields.Boolean('Jasa Dokter', default=False)

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

If its fine to add a sequence record inside the category model, it will be much easier for you. On creating the category record, create a record inside the ir.sequence table and store it inside a field in category model.

Then whenever a record is created in this category, you can get the next sequence number by calling function (next_by_id) to provide next number in ir.sequence.


Suppose if you follow this approach and have a sequence_id field in your category model, then, inside your create function:

sequence_id.next_by_id() will return you next number from this sequence.

Thanks

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 12 21
10381
1
thg 10 24
2170
0
thg 4 22
2613
0
thg 10 17
3486
2
thg 6 24
4527