unit uRptFacturasProveedor_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
  TRptFacturasProveedor = class(TDataModule)
    DADataCabecera: TDADataSource;
    DADataDetalles: TDADataSource;
    tbl_FacturaProveedor: TDACDSDataTable;
    tbl_DetallesFacturaProveedor: TDACDSDataTable;
    DABINAdapter: TDABINAdapter;
    frxRichObject1: TfrxRichObject;
    frxBarCodeObject1: TfrxBarCodeObject;
    frxOLEObject1: TfrxOLEObject;
    frxCrossObject1: TfrxCrossObject;
    frxCheckBoxObject1: TfrxCheckBoxObject;
    frxGradientObject1: TfrxGradientObject;
    frxChartObject1: TfrxChartObject;
    frxDBCabecera: TfrxDBDataset;
    frxDBDetalles: TfrxDBDataset;
    tbl_Vencimientos: TDACDSDataTable;
    DADataVencimientos: TDADataSource;
    frxDBDataset1: TfrxDBDataset;
    schReport: TDASchema;
    DataDictionary: TDADataDictionary;
    frxReport: TfrxReport;
    procedure DataModuleCreate(Sender: TObject);
  private
    FConnection: IDAConnection;
    procedure GenerarFactura(const AFacturaID: integer); overload;
  public
    function GenerarFactura(const AFacturaID : String): Binary; overload;
  end;

implementation

{$R *.dfm}

uses
  uDataModuleServer, schFacturasProveedorClient_Intf;

const
  rptFacturaProveedor = 'InfFacturaProveedor.fr3';

  { Dataset names for schReport }
  ds_InformeFacturasProveedor = 'InformeFacturasProveedor';
  ds_InformeFacturasProveedor_Detalles = 'InformeFacturasProveedor_Detalles';
  ds_InformeFacturasProveedor_Vencimientos = 'InformeFacturasProveedor_Vencimientos';

{ TRptFacturasProveedor }


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

function TRptFacturasProveedor.GenerarFactura(const AFacturaID: String): Binary;
var
  ID_Facturas: TStringList;
  i: Integer;

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

  try
    ID_Facturas := TStringList.Create;
    ID_Facturas.CommaText := AFacturaID;

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

    frxReport.PreviewPages.SaveToStream(Result);

  finally
    FConnection.RollbackTransaction;
  end;
end;

procedure TRptFacturasProveedor.GenerarFactura(const AFacturaID: integer);
var
  AStream: TMemoryStream;
  dsMaster: IDADataset;
  dsDetail: IDADataset;
  dsVencimientos : IDADataset;
begin
  AStream := TMemoryStream.Create;

  try
    dsMaster := schReport.NewDataset(FConnection, ds_InformeFacturasProveedor, ['ID'], [AFacturaID]);
    dsDetail := schReport.NewDataset(FConnection, ds_InformeFacturasProveedor_Detalles, ['ID_FACTURA'], [AFacturaID], False);
    dsVencimientos := schReport.NewDataset(FConnection, ds_InformeFacturasProveedor_Vencimientos, ['ID_FACTURA'], [AFacturaID], False);

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

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

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


    frxReport.LoadFromFile(DarRutaInformes + rptFacturaProveedor, True);
    frxReport.PrepareReport(False);

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

end.
