unit UPrincipal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, DBCtrls, DB, ADODB, Buttons, StdCtrls, Vcl.ComCtrls, JvExComCtrls, JvDateTimePicker, JvDBDateTimePicker,
  Vcl.Mask, JvExMask, JvToolEdit, JvMaskEdit, JvCheckedMaskEdit,
  JvDatePickerEdit, JvDBDatePickerEdit, JvDBControls;

type
  TForm1 = class(TForm)
    TEmpleados: TADOTable;
    DSEmpleados: TDataSource;
    TPuestos: TADOTable;
    DSPuestos: TDataSource;
    TProgramas: TADOTable;
    DSProgramas: TDataSource;
    TAreas: TADOTable;
    DSAreas: TDataSource;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    Label3: TLabel;
    DBEdit3: TDBEdit;
    Label4: TLabel;
    DBEdit4: TDBEdit;
    Label5: TLabel;
    Label6: TLabel;
    DBEdit5: TDBEdit;
    DBComboBox1: TDBComboBox;
    Label7: TLabel;
    DBEdit6: TDBEdit;
    Label8: TLabel;
    DBEdit7: TDBEdit;
    Label9: TLabel;
    DBEdit8: TDBEdit;
    Label10: TLabel;
    DBEdit9: TDBEdit;
    Label11: TLabel;
    DBLookupComboBox1: TDBLookupComboBox;
    DBLookupComboBox2: TDBLookupComboBox;
    Label12: TLabel;
    Label13: TLabel;
    DBLookupComboBox3: TDBLookupComboBox;
    Label14: TLabel;
    Label15: TLabel;
    DBEdit10: TDBEdit;
    Label16: TLabel;
    TtipoEmpleo: TADOTable;
    DSTipoEmpleo: TDataSource;
    DBLookupComboBox4: TDBLookupComboBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Label17: TLabel;
    DBLookupComboBox5: TDBLookupComboBox;
    TCiudades: TADOTable;
    DSCiudades: TDataSource;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    BitBtn3: TBitBtn;
    Label18: TLabel;
    DBLookupComboBox6: TDBLookupComboBox;
    TNacionalidad: TADOTable;
    DSNacionalidad: TDataSource;
    Label19: TLabel;
    DBComboBox2: TDBComboBox;
    Label20: TLabel;
    SpeedButton6: TSpeedButton;
    DBCheckBox1: TDBCheckBox;
    Label21: TLabel;
    Bevel1: TBevel;
    DBEdit11: TDBEdit;
    Label24: TLabel;
    DBEdit13: TDBEdit;
    Label25: TLabel;
    DBEdit14: TDBEdit;
    Label26: TLabel;
    DBEdit15: TDBEdit;
    Label27: TLabel;
    DBEdit16: TDBEdit;
    DBCheckBox2: TDBCheckBox;
    Label22: TLabel;
    DBEdit12: TDBEdit;
    BitBtn4: TBitBtn;
    Label23: TLabel;
    DBEdit17: TDBEdit;
    Label28: TLabel;
    DBEdit18: TDBEdit;
    JvDBDatePickerEdit1: TJvDBDatePickerEdit;
    JvDBDatePickerEdit2: TJvDBDatePickerEdit;
    BitBtn6: TBitBtn;
    Bevel2: TBevel;
    Label29: TLabel;
    DBEdit19: TDBEdit;
    Label30: TLabel;
    Label31: TLabel;
    JvDBMaskEdit1: TJvDBMaskEdit;
    JvDBMaskEdit2: TJvDBMaskEdit;
    Label32: TLabel;
    DBEdit20: TDBEdit;
    Panel1: TPanel;
    btnPrimero: TBitBtn;
    btnAtras: TBitBtn;
    BtnSiguiente: TBitBtn;
    BtnUltimo: TBitBtn;
    BtnNuevo: TBitBtn;
    BtnEditar: TBitBtn;
    BtnGuardar: TBitBtn;
    BtnCancelar: TBitBtn;
    BtnEliminar: TBitBtn;
    BitBtn5: TBitBtn;
    btnFechas: TBitBtn;
    BitBtn7: TBitBtn;
    SpeedButton7: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BitBtn6Click(Sender: TObject);
    procedure btnPrimeroClick(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure DSEmpleadosStateChange(Sender: TObject);
    procedure btnFechasClick(Sender: TObject);
    procedure DSEmpleadosDataChange(Sender: TObject; Field: TField);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure BitBtn7Click(Sender: TObject);
    procedure SpeedButton7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Udm, UReporte, Uconexion, UCatalogos, UExportaciones, UBuscar, UCatus,
  UFechasEmp, UHistorialCon, UBuscarDir;

{$R *.dfm}
Procedure DBControles(Accion : Byte; Tabla:TADOTable; primero: TBitBtn; anterior: TBitBtn;
siguiente: TBitBtn;ultimo: TBitBtn; nuevo: TBitBtn; editar: TBitBtn; eliminar: TBitBtn;
guardar: TBitBtn; cancelar: TBitBtn);
begin
  if Accion = 1 then
  begin
    if tabla.State in [dsinsert, dsedit] then
     begin
      Tabla.Cancel;
      Accion:= 9;
     end;
     Tabla.First;
     primero.Enabled:= false;
     anterior.Enabled:= false;
     siguiente.Enabled:= true;
     ultimo.Enabled:= true;
  end;
  if Accion = 2 then
  begin
  if tabla.State in [dsinsert, dsedit] then
     begin
      Tabla.Cancel;
      Accion:= 9;
     end;
    if Tabla.Bof then
    begin
       primero.Enabled:= false;
       anterior.Enabled:= false;
       siguiente.Enabled:= true;
       ultimo.Enabled:= true;
    end
    else
    begin
     Tabla.Prior;
     primero.Enabled:= true;
     anterior.Enabled:= true;
     siguiente.Enabled:= true;
     ultimo.Enabled:= true;
    end;
  end;

   if Accion = 3 then
   begin
   if tabla.State in [dsinsert, dsedit] then
     begin
      Tabla.Cancel;
      Accion:= 9;
     end;
      if (tabla.Eof) then
      begin
       primero.Enabled:= true;
       anterior.Enabled:= true;
       siguiente.Enabled:= false;
       ultimo.Enabled:= false;
      end
     else
      begin
       tabla.next;
       primero.Enabled:= true;
       anterior.Enabled:= true;
       siguiente.Enabled:= true;
       ultimo.Enabled:= true;
      end;
   end;

   if Accion = 4 then
   begin
   if tabla.State in [dsinsert, dsedit] then
     begin
      Tabla.Cancel;
      Accion:= 9;
     end;
      tabla.Last;
      primero.Enabled:= true;
      anterior.Enabled:= true;
      siguiente.Enabled:= false;
      ultimo.Enabled:= false;
   end;


     if Accion = 5 then
     begin
      tabla.Insert;
      editar.Enabled:=false;
      nuevo.Enabled:= false;
      eliminar.Enabled:= false;
      Cancelar.Enabled:= true;
      Guardar.Enabled:= true;
     end;
     if Accion = 6 then
     begin
      tabla.Edit;
      editar.Enabled:=false;
      nuevo.Enabled:= false;
      eliminar.Enabled:= false;
      Cancelar.Enabled:= true;
      Guardar.Enabled:= true;
     end;

     if Accion = 7 then
     begin
      if tabla.RecordCount > 0 then
       begin
         if messagedlg('Está Seguro de eliminar',mtconfirmation,[mbyes, mbno],0) = mryes then
         tabla.Delete;
       end
       else
       begin
          Eliminar.Enabled:= false;
          messagedlg('La tabla está vacía', mterror, [mbok],0)
       end;
     end;

     if Accion = 8 then
     begin
      if tabla.State in [dsinsert, dsedit] then
      tabla.post;
      editar.Enabled:=true;
      nuevo.Enabled:= true;
      eliminar.Enabled:= true;
      Cancelar.Enabled:= false;
      Guardar.Enabled:= false;
     end;
     if Accion = 9 then
     begin
      if tabla.State in [dsinsert, dsedit] then
      tabla.cancel;
      editar.Enabled:=true;
      nuevo.Enabled:= true;
      eliminar.Enabled:= true;
      Cancelar.Enabled:= false;
      Guardar.Enabled:= false;
     end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
 //InitButtons(DBNavigator1, 'Arial');
 TPuestos.Active:=true;
 TEmpleados.Active:= true;
 TProgramas.Active:= true;
 TCiudades.Active:= true;
 TAreas.Active:= true;
 TtipoEmpleo.Active:= true;
 TNacionalidad.Active:= true;
 end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key=VK_F2) then
  Bitbtn6.OnClick(Bitbtn6);
  if (Key=VK_F3) and (TEmpleados.State in [dsinsert, dsedit]) then
  SpeedButton7.OnClick(SpeedButton7);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 FReporte.ShowModal
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  with FCatalogos do
  begin
    DBEdit2.Visible:= true;
    Label2.Visible:= true;
    Label3.Visible:= true;
    DBEdit3.Visible:= true;
    Tcomodin.Active:= false;
    Tcomodin.TableName := 'CatAreas';
    Label1.Caption:= 'Área';
    DBEdit1.DataField:= 'Area';
    Label2.Caption:= 'Encargado';
    DBEdit2.DataField:= 'Encargado';
    Label3.Caption:= 'Puesto';
    DBEdit3.DataField:= 'PuestoEnc';
    Label4.Visible:= false;
    DBMemo1.Datafield:= '';
    DBMemo1.visible:= false;
    LCBAreas.DataField:= '';
    LCBAreas.Visible:= false;
    //DBNavigator1.VisibleButtons:= DBNavigator1.VisibleButtons + [nbDelete, nbInsert];
    btnEliminar.Visible:= true;
    btnNuevo.Visible:= true;
    Tcomodin.Active:= true;
    //lbBuscar.Visible:=false;
    //txtBuscar.Visible:= false;
    campo:= 'Area';
    ShowModal;
  end;
    TAreas.Active:=false;
    TAreas.Active:=true;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
