unit uInfMargenArticuloController;

interface


uses
  Classes, SysUtils, uDADataTable, uControllerBase, uEditorDBItem,
  uBizInfMargenArticulo, uIDataModuleInfMargenArticulo, uInformeBaseController,
  uBizInformes;

type
  IInfMargenArticuloController = interface(IInformeBaseController)
  ['{1F19FFE4-7AA6-4CAB-B7F0-687FE858FD31}']
//    procedure VerTodos(AInfMargenArticulo: IBizInfMargenArticulo);
//    function BuscarTodos: IBizInfMargenArticulo;
//    function Buscar(const ID_ALMACEN: Integer): IBizInfMargenArticulo;
//    function ExtraerSeleccionados(AArticulos: IBizInfMargenArticulo) : IBizInfMargenArticulo;

//    procedure Anadir(AArticulos, AInfMargenArticulo : IBizInfMargenArticulo);
//    procedure Ver; //(AInfMargenArticulo : IBizInfMargenArticulo);
//    function Guardar(AArticulos : IBizInfMargenArticulo; const FechaMovimiento: TDateTime; const CausaMovimiento: String): Boolean;
  end;

  TInfMargenArticuloController = class(TObservador, IInfMargenArticuloController, IInformeBaseController)
  private
    function BuscarTodos: IBizInfMargenArticulo;

  protected
    FDataModule : IDataModuleInfMargenArticulo;

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

    procedure FiltrarEmpresa(AInfMargenArticulo: IBizInfMargenArticulo);

    //Estos son los tres métodos a sobre escribir si se desea heredar toda la logica de
    //este controller
    procedure AsignarDataModule; virtual;

  public
    constructor Create; virtual;
    destructor Destroy; override;

//    function Buscar(const ID_ALMACEN: Integer): IBizInfMargenArticulo;
//    function ExtraerSeleccionados(AArticulos: IBizInfMargenArticulo) : IBizInfMargenArticulo;
//    function Ver(AArticulos: IBizInfMargenArticulo; AInfMargenArticulo : IBizInfMargenArticulo): Boolean;
    procedure Nuevo(AInforme : IBizInforme);
    procedure Ver(AInforme : IBizInforme);
//    procedure VerTodos(AInfMargenArticulo: IBizInfMargenArticulo);
  end;

implementation

uses
  Forms, cxControls, DB, uEditorRegistryUtils,
  uIEditorInfMargenArticulo, uDataModuleUsuarios,
  uDataModuleInfMargenArticulo,
  uDAInterfaces, uDataTableUtils,
  uDateUtils, uROTypes, DateUtils, Controls, Windows,
  dialogs, Variants, uControllerDetallesBase, uDialogUtils,
  schInfMargenArticuloClient_Intf;

{ TInfMargenArticuloController }

procedure TInfMargenArticuloController.AsignarDataModule;
begin
  FDataModule := TdmInfMargenArticulo.Create(Nil);
end;

{
function TInfMargenArticuloController.Buscar(const ID_ALMACEN: Integer): IBizInfMargenArticulo;
begin
  Result := (FDataModule as IDataModuleInfMargenArticulo).GetItems(ID_ALMACEN);
  FiltrarEmpresa(Result);
end;
}

function TInfMargenArticuloController.BuscarTodos: IBizInfMargenArticulo;
begin
  Result := FDataModule.GetItems;
  FiltrarEmpresa(Result);
end;

constructor TInfMargenArticuloController.Create;
begin
  AsignarDataModule;
end;

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

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


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


{function TInfMargenArticuloController.Ver(AArticulos: IBizInfMargenArticulo; AInfMargenArticulo: IBizInfMargenArticulo): 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;
      InfMargenArticulo := AInfMargenArticulo;
    end;
  finally
    HideHourglassCursor;
  end;

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

procedure TInfMargenArticuloController.Ver(AInforme: IBizInforme);
var
  AEditor : IEditorInfMargenArticulo;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    CreateEditor('EditorInfMargenArticulo', IEditorInfMargenArticulo, AEditor);
    with (AEditor as IEditorInfMargenArticulo) do
    begin
      if not Assigned(FDataModule) then
         AsignarDataModule;
      Controller := Self; //OJO ORDEN MUY IMPORTANTE
      Datos := BuscarTodos;
      Informe := AInforme;


//      Datos.VisibleColumns := FDataModule.GetNameColumns;
    end;
  finally
    HideHourglassCursor;
  end;

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

{procedure TInfMargenArticuloController.VerTodos(AInfMargenArticulo: IBizInfMargenArticulo);
//var
//  AEditor : IEditorInfMargenArticulo;
begin
{  AEditor := NIL;
  ShowHourglassCursor;
  try
    CreateEditor('EditorInfMargenArticulo', IEditorInfMargenArticulo, AEditor);
    with AEditor do
    begin
      Controller := Self; //OJO ORDEN MUY IMPORTANTE
      InfMargenArticulo := AInfMargenArticulo;
      MultiSelect := False;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.ShowEmbedded;

end;}

function TInfMargenArticuloController._Vacio: IBizInfMargenArticulo;
begin
//  Result := Buscar(ID_NULO);
end;

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

procedure TInfMargenArticuloController.FiltrarEmpresa(AInfMargenArticulo: IBizInfMargenArticulo);
begin
  if AInfMargenArticulo.DataTable.Active then
    AInfMargenArticulo.DataTable.Active := False;

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

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

procedure TInfMargenArticuloController.Nuevo(AInforme: IBizInforme);
begin
  AInforme.Insert;
  AInforme.CATEGORIA := 'MARGEN ARTICULOS';
  AInforme.CONTROLLER := 'InfMargenArticuloController';
  AInforme.ID_EMPRESA := dmUsuarios.EmpresaActual.ID;
end;

end.
