unit CadGNRE;

interface

uses
  Windows, system.UItypes, IB_Access,  Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ModeloCadastro, UCBase, ImgList, AppEvnts, IB_Components,
  ExtCtrls, StdCtrls, jpeg, Grids, IB_Grid, Buttons, ComCtrls, IB_Controls,
  Mask,Rotinas, IB_EditButton, System.ImageList;

type
  TfrmCadGNRE = class(TFrModeloCadastro)
    GroupBox2: TGroupBox;
    dbCodigo: TIB_Text;
    edtDataCad: TIB_Edit;
    edtUsuario: TIB_Edit;
    edtVenc: TIB_Date;
    dbCodigoReceita: TIB_Edit;
    dbCodBanco: TIB_Edit;
    dbNomeBanco: TIB_Edit;
    dbAgencia: TIB_Edit;
    qrUsuarioCad: TIB_Query;
    dsrUsuario: TIB_DataSource;
    qryCliente: TIB_Query;
    dtCliente: TIB_DataSource;
    dbDataEmissao: TIB_Date;
    GroupBox1: TGroupBox;
    dbAno: TIB_Edit;
    dbCompetencia: TIB_ComboBox;
    qryBancos: TIB_Query;
    dtBancos: TIB_DataSource;
    qryNF: TIB_Query;
    GroupBox3: TGroupBox;
    dbNF: TIB_Edit;
    btnPesquisaNF: TBitBtn;
    IB_Edit1: TIB_Edit;
    dtNF: TIB_DataSource;
    qrUF_Cliente: TIB_Query;
    dsrUf_Cliente: TIB_DataSource;
    IB_Edit3: TIB_Edit;
    GroupBox7: TGroupBox;
    edtCidadeTransporte: TIB_Edit;
    dbNomeFantasia: TIB_Edit;
    dbInscricao: TIB_Edit;
    edtUF: TIB_LookupCombo;
    edtCNPJ: TLabeledEdit;
    edtValor: TIB_Edit;
    IB_Edit2: TIB_Edit;
    dbConvenioProtocolo: TIB_LookupCombo;
    qryConvenioProtocolo: TIB_Query;
    dtConvenioProtocolo: TIB_DataSource;
    procedure FormCreate(Sender: TObject);
    procedure BtInserirClick(Sender: TObject);
    procedure dbCodBancoExit(Sender: TObject);
    procedure btnPesquisaNFClick(Sender: TObject);
    procedure dbNFExit(Sender: TObject);
    procedure BtEditarClick(Sender: TObject);
    procedure BtCancelarClick(Sender: TObject);
    procedure BtAnteriorClick(Sender: TObject);
    procedure BtProximoClick(Sender: TObject);
    procedure BtGravarClick(Sender: TObject);
  private
    procedure AtualizaTela;
    procedure PegaCompetencias;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCadGNRE: TfrmCadGNRE;
  AnoAtual,MesAtual:integer;
implementation

uses DataModulo, ModeloPesquisa;

{$R *.dfm}

procedure TfrmCadGNRE.FormCreate(Sender: TObject);
begin
  inherited;
  Gera_Codigo:=True;
  Chave_Tabela:='COD_GNRE';
  qrUsuarioCad.Open;
  qryBancos.Open;
  qryNF.Open;
  qrUF_Cliente.Open;
  qryConvenioProtocolo.Open;
end;

procedure TfrmCadGNRE.BtInserirClick(Sender: TObject);
begin
  inherited;
  TbPrincipal.FieldByName('DATA_CADASTRO').Value:=Date;
  TbPrincipal.FieldByName('HORA_CADASTRO').Value:=Time;
  TbPrincipal.FieldByName('COD_FILIAL').Value:=dados.qrFilial.FieldByName('COD_FILIAL').Value;
  TbPrincipal.FieldByName('COD_USUARIO').Value:=dados.qrUsuario.FieldByName('COD_USUARIO').Value;
  PegaCompetencias;
  AtualizaTela;
end;

procedure TfrmCadGNRE.AtualizaTela;
begin

  qryNF.Close;
  qryNF.ParamByName('VARNUMERO').Value:=TbPrincipal.FieldByName('COD_NF').Value;
  qryNF.Open;

   if not qrUsuarioCad.Locate('COD_USUARIO',TbPrincipal.FieldByName('COD_USUARIO').Value ,[]) then
   begin
      if not qrUsuarioCad.Bof then
      begin
         qrUsuarioCad.First;
         qrUsuarioCad.Prior;
      end;
   end;

   if not qryBancos.Locate('CODIGO',TbPrincipal.FieldByName('COD_BANCO_PAGAMENTO').Value ,[]) then
   begin
      if not qryBancos.Bof then
      begin
         qryBancos.First;
         qryBancos.Prior;
      end;
   end;

   if not qryConvenioProtocolo.Locate('COD_CONVENIO_PROTOCOLO',TbPrincipal.FieldByName('CONVENIO_PROTOCOLO').Value ,[]) then
   begin
      if not qryConvenioProtocolo.Bof then
      begin
         qryConvenioProtocolo.First;
         qryConvenioProtocolo.Prior;
      end;
   end;

   qryCliente.Close;
   qryCliente.ParamByName('VARCLIENTE').Value:=qryNF.FieldByName('COD_CLIENTE').AsString;
   qryCliente.Open;
   edtCNPJ.Text:=qryCliente.FieldByname('CNPJ').AsString;
   edtCNPJ.Text:=FormataCPF_CNPJ(edtCNPJ.Text);

    if not qrUF_Cliente.Locate('COD_UF' , qryCliente.FieldByName('ENDERECO_ENTREGA_COD_UF').Value  , [] ) then
    begin
      if  NOT qrUF_Cliente.Bof then
      begin
         qrUF_Cliente.First;
         qrUF_Cliente.Prior;
      end;
    end;  

