unit uEditorArticulos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, ToolWin, ComCtrls, JvExControls, JvComponent,
  JvNavigationPane, uViewArticulos, uBizArticulos, ActnList, DBActns, uViewGrid,
  Menus, uDataModuleBase, ImgList, PngImageList, TB2Dock, TB2Toolbar, TBX,
  TB2Item, StdActns, TB2ExtItems, TBXExtItems, TB2MRU, DB, uDADataTable,
  JvFormAutoSize, uDAScriptingProvider, uDACDSDataTable, JvAppStorage,
  JvAppRegistryStorage, JvFormPlacement, ExtCtrls, uCustomView, uViewBase,
  uViewBarraSeleccion, pngimage, uIEditorArticulos, uEditorGridBase,
  JvComponentBase, uViewGridBase, uArticulosController, JvExComCtrls,
  JvStatusBar;

type
  TfEditorArticulos = class(TfEditorGridBase, IEditorArticulos)
    actFamilias: TAction;
    procedure FormShow(Sender: TObject);
    procedure actFamiliasExecute(Sender: TObject);
  private
    FArticulos: IBizArticulo;    
    FController : IArticulosController;
  protected
    function GetArticulos: IBizArticulo;
    procedure SetArticulos(const Value: IBizArticulo);
    function GetController : IArticulosController; virtual;
    procedure SetController (const Value : IArticulosController); virtual;

    procedure NuevoInterno; override;
    procedure EliminarInterno; override;
    procedure ModificarInterno; override;
    procedure ImprimirInterno; override;
    procedure PrevisualizarInterno; override;
    procedure DuplicarInterno; override;

    //Si queremos crear otra vista para el editor heredado solo tendriamos que
    //sobreescribir este metodo
    procedure AsignarVista; virtual;

  public
    procedure PonerTitulos(const ATitulo: string = ''); override;
    property Articulos: IBizArticulo read GetArticulos write SetArticulos;
    property Controller : IArticulosController read GetController write SetController;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses
  uCustomEditor, uDataModuleArticulos, uDataModuleUsuarios,
  uEditorArticulo, uEditorDBBase,  uBizFamilias, uFamiliasController,
  cxGrid, cxGridCustomTableView; //, uListaArticulos;

{$R *.dfm}

{
****************************** TfEditorArticulos *******************************
}
procedure TfEditorArticulos.FormShow(Sender: TObject);
begin
  inherited;

  if not Assigned(ViewGrid) then
    raise Exception.Create('No hay ninguna vista asignada');

  if not Assigned(Articulos) then
    raise Exception.Create('No hay ningún articulo asignado');

  Articulos.DataTable.Active := True;
  ViewGrid.GotoFirst;
end;

function TfEditorArticulos.GetArticulos: IBizArticulo;
begin
  Result := FArticulos;
end;

function TfEditorArticulos.GetController: IArticulosController;
begin
  Result := FController;
end;

procedure TfEditorArticulos.ImprimirInterno;
begin
  inherited;
end;

procedure TfEditorArticulos.ModificarInterno;
begin
  inherited;
  FController.Ver(Articulos);
end;

procedure TfEditorArticulos.NuevoInterno;
begin
  inherited;
  FController.Anadir(Articulos);
  FController.Ver(Articulos);
end;

procedure TfEditorArticulos.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  FTitulo := 'Lista de artículos - ' + dmUsuarios.EmpresaActual.NOMBRE;
  inherited PonerTitulos(FTitulo);
end;

procedure TfEditorArticulos.PrevisualizarInterno;
begin
  inherited;
end;

procedure TfEditorArticulos.SetArticulos(const Value: IBizArticulo);
begin
  FArticulos := Value;
  dsDataTable.DataTable := FArticulos.DataTable;
  if Assigned(ViewGrid) then
    (ViewGrid as IViewArticulos).Articulos := Articulos;
end;

procedure TfEditorArticulos.SetController(const Value: IArticulosController);
begin
  FController := Value;
end;

destructor TfEditorArticulos.Destroy;
begin
  FArticulos := NIL;
  FController := NIl;  
  inherited;
end;

procedure TfEditorArticulos.DuplicarInterno;
var
  AArticulo : IBizArticulo;
begin
  inherited;
  AArticulo := FController.Duplicar(Articulos);
  try
    FController.Ver(AArticulo);
  finally
    actRefrescar.Execute;
  end;
end;

procedure TfEditorArticulos.EliminarInterno;
begin
  if (Application.MessageBox('¿Desea borrar este artículo?', 'Atención', MB_YESNO) = IDYES) then
  begin
    inherited;
    //Para que en el caso de no poderse realizar la operación se refresquen
    //los datos y no nos permita eliminar un registro a la segunda
    if not FController.Eliminar(Articulos) then
       actRefrescar.Execute;
  end;
end;

procedure TfEditorArticulos.actFamiliasExecute(Sender: TObject);
var
  AFamiliasController : IFamiliasController;
  AFamilias : IBizFamilia;
begin
  AFamiliasController := TFamiliasController.Create;
  try
    AFamilias := (AFamiliasController.BuscarTodos as IBizFamilia);
    AFamiliasController.VerTodos(AFamilias);
  finally
    AFamilias := NIL;
    AFamiliasController := NIL;
  end;
end;

procedure TfEditorArticulos.AsignarVista;
begin
  ViewGrid := CreateView(TfrViewArticulos) as IViewArticulos;
end;

constructor TfEditorArticulos.Create(AOwner: TComponent);
begin
  inherited;
  AsignarVista;
end;

end.

