unit uDataModuleFacturasCliente;

interface

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

type
  TdmFacturasCliente = class(TDAClientDataModule)
    RORemoteService: TRORemoteService;
    tbl_FacturasCliente: TDACDSDataTable;
    ds_FacturasCliente: TDADataSource;
    tbl_DetallesFacturasCliente: TDACDSDataTable;
    ds_DetallesFacturasCliente: TDADataSource;
    DABINAdapter: TDABINAdapter;
    procedure DAClientDataModuleCreate(Sender: TObject);
  private
    { Private declarations }
  public
    function GetDataTableItems(Const IDBusiness: String): TDACDSDataTable;
    function GetNextAutoinc : integer;
    function GetItem(Codigo: Integer): IBizFacturasCliente;
    function GetItems: IBizFacturasCliente;
    procedure Print(Const Codigo: Integer);
    procedure Preview(Const Codigo: Integer);
    function DarNuevaReferencia(const ClaseFactura: String) : String;
  end;

var
  dmFacturasCliente: TdmFacturasCliente;

implementation

{$R *.DFM}

uses
  Forms, Controls, uDataTableUtils, uBizContacto,
  uDataModuleContactos, uDataModuleBase,
  FactuGES_Intf, uEditorPreview, uROTypes, schFacturasClienteClient_Intf;

{ TDAClientDataModule1 }

function TdmFacturasCliente.GetItems: IBizFacturasCliente;
begin
  Result := (GetDataTableItems(BIZ_FACTURASCLIENTE) as IBizFacturasCliente);
end;

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

function TdmFacturasCliente.GetNextAutoinc: integer;
begin
  Result := (RORemoteService as IsrvFacturasCliente).GetNextAutoinc;
end;

procedure TdmFacturasCliente.Preview(const Codigo: Integer);
var
  AStream: TROBinaryMemoryStream;
  AEditorPreview : TfEditorPreview;
begin
  AStream := Nil;
  AEditorPreview := TfEditorPreview.Create(Application);
  try
    AStream := (RORemoteService as IsrvFacturasCliente).GenerateReport(Codigo);
    AEditorPreview.Report.PreviewPages.LoadFromStream(AStream);
    AEditorPreview.ShowModal;
  finally
    AEditorPreview.Release;
    AStream.Free;
  end;
end;

function TdmFacturasCliente.DarNuevaReferencia(const ClaseFactura: String): String;
begin
  if (ClaseFactura = CTE_PROFORMA)
  then Result := (RORemoteService as IsrvFacturasCliente).DarNuevaReferenciaFacturaPro
  else Result := (RORemoteService as IsrvFacturasCliente).DarNuevaReferenciaFactura;
end;

procedure TdmFacturasCliente.Print(const Codigo: Integer);
var
  AStream: TROBinaryMemoryStream;
  AEditorPreview : TfEditorPreview;
begin
  AStream := Nil;
  AEditorPreview := TfEditorPreview.Create(Application);
  try
    AStream := (RORemoteService as IsrvFacturasCliente).GenerateReport(Codigo);
    AEditorPreview.Report.PreviewPages.LoadFromStream(AStream);
    AEditorPreview.Print;
  finally
    AEditorPreview.Release;
    AStream.Free;
  end;
end;

function TdmFacturasCliente.GetItem(Codigo: Integer): IBizFacturasCliente;
var
  AFactura: IBizFacturasCliente;
begin
  AFactura := GetItems;
  with AFactura.DataTable do
  begin
    if Active then Active := False;
    Where.Clear;
    Where.AddText(fld_FacturasClienteCODIGO + ' = ' + IntToStr(Codigo));
    Active := True;
  end;
  Result := AFactura;
end;

function TdmFacturasCliente.GetDataTableItems(const IDBusiness: String): TDACDSDataTable;
var
  dtCabecera: TDACDSDataTable;
  dtDetalles: TDACDSDataTable;
  ACursor: TCursor;

begin
  ACursor := Screen.Cursor;
  Screen.Cursor := crHourGlass;
  try
    dtCabecera := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_FacturasCliente, dtCabecera);
    with dtCabecera do
    begin
      BusinessRulesID := IDBusiness;
      FieldByName(fld_FacturasClienteIVA).BusinessRulesID := 'Client.Field.IVA';
    end;

    dtDetalles := TDACDSDataTable.Create(NIL);
    CloneDataTable(tbl_DetallesFacturasCliente, dtDetalles);
    with dtDetalles do
    begin
      BusinessRulesID := BIZ_DETALLESFACTURASCLIENTE;
      DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];

      FieldByName(fld_DetallesFacturasClienteCANTIDAD).BusinessRulesID := 'Client.Field.Cantidad';
      FieldByName(fld_DetallesFacturasClienteIMPORTEUNIDAD).BusinessRulesID := 'Client.Field.ImporteUnidad';
      FieldByName(fld_DetallesFacturasClienteTIPO).BusinessRulesID := 'Client.Field.TipoDetalle';
    end;
    (dtCabecera as IBizFacturasCliente).Detalles := (dtDetalles as IBizDetallesFacturasCliente);

    Result := dtCabecera;
  finally
    Screen.Cursor := ACursor;
  end;
end;

initialization
  dmFacturasCliente := TdmFacturasCliente.Create(nil);

finalization
  FreeAndNil(dmFacturasCliente);

end.
