unit doble;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.DBCtrls, Vcl.Grids,
  Vcl.DBGrids, Vcl.ExtCtrls, ZAbstractConnection, ZConnection, CPort,
  Vcl.ImgList, ZDataset, Data.DB, ZAbstractRODataset, ZAbstractDataset,
  ZAbstractTable;

type
  TForm1 = class(TForm)
    lesclavo: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    lbascula: TLabel;
    lmesa: TLabel;
    PLIMPIADORAS: TPanel;
    DBGrid2: TDBGrid;
    Panel7: TPanel;
    Label3: TLabel;
    Label5: TLabel;
    Label4: TLabel;
    lnumero: TLabel;
    ltotempleados: TLabel;
    btterminar: TButton;
    btanadir: TButton;
    combopuestos: TDBLookupComboBox;
    Edit1: TEdit;
    ednumero: TEdit;
    Button1: TButton;
    btconfiguracion: TButton;
    btentradasmprimas: TButton;
    btcontrol: TButton;
    Label1: TLabel;
    combovariedad: TDBLookupComboBox;
    comboproducto: TDBLookupComboBox;
    comboenvasadoras: TDBLookupComboBox;
    Label17: TLabel;
    lpesopieza: TLabel;
    Button12: TButton;
    Button11: TButton;
    Label2: TLabel;
    Label7: TLabel;
    BPLIMPIADORA: TButton;
    bpenvasada: TButton;
    tsincronizar: TTimer;
    tcomprobar: TTimer;
    Timer4: TTimer;
    Tboton: TTimer;
    Ta_cero: TTimer;
    tpesar: TTimer;
    DBCheckBox1: TDBCheckBox;
    DBCheckBox2: TDBCheckBox;
    lconectado: TLabel;
    dscalendario: TDataSource;
    tcalendario_laboral: TZTable;
    tcalendario_laboralfecha: TDateField;
    tcalendario_laboralfestivo: TIntegerField;
    tcalendario_laboralalmuerzo: TIntegerField;
    tcalendario_laboralmerienda: TIntegerField;
    tcalendario_laboralinicio_almuerzo: TTimeField;
    tcalendario_laboralfin_almuerzo: TTimeField;
    tcalendario_laboralinicio_merienda: TTimeField;
    tcalendario_laboralfin_merienda: TTimeField;
    empresa: TZTable;
    empresaid: TIntegerField;
    empresacif: TWideStringField;
    empresanombre: TWideStringField;
    empresadireccion: TWideStringField;
    empresacodpost: TWideStringField;
    empresapoblacion: TWideStringField;
    empresaprovincia: TWideStringField;
    empresaemail: TWideStringField;
    empresatelefono1: TWideStringField;
    empresatelefono2: TWideStringField;
    empresacontacto: TWideStringField;
    empresacontingenciacomfij: TFloatField;
    empresasalariobasedia: TFloatField;
    empresacontingenciasdia: TFloatField;
    empresairpf: TFloatField;
    empresadesempleo: TFloatField;
    empresaformacion: TFloatField;
    empresacontingencias: TFloatField;
    empresacc_iban: TWideStringField;
    empresacc_entidad: TWideStringField;
    empresacc_oficina: TWideStringField;
    empresacc_dc: TWideStringField;
    empresacc_cc: TWideStringField;
    empresaajusteentrada: TIntegerField;
    empresaajustesalida: TIntegerField;
    empresaporcbloqueo: TIntegerField;
    empresaactive: TIntegerField;
    empresairpf2: TFloatField;
    empresadesempleo2: TFloatField;
    empresaformacion2: TFloatField;
    empresacontingencias2: TFloatField;
    empresault_envio: TIntegerField;
    empresaregistro_sanitario: TWideStringField;
    empresacoste_kg_gen: TFloatField;
    empresaporc_segsocial: TFloatField;
    empresaprecio_venta_desperdicios: TFloatField;
    empresavercodtrazabilidad: TIntegerField;
    empresacodcomision: TWideStringField;
    empresacodtransporte: TWideStringField;
    empresacc_bic: TWideStringField;
    empresacodotros: TWideStringField;
    empresaavisarstock: TIntegerField;
    empresahtlunes: TFloatField;
    empresahtmartes: TFloatField;
    empresahtmiercoles: TFloatField;
    empresahtjueves: TFloatField;
    empresahtviernes: TFloatField;
    empresahtsabados: TFloatField;
    empresahtdomingos: TFloatField;
    empresatavisostock: TIntegerField;
    empresaresponsable: TWideStringField;
    empresanif_responsable: TWideStringField;
    empresalogo: TBlobField;
    empresafirmaresponsable: TBlobField;
    empresatiempoalmuerzo: TIntegerField;
    empresahorasminimas: TIntegerField;
    empresaidfamilia: TIntegerField;
    empresaajuste15: TIntegerField;
    empresaajuste30: TIntegerField;
    empresaseparartraza_e_s: TIntegerField;
    dsqpuestos: TDataSource;
    qpuestos: TZReadOnlyQuery;
    qpuestoside: TIntegerField;
    qpuestosidsector: TIntegerField;
    qpuestosid: TIntegerField;
    qpuestosdescripcion: TWideStringField;
    qpuestosdescsector: TWideStringField;
    dsqtrabajosalmacen: TDataSource;
    qtrabajosalmacen: TZReadOnlyQuery;
    qtrabajosalmacenid: TIntegerField;
    qtrabajosalmacenpuesto: TWideStringField;
    qtrabajosalmacentipo_trabajo: TWideStringField;
    qtrabajosalmacenarticulo: TWideStringField;
    qtrabajosalmacenparcela: TWideStringField;
    qtrabajosalmacenactivo: TIntegerField;
    qmovimientos_mesa2: TZReadOnlyQuery;
    qmovimientos_mesa2id: TLargeintField;
    qmovimientos_mesa2fecha: TDateField;
    qmovimientos_mesa2mesa: TWideStringField;
    qmovimientos_mesa2idt: TIntegerField;
    qmovimientos_mesa2TRABAJADOR: TWideStringField;
    qmovimientos_mesa2ENTRADA_REAL: TTimeField;
    qmovimientos_mesa2SALIDA_REAL: TTimeField;
    qmovimientos_mesa2ENTRADA_AJUSTADA: TTimeField;
    qmovimientos_mesa2SALIDA_AJUSTADA: TTimeField;
    qmovimientos_mesa2HORASREALES: TTimeField;
    qmovimientos_mesa2HORASAJUSTADAS: TTimeField;
    qmovimientos_mesa2PUESTO: TWideStringField;
    qmovimientos_mesa2TIPOTRABAJO: TWideStringField;
    qmovimientos_mesa2ARTICULO: TWideStringField;
    qmovimientos_mesa2PARCELA: TWideStringField;
    qmovimientos_mesa2hora_fichar: TTimeField;
    qmovimientos_mesa2num_horas: TFloatField;
    qmovimientos_mesa2imprimir: TIntegerField;
    qmovimientos_mesa2num_limpiadora: TIntegerField;
    dsqmovimientos_mesas2: TDataSource;
    dsqvariedadeslinea: TDataSource;
    qvariedadeslinea: TZQuery;
    qvariedadeslineaidsector: TIntegerField;
    qvariedadeslineaid: TWideStringField;
    qvariedadeslineaide: TIntegerField;
    qdesbloqueos: TZQuery;
    qdesbloqueosid: TIntegerField;
    qdesbloqueosfecha: TDateField;
    qdesbloqueoshora: TTimeField;
    qdesbloqueosenvasadora: TWideStringField;
    qdesbloqueosmesa: TWideStringField;
    qdesbloqueosbascula: TWideStringField;
    qdesbloqueosestado: TIntegerField;
    dsdesbloqueos: TDataSource;
    qpesadassectortmp: TZQuery;
    qpesadassectortmpide: TIntegerField;
    qpesadassectortmpid: TIntegerField;
    qpesadassectortmpidea: TIntegerField;
    qpesadassectortmpidsector: TIntegerField;
    qpesadassectortmpdescsector: TWideStringField;
    qpesadassectortmpfecha: TDateField;
    qpesadassectortmphora: TTimeField;
    qpesadassectortmppeso_neto: TFloatField;
    qpesadassectortmplote: TWideStringField;
    qpesadassectortmpvariedad: TWideStringField;
    qrecuperar: TZQuery;
    qauxsrv: TZQuery;
    dspesadassector: TDataSource;
    qproductoslinea: TZQuery;
    qproductoslineaide: TIntegerField;
    qproductoslineaidsector: TIntegerField;
    qproductoslineacodigo: TWideStringField;
    qproductoslineadescripcion: TWideStringField;
    listaiconos: TImageList;
    ImageList1: TImageList;
    ComData1: TComDataPacket;
    comdata2: TComDataPacket;
    dsqenvasadoras: TDataSource;
    qenvasadoras: TZReadOnlyQuery;
    qenvasadorasidt: TIntegerField;
    qenvasadorastrabajador: TWideStringField;
    dsqproductoslinea: TDataSource;
    qproductos2: TZReadOnlyQuery;
    qproductos2codigo: TWideStringField;
    qproductos2descripcion: TWideStringField;
    qproductos2imagen: TBlobField;
    qproductos2alto: TIntegerField;
    qproductos2ancho: TIntegerField;
    qproductos2largo: TIntegerField;
    qproductos2EANUPC: TLargeintField;
    qproductos2tara: TFloatField;
    qproductos2peso: TFloatField;
    qproductos2errorpeso: TFloatField;
    qproductos2pesadomanual: TIntegerField;
    qproductos2coste: TFloatField;
    qproductos2activo: TIntegerField;
    qproductos2mostrar_boton: TIntegerField;
    dsqproductos2: TDataSource;
    qpesadas: TZQuery;
    qpesadasid: TIntegerField;
    qpesadasidempresa: TIntegerField;
    qpesadasidEA: TIntegerField;
    qpesadasid_palet: TWideStringField;
    qpesadasnum_palets: TIntegerField;
    qpesadasid_palet2: TWideStringField;
    qpesadasnum_palets2: TIntegerField;
    qpesadasid_envase: TWideStringField;
    qpesadasnum_envases: TIntegerField;
    qpesadasiddestino: TIntegerField;
    qpesadasdestino: TWideStringField;
    qpesadaspeso_bruto: TFloatField;
    qpesadastara: TFloatField;
    qpesadaspeso_neto: TFloatField;
    qpesadaspeso_neto_teorico: TFloatField;
    qpesadaspeso_medio: TFloatField;
    qpesadastemperatura: TFloatField;
    qpesadasfecha: TDateField;
    qpesadasfechatrabajo: TDateField;
    qpesadashora: TTimeField;
    qpesadasCEA: TWideStringField;
    qpesadasresto_cajas: TFloatField;
    qpesadasresto_pesoneto: TFloatField;
    qpesadaslote: TIntegerField;
    qpesadasORIGEN: TWideStringField;
    qpesadasidorigen: TIntegerField;
    qpesadasmprima: TWideStringField;
    qpesadasgen_caja: TIntegerField;
    qpesadascosto: TFloatField;
    qpesadascoste_manipulacion: TFloatField;
    qpesadasdias_camara: TIntegerField;
    qpesadascosto_camara: TFloatField;
    qpesadascodigoR: TWideStringField;
    qpesadasparcela: TWideStringField;
    qpesadasfecha_cierre: TDateField;
    qpesadascosto_materiales: TFloatField;
    qpesadascosto_cosechar: TFloatField;
    qpesadasborrado: TIntegerField;
    qpesadasseleccionado: TIntegerField;
    qpesadasOBSERVACIONES: TWideStringField;
    qpesadastipo_trabajo: TWideStringField;
    qpesadastipo_generador: TWideStringField;
    qpesadasremanipulado: TIntegerField;
    qpesadasfecha_envasado: TDateField;
    qpesadascodean: TWideStringField;
    qpesadascodssc: TWideStringField;
    qpesadaspedidocliente: TIntegerField;
    qpesadascosto_sinmt: TFloatField;
    qpesadastara0: TFloatField;
    qpesadaslotes_detalle: TWideStringField;
    qpesadasidpedido: TIntegerField;
    qpesadasrecuperar_envases: TIntegerField;
    qpesadascalibre: TFloatField;
    qpesadasprov_albaran: TWideStringField;
    qpesadasprov_fecha: TDateField;
    qpesadasprov_ggn: TWideStringField;
    qpesadasprov_lote: TWideStringField;
    qpesadasprov_grower: TWideStringField;
    qpesadasprov_puk: TWideStringField;
    qpesadasprov_otros: TWideStringField;
    qpesadasidalbaran: TWideStringField;
    qpesadassector: TZQuery;
    qpesadassectoride: TIntegerField;
    qpesadassectorid: TIntegerField;
    qpesadassectoridea: TIntegerField;
    qpesadassectoridsector: TIntegerField;
    qpesadassectordescsector: TWideStringField;
    qpesadassectorfecha: TDateField;
    qpesadassectorhora: TTimeField;
    qpesadassectorpeso_neto: TFloatField;
    qpesadassectorLOTE: TWideStringField;
    qpesadassectorVARIEDAD: TWideStringField;
    esclavo: TZQuery;
    esclavoSlave_IO_State: TWideStringField;
    esclavoMaster_Host: TWideStringField;
    esclavoMaster_User: TWideStringField;
    esclavoMaster_Port: TLargeintField;
    esclavoConnect_Retry: TLargeintField;
    esclavoMaster_Log_File: TWideStringField;
    esclavoRead_Master_Log_Pos: TFloatField;
    esclavoRelay_Log_File: TWideStringField;
    esclavoRelay_Log_Pos: TFloatField;
    esclavoRelay_Master_Log_File: TWideStringField;
    esclavoSlave_IO_Running: TWideStringField;
    esclavoSlave_SQL_Running: TWideStringField;
    esclavoReplicate_Do_DB: TWideStringField;
    esclavoReplicate_Ignore_DB: TWideStringField;
    esclavoReplicate_Do_Table: TWideStringField;
    esclavoReplicate_Ignore_Table: TWideStringField;
    esclavoReplicate_Wild_Do_Table: TWideStringField;
    esclavoReplicate_Wild_Ignore_Table: TWideStringField;
    esclavoLast_Errno: TLargeintField;
    esclavoLast_Error: TWideStringField;
    esclavoSkip_Counter: TLargeintField;
    esclavoExec_Master_Log_Pos: TFloatField;
    esclavoRelay_Log_Space: TFloatField;
    esclavoUntil_Condition: TWideStringField;
    esclavoUntil_Log_File: TWideStringField;
    esclavoUntil_Log_Pos: TFloatField;
    esclavoMaster_SSL_Allowed: TWideStringField;
    esclavoMaster_SSL_CA_File: TWideStringField;
    esclavoMaster_SSL_CA_Path: TWideStringField;
    esclavoMaster_SSL_Cert: TWideStringField;
    esclavoMaster_SSL_Cipher: TWideStringField;
    esclavoMaster_SSL_Key: TWideStringField;
    esclavoSeconds_Behind_Master: TFloatField;
    esclavoMaster_SSL_Verify_Server_Cert: TWideStringField;
    esclavoLast_IO_Errno: TLargeintField;
    esclavoLast_IO_Error: TWideStringField;
    esclavoLast_SQL_Errno: TLargeintField;
    esclavoLast_SQL_Error: TWideStringField;
    esclavoReplicate_Ignore_Server_Ids: TWideStringField;
    esclavoMaster_Server_Id: TLargeintField;
    esclavoMaster_SSL_Crl: TWideStringField;
    esclavoMaster_SSL_Crlpath: TWideStringField;
    esclavoUsing_Gtid: TWideStringField;
    esclavoGtid_IO_Pos: TWideStringField;
    esclavoReplicate_Do_Domain_Ids: TWideStringField;
    esclavoReplicate_Ignore_Domain_Ids: TWideStringField;
    esclavoParallel_Mode: TWideStringField;
    ZReadOnlyQuery1: TZReadOnlyQuery;
    qaux6: TZReadOnlyQuery;
    qaux4: TZReadOnlyQuery;
    qaux3: TZReadOnlyQuery;
    qaux2: TZReadOnlyQuery;
    qaux: TZReadOnlyQuery;
    qaux5: TZReadOnlyQuery;
    dsqaux2: TDataSource;
    dsqaux: TDataSource;
    dsqmprimas: TDataSource;
    qsectores: TZReadOnlyQuery;
    qsectoreside: TIntegerField;
    qsectoresid: TIntegerField;
    qsectoresdescripcion: TWideStringField;
    qsectoresidalmacen: TIntegerField;
    qsectoresdescalmacen: TWideStringField;
    dsqsectores: TDataSource;
    qmprimas: TZReadOnlyQuery;
    qmprimasid: TWideStringField;
    qmprimasdescripcion: TWideStringField;
    qmprimasobservaciones: TWideStringField;
    qmprimasimagen: TBlobField;
    qmprimasactivo: TIntegerField;
    qmprimasidempresa: TIntegerField;
    qmprimasvariedad: TWideStringField;
    qmprimaspesopieza: TFloatField;
    dsproductos: TDataSource;
    qproductos: TZReadOnlyQuery;
    qproductoscodigo: TWideStringField;
    qproductosdescripcion: TWideStringField;
    qproductosimagen: TBlobField;
    qproductosalto: TIntegerField;
    qproductosancho: TIntegerField;
    qproductoslargo: TIntegerField;
    qproductostara: TFloatField;
    qproductosEANUPC: TLargeintField;
    qproductospeso: TFloatField;
    qproductoserrorpeso: TFloatField;
    qproductospesadomanual: TIntegerField;
    qproductoscoste: TFloatField;
    qproductosactivo: TIntegerField;
    qproductosmostrar_boton: TIntegerField;
    trabajo_diario: TZTable;
    trabajo_diarioid: TIntegerField;
    trabajo_diarioempresa: TIntegerField;
    trabajo_diariofecha: TDateField;
    trabajo_diarioparcela: TWideStringField;
    trabajo_diarioidt: TIntegerField;
    trabajo_diariotrabajador: TWideStringField;
    trabajo_diariotypework: TWideStringField;
    trabajo_diarioarticulo: TWideStringField;
    trabajo_diarioqhoras: TFloatField;
    trabajo_diarioprecio: TFloatField;
    trabajo_diariototal: TFloatField;
    trabajo_diarioestado: TWideStringField;
    trabajo_diarioorigen: TWideStringField;
    trabajo_diariofechainsmod: TDateTimeField;
    trabajo_diariousuario: TIntegerField;
    trabajo_diariocontrol: TIntegerField;
    trabajo_diariohoras_nomina: TFloatField;
    trabajo_diariohoras_acumuladas: TFloatField;
    trabajo_diarioidparte1: TIntegerField;
    trabajo_diarioidparte2: TIntegerField;
    lc_trabajo_diario: TZTable;
    lc_trabajo_diarioid: TIntegerField;
    lc_trabajo_diariofecha: TDateField;
    lc_trabajo_diarioparcela: TWideStringField;
    lc_trabajo_diarioidt: TIntegerField;
    lc_trabajo_diariotrabajador: TWideStringField;
    lc_trabajo_diariotypework: TWideStringField;
    lc_trabajo_diarioarticulo: TWideStringField;
    lc_trabajo_diarioqhoras: TFloatField;
    lc_trabajo_diarioprecio: TFloatField;
    lc_trabajo_diariototal: TFloatField;
    lc_trabajo_diarioestadotrabajodiario: TWideStringField;
    lc_trabajo_diarioestado: TIntegerField;
    lc_trabajo_diarioorigen: TWideStringField;
    lc_trabajo_diariofechainsmod: TDateTimeField;
    lc_trabajo_diariousuario: TIntegerField;
    lc_trabajo_diariocontrol: TIntegerField;
    qmprimas2: TZReadOnlyQuery;
    qmprimas2id: TWideStringField;
    qmprimas2descripcion: TWideStringField;
    qmprimas2observaciones: TWideStringField;
    qmprimas2imagen: TBlobField;
    qmprimas2activo: TIntegerField;
    qmprimas2pesopieza: TFloatField;
    dsqmprimas2: TDataSource;
    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;
    dsqmovimientos_mesa: TDataSource;
    dbservidor: TZConnection;
    DBagrigest: TZConnection;
    produccionx: TZTable;
    produccionxid: TLargeintField;
    produccionxfecha: TDateField;
    produccionxhora: TTimeField;
    produccionxmesa: TWideStringField;
    produccionxbascula: TWideStringField;
    produccionxidt: TIntegerField;
    produccionxembasadora: TWideStringField;
    produccionxmateria_prima: TWideStringField;
    produccionxproducto: TWideStringField;
    produccionxtara: TFloatField;
    produccionxpeso_teorico: TFloatField;
    produccionxpeso_real: TFloatField;
    rendimiento: TZTable;
    rendimientoid: TIntegerField;
    rendimientofecha: TDateField;
    rendimientohora: TTimeField;
    rendimientoidt: TIntegerField;
    rendimientotrabajador: TWideStringField;
    rendimientomateria_prima: TWideStringField;
    rendimientopeso_bruto: TFloatField;
    rendimientotara: TFloatField;
    rendimientopesoneto: TFloatField;
    rendimientonum_limpiadora: TIntegerField;
    lc_rendimiento: TZTable;
    lc_rendimientoid: TIntegerField;
    lc_rendimientofecha: TDateField;
    lc_rendimientohora: TTimeField;
    lc_rendimientoidt: TIntegerField;
    lc_rendimientotrabajador: TWideStringField;
    lc_rendimientomateria_prima: TWideStringField;
    lc_rendimientopeso_bruto: TFloatField;
    lc_rendimientotara: TFloatField;
    lc_rendimientopesoneto: TFloatField;
    lc_rendimientoestado: TIntegerField;
    lc_rendimientonum_limpiadora: TIntegerField;
    lc_produccion: TZTable;
    lc_produccionid: TLargeintField;
    lc_produccionfecha: TDateField;
    lc_produccionhora: TTimeField;
    lc_produccionmesa: TWideStringField;
    lc_produccionbascula: TWideStringField;
    lc_produccionidt: TIntegerField;
    lc_produccionembasadora: TWideStringField;
    lc_produccionmateria_prima: TWideStringField;
    lc_produccionproducto: TWideStringField;
    lc_producciontara: TFloatField;
    lc_produccionpeso_teorico: TFloatField;
    lc_produccionpeso_real: TFloatField;
    lc_produccionestado: TIntegerField;
    lc_movimientos_mesa: TZTable;
    lc_movimientos_mesaid: TLargeintField;
    lc_movimientos_mesafecha: TDateField;
    lc_movimientos_mesamesa: TWideStringField;
    lc_movimientos_mesaidt: TIntegerField;
    lc_movimientos_mesaTRABAJADOR: TWideStringField;
    lc_movimientos_mesaENTRADA_REAL: TTimeField;
    lc_movimientos_mesaSALIDA_REAL: TTimeField;
    lc_movimientos_mesaENTRADA_AJUSTADA: TTimeField;
    lc_movimientos_mesaSALIDA_AJUSTADA: TTimeField;
    lc_movimientos_mesaHORASREALES: TTimeField;
    lc_movimientos_mesaHORASAJUSTADAS: TTimeField;
    lc_movimientos_mesaPUESTO: TWideStringField;
    lc_movimientos_mesaTIPOTRABAJO: TWideStringField;
    lc_movimientos_mesaARTICULO: TWideStringField;
    lc_movimientos_mesaPARCELA: TWideStringField;
    lc_movimientos_mesaestado: TIntegerField;
    lc_movimientos_mesanum_limpiadora: TIntegerField;
    movimientos_mesa: TZTable;
    movimientos_mesaid: TLargeintField;
    movimientos_mesafecha: TDateField;
    movimientos_mesamesa: TWideStringField;
    movimientos_mesaidt: TIntegerField;
    movimientos_mesaTRABAJADOR: TWideStringField;
    movimientos_mesaENTRADA_REAL: TTimeField;
    movimientos_mesaSALIDA_REAL: TTimeField;
    movimientos_mesaENTRADA_AJUSTADA: TTimeField;
    movimientos_mesaSALIDA_AJUSTADA: TTimeField;
    movimientos_mesaHORASREALES: TTimeField;
    movimientos_mesaHORASAJUSTADAS: TTimeField;
    movimientos_mesaPUESTO: TWideStringField;
    movimientos_mesaTIPOTRABAJO: TWideStringField;
    movimientos_mesaARTICULO: TWideStringField;
    movimientos_mesaPARCELA: TWideStringField;
    movimientos_mesanum_limpiadora: TIntegerField;
    procedure tpesarTimer(Sender: TObject);
    procedure Ta_ceroTimer(Sender: TObject);
    procedure TbotonTimer(Sender: TObject);
    procedure Timer4Timer(Sender: TObject);
    procedure tcomprobarTimer(Sender: TObject);
    procedure tsincronizarTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Ta_ceroTimer(Sender: TObject);
