unit uEditorObrasFinal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorObras, dxLayoutLookAndFeels, Menus, DB, uDADataTable,
  JvAppStorage, JvAppRegistryStorage, JvComponentBase, JvFormPlacement,
  ImgList, PngImageList, StdActns, ActnList, uViewGrid, uCustomView, uViewBase,
  uViewBarraSeleccion, ComCtrls, TB2ExtItems, TBXExtItems, TBX, TB2Item, TB2Dock,
  TB2Toolbar, ExtCtrls, JvExControls, JvComponent, JvNavigationPane, cxGridCustomTableView,
  uViewObras, uViewProyectosObra, uViewFacturasCliente, uViewFacturasObra,
  uViewRecibosObra, pngimage;

type
  IEditorObrasFinal = interface(IEditorObras)
  ['{361560A6-C86F-4C2A-AAA1-D006C8CB1E70}']
  end;

  TfEditorObrasFinal = class(TfEditorObras, IEditorObrasFinal)
    pagFacturas: TTabSheet;
    frViewFacturasObra1: TfrViewFacturasObra;
    pagRecibos: TTabSheet;
    frViewRecibosObra1: TfrViewRecibosObra;
    procedure frViewBarraSeleccionactSeleccionarExecute(Sender: TObject);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);
    procedure FocusedRecordChange(Sender: TcxCustomGridTableView; APrevFocusedRecord,
      AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure FormShow(Sender: TObject);
    procedure paginasChange(Sender: TObject);

    procedure frViewFacturasObra1actAnadirExecute(Sender: TObject);
    procedure frViewFacturasObra1actModificarExecute(Sender: TObject);
    procedure frViewFacturasObra1actEliminarExecute(Sender: TObject);
    procedure frViewFacturasObra1actEliminarTodoExecute(Sender: TObject);
    procedure frViewFacturasObra1actDuplicarExecute(Sender: TObject);
    procedure frViewFacturasObra1actAnadirUpdate(Sender: TObject);
    procedure frViewFacturasObra1cxGridViewDataControllerDataChanged(Sender: TObject);

    procedure frViewRecibosObra1actAnadirExecute(Sender: TObject);
    procedure frViewRecibosObra1actAnadirUpdate(Sender: TObject);
    procedure frViewRecibosObra1actEditarExecute(Sender: TObject);
    procedure frViewRecibosObra1actEliminarExecute(Sender: TObject);
    procedure frViewRecibosObra1actEliminarTodoExecute(Sender: TObject);
    procedure frViewRecibosObra1actDuplicarExecute(Sender: TObject);
    procedure frViewRecibosObra1cxGridViewDataControllerDataChanged(Sender: TObject);

    procedure frViewProyectosObracxGridViewCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure frViewFacturasObra1cxGridViewCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure frViewRecibosObra1cxGridViewCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);

  protected
    procedure SetSelectionBarVisible(const Value: Boolean); override;
  end;

implementation
{$R *.dfm}

uses
  uEditorUtils, uDBSelectionList, uBizObraFinal, uDataModuleObrasFinal,
  LiteralesObrasFinal;

