unit fconsumos;

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, Data.DB,  
  Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.ComCtrls,
   Vcl.DBCtrls, Vcl.Mask, ZSqlUpdate, frxClass, frxDBSet;

type
  Tconsumos_gastos = class(TForm)
    Panel1: TPanel;
    btEnuevo: TButton;
    btEborrar: TButton;
    btEmodificar: TButton;
    btEguardar: TButton;
    btEcancelar: TButton;
    bteimprimir: TButton;
    Panel2: TPanel;
    qgrupos: TFDQuery;
    qfamilias: TFDQuery;
    qgruposidempresa: TIntegerField;
    qgruposid: TIntegerField;
    qgruposdescripcion: TWideStringField;
    qgruposactivo: TIntegerField;
    qfamiliasidempresa: TIntegerField;
    qfamiliasid: TIntegerField;
    qfamiliasdescripcion: TWideStringField;
    qfamiliasactivo: TIntegerField;
    qfamiliasgrupo: TIntegerField;
    qfamiliasCOMUN: TIntegerField;
    qfamiliasABONOS: TIntegerField;
    qfamiliasFITOSANITARIOS: TIntegerField;
    qfamiliasSUMINISTROS: TIntegerField;
    qfamiliasGENERADORES: TIntegerField;
    qparcelas: TFDQuery;
    qparcelascodpar: TWideStringField;
    qparcelasempresa: TIntegerField;
    qparcelasnombre: TWideStringField;
    qparcelasvariedad: TWideStringField;
    qparcelasnum_plantas: TIntegerField;
    qparcelasfecha_plantacion: TDateField;
    qparcelasmarco_plantacion: TWideStringField;
    qparcelasnum_goteros: TFloatField;
    qparcelasgastosgenerales: TIntegerField;
    qparcelasref_catastro: TWideStringField;
    qparcelassigpac_prov: TIntegerField;
    qparcelassigpac_muni: TIntegerField;
    qparcelassigpac_agre: TIntegerField;
    qparcelassigpac_zona: TIntegerField;
    qparcelassigpac_poli: TIntegerField;
    qparcelassigpac_pacr: TIntegerField;
    qparcelasx: TWideStringField;
    qparcelasy: TWideStringField;
    qparcelasdatum: TWideStringField;
    qparcelashuso: TIntegerField;
    qparcelaspropiedad: TWideStringField;
    qparcelasactivo: TIntegerField;
    qparcelasseleccionado: TIntegerField;
    qparcelassuperficie: TFloatField;
    qparcelasest_meteo: TWideStringField;
    qparcelascoste_kg_ref: TFloatField;
    qparcelasactivo_almacen: TIntegerField;
    qparcelasporc_desperdicio: TFloatField;
    qparcelasporc_merma: TFloatField;
    qparcelases_centrotrabajo: TIntegerField;
    qparcelassigpac_recinto: TIntegerField;
    qparcelaspie_planta: TWideStringField;
    qparcelasnumero: TWideStringField;
    qparcelastiene_gg: TIntegerField;
    qparcelastiene_dop: TIntegerField;
    qparcelasc_calidad: TIntegerField;
    qparcelasc_espcliente: TIntegerField;
    qparcelasc_etiquetado: TIntegerField;
    qparcelassello_gg: TWideStringField;
    qparcelassello_dop: TWideStringField;
    qparcelassello_igp: TWideStringField;
    qparcelastiene_igp: TIntegerField;
    qparcelastiene_ifs: TIntegerField;
    qparcelassello_ifs: TWideStringField;
    qparcelaspropietario: TIntegerField;
    combocentrocoste: TDBLookupComboBox;
    dsparcelas: TDataSource;
    Label1: TLabel;
    Label4: TLabel;
    combofamilia: TDBLookupComboBox;
    dsqgrupos: TDataSource;
    dsqfamilias: TDataSource;
    Label3: TLabel;
    ffecha: TDateTimePicker;
    dsqresponsables: TDataSource;
    qresponsables: TFDQuery;
    qresponsablesnombre: TWideStringField;
    qconsumos: TFDQuery;
    PageControl1: TPageControl;
    Listado: TTabSheet;
    Detalles: TTabSheet;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    Panel7: TPanel;
    Panel8: TPanel;
    Panel9: TPanel;
    checkfechas: TCheckBox;
    checkcc: TCheckBox;
    checkgrupos: TCheckBox;
    checkfamilias: TCheckBox;
    DBGrid1: TDBGrid;
    dsqconsumos: TDataSource;
    btbuscar: TButton;
    Panel10: TPanel;
    qparcelas2: TFDQuery;
    dsqparcelas2: TDataSource;
    dsqgrupos2: TDataSource;
    qgrupos2: TFDQuery;
    qfamilias2: TFDQuery;
    dsqfamilias2: TDataSource;
    qfamilias2idempresa: TIntegerField;
    qfamilias2id: TIntegerField;
    qfamilias2descripcion: TWideStringField;
    qfamilias2activo: TIntegerField;
    qfamilias2grupo: TIntegerField;
    qfamilias2COMUN: TIntegerField;
    qfamilias2ABONOS: TIntegerField;
    qfamilias2FITOSANITARIOS: TIntegerField;
    qfamilias2SUMINISTROS: TIntegerField;
    qfamilias2GENERADORES: TIntegerField;
    qlistadoconsumos: TFDQuery;
    dsqlistadoconsumos: TDataSource;
    DBGrid2: TDBGrid;
    qgrupos2idempresa: TIntegerField;
    qgrupos2id: TIntegerField;
    qgrupos2descripcion: TWideStringField;
    qgrupos2activo: TIntegerField;
    qparcelas2codpar: TWideStringField;
    qparcelas2empresa: TIntegerField;
    qparcelas2nombre: TWideStringField;
    qparcelas2variedad: TWideStringField;
    qparcelas2num_plantas: TIntegerField;
    qparcelas2fecha_plantacion: TDateField;
    qparcelas2marco_plantacion: TWideStringField;
    qparcelas2num_goteros: TFloatField;
    qparcelas2gastosgenerales: TIntegerField;
    qparcelas2ref_catastro: TWideStringField;
    qparcelas2sigpac_prov: TIntegerField;
    qparcelas2sigpac_muni: TIntegerField;
    qparcelas2sigpac_agre: TIntegerField;
    qparcelas2sigpac_zona: TIntegerField;
    qparcelas2sigpac_poli: TIntegerField;
    qparcelas2sigpac_pacr: TIntegerField;
    qparcelas2x: TWideStringField;
    qparcelas2y: TWideStringField;
    qparcelas2datum: TWideStringField;
    qparcelas2huso: TIntegerField;
    qparcelas2propiedad: TWideStringField;
    qparcelas2activo: TIntegerField;
    qparcelas2seleccionado: TIntegerField;
    qparcelas2superficie: TFloatField;
    qparcelas2est_meteo: TWideStringField;
    qparcelas2coste_kg_ref: TFloatField;
    qparcelas2activo_almacen: TIntegerField;
    qparcelas2porc_desperdicio: TFloatField;
    qparcelas2porc_merma: TFloatField;
    qparcelas2es_centrotrabajo: TIntegerField;
    qparcelas2sigpac_recinto: TIntegerField;
    qparcelas2pie_planta: TWideStringField;
    qparcelas2numero: TWideStringField;
    qparcelas2tiene_gg: TIntegerField;
    qparcelas2tiene_dop: TIntegerField;
    qparcelas2c_calidad: TIntegerField;
    qparcelas2c_espcliente: TIntegerField;
    qparcelas2c_etiquetado: TIntegerField;
    qparcelas2sello_gg: TWideStringField;
    qparcelas2sello_dop: TWideStringField;
    qparcelas2sello_igp: TWideStringField;
    qparcelas2tiene_igp: TIntegerField;
    qparcelas2tiene_ifs: TIntegerField;
    qparcelas2sello_ifs: TWideStringField;
    qparcelas2propietario: TIntegerField;
    qresponsablesnif: TWideStringField;
    qresponsablesfirma: TBlobField;
    Panel11: TPanel;
    DBGrid3: TDBGrid;
    qaux: TFDQuery;
    Panel14: TPanel;
    checkcampanyas: TCheckBox;
    dsqcampanyas: TDataSource;
    qcampanyas: TFDQuery;
    qcampanyasid: TIntegerField;
    qcampanyasidempresa: TIntegerField;
    qcampanyasid_anterior: TIntegerField;
    qcampanyasdescripcion: TWideStringField;
    qcampanyasfechainicio: TDateField;
    qcampanyasfechafin: TDateField;
    qcampanyasult_pesada: TIntegerField;
    pcentros: TPanel;
    combocc2: TDBLookupComboBox;
    pgrupos: TPanel;
    combogrupos2: TDBLookupComboBox;
    pfamilias: TPanel;
    combofamilia2: TDBLookupComboBox;
    pfechas: TPanel;
    Label6: TLabel;
    Label7: TLabel;
    fdesde: TDateTimePicker;
    fhasta: TDateTimePicker;
    pcampanyas: TPanel;
    combocampanya: TDBLookupComboBox;
    Label11: TLabel;
    DBText1: TDBText;
    gcrcheck: TDBCheckBox;
    Label8: TLabel;
    comboamortizacion: TComboBox;
    tnumperiodos: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    comboperiodicidad: TComboBox;
    Label2: TLabel;
    Label5: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    DBText6: TDBText;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    DBEdit4: TDBEdit;
    Panel12: TPanel;
    quconsumos_gastos: TFDQuery;
    qconsumosid: TIntegerField;
    qconsumoside: TIntegerField;
    qconsumosidcc: TWideStringField;
    qconsumosidgrupo: TIntegerField;
    qconsumosidfamilia: TIntegerField;
    qconsumosidaf: TIntegerField;
    qconsumoscodigo: TWideStringField;
    qconsumosdescripcion: TWideStringField;
    qconsumosregistro: TWideStringField;
    qconsumosprecio: TFloatField;
    qconsumoscantidad: TFloatField;
    qconsumostotal: TFloatField;
    qconsumostotal_amortizado: TFloatField;
    qconsumosnumplazos: TIntegerField;
    qconsumoslote: TWideStringField;
    qconsumosidentrada: TIntegerField;
    qconsumosfecha: TDateField;
    qconsumosidfraccionado: TIntegerField;
    qconsumosamortizacion: TIntegerField;
    qlistadoconsumoside: TIntegerField;
    qlistadoconsumosid: TIntegerField;
    qlistadoconsumosfecha: TDateField;
    qlistadoconsumosgrupo: TWideStringField;
    qlistadoconsumosfamilia: TWideStringField;
    qlistadoconsumosparcela: TWideStringField;
    qlistadoconsumosamortizacion: TIntegerField;
    qlistadoconsumosdescripcion: TWideStringField;
    qlistadoconsumostotal_amortizado: TFloatField;
    qlistadoconsumosidfraccionado: TIntegerField;
    informedb: TfrxDBDataset;
    informes: TfrxReport;
    Button1: TButton;
    procedure FormShow(Sender: TObject);
    procedure DBGrid3KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid3KeyPress(Sender: TObject; var Key: Char);
    procedure btEnuevoClick(Sender: TObject);
    procedure dsqconsumosStateChange(Sender: TObject);
    procedure btEcancelarClick(Sender: TObject);
    procedure btEguardarClick(Sender: TObject);
    procedure btEmodificarClick(Sender: TObject);
    procedure btEborrarClick(Sender: TObject);
    procedure checkcampanyasClick(Sender: TObject);
    procedure checkfechasClick(Sender: TObject);
    procedure checkccClick(Sender: TObject);
    procedure checkgruposClick(Sender: TObject);
    procedure checkfamiliasClick(Sender: TObject);
    procedure dsqcampanyasDataChange(Sender: TObject; Field: TField);
    procedure btbuscarClick(Sender: TObject);
    procedure dsqconsumosDataChange(Sender: TObject; Field: TField);
    procedure DBGrid2DblClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure DBGrid3DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBEdit4Enter(Sender: TObject);
    procedure DBGrid2TitleClick(Column: TColumn);
    procedure qconsumosBeforePost(DataSet: TDataSet);
    procedure qconsumosAfterPost(DataSet: TDataSet);
    procedure qconsumosBeforeDelete(DataSet: TDataSet);
    procedure ffechaExit(Sender: TObject);
    procedure bteimprimirClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  consumos_gastos: Tconsumos_gastos;