var pesoletras:string;
    peso2:real;
begin
    pesoletras:=lpeso.Caption;

    try
    peso2:=strtofloat(pesoletras);
    except
        on error: exception do
        begin
           peso2:=-1;
        end;
    end;
    pesar.Visible:=false;

    if qproductospesadomanual.Asinteger=1 then
    begin
        ppesadas.Caption:='RETIRE EL ENVASE';
        lpeso.Visible:=false;
        lpiezas.Caption:='0';
        tpesar.Enabled:=false;
        ppesadas.Color:=clfuchsia;
        pesar.Visible:=false;

        if peso2=0 then
        begin
            a_cero:=true;
            ta_cero.Enabled:=false;
            ppesadas.Caption:='';

        end;
        if a_cero then
        begin
          ppesadas.Color:=clBtnFace;
          tpesar.Enabled:=true;
          lpeso.Visible:=true;
        end;

    end;
    if qproductospesadomanual.Asinteger=0 then
    begin
//        ppesadas.Color:=clred;
        ppesadas.Caption:='RETIRE EL ENVASE';
        lpiezas.Caption:='0';
        lpeso.visible:=false;
        ppesadas.Color:=clfuchsia;
        if peso2=0 then
        begin
            a_cero:=true;
            ta_cero.Enabled:=false;
            ppesadas.Caption:='';
        end;
        if a_cero then
        begin
          ppesadas.Color:=clBtnFace;
          tpesar.Enabled:=true;
          lpeso.Visible:=true;
        end;
    end;

