unit uDataModuleContratosCliente;

interface

uses
  DB, Classes, SysUtils, uDADesigntimeCall,
  uRORemoteService, uROClient, uROBinMessage, uDAScriptingProvider,
  uDACDSDataTable, uROWinInetHttpChannel, uDADataTable, uDABINAdapter,

  uROTypes,
  uIDataModuleContratosCliente, uBizContratosCliente, uBizDetallesContratoCliente,
  uIDataModuleContratosClienteReport, uDataModuleBase, uDAInterfaces,
  uRODynamicRequest, uDADataStreamer, uDARemoteDataAdapter, uDABin2DataStreamer,
  uDAMemDataTable, uIntegerListUtils;


type
  TDataModuleContratosCliente = class(TDataModuleBase, IDataModuleContratosCliente, IDataModuleContratosClienteReport)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_ContratosCliente: TDARemoteDataAdapter;
    tbl_ContratosCliente: TDAMemDataTable;
    ds_ContratosCliente: TDADataSource;
    tbl_ContratosCliente_Detalles: TDAMemDataTable;
    ds_ContratosCliente_Detalles: TDADataSource;
    RORemoteService1: TRORemoteService;
    tbl_ListaAnosContratos: TDAMemDataTable;
    ds_ListaAnosContratos: TDADataSource;
    tbl_TiposCapitulos: TDAMemDataTable;
    ds_TiposCapitulos: TDADataSource;
    tbl_ContratosClienteBeneficios: TDAMemDataTable;
    ds_ContratosClienteBeneficios: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  private
    function _GetDetalles : IBizDetallesContratoCliente;

  protected
    procedure AsignarClaseNegocio(AContrato: TDADataTable);

  public
    function GetItems : IBizContratoCliente; overload;
    function GetItems(const AListaID: TIntegerList) : IBizContratoCliente; overload;

    function GetItem(const ID : Integer) : IBizContratoCliente;
    function NewItem : IBizContratoCliente;

    // Report
    function GetRptContratos(const AListaID: TIntegerList; AVerCondiciones: Boolean = false; const TamLetra: Integer = 9): Binary;
    function GetRptPDFContrato(const AID: Integer; AVerCondiciones: Boolean = false; const Firmado: Boolean = False): Binary;

    function GetAnosItems : TStringList;
    function GetContratosClienteBeneficios : IBizContratosClienteBeneficios;
  end;

implementation

{$R *.DFM}

uses
  cxControls, FactuGES_Intf, uFactuGES_App, uDataModuleConexion, Dialogs, DataAbstract4_Intf,
  uDataTableUtils, schContratosClienteClient_Intf, uBizContactos;

{ TdmContratosCliente }

procedure TDataModuleContratosCliente.AsignarClaseNegocio(AContrato: TDADataTable);
begin
  AContrato.BusinessRulesID := BIZ_CLIENT_CONTRATO_CLIENTE;
end;

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

function TDataModuleContratosCliente.GetRptPDFContrato(const AID: Integer; AVerCondiciones: Boolean = false; const Firmado: Boolean = False): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := TIntegerArray.Create;
  try
    AParam.Add(AID);
    Result := (RORemoteService as IsrvContratosCliente).GenerarInformeEnPDF(AParam, AVerCondiciones, AppFactuGES.UsuarioActivo.USERNAME, Firmado)
  finally
    FreeANDNIL(AParam)
  end;
end;

function TDataModuleContratosCliente.GetRptContratos(const AListaID: TIntegerList; AVerCondiciones: Boolean = false; const TamLetra: Integer = 9): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := AListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvContratosCliente).GenerarInforme(AParam, AVerCondiciones, TamLetra)
  finally
    FreeANDNIL(AParam)
  end;
end;

function TDataModuleContratosCliente.NewItem: IBizContratoCliente;
begin
  Result := GetItem(ID_NULO)
end;

{
function TDataModuleContratosCliente.SubirFichero(const ID: Integer; const NombreFichero: String; const Fichero: Binary): Boolean;
begin
 Result := False;
 try
   Result := (RORemoteService1 as IsrvGestorDocumentos).SubirFichero(TRdxAlmacenes_Presupuestos, ID, NombreFichero, Fichero);
 finally
   //
 end;
end;
}

function TDataModuleContratosCliente._GetDetalles: IBizDetallesContratoCliente;
var
  ADetalles : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADetalles := CloneDataTable(tbl_ContratosCliente_Detalles);
    with ADetalles do
    begin
      BusinessRulesID := BIZ_CLIENT_DETALLES_CONTRATO_CLIENTE;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADetalles as IBizDetallesContratoCliente);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContratosCliente.GetAnosItems: TStringList;
var
  AListaAnos: TStringList;
begin
  AListaAnos := TStringList.Create;
  ShowHourglassCursor;
  try
    with tbl_ListaAnosContratos do
    begin
      Open;
      First;
      while not eof do
      begin
        AListaAnos.Add(Format('%s=%s', [Fields[0].AsString, Fields[0].AsString]));
        Next;
      end;
      Close;
    end;
    Result := AListaAnos;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContratosCliente.GetContratosClienteBeneficios: IBizContratosClienteBeneficios;
var
  AContratosBeneficios: TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AContratosBeneficios := CloneDataTable(tbl_ContratosClienteBeneficios);
    AContratosBeneficios.BusinessRulesID := BIZ_CLIENT_CONTRATOS_CLIENTE_BENEFICIOS;
    Result := (AContratosBeneficios as IBizContratosClienteBeneficios);

  finally
    HideHourglassCursor;
  end;

end;

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

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContratosCliente.GetItems(
  const AListaID: TIntegerList): IBizContratoCliente;
var
  Condicion: TDAWhereExpression;
  i : Integer;
  AArray : Array of TDAWhereExpression;
begin
  if not Assigned(AListaID) then
    raise Exception.Create('ListaID no asignada (GetItems)');
    
  ShowHourglassCursor;
  try
    Result := Self.GetItems;

    with Result.DataTable.DynamicWhere do
    begin
      // (ID in (1, 2, 3...)

      SetLength(AArray, AListaID.Count);
      for i := 0 to AListaID.Count - 1 do
        AArray[i] := NewConstant(AListaID[i], datInteger);

      Condicion := NewBinaryExpression(NewField('', fld_ContratosClienteID), NewList(AArray), dboIn);

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleContratosCliente.GetItems: IBizContratoCliente;
var
  AContrato: TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AContrato := CloneDataTable(tbl_ContratosCliente);
    AsignarClaseNegocio(AContrato);

    // EL CAMPO REFERENCIA TIENEN QUE SER AUTOREFRESH!!!!!
    AContrato.FieldByName(fld_ContratosClienteREFERENCIA).ServerAutoRefresh := TRUE;

    with TBizContratoCliente(AContrato.BusinessEventsObj) do
         Detalles := _GetDetalles;

    Result := (AContrato as IBizContratoCliente);

  finally
    HideHourglassCursor;
  end;
end;

end.
