unit uFacturasProveedorReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleFacturasProveedorReport,
  uClientesController, uDetallesFacturaProveedorController, uBizFacturasProveedor,
  uIntegerListUtils;

type
  IFacturasProveedorReportController = interface(IControllerBase)
  ['{551F82DC-B8DC-482E-A20D-15003E845078}']
    procedure Preview(const AListaID : TIntegerList);
    function Print(const AListaID : TIntegerList): Boolean;
    function ExportToPDF(const AID: Integer; const AFileName : String = ''): Boolean;
  end;

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

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


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorFacturasProveedorPreview,
  uEditorPreview, uDataModuleFacturasProveedor, uEditorBase, cxControls,
  uStringsUtils, schFacturasProveedorClient_Intf;

{ TFacturasProveedorReportController }

constructor TFacturasProveedorReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleFacturasProveedor.Create(Nil);
end;

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


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

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

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

procedure TFacturasProveedorReportController.Preview(const AListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorFacturasProveedorPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptFacturas(AListaID);
    try
      CreateEditor('EditorFacturasProveedorPreview', IEditorFacturasProveedorPreview, AEditor);
      if Assigned(AEditor) then
        try
          AEditor.LoadFromStream(AStream);
          AEditor.Preview;
        finally
          AEditor.Release;
        end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TFacturasProveedorReportController.Print(const AListaID : TIntegerList): Boolean;
var
  AStream: Binary;
  AEditor : IEditorFacturasProveedorPreview;
begin
  Result := False; 
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptFacturas(AListaID);
    try
      CreateEditor('EditorFacturasProveedorPreview', IEditorFacturasProveedorPreview, AEditor);
      if Assigned(AEditor) then
        try
          AEditor.LoadFromStream(AStream);
          AEditor.TablaImpresion := nme_FacturasProveedor;
          AEditor.IdTablaImpresion := AListaID.ToString;
          Result := AEditor.Print;
        finally
          AEditor.Release;
        end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
