unit EditorEventos;
//Para poder ver el dfm de esta unidad es necesario abrir primero
//C:\Archivos de programa\Developer Express Inc\ExpressScheduler 2\Sources\cxSchedulerEventEditor

interface

uses Graphics, cxSchedulerDialogs, cxSchedulerEventEditor,
  Menus, cxLookAndFeelPainters, cxGraphics, StdCtrls,
  cxCheckComboBox, cxMemo, cxDropDownEdit, cxCheckBox, cxSpinEdit,
  cxTimeEdit, cxCalendar, cxMaskEdit, cxImageComboBox, cxTextEdit,
  cxControls, cxContainer, cxEdit, cxGroupBox, Controls, ExtCtrls,
  cxButtons, Classes,

  uBizCitas;   

type
  TfrEditorEventos = class(TcxSchedulerEventEditorForm)
    pnlObra: TPanel;
    lbObra: TLabel;
    cbObra: TcxComboBox;
    lbTipo: TLabel;
    cbTipo: TcxComboBox;
    teCodigoContrato: TEdit;
    teNombreCliente: TEdit;
    lbPartida: TLabel;
    cbPartida: TcxComboBox;
    pnlCompletada: TPanel;
    Bevel8: TBevel;
    cbCompletada: TcxCheckBox;
    deFechaCompletada: TcxDateEdit;
    Bevel9: TBevel;
    cbInstaladores: TcxComboBox;
    Bevel10: TBevel;
    cbMedicion: TcxCheckBox;
    procedure cbObraPropertiesEditValueChanged(Sender: TObject);
    procedure cbTipoPropertiesEditValueChanged(Sender: TObject);
    procedure cbTerminadoPropertiesChange(Sender: TObject);
    procedure deFechaCompletadaPropertiesChange(Sender: TObject);
  private
    FTipoEvento : TTipoCitas;
    procedure ApplyChanges;
  protected
    procedure DeleteEvent; override;
    procedure PostEvent; override;
    procedure SaveChanges; override;
    function GetFormColor: TColor; override;
    function GetResourcesPanelVisible: Boolean; override;
    procedure InitializeControls; override;
    procedure LoadEventValuesIntoControls; override;
    function IsValid: Boolean; override;
    procedure SetCaptions; override;
    procedure SetReadOnly(AValue: Boolean); override;
    procedure UpdateEventValuesFromControls; override;
    procedure SaveResourceID; override;
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  frEditorEventos: TfrEditorEventos;

implementation
{$R *.dfm}

uses SysUtils, Dialogs, Variants, DateUtils, cxSchedulerUtils,
schCitasClient_Intf, uDataModuleCalendarios;

{ TfrEditorEventos }

procedure TfrEditorEventos.ApplyChanges;
begin
  inherited;
  //
end;

constructor TfrEditorEventos.Create(AOwner: TComponent);
begin
  inherited;
end;

function TfrEditorEventos.GetFormColor: TColor;
begin
  Result := $00F9FEFF;
end;

procedure TfrEditorEventos.InitializeControls;
begin
  if not VarIsNull(Event.GetCustomFieldValueByName(fld_CITASTIPOTAREA)) then
    FTipoEvento := DarTipoCita(Event.GetCustomFieldValueByName(fld_CITASTIPOTAREA))
  else
    FTipoEvento := tcPersonal;

  inherited;

  pnlMessage.Visible := True;
  pnlCaption.Visible := True;

  if not (FTipoEvento in [tcPersonal, tcMedicion]) then
  begin
    pnlObra.Visible := True;
    if FTipoEvento in [tcPedido, tcConfirmacion, tcRecepcion, tcAviso, tcEntregaCliente] then
    begin
      pnlCompletada.Visible := True;
    end;
  end
  else begin
    pnlObra.Visible := False;
    cbMedicion.Visible := (FTipoEvento = tcMedicion)
  end;
end;

