unit UElegirHoja;

interface

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

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

var
  FElegirHoja: TFElegirHoja;

implementation

{$R *.dfm}

uses UBimestre, Udm;

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;
   //FElegirHoja.Caption:= cade;
   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;
  Function ObtenerCURP(Consulta: TADOQuery; curp: string): string;
 var
    cade, para: string;
  begin
     para:=  chr(39) + curp + chr(39);
     cade:= 'Select Curp from Empleados where Curp = ' +  para + ' or Curp_2 = ' + para;
     with Consulta do
    begin
      active:= false;
      sql.Clear;
      sql.Add(cade);
      active:= true;
      result:= fieldbyname('Curp').AsString;
    end;
  end;

procedure TFElegirHoja.BitBtn1Click(Sender: TObject);
 var
   cont: integer;
begin
 with FBimestre do
 begin
   Timporta.Active:=false;
   //Timporta.Connection:= ADOConnection1;
   Timporta.TableName:= FElegirHoja.Combobox1.Text;
   Timporta.Active:=true;
   //showmessage('¡Conectado!');
   TImporta.First;
   TNodosBim.Active:= false;
   TNodosBim.Active:= true;
  // cont:= 0;
  while not(Timporta.Eof) do
    begin
      if Timporta.Fields.FieldByNumber(2).AsFloat > 0 then
      begin
       if Existe(pruebas, Timporta.Fields.FieldByNumber(1).Asstring) > 0 then
       begin
        TNodosBim.Insert;
        TNodosBim.FieldByName('NumEmp').AsString:=  ObtenerNumEmp(pruebas,Timporta.Fields.FieldByNumber(1).Asstring);
        TNodosBim.FieldByName('Ahorro_ST').AsFloat:=  Timporta.Fields.FieldByNumber(2).AsFloat;
        TNodosBim.FieldByName('IdBimestre').AsInteger:=  TBimestres.FieldByName('IdBimestre').AsInteger;
        //TNodosBim.FieldByName('DiasCot').AsString:=  Timporta.Fields.FieldByNumber(3).AsString;
        TNodosBim.FieldByName('CURP').AsString:= Timporta.Fields.FieldByNumber(1).Asstring;
        TNodosBim.Post;
        inc(cont);
       end
       else
         FElegirHoja.memo1.Lines.Add(Timporta.Fields.FieldByNumber(1).Asstring + ' Faltante');
      end;
      TImporta.Next;
    end;
  TNodos.Active:= false;
  TNodos.Active:= true;
  //showmessage(inttostr(cont));
 end;

end;

end.
