unit concen;

interface
uses DBTables, dialogos,SysUtils,checa, progreso;


procedure concentrar(TablaConcentrar:TTable;rango1,rango2,campofecha,fechaini,fechafin,campodeorden,campodenombre:String;concentrar1,concentrar2,concentrar3,concentrar4,concentrar5,concentrar6,concentrar7,concentrar8,cr:string;op:char;vr,tipo:string);
procedure poneetiquetas(campodeorden,campodenombre:String;concentrar1,concentrar2,concentrar3,concentrar4,concentrar5,concentrar6,concentrar7,concentrar8,cr:string);
procedure concentrarcadenas(TablaConcentrar:TTable;rango1,rango2,campofecha,fechaini,fechafin,campodeorden,campodenombre:string;concentrar1,concentrar2,concentrar3,concentrar4,concentrar5,concentrar6,concentrar7,concentrar8,cr:string;op:char;vr,tipo:string);
implementation

procedure poneetiquetas(campodeorden,campodenombre:String;concentrar1,concentrar2,concentrar3,concentrar4,concentrar5,concentrar6,concentrar7,concentrar8,cr:string);
Type
eti=record
orden,nombre,c1,c2,c3,c4,c5,c6,c7,c8:String[25];
end;

var
arch2:File of Eti;
Etiquetas:Eti;

begin

AssignFile(arch2,leeruta+'\tempo\etique.tmp');
ReWrite(arch2);
Etiquetas.orden:=campodeorden;
Etiquetas.nombre:=campodenombre;
Etiquetas.c1:=concentrar1;
Etiquetas.c2:=concentrar2;
Etiquetas.c3:=concentrar3;
Etiquetas.c4:=concentrar4;
Etiquetas.c5:=concentrar5;
Etiquetas.c6:=concentrar6;
Etiquetas.c7:=concentrar7;
Etiquetas.c8:=concentrar8;
write(arch2,Etiquetas);
CloseFile(arch2);

end;

function operacion(val1,op,val2:string):Currency;
var
signo:Char;
begin
IF val1='' then val1:='0';
if val2='' then val2:='0';
signo:=op[1];
case signo of
'*':operacion:=StrToCurr(val1)*StrToCurr(val2);
'/':operacion:=StrToCurr(val1)/StrToCurr(val2);
'+':operacion:=StrToCurr(val1)+StrToCurr(val2);
'-':operacion:=StrToCurr(val1)-StrToCurr(val2);
{'!':operacion:=StrToCurr(val1)<>StrToCurr(val2);}
else
operacion:=0;
end;
end;

function formula(Tabla:TTable;formu:string):Real;
var
i,j,cuenta:integer;
aux:string;
parametro:array[1..10] of String[30];
suma:Currency;
begin
aux:='';
cuenta:=1;
{*****}
for i:=2 to length(formu) do
begin
if formu[i]= '&' then
begin
parametro[cuenta]:=aux;
cuenta:=cuenta+1;
aux:='';
end
else
 aux:=aux+formu[i];
end;
{****}

for i:=1 to cuenta do
begin
aux:='';
if parametro[i][1]='%' then
begin
for j:=2 to length(parametro[i]) do
aux:=aux+parametro[i][j];
parametro[i]:=Tabla.FieldByName(aux).AsString;
end;
end;

{***}

suma:=0;
suma:=operacion(parametro[1],parametro[2],parametro[3]);
suma:=suma*StrToCurr(parametro[5]);
{***}
formula:=suma;

end;



function periodo(campofecha,fechaini,fechafin:String):Boolean;
begin
if (fechaini<>'f')and (fechafin<>'f')and(fechaini<>'')and (fechafin<>'')and (campofecha<>'') then
begin

if (StrToDateTime(campofecha)>=StrToDateTime(fechaini))and (StrToDateTime(campofecha)<=StrToDateTime(fechafin)) then
periodo:=True
else
periodo:=False;
end
else
periodo:=True;



