{
===============================================================================
  Copyright (©) 2002. Rodax Software.
===============================================================================
  Los contenidos de este fichero son propiedad de Rodax Software titular del
  copyright. Este fichero sólo podrá ser copiado, distribuido y utilizado,
  en su totalidad o en parte, con el permiso escrito de Rodax Software, o de
  acuerdo con los términos y condiciones establecidas en el acuerdo/contrato
  bajo el que se suministra.
  -----------------------------------------------------------------------------
  Web:  www.rodax-software.com
===============================================================================
  Fecha primera versión: 07-12-2002
  Versión actual: 1.0.0
  Fecha versión actual: 07-12-2002
===============================================================================
  Modificaciones:

    Fecha         Comentarios
    ---------------------------------------------------------------------------
===============================================================================
}

unit TablaFamilias;

interface

uses
  //Generales
  SysUtils, Classes, Controls, IBSQL, cxGridDBTableView, cxCustomData, DB,
  //Particulares

  //Aplicacion
  Framework, StrFunc, Entidades, Constantes, BaseDatos;

type
  TDatosFamilia = class(TObjeto)
  protected
    procedure ObtenerDatos; override;
    procedure AssignTo(Dest: TPersistent); override;
  public
		Codigo : Integer;
    Descripcion : String;
    constructor Create(Codigo : Integer); 
  end;


  TdmTablaFamilias = class(TDataModule)
  private
    procedure IniciarSQL;
  public
    sqlInsertar : TStrings;
    sqlModificar : TStrings;
    sqlConsultar : TStrings;
    sqlEliminar : TStrings;

    constructor Create (AOwner : TComponent); override;
    destructor Destroy; override;
    procedure InicializarGridFamilias(var vGrid: TcxGridDBTableView);
    function darFamilias: TStrings;
    function darContadorFamilia: Integer;
  end;

var
  dmTablaFamilias : TdmTablaFamilias;

implementation

{$R *.DFM}
uses
  Literales;

constructor TdmTablaFamilias.Create (AOwner : TComponent);
begin
  inherited;
  sqlInsertar := TStringList.Create;
  sqlModificar := TStringList.Create;
  sqlConsultar := TStringList.Create;
  sqlEliminar := TStringList.Create;
  IniciarSQL;
end;

destructor TdmTablaFamilias.Destroy;
begin
  sqlInsertar.Free;
  sqlModificar.Free;
  sqlConsultar.Free;
  sqlEliminar.Free;
  inherited;
end;

procedure TdmTablaFamilias.IniciarSQL;
begin
  with sqlInsertar do
  begin
    Add('insert into FAMILIAS (CODIGO, DESCRIPCION)');
    Add('values (:CODIGO, UPPER(:DESCRIPCION))');
  end;

  with sqlModificar do
  begin
    Add('update FAMILIAS set ');
    Add('DESCRIPCION = UPPER(:DESCRIPCION) ');
    Add('where CODIGO = :CODIGO');
  end;

  with sqlEliminar do
  begin
    Add('delete from FAMILIAS ');
    Add('where CODIGO = :CODIGO');
  end;

  with sqlConsultar do
  begin
    Add('select CODIGO, DESCRIPCION ');
    Add('from FAMILIAS ');
    Add('order by DESCRIPCION');
  end;
end;

procedure TdmTablaFamilias.InicializarGridFamilias(var vGrid: TcxGridDBTableView);
var
  Columna : TcxGridDBColumn;
begin
  with vGrid do begin
    OptionsView.Footer := True;
    {Columna CODIGO}
    Columna := CreateColumn;
    Columna.DataBinding.FieldName := 'CODIGO';
    Columna.Caption := 'Código familia';
    Columna.Visible := False;

    {Columna DESCRIPCION}
    Columna := CreateColumn;
    Columna.DataBinding.FieldName := 'DESCRIPCION';
    Columna.Caption := 'Nombres de familia';
    Columna.SortOrder := soAscending;
    with TcxGridDBTableSummaryItem(DataController.Summary.FooterSummaryItems.Add) do
    try
      try
        BeginUpdate;
        Column := Columna;
        FieldName := Columna.DataBinding.FieldName;
        Format := 'Total: 0 nombres de familia';
        Kind := skCount;
      finally
        EndUpdate;
      end;
    except
      DataController.Summary.FooterSummaryItems.Delete(DataController.Summary.FooterSummaryItems.Count-1);
    end;
  end;
end;

function TdmTablaFamilias.darFamilias: TStrings;
var
  oSQL : TIBSQL;
  Lista : TStringList;
begin
  oSQL := TIBSQL.Create(Self);
  Lista := TStringList.Create;

  with oSQL do
  begin
    Database := dmBaseDatos.BD;
    Transaction := dmBaseDatos.Transaccion;
    SQL.Add('select DESCRIPCION from FAMILIAS order by DESCRIPCION');

    try
      Prepare;
      ExecQuery;
      while not EOF do begin
        Lista.Append(Fields[0].AsString);
        Next;
      end;
      result := Lista;
    finally
      Close;
      Transaction := NIL;
      Free;
    end;
  end;
end;

function TdmTablaFamilias.darContadorFamilia: Integer;
var
  oSQL : TIBSQL;
begin
  oSQL := TIBSQL.Create(Self);

  with oSQL do
  begin
    Database := dmBaseDatos.BD;
    Transaction := dmBaseDatos.Transaccion;
    SQL.Add('select MAX(CODIGO) from FAMILIAS');

    try
      Prepare;
      ExecQuery;
      if not EOF then
        Result := (Fields[0].AsInteger + 1)
      else
        Result := 0;
    finally
      Close;
      Transaction := NIL;
      Free;
    end;
  end;
end;

{ TDatosFamilia }

procedure TDatosFamilia.AssignTo(Dest: TPersistent);
begin
  inherited;
  //
end;

constructor TDatosFamilia.Create(Codigo: Integer);
begin
  inherited Create;
  Self.Codigo := Codigo;
  ObtenerDatos;
end;

procedure TDatosFamilia.ObtenerDatos;
var
  oSQL : TIBSQL;
begin
  oSQL := TIBSQL.Create(nil);
  with oSQL do
  begin
    Database := dmBaseDatos.BD;
    Transaction := dmBaseDatos.Transaccion;
    SQL.Add('select * from FAMILIAS ');
    SQL.Add('where CODIGO = :CODIGO');

    try
      ParamByName('CODIGO').AsInteger := Codigo;
      Prepare;
      ExecQuery;

      if (RecordCount > 0) then
        Descripcion := FieldByName('DESCRIPCION').AsString
      else begin
        raise Exception.Create(msgDatosNoExisteFamilia);
      end;
    finally
      Close;
      Transaction := NIL;
      Free;
    end;
  end;
end;
end.
