unit fechacierre;

interface

uses
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Comp.Client, uFireDacHelper,
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,datosproduccion, Vcl.StdCtrls,
  Vcl.ComCtrls, Vcl.ExtCtrls, Data.DB;

type
  Tpreguntarfecha = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    Label2: TLabel;
    calendario: TDateTimePicker;
    qaux: TFDQuery;
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure calendarioClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  preguntarfecha: Tpreguntarfecha;

implementation
{$R *.dfm}

procedure Tpreguntarfecha.Button1Click(Sender: TObject);
var anyo,mes,dia:word;
    vanyo,vmes,vdia,vmessig,vfecha:string;
begin
    decodedate(calendario.date,anyo,mes,dia);
    vanyo:=floattostr(anyo);
    vmes:=floattostr(mes);
    vmessig:=floattostr(mes+1);
    if length(vmes)=1 then vmes:='0'+vmes;
    if length(vmessig)=1 then vmessig:='0'+vmessig;
    if length(vdia)=1 then vdia:='0'+vdia;
    vmes:=floattostr(mes);
    vmessig:=floattostr(mes+1);
    vdia:=floattostr(dia);
    vfecha:=floattostr(anyo)+'-'+vmes+'-'+vdia;
    dmproduccion.qactualizar.sql.clear;
    dmproduccion.qactualizar.sql.add('select * from restos_dia where lote='+dmproduccion.qrestos_dialote.asstring+' and fecha='+''''+vfecha+'''');
    dmproduccion.qactualizar.open;
    if dmproduccion.qactualizar.RecordCount=0 then
    begin
        dmproduccion.qactualizar.sql.clear;
        dmproduccion.qactualizar.sql.add('insert into restos_dia (ide,idalmacen,lote,parcela,mprima,fecha,cant_inicial,cant_final) ');
        dmproduccion.qactualizar.sql.add(' select ide,idalmacen,lote,parcela,mprima,'+''''+vfecha+''''+',cant_final,0 from restos_dia where id='+dmproduccion.qrestos_diaid.AsString);
        dmproduccion.qactualizar.ExecSQL;
    end else
    begin
       qaux.SQL.Clear;
       qaux.SQL.Add('select sum(num_envases) from ea_pesadas where fechatrabajo='+''''+vfecha+''''+' and iddestino='+dmproduccion.qrestos_diaidalmacen.asstring+' and idempresa='+dmproduccion.qrestos_diaide.AsString+' and lote='+dmproduccion.qrestos_dialote.AsString);
       qaux.Open;
       if qaux.fields[0].AsInteger>0 then
       begin
           dmproduccion.qactualizar.sql.clear;
           dmproduccion.qactualizar.sql.add('update restos_dia set cant_inicial='+inttostr(qaux.Fields[0].Asinteger+dmproduccion.qrestos_diacant_final.Asinteger) +' where ide='+dmproduccion.qrestos_diaide.AsString+' and lote='+dmproduccion.qrestos_dialote.AsString+' and fecha='+''''+vfecha+'''');
           dmproduccion.qactualizar.ExecSQL;
       end else
       begin
           dmproduccion.qactualizar.sql.clear;
           dmproduccion.qactualizar.sql.add('update restos_dia set cant_inicial='+dmproduccion.qrestos_diacant_final.AsString +' where ide='+dmproduccion.qrestos_diaide.AsString+' and lote='+dmproduccion.qrestos_dialote.AsString+' and fecha='+''''+vfecha+'''');
           dmproduccion.qactualizar.ExecSQL;

       end;
    end;
    close;
    dmproduccion.qrestos_dia.Refresh;
    dmproduccion.qpartesdiarios.Edit;
    dmproduccion.qpartesdiariosfecha_traspaso.Value:=calendario.Date;
    dmproduccion.qpartesdiarios.Post;
end;

procedure Tpreguntarfecha.calendarioClick(Sender: TObject);
begin
   label2.Caption:=datetostr(calendario.Date);
end;

procedure Tpreguntarfecha.FormShow(Sender: TObject);
begin

    label1.Caption:='Prximo da de trabajo para el lote: '+dmproduccion.qrestos_dialote.asstring;
    calendario.Date:=date;
    label2.Caption:=datetostr(calendario.Date);
end;

end.