function TfrEditorEventos.IsValid: Boolean;
begin
  Result := inherited IsValid;

  if Result then
  begin
    if Length(teSubject.Text) = 0 then
    begin
      ShowMessage('Indique el asunto');
      Result := False;
    end;

    if (FTipoEvento in [tcEntrega, tcMontaje, tcRemate]) then
    begin
      if Length(cbInstaladores.EditValue) = 0 then
      begin
        ShowMessage('Hay que indicar el instalador');
        Result := False;
      end;
    end;
  end;
end;

procedure TfrEditorEventos.LoadEventValuesIntoControls;
var
  I, AResourceIndex: Integer;
begin
  inherited LoadEventValuesIntoControls;

  if icbLabel.ItemIndex = 6 then
    cbMedicion.Checked := True;


  if (Event.GetCustomFieldValueByName(fld_CITASTIPOTAREA) <> NULL) and
  not (Length(Event.GetCustomFieldValueByName(fld_CITASTIPOTAREA)) = 0) then
    cbTipo.EditValue := Event.GetCustomFieldValueByName(fld_CITASTIPOTAREA);

  if not (FTipoEvento in [tcPersonal, tcMedicion]) then
  begin
    if (Event.GetCustomFieldValueByName(fld_CITASCODIGOCONTRATO) <> NULL) and
    not (Length(Event.GetCustomFieldValueByName(fld_CITASCODIGOCONTRATO))=0) then
      teCodigoContrato.Text := Event.GetCustomFieldValueByName(fld_CITASCODIGOCONTRATO);

    if (Event.GetCustomFieldValueByName(fld_CITASNOMBRECLIENTE) <> NULL) and
    not (Length(Event.GetCustomFieldValueByName(fld_CITASNOMBRECLIENTE))=0) then
      teNombreCliente.Text := Event.GetCustomFieldValueByName(fld_CITASNOMBRECLIENTE);

    if (Event.GetCustomFieldValueByName(fld_CITASCOMPLETADA) <> NULL) then
    begin
      cbCompletada.Checked := True;
      deFechaCompletada.Date := DateOf(VarToDateTime(Event.GetCustomFieldValueByName(fld_CITASCOMPLETADA)));
    end;
  end;

  AResourceIndex := -1;
  with cbInstaladores do
  begin
    Properties.Items.BeginUpdate;
    try
      Properties.Items.Clear;
      for i := 0 to cbResources.Properties.Items.Count - 1 do
      begin
        Properties.Items.Add(cbResources.Properties.Items[i].Description);
        if cbResources.States[I] = cbsChecked then
          AResourceIndex := i;
      end;
    finally
      Properties.Items.EndUpdate;
    end;
    ItemIndex := AResourceIndex;
  end;
end;

procedure TfrEditorEventos.SetCaptions;
begin
  inherited SetCaptions;

  case FTipoEvento of
    tcEntrega,
    tcMontaje,
    tcRemate  :   lbResource.Caption := 'Instalador:';

    tcMedicion:   lbResource.Caption := 'Vendedor:';
  end;

  lbSubject.Caption := 'Asunto:';
  lbStartTime.Caption := 'Comienzo:';
  lbEndTime.Caption := 'Final:';
  cbAllDayEvent.Caption := 'Todo el día';
  btnOk.Caption := 'Aceptar';
  btnDelete.Caption := 'Eliminar';
  btnCancel.Caption := 'Cancelar';
end;

procedure TfrEditorEventos.SetReadOnly(AValue: Boolean);
begin
  inherited;
  if AValue then
  begin
    cbObra.Enabled:= False;
    cbPartida.Enabled:= False;
    cbTipo.Enabled:= False;
    deFechaCompletada.Enabled:= False;
    cbCompletada.Enabled:= False;
  end
  else
  begin
    cbObra.Enabled:= True;
    cbPartida.Enabled:= True;
    cbTipo.Enabled:= True;
    deFechaCompletada.Enabled:= True;
    cbCompletada.Enabled:= True;
  end;
end;

