unit uDataModuleEmpresas;

interface

uses {vcl:} SysUtils, Classes, DB, DBClient, 
     {RemObjects:} uDADataTable, uDAScriptingProvider,
  uDACDSDataTable, uDABINAdapter, uRORemoteService,
  uROClient, uROBinMessage, uROWinInetHttpChannel, uDADesigntimeCall,
  uIDataModuleEmpresas, uBizEmpresas, uBizEmpresasDatosBancarios,
  uDARemoteDataAdapter, uDADataStreamer, uRODynamicRequest, uDAInterfaces,
  uDAMemDataTable, uDABin2DataStreamer, uIntegerListUtils, uDataModuleBase;

type
  TDataModuleEmpresas = class(TDataModuleBase, IDataModuleEmpresas)
    RORemoteService: TRORemoteService;
    rda_Empresas: TDARemoteDataAdapter;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_Empresas: TDAMemDataTable;
    ds_Empresas: TDADataSource;
    tbl_EmpresasDatosBanco: TDAMemDataTable;
    ds_EmpresasDatosBanco: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _GetDatosBancarios : IBizEmpresasDatosBancarios;
  public
    function GetItem(const ID : Integer) : IBizEmpresa;
    function NewItem : IBizEmpresa;
    function GetItems : IBizEmpresa;

    function DarListaFormasPago: TStringList;
    function DarListaTiposIVA: TStringList;
  end;

implementation

{$R *.DFM}

uses
  uDataModuleConexion, uDataTableUtils, Dialogs, uROTypes,
  FactuGES_Intf, schEmpresasClient_Intf, cxControls;

procedure TDataModuleEmpresas.DAClientDataModuleCreate(Sender: TObject);
begin
  RORemoteService.Channel := dmConexion.Channel;
  RORemoteService.Message := dmConexion.Message;
end;

function TDataModuleEmpresas.DarListaFormasPago: TStringList;
var
  ABinary : Binary;
begin
  ABinary := (RORemoteService as IsrvEmpresas).DarListaFormasPago;
  Result := TStringList.Create;
  Result.LoadFromStream(ABinary);
end;

function TDataModuleEmpresas.DarListaTiposIVA: TStringList;
var
  ABinary : Binary;
begin
  ABinary := (RORemoteService as IsrvEmpresas).DarListaTiposIVA;
  Result := TStringList.Create;
  Result.LoadFromStream(ABinary);
end;

function TDataModuleEmpresas.GetItem(const ID: Integer): IBizEmpresa;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;

    with Result.DataTable.DynamicWhere do
    begin
      Clear;
      // (ID = :ID)
      Expression := NewBinaryExpression(NewField('', fld_EmpresasID), NewConstant(ID, datInteger), dboEqual);
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleEmpresas.GetItems: IBizEmpresa;
var
  AEmpresa : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AEmpresa := CloneDataTable(tbl_Empresas);
    AEmpresa.BusinessRulesID := BIZ_CLIENT_EMPRESA;

    with TBizEmpresa(AEmpresa.BusinessEventsObj) do
    begin
      DatosBancarios := _GetDatosBancarios;
    end;

    Result := (AEmpresa as IBizEmpresa);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleEmpresas.NewItem: IBizEmpresa;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleEmpresas._GetDatosBancarios: IBizEmpresasDatosBancarios;
var
  ADatosBancarios : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADatosBancarios := CloneDataTable(tbl_EmpresasDatosBanco);
    with ADatosBancarios do
    begin
      BusinessRulesID := BIZ_CLIENT_EMPRESAS_DATOS_BANCARIOS;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADatosBancarios as IBizEmpresasDatosBancarios);
  finally
    HideHourglassCursor;
  end;

end;

end.
