unit uAlbaranesClienteReportController;

interface

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

type
  IAlbaranesClienteReportController = interface(IControllerBase)
  ['{4BCC7A93-3322-494C-A3FA-118F4B62CB15}']
    procedure Preview(const AListaID : String);
    procedure Print(const AListaID : String);
    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;    
  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 : String);
    procedure Print(const AListaID : String);
    function ExportToWord(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
  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;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetEtiquetas(AID, withRefCliente);
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
      if Assigned(AEditor) then
      with AEditor do
      begin
        LoadFromStream(AStream);
        Preview;
        Release;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TAlbaranesClienteReportController.EtiquetasPrint(const AID: integer; const withRefCliente: Boolean);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetEtiquetas(AID, withRefCliente);
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
      if Assigned(AEditor) then
      with AEditor do
      begin
        LoadFromStream(AStream);
        Print;
        Release;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    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 DarFicheroWordExportar(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 : String);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AListaID); //Lista de seleccionados (Array de integers)
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, AEditor);
      if Assigned(AEditor) then
        with AEditor do
        begin
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          Title := 'Albarán - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          LoadFromStream(AStream);
          Preview;
          Release;
        end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TAlbaranesClienteReportController.Print(const AListaID : String);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesClientePreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AListaID);
    try
      CreateEditor('EditorAlbaranesClientePreview', IEditorAlbaranesClientePreview, 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.
