unit CalendarioMediciones;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CalendarioBasico, Menus, cxStyles, cxGraphics, cxEdit,
  cxScheduler, cxSchedulerStorage, cxSchedulerCustomControls,
  cxSchedulerCustomResourceView, cxSchedulerDayView,
  cxSchedulerDateNavigator, cxSchedulerWeekView, DB, IBCustomDataSet,
  IBDatabase, IBTable, ActnList, cxSchedulerDBStorage, cxControls,
  RdxBotones, ExtCtrls, RdxPaneles, RdxBarras, cxContainer, cxCheckListBox,
  StdCtrls, JvAppStorage, JvComponent, JvFormPlacement,
  JvAppRegistryStorage, dxPSGlbl, dxPSUtl, dxPSEngn, dxPrnPg, dxBkgnd,
  dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns, dxPSEdgePatterns,
  dxPSCore, dxPSContainerLnk, dxPScxCommon, dxPrnDlg,
  cxSchedulerTimeGridView, cxSchedulerUtils, cxSchedulerYearView,
  dxPScxScheduler2Lnk, ImgList;

type
  TfrCalendarioMediciones = class(TfrCalendarioBasico)
    dsVendedores: TDataSource;
    dsCitas: TDataSource;
    pnlVendedores: TPanel;
    cxResources: TcxCheckListBox;
    Panel2: TPanel;
    Panel3: TPanel;
    bAnadir: TRdxBoton;
    bEliminar: TRdxBoton;
    TablaVendedores: TIBDataSet;
    TablaCitas: TIBDataSet;
    JvFormStorage1: TJvFormStorage;
    JvAppRegistryStorage1: TJvAppRegistryStorage;
    ImageList1: TImageList;
    procedure RdxFrameShow(Sender: TObject);
    procedure cxResourcesEditValueChanged(Sender: TObject);
    procedure actVerTodosExecute(Sender: TObject);
    procedure actImprimirExecute(Sender: TObject);
    procedure cxSchedulerInitEventImages(Sender: TcxCustomScheduler;
      AEvent: TcxSchedulerControlEvent; AImages: TcxSchedulerEventImages);
  private
    procedure TablaCitasBeforePost(DataSet: TDataSet);
    procedure TablaCitasAfterPost(DataSet: TDataSet);
    procedure CargarListaVendedores;
  protected
    procedure InicializarEvento (var aEvent : TcxSchedulerControlEvent); override;
    procedure InicializarCampos; override;
  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
  end;

var
  frCalendarioMediciones: TfrCalendarioMediciones;

implementation

uses BaseDatos, TablaCitas, EditorEventos;

{$R *.dfm}

{ TfrCalendarioMediciones }

