unit uDataModuleFacturasProveedor;

interface

uses
  SysUtils, Classes, DB, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uRORemoteService, uROClient, uROBinMessage,
  uDataModuleBase,
  uIDataModuleFacturasProveedor, uBizFacturasProveedor, uBizDetallesFacturaProveedor,
  uDADesigntimeCall, uIDataModuleFacturasProveedorReport, uDARemoteDataAdapter,
  uDADataStreamer, uDABin2DataStreamer, uDAInterfaces, uDAMemDataTable;

type
  TDataModuleFacturasProveedor = class(TDataModuleBase, IDataModuleFacturasProveedor, IDataModuleFacturasProveedorReport)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_FacturasProveedor: TDARemoteDataAdapter;
    tbl_FacturasProveedor: TDAMemDataTable;
    ds_FacturasProveedor: TDADataSource;
    tbl_FacturasProveedor_Detalles: TDAMemDataTable;
    ds_FacturasProveedor_Detalles: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _GetDetalles : IBizDetallesFacturaProveedor;
  public
    function GetItems : IBizFacturaProveedor;
    function GetItem(const ID : Integer) : IBizFacturaProveedor;
    function NewItem : IBizFacturaProveedor;

    // Report
    function GetReport(const AFacturaID: String): Binary;
  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataTableUtils, uDataModuleConexion, cxControls,
  schFacturasProveedorClient_Intf, uBizContactos;

{ TdmFacturasProveedor }

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

function TDataModuleFacturasProveedor.GetReport(const AFacturaID: String): Binary;
begin
//  Result := (RORemoteService as IsrvFacturasProveedor).GenerateReport(AFacturaID)
end;

function TDataModuleFacturasProveedor.NewItem: IBizFacturaProveedor;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleFacturasProveedor._GetDetalles: IBizDetallesFacturaProveedor;
var
  ADetalles : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADetalles := CloneDataTable(tbl_FacturasProveedor_Detalles);
    with ADetalles do
    begin
      BusinessRulesID := BIZ_CLIENT_DETALLES_FACTURA_PROVEEDOR;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADetalles as IBizDetallesFacturaProveedor);
  finally
    HideHourglassCursor;
  end;
end;

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

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleFacturasProveedor.GetItems: IBizFacturaProveedor;
var
  AFactura : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AFactura := CloneDataTable(tbl_FacturasProveedor);
    AFactura.BusinessRulesID := BIZ_CLIENT_FACTURA_PROVEEDOR;

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

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

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


end.