end;

procedure TForm1.TbotonTimer(Sender: TObject);
var pesoletras:string;
    peso,peso2,pesoconerror:real;
begin
    pesoletras:=lpeso.Caption;
    try
    peso2:=strtofloat(pesoletras);
    except
        on error: exception do
        begin
           peso2:=-1;
        end;
    end;
//
    peso:=(qproductospeso.AsFloat+qproductostara.AsFloat);//*1000;
    pesoconerror:=(qproductospeso.AsFloat+qproductostara.AsFloat+qproductoserrorpeso.AsFloat);//*1000;
    if (qproductospesadomanual.AsInteger=1) and a_cero then
    begin
        if estable and (peso2>0)then
        begin
            pesar.visible:=true;
            pesovalido:=peso2;
            tpesar.enabled:=true;
        end
        else
        begin
           pesar.visible:=false;
           tpesar.enabled:=false;
        end;
    end;
    if (qproductospesadomanual.AsInteger=0) and (qproductosmostrar_boton.AsInteger=1) and a_cero then
    begin
        if estable then
        begin
            pesar.visible:=true;
            pesovalido:=peso2;
            tpesar.enabled:=true;
        end
        else
        begin
           pesar.visible:=false;
           tpesar.enabled:=false;
        end;
    end;
 if (qproductospesadomanual.AsInteger=0) and (qproductosmostrar_boton.AsInteger=0) and a_cero then
    begin
        if estable then
        begin
        if (peso2>=peso) and (peso2<=pesoconerror) and (estable) and (a_cero) then
          begin
            lc_produccion.Open;
            lc_produccion.Append;
            lc_produccionfecha.value:=date;//gfecha;
            lc_produccionhora.value:=time;
            lc_produccionmesa.value:=lmesa.Caption;
            lc_produccionbascula.value:=lbascula.Caption;
            lc_produccionidt.value:=qenvasadorasidt.asinteger;
            lc_produccionembasadora.value:=qenvasadorastrabajador.asstring;
            lc_produccionmateria_prima.value:=qmprimasid.asstring;
            lc_produccionproducto.value:=qproductoscodigo.asstring;
            lc_producciontara.value:=qproductostara.asfloat;
            lc_produccionpeso_teorico.value:=qproductostara.AsFloat+qproductospeso.AsFloat;
            lc_produccionpeso_real.Value:=peso2;
            lc_produccionestado.Value:=1;
            lc_produccion.Post;
            ppesadas.Caption:='PESADA REALIZADA';

            a_cero:=false;
            ta_cero.Enabled:=true;
            estable:=false;
            bloqueado:=false;
          end;
        end
        else
        begin
           pesar.visible:=false;
           tpesar.enabled:=false;
        end;
    end;
