unit uEditorControlVentasTerminadas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorControlBase, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TBX, TB2Item, TB2Dock,
  TB2Toolbar, ExtCtrls, JvExControls, JvComponent, JvNavigationPane, uBizControlesBase,
  uCustomView, uViewBase, uViewControlBase, uViewControlVentasTerminadas,
  Grids, DBGrids, pngimage;

type
  IEditorControlVentasTerminadas = interface(IEditorControlBase)
    ['{721FAE24-2440-44FA-B681-6E3C2BD656FE}']
  end;
  
  TfEditorControlVentasTerminadas = class(TfEditorControlBase, IEditorControlVentasTerminadas)
    dsVentasTerminadas: TDADataSource;
    TBXSeparatorItem16: TTBXSeparatorItem;
    tbxiExpandir: TTBXItem;
    tbxiContraer: TTBXItem;
    tbxiAnchoAuto: TTBXItem;
    actExpandir: TAction;
    actContraer: TAction;
    actAnchoAuto: TAction;
    TBXItem37: TTBXItem;
    TBXItem36: TTBXItem;
    TBXItem35: TTBXItem;
    TBXSeparatorItem17: TTBXSeparatorItem;
    frViewControlVentasTerminadas1: TfrViewControlVentasTerminadas;
    procedure actRefrescarExecute(Sender: TObject);
    procedure actExpandirExecute(Sender: TObject);
    procedure actContraerExecute(Sender: TObject);
    procedure actAnchoAutoExecute(Sender: TObject);
    procedure CustomEditorCreate(Sender: TObject);
    procedure CustomEditorGetModified(Sender: TObject;
      var Modified: Boolean);
    procedure actGuardarExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure actPrevisualizarExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure actConfPaginaExecute(Sender: TObject);
    procedure frViewControlVentasTerminadas1bRefrescarClick(
      Sender: TObject);
  private
    procedure RestaurarConfiguracion;
    procedure GuardarConfiguracion;
  protected
    FModificado : Boolean;
    procedure OnViewControlChanged(Sender: TObject);
    procedure SetControl(const Value: IBizControlBase); override;
    procedure RellenarCabeceraInforme; override;
  public
    constructor Create(AOwner: TComponent); override;
  end;


implementation

uses
  uEditorUtils, uBizControlVentasTerminadas, uROClasses,
  cxGridTableView, uCustomEditor;

{$R *.dfm}

function ShowEditorVentasTerminadas (ABizObject : TDADataTableRules): TModalResult;
var
  AEditor: IEditorControlVentasTerminadas;
begin
  AEditor := TfEditorControlVentasTerminadas.Create(Application);
  try
    AEditor.Control := (ABizObject as IBizControlVentasTerminadas);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;


{ TfEditorControlVentasTerminadas }

procedure TfEditorControlVentasTerminadas.SetControl(
  const Value: IBizControlBase);
begin
  inherited;
  RefrescarTituloEditor;
  if Assigned(FControl) then
  begin
    frViewControlVentasTerminadas1.OnViewChanged := OnViewControlChanged;
    with (FControl as IBizControlVentasTerminadas) do
    begin
      Items.DataTable.Active := False;  // No se abre la tabla hasta que no se pulse en 'Refrescar'
      dsVentasTerminadas.DataTable := Items.DataTable;
      frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.FocusedView.DataController.DataSource := dsVentasTerminadas;
    end;
  end;
end;

procedure TfEditorControlVentasTerminadas.actRefrescarExecute(Sender: TObject);
begin
  inherited;
  frViewControlVentasTerminadas1.Refresh;
  while (frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.FocusedView.Controller.FocusedRecord is TcxGridGroupRow) do
  begin
    frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.FocusedView.Controller.FocusedRecord.Expand(True);
    if (frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.FocusedView.Controller.FocusedRecordIndex <
      frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.FocusedView.DataController.RowCount - 1) then
      frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.FocusedView.Controller.GoToNext(False, True)
    else
      Exit;
  end;
