2007-10-03 16:03:28 +00:00
unit uControllerDetallesBase;
interface
2007-10-22 17:55:10 +00:00
uses Classes, Variants, uDACDSDataTable, uDADataTable, uControllerBase, uCalculosUtils;
2007-10-03 16:03:28 +00:00
const
TIPO_DETALLE_CONCEPTO = 'Concepto' ;
TIPO_DETALLE_TITULO = 'Titulo' ;
2008-09-17 18:38:38 +00:00
TIPO_DETALLE_TITULO_OPCIONAL = 'Titulo opcional' ;
2007-10-03 16:03:28 +00:00
TIPO_DETALLE_SUBTOTAL = 'Subtotal' ;
TIPO_DETALLE_SALTO = 'Salto' ;
2007-12-20 20:32:57 +00:00
TIPO_DETALLE_DESCUENTO = 'Descuento' ;
2007-10-03 16:03:28 +00:00
CTE_DESC_SALTO = 'SALTO DE PAGINA >>' ;
type
TIntegerArray = array of Integer ;
IControllerDetallesBase = interface( ISujeto)
[ '{F0B0E714-EC0D-4B6B-98B1-76F72F70B735}' ]
2008-09-15 17:51:19 +00:00
function GetTipo( ADataTable: IDAStronglyTypedDataTable; pPosicion: Integer ) : String ;
2007-10-03 16:03:28 +00:00
procedure Clear( ADataTable: IDAStronglyTypedDataTable) ;
procedure Add( ADataTable: IDAStronglyTypedDataTable; TipoConcepto: Variant ) ;
procedure Delete( ADataTable: IDAStronglyTypedDataTable; Posicion: TIntegerArray) ;
procedure Move( ADataTable: IDAStronglyTypedDataTable; Posicion: TIntegerArray; Posiciones: Integer ) ;
2008-05-26 14:57:35 +00:00
procedure BeginUpdate( ADataTable: IDAStronglyTypedDataTable) ;
procedure EndUpdate( ADataTable: IDAStronglyTypedDataTable) ;
2007-10-03 16:03:28 +00:00
// procedure Copy(SMExport: TSMExportToClipboard);
// procedure Paste;
procedure ActualizarTotales( ADataTable: IDAStronglyTypedDataTable) ;
function DarTotalImporteTotal( ADataTable: IDAStronglyTypedDataTable) : Double ;
2007-12-20 20:32:57 +00:00
function LocalizarPosicion( ADataTable: IDAStronglyTypedDataTable; const APosicion: Integer ) : Boolean ;
2008-05-30 16:56:23 +00:00
procedure Renumerar( DataTable: TDADataTable; LocalizaPosicion: Integer ) ;
2007-10-03 16:03:28 +00:00
function DarListaTiposDetalle: TStringList;
2008-09-17 18:38:38 +00:00
procedure ValidarCierreCapitulos( ADataTable: IDAStronglyTypedDataTable) ;
2007-10-03 16:03:28 +00:00
end ;
TControllerDetallesBase = class ( TSujeto, IControllerDetallesBase)
private
fUpdateCount: Integer ;
function CalcularTotales( Modificar: boolean ; DataTable: TDADataTable) : Double ;
protected
procedure Renumerar( DataTable: TDADataTable; LocalizaPosicion: Integer ) ;
function DesplazarNPosiciones( DataTable: TDADataTable; NumOrdenIni: Variant ; NPosiciones: Variant ) : Integer ;
procedure Mover( DataTable: TDADataTable; Posicion: Integer ; NumPosiciones: Integer ) ;
//Si en los hijos existen campos a tener en cuenta se sobreescribira este metodo
2008-05-20 18:50:02 +00:00
procedure ValidarCampos( DataTable: TDADataTable) ; virtual ;
2007-10-03 16:03:28 +00:00
//Si sobreescribimos este m<> todo podremos tener en cuenta otras columnas para el calculo del importe total de un concepto
function CalcularImporteTotalConcepto( DataTable: TDADataTable) : Double ; virtual ;
procedure TratamientoDetalleConcepto( DataTable: TDADataTable) ; virtual ;
2008-09-17 18:38:38 +00:00
procedure CalculoDetalleConcepto( DataTable: TDADataTable; var ImporteAcumulado : Double ; var ImporteTotal : Double ; const Opcional: Boolean ) ; virtual ;
2007-10-03 16:03:28 +00:00
procedure TratamientoDetalleSalto( DataTable: TDADataTable) ; virtual ;
procedure CalculoDetalleSalto( DataTable: TDADataTable; var ImporteAcumulado : Double ; var ImporteTotal : Double ) ; virtual ;
procedure TratamientoDetalleTitulo( DataTable: TDADataTable) ; virtual ;
procedure CalculoDetalleTitulo( DataTable: TDADataTable; var ImporteAcumulado : Double ; var ImporteTotal : Double ) ; virtual ;
2008-09-17 18:38:38 +00:00
procedure TratamientoDetalleTituloOpcional( DataTable: TDADataTable) ; virtual ;
procedure CalculoDetalleTituloOpcional( DataTable: TDADataTable; var ImporteAcumulado : Double ; var ImporteTotal : Double ) ; virtual ;
2007-10-03 16:03:28 +00:00
procedure TratamientoDetalleSubtotal( DataTable: TDADataTable) ; virtual ;
procedure CalculoDetalleSubtotal( DataTable: TDADataTable; var ImporteAcumulado : Double ; var ImporteTotal : Double ) ; virtual ;
2007-12-16 22:29:19 +00:00
procedure TratamientoDetalleDescuento( DataTable: TDADataTable) ; virtual ;
procedure CalculoDetalleDescuento( DataTable: TDADataTable; var ImporteAcumulado : Double ; var ImporteTotal : Double ) ; virtual ;
2007-10-03 16:03:28 +00:00
//Si sobreescribimos este metodo es para continuar el CalcularTotales segun los tipos de concepto de los hijos
function CalcularTotalesHijos( Modificar: boolean ; DataTable: TDADataTable; var ImporteAcumulado : Double ; var ImporteTotal : Double ) : Double ; virtual ;
public
constructor Create; override ;
destructor Destroy; override ;
2008-09-15 17:51:19 +00:00
function GetTipo( ADataTable: IDAStronglyTypedDataTable; pPosicion: Integer ) : String ;
2007-10-03 16:03:28 +00:00
procedure Clear( ADataTable: IDAStronglyTypedDataTable) ;
procedure Add( ADataTable: IDAStronglyTypedDataTable; TipoConcepto: Variant ) ; virtual ;
procedure Delete( ADataTable: IDAStronglyTypedDataTable; Posicion: TIntegerArray) ; virtual ;
procedure Move( ADataTable: IDAStronglyTypedDataTable; Posicion: TIntegerArray; Posiciones: Integer ) ; virtual ;
2008-05-26 14:57:35 +00:00
procedure BeginUpdate( ADataTable: IDAStronglyTypedDataTable) ;
procedure EndUpdate( ADataTable: IDAStronglyTypedDataTable) ;
2007-10-03 16:03:28 +00:00
// procedure Copy(SMExport: TSMExportToClipboard);
// procedure Paste;
procedure ActualizarTotales( ADataTable: IDAStronglyTypedDataTable) ;
function DarTotalImporteTotal( ADataTable: IDAStronglyTypedDataTable) : Double ;
function DarListaTiposDetalle: TStringList; virtual ;
2008-05-26 14:57:35 +00:00
function LocalizarPosicion( ADataTable: IDAStronglyTypedDataTable; const APosicion: Integer ) : Boolean ;
2008-09-15 17:51:19 +00:00
// Comprueba que todos los capitulos tengan su subtotal de cierre
procedure ValidarCierreCapitulos( ADataTable: IDAStronglyTypedDataTable) ;
2007-10-03 16:03:28 +00:00
end ;
implementation
{ TControllerDetallesBase }
2007-12-20 20:32:57 +00:00
uses
cxControls, SysUtils, DB, uDAInterfaces, Dialogs;
2007-10-03 16:03:28 +00:00
procedure TControllerDetallesBase. ActualizarTotales( ADataTable: IDAStronglyTypedDataTable) ;
begin
BeginUpdate( ADataTable) ;
try
CalcularTotales( True , ADataTable. DataTable) ;
finally
EndUpdate( ADataTable) ;
end ;
end ;
procedure TControllerDetallesBase. Add( ADataTable: IDAStronglyTypedDataTable; TipoConcepto: Variant ) ;
var
AuxNumOrden : Integer ;
begin
BeginUpdate( ADataTable) ;
try
with ADataTable do
begin
AuxNumOrden : = desplazarNPosiciones( DataTable, DataTable. FieldByName( CAMPO_POSICION) . AsVariant, 1 ) ;
DataTable. Insert;
DataTable. FieldByName( CAMPO_POSICION) . AsInteger : = AuxNumOrden;
DataTable. FieldByName( CAMPO_TIPO) . AsVariant : = TipoConcepto;
2008-02-06 19:16:50 +00:00
DataTable. Post;
2007-10-03 16:03:28 +00:00
end ;
finally
EndUpdate( ADataTable) ;
end ;
end ;
procedure TControllerDetallesBase. BeginUpdate( ADataTable: IDAStronglyTypedDataTable) ;
begin
ShowHourglassCursor;
Inc( fUpdateCount) ;
ADataTable. DataTable. DisableControls;
end ;
function TControllerDetallesBase. CalcularImporteTotalConcepto( DataTable: TDADataTable) : Double ;
begin
with DataTable do
Result : = FieldByName( CAMPO_CANTIDAD) . asInteger * FieldByName( CAMPO_IMPORTE_UNIDAD) . AsFloat;
end ;
function TControllerDetallesBase. CalcularTotales( Modificar: boolean ; DataTable: TDADataTable) : Double ;
{
funcion que recalcula todos los detalles de la tabla pasada por parametro y devuelve
la cantidad total de los mismos
}
var
AuxPosicionIni : Integer ;
AuxPosicion : Integer ;
AuxImporteAcumulado : Double ;
AuxImporteTotal : Double ;
2008-09-17 18:38:38 +00:00
SemaforoOpcional: Boolean ;
2007-10-03 16:03:28 +00:00
begin
if ( DataTable. State in dsEditModes) then
DataTable. Post;
ValidarCampos( DataTable) ;
DataTable. DisableControls;
2007-12-20 20:32:57 +00:00
2007-10-03 16:03:28 +00:00
AuxPosicionIni : = DataTable. FieldByName( CAMPO_POSICION) . AsInteger;
AuxPosicion : = 0 ;
AuxImporteAcumulado : = 0 ;
AuxImporteTotal : = 0 ;
2008-09-17 18:38:38 +00:00
SemaforoOpcional : = False ;
2007-10-03 16:03:28 +00:00
try
DataTable. First;
2007-12-20 20:32:57 +00:00
while DataTable. Locate( CAMPO_POSICION, AuxPosicion, [ ] ) do
2007-10-03 16:03:28 +00:00
begin
//SALTOS DE LINEA
if ( DataTable. FieldByName( CAMPO_TIPO) . AsString = TIPO_DETALLE_SALTO) then
begin
if Modificar then
TratamientoDetalleSalto( DataTable) ; //Se podr<64> sobreescribir para que se tengan en cuenta nuevos campos en hijos
CalculoDetalleSalto( DataTable, AuxImporteAcumulado, AuxImporteTotal) ; //Se podr<64> sobreescribir para posibles nuevos calculos de los hijos
end
//TITULOS
else if ( DataTable. FieldByName( CAMPO_TIPO) . AsString = TIPO_DETALLE_TITULO) then
begin
2008-09-17 18:38:38 +00:00
SemaforoOpcional : = False ;
2007-10-03 16:03:28 +00:00
if Modificar then
TratamientoDetalleTitulo( DataTable) ; //Se podr<64> sobreescribir para que se tengan en cuenta nuevos campos en hijos
CalculoDetalleTitulo( DataTable, AuxImporteAcumulado, AuxImporteTotal) ; //Se podr<64> sobreescribir para posibles nuevos calculos de los hijos
end
2008-09-17 18:38:38 +00:00
//TITULOS OPCIONALES
else if ( DataTable. FieldByName( CAMPO_TIPO) . AsString = TIPO_DETALLE_TITULO_OPCIONAL) then
begin
SemaforoOpcional : = True ;
if Modificar then
TratamientoDetalleTituloOpcional( DataTable) ; //Se podr<64> sobreescribir para que se tengan en cuenta nuevos campos en hijos
CalculoDetalleTituloOpcional( DataTable, AuxImporteAcumulado, AuxImporteTotal) ; //Se podr<64> sobreescribir para posibles nuevos calculos de los hijos
end
2007-12-16 22:29:19 +00:00
//SUBTOTAL
2007-10-03 16:03:28 +00:00
else if ( DataTable. FieldByName( CAMPO_TIPO) . AsString = TIPO_DETALLE_SUBTOTAL) then
begin
if Modificar then
TratamientoDetalleSubtotal( DataTable) ; //Se podr<64> sobreescribir para que se tengan en cuenta nuevos campos
CalculoDetalleSubtotal( DataTable, AuxImporteAcumulado, AuxImporteTotal) ; //Se podr<64> sobreescribir para posibles nuevos calculos de los hijos
2008-09-17 18:38:38 +00:00
SemaforoOpcional : = False ;
2007-10-03 16:03:28 +00:00
end
2007-12-16 22:29:19 +00:00
//DESCUENTO DE CAPITULO
else if ( DataTable. FieldByName( CAMPO_TIPO) . AsString = TIPO_DETALLE_DESCUENTO) then
begin
if Modificar then
TratamientoDetalleDescuento( DataTable) ; //Se podr<64> sobreescribir para que se tengan en cuenta nuevos campos
CalculoDetalleDescuento( DataTable, AuxImporteAcumulado, AuxImporteTotal) ; //Se podr<64> sobreescribir para posibles nuevos calculos de los hijos
end
2007-10-03 16:03:28 +00:00
//CONCEPTOS
else if ( DataTable. FieldByName( CAMPO_TIPO) . AsString = TIPO_DETALLE_CONCEPTO) then
begin
if Modificar then
TratamientoDetalleConcepto( DataTable) ; //Se podr<64> sobreescribir para que se tengan en cuenta nuevos campos
2008-09-17 18:38:38 +00:00
CalculoDetalleConcepto( DataTable, AuxImporteAcumulado, AuxImporteTotal, SemaforoOpcional) ; //Se podr<64> sobreescribir para posibles nuevos calculos de los hijos
2008-09-15 17:51:19 +00:00
end
2007-10-03 16:03:28 +00:00
//HIJOS
else CalcularTotalesHijos( Modificar, DataTable, AuxImporteAcumulado, AuxImporteTotal) ;
Inc( AuxPosicion) ;
DataTable. First;
end ;
finally
//Dejamos el puntero en la misma posici<63> n que la que parti<74>
2007-12-20 20:32:57 +00:00
DataTable. Locate( CAMPO_POSICION, AuxPosicionIni, [ ] ) ;
2007-10-03 16:03:28 +00:00
DataTable. EnableControls;
end ;
Result : = AuxImporteTotal;
end ;
function TControllerDetallesBase. CalcularTotalesHijos( Modificar: boolean ; DataTable: TDADataTable; var ImporteAcumulado : Double ; var ImporteTotal : Double ) : Double ;
begin
//
Result : = 0 ;
end ;
2008-09-17 18:38:38 +00:00
procedure TControllerDetallesBase. CalculoDetalleConcepto( DataTable: TDADataTable; var ImporteAcumulado, ImporteTotal: Double ; const Opcional: Boolean ) ;
2007-10-03 16:03:28 +00:00
begin
with DataTable do
begin
ImporteAcumulado : = ImporteAcumulado + FieldByName( CAMPO_IMPORTE_TOTAL) . AsFloat;
2008-09-17 18:38:38 +00:00
if not Opcional then
ImporteTotal : = ImporteTotal + FieldByName( CAMPO_IMPORTE_TOTAL) . AsFloat;
2007-10-03 16:03:28 +00:00
end ;
end ;
2007-12-16 22:29:19 +00:00
procedure TControllerDetallesBase. CalculoDetalleDescuento(
DataTable: TDADataTable; var ImporteAcumulado, ImporteTotal: Double ) ;
var
ImporteDto : Double ;
begin
with DataTable do
begin
if not Editing then Edit;
ImporteDto : = ( - 1 ) * ( ( ImporteTotal * DataTable. FieldByName( CAMPO_DESCUENTO) . AsFloat) / 1 0 0 ) ;
2008-09-15 17:51:19 +00:00
ImporteTotal : = ImporteTotal + ImporteDto;
2007-12-16 22:29:19 +00:00
FieldByName( CAMPO_IMPORTE_TOTAL) . AsFloat : = ImporteTotal;
Post;
end ;
end ;
2007-10-03 16:03:28 +00:00
procedure TControllerDetallesBase. CalculoDetalleSalto( DataTable: TDADataTable; var ImporteAcumulado, ImporteTotal: Double ) ;
begin
with DataTable do
begin
if not Editing then Edit;
FieldByName( CAMPO_CANTIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_UNIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_TOTAL) . AsVariant : = Null;
Post;
end ;
end ;
procedure TControllerDetallesBase. CalculoDetalleSubtotal( DataTable: TDADataTable; var ImporteAcumulado, ImporteTotal: Double ) ;
begin
with DataTable do
begin
if not Editing then Edit;
FieldByName( CAMPO_IMPORTE_TOTAL) . AsFloat : = ImporteAcumulado;
Post;
end ;
ImporteAcumulado : = 0 ;
end ;
procedure TControllerDetallesBase. CalculoDetalleTitulo( DataTable: TDADataTable; var ImporteAcumulado, ImporteTotal: Double ) ;
begin
//
end ;
2008-09-17 18:38:38 +00:00
procedure TControllerDetallesBase. CalculoDetalleTituloOpcional(
DataTable: TDADataTable; var ImporteAcumulado, ImporteTotal: Double ) ;
begin
//
end ;
2007-10-03 16:03:28 +00:00
procedure TControllerDetallesBase. Clear( ADataTable: IDAStronglyTypedDataTable) ;
begin
//
end ;
constructor TControllerDetallesBase. Create;
begin
inherited ;
end ;
function TControllerDetallesBase. DarListaTiposDetalle: TStringList;
begin
Result : = TStringList. Create;
2007-12-20 20:32:57 +00:00
Result . Values[ TIPO_DETALLE_CONCEPTO] : = TIPO_DETALLE_CONCEPTO;
Result . Values[ TIPO_DETALLE_TITULO] : = TIPO_DETALLE_TITULO;
2008-09-17 18:38:38 +00:00
Result . Values[ TIPO_DETALLE_TITULO_OPCIONAL] : = TIPO_DETALLE_TITULO_OPCIONAL;
2007-12-20 20:32:57 +00:00
Result . Values[ TIPO_DETALLE_SUBTOTAL] : = TIPO_DETALLE_SUBTOTAL;
2008-09-17 18:38:38 +00:00
// Result.Values[TIPO_DETALLE_DESCUENTO] := TIPO_DETALLE_DESCUENTO;
// Result.Values[TIPO_DETALLE_SALTO] := TIPO_DETALLE_SALTO;
2007-10-03 16:03:28 +00:00
end ;
2007-12-20 20:32:57 +00:00
function TControllerDetallesBase. DarTotalImporteTotal( ADataTable: IDAStronglyTypedDataTable) : Double ;
2007-10-03 16:03:28 +00:00
begin
Result : = CalcularTotales( False , ADataTable. DataTable) ;
end ;
procedure TControllerDetallesBase. Delete( ADataTable: IDAStronglyTypedDataTable; Posicion: TIntegerArray) ;
var
i: integer ;
AField: TDAField;
DeletePosicion: Integer ;
begin
2007-10-22 17:55:10 +00:00
DeletePosicion : = 0 ;
2007-10-03 16:03:28 +00:00
AField : = ADataTable. DataTable. FindField( CAMPO_POSICION) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_POSICION + ' no encontrado (Delete)' ) ;
BeginUpdate( ADataTable) ;
try
with ADataTable do
begin
for i : = 0 to High( POSICION) do
begin
DataTable. First;
DeletePosicion : = POSICION[ i] ;
2007-12-20 20:32:57 +00:00
if DataTable. Locate( CAMPO_POSICION, DeletePosicion, [ ] ) then
2007-10-03 16:03:28 +00:00
DataTable. Delete;
end ;
Renumerar( DataTable, DeletePosicion) ;
end ;
finally
EndUpdate( ADataTable) ;
end ;
end ;
function TControllerDetallesBase. DesplazarNPosiciones( DataTable: TDADataTable; NumOrdenIni: Variant ; NPosiciones: Variant ) : Integer ;
{
Funci<EFBFBD> n que desplaza NPosiciones el numero de orden a partir del elemento con el
n<EFBFBD> mero de orden dado. Devuelve el numero de orden del primer elemento del hueco
generado
}
var
AuxNumOrden: Integer ;
AuxNumPos: Integer ;
AField: TDAField;
begin
AField : = DataTable. FindField( CAMPO_POSICION) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_POSICION + ' no encontrado (desplazarNPosiciones)' ) ;
if VarIsNull( NPosiciones)
then AuxNumPos : = 1
else AuxNumPos : = NPosiciones;
if VarIsNull( NumOrdenIni)
then AuxNumOrden : = 0
else AuxNumOrden : = NumOrdenIni + 1 ; //A<> adimos por abajo siempre
Result : = AuxNumOrden;
with DataTable do
begin
First;
while not EOF do
begin
if ( FieldByName( CAMPO_POSICION) . AsInteger > = AuxNumOrden) then
begin
if not Editing then Edit;
FieldByName( CAMPO_POSICION) . AsInteger : = FieldByName( CAMPO_POSICION) . AsInteger + AuxNumPos;
Post;
end ;
Next;
end ;
end ;
end ;
destructor TControllerDetallesBase. Destroy;
begin
inherited ;
end ;
procedure TControllerDetallesBase. EndUpdate( ADataTable: IDAStronglyTypedDataTable) ;
begin
Dec( fUpdateCount) ;
if fUpdateCount = 0 then
2008-05-26 14:57:35 +00:00
begin
CalcularTotales( True , ADataTable. DataTable) ;
2007-10-03 16:03:28 +00:00
AvisarObservadores( ADataTable) ;
2008-05-26 14:57:35 +00:00
end ;
2007-10-03 16:03:28 +00:00
2008-05-26 14:57:35 +00:00
ADataTable. DataTable. EnableControls;
2007-10-03 16:03:28 +00:00
HideHourglassCursor;
end ;
function TControllerDetallesBase. getTipo( ADataTable: IDAStronglyTypedDataTable; pPosicion: Integer ) : String ;
var
posIni: integer ;
AField: TDAField;
begin
AField : = ADataTable. DataTable. FindField( CAMPO_POSICION) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_POSICION + ' no encontrado (getTipo)' ) ;
Result : = '' ;
BeginUpdate( ADataTable) ;
try
with ADataTable do
begin
//Guardamos la posicion en la que estamos
posIni : = DataTable. FieldByName( CAMPO_POSICION) . AsInteger;
DataTable. First;
2007-12-20 20:32:57 +00:00
if DataTable. Locate( CAMPO_POSICION, pPosicion, [ ] ) then
2007-10-03 16:03:28 +00:00
Result : = DataTable. FieldByName( CAMPO_TIPO) . AsString;
//Volvemos a posicionar el puntero donde estaba
DataTable. First;
2007-12-20 20:32:57 +00:00
if not DataTable. Locate( CAMPO_POSICION, posIni, [ ] ) then
2007-10-03 16:03:28 +00:00
raise Exception. Create( 'La posici<63> n ' + IntToStr( posIni) + ' no existe (getTipo)' ) ;
end ;
finally
EndUpdate( ADataTable) ;
end ;
end ;
2007-12-20 20:32:57 +00:00
function TControllerDetallesBase. LocalizarPosicion(
ADataTable: IDAStronglyTypedDataTable; const APosicion: Integer ) : Boolean ;
var
AField: TDAField;
begin
AField : = ADataTable. DataTable. FindField( CAMPO_POSICION) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_POSICION + ' no encontrado (LocalizarPosicion)' ) ;
with ADataTable. DataTable do
begin
DisableControls;
DisableEventHandlers;
ShowHourglassCursor;
try
First;
Result : = Locate( CAMPO_POSICION, APosicion, [ ] ) ;
finally
HideHourglassCursor;
EnableEventHandlers;
EnableControls;
end ;
end ;
end ;
2007-10-03 16:03:28 +00:00
procedure TControllerDetallesBase. Move( ADataTable: IDAStronglyTypedDataTable; Posicion: TIntegerArray; Posiciones: Integer ) ;
var
i: Integer ;
begin
BeginUpdate( ADataTable) ;
try
with ADataTable do
begin
//Empezamos desde abajo
if Posiciones > 0 then
for i: = High( POSICION) downto 0 do
Mover( DataTable, POSICION[ i] , Posiciones)
else
//Empezamos desde arriba
for i: = 0 to High( POSICION) do
Mover( DataTable, POSICION[ i] , Posiciones) ;
end ;
finally
EndUpdate( ADataTable) ;
end ;
end ;
procedure TControllerDetallesBase. Mover( DataTable: TDADataTable; Posicion: Integer ; NumPosiciones: Integer ) ;
{
procedimiento que desplaza el n<EFBFBD> mero de posiciones ( NumPosiciones) pasados por parametro
a la posicion ( Posicion) dada, en caso de ser negativo ser<EFBFBD> hacia arriba y positivo hacia
abajo
}
var
AuxOrden : Integer ;
AuxID : Integer ;
AField: TDAField;
begin
AField : = DataTable. FindField( CAMPO_POSICION) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_POSICION + ' no encontrado (mover)' ) ;
AField : = DataTable. FindField( CAMPO_ID) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_ID + ' no encontrado (mover)' ) ;
//Buscamos el elemento con la posicion pasada por parametro
DataTable. First;
2007-12-20 20:32:57 +00:00
if not DataTable. Locate( CAMPO_POSICION, Posicion, [ ] ) then
2007-10-03 16:03:28 +00:00
raise Exception. Create( 'Error, no se ha encontrado la POSICION [' + IntToStr( Posicion) + '] (mover)' ) ;
//Guardamos el id del elemento a cambiar de posicion y calculamos su nueva posicion
AuxID : = DataTable. FieldByName( CAMPO_ID) . AsInteger;
AuxOrden : = Posicion + NumPosiciones;
DataTable. First;
2007-12-20 20:32:57 +00:00
if DataTable. Locate( CAMPO_POSICION, AuxOrden, [ ] ) then
2007-10-03 16:03:28 +00:00
begin
if not DataTable. Editing then DataTable. Edit;
DataTable. FieldByName( CAMPO_POSICION) . AsInteger : = DataTable. FieldByName( CAMPO_POSICION) . AsInteger - NumPosiciones;
//Se hace dentro por si es el ultimo o el primero
DataTable. First;
2007-12-20 20:32:57 +00:00
if not DataTable. Locate( CAMPO_ID, AuxID, [ ] ) then
2007-10-03 16:03:28 +00:00
raise Exception. Create( 'Error, no se ha encontrado el ID [' + IntToStr( AuxID) + '] (mover)' ) ;
if not DataTable. Editing then DataTable. Edit;
DataTable. FieldByName( CAMPO_POSICION) . AsInteger : = AuxOrden;
DataTable. Post;
end ;
//Colocamos el puntero en la posici<63> n en la que estaba
DataTable. First;
2007-12-20 20:32:57 +00:00
DataTable. Locate( CAMPO_ID, AuxID, [ ] ) ;
2007-10-03 16:03:28 +00:00
end ;
procedure TControllerDetallesBase. Renumerar( DataTable: TDADataTable; LocalizaPosicion: Integer ) ;
{
procedimiento que renumera todos los conceptos de la tabla dada por parametro
}
var
i, j : Integer ;
AField: TDAField;
begin
AField : = DataTable. FindField( CAMPO_POSICION) ;
2007-12-20 20:32:57 +00:00
2007-10-03 16:03:28 +00:00
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_POSICION + ' no encontrado (renumerar)' ) ;
with DataTable do
begin
for i: = 0 to RecordCount- 1 do
begin
First;
2007-12-20 20:32:57 +00:00
if not Locate( CAMPO_POSICION, i, [ ] ) then
2007-10-03 16:03:28 +00:00
begin
j : = i;
First;
2007-12-20 20:32:57 +00:00
while not Locate( CAMPO_POSICION, j, [ ] ) do
2007-10-03 16:03:28 +00:00
begin
Inc( j) ;
First;
end ;
if not Editing then Edit;
FieldByName( CAMPO_POSICION) . AsInteger : = i;
Post;
end ;
end ;
//Posicionamos el puntero en la posici<63> n dada por parametro
2007-12-20 20:32:57 +00:00
Locate( CAMPO_POSICION, LocalizaPosicion, [ ] ) ;
2007-10-03 16:03:28 +00:00
end ;
end ;
procedure TControllerDetallesBase. TratamientoDetalleConcepto( DataTable: TDADataTable) ;
begin
with DataTable do
begin
if not Editing then Edit;
//Si alguno de los campos de calculo de total es nulo el total tambien ser<65> nulo
if ( VarIsNull( FieldByName( CAMPO_CANTIDAD) . AsVariant)
or VarIsNull( FieldByName( CAMPO_IMPORTE_UNIDAD) . AsVariant) )
then FieldByName( CAMPO_IMPORTE_TOTAL) . AsVariant : = Null
else FieldByName( CAMPO_IMPORTE_TOTAL) . AsFloat : = CalcularImporteTotalConcepto( DataTable) ;
Post;
end ;
end ;
2007-12-16 22:29:19 +00:00
procedure TControllerDetallesBase. TratamientoDetalleDescuento(
DataTable: TDADataTable) ;
begin
with DataTable do
begin
if not Editing then Edit;
if ( FieldByName( CAMPO_CONCEPTO) . AsString = CTE_DESC_SALTO) then
FieldByName( CAMPO_CONCEPTO) . AsVariant : = Null;
FieldByName( CAMPO_CANTIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_UNIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_TOTAL) . AsVariant : = Null;
Post;
end ;
end ;
2008-09-17 18:38:38 +00:00
procedure TControllerDetallesBase. TratamientoDetalleSalto( DataTable: TDADataTable) ;
2008-09-15 17:51:19 +00:00
begin
with DataTable do
begin
if not Editing then Edit;
2008-09-17 18:38:38 +00:00
FieldByName( CAMPO_CONCEPTO) . AsString : = CTE_DESC_SALTO;
FieldByName( CAMPO_CANTIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_UNIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_TOTAL) . AsVariant : = Null;
2008-09-15 17:51:19 +00:00
Post;
end ;
end ;
2008-09-17 18:38:38 +00:00
procedure TControllerDetallesBase. TratamientoDetalleSubtotal( DataTable: TDADataTable) ;
2007-10-03 16:03:28 +00:00
begin
with DataTable do
begin
if not Editing then Edit;
2008-09-17 18:38:38 +00:00
if ( FieldByName( CAMPO_CONCEPTO) . AsString = CTE_DESC_SALTO) then
FieldByName( CAMPO_CONCEPTO) . AsVariant : = Null;
2007-10-03 16:03:28 +00:00
FieldByName( CAMPO_CANTIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_UNIDAD) . AsVariant : = Null;
Post;
end ;
end ;
2008-09-17 18:38:38 +00:00
procedure TControllerDetallesBase. TratamientoDetalleTitulo( DataTable: TDADataTable) ;
2007-10-03 16:03:28 +00:00
begin
with DataTable do
begin
if not Editing then Edit;
if ( FieldByName( CAMPO_CONCEPTO) . AsString = CTE_DESC_SALTO) then
FieldByName( CAMPO_CONCEPTO) . AsVariant : = Null;
FieldByName( CAMPO_CANTIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_UNIDAD) . AsVariant : = Null;
2008-09-17 18:38:38 +00:00
FieldByName( CAMPO_IMPORTE_TOTAL) . AsVariant : = Null;
2007-10-03 16:03:28 +00:00
Post;
end ;
end ;
2008-09-17 18:38:38 +00:00
procedure TControllerDetallesBase. TratamientoDetalleTituloOpcional( DataTable: TDADataTable) ;
2007-10-03 16:03:28 +00:00
begin
with DataTable do
begin
if not Editing then Edit;
if ( FieldByName( CAMPO_CONCEPTO) . AsString = CTE_DESC_SALTO) then
FieldByName( CAMPO_CONCEPTO) . AsVariant : = Null;
FieldByName( CAMPO_CANTIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_UNIDAD) . AsVariant : = Null;
FieldByName( CAMPO_IMPORTE_TOTAL) . AsVariant : = Null;
Post;
end ;
end ;
2008-09-15 17:51:19 +00:00
procedure TControllerDetallesBase. ValidarCampos( DataTable: TDADataTable) ;
2007-10-03 16:03:28 +00:00
var
AField: TDAField;
begin
//Validamos la existencia de todos los campos necesarios
AField : = DataTable. FindField( CAMPO_POSICION) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_POSICION + ' no encontrado (validarCampos)' ) ;
AField : = DataTable. FindField( CAMPO_TIPO) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_TIPO + ' no encontrado (validarCampos)' ) ;
AField : = DataTable. FindField( CAMPO_CANTIDAD) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_CANTIDAD + ' no encontrado (validarCampos)' ) ;
AField : = DataTable. FindField( CAMPO_IMPORTE_UNIDAD) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_IMPORTE_UNIDAD + ' no encontrado (validarCampos)' ) ;
AField : = DataTable. FindField( CAMPO_IMPORTE_TOTAL) ;
if not Assigned( AField) then
raise Exception. Create( 'Campo ' + CAMPO_IMPORTE_TOTAL + ' no encontrado (validarCampos)' ) ;
end ;
2008-09-15 17:51:19 +00:00
procedure TControllerDetallesBase. ValidarCierreCapitulos(
ADataTable: IDAStronglyTypedDataTable) ;
var
ATipoCampo : String ;
bEnCapitulo : Boolean ;
AuxPosicionIni : Integer ;
AuxPosicion : Integer ;
begin
bEnCapitulo : = False ;
ATipoCampo : = '' ;
if ( ADataTable. DataTable. State in dsEditModes) then
ADataTable. DataTable. Post;
ValidarCampos( ADataTable. DataTable) ;
AuxPosicionIni : = ADataTable. DataTable. FieldByName( CAMPO_POSICION) . AsInteger;
AuxPosicion : = 0 ;
BeginUpdate( ADataTable) ;
try
ADataTable. DataTable. First;
while ADataTable. DataTable. Locate( CAMPO_POSICION, AuxPosicion, [ ] ) do
begin
ATipoCampo : = ADataTable. DataTable. FieldByName( CAMPO_TIPO) . AsString;
if ( ATipoCampo = TIPO_DETALLE_SUBTOTAL) and bEnCapitulo then
bEnCapitulo : = False ;
2008-09-17 18:38:38 +00:00
if ( ( ATipoCampo = TIPO_DETALLE_TITULO) or ( ATipoCampo = TIPO_DETALLE_TITULO_OPCIONAL) ) then
2008-09-15 17:51:19 +00:00
begin
if bEnCapitulo then
begin
ADataTable. DataTable. First;
if ADataTable. DataTable. Locate( CAMPO_POSICION, AuxPosicion- 1 , [ ] ) then
begin
Add( ADataTable, TIPO_DETALLE_SUBTOTAL) ;
bEnCapitulo : = False ;
end ;
end
else
bEnCapitulo : = True ;
end ;
Inc( AuxPosicion) ;
ADataTable. DataTable. First;
end ;
// <20> Se ha acabado la tabla y queda alg<6C> n cap<61> tulo sin cerrar?
if bEnCapitulo then
if ADataTable. DataTable. Locate( CAMPO_POSICION, AuxPosicion- 1 , [ ] ) then
begin
Add( ADataTable, TIPO_DETALLE_SUBTOTAL) ;
bEnCapitulo : = False ;
end ;
finally
//Dejamos el puntero en la misma posici<63> n que la que parti<74>
ADataTable. DataTable. Locate( CAMPO_POSICION, AuxPosicionIni, [ ] ) ;
EndUpdate( ADataTable) ;
end ;
end ;
2007-10-03 16:03:28 +00:00
end .