unit uEditorInfMargenArticulo;

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, uViewInfMargenArticulo,
  Grids, DBGrids, pngimage, JvExComCtrls, JvStatusBar, uInfMargenArticuloController,
  uIEditorInfMargenArticulo, uBizInfMargenArticulo, uInformesController,
  uViewGridBase, TB2ExtItems, TBXExtItems;


type
  TfEditorInfMargenArticulo = class(TfEditorInformeBase, IEditorInfMargenArticulo)
    dsVentasTerminadas: TDADataSource;
    TBXSeparatorItem16: TTBXSeparatorItem;
    tbxiExpandir: TTBXItem;
    tbxiContraer: TTBXItem;
    tbxiAnchoAuto: TTBXItem;
    actExpandir: TAction;
    actContraer: TAction;
    actAnchoAuto: TAction;
    TBXItem37: TTBXItem;
    TBXItem36: TTBXItem;
    TBXItem35: TTBXItem;
    TBXSeparatorItem17: TTBXSeparatorItem;
    frViewInfMargenArticulo1: TfrViewInfMargenArticulo;
    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 frViewInfMargenArticulo1bRefrescarClick(Sender: TObject);
  private
    FInformesController: IInformesController;
    procedure RestaurarConfiguracion;
    procedure GuardarConfiguracion;
  protected
    FDatos : IBizInfMargenArticulo;
    FController : IInfMargenArticuloController;
    FModificado : Boolean;
    procedure OnViewControlChanged(Sender: TObject);
    procedure SetInforme(const Value: IBizInforme); override;
    procedure RellenarCabeceraInforme; override;

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

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

    procedure GuardarInterno; override;

  public
    property Controller: IInfMargenArticuloController read GetController write SetController;
    property Datos : IBizInfMargenArticulo read GetDatos write SetDatos;

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


implementation

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

{$R *.dfm}

{ TfEditorInfMargenArticulo }

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

procedure TfEditorInfMargenArticulo.SetDatos(const Value: IBizInfMargenArticulo);
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 IViewInfMargenArticulo).Datos := FDatos;

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

procedure TfEditorInfMargenArticulo.SetInforme(const Value: IBizInforme);
begin
  inherited;
//  RefrescarTituloEditor;
  if Assigned(FInforme) then
  begin
    frViewInfMargenArticulo1.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;
//      frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.FocusedView.DataController.DataSource := dsVentasTerminadas;
//    end;
  end;
end;

procedure TfEditorInfMargenArticulo.actRefrescarExecute(Sender: TObject);

begin
  inherited;
  frViewInfMargenArticulo1.Refresh;

  while (frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.FocusedView.Controller.FocusedRecord is TcxGridGroupRow) do
  begin
    frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.FocusedView.Controller.FocusedRecord.Expand(True);
    if (frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.FocusedView.Controller.FocusedRecordIndex <
      frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.FocusedView.DataController.RowCount - 1) then
      frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.FocusedView.Controller.GoToNext(False, True)
    else
      Exit;
  end;
end;

procedure TfEditorInfMargenArticulo.actExpandirExecute(Sender: TObject);
begin
  inherited;
  frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.ExpandirTodo;
end;

procedure TfEditorInfMargenArticulo.actContraerExecute(Sender: TObject);
begin
  inherited;
  frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.ContraerTodo;
end;

procedure TfEditorInfMargenArticulo.actAnchoAutoExecute(Sender: TObject);
begin
  inherited;
  frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.AjustarAncho;
end;

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

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

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

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

function TfEditorInfMargenArticulo.GetController: IInfMargenArticuloController;
begin
  Result := FController;
end;

function TfEditorInfMargenArticulo.GetDatos: IBizInfMargenArticulo;
begin
  Result := FDatos;
end;

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

begin
  NombreFichero := DarFicheroTemporal;
  frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.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 TfEditorInfMargenArticulo.GuardarInterno;
begin
  inherited;

  ShowHourglassCursor;

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

  Modified := False;
end;

procedure TfEditorInfMargenArticulo.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
    frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.cxGridView.RestoreFromIniFile(NombreFichero,True,false,[gsoUseFilter,gsoUseSummary]);

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

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

  frViewInfMargenArticulo1.frViewColumnas1.Refresh;
  frViewInfMargenArticulo1.frViewAgrupaciones1.Refresh;

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

  if not frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.HayColumnasDePromedio then
  begin
    frViewInfMargenArticulo1.TBXMultiDock2.Visible := False;
    actRefrescar.Execute;
  end;
end;

procedure TfEditorInfMargenArticulo.actPrevisualizarExecute(
  Sender: TObject);
begin
  inherited;
  frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.Preview;
end;

procedure TfEditorInfMargenArticulo.actImprimirExecute(Sender: TObject);
begin
  inherited;
  frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.Print;
end;

procedure TfEditorInfMargenArticulo.actConfPaginaExecute(
  Sender: TObject);
begin
  inherited;
  frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.PrintSetup;
end;

procedure TfEditorInfMargenArticulo.frViewInfMargenArticulo1bRefrescarClick(
  Sender: TObject);
begin
  inherited;
  frViewInfMargenArticulo1.actRefrescarExecute(Sender);
end;

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

procedure TfEditorInfMargenArticulo.RellenarCabeceraInforme;
begin
  inherited;
  with frViewInfMargenArticulo1.frViewInfMargenArticuloGrid1.dxPrintStyleManager1Style1.PrinterPage.PageHeader do
  begin
    LeftTitle.Clear;
//    LeftTitle.Add(FInforme.NOMBRE);
    LeftTitle.Add(FInforme.DESCRIPCION);    

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

end.

