unit uViewFacturasComision;

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, cxLookAndFeels, cxLookAndFeelPainters, uDAInterfaces;

type
  IViewFacturasComision = interface
  ['{2716E2DF-54B7-4120-8DD9-50B2537D7855}']
    function GetComision: IBizComision;
    procedure SetComision(const Value: IBizComision);
    property Comision: IBizComision read GetComision write SetComision;

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

  TfrViewFacturasComision = class(TfrViewDetallesGenerico, IViewFacturasComision)
    cxGridViewID_EMPRESA: TcxGridDBColumn;
    cxGridViewID_AGENTE: TcxGridDBColumn;
    cxGridViewAGENTE: TcxGridDBColumn;
    cxGridViewID_FACTURA: TcxGridDBColumn;
    cxGridViewFECHA_FACTURA: TcxGridDBColumn;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewSITUACION: TcxGridDBColumn;
    cxGridViewID_CLIENTE: TcxGridDBColumn;
    cxGridViewNOMBRE: TcxGridDBColumn;
    cxGridViewCOMISION: TcxGridDBColumn;
    cxGridViewBASE_IMPONIBLE: TcxGridDBColumn;
    cxGridViewIMPORTE_COMISION: TcxGridDBColumn;
    actExpandir: TAction;
    actContraer: TAction;
    ToolButton3: TToolButton;
    ToolButton8: TToolButton;
    cxGridViewNiFCIF: TcxGridDBColumn;
    cxGridViewID_COMISION_LIQUIDADA: TcxGridDBColumn;
    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 cxGridViewDataControllerSummaryAfterSummary(
      ASender: TcxDataSummary);
  protected
    FHayCambios : Boolean;
    FComision : IBizComision;
    FController : IComisionesController;

    procedure AnadirInterno; override;
    procedure EliminarInterno; override;

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

    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: IBizComision read GetComision write SetComision;
    property Controller : IComisionesController read GetController write SetController;
  end;


implementation

{$R *.dfm}

uses
  uCustomView; 

{ TfrViewFacturasComision }

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

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

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

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

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

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

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

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

procedure TfrViewFacturasComision.cxGridViewDataControllerSummaryAfterSummary(ASender: TcxDataSummary);
Var
  AImporteTotal: Currency;
begin
  inherited;

  if VarIsNull(ASender.FooterSummaryValues[1]) then
    AImporteTotal := 0
  else
    AImporteTotal := ASender.FooterSummaryValues[1];

  if Assigned(Comision) then
  begin
    if (Comision.IMPORTE_TOTAL <> AImporteTotal) then
    begin
      if not Comision.DataTable.Editing then
      begin
        Comision.Edit;
        Comision.IMPORTE_TOTAL := AImporteTotal;
        Comision.Post;
      end;
    end;
  end;

end;

destructor TfrViewFacturasComision.Destroy;
begin
  FComision := Nil;
  FController := Nil;
  inherited;
end;

procedure TfrViewFacturasComision.EliminarInterno;
begin
  if cxGridView.Controller.SelectedRowCount > 0 then
     if cxGridView.Controller.SelectedRows[0].HasCells then
        Comision.FacturasComision.Delete;
  Modified := True;
end;

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

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

function TfrViewFacturasComision.GetComision: IBizComision;
begin
  Result := FComision;
end;

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

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

procedure TfrViewFacturasComision.SetComision(const Value: IBizComision);
begin
  FComision := Value;
  FHayCambios := False;
  if Assigned(FComision) then
    dsDetalles.DataTable := FComision.FacturasComision.DataTable
  else
    dsDetalles.DataTable := NIL;

  actAnchoAutomatico.Execute;     
end;

end.
