unit uCobrosMontajeReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleCobrosMontajeReport,
  uClientesController, uBizCobrosMontaje;

type
  ICobrosMontajeReportController = interface
  ['{2FDE5654-C676-4C65-B365-53A9514823B2}']
    procedure Preview(const ACobroID : integer);
    procedure Print(const ACobroID : integer);
  end;

  TCobrosMontajeReportController = class(TInterfacedObject, ICobrosMontajeReportController)
  private
    FDataModule : IDataModuleCobrosMontajeReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Preview(const ACobroID : integer);
    procedure Print(const ACobroID : integer);
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorCobrosMontajePreview,
  uEditorPreview, uDataModuleCobrosMontaje, uEditorBase, cxControls;

{ TCobrosMontajeReportController }

constructor TCobrosMontajeReportController.Create;
begin
  FDataModule := TDataModuleCobrosMontaje.Create(Nil);
end;

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


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

procedure TCobrosMontajeReportController.Preview(const ACobroID : integer);
var
  AStream: Binary;
  AEditor : IEditorCobrosMontajePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  AStream := FDataModule.GetReport(ACobroID);
  try
    CreateEditor('EditorCobrosMontajePreview', IEditorCobrosMontajePreview, AEditor);
    AEditor.LoadFromStream(AStream);
  finally
    AStream.Free;
    HideHourglassCursor;
  end;

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

procedure TCobrosMontajeReportController.Print(const ACobroID : integer);
var
  AStream: Binary;
  AEditor : IEditorCobrosMontajePreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  AStream := FDataModule.GetReport(ACobroID);
  try
    CreateEditor('EditorCobrosMontajePreview', IEditorCobrosMontajePreview, AEditor);
    AEditor.LoadFromStream(AStream);
  finally
    AStream.Free;
    HideHourglassCursor;
  end;

  if Assigned(AEditor) then
    AEditor.Print
end;

end.
