unit uDataModuleAlbaranesProveedor;

interface

uses
  DB, Classes, SysUtils, uDADesigntimeCall, uROServiceComponent,
  uRORemoteService, uROClient, uROBinMessage, uDAScriptingProvider,
  uDACDSDataTable, uROWinInetHttpChannel, uDADataTable, uDABINAdapter,
  uDAClientDataModule, uROTypes, uIDataModuleAlbaranesProveedorReport,
  uIDataModuleAlbaranesProveedor, uBizAlbaranesProveedor, uBizDetallesAlbaranProveedor;

type
  TDataModuleAlbaranesProveedor = class(TDAClientDataModule, IDataModuleAlbaranesProveedor, IDataModuleAlbaranesProveedorReport)
    ROChannel: TROWinInetHTTPChannel;
    ROMessage: TROBinMessage;
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    tbl_AlbaranesProveedor: TDACDSDataTable;
    ds_AlbaranesProveedor: TDADataSource;
    tbl_AlbaranesProveedor_Detalles: TDACDSDataTable;
    ds_AlbaranesProveedor_Detalles: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);

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

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

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

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

implementation

{$R *.DFM}

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

{ TdmAlbaranesProveedor }

procedure TDataModuleAlbaranesProveedor.AsignarClaseNegocio(var APedido: TDACDSDataTable);
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.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  if DataSetName = nme_AlbaranesProveedor then
    aGeneratorName := 'GEN_ALBARANES_PRO_ID';

  if DataSetName = nme_AlbaranesProveedor_Detalles then
    aGeneratorName := 'GEN_ALBARANES_PRO_DETALLES_ID';

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

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

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

function TDataModuleAlbaranesProveedor._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 TDataModuleAlbaranesProveedor._GetDetalles: IBizDetallesAlbaranProveedor;
var
  ADetalles : TDACDSDataTable;
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;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
         AddOperator(opAND);
      OpenBraket;
      AddText(fld_AlbaranesProveedorID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleAlbaranesProveedor.GetItems: IBizAlbaranProveedor;
var
  AAlbaran : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AAlbaran := _CloneDataTable(tbl_AlbaranesProveedor);

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

    AsignarClaseNegocio(AAlbaran);

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

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

end.