end;


function cumple(v1:string;ope:Char;v2:String):Boolean;
begin
case ope of
'=':begin
    if v1=v2 then cumple:=True else cumple:=False;
    end;
'<':begin
    if v1<v2 then cumple:=True else cumple:=False;
    end;
'>':begin
    if v1>v2 then cumple:=True else cumple:=False;
    end;
'!':begin
    if v1<>v2 then cumple:=True else cumple:=False;
    end;
'X':begin
    cumple:=True;
    end;
end;

end;


procedure concentraarchivo;
type
registros=record
orden:String[100];
nombre:String[100];
c1,c2,c3,c4,c5,c6,c7,c8:Real;
end;
var
arch,arch2:File of Registros;
anterior,siguiente:Registros;
clave,nombre:string;
nreg:LongInt;
cn1,cn2,cn3,cn4,cn5,cn6,cn7,cn8:Real;
begin
AssignFile(arch,leeruta+'\tempo\orden.tmp');
AssignFile(arch2,leeruta+'\tempo\final.tmp');
Reset(arch);
Rewrite(arch2);
Read(arch,anterior);
clave:=anterior.orden;
nombre:=anterior.nombre;
cn1:=anterior.c1;
cn2:=anterior.c2;
cn3:=anterior.c3;
cn4:=anterior.c4;
cn5:=anterior.c5;
cn6:=anterior.c6;
cn7:=anterior.c7;
cn8:=anterior.c8;
{cn1:=0;cn2:=0;cn3:=0;cn4:=0;cn5:=n0;cn6;=0;cn7:=0;cn8:=0;}
while not Eof(arch) do
begin
Read(arch,siguiente);
if clave=siguiente.orden then
begin
{concentra}
cn1:=cn1+siguiente.c1;
cn2:=cn2+siguiente.c2;
cn3:=cn3+siguiente.c3;
cn4:=cn4+siguiente.c4;
cn5:=cn5+siguiente.c5;
cn6:=cn6+siguiente.c6;
cn7:=cn7+siguiente.c7;
cn8:=cn8+siguiente.c8;

end
else
begin
{nuevo}
anterior.c1:=cn1;
anterior.c2:=cn2;
anterior.c3:=cn3;
anterior.c4:=cn4;
anterior.c5:=cn5;
anterior.c6:=cn6;
anterior.c7:=cn7;
anterior.c8:=cn8;
anterior.orden:=clave;
anterior.nombre:=nombre;

if FileSize(arch)<>FilePos(arch) then
write(arch2,anterior);
cn1:=siguiente.c1;
cn2:=siguiente.c2;
cn3:=siguiente.c3;
cn4:=siguiente.c4;
cn5:=siguiente.c5;
cn6:=siguiente.c6;
cn7:=siguiente.c7;
cn8:=siguiente.c8;

end;
clave:=siguiente.orden;
nombre:=siguiente.nombre;
end;
CloseFile(arch);
CloseFile(arch2);
end;



procedure ordenalista;
type
registros=record
orden:String[100];
nombre:string[100];
c1,c2,c3,c4,c5,c6,c7,c8:Real;
end;

var
arch,arch2:File of Registros;
regaux,dato1,dato2:Registros;
nreg:LongInt;

begin

AssignFile(arch,leeruta+'\tempo\orden.tmp');
AssignFile(arch2,leeruta+'\tempo\concen.tmp');

ReWrite(arch);
Reset(arch2);

while not Eof(arch2) do
begin
Read(arch2,dato2);
nreg:=0;
Seek(arch,0);

if FileSize(arch)>0 then
while not Eof(arch) do
begin
Read(arch,dato1);
if dato1.orden>dato2.orden then
begin
break;
end;
nreg:=nreg+1;
end;
dato1.orden:=dato2.orden;
dato1.nombre:=dato2.nombre;
dato1.c1:=dato2.c1;
dato1.c2:=dato2.c2;
dato1.c3:=dato2.c3;
dato1.c4:=dato2.c4;
dato1.c5:=dato2.c5;
dato1.c6:=dato2.c6;
dato1.c7:=dato2.c7;
dato1.c8:=dato2.c8;

