unit CNAESegmento;

interface

uses
  Windows, system.UItypes, IB_Access,  Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ModeloCadastro, IB_Controls, StdCtrls, Mask, UCBase, ImgList,
  AppEvnts, IB_Components, ExtCtrls, jpeg, Grids, IB_Grid, Buttons,
  ComCtrls,  IBODataset, ppDBPipe, ppParameter, ppBands, ppClass,
  ppCtrls, ppVar, ppPrnabl, ppCache, ppComm, ppRelatv, ppProd, ppReport, Rotinas,
  ppDesignLayer, Data.DB, ppDB, IB_EditButton, System.ImageList;

type
  TfrmCNAESegmento = class(TFrModeloCadastro)
    dbDescricao: TIB_Edit;
    grp1: TGroupBox;
    dbCodigo: TIB_Text;
    btnImprimir: TBitBtn;
    pgCNAEs: TPageControl;
    tabVisualizaCidades: TTabSheet;
    pnlManutencaoVencimento: TPanel;
    btnExcluiCNAE: TBitBtn;
    btnNovoCNAE: TBitBtn;
    tabEditaCNAEs: TTabSheet;
    edtCNAECodigo: TLabeledEdit;
    btnPesquisa: TBitBtn;
    grdCNAEs: TIB_Grid;
    qryCNAESegmento: TIB_Query;
    dtCNAESegmento: TIB_DataSource;
    ppDBPipeline1: TppDBPipeline;
    RelSegmentos: TppReport;
    ds1: TDataSource;
    qrRelSegmentos: TIBOQuery;
    ppHeaderBand1: TppHeaderBand;
    ppDetailBand1: TppDetailBand;
    ppFooterBand1: TppFooterBand;
    ppLabel1: TppLabel;
    ppGroup1: TppGroup;
    ppGroupHeaderBand1: TppGroupHeaderBand;
    ppGroupFooterBand1: TppGroupFooterBand;
    ppDBText1: TppDBText;
    ppDBText2: TppDBText;
    ppDBText3: TppDBText;
    ppDBText4: TppDBText;
    ppLine1: TppLine;
    ppShape1: TppShape;
    btn2: TBitBtn;
    qrCnae: TIB_Query;
    dsrCnae: TIB_DataSource;
    IB_Edit1: TIB_Edit;
    btnSalvaItem: TBitBtn;
    btnCancelaItem: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure btnCancelaItemClick(Sender: TObject);
    procedure btnSalvaItemClick(Sender: TObject);
    procedure btnNovoCNAEClick(Sender: TObject);
    procedure btnExcluiCNAEClick(Sender: TObject);
    procedure AtualizaTela;
    procedure BtInserirClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnEditar2Click(Sender: TObject);
    procedure BtCancelarClick(Sender: TObject);
    procedure BtGravarClick(Sender: TObject);
    procedure BtAnteriorClick(Sender: TObject);
    procedure BtProximoClick(Sender: TObject);
    procedure btnImprimirClick(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btnPesquisaClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCNAESegmento: TfrmCNAESegmento;

implementation

uses ModeloPesquisa, DataModulo, CNAE;

{$R *.dfm}

procedure TfrmCNAESegmento.FormCreate(Sender: TObject);
begin
  Gera_codigo:=True;
  Nome_Tabela:='SEGMENTOS';
  inherited;
  InicioDaTabela(qrCnae);
  AtualizaTela;
end;

procedure TfrmCNAESegmento.btnCancelaItemClick(Sender: TObject);
begin
  inherited;
  qryCNAESegmento.Cancel;
  qryCNAESegmento.RefreshAll;
  AtualizaTela;
  pgCNAEs.ActivePage := tabVisualizaCidades;
end;

procedure TfrmCNAESegmento.btnSalvaItemClick(Sender: TObject);
begin
  inherited;
  tbPrincipal.PostRetaining;
  qryCNAESegmento.FieldByName('COD_CNAE').Value:=qrCnae.FieldByName('COD_CNAE').Value;
  qryCNAESegmento.Post;
  qryCNAESegmento.RefreshAll;
  AtualizaTela;
  pgCNAEs.ActivePage := tabVisualizaCidades;
  btnNovoCNAE.SetFocus;
end;

procedure TfrmCNAESegmento.btnNovoCNAEClick(Sender: TObject);
begin
  inherited;

   pgCNAES.ActivePage := tabEditaCNAEs;
   qryCNAESegmento.Insert;
   qryCNAESegmento.FieldByName('COD_SEGMENTO').Value:=tbPrincipal.FieldByName('COD_SEGMENTO').Value;
   qryCNAESegmento.FieldByName('CODIGO').Value:= ProximoCodigoChave('SEGMENTOS_CNAE' , 'CODIGO', '');

end;

procedure TfrmCNAESegmento.btnExcluiCNAEClick(Sender: TObject);
begin
  inherited;
  if (MessageDlg('Confirma a Exclusão?', mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
    qryCNAESegmento.Delete;

end;

procedure TfrmCNAESegmento.AtualizaTela;
begin
  qryCNAESegmento.Close;
  qryCNAESegmento.ParamByName('varSEGMENTO').Value:=TbPrincipal.fieldbyName('COD_SEGMENTO').Value;
  qryCNAESegmento.Open;

   {============= Realiza a atualizacao das Cidades
       no caso de edicao e navegacao nos resgistros ============}
  if ((TbPrincipal.State in [DssInsert]) or (TbPrincipal.State in [DssEdit]))  then
  begin
    btnNovoCNAE.Visible:=BtGravar.Visible;
    btnExcluiCNAE.Visible:=BtGravar.Visible;
  end;
 btnImprimir.Visible:=btnEditar2.Visible;

end;

procedure TfrmCNAESegmento.BtInserirClick(Sender: TObject);
begin
  inherited;
  AtualizaTela;

end;

procedure TfrmCNAESegmento.FormShow(Sender: TObject);
begin
  inherited;
  pgCNAEs.ActivePage := tabVisualizaCidades;
end;

procedure TfrmCNAESegmento.btnEditar2Click(Sender: TObject);
begin
  inherited;
  AtualizaTela;
end;

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

procedure TfrmCNAESegmento.BtGravarClick(Sender: TObject);
begin
  inherited;
  AtualizaTela;

end;

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

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

end;

procedure TfrmCNAESegmento.btnImprimirClick(Sender: TObject);
begin
  inherited;
  qrRelSegmentos.Close;
  qrRelSegmentos.ParamByName('varSEGMENTO').Value:=TbPrincipal.fieldbyName('COD_SEGMENTO').Value;
  qrRelSegmentos.Open;

  ImpressaoBaseadaNoModelo(RelSegmentos,False,'Segmento Empresarial','')
end;

procedure TfrmCNAESegmento.btn2Click(Sender: TObject);
begin
  inherited;
  frmCNAE := TfrmCNAE.Create(Application);
  frmCNAE.OutraTela := True;
  frmCNAE.BtInserirClick(Self);
  frmCNAE.ShowModal;

   //============== Localiza o Novo CNAE ============

  tbPrincipal.FieldByName('COD_CNAE').Value:=frmCNAE.TbPrincipal.FieldByName('COD_CNAE').Value;
  frmCNAE.Destroy;
  qrCnae.RefreshAll;
  AtualizaTela;
  Screen.OnActiveControlChange := TrocaCor;
end;

procedure TfrmCNAESegmento.btnPesquisaClick(Sender: TObject);
begin
  inherited;
   //==================== Pesquisa CNAES ============
   frmModeloPesquisa        := TfrmModeloPesquisa.Create(Application);
   frmModeloPesquisa.Tabela := qrCnae;
   frmModeloPesquisa.lbltitulo.Caption := 'Pesquisa Atividades Empresariais';
   if frmModeloPesquisa.ShowModal = mrok then
   begin
      qrCnae.Locate('COD_CNAE', frmModeloPesquisa.TbPrincipal.FieldByName('COD_CNAE').Value ,[]);
      edtCNAECodigo.Text := frmModeloPesquisa.TbPrincipal.FieldByName('COD_CNAE').AsString;
   end;
   frmModeloPesquisa.Destroy;
   Screen.OnActiveControlChange := TrocaCor;

end;

end.
