Перейти до основного вмісту

Обробка штрихкоду елементу виробу

ШК містить у собі ID елементу та порядковий виробу.

Формування штрихкоду

function IDToBarCode(ID:string; Q:integer; D10 : boolean = false) : string;
Var ID, CID, BID, QQ : string;
Q : integer;
D10 : boolean;
begin

if pos('_', ID) = 0 then
result := ID else
begin

CID := copy(ID, 1, pos('_', ID) - 1);
BID := copy(ID, pos('_', ID) + 1);
QQ := IntTo26(Q, 1);

result := 'E' + CID + STRFormB(BID, 3, '0') + StrFormB(QQ, 3 , '0');

if (length(BID) = 1) and (length(QQ) = 1) then result := 'A' + CID + BID + QQ
else
if (length(BID) = 2) and (length(QQ) = 1) then result := 'B' + CID + BID + QQ
else
if (length(BID) = 1) and (length(QQ) = 2) then result := 'C' + CID + BID + QQ
else
if (length(BID) = 2) and (length(QQ) = 2) then result := 'D' + CID + BID + QQ;

if D10 then
result := '4' + IntToStr( s26ToInt(CID) ) + FormatFloat('00', s26ToInt(BID) ) + FormatFloat('00', s26ToInt(QQ) );

end;

end;

Const
nos: array [0 .. 35] of char = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

Function s26toInt(s: string): int64;
Var
SL: TStringList;
i, k : integer;
begin

try
SL := TStringList.Create;
result := 0;
for i := 0 to 35 do
SL.add(nos[i]);

for i := 1 to length(s) do
begin
k := SL.IndexOf(s[i]);
if k = -1 then
raise Exception.Create('Numver conversion error: ' + s)
else
result := result * 36 + SL.IndexOf(s[i]);
end;
finally
SL.Free;
end;

end;

Function IntTo26(n: int64; d: integer): string;
Var
b: int64;
a : integer;
begin

result := '';

while n > 0 do
begin
b := n mod 36;
n := n div 36;
result := nos[b] + result;
end;

for a := length(result) + 1 to d do
result := '0' + result;

end;


Приклад використання:

BarCode := IdToBarCode('31_1', 1);

Декодування штрихкоду

procedure DecodeBarCode(BarCode : string; Var CID, BID, Q : string; QB : integer = 0; QQ : integer = 0);
begin

if (QB > 0) or (QQ > 0) then
begin
CID := copy(BarCode, 2, Length(BarCode) - 1 - QB - QQ);
while Copy(CID, 1, 1) = '0' do CID := copy(CID, 2);
BID := copy(BarCode, Length(BarCode) - QB - QQ + 1, QB);
Q := copy(BarCode, Length(BarCode) - QQ + 1, QQ);
end
else
if copy(BarCode, 1, 1) = 'A' then DecodeBarCode( barCode, CID, BID, Q, 1, 1)
else
if copy(BarCode, 1, 1) = 'B' then DecodeBarCode( barCode, CID, BID, Q, 2, 1)
else
if copy(BarCode, 1, 1) = 'C' then DecodeBarCode( barCode, CID, BID, Q, 1, 2)
else
if copy(BarCode, 1, 1) = 'D' then DecodeBarCode( barCode, CID, BID, Q, 2, 2)
else
if copy(BarCode, 1, 1) = 'E' then DecodeBarCode( barCode, CID, BID, Q, 3, 3)
else
if copy(BarCode, 1, 1) = '4' then
begin
DecodeBarCode( BarCode, CID, BID, Q, 2, 2);
CID := IntTo26( StrToInt(CID), 1);
BID := IntTo26( StrToInt(BID), 1);
Q := IntTo26( StrToInt(Q), 1);
end;

end;

Приклад використання:

BarCode := IdToBarCode('31_1', 1);
DecodeBarCode(BarCode, IDConstr, IDBar, Q); // IDConstr = constructions.id
Barcode := IdToBarCode(IDConstr + '_' + IDBar, 1);