unit uInformesController;

interface


uses
  Classes, SysUtils, uDADataTable, uControllerBase, uEditorDBItem,
  uIDataModuleInformes, uBizInformes;

type
  IInformesController = interface(IObservador)
  ['{DC7F2D51-898D-41AD-83F6-CCFBCDE933DF}']
    function BuscarTodos: IBizInforme;
    procedure Nuevo(AInforme : String; AInformes : IBizInforme);
    function Eliminar(AInforme : IBizInforme): Boolean;    
    procedure Ver(AInformes : IBizInforme);
    procedure VerTodos(AInformes: IBizInforme);
    function Guardar(AInforme: IBizInforme): Boolean;
  end;

  TInformesController = class(TObservador, IInformesController)
  protected
    FDataModule : IDataModuleInformes;

    procedure RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable); override;
    function CreateEditor(const AName : String; const IID: TGUID; out Intf): Boolean;
    function _Vacio : IBizInforme;

    procedure FiltrarEmpresa(AInformes: IBizInforme);

    //Estos son los tres métodos a sobre escribir si se desea heredar toda la logica de
    //este controller
    procedure AsignarDataModule; virtual;
    procedure AsignarID(AInforme: IBizInforme; const IDNuevo : Integer);
    function ValidarInforme(AInforme: IBizInforme): Boolean;

  public
    constructor Create; virtual;
    destructor Destroy; override;

//    function Buscar(const ID_ALMACEN: Integer): IBizInforme;
    function BuscarTodos: IBizInforme;
//    function ExtraerSeleccionados(AArticulos: IBizInforme) : IBizInforme;
//    function Ver(AArticulos: IBizInforme; AInformes : IBizInforme): Boolean;
    procedure Nuevo(AInforme : String; AInformes : IBizInforme);
    function Eliminar(AInforme : IBizInforme): Boolean;
    procedure Ver(AInformes : IBizInforme);
    procedure VerTodos(AInformes: IBizInforme);
    function Guardar(AInforme: IBizInforme): Boolean;
  end;

var
  InformesController: TInformesController;

implementation

uses
  Forms, cxControls, DB, uEditorRegistryUtils, uInformeRegistryUtils,
  uInformeBaseController, uIEditorInformes,
  uDataModuleInformes,
  uDataModuleUsuarios, uDAInterfaces, uDataTableUtils,
  uDateUtils, uROTypes, DateUtils, Controls, Windows,
  dialogs, Variants, uControllerDetallesBase, uDialogUtils,
  schInformesClient_Intf; //, uInfVentasArticuloController;

{ TInformesController }

procedure TInformesController.AsignarDataModule;
begin
  FDataModule := TdmInformes.Create(Nil);
end;

{
function TInformesController.Buscar(const ID_ALMACEN: Integer): IBizInforme;
begin
  Result := (FDataModule as IDataModuleInformes).GetItems(ID_ALMACEN);
  FiltrarEmpresa(Result);
end;
}

procedure TInformesController.AsignarID(AInforme: IBizInforme; const IDNuevo: Integer);
begin
  if not Assigned(AInforme) then
    raise Exception.Create ('Informe no asignado');

  if AInforme.EsNuevo then
  begin
    AInforme.Edit;
    AInforme.ID := IDNuevo;
    AInforme.Post;
  end;
end;

function TInformesController.BuscarTodos: IBizInforme;
begin
  Result := FDataModule.GetItems;
  FiltrarEmpresa(Result);
end;

constructor TInformesController.Create;
begin
  AsignarDataModule;
end;

function TInformesController.CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
begin
  Result := Supports(EditorRegistry.CreateEditor(AName), IID, Intf);
end;

destructor TInformesController.Destroy;
begin
  FDataModule := Nil;
  inherited;
end;


function TInformesController.Eliminar(AInforme: IBizInforme): Boolean;
begin
  Result  := False;

  if not Assigned(AInforme) then
    raise Exception.Create ('Informe no asignada');

  ShowHourglassCursor;
  try
    if (AInforme.State in dsEditModes) then
      AInforme.Cancel;

    AInforme.Delete;
    AInforme.DataTable.ApplyUpdates;

    Result := True;
  finally
    HideHourglassCursor;
  end;
end;