procedure TfrEditorEventos.UpdateEventValuesFromControls;
begin
  inherited UpdateEventValuesFromControls;

  SaveResourceID;

  try
    // post the data from the custom editing controls

    if not VarIsNull(teCodigoContrato.Text) then
      Event.SetCustomFieldValueByName(fld_CITASCODIGOCONTRATO, teCodigoContrato.Text);

    if not VarIsNull(teNombreCliente.Text) then
      Event.SetCustomFieldValueByName(fld_CITASNOMBRECLIENTE, teNombreCliente.Text);

    if not VarIsNull(cbPartida.EditValue) then
      Event.SetCustomFieldValueByName(fld_CITASPARTIDA, TextoPartidasObra[cbPartida.ItemIndex]);

    if not VarIsNull(deFechaCompletada.EditValue) then
      Event.SetCustomFieldValueByName(fld_CITASCOMPLETADA, deFechaCompletada.Date)
    else
      Event.SetCustomFieldValueByName(fld_CITASCOMPLETADA, Null);

    if not VarIsNull(cbTipo.EditValue) then
    begin
      FTipoEvento := DarTipoCita(cbTipo.EditValue);
      Event.SetCustomFieldValueByName(fld_CITASTIPOTAREA, TextoTipoCita[Ord(FTipoEvento)]);
    end
    else
      FTipoEvento := tcPersonal;

    if cbMedicion.Checked then
      Event.LabelColor := EventLabelColors[6]
    else
      Event.LabelColor := EventLabelColors[Ord(FTipoEvento) + 1];
  except
    on E: Exception do
      ShowMessage('Can''t post data' + #13#10 + E.Message);
  end;
end;

procedure TfrEditorEventos.cbObraPropertiesEditValueChanged(
  Sender: TObject);
begin
  inherited OnChanged(Sender);
  if Length(cbObra.EditValue) <> 0 then
  begin
    teCodigoContrato.Text := Copy(cbObra.EditValue, 0, Pos(' ', cbObra.EditValue)-1);
    teNombreCliente.Text := Copy(cbObra.EditValue, Pos(' ', cbObra.EditValue)+1, MaxInt);
  end
  else begin
    teCodigoContrato.Text := '0';
    teNombreCliente.Text := '';
  end;
end;

procedure TfrEditorEventos.cbTipoPropertiesEditValueChanged(
  Sender: TObject);
begin
  inherited OnChanged(Sender);
  icbLabel.ItemIndex := cbTipo.ItemIndex + 1;
  icbLabel.EditModified := True;
end;

procedure TfrEditorEventos.cbTerminadoPropertiesChange(Sender: TObject);
begin
  deFechaCompletada.Enabled := cbCompletada.Checked;
  if not cbCompletada.Checked then
    deFechaCompletada.Clear
  else
    if Length(deFechaCompletada.EditValue) = 0 then
      deFechaCompletada.Date := DateOf(Now);

  FModified := True;
  CheckVisible;
end;

procedure TfrEditorEventos.deFechaCompletadaPropertiesChange(
  Sender: TObject);
begin
  FModified := True;
end;

procedure TfrEditorEventos.DeleteEvent;
begin
  inherited;
end;

procedure TfrEditorEventos.PostEvent;
begin
  inherited;
end;

procedure TfrEditorEventos.SaveChanges;
begin
  inherited;
end;

function TfrEditorEventos.GetResourcesPanelVisible: Boolean;
begin
  Result := not (FTipoEvento in [tcPedido, tcConfirmacion, tcRecepcion, tcAviso, tcEntregaCliente]);
end;

procedure TfrEditorEventos.SaveResourceID;
var
  I: Integer;
begin
  if pnlResource.Visible then
  begin
    for I := 0 to cbResources.Properties.Items.Count - 1 do
      cbResources.States[I] := cbsUnchecked;
    cbResources.States[cbInstaladores.ItemIndex] := cbsChecked;
  end
  else
  begin
    showmessage('debo poner a nulo el valor de resources');
    for I := 0 to cbResources.Properties.Items.Count - 1 do
      cbResources.States[I] := cbsUnchecked;
  end;
  inherited;
end;

initialization
  cxEventEditorClass := TfrEditorEventos; // indicate that the new Event modal dialog will be invoked at runtime

end.
