unit uDataModuleAlbaranesProveedor;

interface

uses
  DB, Classes, SysUtils, uDADesigntimeCall, 
  uRORemoteService, uROClient, uROBinMessage, uDAScriptingProvider,
  uDACDSDataTable, uROWinInetHttpChannel, uDADataTable, uDABINAdapter,
  uROTypes, uIDataModuleAlbaranesProveedorReport, uDataModuleBase,
  uIDataModuleAlbaranesProveedor, uBizAlbaranesProveedor, uBizDetallesAlbaranProveedor,
  uDARemoteDataAdapter, uDADataStreamer, uDABin2DataStreamer, uDAInterfaces,
  uDAMemDataTable, uIntegerListUtils;

type
  TDataModuleAlbaranesProveedor = class(TDataModuleBase, IDataModuleAlbaranesProveedor, IDataModuleAlbaranesProveedorReport)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_AlbaranesProveedor: TDARemoteDataAdapter;
    tbl_AlbaranesProveedor: TDAMemDataTable;
    ds_AlbaranesProveedor: TDADataSource;
    tbl_AlbaranesProveedor_Detalles: TDAMemDataTable;
    ds_AlbaranesProveedor_Detalles: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  private
    function _GetDetalles : IBizDetallesAlbaranProveedor;

  protected
    procedure AsignarClaseNegocio(var APedido: TDAMemDataTable); virtual;

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

    // Report
    function GetReport(const ListaID: TIntegerList): Binary;
    function GetRptWordAlbaran(const AID: Integer): Binary;
    function GetRptPDFAlbaran(const AID: Integer): Binary;
  end;

implementation

{$R *.DFM}

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

{ TdmAlbaranesProveedor }

procedure TDataModuleAlbaranesProveedor.AsignarClaseNegocio(var APedido: TDAMemDataTable);
begin
  APedido.BusinessRulesID := BIZ_CLIENT_ALBARAN_PROVEEDOR;
end;

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

function TDataModuleAlbaranesProveedor.GetReport(
  const ListaID: TIntegerList): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := ListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvAlbaranesProveedor).GenerarInforme(AParam);
  finally
    FreeANDNil(AParam);
  end;
end;

function TDataModuleAlbaranesProveedor.GetRptPDFAlbaran(
  const AID: Integer): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := TIntegerArray.Create;
  try
    AParam.Add(AID);
    Result := (RORemoteService as IsrvAlbaranesProveedor).GenerarInformeEnPDF(AParam);
  finally
    FreeANDNil(AParam);
  end;
end;

function TDataModuleAlbaranesProveedor.GetRptWordAlbaran(
  const AID: Integer): Binary;
begin
  Result := (RORemoteService as IsrvAlbaranesProveedor).GenerarInformeEnWord(AID);
end;

function TDataModuleAlbaranesProveedor.NewItem: IBizAlbaranProveedor;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleAlbaranesProveedor._GetDetalles: IBizDetallesAlbaranProveedor;
var
  ADetalles : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADetalles := CloneDataTable(tbl_AlbaranesProveedor_Detalles);
    with ADetalles do
    begin
      BusinessRulesID := BIZ_CLIENT_DETALLES_ALBARAN_PROVEEDOR;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADetalles as IBizDetallesAlbaranProveedor);
  finally
    HideHourglassCursor;
  end;
end;

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

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleAlbaranesProveedor.GetItems: IBizAlbaranProveedor;
var
  AAlbaran : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AAlbaran := CloneDataTable(tbl_AlbaranesProveedor);
    AsignarClaseNegocio(AAlbaran);

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

    with TBizAlbaranProveedor(AAlbaran.BusinessEventsObj) do
    begin
      Detalles := _GetDetalles;
    end;

    Result := (AAlbaran as IBizAlbaranProveedor);
  finally
    HideHourglassCursor;
  end;
end;

end.
