unit uDataModuleFacturasCliente;

interface

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

  uIDataModuleFacturasCliente, uBizFacturasCliente, uBizDetallesFacturaCliente,
  uDADesigntimeCall, uIDataModuleFacturasClienteReport;

type
  TDataModuleFacturasCliente = class(TDAClientDataModule, IDataModuleFacturasCliente, IDataModuleFacturasClienteReport)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    tbl_FacturasCliente: TDACDSDataTable;
    ds_FacturasCliente: TDADataSource;
    tbl_FacturasCliente_Detalles: TDACDSDataTable;
    ds_FacturasCliente_Detalles: TDADataSource;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;
    function _GetDetalles : IBizDetallesFacturaCliente;
  public
    function GetItems : IBizFacturaCliente;
    function GetItem(const ID : Integer) : IBizFacturaCliente;
    function GetNextID(const DataSetName : String) : Integer;
    function NewItem : IBizFacturaCliente;

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

implementation

{$R *.DFM}

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

{ TdmPresupuestos }

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

function TDataModuleFacturasCliente.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  if DataSetName = nme_FacturasCliente then
    aGeneratorName := 'GEN_FACTURAS_CLIENTE_ID';

  if DataSetName = nme_FacturasCliente_Detalles then
    aGeneratorName := 'GEN_FACTURA_CLIENTE_DETALLES_ID';

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

function TDataModuleFacturasCliente.GetReport(
  const AFacturaID: Integer): Binary;
begin
  Result := (RORemoteService as IsrvFacturasCliente).GenerateReport(AFacturaID)
end;

function TDataModuleFacturasCliente.NewItem: IBizFacturaCliente;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleFacturasCliente._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 TDataModuleFacturasCliente._GetDetalles: IBizDetallesFacturaCliente;
var
  ADetalles : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ADetalles := _CloneDataTable(tbl_FacturasCliente_Detalles);
    with ADetalles do
    begin
      BusinessRulesID := BIZ_CLIENT_DETALLES_FACTURA_CLIENTE;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (ADetalles as IBizDetallesFacturaCliente);
  finally
    HideHourglassCursor;
  end;
end;

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

function TDataModuleFacturasCliente.GetItems: IBizFacturaCliente;
var
  AFactura : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AFactura := _CloneDataTable(tbl_FacturasCliente);
    AFactura.BusinessRulesID := BIZ_CLIENT_FACTURA_CLIENTE;

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

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


end.