const
  rojo=$00AAAAFF;
  amarillo=$00B9FFFF;
  verde=$00C7FBAA;
  azul=$00FFFFBB;


implementation

{$R *.dfm}

uses fbuscararticulos, datos, principal;

procedure Tconsumos_gastos.btEborrarClick(Sender: TObject);
begin
   qconsumos.Close;
   qconsumos.ParamByName('IDENTRADA').AsString:=qlistadoconsumosidfraccionado.AsString;
   qconsumos.Open;
   qconsumos.Locate('ID',qlistadoconsumosid.AsString,[locaseinsensitive]);
    qconsumos.Delete;
    qlistadoconsumos.Refresh;
end;

procedure Tconsumos_gastos.btEcancelarClick(Sender: TObject);
begin
    qconsumos.Cancel;
    panel2.Enabled:=false;
end;

procedure Tconsumos_gastos.btEguardarClick(Sender: TObject);
var vidfraccionado,vperiodos,vdias,vidgrupo,vidfamilia,vide,vamortizacion:integer;
  x: Integer;
  vcantidad,vtotal,vprecio:real;
  vidcc,vdescripcion:string;
  vfecha:tdatetime;
begin

    qaux.Close;
    if qconsumos.State in [dsinsert] then
    begin
        vfecha:=ffecha.Date;
        vperiodos:=strtoint(tnumperiodos.Text);
        vamortizacion:=1;
        vdescripcion:=qconsumosdescripcion.AsString;
        vcantidad:=qconsumoscantidad.AsFloat;
        vtotal:=qconsumostotal.AsFloat;
        vprecio:=qconsumosprecio.AsFloat;
        vidfamilia:=combofamilia.Keyvalue;
        vidcc:=combocentrocoste.KeyValue;
        vidgrupo:=qgruposid.AsInteger;
        if comboamortizacion.ItemIndex=1 then
        begin
          case comboperiodicidad.ItemIndex of
              0: begin
                vdias:=365;
              end;
              1: begin
                vdias:=30;
              end;
          end;
        end else
        begin
          vdias:=1;
          vperiodos:=1;
          vamortizacion:=0;
        end;
        qaux.Open;
        if qaux.Fields[0].AsString='' then  vidfraccionado:=1 else vidfraccionado:=qaux.Fields[0].asinteger;
        for x := 0 to vperiodos-1 do
        begin
          if qconsumos.State in [dsbrowse] then qconsumos.Append;
          qaux.Open;
          qconsumosid.Value:=qaux.Fields[0].asinteger;
          qconsumosidfraccionado.Value:=vidfraccionado;
          qconsumoside.Value:=dm.tempresaid.asinteger;
          qconsumosnumplazos.value:=vperiodos;
          qconsumosdescripcion.Value:=vdescripcion;
          qconsumoscantidad.Value:=vcantidad;
          qconsumosprecio.Value:=vprecio;
          qconsumostotal.Value:=vtotal;
          qconsumostotal_amortizado.value:=vtotal/vperiodos;
          qconsumosfecha.Value:=vfecha+(x*vdias);
          qconsumosidcc.Value:=vidcc;
          qconsumosidgrupo.Value:=vidgrupo;
          qconsumosidfamilia.Value:=vidfamilia;
          qconsumosamortizacion.Value:=vamortizacion;
          qconsumos.Post;
          qaux.Close;
        end;
        pagecontrol1.ActivePageIndex:=0;

    end;
    if qconsumos.State in [dsedit] then
    begin
    if qconsumosfecha.AsDateTime<>ffecha.date then
    begin
      ffecha.Date:=qconsumosfecha.AsDateTime;
    end;
        vfecha:=ffecha.Date;
        vperiodos:=strtoint(tnumperiodos.Text);
        vdescripcion:=qconsumosdescripcion.AsString;
        vcantidad:=qconsumoscantidad.AsFloat;
        vtotal:=qconsumostotal.AsFloat;
        vprecio:=qconsumosprecio.AsFloat;
        vidfamilia:=combofamilia.Keyvalue;
        vidcc:=combocentrocoste.KeyValue;
        vidgrupo:=qgruposid.AsInteger;
      qconsumosdescripcion.Value:=vdescripcion;
      qconsumoscantidad.Value:=vcantidad;
      qconsumosprecio.Value:=vprecio;
      qconsumostotal.Value:=vtotal;
      qconsumosfecha.Value:=vfecha;
      qconsumosidcc.Value:=combocentrocoste.KeyValue;
      qconsumosidfamilia.Value:=combofamilia.KeyValue;
      qconsumosidgrupo.Value:=qgruposid.AsInteger;
      qconsumos.Post;
      pagecontrol1.ActivePageIndex:=0;
    end;
    vidgrupo:=qconsumosid.Asinteger;
    vide:=qconsumoside.AsInteger;
    qlistadoconsumos.Refresh;
    qlistadoconsumos.Locate('ide,id',vararrayof([vide,vidgrupo]),[locaseinsensitive]);
    panel2.Enabled:=false;

