unit uEditorInfVentasArticulo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorInformeBase, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TBX, TB2Item, TB2Dock,
  TB2Toolbar, ExtCtrls, JvExControls, JvComponent, JvNavigationPane, uBizInformes,
  uCustomView, uViewBase, uViewInformeBase, uViewInfVentasArticulo,
  Grids, DBGrids, pngimage, JvExComCtrls, JvStatusBar, uInfVentasArticuloController,
  uIEditorInfVentasArticulo, uBizInfVentasArticulo, uInformesController, 
  uViewGridBase, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, TB2ExtItems, TBXExtItems;


type
  TfEditorInfVentasArticulo = class(TfEditorInformeBase, IEditorInfVentasArticulo)
    dsVentasTerminadas: TDADataSource;
    TBXSeparatorItem16: TTBXSeparatorItem;
    tbxiExpandir: TTBXItem;
    tbxiContraer: TTBXItem;
    tbxiAnchoAuto: TTBXItem;
    actExpandir: TAction;
    actContraer: TAction;
    actAnchoAuto: TAction;
    TBXItem37: TTBXItem;
    TBXItem36: TTBXItem;
    TBXItem35: TTBXItem;
    TBXSeparatorItem17: TTBXSeparatorItem;
    frViewInfVentasArticulo1: TfrViewInfVentasArticulo;
    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 FormShow(Sender: TObject);
    procedure actPrevisualizarExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure actConfPaginaExecute(Sender: TObject);
    procedure frViewInfVentasArticulo1bRefrescarClick(Sender: TObject);
  private
    FInformesController: IInformesController;
    procedure RestaurarConfiguracion;
    procedure GuardarConfiguracion;
  protected
    FDatos : IBizInfVentasArticulo;
    FController : IInfVentasArticuloController;
    FModificado : Boolean;
    procedure OnViewControlChanged(Sender: TObject);
    procedure SetInforme(const Value: IBizInforme); override;
    procedure RellenarCabeceraInforme; override;

    function GetController : IInfVentasArticuloController;
    procedure SetController (const Value : IInfVentasArticuloController);

    function GetDatos : IBizInfVentasArticulo;
    procedure SetDatos (const Value : IBizInfVentasArticulo);

    procedure GuardarInterno; override;

  public
    property Controller: IInfVentasArticuloController read GetController write SetController;
    property Datos : IBizInfVentasArticulo read GetDatos write SetDatos;

    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;


implementation

uses
  uROClasses, //cxControls, cxGridCustomView, //cxGridCustomTableView,
  //cxGridTableView,
  uCustomEditor, uDataModuleUsuarios, uSistemaFunc;

{$R *.dfm}

{ TfEditorInfVentasArticulo }

procedure TfEditorInfVentasArticulo.SetController(const Value: IInfVentasArticuloController);
begin
  FController := Value;
//  if Assigned(FController) then
//    (ViewArticulo as IViewArticulo).Controller := (FController as IArticulosController);
end;

procedure TfEditorInfVentasArticulo.SetDatos(const Value: IBizInfVentasArticulo);
begin
  FDatos := Value;

  if Assigned(FDatos) then
  begin
    FDatos.DataTable.Active := False; // No se abre la tabla hasta que no se pulse en 'Refrescar'
    dsVentasTerminadas.DataTable := FDatos.DataTable;

    if Assigned(FViewInforme) then
       (FViewInforme as IViewInfVentasArticulo).Datos := FDatos;

//    frViewInfVentasArticulo1.OnViewChanged := OnViewControlChanged;
//    with (FInforme as IBizInfVentasArticulo) do
//    begin
//      Items.DataTable.Active := False;  // No se abre la tabla hasta que no se pulse en 'Refrescar'
//      dsVentasTerminadas.DataTable := Items.DataTable;
      frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.FocusedView.DataController.DataSource := dsVentasTerminadas;
  end;
end;

procedure TfEditorInfVentasArticulo.SetInforme(const Value: IBizInforme);
begin
  inherited;
//  RefrescarTituloEditor;
  if Assigned(FInforme) then
  begin
    frViewInfVentasArticulo1.OnViewChanged := OnViewControlChanged;
//    with (FInforme as IBizInforme) do
//    begin
//      Items.DataTable.Active := False;  // No se abre la tabla hasta que no se pulse en 'Refrescar'
//      dsVentasTerminadas.DataTable := Items.DataTable;
//      frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.FocusedView.DataController.DataSource := dsVentasTerminadas;
//    end;
  end;
end;

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

procedure TfEditorInfVentasArticulo.actExpandirExecute(Sender: TObject);
begin
  inherited;
  frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.ExpandirTodo;
