unit uFacturasClienteReportController;

interface

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

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

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

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


implementation

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

{ 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.ExportToWord(const AID: Integer;
  const AFileName: String): Boolean;
var
  AStream: Binary;
  AFile : String;
begin
  Result := False;
  AFile := AFileName;
  
  if EsCadenaVacia(AFile) and (not DarFicheroWordExportar(AFile)) then
    Exit;

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

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

procedure TFacturasClienteReportController.Print(const AListaID : String);
var
  AStream: Binary;
  AEditor : IEditorFacturasClientePreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptFacturas(AListaID);
    try
      CreateEditor('EditorFacturasClientePreview', IEditorFacturasClientePreview, AEditor);
      if Assigned(AEditor) then
      with AEditor do
      begin
        AEditor.Controller := Self;
        AEditor.ListaID := AListaID;
        LoadFromStream(AStream);
        Print;
        Release;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
