unit uDataModuleAlbaranesCliente;

interface

uses
  DB, Classes, SysUtils, uDADesigntimeCall,
  uRORemoteService, uROClient, uROBinMessage, uDAScriptingProvider,
  uDACDSDataTable, uROWinInetHttpChannel, uDADataTable, uDABINAdapter,
  uDAClientDataModule, uROTypes, uIntegerListUtils,
  uIDataModuleAlbaranesCliente, uBizAlbaranesCliente, uBizDetallesAlbaranCliente,
  uIDataModuleAlbaranesClienteReport, uDataModuleBase, uDABin2DataStreamer, uDAMemDataTable,
  uDAInterfaces, uDADataStreamer, uDARemoteDataAdapter;

type
  TDataModuleAlbaranesCliente = class(TDataModuleBase, IDataModuleAlbaranesCliente, IDataModuleAlbaranesClienteReport)
    RORemoteService: TRORemoteService;
    rda_AlbaranesCliente: TDARemoteDataAdapter;
    Bin2DataStreamer: TDABin2DataStreamer;
    tbl_AlbaranesCliente: TDAMemDataTable;
    ds_AlbaranesCliente: TDADataSource;
    tbl_AlbaranesCliente_Detalles: TDAMemDataTable;
    ds_AlbaranesCliente_Detalles: TDADataSource;
    tbl_ListaAnosAlbaranes: TDAMemDataTable;
    ds_ListaAnosAlbaranes: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  private
    function _GetDetalles : IBizDetallesAlbaranCliente;

  protected
    procedure AsignarClaseNegocio(AAlbaran: TDADataTable); virtual;

  public
    function GetAnosItems : TStringList;
    function GetItems : IBizAlbaranCliente; virtual;
    function GetItem(const ID : Integer) : IBizAlbaranCliente;
    function NewItem : IBizAlbaranCliente;

    // Report
    function GetRptAlbaranes(const ListaID: TIntegerList; const VerPrecios: Boolean = False): Binary;
    function GetEtiquetas(const AID: Integer; withRefCliente: Boolean): Binary;
    function GetRptPDFAlbaran(const AID: Integer; const VerPrecios: Boolean = False): Binary;
  end;

implementation

{$R *.DFM}

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

{ TdmAlbaranesCliente }

procedure TDataModuleAlbaranesCliente.AsignarClaseNegocio(AAlbaran: TDADataTable);
begin
  AAlbaran.BusinessRulesID := BIZ_CLIENT_ALBARAN_CLIENTE;
end;

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

function TDataModuleAlbaranesCliente.GetRptAlbaranes(const ListaID: TIntegerList; const VerPrecios: Boolean = False): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := ListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvAlbaranesCliente).GenerarInforme(AParam, VerPrecios);
  finally
    FreeAndNil(AParam);
  end;
end;

function TDataModuleAlbaranesCliente.GetRptPDFAlbaran(const AID: Integer; const VerPrecios: Boolean = False): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := TIntegerArray.Create;
  try
    AParam.Add(AID);
    Result := (RORemoteService as IsrvAlbaranesCliente).GenerarInforme(AParam, VerPrecios);
  finally
    FreeAndNil(AParam);
  end;
end;

function TDataModuleAlbaranesCliente.NewItem: IBizAlbaranCliente;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleAlbaranesCliente._GetDetalles: IBizDetallesAlbaranCliente;
var
  ADetalles : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    ADetalles := CloneDataTable(tbl_AlbaranesCliente_Detalles);
    with ADetalles do
    begin
      BusinessRulesID := BIZ_CLIENT_DETALLES_ALBARAN_CLIENTE;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADetalles as IBizDetallesAlbaranCliente);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleAlbaranesCliente.GetAnosItems: TStringList;
var
  AListaAnos: TStringList;
begin
  AListaAnos := TStringList.Create;
  ShowHourglassCursor;
  try
    with tbl_ListaAnosAlbaranes 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 TDataModuleAlbaranesCliente.GetEtiquetas(const AID: Integer; withRefCliente: Boolean): Binary;
begin
  Result := (RORemoteService as IsrvAlbaranesCliente).GenerarInformeEtiquetas(AID, withRefCliente)
end;

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

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleAlbaranesCliente.GetItems: IBizAlbaranCliente;
var
  AAlbaran : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AAlbaran := CloneDataTable(tbl_AlbaranesCliente);
    AsignarClaseNegocio(AAlbaran);

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

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

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

end.
