unit uViewControGridStockPrendasCentros;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewControlGrid, DB, uDADataTable, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData,
  cxTextEdit, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxCurrencyEdit, cxCalendar, dxPSGlbl, dxPSUtl, dxPrnPg, dxBkgnd, dxWrap,
  dxPrnDev, dxPSEngn, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPSCore, dxPScxCommon, dxPScxGridLnk, dxPgsDlg,
  cxDropDownEdit, StdCtrls, cxSpinEdit, uViewGrid, cxMemo,
  cxEditRepositoryItems, uDataModuleStock, uCustomView, uViewBase,
  uViewParametrosControlGrid, Grids, DBGrids, uDAInterfaces, TBXDkPanels,
  uViewParametrosCentro, uViewParametrosColeccion, TB2Dock, uBizStock,
  uViewParametrosFecha;

type
  IViewControlGridStockPrendasCentros = interface(IViewGrid)
  ['{E6251B25-10FE-4D0C-94CD-0E554F88B54D}']
    function GetItems: IBizStockPrendasCentros;
    procedure SetItems(const Value: IBizStockPrendasCentros);
    property Items: IBizStockPrendasCentros read GetItems write SetItems;
  end;

  TfrViewControlGridStockPrendasCentros = class(TfrViewGrid, IViewControlGridStockPrendasCentros)
    dxComponentPrinterLink1: TdxGridReportLink;
    cxStyleEntrada: TcxStyle;
    cxStyleSalida: TcxStyle;
    cxStyleRepositoryInforme: TcxStyleRepository;
    cxStyleContentInforme: TcxStyle;
    cxStyleFooterInforme: TcxStyle;
    cxStyleGroupInforme: TcxStyle;
    cxStyleHeaderInforme: TcxStyle;
    cxStyleSelectionInforme: TcxStyle;
    cxStyleTotales: TcxStyle;
    cxGridViewCODIGO: TcxGridDBColumn;
    cxGridViewPRODUCTO: TcxGridDBColumn;
    cxGridViewTIPO_PRODUCTO: TcxGridDBColumn;
    cxGridViewGRUPO_PRODUCTO: TcxGridDBColumn;
    cxGridViewCOLECCION: TcxGridDBColumn;
    cxGridViewCOLOR: TcxGridDBColumn;
    cxGridView38: TcxGridDBColumn;
    cxGridView40: TcxGridDBColumn;
    cxGridView42: TcxGridDBColumn;
    cxGridView44: TcxGridDBColumn;
    cxGridView46: TcxGridDBColumn;
    cxGridView48: TcxGridDBColumn;
    cxGridViewTOTAL: TcxGridDBColumn;
    TBXDockablePanel1: TTBXDockablePanel;
    frViewParametrosColeccion1: TfrViewParametrosColeccion;
    pnlAgrupaciones: TTBXAlignmentPanel;
    TBXButton3: TTBXButton;
    TBXLabel1: TTBXLabel;
    frViewParametrosFecha1: TfrViewParametrosFecha;
    procedure cxGridViewKTCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure cxGridViewCOLUMN7StylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure cxGridView38GetDataText(Sender: TcxCustomGridTableItem; ARecordIndex: Integer; var AText: String);
  protected
    FItems : IBizStockPrendasCentros;
    function GetFocusedView : TcxGridDBTableView; override;
    function GetItems: IBizStockPrendasCentros;
    procedure SetItems(const Value: IBizStockPrendasCentros);

  public
    property Items: IBizStockPrendasCentros read GetItems write SetItems;
    procedure Preview; override;
    procedure Print; override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Refresh; 
  end;

implementation
{$R *.dfm}

uses uViewGridBase;

{ TfrViewControlGridResumenVentas }

procedure TfrViewControlGridStockPrendasCentros.Preview;
begin
  inherited;
//  dxComponentPrinterLink1.ReportTitle.Text := 'Resumen de stock por centros de colección: ' + frViewFiltrosStock1.cbColeccion.Text;
  dxComponentPrinterLink1.Preview;
