unit uRptPedidosProveedor_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
  TRptPedidosProveedor = 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;
  public
    function GenerarPedido(const AID : Integer): Binary;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleServer, schPedidosProveedorClient_Intf;

const
  rptInforme = 'InfPedidoProveedor.fr3';

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


{ TRptPedidosProveedor }


procedure TRptPedidosProveedor.DataModuleCreate(Sender: TObject);
begin
  schReport.ConnectionManager := dmServer.ConnectionManager;
  FConnection := dmServer.DarNuevaConexion;
end;

function TRptPedidosProveedor.GenerarPedido(const AID: Integer): Binary;
var
  dsMaster: IDADataset;
  dsDetail: IDADataset;
  dsResumen: IDADataset;
  AStream: TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  Result := Binary.Create;
  FConnection.BeginTransaction;
  try
    dsMaster := schReport.NewDataset(FConnection, ds_InformeCabecera, ['ID'], [AID]);
    dsDetail := schReport.NewDataset(FConnection, ds_InformeDetalles, ['ID_PEDIDO'], [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 + rptInforme, True);
    frxReport.PrepareReport;
    frxReport.PreviewPages.SaveToStream(Result);
  finally
    AStream.Free;
    FConnection.RollbackTransaction;
  end;
end;

end.
