unit uDataModuleMedicaciones;

interface

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

  uIDataModuleMedicaciones, uBizMedicaciones, uBizDetallesMedicacion,
  uIDataModuleMedicacionesReport, uDataModuleBase, uDABin2DataStreamer, uDAMemDataTable,
  uDAInterfaces, uDADataStreamer, uDARemoteDataAdapter, uIntegerListUtils;

type
  TDataModuleMedicacion = class(TDataModuleBase, IDataModuleMedicaciones, IDataModuleMedicacionesReport)
    RORemoteService: TRORemoteService;
    rda_Medicacion: TDARemoteDataAdapter;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_Medicacion: TDAMemDataTable;
    ds_Medicacion: TDADataSource;
    tbl_Medicacion_Detalles: TDAMemDataTable;
    ds_Medicacion_Detalles: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _GetDetalles : IBizDetallesMedicacion;
  protected
    procedure AsignarClaseNegocio(AMedicacion: TDADataTable); virtual;

  public
    function GetItems : IBizMedicacion; virtual;
    function GetItem(const ID : Integer) : IBizMedicacion;
    function NewItem : IBizMedicacion;

    // Report
    function GetRptMedicaciones(const AListaID: TIntegerList): Binary;
  end;

implementation

{$R *.DFM}

uses
  cxControls, Etiquetas_Intf, uDataModuleConexion, 
  uDataTableUtils, schMedicacionClient_Intf, uBizContactos;

{ TdmMedicacion }

procedure TDataModuleMedicacion.AsignarClaseNegocio(AMedicacion: TDADataTable);
begin
  AMedicacion.BusinessRulesID := BIZ_CLIENT_MEDICACION;
end;

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

function TDataModuleMedicacion.NewItem: IBizMedicacion;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleMedicacion._GetDetalles: IBizDetallesMedicacion;
var
  ADetalles : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADetalles := CloneDataTable(tbl_Medicacion_Detalles);
    with ADetalles do
    begin
      BusinessRulesID := BIZ_CLIENT_DETALLES_MEDICACION;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADetalles as IBizDetallesMedicacion);
  finally
    HideHourglassCursor;
  end;
end;

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

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleMedicacion.GetItems: IBizMedicacion;
var
  AMedicacion : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AMedicacion := CloneDataTable(tbl_Medicacion);
    AsignarClaseNegocio(AMedicacion);

    // EL CAMPO REFERENCIA TIENE QUE SER AUTOREFRESH!!!!!
    AMedicacion.FieldByName(fld_MedicacionREFERENCIA).ServerAutoRefresh := TRUE;

    with TBizMedicacion(AMedicacion.BusinessEventsObj) do
    begin
      Detalles := _GetDetalles;
    end;

    Result := (AMedicacion as IBizMedicacion);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleMedicacion.GetRptMedicaciones(
  const AListaID: TIntegerList): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := AListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvMedicacion).GenerarInforme(AParam);
  finally
    FreeANDNIL(AParam);
  end;
end;

end.
