unit uBizFacturasClienteServer; interface uses uDAInterfaces, uDADelta, uDABusinessProcessor, schFacturasClienteServer_Intf; const BIZ_SERVER_FACTURAS_CLIENTE = 'Server.FacturasCliente'; type TBizFacturasClienteServer = class(TFacturasClienteBusinessProcessorRules) private FReferenciaAutomatica : Boolean; function DarReferencia : String; function IncrementarReferencia : Boolean; protected procedure Insert_Asiento_Factura(aChange: TDADeltaChange); virtual; procedure Update_Asiento_Factura(aChange: TDADeltaChange); virtual; procedure Delete_Asiento_Factura(aChange: TDADeltaChange); virtual; procedure BeforeProcessDelta(Sender: TDABusinessProcessor; const aDelta: IDADelta); override; procedure AfterProcessChange(Sender: TDABusinessProcessor; aChange: TDADeltaChange; Processed: Boolean; var CanRemoveFromDelta: Boolean); override; end; implementation uses Variants, uDAClasses, uReferenciasUtils, uBusinessUtils, uROClasses, uDataModuleServer, schFacturasClienteClient_Intf, FactuGES_Intf, uROServer, SysUtils; const REF_FACTURAS_CLIENTE = 'REF_FACTURAS_CLIENTE'; REF_ABONOS_CLIENTE = 'REF_ABONOS_CLIENTE'; CTE_TIPO_ABONO = 'A'; CTE_TIPO_FACTURA = 'F'; { TBizFacturasClienteServer } procedure TBizFacturasClienteServer.AfterProcessChange(Sender: TDABusinessProcessor; aChange: TDADeltaChange; Processed: Boolean; var CanRemoveFromDelta: Boolean); begin case aChange.ChangeType of ctInsert: begin Insert_Asiento_Factura(aChange); end; ctUpdate: begin Update_Asiento_Factura(aChange); end; ctDelete: begin Delete_Asiento_Factura(aChange); end; end; // No hay que quitar los deltas para que los datos del contacto se // mantengan por si alguna tabla detalle lo necesita // (por ejemplo, DireccionesContacto) CanRemoveFromDelta := False; // Actualizamos el contador de referencias. case aChange.ChangeType of ctInsert, ctUpdate: begin if FReferenciaAutomatica then begin IncrementarReferencia; FReferenciaAutomatica := False; end; end; end; end; procedure TBizFacturasClienteServer.BeforeProcessDelta(Sender: TDABusinessProcessor; const aDelta: IDADelta); begin FReferenciaAutomatica := False; case Sender.CurrentChange.ChangeType of ctInsert, ctUpdate: begin //Si la referencia no ha sido asignada le asignamos una nosotros if REFERENCIAIsNull or (Length(REFERENCIA) = 0) then begin FReferenciaAutomatica := True; REFERENCIA := DarReferencia; end; end; end; end; procedure TBizFacturasClienteServer.Delete_Asiento_Factura(aChange: TDADeltaChange); var ASchema : TDASchema; ACurrentConn : IDAConnection; ACommand : IDASQLCommand; begin ASchema := BusinessProcessor.Schema; ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor); //Eliminamos los recibos de la factura ACommand := ASchema.NewCommand(ACurrentConn, 'Delete_RecibosFactura'); try with ACommand do begin ParamByName('ID_FACTURA').Value := aChange.OldValueByName[fld_FacturasClienteID]; Execute; end; finally ACommand := NIL; end; //Eliminamos los asientos contables de la factura ACommand := ASchema.NewCommand(ACurrentConn, 'Delete_AsientoFactura'); try with ACommand do begin ParamByName('IdFactura').Value := aChange.OldValueByName[fld_FacturasClienteID]; ParamByName('Tipo').Value := 'c'; Execute; end; finally ACommand := NIL; end; end; function TBizFacturasClienteServer.DarReferencia: String; var AReferenciasService : IsrvReferencias; Intf : IInterface; AClientID : TGUID; ATipo : String; begin //Nos solicita José Luís hacer distinción en la referencia entre facturas y abonos if TIPO = CTE_TIPO_FACTURA then ATipo := REF_FACTURAS_CLIENTE else ATipo := REF_ABONOS_CLIENTE; CreateGUID(AClientID); GetClassFactory('srvReferencias').CreateInstance(AClientID, Intf); AReferenciasService := Intf as IsrvReferencias; Result := AReferenciasService.DarNuevaReferencia(ATipo, ID_EMPRESA, ID_TIENDA) end; function TBizFacturasClienteServer.IncrementarReferencia: Boolean; var AReferenciasService : IsrvReferencias; Intf : IInterface; AClientID : TGUID; ATipo : String; begin if TIPO = CTE_TIPO_FACTURA then ATipo := REF_FACTURAS_CLIENTE else ATipo := REF_ABONOS_CLIENTE; CreateGUID(AClientID); GetClassFactory('srvReferencias').CreateInstance(AClientID, Intf); AReferenciasService := Intf as IsrvReferencias; Result := AReferenciasService.IncrementarValorReferencia(ATipo, Self.REFERENCIA, ID_EMPRESA, ID_TIENDA) end; procedure TBizFacturasClienteServer.Insert_Asiento_Factura(aChange: TDADeltaChange); var ASchema : TDASchema; ACurrentConn : IDAConnection; ACommand : IDASQLCommand; begin ASchema := BusinessProcessor.Schema; ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor); ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_AsientoFactura'); try with ACommand do begin ParamByName('IdFactura').Value := aChange.NewValueByName[fld_FacturasClienteID]; ParamByName('IdSubCuentaVenta').Value := aChange.NewValueByName[fld_FacturasClienteID_SUBCUENTA]; Execute; end; finally ACommand := NIL; end; end; procedure TBizFacturasClienteServer.Update_Asiento_Factura(aChange: TDADeltaChange); var ASchema : TDASchema; ACurrentConn : IDAConnection; ACommand : IDASQLCommand; begin ASchema := BusinessProcessor.Schema; ACurrentConn := GetBusinessProcessorConnection(BusinessProcessor); ACommand := ASchema.NewCommand(ACurrentConn, 'Insert_AsientoFactura'); try with ACommand do begin ParamByName('IdFactura').Value := aChange.NewValueByName[fld_FacturasClienteID]; ParamByName('IdSubCuentaVenta').Value := aChange.NewValueByName[fld_FacturasClienteID_SUBCUENTA]; Execute; end; finally ACommand := NIL; end; end; initialization RegisterBusinessProcessorRules(BIZ_SERVER_FACTURAS_CLIENTE, TBizFacturasClienteServer); end.