unit uViewVendedoresComision;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewDetallesGenerico, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, ActnList, ImgList, PngImageList,
  uDADataTable, ComCtrls, ToolWin, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, cxImageComboBox, cxCurrencyEdit, uDataModuleComisiones, 
  uBizComisiones, uComisionesController, Grids, DBGrids, cxSpinEdit,
  dxLayoutControl, uDAInterfaces, StdCtrls;

type
  IViewVendedoresComision = interface
  ['{06705772-409C-4094-ABB3-F13E10D5B743}']
    function GetComision: IBizComisiones;
    procedure SetComision(const Value: IBizComisiones);
    property Comision: IBizComisiones read GetComision write SetComision;

    function GetController : IComisionesController;
    procedure SetController (const Value : IComisionesController);
    property Controller : IComisionesController read GetController write SetController;
  end;

  TfrViewVendedoresComision = class(TfrViewDetallesGenerico, IViewVendedoresComision)
    cxGridViewRecID: TcxGridDBColumn;
    cxGridViewID_COMISION: TcxGridDBColumn;
    cxGridViewID_VENDEDOR: TcxGridDBColumn;
    cxGridViewNOMBRE: TcxGridDBColumn;
    cxGridViewCOMISION: TcxGridDBColumn;
    cxGridViewIMPORTE_TOTAL: TcxGridDBColumn;
    actExpandir: TAction;
    actContraer: TAction;
    ToolButton3: TToolButton;
    ToolButton8: TToolButton;
    procedure actExpandirExecute(Sender: TObject);
    procedure actContraerExecute(Sender: TObject);
    procedure CustomViewShow(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
    procedure actExpandirUpdate(Sender: TObject);
    procedure actContraerUpdate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  protected
    FHayCambios : Boolean;
    FComision : IBizComisiones;
    FController : IComisionesController;

    procedure AnadirInterno; override;
    procedure EliminarInterno; override;

    function GetComision: IBizComisiones;
    procedure SetComision(const Value: IBizComisiones);

    function GetModified: Boolean; override;
    procedure SetModified(const Value: Boolean); override;

    function GetController : IComisionesController;
    procedure SetController (const Value : IComisionesController);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Comision: IBizComisiones read GetComision write SetComision;
    property Controller : IComisionesController read GetController write SetController;
  end;


implementation

{$R *.dfm}

uses
  uCustomView; 

{ TfrViewVendedoresComision }

procedure TfrViewVendedoresComision.actContraerExecute(Sender: TObject);
begin
  inherited;
  cxGridView.ViewData.Collapse(True);
end;

procedure TfrViewVendedoresComision.actContraerUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := HayDatos;
end;

procedure TfrViewVendedoresComision.actEliminarUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := ((cxGridView.Controller.SelectedRowCount > 0)
                              and (cxGridView.Controller.SelectedRows[0].HasCells));
end;

procedure TfrViewVendedoresComision.actExpandirExecute(Sender: TObject);
begin
  inherited;
  cxGridView.ViewData.Expand(True);
end;

procedure TfrViewVendedoresComision.actExpandirUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := HayDatos;
end;

procedure TfrViewVendedoresComision.AnadirInterno;
begin
  //inherited;  // No hacer el comportamiento normal
  if FController.ElegirVendedoresComision(Comision) then
     Modified := True;
end;

procedure TfrViewVendedoresComision.Button1Click(Sender: TObject);
begin
  inherited;
  dsDetalles.DataTable.Active := true;
end;

constructor TfrViewVendedoresComision.Create(AOwner: TComponent);
begin
  inherited;
  FHayCambios := False;
end;

procedure TfrViewVendedoresComision.CustomViewShow(Sender: TObject);
begin
  inherited;
  cxGridView.OptionsSelection.MultiSelect := False;
end;

destructor TfrViewVendedoresComision.Destroy;
begin
  inherited;
end;

procedure TfrViewVendedoresComision.EliminarInterno;
begin
  inherited;
end;

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

function TfrViewVendedoresComision.GetModified: Boolean;
begin
  Result := FHayCambios or inherited GetModified;
end;

function TfrViewVendedoresComision.GetComision: IBizComisiones;
begin
  Result := FComision;
end;

procedure TfrViewVendedoresComision.SetController(const Value: IComisionesController);
begin
  FController := Value;
end;

procedure TfrViewVendedoresComision.SetModified(const Value: Boolean);
begin
  FHayCambios := Value;
  inherited;
end;

procedure TfrViewVendedoresComision.SetComision(const Value: IBizComisiones);
begin
  FComision := Value;
  FHayCambios := False;

  if Assigned(FComision) then
    dsDetalles.DataTable := FComision.Vendedores.DataTable
  else
    dsDetalles.DataTable := NIL;

  actAnchoAutomatico.Execute;
end;

end.