end;

procedure TfrViewControlGridStockPrendasCentros.Print;
begin
  inherited;
//  dxComponentPrinterLink1.ReportTitle.Text := 'Resumen de stock por centros de colección: ' + frViewFiltrosStock1.cbColeccion.Text;
  dxComponentPrinterLink1.Print(True, Nil);
end;

procedure TfrViewControlGridStockPrendasCentros.cxGridViewKTCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  inherited;
  if not VarIsNull(AViewInfo.Value) then
    if Pos('-', AViewInfo.Value) > 0 then
       ACanvas.Canvas.Font.Color := $000000CC;
end;

destructor TfrViewControlGridStockPrendasCentros.Destroy;
begin
  FItems := Nil;
  inherited;
end;

constructor TfrViewControlGridStockPrendasCentros.Create(AOwner: TComponent);
begin
  inherited;
  FItems := Nil;
  frViewParametrosColeccion1.Refresh;
end;

function TfrViewControlGridStockPrendasCentros.GetFocusedView: TcxGridDBTableView;
begin
  Result := cxGridView;
end;

function TfrViewControlGridStockPrendasCentros.GetItems: IBizStockPrendasCentros;
begin
  Result := FItems;
end;

procedure TfrViewControlGridStockPrendasCentros.SetItems(const Value: IBizStockPrendasCentros);
begin
  FItems := Value;
  if Assigned(FItems) then
     dsDataSource.DataTable := FItems.DataTable;
end;

procedure TfrViewControlGridStockPrendasCentros.cxGridViewCOLUMN7StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  inherited;
  AStyle := cxStyleTotales;
end;

procedure TfrViewControlGridStockPrendasCentros.Refresh;
var
  AFecha : Variant;
  AColeccionesList : TStringList;
  ACentrosList : TStringList;
  AWhereColeccion : String;
  AWhereCentro : String;
  AWhereFecha : String;
  AWhere : String;
  i : integer;
begin
  AColeccionesList := Nil;

  if Assigned(FItems) then
     Items.DataTable.Active := False;
  AColeccionesList := frViewParametrosColeccion1.ColeccionesSeleccionadas;
  AFecha := frViewParametrosFecha1.Fecha;

  ShowHourglassCursor;
  try
    if Assigned(AColeccionesList) then
    begin
       AWhereColeccion := '';
       if AColeccionesList.Count = 0 then
       begin
          ShowMessage('Debe seleccionar al menos una colección en los parámetros de la consulta.');
          Exit;
       end
       else begin
          AWhereColeccion := AWhereColeccion + ' COLECCION in (';
          for i := 0 to AColeccionesList.Count - 1 do
          begin
            if i > 0 then
              AWhereColeccion := AWhereColeccion + ', ';
            AWhereColeccion := AWhereColeccion + '''' + AColeccionesList[i] + '''';
          end;
          AWhereColeccion := AWhereColeccion + ')';
        end;
    end;

    AWhereFecha := '';
    if not VarIsNull(AFecha) then
       AWhereFecha := '(FECHA >= ''' + VarToStr(AFecha) + ''')';

    AWhere := '';
    if AWhereColeccion <> '' then
       AWhere := AWhere + AWhereColeccion;

    if AWhereFecha <> '' then
    begin
       if AWhere <> '' then
          AWhere := AWhere + ' AND ';
       AWhere := AWhere + AWhereFecha;
    end;

    Items.DataTable.Where.Clear;
    Items.DataTable.Where.AddText(AWhere, False);
    Items.DataTable.Active := True;

  finally
    HideHourglassCursor;
    FreeAndNIL(AColeccionesList);
  end;
end;

procedure TfrViewControlGridStockPrendasCentros.cxGridView38GetDataText(
  Sender: TcxCustomGridTableItem; ARecordIndex: Integer;
  var AText: String);
begin
  inherited;
  if AText = '0' then
    AText := ''; 
end;

end.

