unit uvalor;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, DBCtrls, DB, Buttons, ExtCtrls, ComCtrls,
  ToolWin, DBTables;

type
  TFCadastro = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    DSValores: TDataSource;
    TabSheet3: TTabSheet;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    DBEdit3: TDBEdit;
    DBEdit4: TDBEdit;
    DBEdit5: TDBEdit;
    DBEdit6: TDBEdit;
    DBMemo2: TDBMemo;
    Label7: TLabel;
    DSCLiente: TDataSource;
    Label19: TLabel;
    DBEdit17: TDBEdit;
    Label20: TLabel;
    DBEdit18: TDBEdit;
    Label21: TLabel;
    DBEdit19: TDBEdit;
    Label22: TLabel;
    DBEdit20: TDBEdit;
    ToolBar2: TToolBar;
    btnPrimeiro: TSpeedButton;
    btnVoltar: TSpeedButton;
    btnAvancar: TSpeedButton;
    btnUltimo: TSpeedButton;
    Panel6: TPanel;
    Panel7: TPanel;
    btnnovo: TSpeedButton;
    Panel5: TPanel;
    btngravar: TSpeedButton;
    Panel8: TPanel;
    btncancelar: TSpeedButton;
    Panel12: TPanel;
    Panel4: TPanel;
    Panel9: TPanel;
    SpeedButton21: TSpeedButton;
    Panel14: TPanel;
    Panel11: TPanel;
    ScrollBox1: TScrollBox;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    EditNome: TDBEdit;
    EDitData: TDBEdit;
    EDitFantasia: TDBEdit;
    EditCGC: TDBEdit;
    EditIE: TDBEdit;
    EDitEndereco: TDBEdit;
    EditBairro: TDBEdit;
    EDitCidade: TDBEdit;
    EditUf: TDBEdit;
    EditCep: TDBEdit;
    EditCodigo: TDBEdit;
    Label23: TLabel;
    SpeedButton4: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    GroupBox3: TGroupBox;
    EdiCompleta: TDBEdit;
    EDitExterna: TDBEdit;
    EditInterna: TDBEdit;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    GroupBox2: TGroupBox;
    EditMotor: TDBEdit;
    EditCera: TDBEdit;
    Label27: TLabel;
    Label28: TLabel;
    TabSheet4: TTabSheet;
    GroupBox4: TGroupBox;
    MemoComentario1: TDBMemo;
    Label8: TLabel;
    MemoComentario2: TDBMemo;
    Label29: TLabel;
    MemoComentario3: TDBMemo;
    Label30: TLabel;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    TbAux: TTable;
    EditFone: TDBEdit;
    EditFax: TDBEdit;
    Label31: TLabel;
    Label32: TLabel;
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure btnPrimeiroClick(Sender: TObject);
    procedure btnVoltarClick(Sender: TObject);
    procedure btnAvancarClick(Sender: TObject);
    procedure btnUltimoClick(Sender: TObject);
    procedure btnnovoClick(Sender: TObject);
    procedure btngravarClick(Sender: TObject);
    procedure SpeedButton21Click(Sender: TObject);
    procedure btncancelarClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure DBMemo2Enter(Sender: TObject);
    procedure DBMemo2Exit(Sender: TObject);
    procedure EDitFantasiaExit(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FCadastro: TFCadastro;

implementation

uses uservico, UDataModulo;

{$R *.dfm}

procedure TFCadastro.SpeedButton1Click(Sender: TObject);
begin
        modulo.TBValores.edit;
        modulo.TBValores.post;
end;

procedure TFCadastro.SpeedButton2Click(Sender: TObject);
begin
        close;
end;

procedure TFCadastro.btnPrimeiroClick(Sender: TObject);
begin
    modulo.TBCliente.First;
    BtnPrimeiro.Enabled:=False;
    BtnVoltar.Enabled:=False;
    if modulo.TBCliente.eof  then
    begin
      BtnUltimo.Enabled:=False;
      BtnAvancar.Enabled:=False;
    end
    else
    begin
      BtnUltimo.Enabled:=True;
      BtnAvancar.Enabled:=True;
    end;

end;

procedure TFCadastro.btnVoltarClick(Sender: TObject);
begin
   modulo.TBCliente.Prior;
   if modulo.TBCliente.bof  then
   begin
     BtnPrimeiro.Enabled:=False;
     BtnVoltar.Enabled:=False;
   end;
   if not modulo.TBCliente.Eof then
   begin
      BtnUltimo.Enabled:=True;
      BtnAvancar.Enabled:=True;
   end;

end;

procedure TFCadastro.btnAvancarClick(Sender: TObject);
begin
   modulo.TBCliente.next;
   if modulo.TBCliente.eof  then
   begin
    BtnUltimo.Enabled:=False;
    BtnAvancar.Enabled:=False;
   end;
   if not modulo.TBCliente.Bof then
   begin
      BtnPrimeiro.Enabled:=True;
      BtnVoltar.Enabled:=True;
   end;


end;

procedure TFCadastro.btnUltimoClick(Sender: TObject);
begin
    modulo.TBCliente.last;

    BtnUltimo.Enabled:=False;
    BtnAvancar.Enabled:=False;
    if modulo.TBCliente.bof  then
    begin
      BtnPrimeiro.Enabled:=False;
      BtnVoltar.Enabled:=False;
    end
    else
    begin
      BtnPrimeiro.Enabled:=True;
      BtnVoltar.Enabled:=True;
    end;



end;

procedure TFCadastro.btnnovoClick(Sender: TObject);
begin
  btnGravar.Enabled:=true;
  btncancelar.Enabled := true;
  modulo.TBCliente.Refresh;

  with modulo.query1 do
  begin
    close;
    sql.clear;
    sql.add('select max(CodCliente) as codigo from tbcliente');
    open;
  end;
  modulo.TBCliente.insert;
  if modulo.query1['codigo'] <> null then
  begin
    modulo.TBCliente['CodCliente'] := modulo.query1['codigo'] + 1;
    EditData.Text:=DatetoStr(now);
    EditNome.SetFocus;
    BtnPrimeiro.Enabled:=True;
    BtnVoltar.Enabled:=True;
    BtnUltimo.Enabled:=True;
    BtnAvancar.Enabled:=True;
  end
  else
  begin
    modulo.TBCliente['CodCliente']:='1';
    EditData.Text:=DatetoStr(now);
    EditNome.SetFocus;
  end;
    modulo.query1.close;
end;

procedure TFCadastro.btngravarClick(Sender: TObject);
begin
         modulo.TBCliente.Edit;
         modulo.TBCliente.Post;
         btngravar.Enabled:=false;
         btncancelar.Enabled:=false;

end;

procedure TFCadastro.SpeedButton21Click(Sender: TObject);
begin
 if modulo.TBCliente['CodCliente'] <> null then
 begin
   if Application.MessageBox('Deseja excluir o Registro deste Cliente? ',
                              'Confirme', 4)= idYes then
     modulo.TBCliente.delete;
     btnCancelar.Enabled:=False;
     BtnGravar.Enabled:=False;
 end
 else
   ShowMessage('Não existe nenhum dado a ser excluido.');

end;

procedure TFCadastro.btncancelarClick(Sender: TObject);
begin
        modulo.TBCliente.cancel;
        btnGravar.Enabled:=false;
        btncancelar.enabled := false;
end;

procedure TFCadastro.FormKeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then begin
  key := #0;
       Perform(WM_nextdlgctl,0,0);
 end Else if Key = #27 then
   Perform(WM_nextdlgctl,1,0)
end;

procedure TFCadastro.FormCreate(Sender: TObject);
begin
  modulo.TBCliente.Open;
  modulo.TBCliente.Refresh;
  modulo.TBCliente.Last;

  if modulo.TBCliente['CodCliente']>=2 then
  begin
    BtnPrimeiro.Enabled:=True;
    BtnVoltar.Enabled:=True;
    BtnUltimo.Enabled:=True;
    BtnAvancar.Enabled:=True;
  end;

end;

procedure TFCadastro.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  modulo.TBCliente.Edit;
  modulo.TBCliente.Post;
  modulo.TBCliente.Refresh;
  modulo.TBCliente.Close;
  modulo.TBValores.Edit;
  modulo.TBValores.Post;
  modulo.TBValores.Refresh;
  
end;

procedure TFCadastro.DBMemo2Enter(Sender: TObject);
begin
  FCadastro.KeyPreview:=False;
end;

procedure TFCadastro.DBMemo2Exit(Sender: TObject);
begin
   FCadastro.KeyPreview:=True;
end;

procedure TFCadastro.EDitFantasiaExit(Sender: TObject);
begin
  if EditFantasia.Text='' then
  begin
    Application.MessageBox('O campo Nome não pode ser nulo.','Atenção',0+32);
    EditFantasia.SetFocus;
    Abort;
  end;
  TbAux.open;
  if Modulo.TbCliente.State = dsInsert then
    if TbAux.Locate('NomeFantasia',Modulo.TbClienteNomeFantasia.Value,[]) then
    begin
      ShowMessage('Já existe um Cliente com este Nome.');
      EditFantasia.SetFocus;
    end;
  TbAux.Close;
end;

procedure TFCadastro.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case key of
    VK_Insert: BtnNovoClick(self);
    VK_F2: BtnGravarClick(self);
    VK_Prior: BtnVoltarClick(self);
    VK_Home: BtnPrimeiroClick(self);
    VK_Next: BtnAvancarClick(self);
    VK_End: BtnUltimoClick(self);
  end;
end;

end.