if nreg<FileSize(arch) then
begin
Seek(arch,nreg);
Read(arch,regaux);
Seek(arch,nreg);
write(arch,dato1);
dato1:=regaux;
nreg:=FilePos(arch);
end;

if FileSize(arch)>0 then
while not Eof(arch) do
begin
Read(arch,regaux);
Seek(arch,FilePos(arch)-1);
Write(arch,dato1);
dato1:=regaux;
end;
{if FileSize(arch2)<>FilePos(arch2) then}
write(arch,dato1);
end;

dato1.orden:='*';

dato1.c1:=0;
dato1.c2:=0;
dato1.c3:=0;
dato1.c4:=0;
dato1.c5:=0;
dato1.c6:=0;
dato1.c7:=0;
dato1.c8:=0;

write(arch,dato1);
write(arch,dato1);
write(arch,dato1);

{Seek(arch,FileSize(arch)-1);
Truncate(arch);
write(arch,dato1);}

CloseFile(arch);
CloseFile(arch2);

end;



procedure concentrar(TablaConcentrar:TTable;rango1,rango2,campofecha,fechaini,fechafin,campodeorden,campodenombre:string;concentrar1,concentrar2,concentrar3,concentrar4,concentrar5,concentrar6,concentrar7,concentrar8,cr:string;op:char;vr,tipo:string);

type
registros=record
orden:String[100];
nombre:String[100];
c1,c2,c3,c4,c5,c6,c7,c8:Real;
end;

eti=record
orden:String[25];
c1,c2,c3,c4,c5,c6,c7,c8:String[25];
end;

var
arch:File of registros;
arch2:File of Eti;
dato:registros;
etiquetas:Eti;
valor:string;
fechavalida:Boolean;
intervalo, intervalo2:real;
i:integer;

begin
FormaProgreso.Show;

MuestraMensaje2('pausa');
cierramensaje;
intervalo2:=0;
AssignFile(arch,leeruta+'\tempo\concen.tmp');
ReWrite(arch);
if TablaConcentrar.RecordCount>0 then
begin

if (rango1<>'')and(rango2<>'')then
TablaConcentrar.SetRange([rango1],[rango2])
else
TablaConcentrar.CancelRange;
{**muestra avnace*****}

{FormaProgreso.Timer1.Enabled:=True;}

{FormaProgreso.BringToFront;}
{FormaProgreso.Cuadro.Show;
FormaProgreso.Label1.Show;
FormaProgreso.Panel1.Show;}

{****************}

{for i:=1 to 1000000 do;}

intervalo:=100/TablaConcentrar.RecordCount;
TablaConcentrar.First;

while not TablaConcentrar.Eof do
begin
if (cr<>'') then
valor:=TablaConcentrar.FieldByName(cr).AsString;
fechavalida:=True;

if (campofecha<>'')and (campofecha<>'f') then
fechavalida:=periodo(TablaConcentrar.FieldByName(campofecha).AsString,fechaini,fechafin);

if (cumple(valor,op,vr))and(fechavalida) then
begin
if TablaConcentrar.FieldByName(campodeorden).AsString='' then
dato.orden:='N.A.'
else
dato.orden:=TablaConcentrar.FieldByName(campodeorden).AsString;
if campodenombre<>'' then
dato.nombre:=TablaConcentrar.FieldByName(campodenombre).AsString
else
dato.nombre:='';

if concentrar1<>'' then
begin
if (concentrar1[1]='&') then
dato.c1:=formula(TablaConcentrar,concentrar1)
else
dato.c1:=TablaConcentrar.FieldByName(concentrar1).AsFloat;
end
else
dato.c1:=0;

