unit TablaDocumentos; interface uses //Generales SysUtils, Classes, Controls, IBSQL, cxGridDBTableView, cxCustomData, DB, //Particulares TablaPropiedades, //Aplicacion Framework, StrFunc, Entidades, Constantes, BaseDatos; type TListaPreciosDocumento = class; TListaPropiedadesDocumento = class; TDocumento = class(TObjeto) private FListaPropiedades : TListaPropiedadesDocumento; FListaPrecios : TListaPreciosDocumento; protected procedure ObtenerDatos; override; public Codigo : String; Descripcion : String; Familia : Variant; FormaPago : String; PlazoEntrega : String; Notas : String; ContratoVenta : String; Recomendaciones : String; constructor Create(Codigo : String); destructor Destroy; override; property ListaPropiedades : TListaPropiedadesDocumento read FListaPropiedades; property ListaPrecios : TListaPreciosDocumento read FListaPrecios; end; TListaPreciosDocumento = class(TStringList) private FCodigoDocumento : String; protected procedure ObtenerLista; public constructor Create(CodigoDocumento : String); overload; constructor Create; overload; end; TListaPropiedadesDocumento = class(TListaPropiedades) private FCodigoDocumento : String; protected procedure AssignTo(Dest: TPersistent); override; procedure ObtenerLista; override; public constructor Create(CodigoDocumento : String); overload; constructor Create; overload; override; property Items; end; ///////////////////////////////////////////////////////////////////////////////// TdmTablaDocumentos = class(TDataModule) private procedure IniciarSQL; public sqlInsertar : TStrings; sqlModificar : TStrings; sqlConsultar : TStrings; sqlEliminar : TStrings; sqlRefresh : TStrings; sqlDatosDocumento : TStrings; function DarDocumentos : TStrings; constructor Create (AOwner : TComponent); override; destructor Destroy; override; function DarEntidadDocumento(EntidadPadre: TRdxEntidad; Codigo : String): TRdxEntidad; function DarCodigoDocumento(Value : TRdxEntidad): String; function DarDescripcionDocumento(Codigo : String): String; function TienePropiedades (Codigo : String): Boolean; end; var dmTablaDocumentos: TdmTablaDocumentos; implementation {$R *.dfm} uses Literales; { TdmTablaDocumentos } constructor TdmTablaDocumentos.Create(AOwner: TComponent); begin inherited; sqlInsertar := TStringList.Create; sqlModificar := TStringList.Create; sqlConsultar := TStringList.Create; sqlEliminar := TStringList.Create; sqlRefresh := TStringList.Create; sqlDatosDocumento := TStringList.Create; IniciarSQL; end; function TdmTablaDocumentos.DarDocumentos: TStrings; var oSQL : TIBSQL; Lista : TStringList; begin oSQL := TIBSQL.Create(Self); Lista := TStringList.Create; with oSQL do begin Database := dmBaseDatos.BD; Transaction := dmBaseDatos.Transaccion; SQL.Add('select DESCRIPCION from DOCUMENTOS order by DESCRIPCION'); try Prepare; ExecQuery; while not EOF do begin Lista.Append(Fields[0].AsString); Next; end; result := Lista; finally Close; Transaction := NIL; Free; end; end; end; destructor TdmTablaDocumentos.Destroy; begin sqlInsertar.Free; sqlModificar.Free; sqlConsultar.Free; sqlEliminar.Free; sqlRefresh.Free; sqlDatosDocumento.Free; inherited; end; procedure TdmTablaDocumentos.IniciarSQL; begin with sqlInsertar do begin Add('insert into DOCUMENTOS (CODIGO, FAMILIA, FORMAPAGO, PLAZOENTREGA, NOTAS, CONTRATOVENTA, RECOMENDACIONES)'); Add('values (:CODIGO, :FAMILIA, :FORMAPAGO, :PLAZOENTREGA, :NOTAS, :CONTRATOVENTA, :RECOMENDACIONES)'); end; with sqlModificar do begin Add('update DOCUMENTOS set '); Add(' CODIGO = :CODIGO, '); Add(' FAMILIA = :FAMILIA, '); Add(' FORMAPAGO = :FORMAPAGO, '); Add(' PLAZOENTREGA = :PLAZOENTREGA, '); Add(' NOTAS = :NOTAS, '); Add(' CONTRATOVENTA = :CONTRATOVENTA, '); Add(' RECOMENDACIONES = :RECOMENDACIONES '); Add('where CODIGO = :CODIGO'); end; with sqlEliminar do begin Add('delete from DOCUMENTOS '); Add('where CODIGO = :CODIGO'); end; with sqlConsultar do begin Add('select * from DOCUMENTOS'); end; with sqlRefresh do begin Add('select * from DOCUMENTOS '); Add('where CODIGO = :CODIGO'); end; with sqlDatosDocumento do begin Add('select doc.codigo, doc.descripcion, doc.familia, fam.descripcion descfamilia,'); Add('doc.formapago, doc.plazoentrega, doc.notas, doc.contratoventa, doc.recomendaciones'); Add('from documentos doc left outer join familias fam'); Add('on (fam.codigo = doc.familia)'); end; end; function TdmTablaDocumentos.DarEntidadDocumento(EntidadPadre: TRdxEntidad; Codigo : String): TRdxEntidad; begin Codigo := UpperCase(Codigo); if Codigo = 'MCOCINA' then begin case EntidadPadre of entPresupuestoCliente : Result := entPresupuestoCocina; entContratoCliente : Result := entContratoCocina; entFacturaCliente : Result := entFacturaCocina; entAbonoCliente : Result := entAbonoCocina; else Result := EntidadPadre; end end; if Codigo = 'MBANO' then begin case EntidadPadre of entPresupuestoCliente : Result := entPresupuestoBano; entContratoCliente : Result := entContratoBano; entFacturaCliente : Result := entFacturaBano; entAbonoCliente : Result := entAbonoBano; else Result := EntidadPadre; end end; if Codigo = 'ARMARIOS' then begin case EntidadPadre of entPresupuestoCliente : Result := entPresupuestoArmarios; entContratoCliente : Result := entContratoArmarios; entFacturaCliente : Result := entFacturaArmarios; entAbonoCliente : Result := entAbonoArmarios; else Result := EntidadPadre; end end; if Codigo = 'ELECTRO' then begin case EntidadPadre of entPresupuestoCliente : Result := entPresupuestoElectro; entContratoCliente : Result := entContratoElectro; entFacturaCliente : Result := entFacturaElectro; entAbonoCliente : Result := entAbonoElectro; else Result := EntidadPadre; end end; if Codigo = 'VARIOS' then begin case EntidadPadre of entPresupuestoCliente : Result := entPresupuestoVarios; entContratoCliente : Result := entContratoVarios; entFacturaCliente : Result := entFacturaVarios; entAbonoCliente : Result := entAbonoVarios; else Result := EntidadPadre; end end; end; function TdmTablaDocumentos.DarCodigoDocumento(Value: TRdxEntidad): String; begin case Value of entPresupuestoCocina, entContratoCocina, entFacturaCocina, entAbonoCocina : Result := 'MCOCINA'; entPresupuestoBano, entContratoBano, entFacturaBano, entAbonoBano : Result := 'MBANO'; entPresupuestoArmarios, entContratoArmarios, entFacturaArmarios, entAbonoArmarios : Result := 'ARMARIOS'; entPresupuestoElectro, entContratoElectro, entFacturaElectro, entAbonoElectro : Result := 'ELECTRO'; entPresupuestoVarios, entContratoVarios, entFacturaVarios, entAbonoVarios : Result := 'VARIOS'; else Result := ''; end; end; function TdmTablaDocumentos.DarDescripcionDocumento( Codigo : String): String; var oSQL : TIBSQL; Lista : TStringList; begin oSQL := TIBSQL.Create(Self); Lista := TStringList.Create; with oSQL do begin Database := dmBaseDatos.BD; Transaction := dmBaseDatos.Transaccion; SQL.Add('select DESCRIPCION from DOCUMENTOS '); SQL.Add('where CODIGO = :CODIGO'); try ParamByName('CODIGO').AsString := Codigo; Prepare; ExecQuery; Result := Fields[0].AsString; finally Close; Transaction := NIL; Free; end; end; end; function TdmTablaDocumentos.TienePropiedades(Codigo: String): Boolean; begin if (Codigo = 'MCOCINA') or (Codigo = 'MBANO') or (Codigo = 'ARMARIOS') then Result := True else Result := False; end; { TDocumento } constructor TDocumento.Create(Codigo: String); begin inherited Create; Self.Codigo := Codigo; // Recuperar los datos del documento ObtenerDatos; // Recuperar la lista de propiedades del documento FListaPropiedades := TListaPropiedadesDocumento.Create(Self.Codigo); FListaPrecios := TListaPreciosDocumento.Create(Self.Codigo); end; destructor TDocumento.Destroy; begin FListaPropiedades.Free; FListaPropiedades := NIL; FListaPrecios.Free; FListaPrecios := NIL; inherited; end; procedure TDocumento.ObtenerDatos; var oSQL : TIBSQL; begin if EsCadenaVacia(Codigo) then Exit; oSQL := TIBSQL.Create(nil); with oSQL do begin Database := dmBaseDatos.BD; Transaction := dmBaseDatos.Transaccion; SQL.Add('select * from DOCUMENTOS '); SQL.Add('where CODIGO = :CODIGO'); ParamByName('CODIGO').AsString := Codigo; try Prepare; ExecQuery; if (RecordCount > 0) then begin Self.Codigo := FieldByName('CODIGO').AsString; Self.Descripcion := FieldByName('DESCRIPCION').AsString; Self.Familia := FieldByName('FAMILIA').AsVariant; Self.FormaPago := FieldByName('FORMAPAGO').AsString; Self.PlazoEntrega := FieldByName('PLAZOENTREGA').AsString; Self.Notas := FieldByName('NOTAS').AsString; Self.ContratoVenta := FieldByName('CONTRATOVENTA').AsString; Self.Recomendaciones := FieldByName('RECOMENDACIONES').AsString; end else raise Exception.CreateFmt(msgCliNoExisteTipoPre, [Codigo]) finally Close; Transaction := NIL; Free; end; end; end; { TListaPropiedadesArticulo } procedure TListaPropiedadesDocumento.AssignTo(Dest: TPersistent); begin inherited; // end; constructor TListaPropiedadesDocumento.Create(CodigoDocumento: String); begin inherited Create; FCodigoDocumento := CodigoDocumento; ObtenerLista; end; constructor TListaPropiedadesDocumento.Create; begin inherited Create; FCodigoDocumento := ''; end; procedure TListaPropiedadesDocumento.ObtenerLista; var oSQL : TIBSQL; Propiedad : TDatosPropiedad; begin oSQL := TIBSQL.Create(nil); with oSQL do begin Database := dmBaseDatos.BD; Transaction := dmBaseDatos.Transaccion; SQL.Add('select * from PROPIEDADESDOCUMENTOS'); SQL.Add('where CODIGODOCUMENTO = :CODIGODOCUMENTO'); SQL.Add('order by NUMPROPIEDAD'); try ParamByName('CODIGODOCUMENTO').AsString := FCodigoDocumento; Prepare; ExecQuery; while not EOF do begin Propiedad := TDatosPropiedad.Create; Propiedad.NumPropiedad := FieldByName('NUMPROPIEDAD').AsInteger; Propiedad.Seccion := FieldByName('SECCION').AsString; Propiedad.CodigoPropiedad := FieldByName('CODIGOPROPIEDAD').AsInteger; Propiedad.Valor := FieldByName('VALOR').AsString; FLista.Add(Propiedad); Next; end; finally Close; Transaction := NIL; Free; end; end; end; { TListaPreciosDocumento } constructor TListaPreciosDocumento.Create(CodigoDocumento: String); begin inherited Create; FCodigoDocumento := CodigoDocumento; ObtenerLista; end; constructor TListaPreciosDocumento.Create; begin inherited Create; FCodigoDocumento := ''; end; procedure TListaPreciosDocumento.ObtenerLista; var oSQL : TIBSQL; Precio : String; begin oSQL := TIBSQL.Create(nil); with oSQL do begin Database := dmBaseDatos.BD; Transaction := dmBaseDatos.Transaccion; SQL.Add('select DESCRIPCION from PRECIOSDOCUMENTOS'); SQL.Add('where CODIGODOCUMENTO = :CODIGODOCUMENTO'); SQL.Add('order by NUMPRECIO'); try ParamByName('CODIGODOCUMENTO').AsString := FCodigoDocumento; Prepare; ExecQuery; while not EOF do begin Add(FieldByName('DESCRIPCION').AsString); Next; end; finally Close; Transaction := NIL; Free; end; end; end; end.