unit uAlbaranesProveedorReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModuleAlbaranesProveedorReport,
  uAlbaranesProveedorController, uDetallesAlbaranProveedorController,
  uBizAlbaranesProveedor, uIntegerListUtils;

type
  IAlbaranesProveedorReportController = interface(IControllerBase)
  ['{11F9D087-C6BE-4401-AB13-4342025D62BB}']
    procedure Preview(const AListaID : TIntegerList;
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True);
    function Print(const AListaID : TIntegerList;
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
    function ExportToWord(const AID: Integer; const AFileName : String = '';
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
    function ExportToPDF(const AID: Integer; const AFileName : String = '';
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
  end;

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

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


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorAlbaranesProveedorPreview,
  uEditorPreview, uDataModuleAlbaranesProveedor, uEditorBase, cxControls,
  uStringsUtils, uSistemaFunc, uFactuGES_App, schAlbaranesProveedorClient_Intf;

{ TAlbaranesProveedorReportController }

constructor TAlbaranesProveedorReportController.Create;
begin
  inherited;
  FDataModule := TDataModuleAlbaranesProveedor.Create(Nil);
end;

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


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

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

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

function TAlbaranesProveedorReportController.ExportToWord(const AID: Integer; const AFileName : String = '';
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): 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, VerPrecios, VerRefProveedor);
    try
      AStream.SaveToFile(AFile);
      Result := True;
    finally
      FreeAndNil(AStream);
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TAlbaranesProveedorReportController.Preview(const AListaID: TIntegerList;
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True);
var
  AStream: Binary;
  AEditor : IEditorAlbaranesProveedorPreview;
begin
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AListaID, VerPrecios, VerRefProveedor);
    try
      CreateEditor('EditorAlbaranesProveedorPreview', IEditorAlbaranesProveedorPreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.VerPrecios := VerPrecios;
          AEditor.VerRefProveedor := VerRefProveedor;
          AEditor.Title := 'Albarán de proveedor - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.Preview;
        finally
          AEditor.Release;
          AEditor := Nil;
        end;
      end;
    finally
      FreeANDNil(AStream)
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TAlbaranesProveedorReportController.Print(const AListaID: TIntegerList;
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
var
  AStream: Binary;
  AEditor : IEditorAlbaranesProveedorPreview;
begin
  Result := False;
  AEditor := NIL;

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AListaID, VerPrecios, VerRefProveedor);
    try
      CreateEditor('EditorAlbaranesProveedorPreview', IEditorAlbaranesProveedorPreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.VerPrecios := VerPrecios;
          AEditor.VerRefProveedor := VerRefProveedor;
          AEditor.Title := 'Albarán de proveedor - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.TablaImpresion := nme_AlbaranesProveedor;
          AEditor.IdTablaImpresion := AListaID.ToString;
          Result := AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