if concentrar2<>'' then
begin
if (concentrar2[1]='&') then
dato.c2:=formula(TablaConcentrar,concentrar2)
else
dato.c2:=TablaConcentrar.FieldByName(concentrar2).AsFloat;
end
else
dato.c2:=0;

if concentrar3<>'' then
begin
if (concentrar3[1]='&') then
dato.c3:=formula(TablaConcentrar,concentrar3)
else
dato.c3:=TablaConcentrar.FieldByName(concentrar3).AsFloat;
end
else
dato.c3:=0;

if concentrar4<>'' then
begin
if (concentrar4[1]='&') then
dato.c4:=formula(TablaConcentrar,concentrar4)
else
dato.c4:=TablaConcentrar.FieldByName(concentrar4).AsFloat;
end
else
dato.c4:=0;

if concentrar5<>'' then
begin
if (concentrar5[1]='&') then
dato.c5:=formula(TablaConcentrar,concentrar5)
else
dato.c5:=TablaConcentrar.FieldByName(concentrar5).AsFloat;

end
else
dato.c5:=0;

if concentrar6<>'' then
begin
if (concentrar6[1]<>'&') then
dato.c6:=formula(TablaConcentrar,concentrar6)
else
dato.c6:=TablaConcentrar.FieldByName(concentrar6).AsFloat;
end
else
dato.c6:=0;

if concentrar7<>'' then
begin
if (concentrar7[1]='&') then
dato.c7:=formula(TablaConcentrar,concentrar7)
else
dato.c7:=TablaConcentrar.FieldByName(concentrar7).AsFloat;
end
else
dato.c7:=0;

if concentrar8<>'' then
begin
if (concentrar8[1]='&') then
dato.c8:=formula(TablaConcentrar,concentrar8)
else
dato.c8:=TablaConcentrar.FieldByName(concentrar8).AsFloat;
end
else
dato.c8:=0;

write(arch,dato);
end;

Tablaconcentrar.Next;
intervalo2:=intervalo2+intervalo;
{progresando(Trunc(FormaProgreso.BarraProgreso.Progress+intervalo2));}
FormaProgreso.BarraProgreso.Progress:=Trunc(FormaProgreso.BarraProgreso.Progress+intervalo2);

end;
dato.orden:='*';
dato.c1:=0;
dato.c2:=0;
dato.c3:=0;
dato.c4:=0;
dato.c5:=0;
dato.c6:=0;
dato.c7:=0;
dato.c8:=0;

write(arch,dato);

CloseFile(arch);


FormaProgreso.Close;
{FormaProgreso.BarraProgreso.Progress:=100;}
FormaProgreso.BarraProgreso.Progress:=0;
if tipo='C' then
begin
ordenalista;
concentraarchivo;
{DeleteFile(leeruta+'\tempo\final.tmp');
RenameFile(leeruta+'\tempo\orden.tmp',leeruta+'\tempo\final.tmp');
}
end;

if tipo='D' then
begin
DeleteFile(leeruta+'\tempo\final.tmp');
RenameFile(leeruta+'\tempo\concen.tmp',leeruta+'\tempo\final.tmp');
end;
end
else
begin
CloseFile(arch);
AssignFile(arch,leeruta+'\tempo\final.tmp');
ReWrite(arch);
CloseFile(arch);

end;
end;
{******concentra cadenas******************}

procedure concentrarcadenas(TablaConcentrar:TTable;rango1,rango2,campofecha,fechaini,fechafin,campodeorden,campodenombre:string;concentrar1,concentrar2,concentrar3,concentrar4,concentrar5,concentrar6,concentrar7,concentrar8,cr:string;op:char;vr,tipo:string);
type
registros=record
orden:String[100];
nombre:String[100];
c1,c2,c3,c4,c5,c6,c7,c8:String[40];
end;

eti=record
orden:String[25];
c1,c2,c3,c4,c5,c6,c7,c8:String[25];
end;