with FCatalogos do
  begin
    Tcomodin.Active:= false;
    Tcomodin.TableName := 'CatLugares';
    Label1.Caption:= 'Ciudad';
    DBEdit1.DataField:= 'Lugar';

    Label2.Caption:= 'Tipo';
    DBEdit2.DataField:= 'Tipo';
    Label2.Visible:= true;
    DBEdit2.Visible:= true;
    Label3.Visible:= false;
    DBEdit3.DataField:= '';
    DBEdit3.Visible:= false;
    DBEdit2.DataField:= '';
    TAreas.Active:= false;
    Label4.Visible:= false;
    DBMemo1.Datafield:= '';
    DBMemo1.visible:= false;
    LCBAreas.DataField:= '';
    LCBAreas.Visible:= false;
    //DBNavigator1.VisibleButtons:= DBNavigator1.VisibleButtons + [nbDelete, nbInsert];
    btnEliminar.Visible:= true;
    btnNuevo.Visible:= true;
    Tcomodin.Active:= true;
    campo:= 'Lugar';
    //lbBuscar.Visible:=false;
    //txtBuscar.Visible:= false;
    ShowModal;
  end;
   Tciudades.Active:=false;
   Tciudades.Active:=true;
end;

procedure TForm1.SpeedButton3Click(Sender: TObject);
begin
 with FCatalogos do
  begin
    Tcomodin.Active:= false;
    Tcomodin.TableName := 'CatProgramas';
    Label1.Caption:= 'Programa';
    DBEdit1.DataField:= 'Programa';
    //Label2.Caption:= ''
    Label3.Visible:= true;
    Label2.Visible:=false;
    Label3.Caption:= 'Área';
    Label4.Visible:= false;
    DBMemo1.Datafield:= '';
    DBMemo1.visible:= false;
    DBEdit2.DataField:= '';
    DBEdit2.Visible:= false;
    LCBAreas.Visible:= true;
    LCBAreas.DataField:= 'Area';
    LCBAreas.Left:= 7;
    DBEdit3.DataField:= '';
    DBEdit3.Visible:= false;
    Tcomodin.Active:= true;
    //DBNavigator1.VisibleButtons:= DBNavigator1.VisibleButtons + [nbDelete, nbInsert];
    TAreas.Active:= true;
    btnEliminar.Visible:= true;
    btnNuevo.Visible:= true;
    //lbBuscar.Visible:=false;
    //txtBuscar.Visible:= false;
    campo:= 'Programa';
    ShowModal;
  end;
    TProgramas.Active:= false;
    TProgramas.Active:= true;
