unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, ZDataset, Data.DB, ZAbstractRODataset,
  ZAbstractDataset, ZAbstractTable, ZAbstractConnection, ZConnection,
  Vcl.ExtCtrls, ZSqlProcessor, Vcl.StdCtrls, Vcl.Buttons, Vcl.Grids, Vcl.DBGrids;

type
  TForm2 = class(TForm)
    tsincronizar: TTimer;
    DBagrigest: TZConnection;
    dbservidor: TZConnection;
    sv_movimientos_mesa: TZTable;
    sv_movimientos_mesaid: TLargeintField;
    sv_movimientos_mesafecha: TDateField;
    sv_movimientos_mesamesa: TWideStringField;
    sv_movimientos_mesaidt: TIntegerField;
    sv_movimientos_mesaTRABAJADOR: TWideStringField;
    sv_movimientos_mesaENTRADA_REAL: TTimeField;
    sv_movimientos_mesaSALIDA_REAL: TTimeField;
    sv_movimientos_mesaENTRADA_AJUSTADA: TTimeField;
    sv_movimientos_mesaSALIDA_AJUSTADA: TTimeField;
    sv_movimientos_mesaHORASREALES: TTimeField;
    sv_movimientos_mesaHORASAJUSTADAS: TTimeField;
    sv_movimientos_mesaPUESTO: TWideStringField;
    sv_movimientos_mesaTIPOTRABAJO: TWideStringField;
    sv_movimientos_mesaARTICULO: TWideStringField;
    sv_movimientos_mesaPARCELA: TWideStringField;
    sv_trabajo_diario: TZTable;
    sv_trabajo_diarioid: TIntegerField;
    sv_trabajo_diariofecha: TDateField;
    sv_trabajo_diarioparcela: TWideStringField;
    sv_trabajo_diarioidt: TIntegerField;
    sv_trabajo_diariotrabajador: TWideStringField;
    sv_trabajo_diariotypework: TWideStringField;
    sv_trabajo_diarioarticulo: TWideStringField;
    sv_trabajo_diarioqhoras: TFloatField;
    sv_trabajo_diarioprecio: TFloatField;
    sv_trabajo_diariototal: TFloatField;
    sv_trabajo_diarioestado: TWideStringField;
    sv_rendimiento: TZTable;
    sv_rendimientoid: TIntegerField;
    sv_rendimientofecha: TDateField;
    sv_rendimientohora: TTimeField;
    sv_rendimientoidt: TIntegerField;
    sv_rendimientotrabajador: TWideStringField;
    sv_rendimientomateria_prima: TWideStringField;
    sv_rendimientopeso_bruto: TFloatField;
    sv_rendimientotara: TFloatField;
    sv_rendimientopesoneto: TFloatField;
    sv_produccion: TZTable;
    sv_produccionid: TLargeintField;
    sv_produccionfecha: TDateField;
    sv_produccionhora: TTimeField;
    sv_produccionmesa: TWideStringField;
    sv_produccionbascula: TWideStringField;
    sv_produccionidt: TIntegerField;
    sv_produccionembasadora: TWideStringField;
    sv_produccionmateria_prima: TWideStringField;
    sv_produccionproducto: TWideStringField;
    sv_producciontara: TFloatField;
    sv_produccionpeso_teorico: TFloatField;
    sv_produccionpeso_real: TFloatField;
    qaux3: TZReadOnlyQuery;
    qaux4: TZReadOnlyQuery;
    qserv: TZReadOnlyQuery;
    qserv2: TZReadOnlyQuery;
    qaux: TZReadOnlyQuery;
    qaux2: TZReadOnlyQuery;
    qaux5: TZReadOnlyQuery;
    PCONEXION: TPanel;
    qserv3: TZSQLProcessor;
    sv_trabajo_diarioorigen: TWideStringField;
    Panel1: TPanel;
    SpeedButton1: TSpeedButton;
    qpesadas: TZReadOnlyQuery;
    qpesadaspesadas: TLargeintField;
    sv_movimientos_mesanum_limpiadora: TIntegerField;
    sv_rendimientonum_limpiadora: TIntegerField;
    sv_desbloqueos: TZTable;
    sv_desbloqueosid: TIntegerField;
    sv_desbloqueosfecha: TDateField;
    sv_desbloqueoshora: TTimeField;
    sv_desbloqueosenvasadora: TWideStringField;
    sv_desbloqueosmesa: TWideStringField;
    sv_desbloqueosbascula: TWideStringField;
    qmovimientos_mesa: TZReadOnlyQuery;
    qmovimientos_mesaid: TLargeintField;
    qmovimientos_mesafecha: TDateField;
    qmovimientos_mesamesa: TWideStringField;
    qmovimientos_mesaidt: TIntegerField;
    qmovimientos_mesaTRABAJADOR: TWideStringField;
    qmovimientos_mesaENTRADA_REAL: TTimeField;
    qmovimientos_mesaSALIDA_REAL: TTimeField;
    qmovimientos_mesaENTRADA_AJUSTADA: TTimeField;
    qmovimientos_mesaSALIDA_AJUSTADA: TTimeField;
    qmovimientos_mesaHORASREALES: TTimeField;
    qmovimientos_mesaHORASAJUSTADAS: TTimeField;
    qmovimientos_mesaPUESTO: TWideStringField;
    qmovimientos_mesaTIPOTRABAJO: TWideStringField;
    qmovimientos_mesaARTICULO: TWideStringField;
    qmovimientos_mesaPARCELA: TWideStringField;
    qmovimientos_mesaestado: TIntegerField;
    qmovimientos_mesanum_limpiadora: TIntegerField;
    qaux5id: TLargeintField;
    qaux5fecha: TDateField;
    qaux5mesa: TWideStringField;
    qaux5idt: TIntegerField;
    qaux5TRABAJADOR: TWideStringField;
    qaux5ENTRADA_REAL: TTimeField;
    qaux5SALIDA_REAL: TTimeField;
    qaux5ENTRADA_AJUSTADA: TTimeField;
    qaux5SALIDA_AJUSTADA: TTimeField;
    qaux5HORASREALES: TTimeField;
    qaux5HORASAJUSTADAS: TTimeField;
    qaux5PUESTO: TWideStringField;
    qaux5TIPOTRABAJO: TWideStringField;
    qaux5ARTICULO: TWideStringField;
    qaux5PARCELA: TWideStringField;
    qaux5estado: TIntegerField;
    qaux5num_limpiadora: TIntegerField;
    procedure tsincronizarTimer(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormShow(Sender: TObject);
var F: TextFile;
    sLinea,thost,tuser,tpass,tmesa,tpuerto,tdatabase: String;
    lineas: array[0..20] of string;
    i: integer;

begin
  AssignFile( F, ExtractFilePath( Application.ExeName ) + 'mesas.ini' );
  Reset( F );
//  Memo1.Clear;
  i:=0;
  while not Eof( F ) do
  begin
    ReadLn( F, sLinea );
//    Memo1.Lines.Add( sLinea );
    lineas[i]:=sLinea;
    i:=i+1
  end;

  CloseFile( F );
    thost:=lineas[0];
    tuser:=lineas[1];
    tpass:=lineas[2];
    tmesa:=lineas[3];
    tpuerto:=lineas[4];
    tdatabase:=lineas[9];

    DBservidor.Connected:=false;
    DBservidor.HostName:=thost;
    DBservidor.User:=tuser;
    DBservidor.Password:=tpass;
    Dbservidor.Database:=tdatabase;
    //DBservidor.connected:=true;

    DBagrigest.Connected:=false;
    DBagrigest.HostName:='localhost';
    DBagrigest.User:='root';
    DBagrigest.Password:='';
    Dbagrigest.Database:=tdatabase;
//    DBagrigest.connected:=true;
end;

procedure TForm2.SpeedButton1Click(Sender: TObject);
begin
    if tsincronizar.Enabled then
    begin
        tsincronizar.Enabled:=false;
        qpesadas.Open;
        pconexion.Caption:=qpesadas.Fields[0].asstring + ' Pesadas';
    end else
    begin
      tsincronizar.Enabled:=true;
    end;
end;

procedure TForm2.tsincronizarTimer(Sender: TObject);
var valores,fecha,tara,pesoteorico,pesoreal,id,horas,num_limpiadora:string;
    dia,mes,anyo:word;
begin
  tsincronizar.Enabled:=false;
  try
    if dbservidor.connected=true then
    begin
    qserv.Close;
    qserv.SQL.Clear;
    qserv.SQL.Add('select count(*) from materias_primas');
    qserv.Open;
    end;
  except
     dbservidor.Connected:=false;
  end;

  if (dbservidor.Connected =TRUE) and (dbagrigest.Connected=true) then
  begin
// compruebo si se ha metido en otra mesa
       qmovimientos_mesa.Close;
       qmovimientos_mesa.Prepare;
       qmovimientos_mesa.ParamByName('PESTADO').AsInteger:=0;
       qmovimientos_mesa.Open;
       if qmovimientos_mesa.RecordCount>1 then
       begin
//           sv_movimientos_mesa.open;
           while not qmovimientos_mesa.Eof do//while not qaux5.Eof do
           begin
              qserv.Close;
              qserv.SQL.Clear;
              qserv.SQL.Add('select id,salida_real from movimiento_mesas where id='+qmovimientos_mesa.fields[0].asstring);
              qserv.Open;
              if qserv.RecordCount=1 then
              begin
                  if qserv.Fields[1].AsString<>'' then
                  begin
                      qaux4.Close;
                      qaux4.sql.clear;
                      qaux4.sql.Add('delete from movimiento_mesas_local where id='+qserv.Fields[0].Asstring);
                      qaux4.ExecSQL;
                  end;
              end;
              qmovimientos_mesa.next;// qaux5.Next;
           end;
       end;
       if qmovimientos_mesa.RecordCount=1 then
       begin
//  sv_movimientos_mesa.open;
              qserv.Close;
              qserv.SQL.Clear;
              qserv.SQL.Add('select id,salida_real from movimiento_mesas where id='+qmovimientos_mesa.fields[0].asstring);
              qserv.Open;
              if qserv.RecordCount=1 then
              begin
                  if qserv.Fields[1].AsString<>'' then
                  begin
                      qaux4.Close;
                      qaux4.sql.clear;
                      qaux4.sql.Add('delete from movimiento_mesas_local where id='+qserv.Fields[0].Asstring);
                      qaux4.ExecSQL;
                  end;
              end;
       end;



// inserto en movimientos mesa
       qmovimientos_mesa.Close;
       qmovimientos_mesa.Prepare;
       qmovimientos_mesa.ParamByName('PESTADO').AsInteger:=1;
       qmovimientos_mesa.Open;

       if qmovimientos_mesa.RecordCount>1 then
       begin
//           sv_movimientos_mesa.open;
           while not qmovimientos_mesa.Eof do
           begin
              qserv.Close;
              qserv.SQL.Clear;
              qserv.SQL.Add('select max(id)+1 from movimiento_mesas');
              qserv.Open;
              id:=qserv.Fields[0].AsString;
              if id='' then id:='1';

              decodedate(qmovimientos_mesa.Fields[1].asdatetime,anyo,mes,dia);
              fecha:=inttostr(anyo)+'-'+inttostr(mes)+'-'+inttostr(dia);
              valores:='('+id+',';
              valores:=valores+''''+fecha+''''+',';
              valores:=valores+''''+qmovimientos_mesa.Fields[2].asstring+''''+',';
              valores:=valores+qmovimientos_mesa.Fields[3].asstring+',';
              valores:=valores+''''+qmovimientos_mesa.Fields[4].asstring+''''+',';
              if qmovimientos_mesa.fields[5].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[5].asstring+''''+',';
              if qmovimientos_mesa.fields[6].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[6].asstring+''''+',';
              if qmovimientos_mesa.fields[7].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[7].asstring+''''+',';
              if qmovimientos_mesa.fields[8].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[8].asstring+''''+',';
              if qmovimientos_mesa.fields[9].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[9].asstring+''''+',';
              if qmovimientos_mesa.fields[10].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[10].asstring+''''+',';
              valores:=valores+''''+qmovimientos_mesa.Fields[11].asstring+''''+',';
              valores:=valores+''''+qmovimientos_mesa.Fields[12].asstring+''''+',';
              valores:=valores+''''+qmovimientos_mesa.Fields[13].asstring+''''+',';
              valores:=valores+''''+qmovimientos_mesa.Fields[14].asstring+''''+',';
              if qmovimientos_mesa.fields[16].asstring='' then num_limpiadora:='null' else num_limpiadora:=qmovimientos_mesa.Fields[16].asstring;

              valores:=valores+num_limpiadora;
              valores:=valores+')';
              qserv.close;
              qserv.sql.clear;
              qserv.SQL.Add('insert into movimiento_mesas (id,fecha,mesa,idt,trabajador,entrada_real,salida_real,entrada_ajustada,salida_ajustada,horasreales,horasajustadas,puesto,tipotrabajo,articulo,parcela,num_limpiadora)');
              qserv.SQL.Add(' values '+valores);
              qserv.execsql;
              qaux4.Close;
              qaux4.sql.clear;
              qaux4.sql.Add('update movimiento_mesas_local set estado=0,id='+id+' where id='+qmovimientos_mesaid.asstring);
              qaux4.ExecSQL;
              qmovimientos_mesa.Next;
           end;
       end;
       if qmovimientos_mesa.RecordCount=1 then
       begin
          qserv.Close;
          qserv.SQL.Clear;
          qserv.SQL.Add('select max(id)+1 from movimiento_mesas');
          qserv.Open;
          id:=qserv.Fields[0].AsString;
          if id='' then id:='1';

          decodedate(qmovimientos_mesa.Fields[1].asdatetime,anyo,mes,dia);
          fecha:=inttostr(anyo)+'-'+inttostr(mes)+'-'+inttostr(dia);
          valores:='('+id+',';
          valores:=valores+''''+fecha+''''+',';
          valores:=valores+''''+qmovimientos_mesa.Fields[2].asstring+''''+',';
          valores:=valores+qmovimientos_mesa.Fields[3].asstring+',';
          valores:=valores+''''+qmovimientos_mesa.Fields[4].asstring+''''+',';
          if qmovimientos_mesa.fields[5].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[5].asstring+''''+',';
          if qmovimientos_mesa.fields[6].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[6].asstring+''''+',';
          if qmovimientos_mesa.fields[7].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[7].asstring+''''+',';
          if qmovimientos_mesa.fields[8].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[8].asstring+''''+',';
          if qmovimientos_mesa.fields[9].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[9].asstring+''''+',';
          if qmovimientos_mesa.fields[10].asstring='' then valores:=valores+'null'+',' else valores:=valores+''''+qmovimientos_mesa.Fields[10].asstring+''''+',';
          valores:=valores+''''+qmovimientos_mesa.Fields[11].asstring+''''+',';
          valores:=valores+''''+qmovimientos_mesa.Fields[12].asstring+''''+',';
          valores:=valores+''''+qmovimientos_mesa.Fields[13].asstring+''''+',';
          valores:=valores+''''+qmovimientos_mesa.Fields[14].asstring+''''+',';
          if qmovimientos_mesa.fields[16].asstring='' then num_limpiadora:='null' else num_limpiadora:=qmovimientos_mesa.Fields[16].asstring;
          valores:=valores+num_limpiadora;
          valores:=valores+')';

          qserv.close;
          qserv.sql.clear;
          qserv.SQL.Add('insert into movimiento_mesas (id,fecha,mesa,idt,trabajador,entrada_real,salida_real,entrada_ajustada,salida_ajustada,horasreales,horasajustadas,puesto,tipotrabajo,articulo,parcela,num_limpiadora)');
          qserv.SQL.Add(' values '+valores);
          qserv.execsql;

          qaux4.Close;
          qaux4.sql.clear;
          qaux4.sql.Add('update movimiento_mesas_local set estado=0,id='+id+' where id='+qmovimientos_mesa.Fields[0].asstring);
          qaux4.ExecSQL;
       end;
//actualizo en movimiento de mesas
       qmovimientos_mesa.Close;
       qmovimientos_mesa.Prepare;
       qmovimientos_mesa.ParamByName('PESTADO').AsInteger:=2;
       qmovimientos_mesa.Open;

       if qmovimientos_mesa.RecordCount>1 then
       begin
           while not qmovimientos_mesa.Eof do
           begin
              qserv.close;
              qserv.sql.clear;
              qserv.SQL.Add('update movimiento_mesas set salida_real='+''''+qmovimientos_mesa.Fields[6].asstring+''''+',salida_ajustada='+''''+qmovimientos_mesa.Fields[8].asstring+''''+',horasreales='+''''+qmovimientos_mesa.Fields[9].asstring+''''+',horasajustadas='+''''+qmovimientos_mesa.Fields[10].asstring+'''');
              qserv.SQL.Add(' where id='+qmovimientos_mesa.Fields[0].Asstring);
              qserv.execsql;

              qaux4.close;
              qaux4.sql.clear;
              qaux4.SQL.Add('update movimiento_mesas_local set estado=-1 where id='+qmovimientos_mesa.Fields[0].Asstring);
              qaux4.execsql;
              qaux4.Close;
              qaux4.sql.clear;
              qaux4.sql.Add('delete from movimiento_mesas_local where estado=-1 and id='+qmovimientos_mesa.Fields[0].asstring);
              qaux4.ExecSQL;


              qmovimientos_mesa.Next;
           end;
       end;
       if qmovimientos_mesa.RecordCount=1 then
       begin

          qserv.close;
          qserv.sql.clear;
          qserv.SQL.Add('update movimiento_mesas set salida_real='+''''+qmovimientos_mesa.Fields[6].asstring+''''+',salida_ajustada='+''''+qmovimientos_mesa.Fields[8].asstring+''''+',horasreales='+''''+qmovimientos_mesa.Fields[9].asstring+''''+',horasajustadas='+''''+qmovimientos_mesa.Fields[10].asstring+'''');
          qserv.SQL.Add(' where id='+qmovimientos_mesa.Fields[0].Asstring);
          qserv.execsql;

          qaux4.close;
          qaux4.sql.clear;
          qaux4.SQL.Add('update movimiento_mesas_local set estado=0 where id='+qmovimientos_mesa.Fields[0].Asstring);
          qaux4.execsql;

          qaux4.Close;
          qaux4.sql.clear;
          qaux4.sql.Add('delete from movimiento_mesas_local where estado=-1 and id='+qmovimientos_mesa.Fields[0].asstring);
          qaux4.ExecSQL;
       end;


// inserto en servidor produccion local
       qaux3.Close;
       qaux3.SQL.Clear;
       qaux3.sql.Add('select * from produccion_local where estado=1');
       qaux3.Open;
       qaux3.first;
//       sv_produccion.close;
       if qaux3.RecordCount>1 then
       begin
  //         sv_produccion.Open;
           while not qaux3.Eof do
           begin
           decodedate(qaux3.Fields[1].asdatetime,anyo,mes,dia);
           tara:= qaux3.Fields[9].asstring;
           tara:=stringreplace(tara,',','.',[rfReplaceAll, rfIgnoreCase]);
           pesoteorico:= qaux3.Fields[10].asstring;
           pesoteorico:=stringreplace(pesoteorico,',','.',[rfReplaceAll, rfIgnoreCase]);
           pesoreal:= qaux3.Fields[11].asstring;
           pesoreal:=stringreplace(pesoreal,',','.',[rfReplaceAll, rfIgnoreCase]);
           fecha:=inttostr(anyo)+'-'+inttostr(mes)+'-'+inttostr(dia);
           valores:='('+''''+fecha+''''+',';
           valores:=valores+''''+qaux3.Fields[2].asstring+''''+',';
           valores:=valores+''''+qaux3.Fields[3].asstring+''''+',';
           valores:=valores+''''+qaux3.Fields[4].asstring+''''+',';
           valores:=valores+qaux3.Fields[5].asstring+',';
           valores:=valores+''''+qaux3.Fields[6].asstring+''''+',';
           valores:=valores+''''+qaux3.Fields[7].asstring+''''+',';
           valores:=valores+''''+qaux3.Fields[8].asstring+''''+',';
           valores:=valores+tara+',';
           valores:=valores+pesoteorico+',';
           valores:=valores+pesoreal+')';

          qserv.close;
          qserv.sql.clear;
          qserv.SQL.Add('insert into produccion (fecha,hora,mesa,bascula,idt,embasadora,materia_prima,producto,tara,peso_teorico,peso_real)');
          qserv.SQL.Add(' values '+valores);
          qserv.execsql;


              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('update produccion_local set estado=0 where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;


{              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('delete from  produccion_local where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;}

            qaux3.Next;
           end;
       end;
       if qaux3.RecordCount=1 then
       begin
           decodedate(qaux3.Fields[1].asdatetime,anyo,mes,dia);
           tara:= qaux3.Fields[9].asstring;
           tara:=stringreplace(tara,',','.',[rfReplaceAll, rfIgnoreCase]);
           pesoteorico:= qaux3.Fields[10].asstring;
           pesoteorico:=stringreplace(pesoteorico,',','.',[rfReplaceAll, rfIgnoreCase]);
           pesoreal:= qaux3.Fields[11].asstring;
           pesoreal:=stringreplace(pesoreal,',','.',[rfReplaceAll, rfIgnoreCase]);
           fecha:=inttostr(anyo)+'-'+inttostr(mes)+'-'+inttostr(dia);
           valores:='('+''''+fecha+''''+',';
           valores:=valores+''''+qaux3.Fields[2].asstring+''''+',';
           valores:=valores+''''+qaux3.Fields[3].asstring+''''+',';
           valores:=valores+''''+qaux3.Fields[4].asstring+''''+',';
           valores:=valores+qaux3.Fields[5].asstring+',';
           valores:=valores+''''+qaux3.Fields[6].asstring+''''+',';
           valores:=valores+''''+qaux3.Fields[7].asstring+''''+',';
           valores:=valores+''''+qaux3.Fields[8].asstring+''''+',';
           valores:=valores+tara+',';
           valores:=valores+pesoteorico+',';
           valores:=valores+pesoreal+')';

          qserv.close;
          qserv.sql.clear;
          qserv.SQL.Add('insert into produccion (fecha,hora,mesa,bascula,idt,embasadora,materia_prima,producto,tara,peso_teorico,peso_real)');
          qserv.SQL.Add(' values '+valores);
          qserv.execsql;


              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('update produccion_local set estado=0 where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;
{
              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('delete from produccion_local where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;}
       end;

// inserto en servidor rendimiento_local
       qaux3.Close;
       qaux3.SQL.Clear;
       qaux3.sql.Add('select * from rendimiento_local where estado=1');
       qaux3.Open;
       if qaux3.RecordCount>1 then
       begin
           sv_rendimiento.Open;
           while not qaux3.Eof do
           begin
              sv_rendimiento.Append;
              sv_rendimientofecha.value:=qaux3.Fields[1].asdatetime;
              sv_rendimientohora.value:=qaux3.Fields[2].asdatetime;
              sv_rendimientoidt.value:=qaux3.Fields[3].asinteger;
              sv_rendimientotrabajador.value:=qaux3.Fields[4].asstring;
              sv_rendimientomateria_prima.value:=qaux3.Fields[5].asstring;
              sv_rendimientopeso_bruto.value:=qaux3.Fields[6].asfloat;
              sv_rendimientotara.value:=qaux3.Fields[7].asfloat;
              sv_rendimientopesoneto.Value:=qaux3.Fields[8].asfloat;
              sv_rendimientonum_limpiadora.Value:=qaux3.Fields[10].asinteger;
              sv_rendimiento.Post;
              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('update rendimiento_local set estado=0 where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;
              qaux3.Next;
           end;
       end;
       if qaux3.RecordCount=1 then
       begin
              sv_rendimiento.Open;
              sv_rendimiento.Append;
              sv_rendimientofecha.value:=qaux3.Fields[1].asdatetime;
              sv_rendimientohora.value:=qaux3.Fields[2].asdatetime;
              sv_rendimientoidt.value:=qaux3.Fields[3].asinteger;
              sv_rendimientotrabajador.value:=qaux3.Fields[4].asstring;
              sv_rendimientomateria_prima.value:=qaux3.Fields[5].asstring;
              sv_rendimientopeso_bruto.value:=qaux3.Fields[6].asfloat;
              sv_rendimientotara.value:=qaux3.Fields[7].asfloat;
              sv_rendimientopesoneto.Value:=qaux3.Fields[8].asfloat;
              sv_rendimientonum_limpiadora.Value:=qaux3.Fields[10].asinteger;
              sv_rendimiento.Post;
              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('update rendimiento_local set estado=0 where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;
              qaux3.Next;
       end;
// inserto en servidor desbloqueos_ local

       qaux3.Close;
       qaux3.SQL.Clear;
       qaux3.sql.Add('select * from desbloqueos_local where estado=1');
       qaux3.Open;
       if qaux3.RecordCount>1 then
       begin
           sv_desbloqueos.Open;
           while not qaux3.Eof do
           begin
              sv_desbloqueos.Open;
              sv_desbloqueos.Append;
              sv_desbloqueosfecha.value:=qaux3.Fields[1].asdatetime;
              sv_desbloqueoshora.value:=qaux3.Fields[2].asdatetime;
              sv_desbloqueosenvasadora.value:=qaux3.Fields[3].asstring;
              sv_desbloqueosmesa.value:=qaux3.Fields[4].asstring;
              sv_desbloqueosbascula.value:=qaux3.Fields[5].asstring;
              sv_desbloqueos.Post;
              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('update desbloqueos_local set estado=0 where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;
              qaux3.Next;
           end;
       end;
       if qaux3.RecordCount=1 then
       begin
              sv_desbloqueos.Open;
              sv_desbloqueos.Append;
              sv_desbloqueosfecha.value:=qaux3.Fields[1].asdatetime;
              sv_desbloqueoshora.value:=qaux3.Fields[2].asdatetime;
              sv_desbloqueosenvasadora.value:=qaux3.Fields[3].asstring;
              sv_desbloqueosmesa.value:=qaux3.Fields[4].asstring;
              sv_desbloqueosbascula.value:=qaux3.Fields[5].asstring;
              sv_desbloqueos.Post;

              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('update desbloqueos_local set estado=0 where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;

              qaux3.Next;
       end;

////  inserto en el servidor trabajos_diarios
       qaux3.Close;
       qaux3.SQL.Clear;
       qaux3.sql.Add('select * from trabajo_diario_local where estado=1');
       qaux3.Open;
       if qaux3.RecordCount>1 then
       begin
//           sv_trabajo_diario.Open;
           while not qaux3.Eof do
           begin

/////
              qserv.Close;
              qserv.SQL.Clear;
              qserv.SQL.Add('select max(id)+1 from trabajo_diario');
              qserv.Open;
              decodedate(qaux3.Fields[1].asdatetime,anyo,mes,dia);
              fecha:=inttostr(anyo)+'-'+inttostr(mes)+'-'+inttostr(dia);
              id:=qserv.Fields[0].AsString;
              if id='' then id:='1';
              valores:='('+id+',';
              valores:=valores+''''+fecha+''''+',';
              valores:=valores+''''+qaux3.Fields[2].asstring+''''+',';
              valores:=valores+qaux3.Fields[3].asstring+',';
              valores:=valores+''''+qaux3.Fields[4].asstring+''''+',';
              valores:=valores+''''+qaux3.Fields[5].asstring+''''+',';
              valores:=valores+''''+qaux3.Fields[6].asstring+''''+',';
              horas:=qaux3.Fields[7].asstring;
              horas:=stringreplace(horas,',','.',[rfReplaceAll, rfIgnoreCase]);
              valores:=valores+horas+',';
              valores:=valores+qaux3.Fields[8].asstring+',';
              valores:=valores+qaux3.Fields[9].asstring+',';
              valores:=valores+''''+qaux3.Fields[10].asstring+''''+',';
              valores:=valores+''''+qaux3.Fields[12].asstring+'''';
              valores:=valores+')';

              qserv.close;
              qserv.sql.clear;
              qserv.SQL.Add('insert into trabajo_diario (id,fecha,parcela,idt,trabajador,typework,articulo,qhoras,precio,total,estado,origen)');
              qserv.SQL.Add(' values '+valores);
              qserv.execsql;

              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('delete from trabajo_diario_local where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;

              qaux3.Next;
           end;
       end;
       if qaux3.RecordCount=1 then
       begin
              qserv.Close;
              qserv.SQL.Clear;
              qserv.SQL.Add('select max(id)+1 from trabajo_diario');
              qserv.Open;
              decodedate(qaux3.Fields[1].asdatetime,anyo,mes,dia);
              fecha:=inttostr(anyo)+'-'+inttostr(mes)+'-'+inttostr(dia);
              id:=qserv.Fields[0].AsString;
              if id='' then id:='1';

              valores:='('+id+',';
              valores:=valores+''''+fecha+''''+',';
              valores:=valores+''''+qaux3.Fields[2].asstring+''''+',';
              valores:=valores+qaux3.Fields[3].asstring+',';
              valores:=valores+''''+qaux3.Fields[4].asstring+''''+',';
              valores:=valores+''''+qaux3.Fields[5].asstring+''''+',';
              valores:=valores+''''+qaux3.Fields[6].asstring+''''+',';
              horas:=qaux3.Fields[7].asstring;
              horas:=stringreplace(horas,',','.',[rfReplaceAll, rfIgnoreCase]);
              valores:=valores+horas+',';
              valores:=valores+qaux3.Fields[8].asstring+',';
              valores:=valores+qaux3.Fields[9].asstring+',';
              valores:=valores+''''+qaux3.Fields[10].asstring+''''+',';
              valores:=valores+''''+qaux3.Fields[12].asstring+'''';
              valores:=valores+')';

              qserv.close;
              qserv.sql.clear;
              qserv.SQL.Add('insert into trabajo_diario (id,fecha,parcela,idt,trabajador,typework,articulo,qhoras,precio,total,estado,origen)');
              qserv.SQL.Add(' values '+valores);
              qserv.execsql;

              qaux4.Close;
              qaux4.SQL.Clear;
              qaux4.sql.Add('delete from trabajo_diario_local where id='+qaux3.Fields[0].asstring);
              qaux4.ExecSQL;

       end;
       dbservidor.Connected:=false;
       dbagrigest.Connected:=false;
      end
       else
       begin
         try
            if dbservidor.Connected=false then
            begin
              DBservidor.connected:=true;
            end;
            if dbagrigest.Connected =false then
            begin
              dbagrigest.Connect;
            end;
            PCONEXION.Visible:=TRUE;
            PCONEXION.Caption:='CONECTADO';
            PCONEXION.Color:=clLime;
            sv_movimientos_mesa.Close;
            sv_produccion.Close;
            sv_rendimiento.Close;
            sv_trabajo_diario.Close;

          except
            PCONEXION.Visible:=TRUE;
            PCONEXION.Caption:='SIN CONEXION';
            PCONEXION.Color:=clRED;
          end;

         end;

    tsincronizar.Enabled:=true;
end;


end.
