unit DiseClave;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, Spin, Grids, Gauges, ComCtrls, Mask, DBCtrls, Db,
  DBTables;

type
  TFormaClaveDiseno = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Detalle: TStringGrid;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Progreso: TGauge;
    LabelTotal: TLabel;
    Claves: TRichEdit;
    BitBtn3: TBitBtn;
    CamposExtra: TGroupBox;
    Label4: TLabel;
    Label14: TLabel;
    Label19: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    TablaInventario: TTable;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    LabelRuta: TLabel;
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
    procedure CheckBox4Click(Sender: TObject);
    procedure CheckBox5Click(Sender: TObject);
    procedure Edit6Click(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure Edit8Click(Sender: TObject);
    procedure Edit9Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Label1DblClick(Sender: TObject);
    procedure LabelTotalClick(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormaClaveDiseno: TFormaClaveDiseno;

implementation

uses DatoForm;

{$R *.DFM}


function CuentaPalabras(linea:string):integer;
var
i,cuenta:integer;
aux:string;
begin
aux:='';
cuenta:=0;
linea:=linea+',';
if length(linea)>1 then
for i:=1 to length(linea) do
begin
if linea[i]=',' then
cuenta:=cuenta+1;

end;

CuentaPalabras:=cuenta;

end;


function PalabraLinea(linea:string;num:integer):string;
var
i,cuenta:integer;
aux:string;
begin
aux:='';
cuenta:=1;
linea:=linea+',';
for i:=1 to length(linea) do
begin
if linea[i]=',' then
begin
if num=cuenta then
begin
PalabraLinea:=aux;
break;
end;
cuenta:=cuenta+1;
aux:='';
end
else
begin
aux:=aux+linea[i];

end;


end;


end;





procedure TFormaClaveDiseno.BitBtn2Click(Sender: TObject);
begin
FormaClaveDiseno.Tag:=0;

Close;
end;

procedure TFormaClaveDiseno.BitBtn1Click(Sender: TObject);
var
arch:TextFile;
dir,aux:string;
i,j,k,l,m,total:integer;
num_palabras:array[1..10]of integer;
C:array[1..4,1..10] of string[8];
cuenta,multiplo,siguiente,ultimo:integer;
Total_cla:integer;
Lista:TStringList;

begin


for i:=1 to 4 do
for j:=1 to 10 do
c[i,j]:='';






aux:='';
Progreso.Progress:=0;
for i:=1 to 10 do
num_palabras[i]:=0;

for i:=1 to 4 do
begin
if Detalle.Cells[1,i]<>'' then
begin
num_palabras[i]:=CuentaPalabras(Detalle.Cells[1,i]);
end;
end;//for






Total_cla:=1;





for i:=1 to 4 do
begin

if Detalle.Cells[1,i]<>'' then
begin
if num_palabras[i]>0 then
Total_cla:=Total_cla*num_palabras[i];
ultimo:=i;
end;
end;//for

Lista:=TStringList.Create;
Lista.Sorted:=False;

cuenta:=1;
multiplo:=1;
siguiente:=1;


for i:=1 to Total_cla do
Lista.Add('');

for k:=4 downto 1 do
begin
if Detalle.Cells[1,k]<>'' then
begin

{if k=1 then
multiplo:=Trunc(Total_cla/num_palabras[k])
else
begin
if (k=ultimo)or(k=2) then
multiplo:=1
else
begin
multiplo:=Trunc(num_palabras[k+1]);
//multiplo:=Trunc(Total_cla/num_palabras[k]);
//multiplo:=1;
end;
end;}


case k of
1:multiplo:=Trunc(Total_cla/num_palabras[k]);
2:multiplo:=Trunc(Total_cla/num_palabras[k]);
3:multiplo:=num_palabras[k+1];
4:multiplo:=1;
end;


if multiplo=0 then
multiplo:=1;


for i:=1 to Total_cla do
begin
Lista.Strings[i-1]:=PalabraLinea(Detalle.Cells[1,k],siguiente)+
Lista.Strings[i-1];

if (i mod multiplo =0) then
siguiente:=siguiente+1;

if siguiente> num_palabras[k] then
siguiente:=1;
end;//i



//if k=1 then

//begin
{if (k mod 2=0)and(ultimo<>k) then
multiplo:=Trunc(Total_cla/num_palabras[k])
else
begin

if k=ultimo then
multiplo:=1
else
begin
multiplo:=Trunc(num_palabras[k+1]);


//multiplo:=Trunc(Total_cla/num_palabras[k]);
end;
end;

if multiplo=0 then
multiplo:=1;


for i:=1 to Total_cla do
begin


Lista.Strings[i-1]:=Lista.Strings[i-1]+
PalabraLinea(Detalle.Cells[1,k],siguiente);

if (i mod multiplo =0) then
siguiente:=siguiente+1;

if siguiente> num_palabras[k] then
siguiente:=1;




end;//i


}
end;


end;//k




GetDir(0,dir);
AssignFile(arch,dir+'\claves.dat');
Rewrite(arch);


for i:=0 to Lista.Count-1 do
begin
Writeln(arch,Lista[i]);
Progreso.Progress:=Trunc( ((Lista.Count)/(i+1))*100);
end;

CloseFile(arch);

Claves.Lines.LoadFromFile(dir+'\claves.dat');

//WinExec(Pchar('notepad.exe '+dir+'\claves.dat'),SW_SHOWNORMAL);
//ShowMessage(Lista.Text);
Lista.Free;

//*****genera las claves



{GetDir(0,dir);
AssignFile(arch,dir+'\claves.cnf');
Rewrite(arch);


while i<= num_palabras[1] do
begin
aux:= PalabraLinea(Detalle.Cells[1,1],i);

while j<= num_palabras[2] do
begin
aux:= aux+PalabraLinea(Detalle.Cells[1,2],j);


while k<= num_palabras[2] do
begin
aux:= aux+PalabraLinea(Detalle.Cells[1,2],j);

k:=k+1;
end;


j:=j+1;
end;//while  j




i:=i+1;
end;//while i

 }
end;












procedure TFormaClaveDiseno.CheckBox1Click(Sender: TObject);
begin
{sHOWmESSAGE(CheckBox1.cAPTION);
if CheckBox1.Checked then
  begin
  Edit1.Visible:=False;
  Rango1.Visible:=True;
  Rango2.Visible:=True;
  end
  else
  begin
  Edit1.Visible:=True;
  Rango1.Visible:=False;
  Rango2.Visible:=False;

  end;}
end;

procedure TFormaClaveDiseno.CheckBox2Click(Sender: TObject);
begin
{if CheckBox2.Checked then
  begin
  Edit2.Visible:=False;
  Rango3.Visible:=True;
  Rango4.Visible:=True;
  end
  else
  begin
  Edit2.Visible:=True;
  Rango3.Visible:=False;
  Rango4.Visible:=False;

  end;
 }
end;

procedure TFormaClaveDiseno.CheckBox3Click(Sender: TObject);
begin
{if CheckBox3.Checked then
  begin
  Edit3.Visible:=False;
  Rango5.Visible:=True;
  Rango6.Visible:=True;
  end
  else
  begin
  Edit3.Visible:=True;
  Rango5.Visible:=False;
  Rango6.Visible:=False;

  end;
 }
end;

procedure TFormaClaveDiseno.CheckBox4Click(Sender: TObject);
begin
{if CheckBox4.Checked then
  begin
  Edit4.Visible:=False;
  Rango7.Visible:=True;
  Rango8.Visible:=True;
  end
  else
  begin
  Edit4.Visible:=True;
  Rango7.Visible:=False;
  Rango8.Visible:=False;

  end;
 }
end;

procedure TFormaClaveDiseno.CheckBox5Click(Sender: TObject);
begin
{if CheckBox5.Checked then
  begin
  Edit5.Visible:=False;
  Rango9.Visible:=True;
  Rango10.Visible:=True;
  end
  else
  begin
  Edit5.Visible:=True;
  Rango9.Visible:=False;
  Rango10.Visible:=False;

  end;
 }
end;

procedure TFormaClaveDiseno.Edit6Click(Sender: TObject);
begin
{if Edit6.Text<>'+' then
Edit6.Text:='+'
else
Edit6.Text:='';
 }
end;

procedure TFormaClaveDiseno.Edit7Click(Sender: TObject);
begin
{if Edit7.Text<>'+' then
Edit7.Text:='+'
else
Edit7.Text:='';
 }
end;

procedure TFormaClaveDiseno.Edit8Click(Sender: TObject);
begin
{if Edit8.Text<>'+' then
Edit8.Text:='+'
else
Edit8.Text:='';
 }
end;

procedure TFormaClaveDiseno.Edit9Click(Sender: TObject);
begin
{if Edit9.Text<>'+' then
Edit9.Text:='+'
else
Edit9.Text:='';
 }
end;

procedure TFormaClaveDiseno.FormCreate(Sender: TObject);
var
arch:TextFile;
dir:string;
begin
Detalle.ColWidths[0]:=60;

Detalle.Cells[1,0]:='Valores para Clave';
Detalle.Cells[2,0]:='Agregar al Campo';

Detalle.Cells[0,1]:='Dato1';
Detalle.Cells[0,2]:='Dato2';
Detalle.Cells[0,3]:='Dato3';
Detalle.Cells[0,4]:='Dato4';





{GetDir(0,dir);
if FileExists(dir+'\claves.cnf') then
begin

try
AssignFile(arch,dir+'\claves.cnf');
Reset(arch);

Readln(arch,dir);
Label1.Caption:=dir;
Readln(arch,dir);
Label2.Caption:=dir;
Readln(arch,dir);
Label3.Caption:=dir;
Readln(arch,dir);
Label4.Caption:=dir;
Readln(arch,dir);
Label5.Caption:=dir;
Readln(arch,dir);

Edit6.Text:=dir;
Readln(arch,dir);
Edit7.Text:=dir;
Readln(arch,dir);
Edit8.Text:=dir;
Readln(arch,dir);
Edit9.Text:=dir;

Readln(arch,dir);
if dir='S' then
CheckBox1.Checked:=True
else
CheckBox1.Checked:=False;

Readln(arch,dir);
if dir='S' then
CheckBox2.Checked:=True
else
CheckBox2.Checked:=False;

Readln(arch,dir);
if dir='S' then
CheckBox3.Checked:=True
else
CheckBox3.Checked:=False;

Readln(arch,dir);
if dir='S' then
CheckBox4.Checked:=True
else
CheckBox4.Checked:=False;

Readln(arch,dir);
if dir='S' then
CheckBox5.Checked:=True
else
CheckBox5.Checked:=False;


Readln(arch,dir);
Label6.Caption:=dir;
Readln(arch,dir);
Label7.Caption:=dir;
Readln(arch,dir);
Label8.Caption:=dir;
Readln(arch,dir);
Label9.Caption:=dir;
Readln(arch,dir);
Label10.Caption:=dir;

Readln(arch,dir);
Label11.Caption:=dir;
Readln(arch,dir);
Label12.Caption:=dir;
Readln(arch,dir);
Label13.Caption:=dir;

CloseFile(arch);
except
end;
end;

 }
end;

procedure TFormaClaveDiseno.Label1DblClick(Sender: TObject);
begin

with Sender as TLabel do
begin
FormaDato:=TFormaDato.Create(Application);
  FormaDato.Label1.Caption:='Insertar un Dato';
  FormaDato.Tag:=2;
  FormaDato.ShowModal;
  Caption:=FormaDato.EditDato.Text;
FormaDato.Free;
end;



end;




procedure TFormaClaveDiseno.LabelTotalClick(Sender: TObject);
var
i,total:integer;
begin


total:=1;

for i:=1 to Detalle.RowCount-1 do
begin
if Detalle.Cells[1,i]<>'' then
begin
total:=total*CuentaPalabras(Detalle.Cells[1,i]);
LabelTotal.Caption:='Total de Claves    '+IntTostr(total);
end;
end;//for


end;










procedure TFormaClaveDiseno.BitBtn3Click(Sender: TObject);
var
i,cuenta:integer;
begin
TablaInventario.Close;
TablaInventario.DatabaseName:=LabelRuta.Caption;
TablaInventario.IndexName:='iclpro';
TablaInventario.Open;

cuenta:=Claves.Lines.Count-1;
for i:=0 to Claves.Lines.Count-1 do
begin

if not TablaInventario.FindKey([Claves.Lines[i]]) then
begin
TablaInventario.Insert;
TablaInventario.FieldByName('CLPRO_I').AsString:=Claves.Lines[i];
TablaInventario.FieldByName('DSCPR_I').AsString:=Detalle.Cells[2,1];

try
TablaInventario.FieldByName('LIPRO_I').AsString:=Edit1.Text;
TablaInventario.FieldByName('STMIN_I').AsString:=Edit2.Text;
TablaInventario.FieldByName('ULCOS_I').AsString:=Edit3.Text;
TablaInventario.FieldByName('PPUBL_I').AsString:=Edit4.Text;
TablaInventario.FieldByName('PLIS2_I').AsString:=Edit5.Text;
TablaInventario.FieldByName('PLIS3_I').AsString:=Edit6.Text;
TablaInventario.FieldByName('POLI1_I').AsString:=Edit7.Text;
TablaInventario.FieldByName('POLI2_I').AsString:=Edit7.Text;
TablaInventario.FieldByName('POLI3_I').AsString:=Edit9.Text;
except
end;

TablaInventario.Post;
end;


Progreso.Progress:=Trunc( ((cuenta)/(i+1))*100);

end;

Progreso.Progress:=100;
TablaInventario.Close;


end;

end.