end;

procedure TfEditorControlVentasTerminadas.actExpandirExecute(Sender: TObject);
begin
  inherited;
  frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.ExpandirTodo;
end;

procedure TfEditorControlVentasTerminadas.actContraerExecute(Sender: TObject);
begin
  inherited;
  frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.ContraerTodo;
end;

procedure TfEditorControlVentasTerminadas.actAnchoAutoExecute(Sender: TObject);
begin
  inherited;
  frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.AjustarAncho;
end;

procedure TfEditorControlVentasTerminadas.OnViewControlChanged(
  Sender: TObject);
begin
  if FControl.EsModificable then
    FModificado := True;
end;

procedure TfEditorControlVentasTerminadas.CustomEditorCreate(Sender: TObject);
begin
  inherited;
  FModificado := False;
end;

procedure TfEditorControlVentasTerminadas.CustomEditorGetModified(
  Sender: TObject; var Modified: Boolean);
begin
  inherited;
  Modified := FModificado;
end;

procedure TfEditorControlVentasTerminadas.actGuardarExecute(Sender: TObject);
begin
  if FModificado then
    GuardarConfiguracion;
  inherited;
  FModificado := False;
end;

procedure TfEditorControlVentasTerminadas.GuardarConfiguracion;
var
  Aux : IROStrings;
begin
  Aux := NewROStrings();
  Aux.Add(frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.ViewProperties);
  if not (FControl.DataTable.State in dsEditModes) then
    FControl.Edit;
  FControl.VISTA := Aux;
end;

procedure TfEditorControlVentasTerminadas.RestaurarConfiguracion;
begin
  frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.ViewProperties := FControl.VISTA.Text;
end;

procedure TfEditorControlVentasTerminadas.FormShow(Sender: TObject);
begin
  inherited;
  if FControl.DataTable.State <> dsInsert then // No restaurar si estamos insertando
    RestaurarConfiguracion;
  frViewControlVentasTerminadas1.frViewColumnas1.Refresh;
  frViewControlVentasTerminadas1.frViewAgrupaciones1.Refresh;
end;

procedure TfEditorControlVentasTerminadas.actPrevisualizarExecute(
  Sender: TObject);
begin
  inherited;
  frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.Preview;
end;

procedure TfEditorControlVentasTerminadas.actImprimirExecute(Sender: TObject);
begin
  inherited;
  frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.Print;
end;

procedure TfEditorControlVentasTerminadas.actConfPaginaExecute(
  Sender: TObject);
begin
  inherited;
  frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.PrintSetup;
end;

procedure TfEditorControlVentasTerminadas.frViewControlVentasTerminadas1bRefrescarClick(
  Sender: TObject);
begin
  inherited;
  frViewControlVentasTerminadas1.actRefrescarExecute(Sender);
end;

constructor TfEditorControlVentasTerminadas.Create(AOwner: TComponent);
begin
  inherited;
  FViewControl := frViewControlVentasTerminadas1;
  Self.Caption := 'Nueva consulta de ventas terminadas';
  JvNavPanelHeader.Caption := Self.Caption;
end;

procedure TfEditorControlVentasTerminadas.RellenarCabeceraInforme;
begin
  inherited;
  with frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.dxPrintStyleManager1Style1.PrinterPage.PageHeader do
  begin
    LeftTitle.Clear;
    LeftTitle.Add(FControl.NOMBRE);

    RightTitle.Clear;
    RightTitle.Add(DateToStr(frViewControlVentasTerminadas1.frViewPeriodoFechas1.edtFechaIni.Date) +
      ' - ' + DateToStr(frViewControlVentasTerminadas1.frViewPeriodoFechas1.edtFechaFin.Date));
    RightTitle.Add(frViewControlVentasTerminadas1.frViewControlGridVentasTerminadas1.FocusedView.DataController.Filter.FilterText);
  end;
end;

initialization
  RegisterEditor(IBizControlVentasTerminadas, ShowEditorVentasTerminadas, etItem);

end.