end;

procedure Tconsumos_gastos.bteimprimirClick(Sender: TObject);
begin
  informedb.DataSource := dsqlistadoconsumos;

  informes.Report.Clear;
  informes.LoadFromFile('c:\agrigest\informes\informecgastos.fr3');

  informes.ShowReport();
  informes.Clear
end;

procedure Tconsumos_gastos.btEmodificarClick(Sender: TObject);
var videntrada:string;
begin
   videntrada:=qlistadoconsumosidfraccionado.AsString;
   pagecontrol1.ActivePageIndex:=1;
   qconsumos.Close;
   qconsumos.ParamByName('IDENTRADA').AsString:=videntrada;
   qconsumos.Open;
   panel2.Enabled:=true;
   qconsumos.Edit;
end;

procedure Tconsumos_gastos.btEnuevoClick(Sender: TObject);
begin
   pagecontrol1.ActivePageIndex:=1;
   panel2.Enabled:=true;
   qconsumos.Append;
   ffecha.Date:=date;
   ffecha.SetFocus;
   combofamilia.KeyValue:=dm.tempresaidfamilia.AsInteger;

end;

procedure Tconsumos_gastos.Button1Click(Sender: TObject);
var
  archivo: string;
begin
  base.SaveDialog1.FileName := 'CONSUMOS Y GASTOS.xlsx';
  base.SaveDialog1.Execute();
  archivo := base.SaveDialog1.FileName;

  try
    BASE.ExportaraExcel(archivo, 'CONSUMOS Y GASTOS', DBGrid2);
  finally
    showmessage('Los datos han sido exportados correctamente a ' + archivo);
  end;
