unit uAlbaranesClienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleAlbaranesClienteReport,
  uClientesController, uAlbaranesClienteController, uDetallesAlbaranClienteController,
  uBizAlbaranesCliente, uIntegerListUtils;

type
  IAlbaranesClienteReportController = interface(IControllerBase)
  ['{4BCC7A93-3322-494C-A3FA-118F4B62CB15}']
    procedure Preview(const AListaID : TIntegerList);
    procedure Print(const AListaID : TIntegerList);
    procedure EtiquetasPreview(const AID : integer; Const withRefCliente: Boolean);
    procedure EtiquetasPrint(const AID : integer; Const withRefCliente: Boolean);
    function ExportToWord(const AID: Integer; const AFileName : String = ''): Boolean;
    function ExportToPDF(const AID: Integer; const AFileName : String = ''): Boolean;        
  end;

  TAlbaranesClienteReportController = class(TControllerBase, IAlbaranesClienteReportController)
  private
    FDataModule : IDataModuleAlbaranesClienteReport;
    function CreateEditor(const AName: String; const IID: TGUID; out Intf): Boolean;

  public
    constructor Create; override;
    destructor Destroy; override;

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


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorAlbaranesClientePreview,
  uEditorPreview, uDataModuleAlbaranesCliente, uEditorBase, cxControls,
  uStringsUtils, uSistemaFunc, uFactuGES_App;

{ TAlbaranesClienteReportController }

constructor TAlbaranesClienteReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleAlbaranesCliente.Create(Nil);
end;

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


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

procedure TAlbaranesClienteReportController.EtiquetasPreview(const AID: integer; const withRefCliente: Boolean);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;
  AStream := FDataModule.GetEtiquetas(AID, withRefCliente);
  try
    CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
    if Assigned(AEditor) then
      try
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
      end;
  finally
    FreeAndNil(AStream);
    AEditor := Nil;
  end;
end;

procedure TAlbaranesClienteReportController.EtiquetasPrint(const AID: integer; const withRefCliente: Boolean);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;
  AStream := FDataModule.GetEtiquetas(AID, withRefCliente);
  try
    CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
    if Assigned(AEditor) then
      try
        AEditor.LoadFromStream(AStream);
        AEditor.Print;
      finally
        AEditor.Release;
      end;
  finally
    FreeAndNil(AStream);
    AEditor := Nil;
  end;
end;

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

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

function TAlbaranesClienteReportController.ExportToWord(const AID: Integer;
  const AFileName: String): Boolean;
var
  AStream: Binary;
  AFile : String;
begin
  Result := False;
  AFile := AFileName;
  if EsCadenaVacia(AFile) and (not PreguntarFicheroWordExportar(AFile)) then
    Exit;

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

procedure TAlbaranesClienteReportController.Preview(const AListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;
  AStream := FDataModule.GetRptAlbaranes(AListaID);
  try
    CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
    if Assigned(AEditor) then
    begin
      try
        AEditor.Controller := Self;
        AEditor.ListaID := AListaID;
        AEditor.Title := 'Albarán - ' + AppFactuGES.EmpresaActiva.NOMBRE;
        AEditor.LoadFromStream(AStream);
        AEditor.Preview;
      finally
        AEditor.Release;
      end;
    end;
  finally
    FreeAndNil(AStream);
    AEditor := Nil;
  end;
end;

procedure TAlbaranesClienteReportController.Print(const AListaID : TIntegerList);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetRptAlbaranes(AListaID);
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.Title := 'Albarán - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
