unit uPedidosClienteReportController;

interface

uses
  Classes, SysUtils, uDADataTable, uControllerBase, uIDataModulePedidosClienteReport,
  uClientesController, uPedidosClienteController, uDetallesPedidoClienteController,
  uBizPedidosCliente, uIntegerListUtils;

type
  IPedidosClienteReportController = interface(IControllerBase)
  ['{D68620B5-2EB6-47DE-89D9-560943990895}']
    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 ExportToPDF(const AID: Integer; const AFileName : String = '';
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
  end;

  TPedidosClienteReportController = class(TControllerBase, IPedidosClienteReportController)
  private
    FDataModule : IDataModulePedidosClienteReport;
    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 ExportToPDF(const AID: Integer; const AFileName : String = '';
      const VerPrecios: Boolean = True; const VerRefProveedor: Boolean = True): Boolean;
  end;


implementation

uses
  uROTypes, uEditorRegistryUtils, uIEditorPedidosClientePreview,
  uEditorPreview, uDataModulePedidosCliente, uEditorBase, cxControls,
  uFactuGES_App, uStringsUtils, uSistemaFunc, schPedidosClienteClient_Intf;

{ TPedidosClienteReportController }

constructor TPedidosClienteReportController.Create;
begin
  inherited;
  FDataModule := TDataModulePedidosCliente.Create(Nil);
end;

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


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

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

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

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

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

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

  ShowHourglassCursor;
  try
    AStream := FDataModule.GetReport(AListaID, VerPrecios, VerRefProveedor);
    try
      CreateEditor('EditorPedidosClientePreview', IEditorPedidosClientePreview, AEditor);
      if Assigned(AEditor) then
      begin
        try
          AEditor.Controller := Self;
          AEditor.ListaID := AListaID;
          AEditor.Title := 'Pedido de cliente - ' + AppFactuGES.EmpresaActiva.NOMBRE;
          AEditor.LoadFromStream(AStream);
          AEditor.TablaImpresion := nme_PedidosCliente;
          AEditor.IdTablaImpresion := AListaID.ToString;
          Result := AEditor.Print;
        finally
          AEditor.Release;
        end;
      end;
    finally
      FreeAndNil(AStream);
      AEditor := Nil;
    end;
  finally
    HideHourglassCursor;
  end;
end;

end.