end;

procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
  with FCatalogos do
  begin
    Tcomodin.Active:= false;
    Tcomodin.TableName := 'CatPuestos';
    Label1.Caption:= 'Puesto';
    DBEdit1.DataField:= 'Puesto';
    Label2.Visible:= false;
    DBEdit2.DataField:= '';
    DBEdit2.Visible:= false;
    Label3.Visible:= false;
    DBEdit3.DataField:= '';
    DBEdit3.Visible:= false;
    Label4.Visible:= true;
    DBMemo1.Visible:=true;
    DBMemo1.Datafield:= 'Descripcion';
    LCBAreas.DataField:= '';
    LCBAreas.Visible:= false;
    //DBNavigator1.VisibleButtons:= DBNavigator1.VisibleButtons + [nbDelete, nbInsert];
    Tcomodin.Active:= true;
    btnEliminar.Visible:= true;
    btnNuevo.Visible:= true;
    //lbBuscar.Visible:=true;
    //txtBuscar.Visible:= true;
    campo:= 'Puesto';
    ShowModal;
  end;
   Tpuestos.Active:= false;
   Tpuestos.Active:= true;
end;

procedure TForm1.SpeedButton5Click(Sender: TObject);
begin
with FCatalogos do
  begin
    Tcomodin.Active:= false;
    Tcomodin.TableName := 'TipoEmpleo';
    Label1.Caption:= 'Tipo de Contrato';
    DBEdit1.DataField:= 'TipoEmpleo';
    Label2.Visible:= false;
    DBEdit2.Visible:= false;
    Label3.Visible:= false;
    Label4.Visible:= false;
    DBMemo1.Datafield:= '';
    DBMemo1.visible:= false;
    DBEdit2.DataField:= '';
    DBEdit3.DataField:= '';
    DBEdit3.Visible:= false;
    LCBAreas.DataField:= '';
    LCBAreas.Visible:= false;
    Tareas.Active:= false;
    //DBNavigator1.VisibleButtons:= DBNavigator1.VisibleButtons + [nbDelete, nbInsert];
    Tcomodin.Active:= true;
     btnEliminar.Visible:= true;
    btnNuevo.Visible:= true;
    campo:= 'TipoEmpleo';
    //lbBuscar.Visible:=false;
    //txtBuscar.Visible:= false;
    ShowModal;
  end;
  TtipoEmpleo.Active:= false;
  TtipoEmpleo.Active:= true
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
 with FCatalogos do
  begin
    Tcomodin.Active:= false;
    Tcomodin.TableName := 'Directivos';
    Label1.Caption:= 'Oficial Mayor';
    DBEdit1.DataField:= 'OficialMay';
    Label2.Caption:= 'Director';
    Label2.Visible:= true;
    DBEdit2.Visible:= true;
    Label3.Visible:= false;
    Label4.Visible:= false;
    DBMemo1.Datafield:= '';
    DBMemo1.visible:= false;
    DBEdit2.DataField:= 'Director';
    DBEdit3.DataField:= '';
    DBEdit3.Visible:= false;
    LCBAreas.DataField:= '';
    LCBAreas.Visible:= false;
    //DBNavigator1.VisibleButtons:= DBNavigator1.VisibleButtons-[nbDelete, nbInsert];
    btnEliminar.Visible:= false;
    btnNuevo.Visible:= false;
    Tareas.Active:= false;
    Tcomodin.Active:= true;
    campo:= 'OficialMay';
    //lbBuscar.Visible:=false;
    //txtBuscar.Visible:= false;
    ShowModal
  end
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
application.Terminate
end;

