unit uEditorDiario;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorBase, ToolWin, ComCtrls, JvExControls, JvComponent,
  JvNavigationPane, uViewDiario, uBizAsientos, ActnList, DBActns, uViewGrid,
  Menus, uDataModuleBase, ImgList, PngImageList, TB2Dock, TB2Toolbar, TBX,
  TB2Item, StdActns, TB2ExtItems, TBXExtItems, TB2MRU, DB, uDADataTable,
  JvFormAutoSize, uDAScriptingProvider, uDACDSDataTable, JvAppStorage,
  JvAppRegistryStorage, JvFormPlacement, ExtCtrls, uCustomView, uViewBase,
  uViewBarraSeleccion, pngimage, uIEditorDiario, uEditorGridBase,
  JvComponentBase, uViewGridBase, uAsientosController, JvExComCtrls,
  JvStatusBar, uDAInterfaces, uViewEpigrafes, uBizDiario, dxGDIPlusClasses;

type
  TfEditorDiario = class(TfEditorGridBase, IEditorDiario)
    frViewDiario1: TfrViewDiario;
    procedure FormShow(Sender: TObject);
    procedure actModificarUpdate(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
  private
    FDiario: IBizDiario;
    FController : IAsientosController;
  protected
    function GetDiario: IBizDiario;
    procedure SetDiario(const Value: IBizDiario);
    function GetController : IAsientosController; virtual;
    procedure SetController (const Value : IAsientosController); virtual;

    procedure NuevoInterno; override;
    procedure EliminarInterno; override;
    procedure ModificarInterno; override;
    procedure DuplicarInterno; override;
    procedure PrevisualizarInterno; override;
    procedure ImprimirInterno; override;

  public
    procedure PonerTitulos(const ATitulo: string = ''); override;
    property Diario: IBizDiario read GetDiario write SetDiario;
    property Controller : IAsientosController read GetController write SetController;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

uses
  uCustomEditor, uDataModuleUsuarios,
//  uEditorEpigrafe,
  uEditorDBBase, uFactuGES_App,
  cxGrid, cxGridCustomTableView; //, uListaAsientos;

{$R *.dfm}

{
****************************** TfEditorAsientos *******************************
}
procedure TfEditorDiario.FormShow(Sender: TObject);
begin
  inherited;

  if not Assigned(ViewGrid) then
    raise Exception.Create('No hay ninguna vista asignada');

  if not Assigned(Diario) then
    raise Exception.Create('No hay ningún Diario asignado');

  Diario.DataTable.Active := True;
  ViewGrid.GotoFirst;
  //Para que coja el ancho automático
  actAnchoAuto.Execute;    
end;

function TfEditorDiario.GetDiario: IBizDiario;
begin
  Result := FDiario;
end;

procedure TfEditorDiario.ImprimirInterno;
begin
  inherited;
end;

function TfEditorDiario.GetController: IAsientosController;
begin
  Result := FController;
end;

procedure TfEditorDiario.ModificarInterno;
begin
  inherited;
  FController.Ver(FController.Buscar(Diario.ID_ASIENTO));
  actRefrescar.Execute;
end;

procedure TfEditorDiario.NuevoInterno;
{var
  NumOrden: Integer;}
begin
  inherited;

//  NumOrden := ViewGrid._FocusedView.DataController.Summary.FooterSummaryValues[2];
//  Inc(NumOrden);

  FController.Ver(FController.Anadir);
  actRefrescar.Execute;
end;

procedure TfEditorDiario.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  FTitulo := 'Libro de diario - ' + AppFactuGES.EmpresaActiva.NOMBRE + ' - ' + AppFactuGES.EjercicioActivo.NOMBRE;
  inherited PonerTitulos(FTitulo);
end;

procedure TfEditorDiario.PrevisualizarInterno;
begin
  inherited;
end;

procedure TfEditorDiario.SetDiario(const Value: IBizDiario);
begin
  FDiario := Value;
  dsDataTable.DataTable := FDiario.DataTable;
  if Assigned(ViewGrid) then
    (ViewGrid as IViewDiario).Diario := Diario;
end;

procedure TfEditorDiario.SetController(const Value: IAsientosController);
begin
  FController := Value;
end;

destructor TfEditorDiario.Destroy;
begin
  FDiario := NIL;
  inherited;
end;

procedure TfEditorDiario.DuplicarInterno;
{var
  AEpigrafe : IBizAsiento;}
begin
AppFactuGES.ShowCapado;
{
  inherited;
  AEpigrafe := FController.Duplicar(Asientos);
  try
    FController.Ver(AEpigrafe);
  finally
    actRefrescar.Execute;
  end;
}
end;

procedure TfEditorDiario.EliminarInterno;
begin
  if (Application.MessageBox('¿Está seguro que desea borrar el asiento seleccionado?', 'Atención', MB_YESNO) = IDYES) then
  begin
    inherited;
    if FController.Eliminar(Diario.ID_ASIENTO) then
       actRefrescar.Execute;
  end;
end;

procedure TfEditorDiario.actEliminarUpdate(Sender: TObject);
begin
  inherited;
  if (Sender as TAction).Enabled then
     if Assigned(Diario) then
        (Sender as TAction).Enabled := Diario.ID_FACTURAIsNull and Diario.ID_PAGOIsNull;
end;

procedure TfEditorDiario.actModificarUpdate(Sender: TObject);
begin
  inherited;
  if (Sender as TAction).Enabled then
     if Assigned(Diario) then
        (Sender as TAction).Enabled := Diario.ID_FACTURAIsNull and Diario.ID_PAGOIsNull;
end;

constructor TfEditorDiario.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewDiario1; //CreateView(TfrViewAsientos) as IViewAsientos;
end;

end.