var
arch:File of registros;
arch2:File of Eti;
dato:registros;
etiquetas:Eti;
valor:string;
fechavalida:Boolean;
intervalo, intervalo2:real;

begin
{MuestraMensaje(leeruta+'\tempo\concen.tmp');}
intervalo2:=0;
AssignFile(arch,leeruta+'\tempo\concen.tmp');
ReWrite(arch);
if TablaConcentrar.RecordCount>0 then
begin

if (rango1<>'')and(rango2<>'')then
TablaConcentrar.SetRange([rango1],[rango2])
else
TablaConcentrar.CancelRange;
FormaProgreso.Show;

intervalo:=100/TablaConcentrar.RecordCount;
TablaConcentrar.First;

while not TablaConcentrar.Eof do
begin
if (cr<>'') then
valor:=TablaConcentrar.FieldByName(cr).AsString;
fechavalida:=True;

if (campofecha<>'')and (campofecha<>'f') then
fechavalida:=periodo(TablaConcentrar.FieldByName(campofecha).AsString,fechaini,fechafin);

if (cumple(valor,op,vr))and(fechavalida) then
begin
if TablaConcentrar.FieldByName(campodeorden).AsString='' then
dato.orden:='N.A.'
else
dato.orden:=TablaConcentrar.FieldByName(campodeorden).AsString;
if campodenombre<>'' then
dato.nombre:=TablaConcentrar.FieldByName(campodenombre).AsString
else
dato.nombre:='';

if concentrar1<>'' then
begin
dato.c1:=TablaConcentrar.FieldByName(concentrar1).AsString;
end
else
dato.c1:='';

if concentrar2<>'' then
begin
dato.c2:=TablaConcentrar.FieldByName(concentrar2).AsString;
end
else
dato.c2:='';

if concentrar3<>'' then
begin
dato.c3:=TablaConcentrar.FieldByName(concentrar3).AsString;
end
else
dato.c3:='';

if concentrar4<>'' then
begin
if (concentrar4[1]='&') then
dato.c4:=TablaConcentrar.FieldByName(concentrar4).AsString;
end
else
dato.c4:='';

if concentrar5<>'' then
begin
dato.c5:=TablaConcentrar.FieldByName(concentrar5).AsString;

end
else
dato.c5:='';

if concentrar6<>'' then
begin
dato.c6:=TablaConcentrar.FieldByName(concentrar6).AsString;
end
else
dato.c6:='';

if concentrar7<>'' then
begin
dato.c7:=TablaConcentrar.FieldByName(concentrar7).AsString;
end
else
dato.c7:='';

if concentrar8<>'' then
begin
dato.c8:=TablaConcentrar.FieldByName(concentrar8).AsString;
end
else
dato.c8:='';

write(arch,dato);
end;

Tablaconcentrar.Next;
intervalo2:=intervalo2+intervalo;
FormaProgreso.BarraProgreso.Progress:=Trunc(FormaProgreso.BarraProgreso.Progress+intervalo2);

end;
dato.orden:='*';
dato.c1:='';
dato.c2:='';
dato.c3:='';
dato.c4:='';
dato.c5:='';
dato.c6:='';
dato.c7:='';
dato.c8:='';

write(arch,dato);

CloseFile(arch);


FormaProgreso.Close;
FormaProgreso.BarraProgreso.Progress:=0;
if tipo='C' then
begin
{ordenalista;
concentraarchivo;}
{DeleteFile(leeruta+'\tempo\final.tmp');
RenameFile(leeruta+'\tempo\orden.tmp',leeruta+'\tempo\final.tmp');
}
end;

if tipo='D' then
begin
DeleteFile(leeruta+'\tempo\final.tmp');
RenameFile(leeruta+'\tempo\concen.tmp',leeruta+'\tempo\final.tmp');
end;
end
else
begin
CloseFile(arch);
AssignFile(arch,leeruta+'\tempo\final.tmp');
ReWrite(arch);
CloseFile(arch);

end;
end;


{*****************************************}

end.
