unit uDataModuleAgentes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uDataModuleContactos, DB, uDADataTable, uDAScriptingProvider,
  uDACDSDataTable, uDABINAdapter, uROServiceComponent, uRORemoteService,
  uROClient, uROBinMessage, uROWinInetHttpChannel,
  uBizAgentesComisiones,
  uIDataModuleAgentes, uBizContactos, uDADesigntimeCall;

type
  TDataModuleAgentes = class(TDataModuleContactos, IDataModuleAgentes)
    tbl_Agentes: TDACDSDataTable;
    ds_Agentes: TDADataSource;
    tbl_AgentesComisiones: TDACDSDataTable;
    ds_AgentesComisiones: TDADataSource;
  protected
    function _GetComisiones: IBizAgenteComisiones;  
  public
    function GetItem(const ID : Integer) : IBizAgente;
    function NewItem : IBizAgente;
    function GetItems : IBizAgente;
    function GetNextID(const DataSetName : String) : Integer; override;    
  end;


implementation

{$R *.dfm}

uses
  cxControls, schContactosClient_Intf, uDAInterfaces, uDataTableUtils;

{ TDataModuleVendedores }

function TDataModuleAgentes.GetItem(const ID: Integer): IBizAgente;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText(fld_ContactosID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleAgentes.GetItems: IBizAgente;
var
  AContacto : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AContacto := _CloneDataTable(tbl_Agentes);
    AContacto.BusinessRulesID := BIZ_CLIENT_AGENTE;

    with TBizAgente(AContacto.BusinessEventsObj) do
    begin
      DatosBancarios := _GetDatosBancarios;
      _Categorias := _GetCategorias;
      Comisiones := _GetComisiones;
    end;

    Result := (AContacto as IBizAgente);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleAgentes.GetNextID(const DataSetName: String): Integer;
var
  aDataSet : String;
  aGeneratorName : String;
begin
  aDataSet := DataSetName;

  if aDataSet = nme_AgentesComisiones then
  begin
    aGeneratorName := 'GEN_AGENTES_COMISIONES_ID';
    Result := _InternalGetNextID(aGeneratorName);
    Exit;
  end;

  if aDataSet = nme_Agentes then
    aDataSet := nme_Contactos;

  Result := inherited GetNextID(aDataSet);
end;

function TDataModuleAgentes.NewItem: IBizAgente;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleAgentes._GetComisiones: IBizAgenteComisiones;
var
  AComisiones : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AComisiones := _CloneDataTable(tbl_AgentesComisiones);
    with AComisiones do
    begin
      BusinessRulesID := BIZ_CLIENT_AGENTE_COMISIONES;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (AComisiones as IBizAgenteComisiones);
  finally
    HideHourglassCursor;
  end;
end;

end.