end;

procedure  TfrmCadGNRE.PegaCompetencias;
var d,m,y : word;
begin
  DecodeDate(Date,y,m,d);
  TbPrincipal.FieldByName('MES_REFERENCIA').Value :=m;
  TbPrincipal.FieldByName('ANO_REFERENCIA').Value :=y;
end;

procedure TfrmCadGNRE.dbCodBancoExit(Sender: TObject);
begin
  inherited;
  if tbPrincipal.FieldByName('COD_BANCO_PAGAMENTO').IsNotNull then
  begin
    if NOT qryBancos.Locate('CODIGO',tbPrincipal.FieldByName('COD_BANCO_PAGAMENTO').Value,[]) then
    begin
        ShowMessage('Banco não cadastrado!');
        dbCodBanco.Field.Clear;
        dbCodBanco.SetFocus;
    end;
  end;
end;

procedure TfrmCadGNRE.btnPesquisaNFClick(Sender: TObject);
begin
  inherited;
   qryNF.SQLWhere.Clear;
   frmModeloPesquisa        := TfrmModeloPesquisa.Create(Application);
   frmModeloPesquisa.Tabela := qryNF;
   frmModeloPesquisa.lblTitulo.Caption := 'Pesquisa NF';
   if frmModeloPesquisa.ShowModal = mrOk then
      TbPrincipal.FieldByName('COD_NF').Value:=frmModeloPesquisa.TbPrincipal.FieldByName('NUMERO').Value;

   qryNF.SQLWhere.Add('WHERE NUMERO=:VARNUMERO');
   frmModeloPesquisa.Destroy;
   AtualizaTela;

   Screen.OnActiveControlChange := TrocaCor;

   dbCodigoReceita.SetFocus;
end;

procedure TfrmCadGNRE.dbNFExit(Sender: TObject);
begin
  inherited;
  if TbPrincipal.ReadOnly then
    Exit;

  if TbPrincipal.FieldByName('COD_NF').IsNull then
    Exit;

  qryNF.Close;
  qryNF.ParamByName('VARNUMERO').Value:=TbPrincipal.FieldByName('COD_NF').Value;
  qryNF.Open;

  if NOT qryNF.IsEmpty then
  begin
      TbPrincipal.FieldByName('COD_NF').Value:=qryNF.FieldByName('NUMERO').Value;
      TbPrincipal.FieldByName('COD_NF').Value:=qryNF.FieldByName('NUMERO').Value;
  end
  else
  begin
    MessageDlg('Nota Fiscal não encontrada', mtWarning, [mbOK], 0);
    dbNF.Clear;
    qryNF.Close;
  end;

  AtualizaTela;
  dbCodigoReceita.SetFocus;
end;

procedure TfrmCadGNRE.BtEditarClick(Sender: TObject);
begin
  inherited;
  AtualizaTela;
end;

procedure TfrmCadGNRE.BtCancelarClick(Sender: TObject);
begin
  inherited;
  AtualizaTela;
end;

procedure TfrmCadGNRE.BtAnteriorClick(Sender: TObject);
begin
  inherited;
  AtualizaTela;

end;

procedure TfrmCadGNRE.BtProximoClick(Sender: TObject);
begin
  inherited;
  AtualizaTela;

end;

procedure TfrmCadGNRE.BtGravarClick(Sender: TObject);
begin
   if TbPrincipal.FieldByName('COD_NF').AsString = '' then
   begin
      MessageDlg('Informe o Número da NF', mtWarning, [mbOK], 0);
      dbNF.SetFocus ;
      Exit;
   end;   
   if TbPrincipal.FieldByName('VALOR_PRINCIPAL').AsString = '' then
   begin
      MessageDlg('Informe o Valor da Guia', mtWarning, [mbOK], 0);
      edtValor.SetFocus ;
      Exit;
   end;
  TbPrincipal.FieldByName('CONVENIO_PROTOCOLO').Value:=qryConvenioProtocolo.FieldbyName('COD_CONVENIO_PROTOCOLO').Value; 
  inherited;
end;

end.
