unit uDataModuleFacturasProveedor;

interface

uses
  SysUtils, Classes, DB, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uRORemoteService, uROClient, uROBinMessage,
  uDataModuleBase,
  uIDataModuleFacturasProveedor, uBizFacturasProveedor, uBizDetallesFacturaProveedor,
  uBizPedidosFacturaProveedor, uIntegerListUtils,
  uDADesigntimeCall, uIDataModuleFacturasProveedorReport, uDARemoteDataAdapter,
  uDADataStreamer, uDABin2DataStreamer, uDAInterfaces, uDAMemDataTable;

type
  TDataModuleFacturasProveedor = class(TDataModuleBase, IDataModuleFacturasProveedor, IDataModuleFacturasProveedorReport)
    RORemoteService: TRORemoteService;
    Bin2DataStreamer: TDABin2DataStreamer;
    rda_FacturasProveedor: TDARemoteDataAdapter;
    tbl_FacturasProveedor: TDAMemDataTable;
    ds_FacturasProveedor: TDADataSource;
    tbl_FacturasProveedor_Detalles: TDAMemDataTable;
    ds_FacturasProveedor_Detalles: TDADataSource;
    tbl_FacturasProveedor_Pedidos: TDAMemDataTable;
    ds_FacturasProveedor_Pedidos: TDADataSource;
    tbl_ListaAnosFacturas: TDAMemDataTable;
    ds_ListaAnosFacturas: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    function _GetDetalles : IBizDetallesFacturaProveedor;
    function _GetPedidos : IBizPedidosFacturaProveedor;

  public
    function GetItems : IBizFacturaProveedor;
    function GetItem(const ID : Integer) : IBizFacturaProveedor;
    function NewItem : IBizFacturaProveedor;

    // Report
    function GetRptFacturas(const AListaID: TIntegerList): Binary;
    function GetRptPDFFactura(const AID: Integer): Binary;

    function GetAnosItems : TStringList;
  end;

implementation

{$R *.DFM}

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

{ TdmFacturasProveedor }

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

function TDataModuleFacturasProveedor.GetRptFacturas(const AListaID: TIntegerList): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := AListaID.ToIntegerArray;
  try
    Result := (RORemoteService as IsrvFacturasProveedor).GenerarInforme(AParam);
  finally
    FreeANDNIL(AParam);
  end;
end;

function TDataModuleFacturasProveedor.GetRptPDFFactura(const AID: Integer): Binary;
var
  AParam : TIntegerArray;
begin
  AParam := TIntegerArray.Create;
  try
    AParam.Add(AID);
    Result := (RORemoteService as IsrvFacturasProveedor).GenerarInformeEnPDF(AParam);
  finally
    FreeANDNIL(AParam);
  end;
end;

function TDataModuleFacturasProveedor.NewItem: IBizFacturaProveedor;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleFacturasProveedor._GetDetalles: IBizDetallesFacturaProveedor;
var
  ADetalles : TDAMemDataTable;
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._GetPedidos: IBizPedidosFacturaProveedor;
var
  APedidos : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    APedidos := CloneDataTable(tbl_FacturasProveedor_Pedidos);
    with APedidos do
    begin
      BusinessRulesID := BIZ_CLIENT_PEDIDOS_FACTURA_PROVEEDOR;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (APedidos as IBizPedidosFacturaProveedor);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleFacturasProveedor.GetAnosItems: TStringList;
var
  AListaAnos: TStringList;
begin
  AListaAnos := TStringList.Create;
  ShowHourglassCursor;
  try
    with tbl_ListaAnosFacturas 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 TDataModuleFacturasProveedor.GetItem(const ID: Integer): IBizFacturaProveedor;
var
  Condicion: TDAWhereExpression;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;

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

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

  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleFacturasProveedor.GetItems: IBizFacturaProveedor;
var
  AFactura : TDAMemDataTable;
begin
  ShowHourglassCursor;
  try
    AFactura := CloneDataTable(tbl_FacturasProveedor);
    AFactura.BusinessRulesID := BIZ_CLIENT_FACTURA_PROVEEDOR;

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

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

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


end.
