unit graficas;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  ComCtrls, Tabnotbk, StdCtrls, Buttons, Db, DBTables, Grids, ExtCtrls,
  Menus,Printers,checa, Dialogos,describe, Dialogs;

type
  TFormaGraficas = class(TForm)
    TablaFactura: TTable;
    Reporte: TStringGrid;
    Panel1: TPanel;
    MainMenu1: TMainMenu;
    Archivo1: TMenuItem;
    Exportar1: TMenuItem;
    Exc1: TMenuItem;
    Salir1: TMenuItem;
    Imprimir1: TMenuItem;
    Label1: TLabel;
    Pag: TLabel;
    DialogoImprimir: TPrintDialog;
    DialogoGuarda: TSaveDialog;
    Graficas1: TMenuItem;
    BotonImprimir: TSpeedButton;
    Suma: TLabel;
    BotonImprime: TSpeedButton;
    BotonBordes: TSpeedButton;
    BotonSuma: TSpeedButton;
    BotonExportar: TSpeedButton;
    BotonAnterior: TSpeedButton;
    BotonSiguiente: TSpeedButton;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    procedure BotonSiguienteClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BotonImprimirClick(Sender: TObject);
    procedure Exc1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Salir1Click(Sender: TObject);
    procedure BotonSumaClick(Sender: TObject);
    procedure BotonBordesClick(Sender: TObject);
    procedure BotonExportarMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonExportaTextoMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure BotonSumaMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonBordesMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonAnteriorMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonSiguienteMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure BotonImprimeMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonImprimirMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormaGraficas: TFormaGraficas;
  maximo:Real;
  intervalo:Real;
  arch,arch1:TextFile;
  paginas:integer;
  fin:boolean;

procedure ejecuta;
implementation

uses consulta;

{$R *.DFM}



procedure paginaanterior;
var
c:char;
i,j,cuenta,ancho,numlineas:integer;
linea,aux:String;
begin
AssignFile(arch1,FormaConsulta.Destino.Text);
Reset(arch1);
for i:=1 to 50*(StrToInt(FormaGraficas.pag.Caption)-1) do
begin
Readln(arch1,linea);

end;

numlineas:=0;
FormaGraficas.Reporte.Col:=0;
FormaGraficas.Reporte.Row:=1;
while (not Eof(arch1)) and (numlineas<50) do
begin
aux:='';
linea:='';
Readln(arch1,linea);
for i:=1 to Length(linea) do
begin
if linea[i]<>char(9) then
aux:=aux+linea[i]
else
begin
FormaGraficas.Reporte.Cells[FormaGraficas.Reporte.Col,FormaGraficas.Reporte.Row]:=aux;
FormaGraficas.Reporte.Col:=FormaGraficas.Reporte.Col+1;
aux:='';
end;




end;
FormaGraficas.Reporte.Col:=0;
FormaGraficas.Reporte.RowCount:=FormaGraficas.Reporte.RowCount+1;
FormaGraficas.Reporte.Row:=FormaGraficas.Reporte.Row+1;
numlineas:=numlineas+1;

end;

if EoF(arch1) then
fin:=True;
if numlineas<49 then
for i:=numlineas+1 to 50 do
begin
for j:=0 to FormaGraficas.Reporte.ColCount do
FormaGraficas.Reporte.Cells[j,i]:='';
end;

paginas:=paginas-1;
CloseFile(arch1);

end;


procedure chechaarchivos;

begin
with FormaGraficas do
begin
{$I-}
writeln(arch,'');
if IOResult<>0 then
CloseFile(arch);
{$I+}
{****************}
{$I-}
{writeln(arch1,'');
if IOResult<>0 then
CloseFile(arch1);}
{$I+}
end;

end;




procedure sumando;
var
sumar:real;
i:integer;
begin
With FormaGraficas do
begin
sumar:=0;
for i:=1 to Reporte.RowCount-2 do
begin
if Reporte.Cells[Reporte.Col,i]<>'' then
sumar:=sumar+StrToFloat(Reporte.Cells[Reporte.Col,i]);
end;
Suma.Caption:=FloatToStr(sumar);
end;
end;


{function ruta(linea:integer):String;
var
arch:TextFile;
aux:string;
i:integer;

begin
AssignFile(arch,'c:\Proyecto\Sistema\General.cnf');
Reset(arch);
for i:=1 to linea do
Readln(arch,aux);
CloseFile(arch);
ruta:=aux;

end;}

procedure rutas;
var
  j: Integer;
begin

with FormaGraficas do
begin
  for j := 0 to ComponentCount -1 do
    begin
     if Components[j] is TTable  then
       begin
       TTable(Components[j]).DatabaseName:=ruta(8);
       TTable(Components[j]).Open;
       end;
    end;
end;

end;


procedure graficar;
var
x,y,tamo,i:integer;

begin