{function TInformesController.ExtraerSeleccionados(AArticulos: IBizInforme): IBizInforme;
var
  ASeleccionados : IBizInforme;
begin
  ASeleccionados := (Self.Buscar(ID_NULO) as IBizInforme);
  CopyDataTable(AArticulos.DataTable, ASeleccionados.DataTable, True);
  Result := ASeleccionados;
end;}


{function TInformesController.Ver(AArticulos: IBizInforme; AInformes: IBizInforme): Boolean;
var
  AEditor : IEditorEntradaSalidaArticulos;
begin
  Result := False;
  AEditor := NIL;
  ShowHourglassCursor;
  try
    CreateEditor('EditorEntradaSalidaArticulos', IEditorEntradaSalidaArticulos, AEditor);
    with AEditor do
    begin
      Controller := Self; //OJO ORDEN MUY IMPORTANTE
      Articulos := AArticulos;
      Informes := AInformes;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    try
      AEditor.ShowModal;
      Result := AEditor.ResultadoModalOK;
      AEditor.Release;
    finally
      AEditor := NIL;
    end;
end;}

function TInformesController.ValidarInforme(AInforme: IBizInforme): Boolean;
begin
  Result := False;

  if not Assigned(AInforme) then
     raise Exception.Create ('Informe no asignado');

  if (AInforme.DataTable.State in dsEditModes) then
      AInforme.DataTable.Post;

  if (Length(AInforme.CONTROLLER) = 0) then
    raise Exception.Create('Debe indicar el controller del informe');

  if (Length(AInforme.CATEGORIA) = 0) then
    raise Exception.Create('Debe indicar la categoria del informe');

  Result := True;    
end;

procedure TInformesController.Ver(AInformes: IBizInforme);
var
  AEditor : IEditorInformes;
  FControllerInforme: IInformeBaseController;

begin
  AEditor := NIL;

  CreateInforme(AInformes.CONTROLLER, IInformeBaseController, FControllerInforme);
  FControllerInforme.Ver(AInformes);
end;

procedure TInformesController.VerTodos(AInformes: IBizInforme);
var
  AEditor : IEditorInformes;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    CreateEditor('EditorInformes', IEditorInformes, AEditor);
    with AEditor do
    begin
      Controller := Self; //OJO ORDEN MUY IMPORTANTE
      Informes := AInformes;
//      MultiSelect := False;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.ShowEmbedded;
end;

function TInformesController._Vacio: IBizInforme;
begin
//  Result := Buscar(ID_NULO);
end;

procedure TInformesController.RecibirAviso(ASujeto: ISujeto; ADataTable: IDAStronglyTypedDataTable);
begin
  inherited;
//
end;

procedure TInformesController.FiltrarEmpresa(AInformes: IBizInforme);
begin
{
  if AInformes.DataTable.Active then
     AInformes.DataTable.Active := False;

  // Filtrar los Informes actuales por empresa
  with AInformes.DataTable.Where do
  begin
    if NotEmpty then
      AddOperator(opAND);

    OpenBraket;
    AddText(fld_InformesID_EMPRESA + ' = ' + IntToStr(dmUsuarios.IDEmpresaActual));
    CloseBraket;
  end;
}
end;

function TInformesController.Guardar(AInforme: IBizInforme): Boolean;
var
  IDNuevo: Integer;
begin
  Result := False;

  if not Assigned(AInforme) then
    raise Exception.Create ('Informe no asignado');

  if ValidarInforme(AInforme) then
  begin
    ShowHourglassCursor;
    try
      if (AInforme.EsNuevo) then
        IDNuevo := FDataModule.GetNextID(AInforme.DataTable.LogicalName)
      else
        IDNuevo := AInforme.ID;

      AsignarID(AInforme, IDNuevo);
      AInforme.DataTable.ApplyUpdates;

      Result := True;
    finally
      HideHourglassCursor;
    end;
  end;
end;

procedure TInformesController.Nuevo(AInforme: String; AInformes : IBizInforme);
var
  FControllerInforme: IInformeBaseController;

begin
  CreateInforme(AInforme, IInformeBaseController, FControllerInforme);
  FControllerInforme.Nuevo(AInformes);
  FControllerInforme.Ver(AInformes);
end;

{
initialization
  InformesController := TInformesController.Create(nil);

finalization
  FreeAndNil(InformesController);
}
end.
