unit EditorEventos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxSchedulerEventEditor, cxLookAndFeelPainters, cxMemo,
  cxDropDownEdit, cxCheckBox, cxSpinEdit, cxTimeEdit, cxCalendar, ExtCtrls,
  cxMaskEdit, cxImageComboBox, cxControls, cxContainer, cxEdit, cxTextEdit,
  StdCtrls, cxButtons, cxSchedulerStorage, TablaCitas, Menus, cxGraphics,
  cxCheckComboBox, cxGroupBox;

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 CargarObras;
    procedure CargarTipos;
    procedure CargarPartidas;
    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 CreateEx(AEvent: TcxSchedulerControlEvent); override;
    constructor Create(AOwner: TComponent); override;
  end;

var
  frEditorEventos: TfrEditorEventos;

implementation

{$R *.dfm}

uses
  cxSchedulerDialogs, Configuracion, StrFunc, cxSchedulerUtils, TablaObras,
  DateUtils, Mensajes;

{ TfrEditorEventos }

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

procedure TfrEditorEventos.CargarObras;
var
  I, J, AResourceIndex: Integer;
  FListaObras : TListaObras;
  FListaObrasAux : TListaObras;
begin
  I := 0;
  AResourceIndex := -1;

  FListaObras := TListaObras.Create(tsoAbiertas);
  try
    with cbObra.Properties.Items do
    begin
      BeginUpdate;
      Clear;
      Add('');  // Un item vacio.
      for I := 0 to FListaObras.Count - 1 do
      begin
        Add(FListaObras.Obras[I].CodigoContrato + ' ' + FListaObras.Obras[I].Nombre);
        if VarToStr(Event.GetCustomFieldValueByName('CODIGOCONTRATO')) = FListaObras.Obras[I].CodigoContrato then
          AResourceIndex := I+1;
      end;

      //En el caso de ser pertenecer a una obra cerrada la cita que se presenta en modal
      if AResourceIndex = -1 then
      begin
        FListaObrasAux := TListaObras.Create(tsoCerradas);
        for J := 0 to FListaObrasAux.Count - 1 do
        begin
          Add(FListaObrasAux.Obras[J].CodigoContrato + ' ' + FListaObrasAux.Obras[J].Nombre);
          if VarToStr(Event.GetCustomFieldValueByName('CODIGOCONTRATO')) = FListaObrasAux.Obras[J].CodigoContrato then
            AResourceIndex := J+I+1;
        end;
        FreeAndNil(FListaObrasAux);
      end;
      cbObra.ItemIndex := AResourceIndex;
      EndUpdate;
    end;
  finally
    cbObra.Properties.Items.EndUpdate;
    FreeAndNil(FListaObras);
  end;
end;

procedure TfrEditorEventos.CargarTipos;
var
  I : Integer;
  Inicio, Final : Integer;
begin
  I := 0;

  if (FTipoEvento in [tcRemate, tcEntrega, tcMontaje]) then
  begin
    Inicio := Ord(tcEntrega);
    Final := Ord(tcRemate);
  end
  else begin
    if FTipoEvento in [tcPedido, tcConfirmacion, tcRecepcion, tcAviso, tcEntregaCliente] then
    begin
      Inicio := Ord(tcPedido);
      Final := Ord(tcEntregaCliente);
    end;
  end;

  with cbTipo.Properties do
  begin
    BeginUpdate;
    Items.Clear;

    for i := Inicio to Final do
    begin
      Items.Add(LiteralTipoCita[i]);
      if VarToStr(Event.GetCustomFieldValueByName('TIPOTAREA')) = TextoTipoCita[i] then
      begin
        cbTipo.ItemIndex := I;
        cbTipo.EditValue := LiteralTipoCita[i];
      end;
    end;
    EndUpdate;
  end;
end;

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

constructor TfrEditorEventos.CreateEx(AEvent: TcxSchedulerControlEvent);
begin
  inherited;
end;

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

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

  inherited;

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

  if not (FTipoEvento in [tcPersonal, tcMedicion]) then
  begin
    lbPartida.Visible := True;
    pnlObra.Visible := True;
    CargarObras;
    CargarTipos;
    if FTipoEvento in [tcPedido, tcConfirmacion, tcRecepcion, tcAviso, tcEntregaCliente] then
    begin
      lbPartida.Visible := True;
      cbPartida.Visible := True;
      CargarPartidas;
      pnlCompletada.Visible := True;
    end
    else begin
      lbPartida.Visible := False;
      cbPartida.Visible := False;
    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 EsCadenaVacia(teSubject.Text) then
    begin
      VerMensaje('Indique el asunto');
      Result := False;
    end;}

    if (FTipoEvento in [tcEntrega, tcMontaje, tcRemate]) then
    begin
      if EsCadenaVacia(cbInstaladores.EditValue) then
      begin
        VerMensaje('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 not EsCadenaVacia(Event.GetCustomFieldValueByName('TIPOTAREA')) then
    cbTipo.EditValue := Event.GetCustomFieldValueByName('TIPOTAREA');}

  if not (FTipoEvento in [tcPersonal, tcMedicion]) then
  begin
    if not EsCadenaVacia(Event.GetCustomFieldValueByName('CODIGOCONTRATO')) then
      teCodigoContrato.Text := Event.GetCustomFieldValueByName('CODIGOCONTRATO');
    if not EsCadenaVacia(Event.GetCustomFieldValueByName('NOMBRECLIENTE')) then
      teNombreCliente.Text := Event.GetCustomFieldValueByName('NOMBRECLIENTE');

    if not VarIsNull(Event.GetCustomFieldValueByName('COMPLETADA')) then
    begin
      cbCompletada.Checked := True;
      deFechaCompletada.Date := DateOf(VarToDateTime(Event.GetCustomFieldValueByName('COMPLETADA')));
    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;
  if pnlResource.Visible then
    SaveResourceID;

  try
    // post the data from the custom editing controls

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

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

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

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

    if not VarIsNull(cbTipo.EditValue) then
    begin
      FTipoEvento := DarTipoCita(cbTipo.EditValue);
      Event.SetCustomFieldValueByName('TIPOTAREA', 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 not EsCadenaVacia(cbObra.EditValue) 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 := '';
    teNombreCliente.Text := '';
  end;
end;

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

procedure TfrEditorEventos.CargarPartidas;
var
  Inicio, Final : Integer;
  I : Integer;
begin
  Inicio := Ord(tcPedido);
  Final := Ord(tcEntregaCliente);

  with cbPartida.Properties.Items do
  begin
    BeginUpdate;
    Clear;
    for I := 0 to (Final - Inicio - 1) do
    begin
      Add(LiteralPartidasObra[i]);
      if VarToStr(Event.GetCustomFieldValueByName('PARTIDA')) = TextoPartidasObra[i] then
        cbPartida.ItemIndex := I;
    end;
    EndUpdate;
  end;
end;

procedure TfrEditorEventos.cbTerminadoPropertiesChange(Sender: TObject);
begin
  deFechaCompletada.Enabled := cbCompletada.Checked;
  if not cbCompletada.Checked then
    deFechaCompletada.Clear
  else
    if EsCadenaVacia(deFechaCompletada.EditValue) 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
  for I := 0 to cbResources.Properties.Items.Count - 1 do
    cbResources.States[I] := cbsUnchecked;
  cbResources.States[cbInstaladores.ItemIndex] := cbsChecked;
  inherited;
end;

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

end.
