InfoSphere Optim

 View Only

Lua Script CM Procedure - Rijkregisternummer

  • 1.  Lua Script CM Procedure - Rijkregisternummer

    Posted Wed November 01, 2023 08:34 AM

    Dear Colleagues of IBM community,

    I have an LUA script that generates the Belgium security number based on BirthDate and Gender. However, after the new upgrade to Optim v11.07.01, the generation of the numbers is not correct, for instance, a person that was born in 1994, should have the first two characters of the Security Number like 94**, but it is not. It is generating like 18.

    Maybe a new pair of eyes on the code could help me.

     -------------------------------------------------------------------------------
    --  cm_load function - Called before any tables are processed
    -------------------------------------------------------------------------------
    function cm_load()

    --- dagen per maand - geen rekening gehouden met schrikkeljaar
      dagenmaand ={}
      dagenmaand[1] = 31
      dagenmaand[2] = 28
      dagenmaand[3] = 31
      dagenmaand[4] = 30
      dagenmaand[5] = 31
      dagenmaand[6] = 30
      dagenmaand[7] = 31
      dagenmaand[8] = 31
      dagenmaand[9] = 30
      dagenmaand[10] = 31
      dagenmaand[11] = 30
      dagenmaand[12] = 31

      math.randomseed(os.time())
      t=os.date("*t")
      print("Systeem Datum : ",t.month,t.day,t.year)
      currentee = tonumber(string.sub(t.year, 1, 2))
      currentjj = tonumber(string.sub(t.year, 3, 4))
    end

    -------------------------------------------------------------------------------
    --  cm_transform function - Called for each row processed
    -------------------------------------------------------------------------------
    function cm_transform()
     --  oldvalue = optim.source.getcolumnvalue()
      situatie  = 'X'
      isprivate = optim.source.getcolumnvalue("ISPRIVATE")
      rrnr      = optim.source.getcolumnvalue()
      newvalue  = rrnr
      
     --  print("isprivate  : ", isprivate)
     --  print("rrnr       : ", rrnr)
      
     --25  Bepalen welke situaties kunnen optreden.
     --  Situatie A ; Geen Private, dus organisation.  pos 1-3 niet wijzigen, pos 4-8 randomiseren, pos 9-10 controlegetal
     --  Situatie B ; Wel Private, geen geldig nummer. pos 1-3 niet wijzigen, rest nummer scrambelen, wel zelfde formaat houden.
     --  Situatie C ; Wel Private, zou geldig nummer kunnen zijn. Nieuw nummer smenstellen. 
     --  Situatie D ; NationalRegistrationNumber is space of null. Inhoud van het nummer zo laten.

     if rrnr == nil then
       situatie = 'D'
     else  
      if (string.sub(rrnr, 1, 20) < '00000000000000000001') then
       situatie = 'D'
      end
     end


     if (isprivate ~= 'T' and situatie ~= 'D') then
       situatie = 'A'
     end
     if (isprivate == 'T' and situatie ~= 'D') then
       pos12 = (string.sub(rrnr, 12, 12))
       pos11 = (string.sub(rrnr, 11, 11))

       if (pos12:match("%w")) then
        situatie = 'B'
       else
         if(tonumber(pos11) ~= nil) then
          situatie = 'C'
         else
          situatie = 'B'
         end
       end
     end 

    --  print ("situatie : ", situatie)
      
      if (situatie == 'A') then
       org13 = (string.sub(rrnr, 1, 3))
       org48 = (string.sub(rrnr, 4, 8))    
       seedvaluenum = tonumber(rrnr)
       math.randomseed(seedvaluenum)
       org48rand=math.random(10000,99999)
       orgrand18=(org13..org48rand)
       orgrand910=(97 - (orgrand18 % 97))
       orgrand=(orgrand18..orgrand910)
       newvalue=orgrand
      end
      
      if (situatie == 'B') then
       
       local totnr = (string.sub(rrnr, 1, 2))
       for i = 3,18 do
        pos = ""
        pos = (string.sub(rrnr, i, i))
         if (tonumber(pos) ~= nil)then
          totnr = totnr..math.random(0,9)
         else
          if (pos == ".") then
           totnr = totnr..pos
          else 
           if (pos == "@") then
            totnr = totnr..pos
           else
            if (pos:match("%w")) then
             totnr = totnr..string.char(math.random(97,122))
            end
           end
          end
         end
       end
       
    --   print("totnr : ", totnr)
       newvalue = totnr   

      end
      
      if (situatie == 'C') then
    --Bepaal huidige geboortedatum
       birthjj = string.sub(rrnr, 1, 2)
       birthjjnum = tonumber(birthjj)
       if birthjjnum > currentjj then
        birthee = (currentee - 1)
       else
        birthee = currentee
       end
      birthdate = birthee..birthjj..(string.sub(rrnr, 3,6))
    --  print("birthdate : ", birthdate)

    --Bepaal gender
      volgnr = tonumber(string.sub(rrnr, 7, 9))
      if ((volgnr % 2) == 1) then
       gender = "m"
      else 
       gender = "v"
      end

    --Bepaal gedepersonaliseerde geboortedatum
       jaar2 =(string.sub(birthdate, 1, 4))
       maand2=(string.sub(birthdate, 5, 6))
       dag2  =(string.sub(birthdate, 7, 8))

      maandnum = tonumber(maand2)
      dagnum   = tonumber(dag2)
      jaarnum  = tonumber(jaar2)
      maand = tonumber(maand2) 
      
    -- If the person will become 18 years old in the current year, year and month are not changed, day is random
    -- If the person is <18 years old in the current year, year is random in the range (currentyear -17, currentyear -1), month and day are random
    -- If the person is >18 years old in the current year, year is random in the range (currentyear -105, currentyear -19), month and day are random
        seedvalue = (maandnum .. dagnum .. jaarnum)
        seedvaluenum = tonumber(seedvalue)
        math.randomseed(seedvaluenum)
        year2 = tostring(t.year-18)
        if (year2==jaar2) then
         jaar=year2
        else
         if (jaar2 > year2)then     
          jaar=math.random(t.year-17,t.year-1)
         else
          jaar=math.random(t.year-105,t.year-19)
         end
         maand=math.random(1,12)
        end
        dag=math.random(1,dagenmaand[maand])
        if (maand < 10) then
         maand = tostring("0"..maand)
        end
         if (dag < 10) then
         dag = tostring("0"..dag)
        end
        
        newbirthdate = (jaar..maand..dag)
    --    print("newbirthdate : ", newbirthdate)

    --Bepaal gedepersonaliseerd volgnummer
      newvolgnr = math.random(100,996)
      if (newvolgnr%2) == 1 then
       if gender == "v" then
        newvolgnr = newvolgnr + 1
       end
      else
       if gender == "m" then
        newvolgnr = newvolgnr + 1
       end
      end
      
    --Bereken nieuw RRnr
      rrnr19n = tonumber(string.sub(newbirthdate, 3, 8)..newvolgnr)
      rrnr19st = (string.sub(newbirthdate, 3, 8)..newvolgnr)
    --  print ("rrnr19n  : ", rrnr19n) 
    --  print ("rrnr19st : ", rrnr19st) 
      if tonumber(string.sub(newbirthdate, 1, 2)) == 19 then
       rrnr1011=(97 - (rrnr19n % 97))
      else
       rrnr1011=(97 - (tonumber("2"..rrnr19st) % 97))
      end
       if (rrnr1011 < 10) then
        rrnr1011st = tostring("0"..rrnr1011)
       else
        rrnr1011st = tostring(rrnr1011)
       end
       newrrnr = tostring(rrnr19st..rrnr1011st)
      newvalue = newrrnr
      
      end
      
      if (situatie == 'D') then
      newvalue = rrnr
      end
      
    --  print ("newvalue : ", newvalue)
      optim.target.setcolumnvalue(newvalue)
      end



    ------------------------------
    Matheus Rocha
    ------------------------------