with FormaGraficas do
begin
Canvas.Pen.Color:=FormaGraficas.Color;
Canvas.Brush.Color:=FormaGraficas.Color;
Canvas.Rectangle(0,0,645,500);
Canvas.Pen.Color:=clgreen;
Canvas.MoveTo(60,30);
Canvas.LineTo(60,400);

Canvas.MoveTo(60,400);
Canvas.LineTo(600,400);
intervalo:=300/maximo;

{for i:=0 to Trunc(maximo) do
begin}

Canvas.TextOut(2,50,IntToStr(Trunc(maximo)));

{end;}

end;

end;




procedure leeencabezado;
var
c:char;
i,cuenta,ancho,numlineas:integer;
linea,aux:String;

begin
fin:=False;
FormaGraficas.Reporte.ColCount:=20;
FormaGraficas.Reporte.RowCount:=2;

AssignFile(arch,FormaConsulta.Destino.Text);
Reset(arch);
{********************}
aux:='';
linea:='';
Readln(arch,linea);
cuenta:=0;
for i:=1 to Length(linea) do
begin
if linea[i]=char(9) then
begin
cuenta:=cuenta+1;
end;
end;
{***ancho de reporte***}

FormaGraficas.Reporte.ColCount:=cuenta+1;

{********}

for i:=1 to Length(linea) do
begin
if linea[i]<>char(9) then
aux:=aux+linea[i]
else
begin
FormaGraficas.Reporte.Cells[FormaGraficas.Reporte.Col,0]:=aux;
FormaGraficas.Reporte.Col:=FormaGraficas.Reporte.Col+1;
aux:='';
end;
end;
FormaGraficas.Reporte.Col:=0;
cuenta:=0;
aux:='';
linea:='';
Readln(arch,linea);

for i:=0 to FormaGraficas.Reporte.ColCount-1 do
begin
FormaGraficas.Reporte.ColWidths[i]:=length(FormaGraficas.Reporte.Cells[i,0])*6;
end;

for i:=1 to Length(linea) do
begin
if linea[i]<>char(9) then
aux:=aux+linea[i]
else
begin

ancho:=StrToInt(aux);
if FormaGraficas.Reporte.ColWidths[i]<ancho*5 then
if ancho<>0 then
FormaGraficas.Reporte.ColWidths[cuenta]:=5*ancho
else FormaGraficas.Reporte.ColWidths[cuenta]:=60;
cuenta:=cuenta+1;
aux:='';
end;

end;

FormaGraficas.BotonSiguiente.Tag:=1;

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

end;

procedure leepagina;
var
c:char;
i,j,cuenta,ancho,numlineas:integer;
linea,aux:String;
begin
if Filesize(arch)>0 then
begin
numlineas:=0;
FormaGraficas.Reporte.Col:=0;
FormaGraficas.Reporte.Row:=1;
while (not Eof(arch)) and (numlineas<50) do
begin
aux:='';
linea:='';
Readln(arch,linea);
for i:=1 to Length(linea) do
begin
if linea[i]<>char(9) then
aux:=aux+linea[i]
else
begin
FormaGraficas.Reporte.Cells[FormaGraficas.Reporte.Col,FormaGraficas.Reporte.Row]:=aux;
FormaGraficas.Reporte.Col:=FormaGraficas.Reporte.Col+1;
aux:='';
end;




end;
FormaGraficas.Reporte.Col:=0;
FormaGraficas.Reporte.RowCount:=FormaGraficas.Reporte.RowCount+1;
FormaGraficas.Reporte.Row:=FormaGraficas.Reporte.Row+1;
numlineas:=numlineas+1;

end;

if EoF(arch) then
fin:=True;
if numlineas<49 then
for i:=numlineas+1 to 50 do
begin
for j:=0 to FormaGraficas.Reporte.ColCount do
FormaGraficas.Reporte.Cells[j,i]:='';
end;

paginas:=paginas+1;
end;
end;

procedure ejecuta;
begin
with FormaGraficas do
begin
Reporte.Col:=0;
Reporte.ColCount:=0;
Reporte.Row:=1;
Reporte.RowCount:=2;

Reporte.Width:=FormaGraficas.Width;
{Panel1.Width:=FormaGraficas.Width;}

leeencabezado;
leepagina;
end;

end;


procedure TFormaGraficas.BotonSiguienteClick(Sender: TObject);
begin
if fin<>True then
begin
leepagina;
pag.Caption:=IntToStr(StrToInt(pag.Caption)+1);
end;

end;

procedure TFormaGraficas.FormCreate(Sender: TObject);
begin
{rutas;}
FormatosPais;
fin:=False;
BotonSiguiente.Tag:=1;
paginas:=0;
{Panel1.Visible:=False;}
end;

procedure TFormaGraficas.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin

BotonSiguiente.Tag:=1;
chechaarchivos;
{CloseFile(arch);}
Salir1.Tag:=1000;
end;

