unit uBizPresupuestosCliente; interface uses Classes, uDADataTable, uDAInterfaces, uROClasses, schPresupuestosClienteClient_Intf, uDBSelectionListUtils, uBizContactos, uBizDetallesPresupuestoCliente; const BIZ_CLIENT_PRESUPUESTO_CLIENTE = 'Client.PresupuestoCliente'; SITUACION_PRESUPUESTO_PENDIENTE = 'PENDIENTE'; SITUACION_PRESUPUESTO_ACEPTADO = 'ACEPTADO'; SITUACION_PRESUPUESTO_ANULADO = 'ANULADO'; type IBizPresupuestoCliente = interface(IPresupuestosCliente) ['{86A2FAD7-D125-47C2-B0BA-52A42FDFA6F6}'] procedure SetCliente(AValue : IBizCliente); function GetCliente : IBizCliente; property Cliente : IBizCliente read GetCliente write SetCliente; // Esta propidad es para que el controlador pueda acceder directamente // a la propiedad Cliente procedure _SetCliente(AValue : IBizCliente); function _GetCliente : IBizCliente; property _Cliente : IBizCliente read _GetCliente write _SetCliente; function GetDetalles: IBizDetallesPresupuestoCliente; procedure SetDetalles(Value: IBizDetallesPresupuestoCliente); property Detalles: IBizDetallesPresupuestoCliente read GetDetalles write SetDetalles; procedure CalcularImporteTotal; function EsNuevo : Boolean; function DarListaSituaciones: TStringList; end; TBizPresupuestoCliente = class(TPresupuestosClienteDataTableRules, IBizPresupuestoCliente, ISeleccionable) private procedure CalcularBaseImponible; procedure CalcularIVA; protected FSeleccionableInterface : ISeleccionable; FCliente : IBizCliente; FDetalles : IBizDetallesPresupuestoCliente; FDetallesLink : TDADataSource; procedure SetCliente(AValue : IBizCliente); function GetCliente : IBizCliente; procedure _SetCliente(AValue : IBizCliente); function _GetCliente : IBizCliente; function GetDetalles: IBizDetallesPresupuestoCliente; procedure SetDetalles(Value: IBizDetallesPresupuestoCliente); function GetSITUACIONValue: String; override; procedure SetFECHA_PRESUPUESTOValue(const aValue: TDateTime); override; procedure SetID_FORMA_PAGOValue(const aValue: Integer); override; procedure OnNewRecord(Sender: TDADataTable); override; procedure IniciarValoresPresupuestoNuevo; procedure IMPORTE_NETOOnChange(Sender: TDACustomField); procedure IMPORTE_BONIFICACIONOnChange(Sender: TDACustomField); procedure IMPORTE_PORTEOnChange(Sender: TDACustomField); procedure DESCUENTOOnChange(Sender: TDACustomField); procedure IVAOnChange(Sender: TDACustomField); public function DarListaSituaciones: TStringList; function EsNuevo : Boolean; procedure CalcularImporteTotal; property Cliente : IBizCliente read GetCliente write SetCliente; property _Cliente : IBizCliente read _GetCliente write _SetCliente; property Detalles: IBizDetallesPresupuestoCliente read GetDetalles write SetDetalles; property SeleccionableInterface : ISeleccionable read FSeleccionableInterface write FSeleccionableInterface implements ISeleccionable; constructor Create(aDataTable: TDADataTable); override; destructor Destroy; override; end; implementation uses SysUtils, Variants, uDataModuleConfiguracion, uDataModuleUsuarios, Dialogs, uDataTableUtils, DateUtils, uDateUtils, DB, uFactuGES_App; { TBizPresupuestoCliente } procedure TBizPresupuestoCliente.CalcularBaseImponible; begin if not Self.DataTable.Editing then Edit; IMPORTE_DESCUENTO := IMPORTE_NETO * (DESCUENTO/100); BASE_IMPONIBLE := IMPORTE_NETO - IMPORTE_DESCUENTO - IMPORTE_BONIFICACION + IMPORTE_PORTE; end; procedure TBizPresupuestoCliente.CalcularImporteTotal; begin DataTable.DisableControls; DataTable.Fields.FieldEventsDisabled := True; try CalcularBaseImponible; CalcularIVA; if not Self.DataTable.Editing then Edit; IMPORTE_TOTAL := BASE_IMPONIBLE + IMPORTE_IVA; finally DataTable.Fields.FieldEventsDisabled := False; DataTable.EnableControls; end; end; procedure TBizPresupuestoCliente.CalcularIVA; begin if not Self.DataTable.Editing then Edit; IMPORTE_IVA := (IVA / 100) * (BASE_IMPONIBLE); end; constructor TBizPresupuestoCliente.Create(aDataTable: TDADataTable); begin inherited; FCliente := Nil; with DataTable do begin FieldByName(fld_PresupuestosClienteIMPORTE_NETO).OnChange := IMPORTE_NETOOnChange; FieldByName(fld_PresupuestosClienteIMPORTE_BONIFICACION).OnChange := IMPORTE_BONIFICACIONOnChange; FieldByName(fld_PresupuestosClienteIMPORTE_PORTE).OnChange := IMPORTE_PORTEOnChange; FieldByName(fld_PresupuestosClienteDESCUENTO).OnChange := DESCUENTOOnChange; FieldByName(fld_PresupuestosClienteIVA).OnChange := IVAOnChange; end; FDetallesLink := TDADataSource.Create(NIL); FDetallesLink.DataTable := aDataTable; FSeleccionableInterface := TSeleccionable.Create(aDataTable); end; function TBizPresupuestoCliente.DarListaSituaciones: TStringList; begin Result := TStringList.Create; with Result do begin Add(SITUACION_PRESUPUESTO_PENDIENTE); Add(SITUACION_PRESUPUESTO_ACEPTADO); Add(SITUACION_PRESUPUESTO_ANULADO); end; end; procedure TBizPresupuestoCliente.DESCUENTOOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; destructor TBizPresupuestoCliente.Destroy; begin FCliente := NIL; FDetalles := NIL; FDetallesLink.Free; FSeleccionableInterface := NIL; inherited; end; function TBizPresupuestoCliente.EsNuevo: Boolean; begin Result := (ID < 0); end; function TBizPresupuestoCliente.GetCliente: IBizCliente; begin Result := FCliente; end; procedure TBizPresupuestoCliente.IMPORTE_BONIFICACIONOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; procedure TBizPresupuestoCliente.IMPORTE_NETOOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; procedure TBizPresupuestoCliente.IMPORTE_PORTEOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; procedure TBizPresupuestoCliente.IniciarValoresPresupuestoNuevo; begin ID_EMPRESA := AppFactuGES.EmpresaActiva.ID; USUARIO := AppFactuGES.UsuarioActivo.UserName; FECHA_PRESUPUESTO := DateOf(Date); INCIDENCIAS_ACTIVAS := 0; CERTIFICADO_ISO := 1; // INCIDENCIAS := NIL; REFERENCIA := ''; SITUACION := SITUACION_PRESUPUESTO_PENDIENTE; end; procedure TBizPresupuestoCliente.IVAOnChange(Sender: TDACustomField); begin CalcularImporteTotal; end; function TBizPresupuestoCliente.GetDetalles: IBizDetallesPresupuestoCliente; begin Result := FDetalles; end; function TBizPresupuestoCliente.GetSITUACIONValue: String; begin result := Trim(DataTable.Fields[idx_PresupuestosClienteSITUACION].AsString); end; procedure TBizPresupuestoCliente.OnNewRecord(Sender: TDADataTable); begin inherited; // ID := GetRecNo; // -1, -2, -3... IniciarValoresPresupuestoNuevo; end; procedure TBizPresupuestoCliente.SetCliente(AValue: IBizCliente); var bEnEdicion : Boolean; begin FCliente := AValue; if Assigned(FCliente) then begin if not FCliente.DataTable.Active then FCliente.DataTable.Active := True; if ID_Cliente <> FCliente.ID then begin bEnEdicion := (DataTable.State in dsEditModes); if not bEnEdicion then DataTable.Edit; ID_CLIENTE := FCliente.ID; if FCliente.ID_FORMA_PAGO > 0 then ID_FORMA_PAGO := FCliente.ID_FORMA_PAGO; DESCUENTO := FCliente.DESCUENTO; DataTable.Post; //Muy importante ya que es necesario hacer un post de la cabecera antes de aņadir detalles //si se quita el id de la cabecera y los detalles se desincroniza if bEnEdicion then DataTable.Edit; end; end; end; procedure TBizPresupuestoCliente.SetDetalles(Value: IBizDetallesPresupuestoCliente); begin FDetalles := Value; EnlazarMaestroDetalle(FDetallesLink, FDetalles); end; procedure TBizPresupuestoCliente.SetFECHA_PRESUPUESTOValue(const aValue: TDateTime); begin if EsFechaVacia(aValue) then SetFieldNull(DataTable, fld_PresupuestosClienteFECHA_PRESUPUESTO) else inherited; end; procedure TBizPresupuestoCliente.SetID_FORMA_PAGOValue(const aValue: Integer); begin if (aValue = 0) then SetFieldNull(DataTable, fld_PresupuestosClienteID_FORMA_PAGO) else inherited; end; { procedure TBizPresupuestoCliente.SetINCIDENCIASValue(const aValue: IROStrings); begin if not Assigned(aValue) then SetFieldNull(DataTable, fld_PresupuestosClienteINCIDENCIAS) else inherited; end; } function TBizPresupuestoCliente._GetCliente: IBizCliente; begin Result := FCliente; end; procedure TBizPresupuestoCliente._SetCliente(AValue: IBizCliente); begin FCliente := AValue; end; initialization RegisterDataTableRules(BIZ_CLIENT_PRESUPUESTO_CLIENTE, TBizPresupuestoCliente); finalization end.