unit uDataModuleAgentes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uDataModuleContactos, DB, uDADataTable, uDAScriptingProvider,
  uDACDSDataTable, uDABINAdapter, uRORemoteService,
  uROClient, uROBinMessage, uROWinInetHttpChannel, uBizGruposEmpleado, uROTypes,
  uIDataModuleAgentes, uBizContactos, uDADesigntimeCall,
  uDAInterfaces, uDAMemDataTable, uDADataStreamer, uDABin2DataStreamer,
  uDARemoteDataAdapter, uIntegerListUtils, uBizAgentesComisiones;

type
  TDataModuleAgentes = class(TDataModuleContactos, IDataModuleAgentes)
    tbl_Agentes: TDAMemDataTable;
    ds_Agentes: TDADataSource;
    tbl_Agentes_Comisiones: TDAMemDataTable;
    ds_Agentes_Comisiones: TDADataSource;
  protected
    function _GetComisiones: IBizAgentesComisiones;
  public
    function GetItem(const ID : Integer) : IBizAgente;
    function NewItem : IBizAgente;
    function GetItems : IBizAgente;

  end;


implementation

{$R *.dfm}

uses
  FactuGES_Intf, cxControls, schContactosClient_Intf, uDataTableUtils,
  uStringsUtils;

{ TDataModuleVendedores }

function TDataModuleAgentes.GetItem(const ID: Integer): IBizAgente;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;

    with Result.DataTable.DynamicWhere do
    begin
      // (ID = :ID)
      Condicion := NewBinaryExpression(NewField('', fld_ContactosID), NewConstant(ID, datInteger), dboEqual);

      if IsEmpty then
         Expression := Condicion
      else
         Expression := NewBinaryExpression(Expression, Condicion, dboAnd);
    end;

  finally
    HideHourglassCursor;
  end;
end;

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

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

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

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

function TDataModuleAgentes._GetComisiones: IBizAgentesComisiones;
var
  AComisiones : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AComisiones := CloneDataTable(tbl_Agentes_Comisiones);
    with AComisiones do
    begin
      BusinessRulesID := BIZ_CLIENT_AGENTE_COMISIONES;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (AComisiones as IBizAgentesComisiones);
  finally
    HideHourglassCursor;
  end;
end;

end.