end;


procedure TForm1.tcomprobarTimer(Sender: TObject);
var idproducto,idmprima:string;
begin
 tcomprobar.Interval:=60000;
//    dbservidor.Connected:=false;
    try
    idproducto:=qproductoscodigo.AsString;
    qproductos.connection:=dbservidor;
    qproductos.sql.clear;
    qproductos.sql.add('select * from productos WHERE (codigo) in (select codigo from productos_sector ');
    qproductos.SQL.Add(' where idsector=(select idsector from sectores_mesas where id=:MESA)) order by descripcion');

    try
      try
      qproductos.Close;
      qproductos.Prepare;
      qproductos.ParamByName('MESA').AsInteger:=strtoint(tmesa);
      qproductos.open;
      except
          qproductos.Close;
          qproductos.Prepare;
          qproductos.ParamByName('MESA').AsInteger:=strtoint(tmesa);
          qproductos.connection:=dbagrigest;
          qproductos.Open;
      end;
    finally
      qproductos.Locate('codigo',idproducto,[locaseinsensitive]);
      if qproductos.RecordCount>0 then comboproducto.KeyValue:=qproductoscodigo.AsString;
      if qproductos.RecordCount=0 then
      begin
          idproducto:=qproductoscodigo.AsString;
          qproductos.close;
          qproductos.sql.clear;
          qproductos.sql.add('select * from productos WHERE ACTIVO=1 ORDER BY descripcion');
          qproductos.open;
          qproductos.Locate('codigo',idproducto,[locaseinsensitive]);
      end;
    end;

    idmprima:=combovariedad.Text;//.AsString;
    qmprimas.sql.clear;
    qmprimas.sql.add('select * from MATERIAS_PRIMAS WHERE (idempresa,id) in (select ide,mprima from materias_primas_sector ');
    qmprimas.SQL.Add(' where idsector=(select idsector from sectores_mesas where id=:MESA)) order by variedad');
    qmprimas.Prepare;
    qmprimas.ParamByName('MESA').AsInteger:=strtoint(tmesa);
    try
      try
        qmprimas.Open;
      except
        qmprimas.Close;
        qmprimas.Connection:=dbagrigest;
        qmprimas.Open;
      end;
    finally
    qmprimas.Locate('id',idmprima,[locaseinsensitive]);

    if qmprimas.RecordCount>0 then combovariedad.KeyValue:=qmprimasid.AsString;
    if (qmprimas.RecordCount=0) or (dbservidor.HostName='localhost') then
    begin
        idmprima:=qmprimasid.AsString;
        qmprimas.close;
        qmprimas.sql.clear;
        qmprimas.sql.add('select * from MATERIAS_PRIMAS WHERE mesas=1 and activo=1 ORDER BY descripcion');
        qmprimas.open;
        qmprimas.Locate('id',idmprima,[locaseinsensitive]);
    end;

    end;

    if qenvasadoras.RecordCount=1 then comboenvasadoras.KeyValue:=qenvasadorasidt.asstring;
    variedad:=qmprimasid.asstring;
    lvariedad.caption:=idmprima;//qmprimasid.AsString;
    producto:=qproductoscodigo.AsString;
