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;

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;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
    //Genera cada uno de los albaranes a imprimir
    procedure GenerarAlbaran(const ID: integer); overload;
  public
    function GenerarAlbaran(const AID : String): Binary; overload;
    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 AID: String): Binary;
var
  ID_Albaranes: TStringList;
  i: Integer;
begin
  Result := Binary.Create;
  FConnection.BeginTransaction;

  try
    ID_Albaranes := TStringList.Create;
    ID_Albaranes.CommaText := AID;

    //Vamos generando todos y cada uno de los albaranes recibidos
    for i := 0 to ID_Albaranes.Count - 1 do
        GenerarAlbaran(StrToInt(ID_Albaranes.Strings[i]));

    frxReport.PreviewPages.SaveToStream(Result);

  finally
    FConnection.RollbackTransaction;
  end;
end;

procedure TRptAlbaranesCliente.GenerarAlbaran(const ID: integer);
begin
  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);
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.
