unit uAlbaranesClienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleAlbaranesClienteReport,
  uClientesController, uAlbaranesClienteController, uDetallesAlbaranClienteController,
  uBizAlbaranesCliente;

type
  IAlbaranesClienteReportController = interface
  ['{4BCC7A93-3322-494C-A3FA-118F4B62CB15}']
    procedure Preview(const AID : String);
    procedure Print(const AID : String);
    procedure EtiquetasPreview(const AID : integer; Const withRefCliente: Boolean);
    procedure EtiquetasPrint(const AID : integer; Const withRefCliente: Boolean);
  end;

  TAlbaranesClienteReportController = class(TInterfacedObject, IAlbaranesClienteReportController)
  private
    FDataModule : IDataModuleAlbaranesClienteReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;

  public
    constructor Create;
    destructor Destroy; override;

    procedure Preview(const AID : String);
    procedure Print(const AID : String);
    procedure EtiquetasPreview(const AID : integer; const withRefCliente: Boolean);
    procedure EtiquetasPrint(const AID : integer; const withRefCliente: Boolean);
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorAlbaranesClientePreview,
  uEditorPreview, uDataModuleAlbaranesCliente, uEditorBase, cxControls;

{ TAlbaranesClienteReportController }

constructor TAlbaranesClienteReportController.Create;
begin
  FDataModule := TDataModuleAlbaranesCliente.Create(Nil);
end;

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


destructor TAlbaranesClienteReportController.Destroy;
begin
  FDataModule := NIL;
  inherited;
end;

procedure TAlbaranesClienteReportController.EtiquetasPreview(const AID: integer; const withRefCliente: Boolean);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetEtiquetas(AID, withRefCliente);
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

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

procedure TAlbaranesClienteReportController.EtiquetasPrint(const AID: integer; const withRefCliente: Boolean);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetEtiquetas(AID, withRefCliente);
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Print
end;

procedure TAlbaranesClienteReportController.Preview(const AID : String);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AID); //Lista de seleccionados (Array de integers)
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

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

procedure TAlbaranesClienteReportController.Print(const AID : String);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AID);
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
      AEditor.LoadFromStream(AStream);
    finally
      AStream.Free;
    end;
  finally
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Print
end;

end.
