unit uDataModuleReferenciaGenerica;

interface

uses {vcl:} SysUtils, Classes, DB, DBClient, 
     {RemObjects:} uDAClientDataModule, uDADataTable, uDAScriptingProvider,
  uDACDSDataTable, uDABINAdapter, uROServiceComponent, uRORemoteService,
  uROClient, uROBinMessage, uROWinInetHttpChannel, uDataModuleBase,
  uBizReferenciaGenerica;

type
  TdmReferenciaGenerica = class(TDAClientDataModule)
    RORemoteService: TRORemoteService;
    tbl_ReferenciaGenerica: TDACDSDataTable;
    ds_ReferenciaGenerica: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    procedure Print(const Centro: string; const Vendedor : String; const Estado : String; const AFiltro : string = '');
    procedure Preview(const Centro: string; const Vendedor : String; const Estado : String; const AFiltro : string = '');
    function GetItems : IBizReferenciaGenerica;
    function GetProducto(const Codigo: String; const Talla: String;
          const Color: String; out CodigoBarras: String; out Descripcion: String): Boolean;
    function GetSelectedItems(const AItems : IBizReferenciaGenerica): IBizReferenciaGenerica;
  end;

var
  dmReferenciaGenerica: TdmReferenciaGenerica;

implementation

{$R *.DFM}

uses
  Forms, Windows, Controls, VARELA_Intf, uEditorPreview,
  uDataTableUtils, uROTypes, cxControls;

procedure TdmReferenciaGenerica.DAClientDataModuleCreate(Sender: TObject);
begin
  RORemoteService.Channel := dmBase.Channel;
  RORemoteService.Message := dmBase.Message;
end;

function TdmReferenciaGenerica.GetItems: IBizReferenciaGenerica;
var
  dtLista: TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    dtLista := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_ReferenciaGenerica, dtLista);
    dtLista.BusinessRulesID := BIZ_REFERENCIAGENERICA;
    Result := (dtLista as IBizReferenciaGenerica);
  finally
    HideHourglassCursor;
  end;
end;

function TdmReferenciaGenerica.GetProducto(const Codigo, Talla,
  Color: String; out CodigoBarras, Descripcion: String): Boolean;
begin
  Result := (RORemoteService as IsrvReferenciaGenerica).GetProducto(Codigo,
    Talla, Color, CodigoBarras, Descripcion);
end;

function TdmReferenciaGenerica.GetSelectedItems(
  const AItems: IBizReferenciaGenerica): IBizReferenciaGenerica;
var
  dtLista : TDACDSDataTable;
  ATarget : TDADataTable;
begin
  ShowHourglassCursor;
  try
    dtLista := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_ReferenciaGenerica, dtLista, False);
    CopyDataTable(AItems.DataTable, dtLista, True);
    dtLista.BusinessRulesID := BIZ_REFERENCIAGENERICA;
    dtLista.LogChanges := True; // Para guardar los cambios
    Result := (dtLista as IBizReferenciaGenerica);
  finally
    HideHourglassCursor;
  end;
end;

procedure TdmReferenciaGenerica.Preview(const Centro, Vendedor: String; const Estado : String;
  const AFiltro : String);
var
  AStream: TROBinaryMemoryStream;
  AEditorPreview : TfEditorPreview;
begin
  AEditorPreview := TfEditorPreview.Create(Application);
  try
    ShowHourglassCursor;
    try
      AStream := (RORemoteService as IsrvReferenciaGenerica).GenerateReport(Centro, Vendedor, Estado, AFiltro);
      AEditorPreview.Report.PreviewPages.LoadFromStream(AStream);
    finally
      HideHourglassCursor;
    end;
    AEditorPreview.ShowModal;
  finally
    AEditorPreview.Release;
    AStream.Free;
  end;
end;

procedure TdmReferenciaGenerica.Print(const Centro, Vendedor: String; const Estado : String;
  const AFiltro : String);
var
  AStream: TROBinaryMemoryStream;
  AEditorPreview : TfEditorPreview;
begin
  AEditorPreview := TfEditorPreview.Create(Application);
  try
    ShowHourglassCursor;
    try
      AStream := (RORemoteService as IsrvReferenciaGenerica).GenerateReport(Centro, Vendedor, Estado, AFiltro);
      AEditorPreview.Report.PreviewPages.LoadFromStream(AStream);
    finally
      HideHourglassCursor;
    end;
    AEditorPreview.Print;
  finally
    AEditorPreview.Release;
    AStream.Free;
  end;
end;

initialization
  dmReferenciaGenerica := TdmReferenciaGenerica.Create(nil);

finalization
  FreeAndNil(dmReferenciaGenerica);

end.
