This might be a bit crude, but I am using the following code to generate various serial numbers for Sales Orders, Customer Numbers, Work Orders, Job Cards etc.
I am doing it on the server side, before posting the record. It works for now. Still need to consider other cases such as when a record is deleted etc. Still working on these.
def on_apply(item, delta, params, connection):
d = delta
d.edit()
so = item.task.so_list.copy()
if (d.rec_inserted()):
so_num = generate_so_num(item)
d.so_number.value = so_num
d.po_status.value = 1
d.so_status.value = 1
d.post()
def generate_so_num(item):
copy =item.copy()
copy.open()
today = datetime.date.today()
year = (today.year - 2000)
if copy.record_count():
copy.last()
last_sonum = copy.so_number.value
last_sonum_year = int(last_sonum[2:4])
if last_sonum_year == year:
slno = int(last_sonum[-3:])
so_num = 'SO' + str(year*1000+slno+1)
else:
so_num = 'SO'+ str(year*1000 + 1)
else:
so_num = 'SO'+ str(year*1000 + 1)
return so_num
This generates a number of the form - SO23001, SO23002 etc.
Client Side:
function on_after_apply(item) {
item.refresh_record();
}