unit ufuncionario;

interface

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

type
  Tffuncionario = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Panel2: TPanel;
    ScrollBox1: TScrollBox;
    ScrollBox2: TScrollBox;
    Label31: TLabel;
    Label30: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label17: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label18: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    EditData: TDBEdit;
    EditCodigo: TDBEdit;
    EditNomeFntasia: TDBEdit;
    EditNome: TDBEdit;
    EditEndereco: TDBEdit;
    EditBairro: TDBEdit;
    EditCidade: TDBEdit;
    EditCEP: TDBEdit;
    EditCGC: TDBEdit;
    EditIE: TDBEdit;
    EditFone: TDBEdit;
    DBEdit3: TDBEdit;
    EditFoneFax: TDBEdit;
    EditContato: TDBEdit;
    EditCargoContato: TDBEdit;
    DBEdit9: TDBEdit;
    ScrollBox3: TScrollBox;
    DBGrid3: TDBGrid;
    Panel4: TPanel;
    Label26: TLabel;
    Edit2: TEdit;
    Panel1: TPanel;
    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;
    SpeedButton21: TSpeedButton;
    Panel11: TPanel;
    Panel3: TPanel;
    btnimprimir: TSpeedButton;
    Panel9: TPanel;
    Panel14: TPanel;
    dsfuncionario: TDataSource;
    ActionList1: TActionList;
    novo: TAction;
    voltar: TAction;
    avancar: TAction;
    foco: TAction;
    filtro: TAction;
    formatar: TAction;
    Panel10: TPanel;
    DBGrid1: TDBGrid;
    Button1: TButton;
    SpeedButton1: TSpeedButton;
    Label1: TLabel;
    Bevel1: TBevel;
    DBGrid2: TDBGrid;
    Label8: TLabel;
    dsitem: TDataSource;
    Label6: TLabel;
    Label11: TLabel;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    QuickRep1: TQuickRep;
    QRBand1: TQRBand;
    LControle1: TQRLabel;
    QRLabel2: TQRLabel;
    QRLabel3: TQRLabel;
    QRLabel4: TQRLabel;
    QRLabel5: TQRLabel;
    QRLabel6: TQRLabel;
    QRSysData2: TQRSysData;
    QRBand2: TQRBand;
    QRDBText1: TQRDBText;
    QRDBText2: TQRDBText;
    QRDBText3: TQRDBText;
    QRDBText4: TQRDBText;
    QRDBText5: TQRDBText;
    novograde: TAction;
    StatusBar1: TStatusBar;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure Label3Click(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure Label5Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure QRCompositeReport5AddReports(Sender: TObject);
    procedure btngravarClick(Sender: TObject);
    procedure btnnovoClick(Sender: TObject);
    procedure novoExecute(Sender: TObject);
    procedure btnPrimeiroClick(Sender: TObject);
    procedure btnVoltarClick(Sender: TObject);
    procedure btnAvancarClick(Sender: TObject);
    procedure btnUltimoClick(Sender: TObject);
    procedure btncancelarClick(Sender: TObject);
    procedure SpeedButton21Click(Sender: TObject);
    procedure tbclienteAfterOpen(DataSet: TDataSet);
    procedure tbclienteBeforeEdit(DataSet: TDataSet);
    procedure voltarExecute(Sender: TObject);
    procedure avancarExecute(Sender: TObject);
    procedure focoExecute(Sender: TObject);
    procedure EditDataKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure tbclientePostError(DataSet: TDataSet; E: EDatabaseError;
      var Action: TDataAction);
    procedure EditNomeFntasiaExit(Sender: TObject);
    procedure btnimprimirClick(Sender: TObject);
    procedure EditCodigoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure EditNomeFntasiaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure PageControl1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure DBGrid2Enter(Sender: TObject);
    procedure DBGrid2Exit(Sender: TObject);
    procedure DBGrid2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Label7Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Edit2Exit(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Button4Click(Sender: TObject);
    procedure formatarExecute(Sender: TObject);
    procedure DBGrid2ColExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    FReport : TQuickRep;
    procedure SetReport(Value : TQuickRep);
  public
  i:integer;
  ST:string;
  bok : boolean;
  foc : string;
    procedure AppHint(Sender : TObject);
    property Report : TQuickRep read FReport write SetReport;
   { Private declarations }


  { Public declarations }
  end;

var
  ffuncionario: Tffuncionario;
 Texto, Texto1 : String;
implementation

uses udm;

{$R *.DFM}


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


procedure Tffuncionario.SetReport(Value : TQuickRep);
begin
  FReport:=Value;
end;

procedure Tffuncionario.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 Tffuncionario.Label3Click(Sender: TObject);

begin

        BOK := inputquery('Localizar código','Entre com o código:',ST);
         if BOK then
          if not fdm.tbfuncionario.Locate ('Codigo',ST,[]) then
           ShowMessage ('Código não cadastrado !!!');
            ST := '';

end;

procedure Tffuncionario.Label4Click(Sender: TObject);

Var
       Razao:String;
        LocCodogo : Boolean;
begin


     LocCodogo := InputQuery ('localizar', 'Entre com a Razão Social',Razao );
      if LocCodogo then begin
        if not fdm.tbfuncionario.FindKey ([Razao]) then
         ShowMessage ('Nome não encontrado');
          end;
end;

procedure Tffuncionario.Label5Click(Sender: TObject);
begin
        BOK := inputquery('Localizar nome ','Entre com o nome :',ST);
         if BOK then
          if not fdm.tbfuncionario.Locate ('NomeFantasia',uppercase(ST),[]) then
           ShowMessage ('Nome  não encontrado !!!');
            ST := '';

end;

procedure Tffuncionario.SpeedButton12Click(Sender: TObject);
begin
        fdm.tbfuncionario.Filtered := False;
end;

procedure Tffuncionario.Edit2Change(Sender: TObject);
begin
        fdm.tbfuncionario.indexfieldnames := 'nomefantasia';
         fdm.tbfuncionario.FindNearest([edit2.text]);
end;

procedure Tffuncionario.QRCompositeReport5AddReports(Sender: TObject);
    begin;
end;

procedure Tffuncionario.btngravarClick(Sender: TObject);
begin
        fdm.tbfuncionario.edit;
         fdm.tbfuncionario.post;

           btngravar.Enabled:=false;
            btncancelar.Enabled:=false;

end;

procedure Tffuncionario.btnnovoClick(Sender: TObject);

begin
     novoexecute(self);
end;




procedure Tffuncionario.novoExecute(Sender: TObject);
var
it:integer;
begin

        if foc = '1' then begin
         fdm.tbfuncionarioitem.Insert;
          dbgrid2.SelectedIndex := 0;

       end else begin

     try
      fdm.tbfuncionario.DisableControls;
    try
    fdm.tbfuncionario.last;

    if fdm.tbfuncionario['codigo']<> null then
    it := fdm.tbfuncionario['codigo']
    else
    it:= 0;

        fdm.tbfuncionario.insert;

         fdm.tbfuncionario['codigo'] := it + 1;
         fdm.tbfuncionario['admissao'] := datetostr(now);

    finally

    fdm.tbfuncionario.EnableControls;
    end;
    finally

    end;

        btnGravar.Enabled:=true;
         btncancelar.Enabled := true;
          editcodigo.setfocus;

   end;

end;

procedure Tffuncionario.btnPrimeiroClick(Sender: TObject);
begin
        fdm.tbfuncionario.first;
end;

procedure Tffuncionario.btnVoltarClick(Sender: TObject);
begin
        fdm.tbfuncionario.prior;
end;

procedure Tffuncionario.btnAvancarClick(Sender: TObject);
begin
        fdm.tbfuncionario.next;
end;

procedure Tffuncionario.btnUltimoClick(Sender: TObject);
begin
                fdm.tbfuncionario.last;
end;

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

procedure Tffuncionario.SpeedButton21Click(Sender: TObject);
begin

          if Application.MessageBox('Deseja excluir este FUNCIONÁRIO? ',
         'Confirme', 4)= idYes then begin

         fdm.tbfuncionarioitem.Last;

         while not fdm.tbfuncionarioitem.Bof do begin
          fdm.tbfuncionarioitem.delete;
           fdm.tbfuncionarioitem.Next;
            end;

        fdm.tbfuncionario.delete;
          btnGravar.Enabled:=true;
           btncancelar.enabled := true;
        end;

end;

procedure Tffuncionario.tbclienteAfterOpen(DataSet: TDataSet);
begin
        btngravar.enabled := false;
        btncancelar.enabled := false;
end;

procedure Tffuncionario.tbclienteBeforeEdit(DataSet: TDataSet);
begin
       btnGravar.Enabled:=True;
       btncancelar.enabled := true;
end;

procedure Tffuncionario.voltarExecute(Sender: TObject);
begin
        fdm.tbfuncionario.prior;
end;

procedure Tffuncionario.avancarExecute(Sender: TObject);
begin
        fdm.tbfuncionario.next;
end;

procedure Tffuncionario.focoExecute(Sender: TObject);
begin
        i := i + 1;

        if i >= 4 then i:=1;

        case i of

        1: editdata.setfocus;
        2: dbgrid2.setfocus;
        3: edit2.setfocus;
        end;


end;

procedure Tffuncionario.EditDataKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = 27 then dbgrid3.setfocus;
end;

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

procedure Tffuncionario.EditNomeFntasiaExit(Sender: TObject);
begin
        if length(EditNomeFntasia.text) < 1 then begin
         showmessage('preencha o nome!!!');
           end;
end;

procedure Tffuncionario.btnimprimirClick(Sender: TObject);
begin

         quickrep1.PreviewModal;
end;

procedure Tffuncionario.EditCodigoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = vk_f1 then
         label3click(self);
end;

procedure Tffuncionario.EditNomeFntasiaKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = vk_f1 then
         label5click(self);
end;

procedure Tffuncionario.PageControl1Change(Sender: TObject);
begin
        if pagecontrol1.ActivePageIndex = 0 then
         fdm.tbfuncionario.Filtered := false;
end;

procedure Tffuncionario.Button1Click(Sender: TObject);
var
st : string;

begin
        if inputquery('Filtrar Bairro','Entre com o nome do Bairro:',st) then begin

        fdm.tbfuncionario.Filtered := false;
         fdm.tbfuncionario.Filter:= 'bairro='''+ st + '''';
          fdm.tbfuncionario.Filtered := true;
           end;

end;

procedure Tffuncionario.SpeedButton1Click(Sender: TObject);
begin
           quickrep1.PreviewModal;

end;

procedure Tffuncionario.DBGrid2Enter(Sender: TObject);
begin
        btngravarclick(self);
        keypreview :=false;
        foc := '1';
end;

procedure Tffuncionario.DBGrid2Exit(Sender: TObject);
begin
        keypreview := true;
        foc := '0';
end;

procedure Tffuncionario.DBGrid2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
        if key = 13 then
         dbgrid2.SelectedIndex :=  dbgrid2.SelectedIndex + 1;

        if key = 27 then
         dbgrid2.SelectedIndex :=  dbgrid2.SelectedIndex   -1;

end;

procedure Tffuncionario.Label7Click(Sender: TObject);
begin
        BOK := inputquery('Localizar RG','Entre com o RG:',ST);
         if BOK then
          if not fdm.tbfuncionario.Locate ('rg',ST,[]) then
           ShowMessage ('RG não localizado !!!');
            ST := '';
end;

procedure Tffuncionario.Button2Click(Sender: TObject);
var
st : string;

begin
        if inputquery('Filtrar CEP','Entre com o Cep:',st) then begin

        fdm.tbfuncionario.Filtered := false;
         fdm.tbfuncionario.Filter:= 'cep='''+ st + '''';
          fdm.tbfuncionario.Filtered := true;
           end;

end;

procedure Tffuncionario.Button3Click(Sender: TObject);
var
st, st1 : string;

begin
        if inputquery('Filtrar Data','Entre com a data inicial:',st) then begin

        if inputquery('Filtrar Data','Entre com a data final:',st1) then begin

        fdm.tbfuncionario.Filtered := false;
         fdm.tbfuncionario.Filter:= 'admissao>='''+ st + ''' and admissao<='''+ st1 + '''';
          fdm.tbfuncionario.Filtered := true;
           end;
            end;

end;

procedure Tffuncionario.Edit2Exit(Sender: TObject);
begin
        fdm.tbfuncionario.indexfieldnames := 'chave';
end;

procedure Tffuncionario.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin

        IF btngravar.Enabled = true then begin
         case application.MessageBox('Deseja salvar as alterações?', 'Sair de Cadastro de Cliente',mb_yesnocancel + MB_ICONQUESTION )of

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

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

         idcancel :
           canclose:=false;
end;
end;


end;

procedure Tffuncionario.Button4Click(Sender: TObject);
begin
        fdm.tbfuncionario.Filtered := false;
end;

procedure Tffuncionario.formatarExecute(Sender: TObject);
begin
   (fdm.tbfuncionario.fieldbyname ('data') as tdatetimefield).editmask := '99/99/9999';
   (fdm.tbfuncionario.fieldbyname ('codigo') as tfloatfield).displayformat := '000000';
   (fdm.tbfuncionario.fieldbyname ('fone') as tstringfield).editmask := '(99)9999-9999';
   (fdm.tbfuncionario.fieldbyname ('cep') as tstringfield).editmask := '99.999-999';
   (fdm.tbfuncionario.fieldbyname ('Salario') as tfloatfield).displayformat := '###,##0.00';
   (fdm.tbfuncionario.fieldbyname ('HoraEntrada') as tdatetimefield).editmask := '99:99';
   (fdm.tbfuncionario.fieldbyname ('Horasaida') as tdatetimefield).editmask := '99:99';
   (fdm.tbfuncionario.fieldbyname ('Demissao') as tdatetimefield).editmask := '99/99/9999';
   (fdm.tbfuncionarioitem.fieldbyname ('mes_ano') as tdatetimefield).editmask := '99/99/9999';
   (fdm.tbfuncionarioitem.fieldbyname ('salario') as tfloatfield).displayformat := '###,##0.00';
   (fdm.tbfuncionarioitem.fieldbyname ('extra') as tfloatfield).displayformat := '###,##0.00';
   (fdm.tbfuncionarioitem.fieldbyname ('desconto') as tfloatfield).displayformat := '###,##0.00';
   (fdm.tbfuncionarioitem.fieldbyname ('total') as tfloatfield).displayformat := '###,##0.00';
end;
                
procedure Tffuncionario.DBGrid2ColExit(Sender: TObject);
begin
       fdm.tbfuncionarioitem.Edit;
       if dbgrid2.SelectedIndex <> 0 then
        fdm.tbfuncionarioitem.FieldByName('total').asfloat := fdm.tbfuncionarioitem.FieldByName('salario').asfloat +
         fdm.tbfuncionarioitem.FieldByName('extra').asfloat - fdm.tbfuncionarioitem.FieldByName('desconto').asfloat;

end;

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

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

end.
