unit uEditorComision;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorDBItem, ToolWin, ComCtrls, JvExControls, JvComponent,
  uViewComision, uBizComisiones, 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, uDMBase,
  JvAppStorage, JvAppRegistryStorage, JvFormPlacement, JvComponentBase,

  uIEditorComision, uComisionesController, JvExComCtrls,
  JvStatusBar, dxLayoutLookAndFeels, uDAInterfaces, cxGraphics, cxControls,
  cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit, cxLabel,
  JvEnterTab, uViewDetallesGenerico, uViewFacturasComision, Grids, DBGrids;

type
  TfEditorComision = class(TfEditorDBItem, IEditorComision)
    frViewComision1: TfrViewComision;
    frViewFacturasComision1: TfrViewFacturasComision;
    procedure FormShow(Sender: TObject);
    procedure dsDataTableDataChange(Sender: TObject; Field: TField);
    procedure CustomEditorClose(Sender: TObject; var Action: TCloseAction);
  protected
    FController : IComisionesController;
    FComision: IBizComision;
    FViewComision : IViewComision;

    function GetController : IComisionesController;
    procedure SetController (const Value : IComisionesController); virtual;
    function GetComision: IBizComision; virtual;
    procedure SetComision(const Value: IBizComision); virtual;
    function GetViewComision: IViewComision;
    procedure SetViewComision(const Value: IViewComision);
    property ViewComision: IViewComision read GetViewComision write SetViewComision;

    procedure GuardarInterno; override;
    procedure EliminarInterno; override;

    procedure PonerTitulos(const ATitulo: string = ''); override;

    //Si queremos crear otra vista para el editor heredado solo tendriamos que
    //sobreescribir este metodo
    procedure AsignarVista; virtual;

  public
    property Comision: IBizComision read GetComision write SetComision;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

implementation

{$R *.dfm}

uses
  uCustomEditor, uDataModuleComisiones, uDataModuleBase;

function ShowEditorComision (ABizObject : TDADataTableRules): TModalResult;
var
  AEditor: TfEditorComision;
begin
  AEditor := TfEditorComision.Create(Application);
  try
    AEditor.Comision := (ABizObject as IBizComision);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;

{
******************************* TfEditorComision *******************************
}
function TfEditorComision.GetComision: IBizComision;
begin
  Result := FComision;
end;

function TfEditorComision.GetController: IComisionesController;
begin
  Result := FController;
end;

function TfEditorComision.GetViewComision: IViewComision;
begin
  Result := FViewComision;
end;

procedure TfEditorComision.GuardarInterno;
begin
  inherited;
  FController.Guardar(FComision);
  Modified := False;
end;

procedure TfEditorComision.PonerTitulos(const ATitulo: string);
var
  FTitulo : String;
begin
  if Assigned(Comision) then
  begin
    if Comision.EsNuevo then
      FTitulo := 'Nueva comisión'
    else
      FTitulo := 'Comisión'  + ' - ' + Comision.AGENTE;
  end;

  inherited PonerTitulos(FTitulo);
end;

procedure TfEditorComision.SetComision(const Value: IBizComision);
begin
  FComision := Value;
  dsDataTable.DataTable := FComision.DataTable;

  if Assigned(FViewComision) and Assigned(Comision) then
  begin
    FViewComision.Comision := Comision;
    frViewFacturasComision1.Comision := Comision;
  end
  else
  begin
    FViewComision.Comision := Nil;
    frViewFacturasComision1.Comision := Nil;
  end
end;

procedure TfEditorComision.SetController(const Value: IComisionesController);
begin
  FController := Value;
  if Assigned(FController) then
  begin
    frViewFacturasComision1.Controller := FController;
  end;
end;

procedure TfEditorComision.SetViewComision(const Value: IViewComision);
begin
  FViewComision := Value;

  if Assigned(FViewComision) and Assigned(Comision) then
    FViewComision.Comision := Comision;
end;

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

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

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

  Comision.DataTable.Active := True;
end;

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

procedure TfEditorComision.AsignarVista;
begin
  ViewComision := frViewComision1;
end;

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

procedure TfEditorComision.CustomEditorClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  dsDataTable.DataTable := NIL;
  FViewComision := NIL;
  FComision := NIL;
  FController := Nil;
end;

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

procedure TfEditorComision.EliminarInterno;
begin
  if (Application.MessageBox('¿Desea borrar esta comision?', 'Atención', MB_YESNO) = IDYES) then
  begin
    inherited;
    if not FController.Eliminar(FComision) then
       actRefrescar.Execute;
  end;
end;

end.

