unit UnitVendedor;

interface

uses
  Windows, variants, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Grids, DBGrids, ComCtrls, Buttons, StdCtrls, Mask, DBCtrls, Db,
  DBTables, ToolWin, ActnList;

type

THackDBGrid = class(TDBGrid);

  Tfvendedor = class(TForm)
    ScrollBox: TScrollBox;
    ScrollBox1: TScrollBox;
    Bevel3: TBevel;
    Bevel2: TBevel;
    Bevel1: TBevel;
    Label19: TLabel;
    Label13: TLabel;
    Label17: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label9: TLabel;
    Label11: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label18: TLabel;
    EditCodigo: TDBEdit;
    EditNome: TDBEdit;
    EditEndereco: TDBEdit;
    EditBairro: TDBEdit;
    EditCidade: TDBEdit;
    EditCep: TDBEdit;
    EditUF: TDBEdit;
    EditFone: TDBEdit;
    EditEmail: TDBEdit;
    EditComissao: TDBEdit;
    DBEdit4: TDBEdit;
    DBEdit5: TDBEdit;
    DBEdit2: TDBEdit;
    ScrollBox2: TScrollBox;
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    Label10: TLabel;
    Edit1: TEdit;
    ToolBar2: TToolBar;
    btnprimeiro: TSpeedButton;
    btnvoltar: TSpeedButton;
    btnavancar: TSpeedButton;
    btnultimo: TSpeedButton;
    Panel6: TPanel;
    Panel7: TPanel;
    SpeedButton6: TSpeedButton;
    Panel5: TPanel;
    btngravar: TSpeedButton;
    Panel8: TPanel;
    btncancelar: TSpeedButton;
    Panel12: TPanel;
    SpeedButton21: TSpeedButton;
    ActionList1: TActionList;
    novo: TAction;
    voltar: TAction;
    avancar: TAction;
    foco: TAction;
    formatar: TAction;
    StatusBar1: TStatusBar;
    Label12: TLabel;
    Label8: TLabel;
    ajuda: TAction;
    procedure btnVoltarClick(Sender: TObject);
    procedure btnNovoClick(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure Label1Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure Label18Click(Sender: TObject);
    procedure btncancelarClick(Sender: TObject);
    procedure SpeedButton21Click(Sender: TObject);
    procedure btnprimeiroClick(Sender: TObject);
    procedure btnavancarClick(Sender: TObject);
    procedure btnultimoClick(Sender: TObject);
    procedure novoExecute(Sender: TObject);
    procedure voltarExecute(Sender: TObject);
    procedure avancarExecute(Sender: TObject);
    procedure focoExecute(Sender: TObject);
    procedure formatarExecute(Sender: TObject);
    procedure TabelaVendedorPostError(DataSet: TDataSet; E: EDatabaseError;
      var Action: TDataAction);
    procedure Label2Click(Sender: TObject);
    procedure EditCodigoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure EditNomeKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBEdit2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormActivate(Sender: TObject);
    procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Label8Click(Sender: TObject);
    procedure EditFoneExit(Sender: TObject);
    procedure EditFoneKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ajudaExecute(Sender: TObject);
  private
  procedure AppHint(Sender : TObject);
    { Private declarations }
  public
  st:string;
    { Public declarations }
  end;

var
  fvendedor: Tfvendedor;
    Valr, Valr1 : String;
implementation

uses udm;


procedure Tfvendedor.AppHint(Sender: TObject);
begin
  StatusBar1.Panels[0].Text := Application.Hint;
end;

{$R *.DFM}

procedure Tfvendedor.btnVoltarClick(Sender: TObject);
BEGIN
        fdm.tbvendedor.prior;
end;

procedure Tfvendedor.btnNovoClick(Sender: TObject);

begin
     novoexecute(self);
     end;

procedure Tfvendedor.btnGravarClick(Sender: TObject);
begin
         fdm.tbvendedor.Edit;
          fdm.tbvendedor.Post;
           btngravar.Enabled:=false;
            btncancelar.Enabled:=false;
end;

procedure Tfvendedor.Label1Click(Sender: TObject);
begin
        if inputquery('Localizar código','Entre com o código:',ST) then
          if not fdm.tbvendedor.Locate ('Codigo',ST,[]) then
           ShowMessage ('Código não cadastrado !!!');
            ST := ''

end;



procedure Tfvendedor.Edit1Change(Sender: TObject);
begin
         fdm.tbvendedor.FindNearest ([Edit1.Text]);
end;

procedure Tfvendedor.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 Tfvendedor.Label18Click(Sender: TObject);
begin
        if inputquery('Localizar Nome(Apelido)','Entre com o nome:',ST) then
          if not fdm.tbvendedor.Locate ('apel',uppercase(ST),[]) then
           ShowMessage ('Nome não cadastrado !!!');
            ST := ''

end;

procedure Tfvendedor.btncancelarClick(Sender: TObject);
begin
        fdm.tbvendedor.cancel;
          btnGravar.Enabled:=false;
           btncancelar.enabled := false;
end;

procedure Tfvendedor.SpeedButton21Click(Sender: TObject);
begin
          if Application.MessageBox('Deseja excluir este vendedor? ',
         'Confirme', 4  + MB_ICONQUESTION)= idYes then begin

       fdm.tbvendedor.delete;
          btnGravar.Enabled:=false;
           btncancelar.enabled := false;
        end;

end;

procedure Tfvendedor.btnprimeiroClick(Sender: TObject);
begin
        fdm.tbvendedor.first;
end;

procedure Tfvendedor.btnavancarClick(Sender: TObject);
begin
        fdm.tbvendedor.next;
end;

procedure Tfvendedor.btnultimoClick(Sender: TObject);
begin
        fdm.tbvendedor.last;
end;

procedure Tfvendedor.novoExecute(Sender: TObject);
begin
        btnGravar.Enabled:=true;
        btncancelar.Enabled := true;

       fdm.tbvendedor.Refresh;

        with fdm.query1 do begin
         close;
          sql.clear;
           sql.add('select max(codigo) as codigo from tbvendedor');
            open;
         end;

        fdm.tbvendedor.insert;
        if fdm.query1['codigo'] <> null then
         fdm.tbvendedor['codigo'] := fdm.query1['codigo'] + 1
        else
        fdm.tbvendedor['codigo']:='1';
        fdm.query1.close;

            editfone.setfocus;


end;

procedure Tfvendedor.voltarExecute(Sender: TObject);
begin
        btnvoltarclick(self);
end;

procedure Tfvendedor.avancarExecute(Sender: TObject);
begin
        btnavancarclick(self);
end;

procedure Tfvendedor.focoExecute(Sender: TObject);
begin
        if not edit1.Focused then
        edit1.setfocus
        else
        editfone.setfocus;
end;

procedure Tfvendedor.formatarExecute(Sender: TObject);
begin
 (fdm.tbvendedor.fieldbyname ('codigo') as tfloatfield).displayformat := '000000';
  (fdm.tbvendedor.fieldbyname ('cep') as tstringfield).editmask := '99.999-999';
   (fdm.tbvendedor.fieldbyname ('fone') as tstringfield).editmask := '(99)9999-9999';
    (fdm.tbvendedor.fieldbyname ('fone1') as tstringfield).editmask := '(99)9999-9999';
     (fdm.tbvendedor.fieldbyname ('celular') as tstringfield).editmask := '(99)9999-9999';
      (fdm.tbvendedor.fieldbyname ('uf') as tstringfield).editmask := 'aa';

end;

procedure Tfvendedor.TabelaVendedorPostError(DataSet: TDataSet;
  E: EDatabaseError; var Action: TDataAction);
begin
        Messagedlg('Este vendedor já está cadastrado, verificar !!!',mterror,[mbok],0);
                 Messagedlg('Este cliente já está cadastrado, verificar !!!',mterror,[mbok],0);
         fdm.tbvendedor.cancel;
          abort;
end;

procedure Tfvendedor.Label2Click(Sender: TObject);
begin
        if inputquery('Localizar Nome','Entre com o nome:',ST) then
          if not fdm.tbvendedor.Locate ('nome',uppercase(ST),[]) then
           ShowMessage ('Nome não cadastrado !!!');
            ST := ''
end;

procedure Tfvendedor.EditCodigoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = vk_f3 then label1click(self);
end;

procedure Tfvendedor.EditNomeKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
                if key = vk_f3 then label18click(self);
end;

procedure Tfvendedor.DBEdit2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = vk_f3 then label2click(self);
end;

procedure Tfvendedor.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
        IF btngravar.Enabled = true then begin
         case application.MessageBox('Deseja salvar as alterações?', 'Sair do cadastro de vendedor',mb_yesnocancel + MB_ICONQUESTION )of

         idyes : begin
         fdm.tbvendedor.edit;
          fdm.tbvendedor.post;
           canclose:=true;
            end;

         idno : begin
          fdm.tbvendedor.cancel;
           canclose:=true;
            end;

         idcancel :
           canclose:=false;
end;
end;
end;

procedure Tfvendedor.FormActivate(Sender: TObject);
begin
        THackDBGrid(DBGrid1).FixedCols := 2;
end;

procedure Tfvendedor.Label1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
        StatusBar1.Panels[0].Text := 'Consulta dando um clique, ou teclando F3, no campo.';
end;

procedure Tfvendedor.Label8Click(Sender: TObject);
begin
        if inputquery('Localizar telefone','Entre com o telefone:',ST) then
          if not fdm.tbvendedor.Locate ('fone',ST,[]) then
           ShowMessage ('Telefone não encontrado !!!');
            ST := ''
end;

procedure Tfvendedor.EditFoneExit(Sender: TObject);
begin
        btnGravarClick(self);
end;

procedure Tfvendedor.EditFoneKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = vk_f3 then label8click(self);
end;

procedure Tfvendedor.FormCreate(Sender: TObject);
begin
  Application.OnHint := AppHint;
end;

procedure Tfvendedor.FormClose(Sender: TObject; var Action: TCloseAction);
begin
        Application.OnHint := nil;
end;

procedure Tfvendedor.ajudaExecute(Sender: TObject);
var
  Comp: Trichedit; // * variável Comp da classe TMemo
  texto : string;
begin
  Comp := Trichedit.Create (Self);  // * cria o componente Memo
  Comp.Parent := Self;    // * exibe o componente criado

  if fileexists (extractfilepath(paramstr(0)) + '\ajuda\ajudavendedor.txt') then
   comp.lines.loadfromfile (extractfilepath(paramstr(0)) + '\ajuda\ajudavendedor.txt');

  texto := comp.Text;
  comp.Destroy;

  Showmessage (texto);


end;

end.