end;

procedure TfEditorInfVentasArticulo.actContraerExecute(Sender: TObject);
begin
  inherited;
  frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.ContraerTodo;
end;

procedure TfEditorInfVentasArticulo.actAnchoAutoExecute(Sender: TObject);
begin
  inherited;
  frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.AjustarAncho;
end;

procedure TfEditorInfVentasArticulo.OnViewControlChanged(
  Sender: TObject);
begin
//  if FInforme.EsModificable then
    FModificado := True;
end;

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

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

destructor TfEditorInfVentasArticulo.Destroy;
begin
  FInformesController := Nil;
  FViewInforme := Nil;  
  inherited;
end;

function TfEditorInfVentasArticulo.GetController: IInfVentasArticuloController;
begin
  Result := FController;
end;

function TfEditorInfVentasArticulo.GetDatos: IBizInfVentasArticulo;
begin
  Result := FDatos;
end;

procedure TfEditorInfVentasArticulo.GuardarConfiguracion;
var
  Aux : IROStrings;
  Fichero: TextFile;
  NombreFichero: String;

begin
  NombreFichero := DarFicheroTemporal;
  frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.cxGridView.StoreToIniFile(NombreFichero, true, [gsoUseFilter,gsoUseSummary]);

  with TStringList.Create do
  try
     LoadFromFile(NombreFichero);
     Aux := NewROStrings();
     Aux.Add(Text);

     if not (FInforme.DataTable.State in dsEditModes) then
        FInforme.Edit;
     FInforme.VISTA := Aux;

  finally
     Free;
  end;
end;

procedure TfEditorInfVentasArticulo.GuardarInterno;
begin
  inherited;

  ShowHourglassCursor;

  try
    if FModificado then
       GuardarConfiguracion;
       FInformesController.Guardar(FInforme);
       FModificado := False;
  finally
    HideHourglassCursor;
  end;

  Modified := False;
end;

procedure TfEditorInfVentasArticulo.RestaurarConfiguracion;
var
  Fichero: TextFile;
  NombreFichero: String;
begin
  try
    //Creamos un fichero temporal para guardar la configuración del informe que viene dada
    //por su campo vista, para luego restaurarla en el grid a partir de dicho fichero porque el Stream falla
    NombreFichero := DarFicheroTemporal;
    AssignFile(Fichero, NombreFichero);
    Rewrite(Fichero);
    Writeln(Fichero, FInforme.VISTA.Text);
    CloseFile(Fichero);

    //Restauramos la configuración
    frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.cxGridView.RestoreFromIniFile(NombreFichero,True,false,[gsoUseFilter,gsoUseSummary]);

  finally
    //Eliminamos el fichero temporal
    DeleteFile(NombreFichero);
//    FreeAndNil(Fichero)
  end;
end;

procedure TfEditorInfVentasArticulo.FormShow(Sender: TObject);
begin
  inherited;
  if FInforme.DataTable.State <> dsInsert then // No restaurar si estamos insertando
     RestaurarConfiguracion;

  frViewInfVentasArticulo1.frViewColumnas1.Refresh;
  frViewInfVentasArticulo1.frViewAgrupaciones1.Refresh;

  if FInforme.DataTable.State = dsInsert then // Si es nuevo obligamos a meter nombre
     actPropiedades.Execute;
end;

procedure TfEditorInfVentasArticulo.actPrevisualizarExecute(
  Sender: TObject);
begin
  inherited;
  frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.Preview;
end;

procedure TfEditorInfVentasArticulo.actImprimirExecute(Sender: TObject);
begin
  inherited;
  frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.Print;
end;

procedure TfEditorInfVentasArticulo.actConfPaginaExecute(
  Sender: TObject);
begin
  inherited;
  frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.PrintSetup;
end;

procedure TfEditorInfVentasArticulo.frViewInfVentasArticulo1bRefrescarClick(
  Sender: TObject);
begin
  inherited;
  frViewInfVentasArticulo1.actRefrescarExecute(Sender);
end;

constructor TfEditorInfVentasArticulo.Create(AOwner: TComponent);
begin
  inherited;
  FInformesController := TInformesController.Create;
  FViewInforme := frViewInfVentasArticulo1;
end;

procedure TfEditorInfVentasArticulo.RellenarCabeceraInforme;
begin
  inherited;
  with frViewInfVentasArticulo1.frViewInfVentasArticuloGrid1.dxPrintStyleManager1Style1.PrinterPage.PageHeader do
  begin
    LeftTitle.Clear;
//    LeftTitle.Add(FInforme.NOMBRE);
    LeftTitle.Add(FInforme.DESCRIPCION);    

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

end.