//    dbservidor.Connected:=false;
    timer4.Enabled:=false;
    except
    tcomprobar.Interval:=5000;
      try
         DBservidor.connected:=true;
         lconectado.caption:='CONECTADO A: ' +thost;
         timer4.enabled:=true;
      except
         dbservidor.HostName:='localhost';
         dbservidor.User:='root';
         dbservidor.Password:='';
         lesclavo.Color:=clred;
         lconectado.caption:='CONECTADO A: EQUIPO LOCAL';
         dbservidor.Connected:=true;
         timer4.enabled:=false;
      end;

    end;
end;

procedure TForm1.Timer4Timer(Sender: TObject);
var       anyo,mes,dia:word;
       vanyo,vmes,vdia,vfecha:string;
begin

      qmovimientos_mesa.close;
      qmovimientos_mesa.SQL.Clear;
      qmovimientos_mesa.sql.add(' select * from movimiento_mesas_local where mesa='+''''+lmesa.Caption+''''+' and salida_real is null ');
      qmovimientos_mesa.sql.add(' order by trabajador');
      qmovimientos_mesa.open;
    decodedate(date,anyo,mes,dia);
    vanyo:=floattostr(anyo);
    vmes:=floattostr(mes);
    vdia:=floattostr(dia);
    if length(vmes)=1 then vmes:='0'+vmes;
    if length(vdia)=1 then vdia:='0'+vdia;
    vfecha:=vanyo+'-'+vmes+'-'+vdia;
    tcalendario_laboral.refresh;
    tcalendario_laboral.locate('fecha',vfecha,[locaseinsensitive,lopartialkey]);


end;

procedure TForm1.tpesarTimer(Sender: TObject);
var    pesoletras:string;
       peso,peso2,peso3,pesoconerror:real;
       anyo,mes,dia:word;
       vanyo,vmes,vdia,vfecha:string;
       piezas:integer;
begin

//    qpesadassector.close;
//    qpesadassector.Open;
    qmprimas.Locate('id',variedad,[locaseinsensitive]);
    decodedate(date,anyo,mes,dia);
    vanyo:=floattostr(anyo);
    vmes:=floattostr(mes);
    vdia:=floattostr(dia);
    if length(vmes)=1 then vmes:='0'+vmes;
    if length(vdia)=1 then vdia:='0'+vdia;
    vfecha:=vanyo+'-'+vmes+'-'+vdia;
    tcalendario_laboral.refresh;
    tcalendario_laboral.locate('fecha',vfecha,[locaseinsensitive,lopartialkey]);

    pesoletras:=lpeso.Caption;

    try
    peso2:=strtofloat(pesoletras);
    except
        on error: exception do
        begin
           peso2:=0;
        end;
    end;
//

    bloqueominimo:=qproductospeso.AsFloat+qproductostara.AsFloat;
    bloqueominimo:=bloqueominimo*(porcentagebloqueo/100);   // bloqueominimo/1.2
    if (peso2>bloqueominimo) and a_cero then
    begin
       bloqueado:=true;
    end;

    if bloqueado and (peso2<0.100) and a_cero and desbloqueado then
    begin
       desbloqueado:=false;
       bloqueado:=false;
       tpesar.Enabled:=false;
       tsincronizar.Enabled:=false;
 //      puerto1.Connected:=false;
       fdesbloqueo := Tfdesbloqueo.create(nil);
       try
         fdesbloqueo.showmodal();

       finally
         fdesbloqueo.free;
         desbloqueado:=true;
         tpesar.Enabled:=true;
         puerto1.Connected:=true;
         qdesbloqueos.Open;
         qdesbloqueos.append;
         qdesbloqueosenvasadora.Value:=pprincipal.qenvasadorastrabajador.asstring;
         qdesbloqueosmesa.Value:=pprincipal.lmesa.Caption;
         qdesbloqueosbascula.Value:=pprincipal.lbascula.Caption;
         qdesbloqueosestado.Value:=1;
         qdesbloqueosfecha.Value:=date;
         qdesbloqueoshora.value:=time;
         qdesbloqueos.Post;
         qdesbloqueos.Close;
         tsincronizar.Enabled:=true;
       end;
    end;

//    peso2:=peso2*1000;
    if qproductospesadomanual.Asinteger=1 then
    begin
        try
         tboton.Enabled:=true;
         lpesominimo.Caption:='';
         lpesomaximo.Caption:='';
         peso3:=strtofloat(pesoletras);

          except
              on error: exception do
              begin
                 peso3:=0;
              end;
          end;
    end;

    if qproductospesadomanual.Asinteger=0 then
    begin

        lpesominimo.Caption:='Peso Minimo: '+floattostr(qproductospeso.AsFloat+qproductostara.AsFloat);
        lpesomaximo.Caption:='Peso Maximo: '+floattostr(qproductospeso.AsFloat+qproductostara.AsFloat+qproductoserrorpeso.AsFloat);
        peso:=(qproductospeso.AsFloat+qproductostara.AsFloat);//*1000;
        pesoconerror:=(qproductospeso.AsFloat+qproductostara.AsFloat+qproductoserrorpeso.AsFloat);//*1000;

        if (peso2<peso) and a_cero then
        begin
           if vpesopieza=0 then vpesopieza:=1;
           lpiezas.caption:='+'+floattostr(trunc((peso*1000-peso2*1000)/vpesopieza)+1);
           ppesadas.Color:=clyellow;
        end;
        if (peso2>=peso) and (peso2<=pesoconerror) and (estable) and (a_cero) then
        begin
            ppesadas.Color:=cllime;
            lpiezas.Caption:='0';
            if qproductosmostrar_boton.asinteger=1 then tboton.Enabled:=true
            else
                begin
                    tboton.Enabled:=true;
                end;
        end;
        if (peso2>pesoconerror) and a_cero then
        begin
           if vpesopieza=0 then vpesopieza:=1;
           lpiezas.caption:='-'+floattostr(trunc((peso2*1000-pesoconerror*1000)/vpesopieza)+1);
           ppesadas.Color:=clred;
        end;
    end;

end;

procedure TForm1.tsincronizarTimer(Sender: TObject);
var sreal,sajustada,hreal,hajustada:string;
begin
if treplicacion='1' then
begin
    esclavo.Close;
    esclavo.Open;
    TRY
    if esclavo.Active then
    begin
        replicando:=esclavoseconds_behind_master.AsInteger;
        if esclavoslave_sql_running.Asstring='Yes' then corriendo:=1 else corriendo:=0;
        if (replicando=0) and (corriendo=0) then
        begin
         PCONEXION.Color:=clred;
         lesclavo.Caption:='Replicacion detenida: '+esclavolast_sql_error.asstring;
         qrecuperar.SQL.Clear;
         qrecuperar.SQL.Add('set global sql_slave_skip_counter=1');
         qrecuperar.ExecSQL;
         qrecuperar.SQL.Clear;
         qrecuperar.SQL.Add('start slave');
         qrecuperar.ExecSQL;
        end;
        if (replicando>0) and (corriendo=0) then
        begin
         PCONEXION.Color:=clred;
         lesclavo.Caption:='Error en replicacion: '+esclavolast_sql_error.asstring;
        end;

        if (replicando>0) and (corriendo=1) then
          begin
             PCONEXION.Color:=clLime;
             lesclavo.Caption:='Datos pendientes de actualizar: '+inttostr(replicando);
          end;
        if (replicando=0) and (corriendo=1) then
          begin
             PCONEXION.Color:=clLime;
             lesclavo.Caption:='Terminal Actualizado';
          end;
    end else esclavo.Active:=true;
    EXCEPT
      /// tsincronizar.Enabled:=false;

    END;
end else
       begin
             PCONEXION.Color:=clLime;
             lesclavo.Caption:='Actualizacion suspendida';
       end;

end;

end.
