unit uFacturasClienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleFacturasClienteReport,
  uClientesController, uDetallesFacturaClienteController, uIntegerListUtils,
  uBizFacturasCliente;

type
  IFacturasClienteReportController = interface
  ['{A0F41767-4FF6-4BAE-9FC9-894DD721D756}']
    procedure Preview(const AListaID : TIntegerList);
    function Print(const AListaID : TIntegerList): Boolean;
    function ExportToPDF(const AID: Integer; const AFileName : String = ''): Boolean;
  end;

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

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

{    procedure Preview(const AFacturaID : String);
    function Print(const AFacturaID : String): Boolean;}
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorFacturasClientePreview,
  uEditorPreview, uDataModuleFacturasCliente, uEditorBase, cxControls,
  schFacturasClienteClient_Intf, uStringsUtils;

{ TFacturasClienteReportController }

constructor TFacturasClienteReportController.Create;
begin
  FDataModule := TDataModuleFacturasCliente.Create(Nil);
end;

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


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

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

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

procedure TFacturasClienteReportController.Preview(
  const AListaID: TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorFacturasClientePreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptFacturas(AListaID);
    try
      CreateEditor('EditorFacturasClientePreview', IEditorFacturasClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.LoadFromStream(AStream);
          AEditor.Preview;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TFacturasClienteReportController.Print(const AListaID: TIntegerList): Boolean;
var
  AStream: Binary;
  AEditor : IEditorFacturasClientePreview;
  IdTablaImpresion : String;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptFacturas(AListaID);
    try
      CreateEditor('EditorFacturasClientePreview', IEditorFacturasClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.LoadFromStream(AStream);
          AEditor.TablaImpresion := nme_FacturasCliente;
          AEditor.IdTablaImpresion := AListaID.ToString;
          Result := AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
