unit uDataModuleContactos;

interface

uses
  SysUtils, Classes, DB, DBClient,
  uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel,
  uRORemoteService, uROClient, uROBinMessage,
  uBizContactos, uBizContactosDatosBancarios, 
  uBizDireccionesContacto, uIDataModuleEtiquetasContactosReport,
  uIDataModuleContactos, uDADesigntimeCall, uROTypes, uRODynamicRequest,
  uDAInterfaces, uDADataStreamer, uDARemoteDataAdapter, uDABin2DataStreamer,
  uDAMemDataTable, uDataModuleBase, uBizContactosPersonal,
  uIntegerListUtils;

type
  TDataModuleContactos = class(TDataModuleBase, IDataModuleContactos, IDataModuleEtiquetasContactosReport)
    RORemoteService: TRORemoteService;
    rda_Contactos: TDARemoteDataAdapter;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_Contactos: TDAMemDataTable;
    ds_Contactos: TDADataSource;
    tbl_DireccionesContacto: TDAMemDataTable;
    ds_DireccionesContacto: TDADataSource;
    tbl_DatosBancarios: TDAMemDataTable;
    ds_DatosBancarios: TDADataSource;
    tbl_PersonalContacto: TDAMemDataTable;
    ds_PersonalContacto: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  public
    function _GetDatosBancarios : IBizContactosDatosBancarios;
    function _GetDirecciones: IBizDireccionesContacto;
    function _GetItems (IDCategoria : Integer): IBizContacto; virtual;
    function _GetPersonal: IBizContactoPersonal;
    function NewDireccion : IBizDireccionesContacto;    

    // Report
    function GetReport(const ListaID: TIntegerList): Binary;
  end;

implementation

{$R *.DFM}

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

{ TdmContactos }

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

procedure TDataModuleContactos.DataModuleDestroy(Sender: TObject);
begin
  //
end;

function TDataModuleContactos.GetReport(const ListaID: TIntegerList): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := ListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvContactos).GenerarInformeEtiquetas(AParam);
  finally
    FreeANDNIL(AParam);
  end;
end;

function TDataModuleContactos.NewDireccion: IBizDireccionesContacto;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    Result := Self._GetDirecciones;

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContactos._GetDatosBancarios: IBizContactosDatosBancarios;
var
  ADatosBancarios : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADatosBancarios := CloneDataTable(tbl_DatosBancarios);
    with ADatosBancarios do
    begin
      BusinessRulesID := BIZ_CLIENT_CONTACTOS_DATOS_BANCARIOS;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADatosBancarios as IBizContactosDatosBancarios);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContactos._GetDirecciones: IBizDireccionesContacto;
var
  ADirecciones : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADirecciones := CloneDataTable(tbl_DireccionesContacto);
    with ADirecciones do
    begin
      BusinessRulesID := BIZ_CLIENT_DIRECCIONES_CONTACTO;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADirecciones as IBizDireccionesContacto);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContactos._GetItems(IDCategoria: Integer): IBizContacto;
var
  AContacto : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AContacto := CloneDataTable(tbl_Contactos);

    with AContacto do
      case IDCategoria of
        CATEGORIA_CLIENTE     : BusinessRulesID := BIZ_CLIENT_CLIENTE;
        CATEGORIA_PROVEEDOR   : BusinessRulesID := BIZ_CLIENT_PROVEEDOR;
        CATEGORIA_EMPLEADO    : BusinessRulesID := BIZ_CLIENT_EMPLEADO;
      end;

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

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

function TDataModuleContactos._GetPersonal: IBizContactoPersonal;
var
  APersonal : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    APersonal := CloneDataTable(tbl_PersonalContacto);
    with APersonal do
    begin
      BusinessRulesID := BIZ_CLIENT_CONTACTO_PERSONAL;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (APersonal as IBizContactoPersonal);
  finally
    HideHourglassCursor;
  end;
end;

end.
