unit uRptAlbaranesProveedor_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;

type
  TRptAlbaranesProveedor = class(TDataModule)
    DADataCabecera: TDADataSource;
    DADataDetalles: TDADataSource;
    tbl_Cabecera: TDACDSDataTable;
    tbl_Detalles: TDACDSDataTable;
    DABINAdapter: TDABINAdapter;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    frxDBDetalles: TfrxDBDataset;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    frxReport: TfrxReport;
    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;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleServer, schAlbaranesProveedorClient_Intf;

const
  rptInforme = 'InfAlbaranProveedor.fr3';

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


{ TRptAlbaranesCliente }


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

function TRptAlbaranesProveedor.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 TRptAlbaranesProveedor.GenerarAlbaran(const ID: integer);
var
  AStream: TMemoryStream;
  dsMaster: IDADataset;
  dsDetail: IDADataset;
begin
  AStream := TMemoryStream.Create;

  try
    dsMaster := schReport.NewDataset(FConnection, ds_InformeCabecera, ['ID'], [ID]);
    dsDetail := schReport.NewDataset(FConnection, ds_InformeDetalles, ['ID_ALBARAN'], [ID], 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 + rptInforme, True);
    frxReport.PrepareReport(False);

  finally
    AStream.Free;
    dsMaster := Nil;
    dsDetail := Nil;
  end;
end;

end.
