unit uPedidosProveedorReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModulePedidosProveedorReport,
  uClientesController, uPedidosProveedorController, uDetallesPedidoProveedorController,
  uBizPedidosProveedor;

type
  IPedidosProveedorReportController = interface(IControllerBase)
  ['{D0686358-251C-43C4-9927-6112F2F4D3B8}']
    procedure Preview(const AID : String);
    procedure Print(const AID : String);
    function ExportToPDF(const AID: Integer; const AFileName : String = ''): Boolean;
  end;

  TPedidosProveedorReportController = class(TControllerBase, IPedidosProveedorReportController)
  private
    FDataModule : IDataModulePedidosProveedorReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;
  public
    constructor Create; override;
    destructor Destroy; override;

    procedure Preview(const AID : String);
    procedure Print(const AID : String);
    function ExportToPDF(const AID: Integer; const AFileName : String = ''): Boolean;
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorPedidosProveedorPreview,
  uEditorPreview, uDataModulePedidosProveedor, uEditorBase, cxControls,
  uFactuGES_App, uStringsUtils, uSistemaFunc;

{ TPedidosProveedorReportController }

constructor TPedidosProveedorReportController.Create;
begin
  inherited;
  FDataModule := TDataModulePedidosProveedor.Create(Nil);
end;

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


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

function TPedidosProveedorReportController.ExportToPDF(const AID: Integer;
  const AFileName: String): Boolean;
var
  AStream: Binary;
begin
  Result := False;
  if EsCadenaVacia(AFileName) then
    Exit;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptPDFPedido(AID);
    try
      AStream.SaveToFile(AFileName);
      Result := True;
    finally
      FreeAndNil(AStream);
    end;
  finally
    HideHourglassCursor;
  end;
end;

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

  AStream := FDataModule.GetReport(AID);
  try
    CreateEditor('EditorPedidosProveedorPreview', IEditorPedidosProveedorPreview, AEditor);
    if Assigned(AEditor) then
      try
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
        AEditor := NIL;
      end;
  finally
    FreeAndNil(AStream);
  end;
end;

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

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AID);
    try
      CreateEditor('EditorPedidosProveedorPreview', IEditorPedidosProveedorPreview, AEditor);
      if Assigned(AEditor) then
        try
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
          AEditor := NIL;
        end;
    finally
      FreeAndNil(AStream);
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
