{
===============================================================================
  Copyright (©) 2001. 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: 13-05-2002
  Versión actual: 1.0.0
  Fecha versión actual:  13-05-2002
===============================================================================
  Modificaciones:

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

unit ArticulosModelo;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables, Grids, DBGrids, ComCtrls, IBDatabase, ToolWin, ExtCtrls,
  IBCustomDataSet, IBQuery, RXDBCtrl, RdxPaneles, RdxBarras,
  RdxBotones, Mensajes, RdxFrameArticulos, Configuracion,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxEdit, cxDBData,
  cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, RdxFrame, Menus, am2000menuitem,
  am2000popupmenu, am2000, StdCtrls, RdxComboBox, RdxCampos, AdvPanel, cxContainer,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, DBCtrls, ActnList, am2000utils, cxDataStorage;

type
  TfrArticulosModelo = class(TRdxFrameArticulos)
    brSeleccion: TRdxBarraInferior;
    bSeleccionar: TRdxBoton;
    bCancelar: TRdxBoton;
    dsArticulos: TDataSource;
    BarraSuperior: TRdxBarraSuperior;
    bConsultar: TRdxBotonSuperior;
    bEliminar: TRdxBotonSuperior;
    bModificar: TRdxBotonSuperior;
    bAnadir: TRdxBotonSuperior;
    MenuOpciones: TPopupMenu2000;
    mnuAnadir: TMenuItem2000;
    mnuModificar: TMenuItem2000;
    mnuEliminar: TMenuItem2000;
    mnuConsultar: TMenuItem2000;
    pnlCuerpo: TPanel;
    pnlGrid: TPanel;
    gridArticulos: TcxGrid;
    gridArticulosView: TcxGridDBTableView;
    gridArticulosLevel: TcxGridLevel;
    Acciones: TActionList;
    actAnadir: TAction;
    actModificar: TAction;
    actEliminar: TAction;
    actConsultar: TAction;
    actSeleccionar: TAction;
    actCancelar: TAction;
    imgSombra: TImage;
    procedure gridArticulosRefreshButton(Sender: TObject);
    procedure RdxFrameArticulosShow(Sender: TObject);
    procedure actAnadirExecute(Sender: TObject); virtual;
    procedure actModificarExecute(Sender: TObject); virtual;
    procedure actEliminarExecute(Sender: TObject); virtual;
    procedure actConsultarExecute(Sender: TObject); virtual;
    procedure actSeleccionarExecute(Sender: TObject);
    procedure actCancelarExecute(Sender: TObject);
    procedure gridArticulosViewDblClick(Sender: TObject);
  private
  protected
    procedure ActualizarBotones;
    procedure BuscarArticulo; override;
    procedure FreeContenido; override;
    procedure InicializarTablas; virtual;
  public
    constructor Create (AOwner : TComponent); overload; override;
    destructor Destroy; override;
  published
    property TablaArticulos;
    property Entidad;
  end;

var
  frArticulosModelo: TfrArticulosModelo;

implementation

{$R *.DFM}

uses
  Variants, BaseDatos, Entidades, TablaArticulos, ArticuloModelo;

constructor TfrArticulosModelo.Create (AOwner : TComponent);
begin
  inherited Create(AOwner);
  BaseDatos := dmBaseDatos.BD;
  Transaccion := dmBaseDatos.Transaccion;
  TablaArticulos := TIBDataSet.Create(Self);
  dsArticulos.DataSet := TablaArticulos;

  InicializarTablas;

  dmTablaArticulos.InicializarGridArticulos(gridArticulosView);
  gridArticulosView.OnDblClick := gridArticulosViewDblClick;
  gridArticulosView.Controller.GoToFirst;
end;

destructor TfrArticulosModelo.Destroy;
begin
  TablaArticulos.Close;
  TablaArticulos.UnPrepare;
  TablaArticulos.Free;
  inherited;
end;

procedure TfrArticulosModelo.BuscarArticulo;
begin
  with TablaArticulos do
  begin
    DisableControls;
    Close;
    Open;
    dmTablaArticulos.InicializarTablaArticulos(@TablaArticulos);

    ActualizarBotones;
    EnableControls;
    if not Locate('CODIGO', CodigoArticulo, []) then
      gridArticulosView.Controller.GoToFirst
    else
      gridArticulosView.Controller.TopRowIndex := gridArticulosView.Controller.FocusedRowIndex;
  end;
end;

procedure TfrArticulosModelo.FreeContenido;
begin
  if (Contenido is TRdxFrameArticulos) then
    CodigoArticulo := (Contenido as TRdxFrameArticulos).CodigoArticulo;
  inherited FreeContenido;
  gridArticulos.SetFocus;
end;

procedure TfrArticulosModelo.ActualizarBotones;
begin
  if BaseDatos.IsReadOnly then
  begin
    actAnadir.Enabled := False;
    actModificar.Enabled := False;
    actEliminar.Enabled := False;
    actConsultar.Enabled := True;
    Exit;
  end;

  if TablaArticulos.RecordCount = 0 then begin
    actAnadir.Enabled := True;
    actModificar.Enabled := False;
    actEliminar.Enabled := False;
    actConsultar.Enabled := False;
  end
  else begin
    actAnadir.Enabled := True;
    actModificar.Enabled := True;
    actEliminar.Enabled := True;
    actConsultar.Enabled := True;
  end;
end;

procedure TfrArticulosModelo.gridArticulosRefreshButton(Sender: TObject);
begin
  BuscarArticulo;
end;

procedure TfrArticulosModelo.RdxFrameArticulosShow(Sender: TObject);
begin
  gridArticulos.SetFocus
end;

procedure TfrArticulosModelo.actAnadirExecute(Sender: TObject);
begin
  Contenido := TfrArticuloModelo.Create(Self);
  Contenido.Modo := Anadir;
end;

procedure TfrArticulosModelo.actModificarExecute(Sender: TObject);
begin
  Contenido := TfrArticuloModelo.Create(Self);
  Contenido.Modo := Modificar;
end;

procedure TfrArticulosModelo.actEliminarExecute(Sender: TObject);
begin
  Contenido := TfrArticuloModelo.Create(Self);
  Contenido.Modo := Eliminar;
end;

procedure TfrArticulosModelo.actConsultarExecute(Sender: TObject);
begin
  Contenido := TfrArticuloModelo.Create(Self);
  Contenido.Modo := Consultar;
end;

procedure TfrArticulosModelo.actSeleccionarExecute(Sender: TObject);
begin
  CodigoArticulo := TablaArticulos.FieldByName('CODIGO').AsString;
  CloseFrame;
end;

procedure TfrArticulosModelo.actCancelarExecute(Sender: TObject);
begin
  FCodigoArticulo := NULL;
  CloseFrame;
end;

procedure TfrArticulosModelo.gridArticulosViewDblClick(Sender: TObject);
begin
  if Modo = Seleccionar then
    actSeleccionar.Execute
  else
    actModificar.Execute;
end;

procedure TfrArticulosModelo.InicializarTablas;
begin
  Entidad := entArticuloModelo;

  with TablaArticulos do
  begin
    DisableControls;
    Database := BaseDatos;
    Transaction := Transaccion;
    SelectSQL.Assign(dmTablaArticulos.sqlGridArtModelo);
    Prepare;
    Open;
    ActualizarBotones;
    EnableControls;
  end;
end;

end.
