unit uDataModuleFacturasProveedor;

interface

uses
  SysUtils, Classes, DB, uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,

  uIDataModuleFacturasProveedor, uBizFacturasProveedor, uBizDetallesFacturaProveedor,
  uDADesigntimeCall, uIDataModuleFacturasProveedorReport;

type
  TDataModuleFacturasProveedor = class(TDAClientDataModule, IDataModuleFacturasProveedor, IDataModuleFacturasProveedorReport)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_FacturasProveedor: TDACDSDataTable;
    ds_FacturasProveedor: TDADataSource;
    tbl_FacturasProveedor_Detalles: TDACDSDataTable;
    ds_FacturasProveedor_Detalles: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    function _GetDetalles : IBizDetallesFacturaProveedor;
  public
    function GetItems : IBizFacturaProveedor;
    function GetItem(const ID : Integer) : IBizFacturaProveedor;
    function GetNextID(const DataSetName : String) : Integer;
    function NewItem : IBizFacturaProveedor;

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

implementation

{$R *.DFM}

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

{ TdmFacturasProveedor }

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

function TDataModuleFacturasProveedor.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  if DataSetName = nme_FacturasProveedor then
    aGeneratorName := 'GEN_FACTURAS_PROVEEDOR_ID';

  if DataSetName = nme_FacturasProveedor_Detalles then
    aGeneratorName := 'GEN_FACTURA_PROV_DETALLES_ID';

  Result := (RORemoteService as IsrvFacturasProveedor).GetNextAutoInc(aGeneratorName)
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._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 TDataModuleFacturasProveedor._GetDetalles: IBizDetallesFacturaProveedor;
var
  ADetalles : TDACDSDataTable;
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;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText(fld_FacturasProveedorID + ' = ' + IntToStr(ID));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleFacturasProveedor.GetItems: IBizFacturaProveedor;
var
  AFactura : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AFactura := _CloneDataTable(tbl_FacturasProveedor);

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

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

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


end.
