--LUA ------------------------------------------------------------------------------- -- cm_transform function - called For each row processed -- Brazil NID CNPJ Corporate Random Generation ------------------------------------------------------------------------------- function cm_transform() if (not colinfoshown) then colinfoshown = true print(" LUA version ", _VERSION) print(" processing column " .. source.column.getname()) print(" type: " .. source.column.gettype()) print(" length: " .. source.column.getlength()) end oldvalue = source.column.getvalue() print(" cm_transform oldvalue ", oldvalue) if oldvalue ~= nil then if string.len(oldvalue) == 14 then newvalue = cnpj_random_format(oldvalue) else newvalue = cnpj_random(oldvalue) end end target.column.setvalue(newvalue) end function _str2tbl(str) print(" _str2tbl str", str) local t = {} for i = 1, #str, 1 do t[i] = str:sub(i, i) end return t end function _tbl2str(tbl) print(" _tbl2str") local newstr newstr = "" if #tbl == 1 then newstr = tbl[1] else for ti = 1, #tbl, 1 do newstr = newstr .. tbl[ti] end end return newstr end function _dott (a, b) -- multiple table a by table b, return sum print(" _dott") local sum if #a ~= #b then print("arguments a and b should have the same length") else sum = 0 for ix = 1, #a, 1 do if tonumber(a[ix]) == nil or tonumber(b[ix]) == nil then print("input must be numeric") sum = nil break end sum = sum + (a[ix] * b[ix]) end end return sum end function _unformat_cnpj(cnpj) print(" _unformat_cnpj cnpj ", cnpj) local new_cnpj if string.len(cnpj) ~= 14 then print("formatted cnpj has two dots and a dash nnn.nnn.nnn-nn ") else new_cnpj = string.sub(cnpj,1,3) .. string.sub(cnpj,5,7) .. string.sub(cnpj,9,11) .. string.sub(cnpj,13,14) end return new_cnpj end function _format_cnpj(cnpj) print(" _format_cnpj cnpj ", cnpj) local new_cnpj if string.len(cnpj) ~= 11 then print("unformatted cnpj has 11 digits ") else new_cnpj = string.sub(cnpj,1,3) .. "." .. string.sub(cnpj,4,6) .. "." .. string.sub(cnpj,7,9) .. "-" .. string.sub(cnpj,10,11) end return new_cnpj end function cnpj_checksum_validate(cnpj) print(" cnpj_checksum_validate cnpj ", cnpj) local lcnpj = cnpj if string.len(cnpj) == 14 then lcnpj = _unformat_cnpj(cnpj) end local lcnpj_t = _str2tbl(lcnpj) local lt1 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 0, 0} local lt2 = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 0} local ls1 = math.floor(assert(_dott(lcnpj_t, lt1),"table multiply failed check digit 1") % 11) if ls1 < 2 then lcnpj_t[10] = 0 else lcnpj_t[10] = 11 - ls1 end local ls2 = math.floor(assert(_dott(lcnpj_t, lt2),"table multiply failed check digit 2") % 11) if ls2 < 2 then lcnpj_t[11] = 0 else lcnpj_t[11] = 11 - ls2 end local gen_lcnpj = _tbl2str(lcnpj_t) -- print("val ", gen_lcnpj, lcnpj) if lcnpj == gen_lcnpj then return gen_lcnpj else return nil end end function cnpj_checksum_generate(cnpj) print(" cnpj_checksum_generate cnpj ", cnpj) local lcnpj = cnpj if string.len(cnpj) == 14 then lcnpj = _unformat_cnpj(cnpj) end local cnpj9 = string.sub(lcnpj,1,9) local lcnpj_t = _str2tbl(cnpj9 .. '00') local lt1 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 0, 0} local lt2 = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 0} local ls1 = math.floor(assert(_dott(lcnpj_t, lt1),"table multiply failed check digit 1") % 11) if ls1 < 2 then lcnpj_t[10] = 0 else lcnpj_t[10] = 11 - ls1 end local ls2 = math.floor(assert(_dott(lcnpj_t, lt2),"table multiply failed check digit 2") % 11) if ls2 < 2 then lcnpj_t[11] = 0 else lcnpj_t[11] = 11 - ls2 end local gen_lcnpj = _tbl2str(lcnpj_t) return gen_lcnpj end function cnpj_make_invalid (in_cnpj) print(" cnpj_make_invalid in_cnpj ", in_cnpj) local lcnpj_t = _str2tbl(in_cnpj) local t = lcnpj_t[10] lcnpj_t[10] = lcnpj_t[11] lcnpj_t[11] = t return _tbl2str(lcnpj_t) end function cnpj_random (in_cnpj, ...) print(" cnpj_random in_cnpj ", in_cnpj) local arg={...} local reseed = -1 if #arg == 1 then reseed = arg[1] end local in_cnpj_val = cnpj_checksum_validate(in_cnpj) local start_cnpj = '10000000000' local end_cnpj = '99999999999' if in_cnpj_val ~= nil then end_cnpj = in_cnpj if string.len(in_cnpj) == 14 then end_cnpj = _unformat_cnpj(in_cnpj) end end if reseed ~= nil and reseed >= 0 then math.randomseed (reseed) end local new_cnpj = tostring(math.random(start_cnpj, end_cnpj)) print("inside random, before generate, start, end, new ", start_cnpj, end_cnpj, new_cnpj) -- inValtemp = optimmask(inVal,'PRO=AFF,MTD=REP,COPY=(1,2)(9,2),FLDDEF1=(NAME="inVal",DATATYPE=WVARCHAR_SZ,LEN=10)') --workaround while random isn't functioning as expected -- local temp_cnpj = in_cnpj -- if string.len(in_cnpj) == 14 then -- temp_cnpj = _unformat_cnpj(in_cnpj) -- end -- local ncnpj_t = _str2tbl(temp_cnpj) -- local t = ncnpj_t[3] -- ncnpj_t[3] = ncnpj_t[4] -- ncnpj_t[4] = t -- local new_cnpj = _tbl2str(ncnpj_t) -- end workaround local new_cnpj_val = cnpj_checksum_generate(new_cnpj) if in_cnpj_val == nil then if new_cnpj_val ~= nil then new_cnpj = cnpj_make_invalid(new_cnpj) new_cnpj_val = cnpj_checksum_validate(new_cnpj) end end return new_cnpj end function cnpj_random_format (in_cnpj) print(" cnpj_random_format in_cnpj ", in_cnpj) return _format_cnpj(cnpj_random(in_cnpj)) end cnpji = '571.984.484-88' cnpju = cnpj_random(cnpji,nil) cnpji = '294.262.409-42' cnpju = cnpj_random(cnpji,10) cnpji = '999.444.333-55' cnpju = cnpj_random(cnpji,15) cnpji = '263.946.533-30' cnpju = cnpj_random(cnpji)