unit uViewControGridResumenVentas;

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;

type
  IViewControlGridResumenVentas = interface(IViewGrid)
  ['{E6D7FF0F-7E64-4E8C-8844-CAC011A62CDE}']
  end;

  TfrViewControlGridResumenVentas = class(TfrViewGrid, IViewControlGridResumenVentas)
    dxComponentPrinterLink1: TdxGridReportLink;
    cxEditRepository1: TcxEditRepository;
    cxEditDia: TcxEditRepositoryTextItem;
    cxEditImporte: TcxEditRepositoryCurrencyItem;
    cxStyleDia: TcxStyle;
    cxStyleTotal: TcxStyle;
    cxStyleImporte: TcxStyle;
    procedure cxGridViewCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure dsDataSourceStateChange(Sender: TObject);
  private
    procedure LimpiarGrid;
    procedure GenerarGrid;
  public
    procedure Preview(Mes: String; Ano: String); overload;
    procedure Print(Mes: String; Ano: String); overload;
  end;

implementation
{$R *.dfm}

uses schVentasTerminadasClient_Intf, uViewGridBase;

{ TfrViewControlGridResumenVentas }

procedure TfrViewControlGridResumenVentas.Preview(Mes: String; Ano: String);
begin
  inherited Preview;
  dxComponentPrinterLink1.ReportTitle.Text := 'Ventas terminadas ' + Mes + ' de ' + Ano;
  dxComponentPrinterLink1.Preview;
end;

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

procedure TfrViewControlGridResumenVentas.dsDataSourceStateChange(Sender: TObject);
begin
  if dsDataSource.State = dsInactive
  then LimpiarGrid
  else GenerarGrid;
end;

procedure TfrViewControlGridResumenVentas.GenerarGrid;
var
  i, AuxPos, AuxLen: Integer;

begin
   cxGridView.BeginUpdate;

   try
     cxGridView.DataController.CreateAllItems;

     for i := 0 to cxGridView.ColumnCount - 1 do
     begin
       case i of
         0: cxGridView.Columns[i].Visible := False;
         1: begin
              cxGridView.Columns[i].Styles.Content := cxStyleDia;
              cxGridView.Columns[i].Properties := cxEditDia.Properties;
              cxGridView.Columns[i].HeaderAlignmentHorz := taRightJustify;
              cxGridView.Columns[i].FooterAlignmentHorz := taRightJustify;
              cxGridView.Columns[i].Width := 30;
              cxGridView.Columns[i].SortIndex := 0;
              cxGridView.Columns[i].SortOrder := soAscending;

            end;
         else begin
           cxGridView.Columns[i].Styles.Content := cxStyleImporte;
           cxGridView.Columns[i].PropertiesClassName := 'TcxCurrencyEditProperties';
           cxGridView.Columns[i].Properties := cxEditImporte.Properties;
           cxGridView.Columns[i].Summary.FooterKind := skSum;
           cxGridView.Columns[i].Summary.FooterFormat := ',0.## €;-,0.## €';
           cxGridView.Columns[i].HeaderAlignmentHorz := taRightJustify;
           cxGridView.Columns[i].FooterAlignmentHorz := taRightJustify;
           cxGridView.Columns[i].Width := 175;           
         end;
       end;

       if i = 2 then
       begin
          cxGridView.Columns[i].HeaderAlignmentHorz := taLeftJustify;
          cxGridView.Columns[i].Styles.Content := cxStyleTotal;
       end;

       //Dejamos de la descripción del centro lo más corta y representativa posible
       AuxPos := Pos('INGLES', cxGridView.Columns[i].Caption);
       if AuxPos > 0 then AuxPos := AuxPos + 6;
       AuxLen := Length(cxGridView.Columns[i].Caption) - AuxPos;
       cxGridView.Columns[i].Caption := Trim(Copy(cxGridView.Columns[i].Caption, AuxPos, AuxLen));
     end;

   finally
     cxGridView.EndUpdate;
   end;
end;

procedure TfrViewControlGridResumenVentas.LimpiarGrid;
begin
   cxGridView.BeginUpdate;
   try
     cxGridView.ClearItems;
   finally
     cxGridView.EndUpdate;
   end;
end;

procedure TfrViewControlGridResumenVentas.Print(Mes, Ano: String);
begin
  inherited Print;
  dxComponentPrinterLink1.ReportTitle.Text := 'Ventas terminadas ' + Mes + ' de ' + Ano;
  dxComponentPrinterLink1.Print(True, Nil);
end;

end.

