unit uViewObra; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, uViewBase, ExtCtrls, StdCtrls, Buttons, DB, uDADataTable, DBCtrls, Grids, DBGrids, uBizObras, Mask, ComCtrls, uCustomView, JvComponent, JvFormAutoSize, cxControls, cxContainer, cxEdit, cxTextEdit, cxDBEdit, dxLayoutControl, uDAInterfaces, uViewDatosYSeleccionCliente, cxMemo, uViewDatosYSeleccionProveedor, uViewSubcontrataObra, uViewClienteAsociadoObra, cxGraphics, cxMaskEdit, cxDropDownEdit, uObrasController; type IViewObra = interface(IViewBase) ['{D8669267-CAC9-468E-9923-2D3B2DD30CED}'] function GetObra: IBizObra; procedure SetObra(const Value: IBizObra); property Obra: IBizObra read GetObra write SetObra; function GetController : IObrasController; procedure SetController (const Value : IObrasController); property Controller: IObrasController read GetController write SetController; end; TfrViewObra = class(TfrViewBase, IViewObra) DADataSource: TDADataSource; ePersonaContacto: TcxDBTextEdit; eTlfTelefono: TcxDBTextEdit; eTlfMovil: TcxDBTextEdit; eFax: TcxDBTextEdit; dxLayoutControlObraGroup_Root: TdxLayoutGroup; dxLayoutControlObra: TdxLayoutControl; dxLayoutControlObraGroup1: TdxLayoutGroup; dxLayoutControlObraItem3: TdxLayoutItem; eNombre: TcxDBTextEdit; dxLayoutControlObraItem4: TdxLayoutItem; dxLayoutControlObraItem1: TdxLayoutItem; dxLayoutControlObraItem5: TdxLayoutItem; dxLayoutControlObraItem6: TdxLayoutItem; dxLayoutControlObraGroup3: TdxLayoutGroup; dxLayoutControlObraItem10: TdxLayoutItem; eCalle: TcxDBTextEdit; dxLayoutControlObraItem2: TdxLayoutItem; cbPoblacion: TcxDBComboBox; dxLayoutControlObraItem7: TdxLayoutItem; dxLayoutControlObraGroup5: TdxLayoutGroup; eCodigoPostal: TcxDBTextEdit; dxLayoutControlObraItem9: TdxLayoutItem; cbProvincia: TcxDBComboBox; dxLayoutControlObraGroup7: TdxLayoutGroup; dxLayoutControlObraGroup4: TdxLayoutGroup; dxLayoutControlObraItem11: TdxLayoutItem; eObservaciones: TcxDBMemo; dxLayoutControlObraGroup2: TdxLayoutGroup; dxLayoutControlObraGroup8: TdxLayoutGroup; frViewClienteAsociadoObra1: TfrViewClienteAsociadoObra; dxLayoutControlObraItem12: TdxLayoutItem; dxLayoutControlObraItem13: TdxLayoutItem; dxLayoutControlObraGroup11: TdxLayoutGroup; procedure cbPoblacionPropertiesInitPopup(Sender: TObject); procedure cbProvinciaPropertiesInitPopup(Sender: TObject); protected FObra: IBizObra; FController : IObrasController; FProvincias : TStringList; FIDProvincia : Integer; //Almacenará la provincia que hay seleccionada para no cargar las poblaciones si no es necesario FPoblaciones : TStringList; procedure CargarProvincias; procedure CargarPoblaciones; function GetObra: IBizObra; procedure SetObra(const Value: IBizObra); procedure OnClienteChanged(Sender : TObject); function GetController : IObrasController; procedure SetController (const Value : IObrasController); virtual; public constructor Create(AOwner : TComponent); override; destructor Destroy; override; property Obra: IBizObra read GetObra write SetObra; property Controller: IObrasController read GetController write SetController; end; implementation uses uDataModuleObras, uProvinciasPoblacionesController, uStringsUtils, uClientesController, uBizDireccionesContacto, uDialogUtils; {$R *.dfm} { TfrViewObras } { ******************************* TfrViewObras ******************************** } procedure TfrViewObra.CargarPoblaciones; var i : integer; begin if (FIDProvincia <> StrToInt(FProvincias.Values[cbProvincia.Text])) then Begin FIDProvincia := StrToInt(FProvincias.Values[cbProvincia.Text]); with TProvinciasPoblacionesController.Create do try FPoblaciones := DarListaPoblaciones(FIDProvincia); with cbPoblacion.Properties.Items do begin BeginUpdate; try Clear; for i := 0 to FPoblaciones.Count - 1 do Add(FPoblaciones.Names[i]); finally EndUpdate; end; end; finally Free; end; End; end; procedure TfrViewObra.CargarProvincias; var i : integer; begin with TProvinciasPoblacionesController.Create do try FProvincias := DarListaProvincias; with cbProvincia.Properties.Items do begin BeginUpdate; try Clear; for i := 0 to FProvincias.Count - 1 do Add(FProvincias.Names[i]); finally EndUpdate; end; end; finally Free; end; end; procedure TfrViewObra.cbPoblacionPropertiesInitPopup(Sender: TObject); begin ShowHourglassCursor; try FreeANDNIL(FPoblaciones); if not Assigned(FProvincias) then CargarProvincias; if not EsCadenaVacia(cbProvincia.Text) and (FProvincias.IndexOfName(cbProvincia.Text) <> -1) then CargarPoblaciones finally HideHourglassCursor; end; end; procedure TfrViewObra.cbProvinciaPropertiesInitPopup(Sender: TObject); begin ShowHourglassCursor; try if not Assigned(FProvincias) then CargarProvincias; finally HideHourglassCursor; end; end; constructor TfrViewObra.Create(AOwner: TComponent); begin inherited; FIDProvincia := 0; FProvincias := NIL; FPoblaciones := NIL; end; destructor TfrViewObra.Destroy; begin if Assigned(FProvincias) then FreeANDNIL(FProvincias); if Assigned(FPoblaciones) then FreeANDNIL(FPoblaciones); FObra := Nil; FController := Nil; inherited; end; function TfrViewObra.GetController: IObrasController; begin Result := FController; end; function TfrViewObra.GetObra: IBizObra; begin Result := FObra; end; procedure TfrViewObra.OnClienteChanged(Sender: TObject); var AClientesController : IClientesController; ADireccion : IBizDireccionesContacto; begin if Assigned(FObra) then begin FObra.Edit; FObra.ID_CLIENTE := frViewClienteAsociadoObra1.ID_Cliente; AClientesController := TClientesController.Create; try { // Esto podría ser configurable en el programa if not AClientesController.TieneDatosBancarios(FPresupuesto.Cliente) then MessageBox(0, 'Este cliente no tiene datos bancarios en su ficha', 'Atención', MB_ICONWARNING or MB_OK);} case frViewClienteAsociadoObra1.Cliente.Direcciones.RecordCount of 0 : begin FController.CopiarDireccion(frViewClienteAsociadoObra1.Cliente, FObra); ShowInfoMessage('Dirección de la obra actualizada.', 'Se ha actualizado los datos de la dirección de la obra con los datos del cliente.'); end; else begin ADireccion := AClientesController.ElegirDireccionEntrega(frViewClienteAsociadoObra1.Cliente, ''); ShowHourglassCursor; try if Assigned(ADireccion) then FController.CopiarDireccion(ADireccion, FObra); finally HideHourglassCursor; ADireccion := NIL; end; end; end; finally AClientesController := NIL; end; end; end; procedure TfrViewObra.SetController(const Value: IObrasController); begin FController := Value; end; procedure TfrViewObra.SetObra(const Value: IBizObra); begin FObra := Value; frViewClienteAsociadoObra1.OnClienteChanged := NIL; if Assigned(FObra) then begin DADataSource.DataTable := FObra.DataTable; if not FObra.ID_CLIENTEIsNull then frViewClienteAsociadoObra1.ID_Cliente := FObra.ID_CLIENTE; frViewClienteAsociadoObra1.OnClienteChanged := OnClienteChanged; end else DADataSource.DataTable := NIL; end; end.