Assalamualaikum warahmatullahi wabarakatuh.
Bismillah.. pada kesempatan kali ini saya akan berbagi sedikit pengalaman mengenai merubah/mengedit sequence yang biasanya menggunakan angka latin dengan angka romawi, model yang saya gunakan disini sebagai contoh adalah model SO. Berikut langkah langkahnya:
>> Inherit Model Ir.Sequence
from datetime import datetime, timedelta
import logging
import pytz
from odoo import api, fields, models, _
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
from odoo import tools
from odoo.tools import image_get_resized_images,
image_resize_image_big
image_resize_image_big
from collections import OrderedDict
class ir_sequence(models.Model):
_inherit = 'ir.sequence'
def write_roman(self,num):
roman = OrderedDict()
roman[1000] = "M"
roman[900] = "CM"
roman[500] = "D"
roman[400] = "CD"
roman[100] = "C"
roman[90] = "XC"
roman[50] = "L"
roman[40] = "XL"
roman[10] = "X"
roman[9] = "IX"
roman[5] = "V"
roman[4] = "IV"
roman[1] = "I"
def roman_num(num):
for r in roman.keys():
x, y = divmod(num, r)
yield roman[r] * x
num -= (r * x)
if num > 0:
roman_num(num)
else:
break
return "".join([a for a in roman_num(num)])
def get_next_char(self, number_next):
def _interpolate(s, d):
return (s % d) if s else ''
def _interpolation_dict():
now = range_date = effective_date =
datetime.now(pytz.timezone(self._context.get('tz') or 'UTC'))
datetime.now(pytz.timezone(self._context.get('tz') or 'UTC'))
if self._context.get('ir_sequence_date'):
effective_date =
datetime.strptime(self._context.get('ir_sequence_date'),
'%Y-%m-%d')
datetime.strptime(self._context.get('ir_sequence_date'),
'%Y-%m-%d')
if self._context.get('ir_sequence_date_range'):
range_date =
datetime.strptime(self._context.get('ir_sequence_date_range'),
'%Y-%m-%d')
datetime.strptime(self._context.get('ir_sequence_date_range'),
'%Y-%m-%d')
sequences = {
'year': '%Y', 'month': '%m', 'day': '%d',
'y': '%y', 'doy': '%j', 'woy': '%W',
'y': '%y', 'doy': '%j', 'woy': '%W',
'weekday': '%w', 'h24': '%H', 'h12': '%I',
'min': '%M', 'sec': '%S',
'min': '%M', 'sec': '%S',
#Additional dict for roman
'rom_year': '%Y', 'rom_month': '%m',
'rom_y': '%y', 'rom_day': '%d'
'rom_y': '%y', 'rom_day': '%d'
}
res = {}
for key, format in sequences.iteritems():
if "rom_" in key:
#Convert to Roman
res[key] = self.write_roman(int(
effective_date.strftime(format)))
effective_date.strftime(format)))
res['range_' + key] = self.write_roman(
int(range_date.strftime(format)))
int(range_date.strftime(format)))
res['current_' + key] = self.write_roman(int
(now.strftime(format)))
(now.strftime(format)))
else:
res[key] = effective_date.strftime(format)
res['range_' + key] =
range_date.strftime(format)
range_date.strftime(format)
res['current_' + key] = now.strftime(format)
return res
d = _interpolation_dict()
try:
interpolated_prefix = _interpolate(self.prefix, d)
interpolated_suffix = _interpolate(self.suffix, d)
except ValueError:
raise UserError(_('Invalid prefix or suffix for
sequence \'%s\'') % (self.get('name')))
sequence \'%s\'') % (self.get('name')))
return interpolated_prefix + '%%0%sd' % self.padding %
number_next + interpolated_suffix
number_next + interpolated_suffix
Tambah file xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="sequence_view_inherit" model="ir.ui.view">
<field name="name">ir.sequence.inherit</field>
<field name="model">ir.sequence</field>
<field name="inherit_id" ref="base.sequence_view" />
<field name="arch" type="xml">
<xpath expr="//form//page//group[3]" position="after">
<group>
<label colspan="2" string="Current Year
with Century (Roman): %%(rom_year)s"/>
with Century (Roman): %%(rom_year)s"/>
<label colspan="2" string="Current Year
without Century (Roman): %%(rom_y)s"/>
without Century (Roman): %%(rom_y)s"/>
<label colspan="2" string="Month (Roman):
%%(rom_month)s"/>
%%(rom_month)s"/>
<label colspan="2" string="Day (Roman):
%%(rom_day)s"/>
%%(rom_day)s"/>
</group>
</xpath>
</field>
</record>
</odoo>