unit uDataModuleRecibosCliente;

interface

uses {vcl:} SysUtils, Classes, DB, DBClient,
     {RemObjects:} uDAClientDataModule, uDADataTable, uDABINAdapter,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uROWinInetHttpChannel, uDAScriptingProvider, uDACDSDataTable,
  uBizRecibosCliente;

type
  TdmRecibosCliente = class(TDAClientDataModule)
    RORemoteService: TRORemoteService;
    tbl_RecibosCliente: TDACDSDataTable;
    ds_RecibosCliente: TDADataSource;
    DABINAdapter: TDABINAdapter;
    procedure DAClientDataModuleCreate(Sender: TObject);
  public
    function GetDataTableItems(Const IDBusiness: String): TDACDSDataTable;
    function GetNextAutoinc : integer;
    function GetItem(Codigo: Integer): IBizRecibosCliente;
    function GetItems: IBizRecibosCliente;
    procedure Print(Const Codigo: Integer);
    procedure Preview(Const Codigo: Integer);
  end;

var
  dmRecibosCliente: TdmRecibosCliente;

implementation

{$R *.DFM}

uses
  Forms, Controls, uDataTableUtils, uDataModuleBase, FactuGES_Intf, uEditorPreview,
  uROTypes, uBizContacto, uDataModuleContactos, schRecibosClienteClient_Intf;

{ TDAClientDataModule1 }

function TdmRecibosCliente.GetItems: IBizRecibosCliente;
begin
  Result := (GetDataTableItems(BIZ_RECIBOSCLIENTE) as IBizRecibosCliente);
end;

procedure TdmRecibosCliente.DAClientDataModuleCreate(Sender: TObject);
begin
  RORemoteService.Channel := dmBase.Channel;
  RORemoteService.Message := dmBase.Message;
  Adapter := dmBase.Adapter;
end;

function TdmRecibosCliente.GetNextAutoinc: integer;
begin
  Result := (RORemoteService as IsrvRecibosCliente).GetNextAutoinc;
end;

procedure TdmRecibosCliente.Preview(const Codigo: Integer);
var
  AStream: TROBinaryMemoryStream;
  AEditorPreview : TfEditorPreview;
begin
  AStream := Nil;
  AEditorPreview := TfEditorPreview.Create(Application);
  try
    AStream := (RORemoteService as IsrvRecibosCliente).GenerateReport(Codigo);
    AEditorPreview.Report.PreviewPages.LoadFromStream(AStream);
    AEditorPreview.ShowModal;
  finally
    AEditorPreview.Release;
    AStream.Free;
  end;
end;

procedure TdmRecibosCliente.Print(const Codigo: Integer);
var
  AStream: TROBinaryMemoryStream;
  AEditorPreview : TfEditorPreview;
begin
  AStream := Nil;
  AEditorPreview := TfEditorPreview.Create(Application);
  try
    AStream := (RORemoteService as IsrvRecibosCliente).GenerateReport(Codigo);
    AEditorPreview.Report.PreviewPages.LoadFromStream(AStream);
    AEditorPreview.Print;
  finally
    AEditorPreview.Release;
    AStream.Free;
  end;
end;

function TdmRecibosCliente.GetItem(Codigo: Integer): IBizRecibosCliente;
var
  ARecibo: IBizRecibosCliente;
begin
  ARecibo := GetItems;
  with ARecibo.DataTable do
  begin
    if Active then Active := False;
    Where.Clear;
    Where.AddText(fld_RecibosClienteCODIGO + ' = ' + IntToStr(Codigo));
    Active := True;
  end;
  Result := ARecibo;
end;

function TdmRecibosCliente.GetDataTableItems(const IDBusiness: String): TDACDSDataTable;
var
  dtCabecera: TDACDSDataTable;
  ACursor: TCursor;

begin
  ACursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    dtCabecera := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_RecibosCliente, dtCabecera);
    with dtCabecera do
    begin
      BusinessRulesID := IDBusiness;
    end;

    Result := dtCabecera;
  finally
    Screen.Cursor := ACursor;
  end;
end;

initialization
  dmRecibosCliente := TdmRecibosCliente.Create(nil);

finalization
  FreeAndNil(dmRecibosCliente);

end.
