unit uViewAlbaranesProveedor;

interface

uses
  Types, uViewGrid, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, dxPSGlbl, dxPSUtl, dxPSEngn,
  dxPrnPg, dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, cxImageComboBox, ImgList, Controls, PngImageList,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGridCustomPopupMenu, cxGridPopupMenu, dxPSCore, dxPScxCommon, dxPScxGrid6Lnk,
  uDADataTable, cxGridLevel, cxClasses, cxControls, cxGridCustomView, Classes,
  cxGrid, uBizAlbaranesProveedor, cxCurrencyEdit, Forms, uViewFiltroBase,
  ActnList, TB2Item, TBX, TB2Toolbar, TBXDkPanels, TB2Dock, dxPgsDlg, cxTextEdit,
  uDAInterfaces, uCustomView, uViewBase;

type
  IViewAlbaranesProveedor = interface(IViewGrid)
  ['{DFD9B90C-E3AE-48E1-A13F-AA75A05BBF3E}']
    function GetAlbaranes: IBizAlbaranProveedor;
    procedure SetAlbaranes(const Value: IBizAlbaranProveedor);
    property Albaranes: IBizAlbaranProveedor read GetAlbaranes write SetAlbaranes;
  end;

  TfrViewAlbaranesProveedor = class(TfrViewGrid, IViewAlbaranesProveedor)
    PngImageList: TPngImageList;
    cxGridViewREFERENCIA: TcxGridDBColumn;
    cxGridViewFECHA_ALBARAN: TcxGridDBColumn;
    cxGridViewINCIDENCIAS_ACTIVAS: TcxGridDBColumn;
    cxGridViewNOMBRE: TcxGridDBColumn;
    cxGridViewCALLE: TcxGridDBColumn;
    cxGridViewCODIGO_POSTAL: TcxGridDBColumn;
    cxGridViewPOBLACION: TcxGridDBColumn;
    cxGridViewPROVINCIA: TcxGridDBColumn;
    cxGridViewPERSONA_CONTACTO: TcxGridDBColumn;
    cxGridViewTELEFONO: TcxGridDBColumn;
    cxGridViewNOMBRE_ALMACEN: TcxGridDBColumn;
    cxGridViewREF_PED_PROVEEDOR: TcxGridDBColumn;
    cxGridViewREF_FACTURA: TcxGridDBColumn;
    cxGridViewIMPORTE_TOTAL: TcxGridDBColumn;
    actProveedor: TAction;
    actAlmacen: TAction;
    TBXItem2: TTBXItem;
    TBXSeparatorItem1: TTBXSeparatorItem;
    TBXItem3: TTBXItem;
    TBXSeparatorItem2: TTBXSeparatorItem;
    cxGridViewREFERENCIA_PROVEEDOR: TcxGridDBColumn;
    cxGridViewREF_FACTURA_PROV: TcxGridDBColumn;
    cxGridViewINCIDENCIAS: TcxGridDBColumn;
    procedure cxGridActiveTabChanged(Sender: TcxCustomGrid; ALevel:
            TcxGridLevel);
    procedure cxGridViewStylesGetContentStyle(Sender: TcxCustomGridTableView;
            ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out
            AStyle: TcxStyle);
    procedure actProveedorExecute(Sender: TObject);
    procedure actProveedorUpdate(Sender: TObject);
    procedure actAlmacenExecute(Sender: TObject);
    procedure actAlmacenUpdate(Sender: TObject);
    procedure cxGridViewINCIDENCIAS_ACTIVASGetCellHint(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      ACellViewInfo: TcxGridTableDataCellViewInfo; const AMousePos: TPoint;
      var AHintText: TCaption; var AIsHintMultiLine: Boolean;
      var AHintTextRect: TRect);
    procedure cxGridViewDataControllerCompare(
      ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
      AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
    procedure cxGridViewNUM_COPIASCustomDrawHeader(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridColumnHeaderViewInfo;
      var ADone: Boolean);
    procedure cxGridViewNUM_CORREOSCustomDrawHeader(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridColumnHeaderViewInfo;
      var ADone: Boolean);

  private
    //Filtros relativos a la vista
    procedure AnadirFiltroFechas;

  protected
    FAlbaranes: IBizAlbaranProveedor;
    function GetAlbaranes: IBizAlbaranProveedor;
    procedure SetAlbaranes(const Value: IBizAlbaranProveedor);
  public
    procedure AnadirOtrosFiltros; override;
    property Albaranes: IBizAlbaranProveedor read GetAlbaranes write SetAlbaranes;
  end;

implementation

uses
  SysUtils, variants, windows, uDataModuleAlbaranesProveedor,
  schAlbaranesProveedorClient_Intf, uViewGridBase, cxGridDBDataDefinitions,
  uReferenciasUtils, cxVariants;

{$R *.dfm}

procedure TfrViewAlbaranesProveedor.actAlmacenExecute(Sender: TObject);
begin
  if (cxGridViewNOMBRE_ALMACEN.GroupIndex < 0) then
  begin
    cxGridViewNOMBRE_ALMACEN.GroupIndex := cxGridView.GroupedColumnCount;
    cxGridViewNOMBRE_ALMACEN.Visible := False;
  end
  else
  begin
    cxGridViewNOMBRE_ALMACEN.GroupIndex := -1;
    cxGridViewNOMBRE_ALMACEN.Visible := True;
  end;
end;

procedure TfrViewAlbaranesProveedor.actAlmacenUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := not (cxGridViewNOMBRE_ALMACEN.GroupIndex < 0);
end;

procedure TfrViewAlbaranesProveedor.actProveedorExecute(Sender: TObject);
begin
  if (cxGridViewNOMBRE.GroupIndex < 0) then
  begin
    cxGridViewNOMBRE.GroupIndex := cxGridView.GroupedColumnCount;
    cxGridViewNOMBRE.Visible := False;
  end
  else
  begin
    cxGridViewNOMBRE.GroupIndex := -1;
    cxGridViewNOMBRE.Visible := True;
  end;
end;

procedure TfrViewAlbaranesProveedor.actProveedorUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Checked := not (cxGridViewNOMBRE.GroupIndex < 0);
end;

procedure TfrViewAlbaranesProveedor.AnadirFiltroFechas;
var
  Columna: TcxGridDBColumn;
  Fecha1, Fecha2: Variant;
  FFiltro : TcxFilterCriteriaItemList;
begin
  Fecha1 := frViewFiltroBase1.edtFechaIniFiltro.EditValue;
  Fecha2 := frViewFiltroBase1.edtFechaFinFiltro.EditValue;

  if  not VarIsNull(Fecha1)
  and not VarIsNull(Fecha2) then
  begin
    cxGridView.DataController.Filter.Options := [fcoCaseInsensitive, fcoSoftCompare];
    FFiltro := AddFilterGrid(fboAnd);

    Columna := (cxGridView as TcxGridDBTableView).GetColumnByFieldName('FECHA_ALBARAN');
    FFiltro.AddItem(Columna, foBetween, varArrayOf([Fecha1, Fecha2]), VarToStr(Fecha1) + ' and ' + VarToStr(Fecha2));
  end;
end;

procedure TfrViewAlbaranesProveedor.AnadirOtrosFiltros;
begin
  inherited;

  AnadirFiltroFechas;

  //Finalmente activamos el filtro si tenemos algo
  if cxGridView.DataController.Filter.IsEmpty
  then cxGridView.DataController.Filter.Active := False
  else cxGridView.DataController.Filter.Active := True;
  cxGrid.ActiveLevel.GridView := cxGridView;
end;

procedure TfrViewAlbaranesProveedor.cxGridActiveTabChanged(Sender: TcxCustomGrid; ALevel: TcxGridLevel);
begin
  inherited;
  RefrescarFiltro;
end;

procedure TfrViewAlbaranesProveedor.cxGridViewDataControllerCompare(
  ADataController: TcxCustomDataController; ARecordIndex1, ARecordIndex2,
  AItemIndex: Integer; const V1, V2: Variant; var Compare: Integer);
begin
  inherited;
  if ((AItemIndex = cxGridViewREFERENCIA.Index) or
      (AItemIndex = cxGridViewREF_PED_PROVEEDOR.Index) or
      (AItemIndex = cxGridViewREF_FACTURA_PROV.Index) or
      (AItemIndex = cxGridViewREFERENCIA_PROVEEDOR.Index)) and
     (VarType(V1) = VarType(V2)) and (VarType(V1) = varString) then
    Compare := CompararReferencias(V1, V2)
  else
    Compare := VarCompare(V1, V2);
end;

procedure TfrViewAlbaranesProveedor.cxGridViewINCIDENCIAS_ACTIVASGetCellHint(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  ACellViewInfo: TcxGridTableDataCellViewInfo; const AMousePos: TPoint;
  var AHintText: TCaption; var AIsHintMultiLine: Boolean;
  var AHintTextRect: TRect);
begin
  inherited;

  if (ARecord.Values[Sender.Index] = '1') then
  begin
    AIsHintMultiLine := True;
    AHintText := ARecord.Values[cxGridViewINCIDENCIAS.Index]
  end;
end;

procedure TfrViewAlbaranesProveedor.cxGridViewNUM_COPIASCustomDrawHeader(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridColumnHeaderViewInfo; var ADone: Boolean);
var
  R : TRect;
begin
  inherited;
  with AViewInfo do
  begin
    LookAndFeelPainter.DrawHeader(ACanvas, Bounds, TextAreaBounds, Neighbors,
      Borders, ButtonState, AlignmentHorz, AlignmentVert, MultiLine,
      False, '', Params.Font, Params.TextColor, Params.Color);

    R := AViewInfo.ContentBounds;
    ACanvas.FillRect(R);
    ACanvas.DrawImage(GridPNGImageList, R.Left + 2, R.Top, 2);
  end;

  ADone := True;
end;

procedure TfrViewAlbaranesProveedor.cxGridViewNUM_CORREOSCustomDrawHeader(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridColumnHeaderViewInfo; var ADone: Boolean);
var
  R : TRect;
begin
  inherited;
  with AViewInfo do
  begin
    LookAndFeelPainter.DrawHeader(ACanvas, Bounds, TextAreaBounds, Neighbors,
      Borders, ButtonState, AlignmentHorz, AlignmentVert, MultiLine,
      False, '', Params.Font, Params.TextColor, Params.Color);

    R := AViewInfo.ContentBounds;
    ACanvas.FillRect(R);
    ACanvas.DrawImage(GridPNGImageList, R.Left + 2, R.Top, 3);
  end;

  ADone := True;
end;

procedure TfrViewAlbaranesProveedor.cxGridViewStylesGetContentStyle(Sender:
        TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem:
        TcxCustomGridTableItem; out AStyle: TcxStyle);
{var
  IndiceCol: Integer;
  ASituacion: string;}
begin
  inherited;
{  if Assigned(ARecord) then
  begin
    IndiceCol := (Sender as TcxGridDBTableView).GetColumnByFieldName(fld_PresupuestosSITUACION).Index;
    ASituacion := VarToStr(ARecord.DisplayTexts[IndiceCol]);
    if ASituacion = SITUACION_ACEPTADO then
      AStyle := cxStyleAceptado
    else if ASituacion = SITUACION_RECHAZADO then
        AStyle := cxStyleRechazado;
  end;
}
end;

function TfrViewAlbaranesProveedor.GetAlbaranes: IBizAlbaranProveedor;
begin
  Result := FAlbaranes;
end;

procedure TfrViewAlbaranesProveedor.SetAlbaranes(const Value: IBizAlbaranProveedor);
begin
  FAlbaranes := Value;
  if Assigned(FAlbaranes) then
    dsDataSource.DataTable := FAlbaranes.DataTable;
end;

end.
