unit gastosconsumos;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Grids,
  Vcl.DBGrids, Data.DB, ZAbstractRODataset, ZDataset, Vcl.ComCtrls,
  ZAbstractDataset, Vcl.Mask, Vcl.DBCtrls;

type
  Tfconsumosgastos = class(TForm)
    Panel4: TPanel;
    Label36: TLabel;
    btAcancelar: TButton;
    btAmodificar: TButton;
    btAborrar: TButton;
    btAnuevo: TButton;
    btAguardar: TButton;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel7: TPanel;
    Panel6: TPanel;
    Panel8: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Edit2: TEdit;
    DBGrid2: TDBGrid;
    Panel9: TPanel;
    Panel10: TPanel;
    Label5: TLabel;
    Label6: TLabel;
    Edit3: TEdit;
    DBGrid3: TDBGrid;
    tfecha: TDateTimePicker;
    Label1: TLabel;
    qcentrocoste: TZReadOnlyQuery;
    dsqcentrocoste: TDataSource;
    qcentrocostecodpar: TWideStringField;
    qcentrocosteempresa: TIntegerField;
    qcentrocostenombre: TWideStringField;
    dsqarticulos: TDataSource;
    qarticulos: TZReadOnlyQuery;
    qarticulosidempresa: TIntegerField;
    qarticulosid: TIntegerField;
    qarticuloscodigo: TWideStringField;
    qarticulosdescripcion: TWideStringField;
    DBGrid1: TDBGrid;
    consumosgastos: TZQuery;
    consumosgastosid: TIntegerField;
    consumosgastoside: TIntegerField;
    consumosgastosida: TIntegerField;
    consumosgastosfecha: TDateField;
    consumosgastoscantidad: TFloatField;
    consumosgastosobservaciones: TWideStringField;
    consumosgastosdescripcion: TWideStringField;
    qconsumosgastos: TZReadOnlyQuery;
    dsqconsumosgastos: TDataSource;
    qconsumosgastosid: TIntegerField;
    qconsumosgastoside: TIntegerField;
    qconsumosgastosida: TIntegerField;
    qconsumosgastosfecha: TDateField;
    qconsumosgastoscantidad: TFloatField;
    qconsumosgastosobservaciones: TWideStringField;
    qconsumosgastosdescripcion: TWideStringField;
    qconsumosgastosnombre: TWideStringField;
    qconsumosgastoscodigo: TWideStringField;
    DBEdit1: TDBEdit;
    dsconsumosgastos: TDataSource;
    DBText1: TDBText;
    qconsumosgastosidcc: TWideStringField;
    consumosgastosidcc: TWideStringField;
    DBEdit2: TDBEdit;
    DataSource1: TDataSource;
    qcentrocoste2: TZReadOnlyQuery;
    Label2: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    DBEdit3: TDBEdit;
    DBEdit4: TDBEdit;
    DBMemo1: TDBMemo;
    qcentrocoste2empresa: TIntegerField;
    qcentrocoste2codpar: TWideStringField;
    qcentrocoste2nombre: TWideStringField;
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure dsconsumosgastosStateChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btAnuevoClick(Sender: TObject);
    procedure btAborrarClick(Sender: TObject);
    procedure btAmodificarClick(Sender: TObject);
    procedure btAguardarClick(Sender: TObject);
    procedure dsqcentrocosteDataChange(Sender: TObject; Field: TField);
    procedure btAcancelarClick(Sender: TObject);
    procedure dsqarticulosDataChange(Sender: TObject; Field: TField);
    procedure dsqconsumosgastosDataChange(Sender: TObject; Field: TField);
    procedure consumosgastosBeforeOpen(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fconsumosgastos: Tfconsumosgastos;

implementation

{$R *.dfm}

procedure Tfconsumosgastos.btAborrarClick(Sender: TObject);
begin
    consumosgastos.Delete;
    qconsumosgastos.Refresh;
end;

procedure Tfconsumosgastos.btAcancelarClick(Sender: TObject);
begin
    consumosgastos.Cancel;
end;

procedure Tfconsumosgastos.btAguardarClick(Sender: TObject);
var id,ide:string;
begin
    consumosgastos.post;
    id:=consumosgastosid.asstring;
    ide:=consumosgastoside.asstring;
    qconsumosgastos.Refresh;
    qconsumosgastos.Locate('ide,id',vararrayof([ide,id]),[locaseinsensitive]);
    consumosgastos.SQL.Clear;
    consumosgastos.SQL.Add('select * from consumosgastos where ide='+ide+' and id='+ide);
    consumosgastos.Open;
end;

procedure Tfconsumosgastos.btAmodificarClick(Sender: TObject);
begin
    consumosgastos.Edit;
end;

procedure Tfconsumosgastos.btAnuevoClick(Sender: TObject);
begin
    if consumosgastos.Active=false then consumosgastos.open;
    consumosgastos.Append;
    consumosgastosfecha.Value:=tfecha.Date;
    consumosgastoside.Value:=qcentrocosteempresa.asinteger;
    consumosgastosidcc.Value:=qcentrocostecodpar.AsString;
    consumosgastosida.Value:=qarticulosid.AsInteger;
    consumosgastosdescripcion.Value:=qarticulosdescripcion.AsString;
end;

procedure Tfconsumosgastos.consumosgastosBeforeOpen(DataSet: TDataSet);
var ide,id:string;
begin
   if qconsumosgastos.Active=true then
   begin
        ide:=qconsumosgastoside.asstring;
        id:=qconsumosgastosid.asstring;
        consumosgastos.SQL.Clear;
        consumosgastos.SQL.Add('select * from consumosgastos where ide='+ide+' and id='+ide);
        tfecha.date:=consumosgastosfecha.AsDateTime;
   end;
end;

procedure Tfconsumosgastos.dsconsumosgastosStateChange(Sender: TObject);
begin
    if consumosgastos.State in [dsedit,dsinsert] then
    begin
        btanuevo.Enabled:=false;
        btamodificar.Enabled:=false;
        btaborrar.Enabled:=false;
        btacancelar.Enabled:=true;
        btaguardar.Enabled:=true;
        panel1.Enabled:=true;
//        btfanterior.Enabled:=false;
  //      btfsiguiente.Enabled:=false;
    //    btfimportar.Enabled:=false;
      //  btfimprimir.Enabled:=false;
    end else
    begin
        btanuevo.Enabled:=true;
        btamodificar.Enabled:=true;
        btaborrar.Enabled:=true;
        btacancelar.Enabled:=false;
        btaguardar.Enabled:=false;
        panel1.Enabled:=false;
  //        btfanterior.Enabled:=true;
//        btfsiguiente.Enabled:=true;
  //      btfimportar.Enabled:=true;
//        btfimprimir.Enabled:=true;
    end;

end;

procedure Tfconsumosgastos.dsqarticulosDataChange(Sender: TObject;
  Field: TField);
begin
   if consumosgastos.State in [dsedit,dsinsert] then
   begin
    consumosgastosida.Value:=qarticulosid.AsInteger;
    consumosgastosdescripcion.Value:=qarticulosdescripcion.AsString;
   end;

end;

procedure Tfconsumosgastos.dsqcentrocosteDataChange(Sender: TObject;
  Field: TField);
var ide,id:string;
begin
    if (qconsumosgastos.Active=true) then
    begin
    if (qconsumosgastos.RecordCount>0) then
      begin
        ide:=qconsumosgastoside.asstring;
        id:=qconsumosgastosid.asstring;
        consumosgastos.SQL.Clear;
        consumosgastos.SQL.Add('select * from consumosgastos where ide='+ide+' and id='+ide);
        consumosgastos.Open;
        tfecha.date:=consumosgastosfecha.AsDateTime;
      end;
    end;
   if consumosgastos.State in [dsedit,dsinsert] then
   begin
    consumosgastoside.Value:=qcentrocosteempresa.asinteger;
    consumosgastosidcc.Value:=qcentrocostecodpar.AsString;
   end;
end;

procedure Tfconsumosgastos.dsqconsumosgastosDataChange(Sender: TObject;
  Field: TField);
  var ide,id:string;
begin
    if (qconsumosgastos.RecordCount>0) then
      begin
        ide:=qconsumosgastoside.asstring;
        id:=qconsumosgastosid.asstring;
        consumosgastos.close;
        consumosgastos.Open;
      end;
end;

procedure Tfconsumosgastos.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    if (activecontrol is tdbgrid) then
    begin
        if Key = 13 then Key :=9;
    end;

end;

procedure Tfconsumosgastos.FormKeyPress(Sender: TObject; var Key: Char);
var formatos:tformatsettings;
punto:string;
begin
    GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatos);
    punto:=formatos.DecimalSeparator;
    if punto=',' then
    begin
        key:=upcase(key);
        if (key='.') and (activecontrol is tdbedit) then
        begin
           if  (activecontrol as tdbedit).Field.DataType in [ftfloat,ftCurrency,ftbcd,ftfmtbcd] then key:=','
        end;
        if (key='.') and (activecontrol is tdbgrid) then
        begin
            key:=',';
        end;
    end;
    if (activecontrol is tdbgrid) then
    begin
        if Key = #13 then Key :=#9;
    end;

    if (key=#13) then
    begin
        Perform(WM_NextDLGCtl,0,0);
        key:=#0;
    end;

end;

procedure Tfconsumosgastos.FormShow(Sender: TObject);
begin
   qcentrocoste.Close;
   qcentrocoste2.Close;
   qarticulos.Close;
   qconsumosgastos.Close;
   consumosgastos.Close;
   qconsumosgastos.open;
   qcentrocoste.open;
   qcentrocoste2.open;
   qarticulos.open;
   tfecha.date:=date();
end;

end.
