Random Number SKU Generator

1,331 views
Skip to first unread message

peterd

unread,
Oct 1, 2013, 9:40:03 AM10/1/13
to idem...@googlegroups.com
Hi,
I would like to have as a SKU random generated 6 digit number or 7 digit with the last digit as a checksum.
Is Idempiere got incorporated Random Number Generator?

Thanks
Peter D

Carlos Antonio Ruiz Gomez

unread,
Oct 2, 2013, 4:37:50 PM10/2/13
to idem...@googlegroups.com
No, but I think it must be something simple to add - even on database side.

Regards,

Carlos Ruiz

peterd

unread,
Oct 3, 2013, 6:28:11 AM10/3/13
to idem...@googlegroups.com
Hi,
Thanks Carlos for info.
In the end I decided to have 5 digit part reference preferably random unique number.
Some of products have EAN-13 barcodes so five digits internal SKU probably is right,
and could form basis if we want to expand to GS1 system in the future.

Thanks
Petr



peterd

unread,
Oct 3, 2013, 9:21:50 AM10/3/13
to idem...@googlegroups.com
Hi,

I learned that it could be done with the RANDOM() Function


postgres=# SELECT ROUND(RANDOM() * 100000);
 round
-------
 79544
(1 row)


next i would have to learn how to implement it on a table and I think that should be some check for uniqueness.

Best Regards
Peter

Carlos Antonio Ruiz Gomez

unread,
Oct 3, 2013, 4:05:33 PM10/3/13
to idem...@googlegroups.com
Very interesting, thanks for the feedback.

For the " last digit as a checksum" part I found this implementation:
http://wiki.postgresql.org/wiki/Luhn_algorithm

With those functions in postgresql you can define the default for the SKU column:

For example:
- to generate SKU on saving
ALTER TABLE m_product ALTER sku SET DEFAULT ltrim(to_char(luhn_generate(round(random()*10000)::int), '00000'))

- to generate SKY as default on product window, define the default for column SKU as:
@SQL=select ltrim(to_char(luhn_generate(round(random()*10000)::int), '00000'))


Regards,

Carlos Ruiz
--

peterd

unread,
Oct 6, 2013, 12:11:48 AM10/6/13
to idem...@googlegroups.com
Carlos,
Thank you for a code it works perfectly.

Best Regards
PeterD

peterd

unread,
Jul 26, 2014, 9:38:22 PM7/26/14
to idem...@googlegroups.com
Hi,
I implemented this against my database and got SKU numbers generated as product has been added.
But I just funding that SKU numbers are not unique as I coming across of products with the same SKU number.
How to make sure that generated numbers are unique?

Best Regards
Peter

Deepak Pansheriya (Logilite.com)

unread,
Aug 1, 2014, 2:53:55 AM8/1/14
to idem...@googlegroups.com
Peter,

You need to write ModelValidator for Product window to check uniqueness of SKU and if not regenerate number.
If you have expertise on PLSQL, you can write stored procedure and do defaulting as Carlos suggested at above.

peterd

unread,
Jan 11, 2015, 3:26:44 AM1/11/15
to idem...@googlegroups.com
Thanks Deepak,

It seems I could not find any  samples how to implement solutions mentioned.
At the moment I got this code applied on m_product table as suggested by Carlos and 5 digit SKU number is created on product window save, but from time to time is duplicated, so numbers are not unique.


ALTER TABLE m_product ALTER sku SET DEFAULT ltrim(to_char(luhn_generate(round(random()*10000)::int), '00000'))

Best Regards
Peter

peterd

unread,
Feb 6, 2015, 7:23:55 PM2/6/15
to idem...@googlegroups.com
I do not know if this is right place to ask.
If samebody with database knowledge could implement for me a code that SKU numbers generated could be unique.

Best Regards
Peter

Deepak Pansheriya (Logilite.com)

unread,
Feb 9, 2015, 12:17:48 AM2/9/15
to idem...@googlegroups.com
Peter,

Default logic can't help you to validate uniqueness, Either need to write stored procedure for same or need to add validator.

Jan Thielemann

unread,
Feb 11, 2015, 3:55:06 AM2/11/15
to idem...@googlegroups.com
I think you should write a function to encapsulate "ltrim(to_char(luhn_generate(
round(random()*10000)::int), '00000'))"  and at the same time, check if the value is already used in the table.

in pseudo code it would look something like this:
generateMProductSKU(){
    skuGen
=""
    needToGenerate = true
    while(needToGenerate){
      skuGen = ltrim(to_char(luhn_generate(round(random()*10000)::int), '00000'))
     
if((Select count(*) from M_Product where sku =skuGen) = 0
         needToGenerate
=  false
   }
   
return skuGen
}


peterd

unread,
Feb 12, 2015, 6:56:21 AM2/12/15
to idem...@googlegroups.com
Is this code has to replace sku "default value" or function created?
Message has been deleted

peterd

unread,
Feb 15, 2015, 12:23:38 AM2/15/15
to idem...@googlegroups.com
Hi
I would like to thank you all who contributed to this thread in particular Carlos Gomez, Ian Thielemann
and Roxane  Reid-Bennett from postgresql forums whose wrapped up sql code for Random Unique SKU Generator.

create or replace function generateMProductSKU()
RETURNS text AS
$BODY$

DECLARE
    skuGen text
;
    needToGenerate
boolean;
BEGIN
    skuGen
:= '';
    needToGenerate
:= true;

    WHILE needToGenerate LOOP
        skuGen
:= to_char(luhn_generate(round(random()*10000)::int), '00000');
        SELECT count
(*) != 0 into needToGenerate from m_product where sku = skuGen;
   
END LOOP;

   
return skuGen;
END
$BODY$
  LANGUAGE
'plpgsql' STABLE;

ALTER TABLE m_product ALTER sku SET DEFAULT generateMProductSKU
();


Best Regards
Peter

Reply all
Reply to author
Forward
0 new messages