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, uDADataStreamer, frxExportPDF, FactuGES_Intf;

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;
    frxReport: TfrxReport;
    frxPDFExport1: TfrxPDFExport;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    procedure DataModuleCreate(Sender: TObject);
    procedure frxReportGetValue(const VarName: string; var Value: Variant);
  private
    FConnection: IDAConnection;
    procedure _GenerarPedido(const AID : Integer); overload;
  public
    function GenerarPedido(const AID : String): Binary; overload;
    function GenerarPedidoEnPDF(const ListaID : TIntegerArray): 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;
  frxReport.EngineOptions.NewSilentMode := simReThrow;
end;

function TRptPedidosProveedor.GenerarPedido(const AID: String): Binary;
var
  ID_Pedidos: TStringList;
  i: Integer;

begin
  Result := Binary.Create;
  FConnection.BeginTransaction;

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

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

    frxReport.PreviewPages.SaveToStream(Result);

  finally
    FConnection.RollbackTransaction;
  end;
end;

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

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

procedure TRptPedidosProveedor.frxReportGetValue(const VarName: string;
  var Value: Variant);
begin
  if VarName = 'ImprimirPrecio' then
    Value := False;
  if VarName = 'ImprimirRefProveedor' then
    Value := False;
end;

procedure TRptPedidosProveedor._GenerarPedido(const AID: Integer);
var
  AStream: TMemoryStream;
  dsMaster: IDADataset;
  dsDetail: IDADataset;
begin
  AStream := TMemoryStream.Create;

  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(False);

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

end.
