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, uDAInterfaces, cxGrid, cxGridLevel, uViewArticulosPorProveedor;

type
  TfEditorArticulos = class(TfEditorGridBase, IEditorArticulos)
    actFamilias: TAction;
    pgPaginas: TPageControl;
    pagArticulos: TTabSheet;
    pagArticulosPorProveedor: TTabSheet;
    frViewArticulos1: TfrViewArticulos;
    frViewArticulosPorProveedor1: TfrViewArticulosPorProveedor;
    procedure FormShow(Sender: TObject);
    procedure actFamiliasExecute(Sender: TObject);
    procedure pgPaginasChange(Sender: TObject);
  private
    FArticulos: IBizArticulo;
    FArticulosPorProveedor: IBizArticulo;
    FController : IArticulosController;
  protected
    procedure SetViewGrid(const Value : IViewGridBase); override;
    function GetArticulos: IBizArticulo;
    procedure SetArticulos(const Value: IBizArticulo);
    function GetArticulosPorProveedor: IBizArticulo;
    procedure SetArticulosPorProveedor(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;
    procedure RefrescarInterno; override;

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

implementation

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

{$R *.dfm}

{
****************************** TfEditorArticulos *******************************
}
procedure TfEditorArticulos.FormShow(Sender: TObject);
begin
  inherited;
  ViewGrid := frViewArticulos1;
end;

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

function TfEditorArticulos.GetArticulosPorProveedor: IBizArticulo;
begin
  Result := FArticulosPorProveedor;
end;

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

procedure TfEditorArticulos.ImprimirInterno;
begin
  inherited;
end;

procedure TfEditorArticulos.ModificarInterno;
var
  AArticulo: IBizArticulo;

begin
  inherited;

  case pgPaginas.ActivePage.TabIndex of
  0: begin
       AArticulo := Articulos;
       FController.Ver(AArticulo);
     end;
  1: begin
       AArticulo := FController.Buscar(ArticulosPorProveedor.DataTable.FieldByName('ID_ARTICULO').AsInteger);
       FController.Ver(AArticulo);
       RefrescarInterno;
     end;
  end;
end;

procedure TfEditorArticulos.NuevoInterno;
var
  AArticulo: IBizArticulo;

begin
  inherited;

  if (FViewGrid._Grid.ActiveLevel.Tag = 1) then
    AArticulo := FController.Nuevo
  else
  begin
    AArticulo := Articulos;
    FController.Anadir(AArticulo);
  end;

  if not AArticulo.DataTable.Active then
     AArticulo.DataTable.Active := True;

  FController.Ver(AArticulo);

  if (FViewGrid._Grid.ActiveLevel.Tag = 1) then
    Articulos.DataTable.Refresh;
end;

procedure TfEditorArticulos.pgPaginasChange(Sender: TObject);
begin
  inherited;
  if pgPaginas.ActivePage.TabIndex = 0 then
     ViewGrid := frViewArticulos1
  else
     ViewGrid := frViewArticulosPorProveedor1;
end;

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

procedure TfEditorArticulos.PrevisualizarInterno;
begin
  inherited;
end;

procedure TfEditorArticulos.RefrescarInterno;
begin
//  inherited;
  if Assigned(FArticulos) then
  begin
     frViewArticulos1.SaveGridStatus;
     try
       frViewArticulos1.dsDataSource.DataTable.Refresh;
     finally
       frViewArticulos1.RestoreGridStatus;
     end;
  end;

  if Assigned(FArticulosPorProveedor) then
  begin
    frViewArticulosPorProveedor1.SaveGridStatus;
    try
      frViewArticulosPorProveedor1.dsDataSource.DataTable.Refresh;
    finally
      frViewArticulosPorProveedor1.RestoreGridStatus;
    end;
  end;
end;

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

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

procedure TfEditorArticulos.SetController(const Value: IArticulosController);
begin
  FController := Value;
  if Assigned(ViewGrid) then
    (ViewGrid as IViewArticulos).Controller := FController;
end;

procedure TfEditorArticulos.SetViewGrid(const Value: IViewGridBase);
var
  APosicionarAlprincipio: Boolean;

begin
  inherited;

  APosicionarAlprincipio := False;
  case pgPaginas.ActivePage.TabIndex of
  0: begin
       if not Assigned(Articulos) then
       begin
          APosicionarAlprincipio := True;
          Articulos := FController.BuscarTodos;
       end;
       Articulos.DataTable.Active := True;

       if actFiltrar.Checked then
       begin
         if not frViewArticulos1.ViewFiltros.VerFiltros then
         begin
            actFiltrar.Execute;
            frViewArticulos1.ViewFiltros.Texto := frViewArticulosPorProveedor1.ViewFiltros.Texto;
         end
         else
            frViewArticulos1.ViewFiltros.Texto := frViewArticulosPorProveedor1.ViewFiltros.Texto;
       end
       else
       begin

         if frViewArticulos1.ViewFiltros.VerFiltros then
            actFiltrar.Execute;

          ViewGrid.Filter := tbxEditFiltro.Text;
       end;
     end;
  1: begin
       if not Assigned(ArticulosPorProveedor) then
       begin
          APosicionarAlprincipio := True;
          ArticulosPorProveedor := FController.BuscarTodosPorProveedor;
       end;
       ArticulosPorProveedor.DataTable.Active := True;

       if actFiltrar.Checked then
       begin
          if not frViewArticulosPorProveedor1.ViewFiltros.VerFiltros then
          begin
             actFiltrar.Execute;
            frViewArticulosPorProveedor1.ViewFiltros.Texto := frViewArticulos1.ViewFiltros.Texto;
          end
          else
            frViewArticulosPorProveedor1.ViewFiltros.Texto := frViewArticulos1.ViewFiltros.Texto;
       end
       else
       begin
          if frViewArticulosPorProveedor1.ViewFiltros.VerFiltros then
             actFiltrar.Execute;
          ViewGrid.Filter := tbxEditFiltro.Text;
       end;
     end;
  end;

  if APosicionarAlprincipio then
     ViewGrid._FocusedView.DataController.GotoFirst;
end;

destructor TfEditorArticulos.Destroy;
begin
  FArticulos := NIL;
  FArticulosPorProveedor := 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
    case pgPaginas.ActivePage.TabIndex of
    0: begin
       if not FController.Eliminar(Articulos.ID) then
          actRefrescar.Execute;
       end;
    1: begin
       if not FController.Eliminar(ArticulosPorProveedor.DataTable.FieldByName('ID_ARTICULO').AsInteger) then
          actRefrescar.Execute;
       end;
    end;
    RefrescarInterno;    
  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;

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

end.
