unit uDataModuleClientes;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  DB, uDADataTable, uDAScriptingProvider,
  uDACDSDataTable, uDABINAdapter, uRORemoteService,
  uROClient, uROBinMessage, uROWinInetHttpChannel, uBizClientesDescuentos,
  uBizGruposCliente, uBizContactos, uDataModuleContactos,
  uIDataModuleClientes, uDADesigntimeCall, uDARemoteDataAdapter,
  uDADataStreamer, uRODynamicRequest, uDAInterfaces, uDAMemDataTable,
  uDABin2DataStreamer, uControllerBase, uBizContactosDatosBancarios,
  uIDataModuleContactos;

type
  TDataModuleClientes = class(TDataModuleContactos, IDataModuleClientes)
    tbl_Clientes: TDAMemDataTable;
    ds_Clientes: TDADataSource;
    tbl_ClientesDescuentos: TDAMemDataTable;
    ds_ClientesDescuentos: TDADataSource;
    tbl_GruposCliente: TDAMemDataTable;
    ds_GruposCliente: TDADataSource;
  protected
    function _GetDescuentos: IBizClienteDescuentos;
  public
    function GetItem(const ID : Integer) : IBizCliente;
    function NewItem : IBizCliente;
    function GetItems : IBizCliente;
    function GetItemsTiendaWeb : IBizCliente;
    function GetItemsFelicitacion : IBizCliente;    
    function GetGruposCliente : IBizGrupoCliente;
  end;

implementation

{$R *.dfm}

uses
  FactuGES_Intf,  cxControls, schContactosClient_Intf,
  uDataTableUtils, Dialogs, DataAbstract4_Intf;

{ TdmClientes }

function TDataModuleClientes.GetGruposCliente: IBizGrupoCliente;
var
  AGrupoCliente : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AGrupoCliente := CloneDataTable(tbl_GruposCliente);
    AGrupoCliente.BusinessRulesID := BIZ_CLIENT_GRUPOSCLIENTE;
    Result := (AGrupoCliente as IBizGrupoCliente);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleClientes.GetItem(const ID: Integer): IBizCliente;
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 TDataModuleClientes.GetItems: IBizCliente;
var
  AContacto : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AContacto := CloneDataTable(tbl_Clientes);
    AContacto.BusinessRulesID := BIZ_CLIENT_CLIENTE;

    with TBizCliente(AContacto.BusinessEventsObj) do
    begin
      DatosBancarios := _GetDatosBancarios;
      Direcciones := _GetDirecciones;
      Descuentos := _GetDescuentos;
      Personal := _GetPersonal;      
    end;

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

function TDataModuleClientes.GetItemsFelicitacion: IBizCliente;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;

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

      Clear;
      Expression := Condicion
    end;

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleClientes.GetItemsTiendaWeb: IBizCliente;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;

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

      Clear;
      Expression := Condicion
    end;

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleClientes.NewItem: IBizCliente;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleClientes._GetDescuentos: IBizClienteDescuentos;
var
  ADescuentos : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADescuentos := CloneDataTable(tbl_ClientesDescuentos);
    with ADescuentos do
    begin
      BusinessRulesID := BIZ_CLIENT_CLIENTE_DESCUENTOS;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADescuentos as IBizClienteDescuentos);
  finally
    HideHourglassCursor;
  end;
end;

end.
