unit uViewDetallesDTO;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewDetallesBase, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxImageComboBox, cxRichEdit,
  cxMaskEdit, cxCurrencyEdit, cxCheckBox, ImgList, PngImageList, uDADataTable,
  StdActns, ExtActns, ActnList, TB2Item, TBX, TB2Dock, TB2Toolbar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ComCtrls, StdCtrls, JvExStdCtrls,
  JvCombobox, JvColorCombo, ToolWin, uDAInterfaces, Menus,
  cxGridCustomPopupMenu, cxGridPopupMenu;

type
  IViewDetallesDTO = interface(IViewDetallesBase)
  ['{0D221FFB-9F43-48FC-9AE7-0AD0F0791AD1}']
  end;

  TfrViewDetallesDTO = class(TfrViewDetallesBase, IViewDetallesDTO)
    cxGridViewDESCUENTO: TcxGridDBColumn;
    cxGridViewIMPORTEPORTE: TcxGridDBColumn;
    cxGridViewIMPORTENETO: TcxGridDBColumn;
    procedure cxGridViewIMPORTENETOGetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: string);
    procedure cxGridViewIMPORTENETOPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
  protected
    function HayQueRecalcular(AItem: TcxCustomGridTableItem): Boolean; override;
  end;

implementation
{$R *.dfm}

{ TfrViewDetallesDTO }

procedure TfrViewDetallesDTO.cxGridViewIMPORTENETOGetDisplayText(Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AText: string);
var
  ImporteNeto : Double;
begin
  //Se encarga de mostrar el campo calculado de importe neto
  ImporteNeto := -1;

  if not VarIsNull(ARecord.Values[cxGridViewIMPORTEUNIDAD.Index]) then
     if not VarIsNull(ARecord.Values[cxGridViewDESCUENTO.Index]) then
        ImporteNeto := ARecord.Values[cxGridViewIMPORTEUNIDAD.Index] - ((ARecord.Values[cxGridViewIMPORTEUNIDAD.Index] * ARecord.Values[cxGridViewDESCUENTO.Index])/100)
     else
        ImporteNeto := ARecord.Values[cxGridViewIMPORTEUNIDAD.Index];

  if (ImporteNeto <> -1) then
  begin
    AText := FormatCurr(',0.00 €;-,0.00 €', FloatToCurr(ImporteNeto))
  end;
end;

procedure TfrViewDetallesDTO.cxGridViewIMPORTENETOPropertiesValidate(Sender: TObject; var DisplayValue: Variant; var ErrorText: TCaption;
  var Error: Boolean);
begin
  inherited;
  if not VarIsNull(DisplayValue) then
  begin
    cxGridViewDESCUENTO.DataBinding.Field.Value := ((cxGridViewIMPORTEUNIDAD.DataBinding.Field.Value - DisplayValue) * 100) / cxGridViewIMPORTEUNIDAD.DataBinding.Field.Value;
    Controller.ActualizarTotales(Detalles);
  end;
end;

function TfrViewDetallesDTO.HayQueRecalcular(AItem: TcxCustomGridTableItem): Boolean;
begin
  Result := inherited HayQueRecalcular(AItem);
  if not Result then
    Result := (AItem = cxGridViewDESCUENTO) or (AItem = cxGridViewIMPORTEPORTE);
end;

end.
