{
===============================================================================
  Copyright (©) 2005. Rodax Software.
===============================================================================
  Los contenidos de este fichero son propiedad de Rodax Software titular del
  copyright. Este fichero sólo podrá ser copiado, distribuido y utilizado,
  en su totalidad o en parte, con el permiso escrito de Rodax Software, o de
  acuerdo con los términos y condiciones establecidas en el acuerdo/contrato
  bajo el que se suministra.
  -----------------------------------------------------------------------------
  Web:  www.rodax-software.com
===============================================================================
  Fecha primera versión: 08-06-2004
  Versión actual: 1.0.0
  Fecha versión actual: 08-06-2004
===============================================================================
  Modificaciones:

    Fecha         Comentarios
    ---------------------------------------------------------------------------
===============================================================================
}

unit Obras;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids, DBGrids, ComCtrls, RdxBotones, ToolWin, ExtCtrls,
  Menus, am2000menuitem, am2000popupmenu, am2000, Db, DBTables,
  RXDBCtrl, RdxPaneles, RdxBarras, TablaClientes,
  RdxDBFrame, RdxFrameObras, RdxFrame, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxEdit, cxDBData, cxGridTableView,
  cxGridCustomTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, Configuracion, StdCtrls, Buttons,
  AdvPanel, cxContainer, cxTextEdit,
  cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, ActnList, am2000utils, cxDataStorage, cxDBEdit,
  cxSplitter, CalendarioPagos;