end;

procedure Tconsumos_gastos.btbuscarClick(Sender: TObject);
var vcc,vgrupo,vfamilia,videntrada:string;
begin
   videntrada:='%';
   if checkcc.Checked then vcc:=qparcelas2codpar.AsString else vcc:='%';
   if checkgrupos.Checked then vgrupo:=qgrupos2id.AsString else vgrupo:='%';
   if checkfamilias.Checked then vfamilia:=qfamilias2id.AsString else vfamilia:='%';
   qlistadoconsumos.Close;
   qlistadoconsumos.Prepare;
   if checkfechas.Checked or checkcampanyas.Checked then
   begin
      qlistadoconsumos.ParamByName('FDESDE').AsDate:=fdesde.Date;
      qlistadoconsumos.ParamByName('FHASTA').AsDate:=fhasta.Date;
   end else
   begin
      qlistadoconsumos.ParamByName('FDESDE').AsDate:=strtodate('01/01/2000');
      qlistadoconsumos.ParamByName('FHASTA').AsDate:=strtodate('31/12/2100');
   end;
   qlistadoconsumos.ParamByName('CC').Asstring:=vcc;
   qlistadoconsumos.ParamByName('GRUPO').Asstring:=vgrupo;
   qlistadoconsumos.ParamByName('FAMILIA').Asstring:=vfamilia;
   qlistadoconsumos.open;
   videntrada:=qlistadoconsumosidfraccionado.AsString;
   qconsumos.Close;
   qconsumos.ParamByName('IDENTRADA').Asstring:=videntrada;
   qconsumos.open;