function ShowEditorObrasFinal (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorObrasFinal;
begin
  AEditor := TfEditorObrasFinal.Create(Application);
  try
    AEditor.Obras := (ABizObject as IBizObraFinal);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;

function ShowSelectEditorObrasFinal (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorObrasFinal;
begin
  AEditor := TfEditorObrasFinal.Create(Application);
  try
    AEditor.Obras := (ABizObject as IBizObraFinal);
    AEditor.SelectionBarVisible := True;
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;

procedure TfEditorObrasFinal.frViewBarraSeleccionactSeleccionarExecute(Sender: TObject);
begin
  inherited;
  (Obras as ISelectedRowList).SelectedRows.CurrentRowSelected := True;
  ModalResult := mrOK;
end;

procedure TfEditorObrasFinal.CustomEditorClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  frViewObras.cxGridView.OnFocusedRecordChanged := Nil;

  if ModalResult <> mrOk then
     (Obras as ISelectedRowList).SelectedRows.Clear;
end;

procedure TfEditorObrasFinal.SetSelectionBarVisible(const Value: Boolean);
begin
  inherited;
  if Value then
  begin
     frViewObras.Grid.ActiveView.OnDblClick := frViewBarraSeleccion.actSeleccionar.OnExecute;
     paginas.Visible := False;
  end;
end;

procedure TfEditorObrasFinal.FocusedRecordChange(Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  inherited;
  if (Obras.DataTable.State = dsBrowse) then
      paginasChange(Self);
end;

procedure TfEditorObrasFinal.FormShow(Sender: TObject);
begin
  inherited;
  frViewObras.cxGridView.OnFocusedRecordChanged := FocusedRecordChange;
  Paginas.ActivePage := pagProyectos;
end;

procedure TfEditorObrasFinal.paginasChange(Sender: TObject);
begin
  if (paginas.ActivePageIndex = pagFacturas.PageIndex) then
  begin
     frViewFacturasObra1.Facturas := (Obras as IBizObraFinal).Facturas;
     frViewFacturasObra1.setFiltro(Obras.CODIGO);
  end
  else if (paginas.ActivePageIndex = pagRecibos.PageIndex) then
  begin
     frViewRecibosObra1.Recibos := (Obras as IBizObraFinal).Recibos;
     frViewRecibosObra1.setFiltro(Obras.CODIGO);
  end;
end;

procedure TfEditorObrasFinal.frViewFacturasObra1actAnadirExecute(Sender: TObject);
begin
  inherited;
  (Obras as IBizObraFinal).Facturas.Insert;
  (Obras as IBizObraFinal).Facturas.Obra := dmObrasFinal.GetObraItem(Obras.CODIGO);
  (Obras as IBizObraFinal).Facturas.Show;
end;

procedure TfEditorObrasFinal.frViewFacturasObra1actModificarExecute(Sender: TObject);
begin
  inherited;
  (Obras as IBizObraFinal).Facturas.Show;
end;

procedure TfEditorObrasFinal.frViewFacturasObra1actEliminarExecute(Sender: TObject);
begin
  if (MessageDlg('żEsta seguro de desear borrar esta factura?', mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
  begin
     (Obras as IBizObraFinal).Facturas.Delete;
     (Obras as IBizObraFinal).Facturas.DataTable.ApplyUpdates;
     frViewFacturasObra1.RefreshGrid;
     frViewFacturasObra1.GotoFirst;
  end;
end;

procedure TfEditorObrasFinal.frViewFacturasObra1actAnadirUpdate(Sender: TObject);
begin
  if Assigned(dsDataTable.DataTable) then
     (Sender as TAction).Enabled := not dsDataTable.DataTable.IsEmpty
  else
     (Sender as TAction).Enabled := False;
end;

procedure TfEditorObrasFinal.frViewFacturasObra1cxGridViewDataControllerDataChanged(Sender: TObject);
begin
  inherited;
  if not (Obras.DataTable.Fetching)
  or not (Obras.DataTable.Opening)
  or not (Obras.DataTable.Closing) then
    frViewFacturasObra1.JvNavPanelHeader.Caption := 'Lista de facturas de la obra ' + Obras.NOMBRE;
end;

procedure TfEditorObrasFinal.frViewRecibosObra1actAnadirExecute(Sender: TObject);
begin
  (Obras as IBizObraFinal).Recibos.Insert;
  (Obras as IBizObraFinal).Recibos.Obra := dmObrasFinal.GetObraItem(Obras.CODIGO);
  (Obras as IBizObraFinal).Recibos.Show;
end;

procedure TfEditorObrasFinal.frViewRecibosObra1actAnadirUpdate(Sender: TObject);
begin
  if Assigned(dsDataTable.DataTable) then
     (Sender as TAction).Enabled := not dsDataTable.DataTable.IsEmpty
  else
     (Sender as TAction).Enabled := False;
end;

procedure TfEditorObrasFinal.frViewRecibosObra1actEditarExecute(Sender: TObject);
begin
  inherited;
  (Obras as IBizObraFinal).Recibos.Show;
end;

procedure TfEditorObrasFinal.frViewFacturasObra1actEliminarTodoExecute(Sender: TObject);
begin
  if (MessageDlg(msgLitBorrarTFactura, mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
  begin
     frViewFacturasObra1.Facturas.DataTable.ClearRows;
     frViewFacturasObra1.Facturas.DataTable.ApplyUpdates;
     frViewFacturasObra1.RefreshGrid;
  end;
end;

procedure TfEditorObrasFinal.frViewFacturasObra1actDuplicarExecute(Sender: TObject);
var
  AFactura : IBizFacturasObras;
begin
  inherited;

  AFactura := dmObrasFinal.GetFacturasItem(frViewFacturasObra1.Facturas.CODIGO);
  if not AFactura.DataTable.Active then
     AFactura.DataTable.Active := True;

  frViewFacturasObra1.Facturas.Insert;
  frViewFacturasObra1.Facturas.CopyFrom(AFactura);
  frViewFacturasObra1.Facturas.DataTable.ApplyUpdates;
end;

procedure TfEditorObrasFinal.frViewProyectosObracxGridViewCellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  frViewProyectosObra.actModificar.Execute;
end;

procedure TfEditorObrasFinal.frViewFacturasObra1cxGridViewCellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  frViewFacturasObra1.actModificar.Execute;
end;

procedure TfEditorObrasFinal.frViewRecibosObra1cxGridViewCellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  frViewRecibosObra1.actEditar.Execute;
end;

procedure TfEditorObrasFinal.frViewRecibosObra1actDuplicarExecute(Sender: TObject);
var
  ARecibo : IBizRecibosObras;
begin
  inherited;

  ARecibo := dmObrasFinal.GetRecibosItem(frViewRecibosObra1.Recibos.CODIGO);
  if not ARecibo.DataTable.Active then
     ARecibo.DataTable.Active := True;

  frViewRecibosObra1.Recibos.Insert;
  frViewRecibosObra1.Recibos.CopyFrom(ARecibo);
  frViewRecibosObra1.Recibos.DataTable.ApplyUpdates;
end;

procedure TfEditorObrasFinal.frViewRecibosObra1actEliminarExecute(Sender: TObject);
begin
  if (MessageDlg('żEsta seguro de desear borrar este recibo?', mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
  begin
     (Obras as IBizObraFinal).Recibos.Delete;
     (Obras as IBizObraFinal).Recibos.DataTable.ApplyUpdates;
     frViewRecibosObra1.RefreshGrid;
     frViewRecibosObra1.GotoFirst;
  end;
end;

procedure TfEditorObrasFinal.frViewRecibosObra1actEliminarTodoExecute(Sender: TObject);
begin
  if (MessageDlg(msgLitBorrarTRecibo, mtConfirmation, [mbYes, mbNo], 0) = mrYes) then
  begin
     frViewRecibosObra1.Recibos.DataTable.ClearRows;
     frViewRecibosObra1.Recibos.DataTable.ApplyUpdates;
     frViewRecibosObra1.RefreshGrid;
  end;
end;

procedure TfEditorObrasFinal.frViewRecibosObra1cxGridViewDataControllerDataChanged(
  Sender: TObject);
begin
  inherited;
  if not (Obras.DataTable.Fetching)
  or not (Obras.DataTable.Opening)
  or not (Obras.DataTable.Closing) then
    frViewRecibosObra1.JvNavPanelHeader.Caption := 'Lista de recibos de la obra ' + Obras.NOMBRE;
end;

initialization
  RegisterEditor(IBizObraFinal, ShowEditorObrasFinal, etItems);
  RegisterEditor(IBizObraFinal, ShowSelectEditorObrasFinal, etSelectItem);

end.