constructor TfrCalendarioMediciones.Create(AOwner: TComponent);
begin
  inherited;

  dsCitas.DataSet := TablaCitas;
  dsVendedores.DataSet := TablaVendedores;

  with TablaCitas do
  begin
    Database := dmBaseDatos.BD;
    Transaction := dmBaseDatos.Transaccion;
    SelectSQL.Assign(dmTablaCitas.sqlConsultar);
    SelectSQL.Add('WHERE TIPOTAREA = ''' + TextoTipoCita[Ord(tcMedicion)] + '''');
    InsertSQL.Assign(dmTablaCitas.sqlInsertar);
    ModifySQL.Assign(dmTablaCitas.sqlModificar);
    DeleteSQL.Assign(dmTablaCitas.sqlEliminar);
    RefreshSQL.Assign(dmTablaCitas.sqlRefrescar);
    RefreshSQL.Add('and TIPOTAREA = ''' + TextoTipoCita[Ord(tcMedicion)]  + '''');

    BeforePost := TablaCitasBeforePost;
    AfterPost := TablaCitasAfterPost;
    Prepare;
  end;

  with TablaVendedores do
  begin
    Database := dmBaseDatos.BD;
    Transaction := dmBaseDatos.Transaccion;
    SelectSQL.Add('select * from VENDEDORES');
    Prepare;
  end;

end;

destructor TfrCalendarioMediciones.Destroy;
var
  i : integer;
  cadena : string;
begin
  cadena := '#';
  for i := 0 to cxResources.Items.Count - 1 do
    if cxResources.Items[i].Checked then
      cadena := cadena + IntToStr(i) + '#';

  JvFormStorage1.WriteString('Mediciones', cadena);

  TablaCitas.Close;
  TablaVendedores.Close;
  inherited;
end;

procedure TfrCalendarioMediciones.RdxFrameShow(Sender: TObject);
begin
  inherited;
  try
    cxScheduler.BeginUpdate;

    TablaVendedores.Open;
    TablaCitas.Open;

    cxSchedulerDBStorage.DataSource := dsCitas;
    cxSchedulerDBStorage.Resources.DataSource := dsVendedores;

    CargarListaVendedores;
  finally;
    cxScheduler.EndUpdate;
  end;
  actSemana.Execute;  
end;

procedure TfrCalendarioMediciones.InicializarEvento(
  var aEvent: TcxSchedulerControlEvent);
begin
  inherited;
  with AEvent do
  begin
    SetCustomFieldValueByName('TIPOTAREA', TextoTipoCita[Ord(tcMedicion)]);
  end;
end;

procedure TfrCalendarioMediciones.InicializarCampos;
var
  aField : TcxSchedulerDBStorageField;
begin
  inherited;
  Exit;
  with cxSchedulerDBStorage.CustomFields do
  begin
    try
      BeginUpdate;
      Clear;

      aField := Add;
      aField.FieldName := 'TIPOTAREA';

      aField := Add;
      aField.FieldName := 'CODIGOCONTRATO';

      aField := Add;
      aField.FieldName := 'NOMBRECLIENTE';

      aField := Add;
      aField.FieldName := 'PARTIDA';

      aField := Add;
      aField.FieldName := 'COMPLETADA';

      aField := Add;
      aField.FieldName := 'NOMBREPROVEEDOR';
    finally
      EndUpdate;
    end;
  end;
end;

procedure TfrCalendarioMediciones.TablaCitasBeforePost(DataSet: TDataSet);
begin
  if (DataSet.FieldByName('ID').AsVariant = -1) or
     (VarIsNull(DataSet.FieldByName('ID').AsVariant)) then
  begin
    DataSet.Edit;
    DataSet.FieldByName('ID').AsVariant := dmTablaCitas.DarNuevoCodigo;
  end;
end;

procedure TfrCalendarioMediciones.TablaCitasAfterPost(DataSet: TDataSet);
begin
  dmBaseDatos.Transaccion.CommitRetaining;
end;

procedure TfrCalendarioMediciones.CargarListaVendedores;
var
  I: Integer;
  cadena : string;
begin
  cadena := JvFormStorage1.ReadString('Mediciones', '');

  cxResources.Items.BeginUpdate;
  try
    cxResources.Items.Clear;

    for I := 0 to cxScheduler.Storage.ResourceCount - 1 do
    begin
      with cxResources.Items.Add do
      begin
        ImageIndex := 0;
        Text := cxScheduler.Storage.ResourceNames[I];
        Checked := cxScheduler.Storage.Resources.ResourceItems[I].Visible;
        ItemObject := cxScheduler.Storage.Resources.ResourceItems[I];
      end;
    end;
  finally
    cxResources.Items.EndUpdate;
  end;

  for I := 0 to cxResources.Items.Count - 1 do
    if (Length(cadena) > 0) and (Pos('#' + IntToStr(i) + '#', cadena) = 0) then
    begin
      cxResources.Items[i].Checked := False;
      (cxResources.Items[i].ItemObject as TcxSchedulerStorageResourceItem).Visible := False;
    end;
end;

procedure TfrCalendarioMediciones.cxResourcesEditValueChanged(
  Sender: TObject);
var
  AIndex: Integer;
begin
  AIndex := cxResources.ItemIndex;

  if AIndex < 0 then
    Exit;
  with cxResources.Items[AIndex] do
    TcxSchedulerStorageResourceItem(ItemObject).Visible := Checked;

  if cxScheduler.Storage.Resources.ResourceItems.VisibleResourceCount = 0 then
  begin
    cxResources.Items[AIndex].Checked := True;
    Exit;
  end;
end;

procedure TfrCalendarioMediciones.actVerTodosExecute(Sender: TObject);
var
  I: Integer;
begin
  cxResources.Items.BeginUpdate;
  try
    for I := 0 to cxResources.Items.Count - 1 do
    begin
      cxResources.Items[I].Checked := True;
      TcxSchedulerStorageResourceItem(cxResources.Items[I].ItemObject).Visible := True;
    end;
  finally
    cxResources.Items.EndUpdate;

  end;
end;

procedure TfrCalendarioMediciones.actImprimirExecute(Sender: TObject);
begin
  inherited;
  //
end;

procedure TfrCalendarioMediciones.cxSchedulerInitEventImages(
  Sender: TcxCustomScheduler; AEvent: TcxSchedulerControlEvent;
  AImages: TcxSchedulerEventImages);
begin
  inherited;
  if not cxScheduler.ViewWeeks.Active then
    if AEvent.LabelColor = EventLabelColors[6] then
      AImages.Add(0);
end;

end.
