unit CalendarioMontajes;

interface

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

type
  TfrCalendarioMontajes = class(TfrCalendarioBasico)
    TablaCitas: TIBDataSet;
    dsCitas: TDataSource;
    dsInstaladores: TDataSource;
    TablaInstaladores: TIBDataSet;
    pnlInstaladores: TPanel;
    cxResources: TcxCheckListBox;
    Panel2: TPanel;
    Panel3: TPanel;
    bAnadir: TRdxBoton;
    bEliminar: TRdxBoton;
    ImageList1: TImageList;
    procedure RdxFrameShow(Sender: TObject);
    procedure cxResourcesEditValueChanged(Sender: TObject);
    procedure actVerTodosExecute(Sender: TObject);
    procedure cxSchedulerGetEventDisplayText(Sender: TObject;
      AEvent: TcxSchedulerControlEvent; var AText: String);
    procedure cxSchedulerGetEventHintText(Sender: TObject;
      AEvent: TcxSchedulerControlEvent; var AText: String);
  private
    procedure TablaCitasBeforePost(DataSet: TDataSet);
    procedure TablaCitasAfterPost(DataSet: TDataSet);
    procedure CargarListaInstaladores;
  protected
    procedure InicializarEvento (var aEvent : TcxSchedulerControlEvent); override;
    procedure InicializarCampos; override;
  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
  end;

var
  frCalendarioMontajes: TfrCalendarioMontajes;

implementation

uses BaseDatos, TablaCitas, TablaObras;

{$R *.dfm}

{ TfrCalendarioMontajes }

procedure TfrCalendarioMontajes.CargarListaInstaladores;
var
  I: Integer;
begin
  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;
end;

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

  dsCitas.DataSet := TablaCitas;
  dsInstaladores.DataSet := TablaInstaladores;

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

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

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

destructor TfrCalendarioMontajes.Destroy;
begin
  TablaCitas.Close;
  TablaInstaladores.Close;
  inherited;
end;

procedure TfrCalendarioMontajes.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 TfrCalendarioMontajes.InicializarEvento(
  var aEvent: TcxSchedulerControlEvent);
begin
  inherited;
  with AEvent do
  begin
    SetCustomFieldValueByName('TIPOTAREA', TextoTipoCita[Ord(tcMontaje)]);
    AllDayEvent := True;
  end;
end;

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

procedure TfrCalendarioMontajes.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 TfrCalendarioMontajes.RdxFrameShow(Sender: TObject);
begin
  inherited;
  try
    cxScheduler.BeginUpdate;

    TablaInstaladores.Open;
    TablaCitas.Open;

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

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

procedure TfrCalendarioMontajes.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 TfrCalendarioMontajes.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 TfrCalendarioMontajes.cxSchedulerGetEventDisplayText(
  Sender: TObject; AEvent: TcxSchedulerControlEvent; var AText: String);
var
  Nombre : String;
  Tipo : String;
begin
  inherited;

  if not VarIsNull(AEvent.GetCustomFieldValueByName('NOMBRECLIENTE')) then
  begin
    Nombre := VarToStr(AEvent.GetCustomFieldValueByName('NOMBRECLIENTE'));
    Tipo := VarToStr(AEvent.GetCustomFieldValueByName('TIPOTAREA'));
    AText := AEvent.Caption + ' - ' + Tipo + ' (' + Nombre + ')';
  end;
end;

procedure TfrCalendarioMontajes.cxSchedulerGetEventHintText(
  Sender: TObject; AEvent: TcxSchedulerControlEvent; var AText: String);
var
  Nombre : String;
  Tipo : String;
begin
  inherited;
  if not VarIsNull(AEvent.GetCustomFieldValueByName('NOMBRECLIENTE')) then
  begin
    Nombre := VarToStr(AEvent.GetCustomFieldValueByName('NOMBRECLIENTE'));
    Tipo := VarToStr(AEvent.GetCustomFieldValueByName('TIPOTAREA'));
    AText := AEvent.Caption + #10#13 + Tipo + ' (' + Nombre + ')';
  end;
end;

end.
