unit CadCFOP;
                                                                                                      
interface

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

type
  TfrmCadCFOP = class(TFrModeloCadastro)
    edtDescricao: TIB_Edit;
    GroupBox2: TGroupBox;
    edtCodigo: TIB_Edit;
    IB_RadioGroup1: TIB_RadioGroup;
    IB_RadioGroup2: TIB_RadioGroup;
    IB_Edit1: TIB_Edit;
    dtCST: TIB_DataSource;
    qryCST: TIB_Query;
    btnPesquisaCST: TBitBtn;
    btnAdicionaCST: TBitBtn;
    IB_Edit2: TIB_Edit;
    procedure BtGravarClick(Sender: TObject);
    procedure BtInserirClick(Sender: TObject);
    procedure BtExcluirClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtCancelarClick(Sender: TObject);
    procedure BtAnteriorClick(Sender: TObject);
    procedure BtProximoClick(Sender: TObject);
    procedure BtEditarClick(Sender: TObject);
    procedure btnPesquisaCSTClick(Sender: TObject);
  private
    procedure AtualizaTela;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCadCFOP: TfrmCadCFOP;

implementation

uses DataModulo, ModeloPesquisa;

{$R *.dfm}

procedure TfrmCadCFOP.BtGravarClick(Sender: TObject);
begin
   if  edtCodigo. Text = '' then
   begin
      MessageDlg('Informe o código', mtWarning, [mbOK], 0);
      edtCodigo.SetFocus;
      Exit;
   end;

   if edtDescricao.Text = '' then
   begin
      MessageDlg('Informe a descrição', mtWarning, [mbOK], 0);
      edtDescricao.SetFocus;
      Exit;
   end;

   TbPrincipal.FieldByName('COD_CST').Value:=qryCST.FieldByName('CODIGO').Value;

   if TbPrincipal.State in [DSSINSERT] then
   begin
      descricao_auditoria := 'CADASTROU O CFOP ' + TbPrincipal.FieldByName('COD_CFOP').AsString + ' ' + TbPrincipal.FieldByName('DESCRICAO').AsString;
   end;

   if TbPrincipal.State in [DSSEDIT] then
   begin
      descricao_auditoria := 'ALTEROU O CFOP ' + TbPrincipal.FieldByName('COD_CFOP').OldAsString + ' ' + TbPrincipal.FieldByName('DESCRICAO').OldAsString;
   end;

  inherited;
  AtualizaTela;
end;

procedure TfrmCadCFOP.BtInserirClick(Sender: TObject);
begin
   inherited;
   try
      edtCodigo.SetFocus;
      except
   end;
  AtualizaTela;
end;

procedure TfrmCadCFOP.BtExcluirClick(Sender: TObject);
begin
   descricao_auditoria := 'EXCLUIU O CFOP ' + TbPrincipal.FieldByName('COD_CFOP').AsString + ' ' + TbPrincipal.FieldByName('DESCRICAO').AsString;
   inherited;

end;

procedure TfrmCadCFOP.FormCreate(Sender: TObject);
begin
   Chave_Tabela := 'COD_CFOP';
   Descricao_Unica := True;
   Campo_Tabela    := 'DESCRICAO';
   inherited;

   qryCST.Open;

end;

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

end;

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

end;

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

end;

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

end;

procedure TfrmCadCFOP.AtualizaTela;
begin
  if not qryCST.Locate('CODIGO',TbPrincipal.FieldByName('COD_CST').Value ,[]) then
  begin
    if not qryCST.Bof then
    begin
       qryCST.First;
       qryCST.Prior;
    end;
  end;
end;

procedure TfrmCadCFOP.btnPesquisaCSTClick(Sender: TObject);
begin
  inherited;
   frmModeloPesquisa        := TfrmModeloPesquisa.Create(Application);
   frmModeloPesquisa.Tabela := qryCST;
   frmModeloPesquisa.lblTitulo.Caption := 'Pesquisa CST';
   if frmModeloPesquisa.ShowModal = mrOk then
   begin
      qryCST.Locate('CODIGO' , frmModeloPesquisa.TbPrincipal.FieldByName('CODIGO').Value , []);
   end;
   frmModeloPesquisa.Destroy;

   Screen.OnActiveControlChange := TrocaCor;

end;

end.