procedure imprimepagina;
var i,j,pos,ren,col:integer;
begin
with FormaGraficas do
begin

Printer.BeginDoc;
ren:=Trunc(Printer.PageHeight/55);
col:=Trunc(Printer.PageWidth/55);
col:=Trunc(col/500);
Printer.Canvas.Font.Size:=10;
pos:=0;
for j:=0 to 50 do
begin
for i:=0 to Reporte.ColCount do
begin
Printer.Canvas.TextOut(pos,j*ren,Reporte.Cells[i,j]);
pos:=pos+(Reporte.ColWidths[i])*3;
end;
pos:=0;

end;
Printer.EndDoc;
end;
end;

procedure TFormaGraficas.BotonImprimirClick(Sender: TObject);
begin
if ImpresoraLista then
begin
fin:=False;
DialogoImprimir.Options := [poHelp, poWarning];
if DialogoImprimir.Execute then
begin
{numlineas:=0;}
while fin<>True do
begin
leepagina;
pag.Caption:=IntToStr(StrToInt(pag.Caption)+1);
imprimepagina;
end;
end;
end
else
MuestraMensaje('La Impresora No Se Encuentra Lista...');
end;

procedure TFormaGraficas.Exc1Click(Sender: TObject);
var
arch2:TextFile;
fuente,destino:PChar;
nombre,nombre2:String[125];
f:longbool;
begin

if DialogoGuarda.Execute then
begin
{ AssignFile(arch2,'c:\Proyecto\tempo\tempo.xls');
Rename(arch2, DialogoGuarda.FileName);}
if FileExists(PChar(FormaConsulta.Destino.Text)) then
DeleteFile(PChar(FormaConsulta.Destino.Text));
CopyFile(PChar(FormaConsulta.Destino.Text),PChar(DialogoGuarda.FileName),F);
end;




end;

procedure TFormaGraficas.FormActivate(Sender: TObject);
var
f:longbool;

begin
{if FormaGraficas.Tag=0 then
rutas;}
{CopyFile(PChar(FormaConsulta.Destino.Text),'c:\Proyecto\tempo\tempo.xls',F);}
{Reporte.Col:=0;
Reporte.ColCount:=0;
Reporte.Row:=1;
Reporte.RowCount:=2;

Reporte.Width:=FormaGraficas.Width;
Panel1.Width:=FormaGraficas.Width;
if Tag=1 then
begin
leeencabezado;
leepagina;
end; }
{Tag:=2;}
{BotonSiguiente.Tag:=0;}

end;

procedure TFormaGraficas.Salir1Click(Sender: TObject);
begin
Close;

end;







procedure TFormaGraficas.BotonSumaClick(Sender: TObject);
begin
sumando;
end;

procedure TFormaGraficas.BotonBordesClick(Sender: TObject);
begin
if Reporte.GridLineWidth=0 then
Reporte.GridLineWidth:=1
else
Reporte.GridLineWidth:=0;
end;

procedure TFormaGraficas.BotonExportarMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaGraficas.Left+BotonExportar.Left,FormaGraficas.Top+BotonExportar.Top+BotonExportar.Height,89);
end;

procedure TFormaGraficas.BotonExportaTextoMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
{describir(Button,FormaGraficas.Left+BotonExportaTexto.Left,FormaGraficas.Top+BotonExportaTexto.Top+BotonExportaTexto.Height,90);}
end;

procedure TFormaGraficas.BotonSumaMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaGraficas.Left+BotonSuma.Left,FormaGraficas.Top+BotonSuma.Top+BotonSuma.Height,91);
end;

procedure TFormaGraficas.BotonBordesMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaGraficas.Left+BotonBordes.Left,FormaGraficas.Top+BotonBordes.Top+BotonBordes.Height,92);
end;

procedure TFormaGraficas.BotonAnteriorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaGraficas.Left+BotonAnterior.Left,FormaGraficas.Top+BotonAnterior.Top+BotonAnterior.Height,93);
end;

procedure TFormaGraficas.BotonSiguienteMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaGraficas.Left+BotonSiguiente.Left,FormaGraficas.Top+BotonSiguiente.Top+BotonSiguiente.Height,94);
end;

procedure TFormaGraficas.BotonImprimeMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaGraficas.Left+BotonImprime.Left,FormaGraficas.Top+BotonImprime.Top+BotonImprime.Height,95);
end;

procedure TFormaGraficas.BotonImprimirMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaGraficas.Left+BotonImprimir.Left,FormaGraficas.Top+BotonImprimir.Top+BotonImprimir.Height,96);
end;

procedure TFormaGraficas.SpeedButton1Click(Sender: TObject);
begin
paginaanterior;
pag.Caption:=IntToStr(StrToInt(pag.Caption)-1);
if StrToInt(pag.Caption)<1 then pag.Caption:='1';

end;

end.
