unit uPedidosProveedorReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModulePedidosProveedorReport,
  uClientesController, uPedidosProveedorController, uDetallesPedidoProveedorController,
  uBizPedidosProveedor;

type
  IPedidosProveedorReportController = interface(IControllerBase)
  ['{D0686358-251C-43C4-9927-6112F2F4D3B8}']
    procedure Preview(const AListaID : String);
    procedure Print(const AListaID : String);
    function ExportToWord(const AID: Integer; const AFileName : String = ''): Boolean;
  end;

  TPedidosProveedorReportController = class(TControllerBase, IPedidosProveedorReportController)
  private
    FDataModule : IDataModulePedidosProveedorReport;
    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, uIEditorPedidosProveedorPreview,
  uEditorPreview, uDataModulePedidosProveedor, uEditorBase, cxControls,
  uFactuGES_App, uStringsUtils, uSistemaFunc;

{ TPedidosProveedorReportController }

constructor TPedidosProveedorReportController.Create;
begin
  FDataModule := TDataModulePedidosProveedor.Create(Nil);
end;

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


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

function TPedidosProveedorReportController.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.GetRptWordPedido(AID);
    try
      AStream.SaveToFile(AFile);
      Result := True;
    finally
      FreeAndNil(AStream);
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TPedidosProveedorReportController.Preview(const AListaID : String);
var
  AStream: Binary;
  AEditor : IEditorPedidosProveedorPreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AListaID);
    try
      CreateEditor('EditorPedidosProveedorPreview', IEditorPedidosProveedorPreview, AEditor);
      if Assigned(AEditor) then
      with AEditor do
      begin
        AEditor.Controller := Self;
        AEditor.ListaID := AListaID;
        Title := 'Pedido a proveedor - ' + AppFactuGES.EmpresaActiva.NOMBRE;
        LoadFromStream(AStream);
        Preview;
        Release;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TPedidosProveedorReportController.Print(const AListaID : String);
var
  AStream: Binary;
  AEditor : IEditorPedidosProveedorPreview;
begin
  AEditor := NIL;
  ShowHourglassCursor;
  try
  AStream := FDataModule.GetReport(AListaID);
    try
      CreateEditor('EditorPedidosProveedorPreview', IEditorPedidosProveedorPreview, AEditor);
      if Assigned(AEditor) then
      with AEditor do
      begin
        LoadFromStream(AStream);
        Print;
        Release;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
