unit uDataModuleAlbaranesCliente;

interface

uses
  DB, Classes, SysUtils, uDADesigntimeCall, uROServiceComponent,
  uRORemoteService, uROClient, uROBinMessage, uDAScriptingProvider,
  uDACDSDataTable, uROWinInetHttpChannel, uDADataTable, uDABINAdapter,
  uDAClientDataModule, uROTypes,
  uIDataModuleAlbaranesCliente, uBizAlbaranesCliente, uBizDetallesAlbaranCliente,
  uIDataModuleAlbaranesClienteReport;

type
  TDataModuleAlbaranesCliente = class(TDAClientDataModule, IDataModuleAlbaranesCliente, IDataModuleAlbaranesClienteReport)
    ROChannel: TROWinInetHTTPChannel;
    ROMessage: TROBinMessage;
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    tbl_AlbaranesCliente: TDACDSDataTable;
    ds_AlbaranesCliente: TDADataSource;
    tbl_AlbaranesCliente_Detalles: TDACDSDataTable;
    ds_AlbaranesCliente_Detalles: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

  private
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    function _GetDetalles : IBizDetallesAlbaranCliente;

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

  public
    function GetItems : IBizAlbaranCliente; virtual;
    function GetItem(const ID : Integer) : IBizAlbaranCliente;
    function GetNextID(const DataSetName : String) : Integer;
    function NewItem : IBizAlbaranCliente;

    // Report
    function GetReport(const AID: String): Binary;
    function GetEtiquetas(const AID: Integer; withRefCliente: Boolean): Binary;
  end;

implementation

{$R *.DFM}

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

{ TdmAlbaranesCliente }

procedure TDataModuleAlbaranesCliente.AsignarClaseNegocio(var APedido: TDACDSDataTable);
begin
  APedido.BusinessRulesID := BIZ_CLIENT_ALBARAN_CLIENTE;
end;

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

function TDataModuleAlbaranesCliente.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  if DataSetName = nme_AlbaranesCliente then
    aGeneratorName := 'GEN_ALBARANES_CLI_ID';

  if DataSetName = nme_AlbaranesCliente_Detalles then
    aGeneratorName := 'GEN_ALBARANES_CLI_DETALLES_ID';

  Result := (RORemoteService as IsrvAlbaranesCliente).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleAlbaranesCliente.GetReport(const AID: String): Binary;
begin
  Result := (RORemoteService as IsrvAlbaranesCliente).GenerateReport(AID)
end;

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

function TDataModuleAlbaranesCliente._CloneDataTable(const ADataTable : TDACDSDataTable): TDACDSDataTable;
begin
  Result := NIL;

  if not Assigned(ADataTable) then
    raise Exception.Create ('No se ha asignado la tabla de origen (CloneDataTable)');

  Result := TDACDSDataTable.Create(NIL);
  CloneDataTable(ADataTable, Result);
end;

function TDataModuleAlbaranesCliente._GetDetalles: IBizDetallesAlbaranCliente;
var
  ADetalles : TDACDSDataTable;
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.GetEtiquetas(const AID: Integer; withRefCliente: Boolean): Binary;
begin
  Result := (RORemoteService as IsrvAlbaranesCliente).GenerateEtiquetas(AID, withRefCliente)
end;

function TDataModuleAlbaranesCliente.GetItem(const ID: Integer): IBizAlbaranCliente;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
         AddOperator(opAND);
      OpenBraket;
      AddText(fld_AlbaranesClienteID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleAlbaranesCliente.GetItems: IBizAlbaranCliente;
var
  APedido : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    APedido := _CloneDataTable(tbl_AlbaranesCliente);

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

    AsignarClaseNegocio(APedido);

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

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

end.
