unit uEditorContratoCliente; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, uEditorDBItem, DB, uDADataTable, JvAppStorage, JvAppRegistryStorage, JvComponent, JvFormPlacement, ImgList, PngImageList, StdActns, ActnList, ComCtrls, TBX, TB2Item, TB2Dock, TB2Toolbar, ExtCtrls, JvExControls, JvNavigationPane, uCustomView, uViewBase, JvComponentBase, StdCtrls, pngimage, AppEvnts, cxControls, cxContainer, cxEdit, cxTextEdit, cxMemo, cxDBEdit, uViewIncidencias, uContratosClienteController, uViewDetallesBase, dxLayoutLookAndFeels, JvExComCtrls, JvStatusBar, uViewTotales, uIEditorContratoCliente, uBizContratosCliente, uViewContratoCliente, uViewDetallesDTO, uViewDetallesArticulos, uViewDetallesContratoCliente, uDAInterfaces, cxRichEdit, cxDBRichEdit, JvGIF, cxPC, uViewDocumentos, uViewDocumentosContratoCliente, uViewDatosYSeleccionClienteContrato, cxLabel, uBizTiposIVA, uTiposIVAController; type TfEditorContratoCliente = class(TfEditorDBItem, IEditorContratoCliente) pagContenido: TTabSheet; pagInicidencias: TTabSheet; frViewIncidenciasCli: TfrViewIncidencias; dxLayoutLookAndFeelList1: TdxLayoutLookAndFeelList; dxLayoutOfficeLookAndFeel1: TdxLayoutOfficeLookAndFeel; TBXSubmenuItem2: TTBXSubmenuItem; TBXItem7: TTBXItem; TBXSeparatorItem6: TTBXSeparatorItem; actEnviarPorEMail: TAction; TBXItem34: TTBXItem; TBXItem35: TTBXItem; pagDocumentos: TTabSheet; frViewDocumentosContratoCliente1: TfrViewDocumentosContratoCliente; frViewTotales1: TfrViewTotales; frViewDetallesContratoCliente1: TfrViewDetallesContratoCliente; actRecalcularPrecioPunto: TAction; procedure FormShow(Sender: TObject); procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction); procedure frViewTotales1ePortePropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); procedure pgPaginasChanging(Sender: TObject; var AllowChange: Boolean); procedure actEnviarPorEMailExecute(Sender: TObject); procedure actEnviarPorEMailUpdate(Sender: TObject); procedure actEliminarUpdate(Sender: TObject); procedure frViewTotales1bTiposIVAClick(Sender: TObject); procedure frViewTotales1eIVAPropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); procedure OnRecargoEquivalenciaPropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); procedure actRecalcularPrecioPuntoExecute(Sender: TObject); //Importante en este punto se deben de quitar los eventos que puedan afectar a la tabla una vez se cierre el editor. private //procedure RecalcularPortePorUnidad; protected FContrato: IBizContratoCliente; FTiposIVAController : ITiposIVAController; FTiposIVA: IBizTipoIVA; FViewContrato: IViewContratoCliente; FViewClienteContrato :IViewDatosYSeleccionClienteContrato; FController : IContratosClienteController; function GetController : IContratosClienteController; procedure SetController (const Value : IContratosClienteController); virtual; function GetContrato: IBizContratoCliente; procedure SetContrato(const Value: IBizContratoCliente); virtual; function GetViewContrato: IViewContratoCliente; procedure SetViewContrato(const Value: IViewContratoCliente); procedure PrevisualizarInterno; override; procedure ImprimirInterno; override; procedure GuardarInterno; override; procedure EliminarInterno; override; property ViewContrato: IViewContratoCliente read GetViewContrato write SetViewContrato; procedure OnClienteChanged(Sender : TObject); //Si queremos crear otra vista para el editor heredado solo tendriamos que //sobreescribir este metodo procedure AsignarVista; virtual; procedure PonerTitulos(const ATitulo: string = ''); override; function GetModified: Boolean; override; procedure SetModified(const Value : Boolean); override; function PuedoImprimir: Boolean; override; public property Controller : IContratosClienteController read GetController write SetController; property Contrato: IBizContratoCliente read GetContrato write SetContrato; constructor Create(AOwner: TComponent); override; destructor Destroy; override; end; implementation uses uViewDatosYSeleccionCliente, uDataModuleUsuarios, uClientesController, uDialogOpcionesImpresionContratosCliente, uBizDireccionesContacto, uDialogUtils, uFactuGES_App, uDataTableUtils; {$R *.dfm} { TfEditorContratoCliente } { **************************** TfEditorContratoCliente **************************** } procedure TfEditorContratoCliente.actEliminarUpdate(Sender: TObject); begin inherited; if (Sender as TAction).Enabled then (Sender as TAction).Enabled := (FContrato.SITUACION = SITUACION_Contrato_PENDIENTE); end; procedure TfEditorContratoCliente.actEnviarPorEMailExecute(Sender: TObject); begin inherited; FController.EnviarContratoPorEMail(Contrato); end; procedure TfEditorContratoCliente.actEnviarPorEMailUpdate(Sender: TObject); begin inherited; (Sender as TAction).Enabled := HayDatos; end; procedure TfEditorContratoCliente.actRecalcularPrecioPuntoExecute(Sender: TObject); begin ShowHourglassCursor; try inherited; //Esta lógica se llamará en el editor porque es para facilitar el rellenado de información del documento //no puede ir en la clase de negocio porque no es una lógica que tenga sentido fuera del editor. if (Contrato.Detalles.Datatable.State <> dsInactive) and (not Contrato.Detalles.DataTable.IsEmpty) and Assigned(Controller) and Assigned(Controller.DetallesController) then begin Controller.DetallesController.PrecioPunto := FContrato.PRECIO_PUNTO; Controller.DetallesController.RecalcularPrecioPuntoDetalles(FContrato.PRECIO_PUNTO, FContrato.Detalles) end; finally HideHourglassCursor; end; end; procedure TfEditorContratoCliente.AsignarVista; var AViewContratoCliente: TfrViewContratoCliente; begin AViewContratoCliente := TfrViewContratoCliente.create(Self); with AViewContratoCliente do begin Parent := pagGeneral; Align := alClient; dxLayoutControl1.LookAndFeel := dxLayoutOfficeLookAndFeel1; end; ViewContrato := AViewContratoCliente; end; constructor TfEditorContratoCliente.Create(AOwner: TComponent); begin inherited; pgPaginas.ActivePageIndex := 0; AsignarVista; FTiposIVAController := TTiposIVAController.Create; end; procedure TfEditorContratoCliente.CustomEditorClose(Sender: TObject; var Action: TCloseAction); begin inherited; //GESTION_DOCUMENTOS //Este método eliminará el directorio temporal si se ha creado frViewDocumentosContratoCliente1.actEliminarTodo.Execute; FViewContrato := NIL; FViewClienteContrato := NIL; FContrato := NIL; FTiposIVAController := Nil; end; destructor TfEditorContratoCliente.Destroy; begin // Utilizar mejor OnClose; inherited; end; procedure TfEditorContratoCliente.EliminarInterno; begin if (ShowConfirmMessage('¿Desea borrar este Contrato de cliente?', '') = IDYES) then begin FController.Eliminar(FContrato); inherited end end; procedure TfEditorContratoCliente.FormShow(Sender: TObject); begin inherited; if not Assigned(FViewContrato) then raise Exception.Create('No hay ninguna vista asignada'); if not Assigned(Contrato) then raise Exception.Create('No hay ningún Contrato asignado'); frViewTotales1.cbRecargoEquivalencia.Properties.OnValidate := OnRecargoEquivalenciaPropertiesValidate; pgPaginas.ActivePage := pagGeneral; Contrato.DataTable.Active := True; end; procedure TfEditorContratoCliente.frViewTotales1bTiposIVAClick( Sender: TObject); begin inherited; FTiposIVAController.VerTodos(FTiposIVA); end; procedure TfEditorContratoCliente.frViewTotales1eIVAPropertiesValidate( Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); begin inherited; Contrato.Edit; Contrato.ID_TIPO_IVA := FTiposIVA.ID; // ((frViewTotales1.dsTiposIVA.DataTable) as IBizTipoIVA).ID; end; procedure TfEditorContratoCliente.frViewTotales1ePortePropertiesValidate( Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); begin inherited; { if (not VarIsNull(DisplayValue)) and (Length(DisplayValue) > 0) then FContrato.IMPORTE_PORTE := DisplayValue else FContrato.IMPORTE_PORTE := 0; } // RecalcularPortePorUnidad; En los documentos de cliente no se desglosa el porte por articulo end; function TfEditorContratoCliente.GetController: IContratosClienteController; begin Result := FController; end; function TfEditorContratoCliente.GetModified: Boolean; begin // inherited; <- No llamar al padre porque queremos tratamiento especial. if ReadOnly then Result := False else begin Result := DataTableModified(dsDataTable.DataTable); if not Result then Result := frViewDocumentosContratoCliente1.Modified; end; end; function TfEditorContratoCliente.GetContrato: IBizContratoCliente; begin Result := FContrato; end; function TfEditorContratoCliente.GetViewContrato: IViewContratoCliente; begin Result := FViewContrato; end; procedure TfEditorContratoCliente.GuardarInterno; var bEsNuevo : Boolean; begin inherited; ShowHourglassCursor; // frViewDetallesContratoCliente1.SaveGridStatus; frViewDetallesContratoCliente1.BeginUpdate; try bEsNuevo := FContrato.EsNuevo; //GESTION_DOCUMENTOS if FController.Guardar(FContrato) then frViewDocumentosContratoCliente1.actActualizarServidor.Execute; finally frViewDetallesContratoCliente1.EndUpdate; // frViewDetallesContratoCliente1.RestoreGridStatus; HideHourglassCursor; end; if bEsNuevo then ShowInfoMessage('El Contrato se ha dado de alta con el código ' + FContrato.REFERENCIA); Modified := False; end; procedure TfEditorContratoCliente.ImprimirInterno; var FVerLogotipo: Boolean; FImprimirPrecios : Boolean; FImprimirTotales : Boolean; FImprimirRefProveedor : Boolean; FImprimirObservaciones: Boolean; FImprimirIncidencias: Boolean; begin inherited; FVerLogotipo := True; FImprimirPrecios := False; FImprimirTotales := True; FImprimirRefProveedor := False; FImprimirObservaciones := False; FImprimirIncidencias := False; if not Modified then if ElegirOpcionesImpresionContratoCliente(FVerLogotipo, FImprimirPrecios, FImprimirTotales, FImprimirRefProveedor, FImprimirObservaciones, FImprimirIncidencias) then FController.Print(FContrato, False, FVerLogotipo, FImprimirPrecios, FImprimirTotales, FImprimirRefProveedor, FImprimirObservaciones, FImprimirIncidencias); end; procedure TfEditorContratoCliente.OnClienteChanged(Sender: TObject); var APrimerCliente : Boolean; begin if Assigned(FContrato) then begin APrimerCliente := (FContrato.Cliente.ID = 0); FContrato.Cliente := ViewContrato.ViewClienteContrato.Cliente; Controller.DetallesController.PrecioPunto := FContrato.Cliente.PRECIO_PUNTO; if (ShowConfirmMessage('Cambio forma de pago y tipo de IVA', '¿Desea actualizar la forma de pago y el tipo de IVA a la asignada en la ficha del cliente seleccionado?') = IDYES) then FController.ActualizarFormaDePago(FContrato, FContrato.Cliente.ID_FORMA_PAGO); if (not APrimerCliente) and (FContrato.Detalles.RecordCount > 0) then begin if (ShowConfirmMessage('Actualizar los descuentos de las líneas', '¿Desea actualizar las líneas de detalle con el descuento del cliente?' + #10#13 + 'Sólo se cambiarán las líneas que tengan cantidades.') = IDYES) then begin FController.DetallesController.ActualizarDetalles(FContrato.Detalles, FContrato.Cliente); ShowInfoMessage('Se han actualizado las líneas con el descuento del cliente seleccionado'); end; end; //Se refresca dirección de envio ViewContrato.ViewDireccionEntregaContratoCli.ContratoCliente := FContrato; end; end; procedure TfEditorContratoCliente.OnRecargoEquivalenciaPropertiesValidate( Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean); begin if frViewTotales1.cbRecargoEquivalencia.Checked then Contrato.RECARGO_EQUIVALENCIA := 1 else Contrato.RECARGO_EQUIVALENCIA := 0; end; procedure TfEditorContratoCliente.pgPaginasChanging(Sender: TObject; var AllowChange: Boolean); var ACadena : String; begin inherited; if (not Assigned(FContrato)) or (FContrato.ID_CLIENTE = 0) then begin ACadena := 'Antes de introducir conceptos debe elegir un cliente para este Contrato'; ShowWarningMessage(ACadena); AllowChange := False; end end; procedure TfEditorContratoCliente.PonerTitulos(const ATitulo: string); var FTitulo : String; begin FTitulo := ATitulo; if (FTitulo = '') and Assigned(FContrato) then begin if FContrato.EsNuevo then FTitulo := 'Nuevo Contrato de cliente' else FTitulo := 'Contrato de cliente' + ' - ' + FContrato.Cliente.Nombre + ' - ' + FContrato.SITUACION; end; inherited PonerTitulos(FTitulo); Self.Caption := FTitulo + ' (' + AppFactuGES.EmpresaActiva.NOMBRE + ')'; end; procedure TfEditorContratoCliente.PrevisualizarInterno; var FVerLogotipo: Boolean; FImprimirPrecios : Boolean; FImprimirTotales : Boolean; FImprimirRefProveedor : Boolean; FImprimirObservaciones: Boolean; FImprimirIncidencias: Boolean; begin inherited; FVerLogotipo := True; FImprimirPrecios := False; FImprimirTotales := True; FImprimirRefProveedor := False; FImprimirObservaciones := False; FImprimirIncidencias := False; if not Modified then if ElegirOpcionesImpresionContratoCliente(FVerLogotipo, FImprimirPrecios, FImprimirTotales, FImprimirRefProveedor, FImprimirObservaciones, FImprimirIncidencias) then FController.Preview(FContrato, False, FVerLogotipo, FImprimirPrecios, FImprimirTotales, FImprimirRefProveedor, FImprimirObservaciones, FImprimirIncidencias); end; {procedure TfEditorContratoCliente.RecalcularPortePorUnidad; begin //Esta lógica se llamará en el editor porque es para facilitar el rellenado de información del documento //no puede ir en la clase de negocio porque no es una lógica que tenga sentido fuera del editor. if Assigned(Controller) and Assigned(Controller.DetallesController) then Controller.DetallesController.DesglosarPorteDetalles(FContrato.IMPORTE_PORTE, FContrato.Detalles) end;} procedure TfEditorContratoCliente.SetController(const Value: IContratosClienteController); begin FController := Value; if Assigned(FController) then begin // ViewContrato.ViewClienteContrato.Controller := Controller.ClienteController; frViewDetallesContratoCliente1.Controller := Controller.DetallesController; //GESTION_DOCUMENTOS frViewDocumentosContratoCliente1.Controller := Controller; // frViewTotales1.Controller := FController; end; end; procedure TfEditorContratoCliente.SetModified(const Value: Boolean); begin inherited; frViewDocumentosContratoCliente1.Modified := Value; end; function TfEditorContratoCliente.PuedoImprimir: Boolean; begin Result := inherited PuedoImprimir and (not Contrato.Cliente.IDIsNull); end; procedure TfEditorContratoCliente.SetContrato(const Value: IBizContratoCliente); begin FContrato := Value; if Assigned(FContrato) then begin dsDataTable.DataTable := FContrato.DataTable; frViewIncidenciasCli.DADataSource.DataTable := dsDataTable.DataTable; frViewTotales1.DADataSource.DataTable := FContrato.DataTable; FTiposIVA := FTiposIVAController.BuscarTodos; frViewTotales1.dsTiposIVA.DataTable := FTiposIVA.DataTable; FTiposIVA.DataTable.Active := True; if Assigned(FViewContrato) then begin ViewContrato.ViewClienteContrato.OnClienteChanged := NIL; try FViewContrato.Contrato := FContrato; frViewDetallesContratoCliente1.Detalles := FContrato.Detalles; frViewDetallesContratoCliente1.Contrato := FContrato; //Para poder sacar los descuento del articulos segun el cliente seleccionado Controller.DetallesController.PrecioPunto := FContrato.Cliente.PRECIO_PUNTO; //GESTION_DOCUMENTOS frViewDocumentosContratoCliente1.Contrato := FContrato; finally ViewContrato.ViewClienteContrato.OnClienteChanged := OnClienteChanged; end; end; end; end; procedure TfEditorContratoCliente.SetViewContrato(const Value: IViewContratoCliente); begin FViewContrato := Value; if Assigned(FViewContrato) then begin FViewClienteContrato := FViewContrato.ViewClienteContrato; if Assigned(Contrato) then FViewContrato.Contrato := Contrato; end; end; end.