unit uEditorContratosCliente; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Menus, DB, uDADataTable, JvAppStorage, JvAppRegistryStorage, JvComponent, JvFormPlacement, ImgList, PngImageList, StdActns, ActnList, ComCtrls, TB2ExtItems, TBXExtItems, TBX, TB2Item, TB2Dock, TB2Toolbar, ExtCtrls, JvExControls, JvNavigationPane, uViewGrid, pngimage, JvComponentBase, uEditorGridBase, uBizContratosCliente, uIEditorContratosCliente, uContratosClienteController, uCustomView, uViewBase, uViewBarraSeleccion, uViewGridBase, uViewContratosCliente, JvExComCtrls, JvStatusBar, JSDialog, StdCtrls, uDAInterfaces; type TfEditorContratosCliente = class(TfEditorGridBase, IEditorContratosCliente) actGenerarAlbaranCli: TAction; actGenerarFactura: TAction; TBXSubmenuItem2: TTBXSubmenuItem; TBXSeparatorItem17: TTBXSeparatorItem; TBXItem38: TTBXItem; TBXItem39: TTBXItem; N4: TMenuItem; Generar2: TMenuItem; JsGenerarDialog: TJSDialog; JsPrevisualizarDialog: TJSDialog; JsImprimirDialog: TJSDialog; TBXItem40: TTBXItem; JsListaContratosNoEliminados: TJSDialog; TBXItem41: TTBXItem; Generarcertificado1: TMenuItem; TBXSeparatorItem18: TTBXSeparatorItem; actEnviarEMail: TAction; TBXItem45: TTBXItem; Enviarporemail1: TMenuItem; actInformes: TAction; TBXItem46: TTBXItem; JSInformesDialog: TJSDialog; TBXItem47: TTBXItem; actListadoBeneficios: TAction; TBXItem42: TTBXItem; TBXSubmenuItem3: TTBXSubmenuItem; TBXItem43: TTBXItem; Generaralbarndecliente1: TMenuItem; actGenerarPedidoProv: TAction; TBXItem44: TTBXItem; TBXSeparatorItem19: TTBXSeparatorItem; actVerPedidosProveedor: TAction; TBXItem48: TTBXItem; TBXSeparatorItem20: TTBXSeparatorItem; actVerFacturasProveedor: TAction; TBXItem49: TTBXItem; actAceptar: TAction; actRechazar: TAction; actGenerarReciboCli: TAction; TBXSeparatorItem21: TTBXSeparatorItem; actLOPD: TAction; TBXItem50: TTBXItem; procedure FormShow(Sender: TObject); procedure actGenerarAlbaranCliExecute(Sender: TObject); procedure actEliminarUpdate(Sender: TObject); procedure actGenerarFacturaUpdate(Sender: TObject); procedure actGenerarFacturaExecute(Sender: TObject); procedure actGenerarPresupuestoProvExecute(Sender: TObject); procedure actGenerarExecute(Sender: TObject); procedure actAceptarExecute(Sender: TObject); procedure actAceptarUpdate(Sender: TObject); procedure actAnularExecute(Sender: TObject); procedure actAnularUpdate(Sender: TObject); procedure actEnviarEMailExecute(Sender: TObject); procedure actEnviarEMailUpdate(Sender: TObject); procedure actInformesExecute(Sender: TObject); procedure OnListaAnosChange(Sender: TObject; const Text: string); procedure actListadoBeneficiosExecute(Sender: TObject); procedure bCocinaClick(Sender: TObject); procedure bArmarioClick(Sender: TObject); procedure bBanoClick(Sender: TObject); procedure bElectrodomesticoClick(Sender: TObject); procedure bVariosClick(Sender: TObject); procedure bObraClick(Sender: TObject); procedure actGenerarAlbaranCliUpdate(Sender: TObject); procedure actGenerarPedidoProvExecute(Sender: TObject); procedure actGenerarPedidoProvUpdate(Sender: TObject); procedure actVerPedidosProveedorExecute(Sender: TObject); procedure actVerFacturasProveedorExecute(Sender: TObject); procedure actRechazarExecute(Sender: TObject); procedure actRechazarUpdate(Sender: TObject); procedure actGenerarReciboCliExecute(Sender: TObject); procedure actGenerarReciboCliUpdate(Sender: TObject); procedure actLOPDExecute(Sender: TObject); procedure actLOPDUpdate(Sender: TObject); private procedure cambiarSituacion (const ASituacion: String); protected FContratos: IBizContratoCliente; FController : IContratosClienteController; function GetContratos: IBizContratoCliente; procedure SetContratos(const Value: IBizContratoCliente); function GetController : IContratosClienteController; virtual; procedure SetController (const Value : IContratosClienteController); virtual; procedure NuevoInterno; override; procedure ModificarInterno; override; procedure EliminarInterno; override; procedure DuplicarInterno; override; procedure ImprimirInterno; override; procedure PrevisualizarInterno; override; procedure RefrescarInterno; override; //Si queremos crear otra vista para el editor heredado solo tendriamos que //sobreescribir este metodo procedure AsignarVista; virtual; public procedure PonerTitulos(const ATitulo: string = ''); override; property Contratos: IBizContratoCliente read GetContratos write SetContratos; property Controller : IContratosClienteController read GetController write SetController; constructor Create(AOwner: TComponent); override; destructor Destroy; override; end; implementation uses uDataModuleContratosCliente, uDataModuleUsuarios, uFactuGES_App, uEditorBase, uEditorDBBase, uDialogUtils, Dialogs, uDBSelectionListUtils, uGridUtils, uGenerarFacturasCliUtils, uGenerarRecibosCliUtils, uGenerarAlbaranesCliUtils, uGestorInformesController, uGenerarPedidosProvUtils, uEditorFechaDecisionContrato; {$R *.dfm} { *************************** TfEditorContratosCliente *************************** } procedure TfEditorContratosCliente.actAceptarExecute(Sender: TObject); var AContratos: IBizContratoCliente; AllItems: Boolean; AFecha: TDate; begin AContratos := Nil; AllItems := False; //Pedimos fecha de decision y realizamos todo el proceso de asignacion, en el caso de no darla no se hace nada if ElegirFechaDecisionContrato(AFecha) then begin if MultiSelect and Assigned(ViewGrid) then AllItems := (ViewGrid.NumSeleccionados > 1); if AllItems then begin SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, (Contratos as ISeleccionable).SelectedRecords); AContratos := (Controller as IContratosClienteController).ExtraerSeleccionados(Contratos) as IBizContratoCliente; end else begin AContratos := Contratos; end; ViewGrid._Grid.BeginUpdate; if Assigned(AContratos) then begin FController.CambiarSituacion(AContratos, SITUACION_CONTRATO_ACEPTADO, AFecha, AllItems); actRefrescar.Execute; end; ViewGrid._Grid.EndUpdate; end; end; procedure TfEditorContratosCliente.actAceptarUpdate(Sender: TObject); begin inherited; (Sender as TAction).Enabled := HayDatos and ViewGrid.esSeleccionCeldaDatos and (FContratos.SITUACION = SITUACION_CONTRATO_PENDIENTE); end; procedure TfEditorContratosCliente.actAnularExecute(Sender: TObject); begin { AContratos := Nil; AllItems := False; //Pedimos fecha de decision y realizamos todo el proceso de asignacion, en el caso de no darla no se hace nada AFecha := Null; with TfEditorFechaDecision.Create(nil) do try if ShowModal = mrOk then AFecha := edtFechaDecision.Date; finally Free; end; if not VarIsNull(AFecha) then begin if MultiSelect and Assigned(ViewGrid) then AllItems := (ViewGrid.NumSeleccionados > 1); if AllItems then begin SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, (Contratos as ISeleccionable).SelectedRecords); AContratos := (Controller as IContratosClienteController).ExtraerSeleccionados(Contratos) as IBizContratoCliente; end else begin AContratos := Contratos; end; ViewGrid._Grid.BeginUpdate; if Assigned(AContratos) then begin FController.CambiarSituacion(AContratos, SITUACION_PRESUPUESTO_ANULADO, AFecha, AllItems); actRefrescar.Execute; end; ViewGrid._Grid.EndUpdate; end; } end; procedure TfEditorContratosCliente.actAnularUpdate(Sender: TObject); begin inherited; { (Sender as TAction).Enabled := HayDatos and ViewGrid.esSeleccionCeldaDatos and (FContratos.SITUACION <> SITUACION_PRESUPUESTO_ANULADO); } end; procedure TfEditorContratosCliente.actEliminarUpdate(Sender: TObject); begin inherited; if (Sender as TAction).Enabled then (Sender as TAction).Enabled := (FContratos.SITUACION = SITUACION_CONTRATO_PENDIENTE); end; procedure TfEditorContratosCliente.actEnviarEMailExecute(Sender: TObject); begin inherited; FController.EnviarContratoPorEMail(FContratos); end; procedure TfEditorContratosCliente.actEnviarEMailUpdate(Sender: TObject); begin inherited; if HayDatos and Assigned(ViewGrid) then (Sender as TAction).Enabled := ViewGrid.EsSeleccionCeldaDatos and (ViewGrid.NumSeleccionados > 0) else (Sender as TAction).Enabled := False; end; procedure TfEditorContratosCliente.actGenerarAlbaranCliExecute(Sender: TObject); begin inherited; GenerarAlbaranCli(Contratos.ID); actRefrescar.Execute; end; procedure TfEditorContratosCliente.actGenerarAlbaranCliUpdate(Sender: TObject); begin inherited; (Sender as TAction).Enabled := HayDatos and not (ViewGrid.NumSeleccionados > 1) and ViewGrid.esSeleccionCeldaDatos; end; procedure TfEditorContratosCliente.actGenerarExecute(Sender: TObject); //var // Respuesta : Integer; begin inherited; { Respuesta := JsGenerarDialog.Execute; if Respuesta <> IDCANCEL then begin case JsGenerarDialog.CustomButtonResult of 200 : begin // Generar factura de cliente actGenerarFactura.Execute; end; 100 : begin // Generar albaran de cliente // actGenerarAlbaranCli.Execute; end; end; actRefrescar.Execute; end; } end; procedure TfEditorContratosCliente.actGenerarFacturaExecute(Sender: TObject); begin inherited; GenerarFacturaCli(Contratos.ID); actRefrescar.Execute; end; procedure TfEditorContratosCliente.actGenerarFacturaUpdate(Sender: TObject); begin inherited; (Sender as TAction).Enabled := HayDatos and not (ViewGrid.NumSeleccionados > 1) and ViewGrid.esSeleccionCeldaDatos and (FContratos.SITUACION = SITUACION_CONTRATO_ACEPTADO); end; procedure TfEditorContratosCliente.actGenerarPedidoProvExecute(Sender: TObject); var ACopiarDetalles: Boolean; begin inherited; ACopiarDetalles := (ShowConfirmMessage('Generar pedido a proveedor', Format('¿Desea copiar el contenido del contrato seleccionado %s?', [Contratos.REFERENCIA])) = IDYES); GenerarPedidoProv(Contratos.ID, ACopiarDetalles); end; procedure TfEditorContratosCliente.actGenerarPedidoProvUpdate(Sender: TObject); begin inherited; (Sender as TAction).Enabled := HayDatos and not (ViewGrid.NumSeleccionados > 1) and ViewGrid.esSeleccionCeldaDatos and (FContratos.SITUACION = SITUACION_CONTRATO_PENDIENTE); end; procedure TfEditorContratosCliente.actGenerarPresupuestoProvExecute(Sender: TObject); begin inherited; // GenerarContratosProv(FContratos); end; procedure TfEditorContratosCliente.actInformesExecute(Sender: TObject); var Respuesta : Integer; AGestorInformesController : IGestorInformesController; begin AGestorInformesController := TGestorInformesController.Create; try Respuesta := JsInformesDialog.Execute; if Respuesta <> IDCANCEL then begin case JsInformesDialog.CustomButtonResult of 100 : begin // Listado de facturas pendientes // AGestorInformesController.VerInformeListadoContratos; end; end; end; finally AGestorInformesController := NIL; end; end; procedure TfEditorContratosCliente.actListadoBeneficiosExecute(Sender: TObject); begin inherited; FController.VerListadoBeneficios; end; procedure TfEditorContratosCliente.actLOPDExecute(Sender: TObject); var AContratos: IBizContratoCliente; begin inherited; ViewGrid.SaveGridStatus; try SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, (Contratos as ISeleccionable).SelectedRecords); AContratos := (Controller as IContratosClienteController).ExtraerSeleccionados(Contratos) as IBizContratoCliente; finally ViewGrid.RestoreGridStatus; end; if (Controller as IContratosClienteController).AsignarLOPD(AContratos) then RefrescarInterno; end; procedure TfEditorContratosCliente.actLOPDUpdate(Sender: TObject); begin inherited; if HayDatos and Assigned(ViewGrid) then (Sender as TAction).Enabled := not (dsDataTable.DataTable.State in dsEditModes) and ViewGrid.esSeleccionCeldaDatos and (ViewGrid.NumSeleccionados > 0) else (Sender as TAction).Enabled := False; end; procedure TfEditorContratosCliente.actRechazarExecute(Sender: TObject); begin cambiarSituacion(SITUACION_CONTRATO_RECHAZADO); end; procedure TfEditorContratosCliente.actRechazarUpdate(Sender: TObject); begin inherited; (Sender as TAction).Enabled := HayDatos and ViewGrid.esSeleccionCeldaDatos and (FContratos.SITUACION = SITUACION_CONTRATO_PENDIENTE); end; procedure TfEditorContratosCliente.actGenerarReciboCliExecute(Sender: TObject); begin inherited; GenerarReciboCli(Contratos.ID); actRefrescar.Execute; end; procedure TfEditorContratosCliente.actGenerarReciboCliUpdate(Sender: TObject); begin inherited; (Sender as TAction).Enabled := HayDatos and not (ViewGrid.NumSeleccionados > 1) and ViewGrid.esSeleccionCeldaDatos and (FContratos.SITUACION = SITUACION_CONTRATO_ACEPTADO) and (FContratos.FACTURA_ASOCIADA = ''); end; procedure TfEditorContratosCliente.actVerFacturasProveedorExecute( Sender: TObject); begin inherited; FController.VerFacturasProveedor(Contratos); end; procedure TfEditorContratosCliente.actVerPedidosProveedorExecute( Sender: TObject); begin inherited; FController.VerPedidosProveedor(Contratos); end; procedure TfEditorContratosCliente.AsignarVista; begin ViewGrid := CreateView(TfrViewContratosCliente) as IViewContratosCliente; end; procedure TfEditorContratosCliente.bArmarioClick(Sender: TObject); begin inherited; if FController.Anadir(Contratos, teArmario) then FController.Ver(Contratos); end; procedure TfEditorContratosCliente.bBanoClick(Sender: TObject); begin inherited; if FController.Anadir(Contratos, teBano) then FController.Ver(Contratos); end; procedure TfEditorContratosCliente.bCocinaClick(Sender: TObject); begin inherited; if FController.Anadir(Contratos, teCocina) then FController.Ver(Contratos); end; procedure TfEditorContratosCliente.bElectrodomesticoClick(Sender: TObject); begin inherited; if FController.Anadir(Contratos, teElectrodomestico) then FController.Ver(Contratos); end; procedure TfEditorContratosCliente.bObraClick(Sender: TObject); begin inherited; if FController.Anadir(Contratos, teObra) then FController.Ver(Contratos); end; procedure TfEditorContratosCliente.bVariosClick(Sender: TObject); begin inherited; if FController.Anadir(Contratos, teVarios) then FController.Ver(Contratos); end; procedure TfEditorContratosCliente.cambiarSituacion(const ASituacion: String); var AContratos: IBizContratoCliente; AllItems: Boolean; AFecha: TDate; begin AContratos := Nil; AllItems := False; //Pedimos fecha de decision y realizamos todo el proceso de asignacion, en el caso de no darla no se hace nada if ElegirFechaDecisionContrato(AFecha) then begin if MultiSelect and Assigned(ViewGrid) then AllItems := (ViewGrid.NumSeleccionados > 1); if AllItems then begin SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, (Contratos as ISeleccionable).SelectedRecords); AContratos := (Controller as IContratosClienteController).ExtraerSeleccionados(Contratos) as IBizContratoCliente; end else begin AContratos := Contratos; end; ViewGrid._Grid.BeginUpdate; if Assigned(AContratos) then begin FController.CambiarSituacion(AContratos, ASituacion, AFecha, AllItems); actRefrescar.Execute; end; ViewGrid._Grid.EndUpdate; end; end; constructor TfEditorContratosCliente.Create(AOwner: TComponent); begin inherited; // FHeaderText := 'Lista de Contratos de cliente'; // FWindowCaption := FHeaderText; AsignarVista; end; destructor TfEditorContratosCliente.Destroy; begin FContratos := NIL; FController := NIL; inherited; end; procedure TfEditorContratosCliente.DuplicarInterno; var APresupuesto : IBizContratoCliente; begin inherited; APresupuesto := FController.Duplicar(Contratos); try FController.Ver(APresupuesto); finally actRefrescar.Execute; end; end; procedure TfEditorContratosCliente.EliminarInterno; var AContratos: IBizContratoCliente; AllItems: Boolean; begin AContratos := Nil; AllItems := False; if MultiSelect and Assigned(ViewGrid) then AllItems := (ViewGrid.NumSeleccionados > 1); if AllItems then begin if (Application.MessageBox('¿Desea borrar los Contratos seleccionados?', 'Atención', MB_YESNO) = IDYES) then begin SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, (Contratos as ISeleccionable).SelectedRecords); AContratos := (Controller as IContratosClienteController).ExtraerSeleccionados(Contratos) as IBizContratoCliente; end end else begin if (Application.MessageBox('¿Desea borrar el presupuesto seleccionado?', 'Atención', MB_YESNO) = IDYES) then AContratos := Contratos; end; if Assigned(AContratos) then begin FController.Eliminar(AContratos, AllItems); if AllItems then begin if (AContratos.DataTable.RecordCount > 0) then begin with AContratos.DataTable do begin First; while not EOF do begin JsListaContratosNoEliminados.Content.Add('Ref. Presupuesto: ' + AContratos.REFERENCIA + ' ' + AContratos.NOMBRE); Next; end; end; JsListaContratosNoEliminados.Execute; end; actRefrescar.Execute; end; end; ViewGrid.GotoFirst; end; procedure TfEditorContratosCliente.FormShow(Sender: TObject); begin cbxListaAnos.OnChange := OnListaAnosChange; //OJO SIEMPRE ANTES DEL INHERITED inherited; if not Assigned(ViewGrid) then raise Exception.Create('No hay ninguna vista asignada'); if not Assigned(Contratos) then raise Exception.Create('No hay ningún Presupuesto asignado'); Contratos.DataTable.Active := True; ViewGrid.GotoFirst; end; function TfEditorContratosCliente.GetController: IContratosClienteController; begin Result := FController; end; function TfEditorContratosCliente.GetContratos: IBizContratoCliente; begin Result := FContratos; end; procedure TfEditorContratosCliente.ImprimirInterno; var Respuesta : Integer; AContratos: IBizContratoCliente; AllItems: Boolean; FVerCondiciones : Boolean; begin AContratos := Nil; AllItems := False; FVerCondiciones := False; if MultiSelect and Assigned(ViewGrid) then AllItems := (ViewGrid.NumSeleccionados > 1); //Si esta agrupado solo podré imprimir la lista de elementos visibles if not ViewGrid.esSeleccionCeldaDatos then inherited else begin Respuesta := JsImprimirDialog.Execute; if Respuesta <> IDCANCEL then begin case JsImprimirDialog.CustomButtonResult of 200 : begin // Lista de elementos visibles inherited; end; 100 : begin // Elemento Seleccionado if AllItems then begin SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, (Contratos as ISeleccionable).SelectedRecords); AContratos := (Controller as IContratosClienteController).ExtraerSeleccionados(Contratos) as IBizContratoCliente; end else AContratos := Contratos; if Assigned(AContratos) then FVerCondiciones := ShowConfirmMessage('Previsualizar presupuesto', '¿Desea previsualizar Montadores/Enganches?') = IDYES; FController.Print(AContratos, AllItems, FVerCondiciones); end; end; end; end; end; procedure TfEditorContratosCliente.ModificarInterno; begin inherited; if (Length(Trim(Contratos.FACTURA_ASOCIADA)) > 0) then begin if (ShowConfirmMessage('¡Atención!, el presupuesto tiene al menos una factura asociada, "no debería modificar nada", antes hablar con Mónica.', '¿Desea continuar?') = IDYES) then FController.Ver(Contratos); end else FController.Ver(Contratos); end; procedure TfEditorContratosCliente.NuevoInterno; begin inherited; if FController.Anadir(Contratos) then FController.Ver(Contratos); end; procedure TfEditorContratosCliente.OnListaAnosChange(Sender: TObject; const Text: string); begin Controller.FiltrarAno(Contratos, DynWhereDataTable, Text); if Contratos.DataTable.Active then RefrescarInterno; end; procedure TfEditorContratosCliente.PonerTitulos(const ATitulo: string); var FTitulo : String; begin if (ATitulo = '') then FTitulo := 'Lista de presupuestos de cliente - ' + AppFactuGES.EmpresaActiva.NOMBRE else FTitulo := ATitulo; inherited PonerTitulos(FTitulo); end; procedure TfEditorContratosCliente.PrevisualizarInterno; var Respuesta : Integer; AContratos: IBizContratoCliente; AllItems: Boolean; FVerCondiciones : Boolean; begin AContratos := Nil; AllItems := False; FVerCondiciones := False; if MultiSelect and Assigned(ViewGrid) then AllItems := (ViewGrid.NumSeleccionados > 1); //Si esta agrupado solo podré imprimir la lista de elementos visibles if not ViewGrid.esSeleccionCeldaDatos then inherited else begin Respuesta := JsPrevisualizarDialog.Execute; if Respuesta <> IDCANCEL then begin case JsPrevisualizarDialog.CustomButtonResult of 200 : begin // Lista de elementos visibles inherited; end; 100 : begin // Elemento Seleccionado if AllItems then begin SeleccionarFilasDesdeGrid(ViewGrid._FocusedView, (Contratos as ISeleccionable).SelectedRecords); AContratos := (Controller as IContratosClienteController).ExtraerSeleccionados(Contratos) as IBizContratoCliente; end else AContratos := Contratos; if Assigned(AContratos) then FVerCondiciones := ShowConfirmMessage('Previsualizar presupuesto', '¿Desea previsualizar Montadores/Enganches?') = IDYES; FController.Preview(AContratos, AllItems, FVerCondiciones); end; end; end; end; AContratos := Nil; end; procedure TfEditorContratosCliente.RefrescarInterno; begin //Volvemos a cargar los años de los Contratos if Assigned(FController) then ListaAnos := FController.DarListaAnosContratos; inherited; end; procedure TfEditorContratosCliente.SetController(const Value: IContratosClienteController); begin FController := Value; if Assigned(FController) then ListaAnos := FController.DarListaAnosContratos; end; procedure TfEditorContratosCliente.SetContratos(const Value: IBizContratoCliente); begin FContratos := Value; if Assigned(FContratos) then begin //Se guarda el where de la sentencia origen, por si el editor tiene filtros que //afecten a este where y en un futuro se desea volver al where origen (filtro de año)) DynWhereDataTable := FContratos.DataTable.DynamicWhere.Xml; dsDataTable.DataTable := FContratos.DataTable; if Assigned(ViewGrid) then (ViewGrid as IViewContratosCliente).Contratos := Contratos; end; end; end.