unit uDataModuleFacturasCliente;

interface

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

  uIDataModuleFacturasCliente, uBizFacturasCliente, uBizDetallesFacturaCliente,
  uDADesigntimeCall, uIDataModuleFacturasClienteReport, uDataModuleBase,
  uDAInterfaces, uDAMemDataTable, uDADataStreamer, uDABin2DataStreamer,
  uDARemoteDataAdapter, uIntegerListUtils;

type
  TDataModuleFacturasCliente = class(TDataModuleBase, IDataModuleFacturasCliente, IDataModuleFacturasClienteReport)
    RORemoteService: TRORemoteService;
    rda_FacturasCliente: TDARemoteDataAdapter;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_FacturasCliente: TDAMemDataTable;
    ds_FacturasCliente: TDADataSource;
    tbl_FacturasCliente_Detalles: TDAMemDataTable;
    ds_FacturasCliente_Detalles: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _GetDetalles : IBizDetallesFacturaCliente;
  public
    function GetItems : IBizFacturaCliente;
    function GetItem(const ID : Integer) : IBizFacturaCliente;
    function NewItem : IBizFacturaCliente;

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

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls, 
  schFacturasClienteClient_Intf, uBizContactos, Dialogs;

{ TdmPresupuestos }

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

function TDataModuleFacturasCliente.GetReport(const AListaID: TIntegerList): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := AListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvFacturasCliente).GenerateReport(AParam);
  finally
    FreeANDNIL(AParam);
  end;
end;

function TDataModuleFacturasCliente.NewItem: IBizFacturaCliente;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleFacturasCliente._GetDetalles: IBizDetallesFacturaCliente;
var
  ADetalles : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADetalles := CloneDataTable(tbl_FacturasCliente_Detalles);
    with ADetalles do
    begin
      BusinessRulesID := BIZ_CLIENT_DETALLES_FACTURA_CLIENTE;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADetalles as IBizDetallesFacturaCliente);
  finally
    HideHourglassCursor;
  end;
end;

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

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleFacturasCliente.GetItems: IBizFacturaCliente;
var
  AFactura : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AFactura := CloneDataTable(tbl_FacturasCliente);

    // EL CAMPO REFERENCIA TIENE QUE SER AUTOREFRESH!!!!!
    AFactura.FieldByName(fld_FacturasClienteREFERENCIA).ServerAutoRefresh := TRUE;
    AFactura.BusinessRulesID := BIZ_CLIENT_FACTURA_CLIENTE;

    with TBizFacturaCliente(AFactura.BusinessEventsObj) do
    begin
      Detalles := _GetDetalles;
    end;

    Result := (AFactura as IBizFacturaCliente);
  finally
    HideHourglassCursor;
  end;
end;


end.
