unit BuscaNFe_Para_Manifestar;

interface

uses
  Windows, system.UItypes, IB_Access,  Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TelaComum, ImgList, ExtCtrls, StdCtrls, Buttons, IB_Components,
  Mask, IB_Controls, Grids, IB_Grid, Gauges, IB_EditButton, System.ImageList;

type
  TfrmBuscaNFeParaManifestar = class(TfrmTelaComum)
    qrSelecionaNF: TIB_Query;
    GroupBox1: TGroupBox;
    rdbTodos: TRadioButton;
    rdbPeriodo: TRadioButton;
    edtDataInicio: TIB_Date;
    edtDataFim: TIB_Date;
    btnBuscar: TBitBtn;
    dsrSelecionaCtrc: TIB_DataSource;
    Label1: TLabel;
    Label2: TLabel;
    rdbNumeracao: TRadioButton;
    edtNumeracaoInicio: TLabeledEdit;
    edtNumeracaoFim: TLabeledEdit;
    GroupBox2: TGroupBox;
    edtVolumes: TIB_Currency;
    edtValorTotal: TIB_Currency;
    edtPeso: TIB_Currency;
    edtQuantidade: TIB_Currency;
    prgBarraprogresso: TGauge;
    grdPesquisa: TIB_Grid;
    procedure btnBuscarClick(Sender: TObject);
    procedure Label1Click(Sender: TObject);
    procedure Label2Click(Sender: TObject);
    procedure rdbNumeracaoClick(Sender: TObject);
    procedure rdbPeriodoClick(Sender: TObject);
    procedure rdbTodosClick(Sender: TObject);
    procedure grdPesquisaCellClick(Sender: TObject; ACol, ARow: Integer;
      AButton: TMouseButton; AShift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmBuscaNFeParaManifestar: TfrmBuscaNFeParaManifestar;

implementation

uses DataModulo, Rotinas;

{$R *.dfm}

procedure TfrmBuscaNFeParaManifestar.btnBuscarClick(Sender: TObject);
var Quantidade, Peso, ValorTotal : Real;
    QtdCtrc : Integer;
begin
  inherited;
   qrSelecionaNF.Close;
   qrSelecionaNF.SQLWhere.Clear;

   qrSelecionaNF.SQLWhere.Add( ' WHERE NF.COD_MANIFESTO IS NULL ');
   qrSelecionaNF.SQLWhere.Add( '   and NF.COD_FILIAL = :codFilial ');
   qrSelecionaNF.SQLWhere.Add( '   and (NF.cancelado <> ' + QuotedStr('T')+' or NF.cancelado is null)' );

   qrSelecionaNF.ParamByName('codFilial').Value    := Dados.qrFilial.FieldByName('cod_filial').Value;

   if rdbPeriodo.Checked then
   begin
      qrSelecionaNF.SQLWhere.Add( ' and NF.DATA_EMISSAO BETWEEN :dataInicio and :Datafim');
      qrSelecionaNF.Prepare;
      qrSelecionaNF.ParamByName('dataInicio').Value := edtDataInicio.Value;
      qrSelecionaNF.ParamByName('Datafim').Value    := edtDataFim.Value;
   end;

   if rdbNumeracao.Checked then
   begin
      qrSelecionaNF.SQLWhere.Add( ' and NF.NUMERO BETWEEN :NumInicio and :NumFim');
      qrSelecionaNF.Prepare;
      qrSelecionaNF.ParamByName('NumInicio').Value := edtNumeracaoInicio.Text;
      qrSelecionaNF.ParamByName('NumFim').Value    := edtNumeracaoFim.Text;
   end;

   qrSelecionaNF.Open;

   if not qrSelecionaNF.Bof then
     qrSelecionaNF.First;

   QtdCtrc := qrSelecionaNF.RecordCount;

   prgBarraprogresso.Progress := 0;
   prgBarraprogresso.MaxValue := QtdCtrc;

   grdPesquisa.DataSource := nil;
   while not qrSelecionaNF.Eof do
   begin
      ValorTotal := ValorTotal + qrSelecionaNF.FieldByName('VALOR_TOTAL').AsCurrency;
      Quantidade := Quantidade + qrSelecionaNF.FieldByName('Volumes').AsCurrency;
      Peso       := Peso + qrSelecionaNF.FieldByName('PESO').AsCurrency;

      qrSelecionaNF.Next;
      prgBarraprogresso.Progress := prgBarraprogresso.Progress + 1;
      Application.ProcessMessages;
   end;

   qrSelecionaNF.First;
   grdPesquisa.DataSource := dsrSelecionaCtrc;

   edtVolumes.Value    := Quantidade;
   edtValorTotal.Value := ValorTotal;
   edtPeso.Value       := Peso;
   edtQuantidade.Value := QtdCtrc;
   
   prgBarraprogresso.Progress := 0;

end;

procedure TfrmBuscaNFeParaManifestar.Label1Click(Sender: TObject);
var QtdCtrc : Integer;
begin
  inherited;
   if not qrSelecionaNF.Bof then
     qrSelecionaNF.First;

   QtdCtrc := qrSelecionaNF.RecordCount;

   prgBarraprogresso.Progress := 0;
   prgBarraprogresso.MaxValue := QtdCtrc;

   grdPesquisa.DataSource := nil;
   while not qrSelecionaNF.Eof do
   begin
      qrSelecionaNF.Edit;
      qrSelecionaNF.FieldByName('Selecao').Value := 'T';
      qrSelecionaNF.Post;

      qrSelecionaNF.Next;
      prgBarraprogresso.Progress := prgBarraprogresso.Progress + 1;
      Application.ProcessMessages;
   end;

   qrSelecionaNF.First;
   grdPesquisa.DataSource := dsrSelecionaCtrc;
   prgBarraprogresso.Progress := 0;
end;

procedure TfrmBuscaNFeParaManifestar.Label2Click(Sender: TObject);
var QtdCtrc : Integer;
begin
  inherited;
   if not qrSelecionaNF.Bof then
     qrSelecionaNF.First;

   QtdCtrc := qrSelecionaNF.RecordCount;

   prgBarraprogresso.Progress := 0;
   prgBarraprogresso.MaxValue := QtdCtrc;

   grdPesquisa.DataSource := nil;
   while not qrSelecionaNF.Eof do
   begin
      qrSelecionaNF.Edit;
      qrSelecionaNF.FieldByName('Selecao').Value := 'F';
      qrSelecionaNF.Post;

      qrSelecionaNF.Next;
      prgBarraprogresso.Progress := prgBarraprogresso.Progress + 1;
      Application.ProcessMessages;
   end;

   qrSelecionaNF.First;
   grdPesquisa.DataSource := dsrSelecionaCtrc;
   prgBarraprogresso.Progress := 0;
end;

procedure TfrmBuscaNFeParaManifestar.rdbNumeracaoClick(Sender: TObject);
begin
  inherited;
   edtDataInicio.Visible      := False;
   edtDataFim.Visible         := False;
   edtNumeracaoInicio.Visible := False;
   edtNumeracaoFim.Visible    := False;

   if rdbNumeracao.Checked then
   begin
      edtNumeracaoInicio.Visible := True;
      edtNumeracaoFim.Visible    := True;
   end;
end;

procedure TfrmBuscaNFeParaManifestar.rdbPeriodoClick(Sender: TObject);
begin
  inherited;
   edtDataInicio.Visible      := False;
   edtDataFim.Visible         := False;
   edtNumeracaoInicio.Visible := False;
   edtNumeracaoFim.Visible    := False;


   if rdbPeriodo.Checked then
   begin
      edtDataInicio.Visible := True;
      edtDataFim.Visible    := True;
   end;
end;

procedure TfrmBuscaNFeParaManifestar.rdbTodosClick(Sender: TObject);
begin
  inherited;
   if rdbTodos.Checked then
   begin
      edtDataInicio.Visible      := False;
      edtDataFim.Visible         := False;
      edtNumeracaoInicio.Visible := False;
      edtNumeracaoFim.Visible    := False;
   end;
end;

procedure TfrmBuscaNFeParaManifestar.grdPesquisaCellClick(Sender: TObject;
  ACol, ARow: Integer; AButton: TMouseButton; AShift: TShiftState);
begin
  inherited;
   qrSelecionaNF.Edit;
   if qrSelecionaNF.FieldByName('Selecao').AsString = 'T' then
      qrSelecionaNF.FieldByName('Selecao').Value := 'F'
   else
      qrSelecionaNF.FieldByName('Selecao').Value := 'T';

   qrSelecionaNF.Post;

end;

end.