procedure TForm1.SpeedButton6Click(Sender: TObject);
begin
//Nacionalidad
 with FCatalogos do
  begin
    Tcomodin.Active:= false;
    Tcomodin.TableName := 'CatNacionalidad';
    Label1.Caption:= 'Nacionalidad';
    DBEdit1.DataField:= 'Nacionalidad';
    Label2.Visible:= false;
    DBEdit2.DataField:= '';
    DBEdit2.Visible:= false;
    Label3.Visible:= false;
    DBEdit3.DataField:= '';
    DBEdit3.Visible:= false;
    Label4.Visible:= false;
    DBMemo1.Visible:=false;
    DBMemo1.Datafield:= '';
    LCBAreas.DataField:= '';
    LCBAreas.Visible:= false;
    btnEliminar.Visible:= true;
    btnNuevo.Visible:= true;
    //DBNavigator1.VisibleButtons:= DBNavigator1.VisibleButtons + [nbDelete, nbInsert];
    Tcomodin.Active:= true;
     campo:= 'Nacionalidad';
    //lbBuscar.Visible:=false;
    //txtBuscar.Visible:= false;
    ShowModal;
  end;
    TNacionalidad.Active:= false;
    TNacionalidad.Active:= true
end;

procedure TForm1.SpeedButton7Click(Sender: TObject);
begin
if (TEmpleados.State in [dsinsert, dsedit])then
begin
 if (FbuscarDir.ShowModal = mrok)  then
  begin
   TAreas.Locate('ide',FBuscarDir.QBuscarDir.FieldByName('ide').AsInteger, []);
   DBLookupCombobox1.KeyValue:= FBuscarDir.QBuscarDir.FieldByName('ide').AsInteger;
  end;
