unit UElegirHojaExt;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Data.Win.ADODB, Vcl.StdCtrls,
  Vcl.Buttons;

type
  TFElegirHojaExt = class(TForm)
    BitBtn1: TBitBtn;
    Memo1: TMemo;
    Label1: TLabel;
    ComboBox1: TComboBox;
    pruebas: TADOQuery;
    BitBtn2: TBitBtn;
    CBViviendaE: TCheckBox;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FElegirHojaExt: TFElegirHojaExt;

implementation

{$R *.dfm}

uses UExtempo;

Function Existe(Consulta: TADOQuery; curp: string): byte;
  var
    cade, para: string;
 begin
   para:= chr(39) + curp + chr(39);
   cade:= 'Select count(*) As Resul from Empleados where Curp = ' + para + ' or Curp_2 = ' + para;
   with Consulta do
    begin
      active:= false;
      sql.Clear;
      sql.Add(cade);
      active:= true;
      result:= fieldbyname('Resul').AsInteger;
    end;
 end;

 Function ObtenerNumEmp(Consulta: TADOQuery; curp: string): string;
 var
    cade, para: string;
  begin
     para:=  chr(39) + curp + chr(39);
     cade:= 'Select NumEmp from Empleados where Curp = ' +  para + ' or Curp_2 = ' + para;
     with Consulta do
    begin
      active:= false;
      sql.Clear;
      sql.Add(cade);
      active:= true;
      result:= fieldbyname('NumEmp').AsString;
    end;
  end;

procedure TFElegirHojaExt.BitBtn1Click(Sender: TObject);
begin
with Extemporaneos do
 begin
   Timporta.Active:=false;
   //Timporta.Connection:= ADOConnection1;
   Timporta.TableName:= FElegirHojaExt.Combobox1.Text;
   Timporta.Active:=true;
   //showmessage('¡Conectado!');
   TImporta.First;
   TNodos.Active:= false;
   TNodos.Active:= true;
   Screen.Cursor:= CrHourGlass;
  while not(Timporta.Eof) do
    begin
       if Existe(pruebas, Timporta.Fields.FieldByNumber(1).Asstring) > 0 then
       begin
        TNodos.Insert;
        TNodos.FieldByName('NumEmp').AsString:=  ObtenerNumEmp(pruebas,Timporta.Fields.FieldByNumber(1).Asstring);
        TNodos.FieldByName('DiasCot').AsString:=  Timporta.Fields.FieldByNumber(2).AsString;
        TNodos.FieldByName('DiasInc').AsString:=  Timporta.Fields.FieldByNumber(3).AsString;
        TNodos.FieldByName('DiasAus').AsString:=  Timporta.Fields.FieldByNumber(4).AsString;
        TNodos.FieldByName('SueldoBas').AsFloat:=  Timporta.Fields.FieldByNumber(5).AsFloat;
        TNodos.FieldByName('PagosFijos').AsFloat:=  Timporta.Fields.FieldByNumber(6).AsFloat;
        TNodos.FieldByName('AhorroSol').AsString:=  Timporta.Fields.FieldByNumber(7).AsString;
        TNodos.FieldByName('ImporteSol').AsFloat:=  Timporta.Fields.FieldByNumber(8).AsFloat;
        TNodos.FieldByName('IdBimestre').AsInteger:=  TBimestresExt.FieldByName('IdBimestre').AsInteger;
        if CBViviendaE.Checked then
        TNodos.FieldByName('Vivienda').AsFloat:=  Timporta.Fields.FieldByNumber(9).AsFloat;
        TNodos.Post;
       end
       else
         FElegirHojaExt.memo1.Lines.Add(Timporta.Fields.FieldByNumber(1).Asstring);
       TImporta.Next;
      end;
   TNodos.Active:= false;
  TNodos.Active:= true;
  bitbtn3.Enabled:=true;
  Screen.Cursor:= CrDefault;
  messagedlg('Importación terminada',mtinformation,[mbok],0);
end;

end;

end.
