unit uEditorControlResumenVentas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uEditorControlBase, DB, uDADataTable, JvAppStorage,
  JvAppRegistryStorage, JvComponentBase, JvFormPlacement, ImgList,
  PngImageList, StdActns, ActnList, ComCtrls, TBX, TB2Item, TB2Dock,
  TB2Toolbar, ExtCtrls, JvExControls, JvComponent, JvNavigationPane, uBizControlesBase,
  uCustomView, uViewBase, uViewControlBase, Grids, DBGrids, pngimage,
  uViewControlVentasTerminadas, uEditorGridBase, uViewGridBase, uViewGrid,
  uViewControGridResumenVentas, Menus, TB2ExtItems, TBXExtItems,
  uBizVentasTerminadas, StdCtrls;

type
  IEditorControlResumenVentas = interface(IEditorGridBase)
  ['{FC3218D5-319A-4ECF-88E9-0CB8655EDFA9}']
    function GetItems: IBizResumenVentas;
    procedure SetItems(const Value: IBizResumenVentas);
    property Items: IBizResumenVentas read GetItems write SetItems;
  end;

  TfEditorControlResumenVentas = class(TfEditorGridBase, IEditorControlResumenVentas)
    dsVentasTerminadas: TDADataSource;
    TBXSeparatorItem16: TTBXSeparatorItem;
    tbxiExpandir: TTBXItem;
    tbxiContraer: TTBXItem;
    tbxiAnchoAuto: TTBXItem;
    actExpandir: TAction;
    actContraer: TAction;
    actAnchoAuto: TAction;
    TBXItem37: TTBXItem;
    TBXItem36: TTBXItem;
    TBXItem35: TTBXItem;
    TBXSeparatorItem17: TTBXSeparatorItem;
    edtMes: TTBXComboBoxItem;
    edtAno: TTBXSpinEditItem;
    frViewControlGridResumenVentas1: TfrViewControlGridResumenVentas;
    TBXLabelItem2: TTBXLabelItem;
    TBXLabelItem3: TTBXLabelItem;
    TBXSeparatorItem18: TTBXSeparatorItem;
    edtColeccion: TTBXEditItem;
    TBXLabelItem4: TTBXLabelItem;
    TBXSeparatorItem19: TTBXSeparatorItem;
    procedure actAnchoAutoExecute(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure actRefrescarExecute(Sender: TObject);
    procedure actPrevisualizarExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
  protected
    FItems : IBizResumenVentas;
    function GetItems: IBizResumenVentas;
    procedure SetItems(const Value: IBizResumenVentas);
  public
    property Items: IBizResumenVentas read GetItems write SetItems;
    constructor Create(AOwner: TComponent); override;
  end;


implementation

uses
  cxControls, uEditorUtils, DateUtils, uROClasses, cxGridTableView, uCustomEditor, uEditorBase;

{$R *.dfm}

{ TfEditorControlVentasTerminadas }

procedure TfEditorControlResumenVentas.actAnchoAutoExecute(Sender: TObject);
begin
  inherited;
  frViewControlGridResumenVentas1.AjustarAncho;
end;

procedure TfEditorControlResumenVentas.FormShow(Sender: TObject);
begin
  inherited;

  if not Assigned(FItems) then
    raise Exception.Create('No hay items asignados');

  if not Assigned(ViewGrid) then
    raise Exception.Create('No hay una vista asignada');

  //Inicializar
  edtMes.ItemIndex := (MonthOf(Today)-1);
  edtAno.Value := YearOf(Today);
end;

procedure TfEditorControlResumenVentas.actImprimirExecute(Sender: TObject);
begin
  inherited;
  frViewControlGridResumenVentas1.Print(edtMes.Text, edtAno.Text);
end;

constructor TfEditorControlResumenVentas.Create(AOwner: TComponent);
begin
  inherited;
  ViewGrid := frViewControlGridResumenVentas1;
  Self.Caption := 'Resumen de ventas mensuales por centro';
  JvNavPanelHeader.Caption := Self.Caption;
end;

function TfEditorControlResumenVentas.GetItems: IBizResumenVentas;
begin
  Result := FItems;
end;

procedure TfEditorControlResumenVentas.SetItems(
  const Value: IBizResumenVentas);
begin
  FItems := Value;
  if Assigned(FItems) then
  begin
    dsDataTable.DataTable := FItems.DataTable;
    frViewControlGridResumenVentas1.dsDataSource.DataTable := FItems.DataTable;
  end;
end;

procedure TfEditorControlResumenVentas.actRefrescarExecute(Sender: TObject);
var
  AuxDate: TDateTime;
  UltimoDia: Integer;
  i : Integer;

begin
  AuxDate := EncodeDate(edtAno.AsInteger , (edtMes.ItemIndex + 1), 1);

  ShowHourglassCursor;
  try
    with FItems do
    begin
      if DataTable.Active then
          DataTable.Active := False;

      DataTable.ParamByName('FECHAINI').AsString := DateToStr(StartOfTheMonth(AuxDate));
      DataTable.ParamByName('FECHAFIN').AsString := DateToStr(EndOfTheMonth(AuxDate));
      DataTable.ParamByName('COLECCION').AsString := edtColeccion.Text;

      if not DataTable.Active then
        DataTable.Active := True;
    end;
  finally
    HideHourglassCursor;
  end;
end;

procedure TfEditorControlResumenVentas.actPrevisualizarExecute(Sender: TObject);
begin
  frViewControlGridResumenVentas1.Preview(edtMes.Text, edtAno.Text);
end;

end.