type
  TfrObras = class(TRdxFrameObras)
    dsObras: TDataSource;
    brSeleccion: TRdxBarraInferior;
    bSeleccionar: TRdxBoton;
    bCancelar: TRdxBoton;
    BarraClientes: TRdxBarraSuperior;
    bAnadir: TRdxBotonSuperior;
    bEliminar: TRdxBotonSuperior;
    gridObrasDBTableView1: TcxGridDBTableView;
    gridObrasLevel1: TcxGridLevel;
    gridObras: TcxGrid;
    pnlCuerpo: TPanel;
    Acciones: TActionList;
    actAnadir: TAction;
    actModificar: TAction;
    actEliminar: TAction;
    actConsultar: TAction;
    actSeleccionar: TAction;
    actCancelar: TAction;
    actImprimir: TAction;
    imgSombra: TImage;
    pnlExtra: TRdxBarraSuperior;
    pnlCuerpo2: TRdxPanel;
    bRefrescar: TRdxBoton;
    actConsultarCobros: TAction;
    pnlBusqueda: TPanel;
    Buscar: TcxTextEdit;
    bLimpiar: TRdxBoton;
    eNombre: TLabel;
    actAbrirObra: TAction;
    actCerrarObra: TAction;
    RdxBoton1: TRdxBoton;
    RdxBoton2: TRdxBoton;
    bImprimir: TRdxBotonSuperior;
    pnlObra: TPanel;
    cxSplitter1: TcxSplitter;
    RdxBotonSuperior1: TRdxBotonSuperior;
    actVistaPrevia: TAction;
    procedure cxLookupComboBox1PropertiesEditValueChanged(Sender: TObject);
    procedure actSeleccionarExecute(Sender: TObject);
    procedure actCancelarExecute(Sender: TObject);
    procedure actRefrescarDatosExecute(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure bLimpiarClick(Sender: TObject);
    procedure RdxFrameObrasShow(Sender: TObject);

    procedure actAnadirExecute(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure gridObrasDBTableView1DblClick(Sender: TObject);
    procedure actAbrirObraExecute(Sender: TObject);
    procedure actCerrarObraExecute(Sender: TObject);
    procedure gridObrasDBTableView1CustomDrawCell(
      Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
      AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure actCerrarObraUpdate(Sender: TObject);
    procedure actAbrirObraUpdate(Sender: TObject);
    procedure gridObrasDBTableView1FocusedRecordChanged(
      Sender: TcxCustomGridTableView; APrevFocusedRecord,
      AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure actImprimirUpdate(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
    procedure actVistaPreviaExecute(Sender: TObject);
  private
    FCodigoContrato : Variant;
    FFrameObra : TRdxFrameObras;
    procedure ActualizarBotones;
  protected
    procedure BuscarObra; override;
    procedure FreeContenido; override;
    procedure SetContenido (NuevoFrame : TRdxFrame); override;
    procedure VerModal; override;
  public
    constructor Create (AOwner : TComponent); override;
    destructor Destroy; override;
  published
    property TablaObras;
    property Entidad;
  end;

var
  frObras: TfrObras;

implementation
{$R *.DFM}
uses
  IBCustomDataSet, BaseDatos, Entidades, Cliente, Variants, ListadoClientes,
  ListadoPagosCliente, TablaFacturasCliente, TablaPagosCliente, Mensajes, Literales,
  TablaObras, RdxFrameContratos, ContratosClientes, strFunc, CerrarObra, Constantes;

constructor TfrObras.Create (AOwner : TComponent);
begin
  inherited Create(AOwner);
  FCodigoContrato := Null;
  Entidad := entObraCliente;

  BaseDatos := dmBaseDatos.BD;
  Transaccion := dmBaseDatos.Transaccion;
  TablaObras := TIBDataSet.Create(Self);
  dsObras.DataSet := TablaObras;

  with TablaObras do
  begin
    DisableControls;
    Database := BaseDatos;
    Transaction := Transaccion;
    SelectSQL.Assign(dmTablaObras.sqlGrid);
    Prepare;
    Open;
    ActualizarBotones;
    EnableControls;
  end;

  dmTablaObras.InicializarGridObras(gridObrasDBTableView1);
  gridObrasDBTableView1.Controller.GoToFirst;
  FCodigoObra := TablaObras.fieldbyname('CODIGOCONTRATO').AsString;

  gridObras.Height := (pnlCuerpo.Height - pnlBusqueda.Height - cxSplitter1.Height) div 2;
  FFrameObra := TfrObra2.Create(Self);
  FFrameObra.Parent := pnlObra;
  FFrameObra.CodigoObra := CodigoObra;
end;

procedure TfrObras.BuscarObra;
begin
  with TablaObras do
  begin
    DisableControls;
    Close;
    Open;
    ActualizarBotones;
    EnableControls;
    if not Locate('CODIGOCONTRATO', CodigoObra, []) then
      gridObrasDBTableView1.Controller.GoToFirst
    else
      gridObrasDBTableView1.Controller.TopRowIndex := gridObrasDBTableView1.Controller.FocusedRowIndex;
    FFrameObra.CodigoObra := CodigoObra;
  end;
end;

destructor TfrObras.Destroy;
begin
  FFrameObra.CloseFrame;
  FreeAndNil(FFrameObra);
  
  TablaObras.Close;
  TablaObras.Unprepare;
  TablaObras.Free;
  inherited;
end;

procedure TfrObras.FreeContenido;
begin
  if (ContenidoModal is TRdxFrameContratos) then
    FCodigoContrato := (ContenidoModal as TRdxFrameContratos).CodigoContrato;

  if (Contenido is TRdxFrameObras) then
    CodigoObra := (Contenido as TRdxFrameObras).CodigoObra;

  inherited FreeContenido;
  gridObras.SetFocus;
end;

procedure TfrObras.ActualizarBotones;
begin
  if BaseDatos.IsReadOnly then
  begin
    actAnadir.Enabled := False;
    actModificar.Enabled := False;
    actEliminar.Enabled := False;
    actConsultar.Enabled := True;
    actImprimir.Enabled := True;
    Exit;
  end;

  if TablaObras.RecordCount = 0 then
  begin
    actAnadir.Enabled := True;
    actModificar.Enabled := False;
    actEliminar.Enabled := False;
    actConsultar.Enabled := False;
    actImprimir.Enabled := False;
  end
  else begin
    actAnadir.Enabled := True;
    actModificar.Enabled := True;
    actEliminar.Enabled := True;
    actConsultar.Enabled := True;
    actImprimir.Enabled := True;
  end;
end;

procedure TfrObras.cxLookupComboBox1PropertiesEditValueChanged(
  Sender: TObject);
begin
  TablaObras.Locate('CODIGOCONTRATO', (Sender as TcxLookupComboBox).EditValue, [loCaseInsensitive]);
end;

procedure TfrObras.actAnadirExecute(Sender: TObject);
begin
  // Mostrar la lista de contratos sin obras abiertas
  CaptionModal := msgObrElijaContrago;
  ContenidoModal := TfrContratosClientes.Create(Self);

  if (not EsCadenaVacia(FCodigoContrato)) then
    if dmTablaObras.CrearObra(FCodigoContrato) then
    begin
      Commit;
      VerMensajeFmt(msgObrAnadida,[FCodigoContrato]);
    end;
    
  CodigoObra := FCodigoContrato;
end;

procedure TfrObras.actEliminarExecute(Sender: TObject);
begin
  (FFrameObra as TfrObra2).EliminarDatos;
  BuscarObra;
end;

procedure TfrObras.gridObrasDBTableView1DblClick(Sender: TObject);
begin
{  if Modo = Seleccionar then
    actSeleccionar.Execute
  else
    actModificar.Execute;}
end;

procedure TfrObras.actSeleccionarExecute(Sender: TObject);
begin
  CodigoObra := TablaObras.FieldByName('CODIGOCONTRATO').AsString;
  CloseFrame;
end;

procedure TfrObras.actCancelarExecute(Sender: TObject);
begin
  FCodigoObra := NULL;
  CloseFrame;
end;

procedure TfrObras.actImprimirExecute(Sender: TObject);
begin
  (FFrameObra as TfrObra2).Imprimir;
end;

procedure TfrObras.actRefrescarDatosExecute(Sender: TObject);
begin
  FCodigoObra := TablaObras.FieldByName('CODIGOCONTRATO').AsString;
  BuscarObra;
end;

procedure TfrObras.SetContenido(NuevoFrame: TRdxFrame);
begin
  inherited;
end;

procedure TfrObras.Edit1Change(Sender: TObject);
begin
  FiltrarGrid(gridObras, Buscar.Text);
end;

procedure TfrObras.bLimpiarClick(Sender: TObject);
begin
  Buscar.Text := '';
end;

procedure TfrObras.RdxFrameObrasShow(Sender: TObject);
begin
  gridObras.SetFocus;
end;

procedure TfrObras.VerModal;
begin
  if (ContenidoModal is TRdxFrameContratos) then
    (ContenidoModal as TRdxFrameContratos).Modo := AbrirObra;
  inherited;
end;

procedure TfrObras.actAbrirObraExecute(Sender: TObject);
begin
  if dmTablaObras.AbrirObra(TablaObras.FieldByName('CODIGOCONTRATO').AsString) then
     Commit;
  //Refrescamos   
  CodigoObra := TablaObras.FieldByName('CODIGOCONTRATO').AsString;
end;

procedure TfrObras.actCerrarObraExecute(Sender: TObject);
var
  fCerrarObra : TfrCerrarObra;
begin
  fCerrarObra := TfrCerrarObra.Create(Self);
  fCerrarObra.CodigoObra := TablaObras.FieldByName('CODIGOCONTRATO').AsString;
  fCerrarObra.ShowModal;
  CodigoObra := TablaObras.FieldByName('CODIGOCONTRATO').AsString;
end;

procedure TfrObras.gridObrasDBTableView1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  IndiceCol : Integer;
begin
  if not AViewInfo.Selected then
  begin
    IndiceCol := (Sender as TcxGridDBTableView).GetColumnByFieldName('FECHAFIN').Index;
    if not esCadenaVacia(AViewInfo.GridRecord.DisplayTexts[IndiceCol]) then
      ACanvas.Canvas.Font.Color := clGray;
  end;
end;

procedure TfrObras.actCerrarObraUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := esCadenaVacia(TablaObras.FieldByName('FECHAFIN').AsString);
end;

procedure TfrObras.actAbrirObraUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := not esCadenaVacia(TablaObras.FieldByName('FECHAFIN').AsString);
end;

procedure TfrObras.gridObrasDBTableView1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  if Assigned(FFrameObra) then
  begin
     FFrameObra.CodigoObra := TablaObras.FieldByName('CODIGOCONTRATO').AsString;
  end;
end;

procedure TfrObras.actImprimirUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (TablaObras.RecordCount > 0);
end;

procedure TfrObras.actEliminarUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled := (TablaObras.RecordCount > 0);
  pnlObra.Visible := (Sender as TAction).Enabled;
  if pnlObra.Visible then
    pnlObra.Height := (Self.Height div 2)
  else
    pnlObra.Height := 0;
end;

procedure TfrObras.actVistaPreviaExecute(Sender: TObject);
begin
  (FFrameObra as TfrObra2).VistaPrevia;
end;

end.