end;

procedure Tconsumos_gastos.checkcampanyasClick(Sender: TObject);
begin
   if checkcampanyas.Checked=true then
   begin
    pcampanyas.Visible:=true;
    panel14.Height:=55;
   end
     else
     begin
       pcampanyas.visible:=false;
       panel14.Height:=20;
     end;
end;

procedure Tconsumos_gastos.checkccClick(Sender: TObject);
begin
   if checkcc.Checked=true then
   begin
      pcentros.Visible:=true;
      panel7.Height:=55;
      combocc2.KeyValue:=qparcelas2codpar.Asstring;
   end
       else
       begin
          pcentros.visible:=false;
          panel7.Height:=20;
       end;
end;

procedure Tconsumos_gastos.checkfamiliasClick(Sender: TObject);
begin
   if checkfamilias.Checked=true then
   begin
     pfamilias.Visible:=true;
     panel6.Height:=55;
      combofamilia2.KeyValue:=qfamilias2id.AsInteger;
   end
       else
       begin
          pfamilias.visible:=false;
          panel6.Height:=20;
       end;

end;

procedure Tconsumos_gastos.checkfechasClick(Sender: TObject);
begin
   if checkfechas.Checked=true then
   begin
     pfechas.Visible:=true;
     panel8.Height:=75;
   end
      else
      begin
        pfechas.visible:=false;
        panel8.Height:=20;
       end;

