unit uEditorArticulo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBItem, ToolWin, ComCtrls, JvExControls, JvComponent,
  uViewArticulo, uBizArticulos, JvNavigationPane, ActnList,
  uEditorBase, StdActns, TB2Dock, TB2Toolbar, SpTBXItem, ImgList, PngImageList,
  TB2Item, uEditorItem, DB, uDADataTable, uEditorDBBase, JvFormAutoSize,
  uDAScriptingProvider, uDACDSDataTable, StdCtrls, pngimage, ExtCtrls,
  SpTBXDkPanels, JvButton, AppEvnts, uCustomView, uViewBase,
  JvAppStorage, JvAppRegistryStorage, JvFormPlacement, JvComponentBase,
  uViewArticulos, uIEditorArticulo, uArticulosController, JvExComCtrls,
  JvStatusBar, dxLayoutLookAndFeels, uDAInterfaces, Grids, DBGrids, cxStyles,
  cxCustomData, uDMBase,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGrid, uViewDetallesGenerico,
  uViewArticulosProveedores,  dxLayoutControl, cxContainer,
  cxTextEdit, cxMaskEdit, cxSpinEdit, cxDBEdit, cxCurrencyEdit, cxLabel,
  cxLookAndFeels, cxLookAndFeelPainters;

type
  TfEditorArticulo = class(TfEditorDBItem, IEditorArticulo)
    TBXItem33: TSpTBXItem;
    actFamilias: TAction;
    TBXSeparatorItem6: TSpTBXSeparatorItem;
    procedure FormShow(Sender: TObject);
    procedure dsDataTableDataChange(Sender: TObject; Field: TField);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);
    procedure actFamiliasExecute(Sender: TObject);
  protected
    FController : IArticulosController;
    FArticulo: IBizArticulo;
    FViewArticulo : IViewArticulo;

    function GetController : IArticulosController;
    procedure SetController (const Value : IArticulosController); virtual;
    function GetArticulo: IBizArticulo; virtual;
    procedure SetArticulo(const Value: IBizArticulo); virtual;
    function GetViewArticulo: IViewArticulo;
    procedure SetViewArticulo(const Value: IViewArticulo);
    property ViewArticulo: IViewArticulo read GetViewArticulo write SetViewArticulo;

    procedure GuardarInterno; override;
    procedure EliminarInterno; override;

    //Si queremos crear otra vista para el editor heredado solo tendriamos que
    //sobreescribir este metodo
    procedure AsignarVista; virtual;
    procedure PonerTitulos(const ATitulo: string = ''); override;

  public
    property Articulo: IBizArticulo read GetArticulo write SetArticulo;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

{$R *.dfm}

uses
  uCustomEditor, uDataModuleArticulos, uDataModuleBase,
  uBizFamilias, uFamiliasController;

{
******************************* TfEditorArticulo *******************************
}
function TfEditorArticulo.GetArticulo: IBizArticulo;
begin
  Result := FArticulo;
end;

function TfEditorArticulo.GetController: IArticulosController;
begin
  Result := FController;
end;

function TfEditorArticulo.GetViewArticulo: IViewArticulo;
begin
  Result := FViewArticulo;
end;

procedure TfEditorArticulo.GuardarInterno;
begin
  inherited;
  FController.Guardar(FArticulo);
  Modified := False;
end;

procedure TfEditorArticulo.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  if Assigned(Articulo) then
  begin
    if Articulo.EsNuevo then
      FTitulo := 'Nuevo artículo'
    else
      FTitulo := 'Artículo';

    if Length(Articulo.DESCRIPCION) > 0 then
      FTitulo := FTitulo + ' - ' + Articulo.DESCRIPCION;
  end;

  inherited PonerTitulos(FTitulo);
end;

procedure TfEditorArticulo.SetArticulo(const Value: IBizArticulo);
begin
  FArticulo := Value;
  dsDataTable.DataTable := FArticulo.DataTable;

  if Assigned(FViewArticulo) and Assigned(Articulo) then
  begin
     FViewArticulo.Articulo := Articulo;
//     frViewArticulosProveedores1.Articulo := Articulo;
  end;
end;

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

procedure TfEditorArticulo.SetViewArticulo(const Value: IViewArticulo);
begin
  FViewArticulo := Value;

  if Assigned(FViewArticulo) and Assigned(Articulo) then
    FViewArticulo.Articulo := Articulo;
end;

procedure TfEditorArticulo.FormShow(Sender: TObject);
begin
  inherited;

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

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

  Articulo.DataTable.Active := True;
end;

destructor TfEditorArticulo.Destroy;
begin
  // Utilizar mejor OnClose;
  inherited;
end;

procedure TfEditorArticulo.actFamiliasExecute(Sender: TObject);
var
  AFamiliasController : IFamiliasController;
  AFamilias : IBizFamilia;
begin
  AFamiliasController := TFamiliasController.Create;
  try
    AFamilias := (AFamiliasController.BuscarTodos as IBizFamilia);
    AFamiliasController.VerTodos(AFamilias);
  finally
    AFamilias := NIL;
    AFamiliasController := NIL;
  end;
end;

procedure TfEditorArticulo.AsignarVista;
var
  AViewArticulo: TfrViewArticulo;
begin
  AViewArticulo := TfrViewArticulo.create(Self);
  with AViewArticulo do
  begin
    Parent := pagGeneral;
    Align := alClient;
    dxLayoutControlArticulo.LookAndFeel := dmBase.dxLayoutSkinLookAndFeel;
  end;
  ViewArticulo := AViewArticulo;
end;

constructor TfEditorArticulo.Create(AOwner: TComponent);
begin
  inherited;
  pgPaginas.ActivePageIndex := 0;
  AsignarVista;
end;

procedure TfEditorArticulo.CustomEditorClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  dsDataTable.DataTable := NIL;
  FViewArticulo := NIL;
  FArticulo := NIL;
end;

procedure TfEditorArticulo.dsDataTableDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;
  if Assigned(FArticulo) and (not (FArticulo.DataTable.Fetching) or
    not (FArticulo.DataTable.Opening) or not (FArticulo.DataTable.Closing)) then
    PonerTitulos;
end;

procedure TfEditorArticulo.EliminarInterno;
begin
  if (Application.MessageBox('¿Desea borrar este articulo?', 'Atención', MB_YESNO) = IDYES) then
  begin
    inherited;
    if not FController.Eliminar(FArticulo) then
       actRefrescar.Execute;
  end;
end;

end.

