unit uRptAlbaranesCliente_Server;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, frxClass, frxDBSet, uDAScriptingProvider,
  uDADataTable, uDACDSDataTable, DB, uDAClasses, frxChart, frxGradient,
  frxChBox, frxCross, frxOLE, frxBarcode, frxRich, uDABINAdapter, uROTypes,
  uDAInterfaces, uDADataStreamer, uDABin2DataStreamer, uDAMemDataTable,
  frxExportPDF, FactuGES_Intf;

type
  TRptAlbaranesCliente = class(TDataModule)
    DADataCabecera: TDADataSource;
    DADataDetalles: TDADataSource;
    tbl_Cabecera: TDAMemDataTable;
    tbl_Detalles: TDAMemDataTable;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    frxDBDetalles: TfrxDBDataset;
    frxReport: TfrxReport;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_Etiquetas: TDAMemDataTable;
    DADataEtiquetas: TDADataSource;
    frxDBEtiquetas: TfrxDBDataset;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    frxPDFExport1: TfrxPDFExport;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
    //Genera cada uno de los albaranes a imprimir
    procedure _GenerarAlbaran(const ID: Integer);
  public
    function GenerarAlbaran(const ListaID : TIntegerArray): Binary;
    function GenerarAlbaranEnPDF(const ListaID : TIntegerArray): Binary;
    function GenerarEtiquetas(const AID : Integer; withRefCliente: Boolean): Binary;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleServer, schAlbaranesClienteClient_Intf;

const
  rptInfAlbaran = 'InfAlbaranCliente.fr3';
  rptInfEtiquetas = 'InfEtiquetasAlbaranCliente.fr3';

  { Dataset names for schReport }
  ds_InformeCabecera = 'Informe_Cabecera';
  ds_InformeDetalles = 'Informe_Detalles';
  ds_InformeDetallesEtiquetas = 'Informe_Detalles_Etiquetas';


{ TRptAlbaranesCliente }


procedure TRptAlbaranesCliente.DataModuleCreate(Sender: TObject);
begin
  schReport.ConnectionManager := dmServer.ConnectionManager;
  FConnection := dmServer.DarNuevaConexion;
  frxReport.EngineOptions.NewSilentMode := simReThrow;
end;

function TRptAlbaranesCliente.GenerarAlbaran(const ListaID : TIntegerArray): Binary;
var
  i: Integer;
begin
  Result := Binary.Create;
  //Vamos generando todos y cada uno de los presupuestos recibidos
  for i := 0 to ListaID.Count - 1 do
    _GenerarAlbaran(ListaID.Items[i]);

  frxReport.PreviewPages.SaveToStream(Result);
end;

function TRptAlbaranesCliente.GenerarAlbaranEnPDF(
  const ListaID: TIntegerArray): Binary;
var
  i: Integer;
begin
  Result := Binary.Create;

  //Vamos generando todos y cada uno de los presupuestos recibidos
  for i := 0 to ListaID.Count - 1 do
    _GenerarAlbaran(ListaID.Items[i]);

  frxPDFExport1.Stream := Result;
  frxReport.Export(frxPDFExport1)
end;

procedure TRptAlbaranesCliente._GenerarAlbaran(const ID: integer);
begin
  FConnection.BeginTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
  try
    tbl_Cabecera.ParamByName('ID').AsInteger := ID;
    tbl_Detalles.ParamByName('ID_ALBARAN').AsInteger := ID;
    tbl_Cabecera.Active := True;
    tbl_Detalles.Active := True;

    frxReport.LoadFromFile(DarRutaInformes + rptInfAlbaran, True);
    frxReport.PrepareReport(False);
  finally
    FConnection.RollbackTransaction; //<--- Creo que no va a hacer falta. "PUES SI ES NECESARIO"
  end;
end;

function TRptAlbaranesCliente.GenerarEtiquetas(const AID: Integer; withRefCliente: Boolean): Binary;
{var
  dsMaster: IDADataset;
  dsDetail: IDADataset;
  AStream: TMemoryStream;}
begin
  Result := Binary.Create;
  FConnection.BeginTransaction;

  try
    tbl_Etiquetas.ParamByName('ID_ALBARAN').AsInteger := AID;
    tbl_Etiquetas.Active := True;

    frxReport.LoadFromFile(DarRutaInformes + rptInfEtiquetas, True);
    if withRefCliente then
       frxReport.Variables.Variables['withRefCliente'] := 1
    else
       frxReport.Variables.Variables['withRefCliente'] := 0;
    frxReport.PrepareReport(False);
    frxReport.PreviewPages.SaveToStream(Result);

  finally
    FConnection.RollbackTransaction;
  end;

{
  AStream := TMemoryStream.Create;
  Result := Binary.Create;
  FConnection.BeginTransaction;
  try
    dsMaster := schReport.NewDataset(FConnection, ds_InformeCabecera, ['ID'], [AID]);
    dsDetail := schReport.NewDataset(FConnection, ds_InformeDetallesEtiquetas, ['ID_ALBARAN'], [AID], False);

    AStream.Clear;
    DABINAdapter.WriteDataset(AStream, dsMaster, [woRows, woSchema], -1);
    DABINAdapter.ReadDataset(AStream, tbl_Cabecera, TRUE, '', TRUE, TRUE);

    AStream.Clear;
    DABINAdapter.WriteDataset(AStream, dsDetail, [woRows, woSchema], -1);
    DABINAdapter.ReadDataset(AStream, tbl_Detalles, TRUE, '', TRUE, TRUE);

    frxReport.LoadFromFile(DarRutaInformes + rptInfEtiquetas, True);
    if withRefCliente then
       frxReport.Variables.Variables['withRefCliente'] := 1
    else
       frxReport.Variables.Variables['withRefCliente'] := 0;
    frxReport.PrepareReport(False);
    frxReport.PreviewPages.SaveToStream(Result);
  finally
    AStream.Free;
    FConnection.RollbackTransaction;
  end;
}  
end;

end.