end;

procedure Tconsumos_gastos.checkgruposClick(Sender: TObject);
begin
   if checkgrupos.Checked=true then
   begin
      pgrupos.Visible:=true;
      panel5.Height:=55;
      combogrupos2.KeyValue:=qgrupos2id.AsInteger
   end
       else
       begin
          pgrupos.visible:=false;
          panel5.Height:=20;
       end;

end;

procedure Tconsumos_gastos.DBEdit4Enter(Sender: TObject);
begin
    if qconsumos.State in [dsedit,dsinsert] then qconsumostotal.Value:=qconsumoscantidad.AsFloat*qconsumosprecio.AsFloat;
end;

procedure Tconsumos_gastos.DBGrid2DblClick(Sender: TObject);
var videntrada:string;
begin
   videntrada:=qlistadoconsumosidfraccionado.AsString;
   pagecontrol1.ActivePageIndex:=1;
   qconsumos.Close;
   qconsumos.ParamByName('IDENTRADA').AsString:=videntrada;
   qconsumos.Open;
end;

procedure Tconsumos_gastos.DBGrid2DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
const IsChecked : array[Boolean] of Integer =
 (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
  DrawState: Integer;
  DrawRect: TRect;
begin
    if qlistadoconsumosamortizacion.Asinteger=1 then
    begin
        dbgrid2.Canvas.brush.Color := azul;
    end;
    if qlistadoconsumosamortizacion.Asinteger=0 then
    begin
        dbgrid2.Canvas.brush.Color := clwindow;
    end;

    dbgrid2.DefaultDrawColumnCell(Rect, DataCol, Column, State);
if (gdFocused in State) then
begin

    if (Column.Field.FieldName = 'amortizacion') then
     begin
       gcrcheck.Left := Rect.Left + DBgrid2.Left + 2;
       gcrcheck.Top := Rect.Top + DBgrid2.top + 2;
       gcrcheck.Width := Rect.Right - Rect.Left;
       gcrcheck.Height := Rect.Bottom - Rect.Top;
       gcrcheck.Visible := True;
     end;
end else
     begin

       if (Column.Field.FieldName = 'amortizacion') then
       begin
         DrawRect:=Rect;
         InflateRect(DrawRect,-1,-1);
         if column.Field.Asinteger=0  then DrawState := ischecked[false];
         if column.Field.Asinteger=1  then DrawState := ischecked[true];
      //   DrawState := ISChecked[Column.Field.Asboolean];
         DBgrid2.Canvas.FillRect(Rect);
         DrawFrameControl(DBgrid2.Canvas.Handle, DrawRect,
         DFC_BUTTON, DrawState);
       end;
    end;
 gcrcheck.visible:=false;






end;

procedure Tconsumos_gastos.DBGrid2TitleClick(Column: TColumn);
var
  st:TFDQuerySortType;
begin
  st:=qlistadoconsumos.SortType;
  qlistadoconsumos.SortedFields:=Column.FieldName;
  If st = stAscending then qlistadoconsumos.SortType:=stDescending else qlistadoconsumos.SortType:=stAscending;
  dsqlistadoconsumos.DataSet.First;

end;

procedure Tconsumos_gastos.DBGrid3DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
const IsChecked : array[Boolean] of Integer =
 (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
  DrawState: Integer;
  DrawRect: TRect;
begin
if (gdFocused in State) then
begin

    if (Column.Field.FieldName = 'amortizacion') then
     begin
       gcrcheck.Left := Rect.Left + DBgrid3.Left + 2;
       gcrcheck.Top := Rect.Top + DBgrid3.top + 2;
       gcrcheck.Width := Rect.Right - Rect.Left;
       gcrcheck.Height := Rect.Bottom - Rect.Top;
       gcrcheck.Visible := True;
     end;
end else
     begin

       if (Column.Field.FieldName = 'amortizacion') then
       begin
         DrawRect:=Rect;
         InflateRect(DrawRect,-1,-1);
         if column.Field.Asinteger=0  then DrawState := ischecked[false];
         if column.Field.Asinteger=1  then DrawState := ischecked[true];
      //   DrawState := ISChecked[Column.Field.Asboolean];
         DBgrid3.Canvas.FillRect(Rect);
         DrawFrameControl(DBgrid3.Canvas.Handle, DrawRect,
         DFC_BUTTON, DrawState);
       end;
    end;
 gcrcheck.visible:=false;
end;

procedure Tconsumos_gastos.DBGrid3KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    if (key=115) then
    begin
      buscararticulos := Tbuscararticulos.Create(nil);
      try
        fbuscararticulos.filtro:=qconsumosdescripcion.AsString;
        buscararticulos.Edit1.Text:=qconsumosdescripcion.AsString;
        buscararticulos.showmodal();
      finally
        if qconsumos.State in [dsbrowse] then qconsumos.Edit;
        qconsumosidaf.Value:=buscararticulos.qarticulosid.Asinteger;
        qconsumoscodigo.Value:=buscararticulos.qarticuloscodigo.Asstring;
        qconsumosregistro.Value:=buscararticulos.qarticulosregistro.Asstring;
        qconsumosdescripcion.value:=buscararticulos.qarticulosdescripcion.AsString;
        qconsumosprecio.Value:=buscararticulos.qarticulosult_preciocompra.Asfloat;
        buscararticulos.Free;
      end;
    end;
end;

procedure Tconsumos_gastos.DBGrid3KeyPress(Sender: TObject; var Key: Char);
begin
  if (key = Chr(9)) then Exit;

end;

procedure Tconsumos_gastos.dsqcampanyasDataChange(Sender: TObject;
  Field: TField);
begin
    fdesde.Date:=qcampanyasfechainicio.asdatetime;
    fhasta.Date:=qcampanyasfechafin.asdatetime;
end;

procedure Tconsumos_gastos.dsqconsumosDataChange(Sender: TObject;
  Field: TField);
begin
   if qconsumos.State in [dsbrowse] then  ffecha.Date:=qconsumosfecha.AsDateTime;
end;

procedure Tconsumos_gastos.dsqconsumosStateChange(Sender: TObject);
begin
    if qconsumos.State in [dsedit,dsinsert] then
    begin
        btenuevo.Enabled:=false;
        btemodificar.Enabled:=false;
        bteborrar.Enabled:=false;
        btecancelar.Enabled:=true;
        bteguardar.Enabled:=true;

    end else
    begin
        btenuevo.Enabled:=true;
        btemodificar.Enabled:=true;
        bteborrar.Enabled:=true;
        btecancelar.Enabled:=false;
        bteguardar.Enabled:=false;
    end;

end;

procedure Tconsumos_gastos.ffechaExit(Sender: TObject);
begin
   if qconsumos.State in [dsinsert,dsedit] then
   begin
     qconsumosfecha.Value:=ffecha.Date;
   end;

end;

procedure Tconsumos_gastos.FormKeyPress(Sender: TObject; var Key: Char);
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;
    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 Tconsumos_gastos.FormShow(Sender: TObject);
begin
    qparcelas.close;
    qparcelas2.Close;
    qgrupos.Close;
    qgrupos2.close;
    qfamilias.Close;
    qfamilias2.Close;
    qlistadoconsumos.close;
    qconsumos.Close;
    qresponsables.close;
    qparcelas.open;
    qparcelas2.open;
    qgrupos.open;
    qgrupos2.open;
    qfamilias.open;
    qfamilias2.open;
    qlistadoconsumos.prepare;
    qresponsables.open;
    qcampanyas.Open;
    combocampanya.KeyValue:=qcampanyasid.AsInteger;
    qconsumos.prepare;
    pagecontrol1.ActivePageIndex:=0;
    ffecha.Date:=date;
    btbuscar.Click;
end;

procedure Tconsumos_gastos.qconsumosAfterPost(DataSet: TDataSet);
begin
    if qconsumosamortizacion.AsInteger=1 then
    begin
    quconsumos_gastos.ParamByName('IDCC').AsString:=qconsumosidcc.AsString;
    quconsumos_gastos.ParamByName('IDCC').Asstring:=qconsumosidcc.Asstring;
    quconsumos_gastos.ParamByName('IDGRUPO').AsInteger:=qconsumosidgrupo.AsInteger;
    quconsumos_gastos.ParamByName('IDFAMILIA').AsInteger:=qconsumosidfamilia.AsInteger;
    quconsumos_gastos.ParamByName('AF').Asinteger:=qconsumosidaf.AsInteger;
    quconsumos_gastos.ParamByName('DESCRIPCION').Asstring:=qconsumosdescripcion.Asstring;
    quconsumos_gastos.ParamByName('REGISTRO').Asstring:=qconsumosregistro.AsString;
    quconsumos_gastos.ParamByName('PRECIO').Asfloat:=qconsumosprecio.Asfloat;
    quconsumos_gastos.ParamByName('CANTIDAD').Asfloat:=qconsumoscantidad.AsFloat;
    quconsumos_gastos.ParamByName('TOTAL').Asfloat:=qconsumostotal.Asfloat;
    quconsumos_gastos.ParamByName('TOTALAMORTIZADO').Asfloat:=qconsumostotal_amortizado.AsFloat;
    quconsumos_gastos.ParamByName('NUMPLAZOS').AsInteger:=qconsumosnumplazos.AsInteger;
    quconsumos_gastos.ParamByName('LOTE').Asstring:=qconsumoslote.Asstring;
    quconsumos_gastos.ParamByName('IDENTRADA').AsInteger:=qconsumosidentrada.AsInteger;
    quconsumos_gastos.ParamByName('IDFRACCIONADO').AsInteger:=qconsumosidfraccionado.AsInteger;
    quconsumos_gastos.ParamByName('ID').AsInteger:=qconsumosid.AsInteger;
    quconsumos_gastos.ExecSQL;
    qconsumos.refresh;
    qlistadoconsumos.Refresh;

    end;
end;

procedure Tconsumos_gastos.qconsumosBeforeDelete(DataSet: TDataSet);
begin
   case dm.PedirConfirmacion('Confirma que desea borrar este consumo/gasto?',true,true,false) of
        mrno:Abort;
   end;
end;

procedure Tconsumos_gastos.qconsumosBeforePost(DataSet: TDataSet);
var x:integer;
begin
    qconsumostotal.Value:=qconsumosprecio.AsFloat*qconsumoscantidad.AsFloat;
    qconsumostotal_amortizado.Value:=qconsumostotal.AsFloat/qconsumosnumplazos.AsInteger;
    if qconsumos.state=dsinsert then x:=dm.PedirConfirmacion('Confirma que desea dar de alta esta consumo/gasto?',true,true,true)
                                     else x:=dm.PedirConfirmacion('Confirma que desea modificar esta consumo/gasto?',true,true,true);
    case x of
        mrno: begin
                 qconsumos.cancel;
                 abort;
              end;
        mrcancel:BEGIN
                 abort;
                 END;

    end;
end;

end.
