unit uArticulosController;

interface


uses
  Classes, SysUtils, uDADataTable, uControllerBase, uEditorDBItem,
  uIDataModuleArticulos, uBizArticulos,
  uBizContactos;

type
  TEnumReferencia = (tReferenciaParaVentas, tRefProveedorParaVentas, tReferenciaParaCompras, tRefProveedorParaCompras);

  IArticulosController = interface(IControllerBase)
  ['{814B8A9D-E87F-4BDD-A96F-CEE04F2B348F}']
    function Buscar(const ID: Integer): IBizArticulo;
    function BuscarTodos: IBizArticulo;
    function BuscarReferencia(const Ref: String): IBizArticulo;

    procedure Ver(AArticulo : IBizArticulo);
    procedure VerTodos;
    function Existe(const ID: Integer) : Boolean;
    //procedure Preview(AArticulo : IBizArticulo);
    //procedure Print(AArticulo : IBizArticulo);

    function ExtraerSeleccionados(AArticulos: IBizArticulo) : IBizArticulo;
    function ElegirArticulos(AArticulos : IBizArticulo; AMensaje: String; AMultiSelect: Boolean): IBizArticulo;
  end;

  TArticulosController = class(TControllerBase, IArticulosController)
  private
  protected
    FDataModule : IDataModuleArticulos;

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

    procedure RecuperarObjetos(AArticulo: IBizArticulo); virtual;
    procedure AsignarDataModule; virtual;

  public
    constructor Create; override;
    destructor Destroy; override;

    function Existe(const ID: Integer) : Boolean; virtual;
    function Buscar(const ID: Integer): IBizArticulo;
    function BuscarTodos: IBizArticulo; overload;
    function BuscarReferencia(const Ref: String): IBizArticulo;

    procedure Ver(AArticulo : IBizArticulo); virtual;
    procedure VerTodos; virtual;
    procedure Preview(AArticulo : IBizArticulo); virtual;
    procedure Print(AArticulo : IBizArticulo); virtual;
    function ExtraerSeleccionados(AArticulos: IBizArticulo) : IBizArticulo; virtual;
    function ElegirArticulos(AArticulos : IBizArticulo; AMensaje: String; AMultiSelect: Boolean): IBizArticulo; virtual;
  end;

implementation

uses
  cxControls, DB, uEditorRegistryUtils, schArticulosClient_Intf, uFactuGES_App,
  uIEditorArticulos, uIEditorArticulo, uIEditorElegirArticulos,
  uDataModuleArticulos, uDataModuleUsuarios, uDAInterfaces, Dialogs,
  uDataTableUtils, uDateUtils, uROTypes, DateUtils, Controls, Windows;

{ TArticulosController }

procedure TArticulosController.AsignarDataModule;
begin
  FDataModule := TDataModuleArticulos.Create(Nil);
end;

function TArticulosController.Buscar(const ID: Integer): IBizArticulo;
begin
    Result := (FDataModule as IDataModuleArticulos).GetItem(ID);
end;

function TArticulosController.BuscarReferencia(const Ref: String): IBizArticulo;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  Result := BuscarTodos;
  try
     with Result.DataTable.DynamicWhere do
     begin
       Condicion := NewBinaryExpression(NewMacro('UpperCase', [NewField('', fld_ArticulosREFERENCIA)]),
                                        NewConstant(UpperCase(Ref), datString),dboEqual);
       if IsEmpty then
          Expression := Condicion
       else
          Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
     end;
  finally
    HideHourglassCursor;
  end;
end;

function TArticulosController.BuscarTodos: IBizArticulo;
begin
  Result := FDataModule.GetItems;
end;

constructor TArticulosController.Create;
begin
  inherited;
  AsignarDataModule;
end;

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

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

procedure TArticulosController.Ver(AArticulo: IBizArticulo);
var
  AEditor : IEditorArticulo;
begin
  AEditor := NIL;

  RecuperarObjetos(AArticulo);

  CreateEditor('EditorArticulo', IEditorArticulo, AEditor);
  if Assigned(AEditor) then
    try
      AEditor.Controller := Self; //OJO ORDEN MUY IMPORTANTE
      AEditor.Articulo := AArticulo;
      AEditor.ShowModal;
    finally
      AEditor.Release;
      AEditor := NIL;
    end;
end;

procedure TArticulosController.VerTodos;
var
  AEditor : IEditorArticulos;
begin
  AEditor := NIL;

  CreateEditor('EditorArticulos', IEditorArticulos, AEditor);
  if Assigned(AEditor) then
     with (AEditor as IEditorArticulos) do
     begin
        Controller := Self; //OJO ORDEN MUY IMPORTANTE
        ShowEmbedded;
     end;
end;

function TArticulosController._Vacio: IBizArticulo;
begin
  Result := Buscar(ID_NULO);
end;

function TArticulosController.ElegirArticulos(AArticulos: IBizArticulo; AMensaje: String; AMultiSelect: Boolean): IBizArticulo;
var
  AEditor : IEditorElegirArticulos;
begin
  Result := NIL;

  CreateEditor('EditorElegirArticulos', IEditorElegirArticulos, AEditor);
  if Assigned(AEditor) then
    try
       AEditor.Controller := Self;
       AEditor.Articulos := AArticulos;
       AEditor.MultiSelect := AMultiSelect;
       AEditor.Mensaje := AMensaje;
       if IsPositiveResult(AEditor.ShowModal) then
          Result := AEditor.ArticulosSeleccionados;
    finally
       AEditor.Release;
       AEditor := NIL;
    end;
end;

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

procedure TArticulosController.RecuperarObjetos(AArticulo: IBizArticulo);
begin
  //
end;

function TArticulosController.Existe(const ID: Integer): Boolean;
var
  AArticulo : IBizArticulo;
begin
  try
    AArticulo := Buscar(ID);
    Result := Assigned(AArticulo) and (AArticulo.ID = ID);
  finally
    AArticulo := NIL;
  end;
end;

function TArticulosController.ExtraerSeleccionados(AArticulos: IBizArticulo): IBizArticulo;
var
  ASeleccionados : IBizArticulo;
begin
  ASeleccionados := (Self.Buscar(ID_NULO) as IBizArticulo);
  CopyDataTableDA5(AArticulos.DataTable, ASeleccionados.DataTable, True);
  Result := ASeleccionados;
end;

procedure TArticulosController.Preview(AArticulo: IBizArticulo);
//var
//  AReportController : IArticulosReportController;
begin
{  AReportController := TArticulosReportController.Create;
  try
    AReportController.Preview(AArticulo.ID);
  finally
    AReportController := NIL;
  end;
}
end;

procedure TArticulosController.Print(AArticulo: IBizArticulo);
//var
//  AReportController : IArticulosReportController;
begin
{  AReportController := TArticulosReportController.Create;
  try
    AReportController.Print(AArticulo.ID);
  finally
    AReportController := NIL;
  end;
}
end;

end.
