unit uDataModuleContactos; interface uses {vcl:} SysUtils, Classes, DB, DBClient, {RemObjects:} uDAClientDataModule, uDADataTable, uDABINAdapter, uROServiceComponent, uRORemoteService, uROClient, uROBinMessage, uROWinInetHttpChannel, uDAScriptingProvider, uDACDSDataTable, uBizContacto; type TdmContactos = class(TDAClientDataModule) DABINAdapter: TDABINAdapter; RORemoteService: TRORemoteService; tbl_Categorias: TDACDSDataTable; ds_Categorias: TDADataSource; tbl_CategoriasContacto: TDACDSDataTable; ds_CategoriasContacto: TDADataSource; tbl_Contactos: TDACDSDataTable; ds_Contactos: TDADataSource; procedure DAClientDataModuleCreate(Sender: TObject); private function GetContactos(CodigoCategoria : Integer): IBizContacto; function GetContacto(CodigoCategoria : Integer; const Codigo : Integer): IBizContacto; overload; public function PuedoEliminarContacto (CodigoContacto : Integer) : Boolean; function GetNextAutoinc : integer; procedure Preview; procedure GetContacto(AContacto : IBizContacto; const Codigo : Integer); overload; function GetCliente(Codigo : Integer): IBizCliente; function GetProveedor(Codigo : Integer): IBizProveedor; function GetInstalador(Codigo : Integer): IBizInstalador; function GetVendedor(Codigo : Integer): IBizVendedor; function GetClientes: IBizCliente; function GetProveedores: IBizProveedor; function GetInstaladores : IBizInstalador; function GetVendedores : IBizVendedor; function GetItemsSeleccionados(ASource : IBizContacto): IBizContacto; end; var dmContactos: TdmContactos; implementation {$R *.DFM} uses Controls, Forms, uDAInterfaces, DataAbstract_Intf, FactuGES_Intf, uDataTableUtils, uROTypes, uEditorPreview, Dialogs, schContactosClient_Intf, uDataModuleBase, uDBSelectionList; procedure TdmContactos.DAClientDataModuleCreate(Sender: TObject); begin RORemoteService.Channel := dmBase.Channel; RORemoteService.Message := dmBase.Message; end; function TdmContactos.GetClientes: IBizCliente; var ACliente: IBizCliente; begin ACliente := (GetContactos(CLIENTE) as IBizCliente); Result := ACliente; end; function TdmContactos.GetProveedores: IBizProveedor; var AProveedor: IBizProveedor; begin AProveedor := (GetContactos(PROVEEDOR) as IBizProveedor); Result := AProveedor; end; function TdmContactos.GetContactos(CodigoCategoria : Integer): IBizContacto; var dtContactos: TDACDSDataTable; dtCategorias: TDACDSDataTable; ACursor: TCursor; begin ACursor := Screen.Cursor; Screen.Cursor := crHourGlass; try dtContactos := TDACDSDataTable.Create(NIL); CloneDataTable(tbl_Contactos, dtContactos); with dtContactos do begin case CodigoCategoria of CLIENTE : BusinessRulesID := BIZ_CLIENTE; PROVEEDOR : BusinessRulesID := BIZ_PROVEEDOR; INSTALADOR : BusinessRulesID := BIZ_INSTALADOR; VENDEDOR : begin BusinessRulesID := BIZ_VENDEDOR; with Fields.Add do begin Name := fld_ContactosCOMISION; DisplayLabel := 'Comisión'; LogChanges := False; Calculated := True; Alignment := taRightJustify; DataType := datFloat; BlobType := dabtUnknown; InPrimaryKey := False; Lookup := False; LookupCache := False; end; end; end; Where.Clear; // No funciona porque la columna no está en la select // --> Where.AddCondition(fld_CategoriasContactoCODIGOCATEGORIA, cEqual, CLIENTE); Where.AddText(fld_ContactosBAJA_LOGICA +' = 0 and ' + fld_CategoriasContactoCODIGOCATEGORIA + '=' + IntToStr(CODIGOCATEGORIA)); end; dtCategorias := TDACDSDataTable.Create(NIL); CloneDataTable(tbl_CategoriasContacto, dtCategorias); with dtCategorias do begin BusinessRulesID := BIZ_CATEGORIACONTACTO; DetailOptions := DetailOptions - [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates]; end; (dtContactos as IBizContacto).Categorias := (dtCategorias as IBizCategoriasContacto); Result := (dtContactos as IBizContacto); finally Screen.Cursor := ACursor; end; end; procedure TdmContactos.Preview; var AStream: TMemoryStream; AEditorPreview : TfEditorPreview; begin AStream := Binary.Create; AEditorPreview := TfEditorPreview.Create(Application); try AStream := (RemoteService as ISrvContactos).GenerateReport; AEditorPreview.Report.PreviewPages.LoadFromStream(AStream); AEditorPreview.ShowModal; finally AEditorPreview.Release; AStream.Free; end; end; function TdmContactos.GetCliente(Codigo: Integer): IBizCliente; var ACursor: TCursor; begin ACursor := Screen.Cursor; Screen.Cursor := crHourGlass; try Result := (GetContacto(CLIENTE, Codigo) as IBizCliente); finally Screen.Cursor := ACursor; end; end; function TdmContactos.GetItemsSeleccionados(ASource: IBizContacto): IBizContacto; var aObj : ISelectedRowList; dtContacto : IBizContacto; i : integer; begin if Supports(ASource, ISelectedRowList, aObj) then begin if (aObj.SelectedRows.Count = 1) then begin ASource.DataTable.GotoBookmark(aObj.SelectedRows[0]); if Supports(ASource, IBizCliente) then Result := GetCliente(ASource.CODIGO) else if Supports(ASource, IBizProveedor) then Result := GetProveedor(ASource.CODIGO); // else if Supports(ASource, IBizInstalador) then // Result := GetInstalador(ASource.CODIGO); Exit; end else begin if Supports(ASource, IBizCliente) then dtContacto := GetClientes else if Supports(ASource, IBizProveedor) then dtContacto := GetProveedores; // else if Supports(ASource, IBizInstalador) then // dtContacto := GetInstaladores; end; end else raise Exception.Create('Interface no soportada (GetItemsSeleccionados)'); if dtContacto.DataTable.Active then dtContacto.DataTable.Active := False; with dtContacto.DataTable.Where do begin Clear; for i := 0 to aObj.SelectedRows.Count - 1 do begin ASource.DataTable.GotoBookmark(aObj.SelectedRows[i]); if i > 0 then AddOperator(opAND); AddCondition(fld_ContactosCODIGO, cEqual, ASource.CODIGO); end; end; Result := dtContacto; end; function TdmContactos.GetInstaladores: IBizInstalador; var AInstalador: IBizInstalador; begin AInstalador := (GetContactos(INSTALADOR) as IBizInstalador); Result := AInstalador; end; function TdmContactos.GetProveedor(Codigo: Integer): IBizProveedor; var ACursor: TCursor; begin ACursor := Screen.Cursor; Screen.Cursor := crHourGlass; try Result := (GetContacto(PROVEEDOR, Codigo) as IBizProveedor); finally Screen.Cursor := ACursor; end; end; function TdmContactos.GetNextAutoinc: integer; begin Result := (RORemoteService as IsrvContactos).GetNextAutoinc; end; function TdmContactos.PuedoEliminarContacto( CodigoContacto: Integer): Boolean; begin Result := (RORemoteService as IsrvContactos).PuedoEliminarContacto(CodigoContacto); end; function TdmContactos.GetVendedores: IBizVendedor; var AVendedor: IBizVendedor; begin AVendedor := (GetContactos(VENDEDOR) as IBizVendedor); Result := AVendedor; end; procedure TdmContactos.GetContacto(AContacto: IBizContacto; const Codigo: Integer); var AuxCategoria : Integer; begin with AContacto.DataTable do begin if Active then Active := False; Where.Clear; // No funciona porque la columna no está en la select // --> Where.AddCondition(fld_CategoriasContactoCODIGOCATEGORIA, cEqual, CLIENTE); if Supports(AContacto, IBizCliente) then AuxCategoria := CLIENTE else begin if Supports(AContacto, IBizProveedor) then AuxCategoria := PROVEEDOR else begin if Supports(AContacto, IBizInstalador) then AuxCategoria := INSTALADOR else begin if Supports(AContacto, IBizVendedor) then AuxCategoria := VENDEDOR else raise Exception.Create('Tipo de contacto desconocido (GetContacto)'); end; end; end; Where.AddText(fld_CategoriasContactoCODIGOCATEGORIA + '=' + IntToStr(AuxCategoria)); Where.AddOperator(opAND); Where.AddCondition(fld_ContactosCODIGO, cEqual, Codigo); end; end; function TdmContactos.GetInstalador(Codigo: Integer): IBizInstalador; var ACursor: TCursor; begin ACursor := Screen.Cursor; Screen.Cursor := crHourGlass; try Result := (GetContacto(INSTALADOR, Codigo) as IBizInstalador); finally Screen.Cursor := ACursor; end; end; function TdmContactos.GetVendedor(Codigo: Integer): IBizVendedor; var ACursor: TCursor; begin ACursor := Screen.Cursor; Screen.Cursor := crHourGlass; try Result := (GetContacto(VENDEDOR, Codigo) as IBizVendedor); finally Screen.Cursor := ACursor; end; end; function TdmContactos.GetContacto(CodigoCategoria: Integer; const Codigo: Integer): IBizContacto; var AContactos : IBizContacto; begin AContactos := GetContactos(CodigoCategoria) as IBizContacto; GetContacto(AContactos, Codigo); Result := AContactos; end; initialization dmContactos := TdmContactos.Create(nil); finalization FreeAndNil(dmContactos); end.