end;

end;

procedure TForm1.BitBtn4Click(Sender: TObject);
begin
 FExportaciones.ShowModal
end;

procedure TForm1.BitBtn5Click(Sender: TObject);
begin
Fusuarios.TUsuarios.Active:=false;
Fusuarios.TUsuarios.Active:=true;
Fusuarios.ShowModal
end;

procedure TForm1.BitBtn6Click(Sender: TObject);
begin
//LDBuscar.Execute
//BuscarDial.Execute
 //Fbuscar.txtNombre.Clear;
FBuscar.QBuscar.Active:= false;
 FBuscar.QBuscar.Active:= true;
if FBuscar.ShowModal = mrok then
  TEmpleados.Locate('ide',FBuscar.QBuscar.FieldByName('ide').AsInteger, [])
end;

procedure TForm1.BitBtn7Click(Sender: TObject);
begin
 FHistorialCon.QHistorial.Active:= false;
 FHistorialCon.QHistorial.Parameters.ParamByName('p').Value:=  TEmpleados.FieldByName('ide').AsInteger;
 FHistorialCon.QHistorial.Active:= true;
 FHistorialCon.ShowModal
end;

procedure TForm1.btnFechasClick(Sender: TObject);
begin
 with FechasEmp do
  begin
    if TFechasEmp.Locate('Empleado',TEmpleados.FieldByName('ide').AsInteger, []) then
       TFechasEmp.Edit
     else
       TFechasEmp.Insert;
    ShowModal;
  end;
end;

procedure TForm1.btnPrimeroClick(Sender: TObject);
begin
DBControles((Sender As Tbitbtn).Tag, TEmpleados, btnPrimero, btnAtras, btnSiguiente, btnUltimo, btnNuevo, btnEditar, btnEliminar, btnGuardar, btnCancelar)
end;

procedure TForm1.DSEmpleadosDataChange(Sender: TObject; Field: TField);
begin
 if TEmpleados.State in [dsinsert, dsedit] then
     btnFechas.Enabled:= false
  else
      btnFechas.Enabled:= true
end;

procedure TForm1.DSEmpleadosStateChange(Sender: TObject);
begin
if TEmpleados.State = dsEdit then
   btnPrimeroClick(btnEditar);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 application.Terminate
end;

end.
