unit uEditorElegirPersonaContactoPedido;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  uEditorBasico, ActnList, StdCtrls, ExtCtrls, cxGraphics, cxControls,
  cxContainer, cxEdit, cxTextEdit, cxMaskEdit, cxDropDownEdit;

type
  TfEditorElegirPersonaContactoPedido = class(TfEditorBasico)
    Panel1: TPanel;
    bAceptar: TButton;
    bCancelar: TButton;
    Label1: TLabel;
    rdxDestino1: TRadioButton;
    cbListaEmpleados: TcxComboBox;
    cbListaPersonalSubcontrata: TcxComboBox;
    RadioButton1: TRadioButton;
    rdxDestino2: TRadioButton;
    ActionList1: TActionList;
    actEmpleadoEmpresa: TAction;
    actSubcontrata: TAction;
    edtTextoPersona: TcxTextEdit;
    actOtro: TAction;
    procedure CustomEditorCreate(Sender: TObject);
    procedure CustomEditorShow(Sender: TObject);
    procedure actSubcontrataExecute(Sender: TObject);
    procedure actOtroExecute(Sender: TObject);
    procedure actEmpleadoEmpresaExecute(Sender: TObject);
  private
    FIDSubcontrata: Integer;
    FPersonaContacto : String;
    procedure RefrescarEstado;
    procedure RellenarEmpleados;
    procedure RellenarPersonalSubcontrata;
    procedure BuscarPersonaContacto;
    function GetPersonaContacto: String;
    procedure SetPersonaContacto(const Value: String);
  public
    property ID_Subcontrata : Integer read FIDSubcontrata write FIDSubcontrata;
    property PersonaContacto : String read GetPersonaContacto write SetPersonaContacto;
  end;

function ElegirPersonaContactoPedido(const AID_Subcontrata: Integer; var ANombrePersona: String): Boolean;

implementation

{$R *.dfm}

uses
  uFactuGES_App, uEmpleadosController, uBizContactos, uProveedoresController,
  uStringsUtils;


function ElegirPersonaContactoPedido(const AID_Subcontrata: Integer; var ANombrePersona: String): Boolean;
begin
  with TfEditorElegirPersonaContactoPedido.Create(NIL) do
    try
      ID_Subcontrata := AID_Subcontrata;
      PersonaContacto := ANombrePersona;
      if IsPositiveResult(ShowModal) then
      begin
        ANombrePersona := PersonaContacto;
        Result := True;
      end
      else
        Result := False;
    finally
      Free;
    end;
end;


procedure TfEditorElegirPersonaContactoPedido.actEmpleadoEmpresaExecute(
  Sender: TObject);
begin
  actEmpleadoEmpresa.Checked := True;
  RefrescarEstado;
end;

procedure TfEditorElegirPersonaContactoPedido.actOtroExecute(Sender: TObject);
begin
  actOtro.Checked := True;
  RefrescarEstado;
end;

procedure TfEditorElegirPersonaContactoPedido.actSubcontrataExecute(
  Sender: TObject);
begin
  inherited;
  actSubcontrata.Checked := True;
  RefrescarEstado;
end;

procedure TfEditorElegirPersonaContactoPedido.BuscarPersonaContacto;
var
  i : integer;
begin
  // żEs un empleado?
  for i := 0 to (cbListaEmpleados.Properties.Items.Count - 1) do
  begin
    if (Pos(FPersonaContacto, cbListaEmpleados.Properties.Items[i]) > 0) then
    begin
      cbListaEmpleados.Text := FPersonaContacto;
      actEmpleadoEmpresa.Checked := True;
      RefrescarEstado;
      Exit;
    end;
  end;

  // żEs personal de una subcontrata?
  if (ID_Subcontrata > 0) then
    for i := 0 to (cbListaPersonalSubcontrata.Properties.Items.Count - 1) do
    begin
      if (Pos(FPersonaContacto, cbListaPersonalSubcontrata.Properties.Items[i]) > 0) then
      begin
        cbListaPersonalSubcontrata.Text := FPersonaContacto;
        actSubcontrata.Checked := True;
        RefrescarEstado;
        Exit;
      end;
    end;

  // Es otra persona
  edtTextoPersona.Text := FPersonaContacto;
  actOtro.Checked := True;
  RefrescarEstado;
end;

procedure TfEditorElegirPersonaContactoPedido.CustomEditorCreate(
  Sender: TObject);
begin
  inherited;
  FPersonaContacto := '';
  FIDSubcontrata := -1;
  actEmpleadoEmpresa.Caption := actEmpleadoEmpresa.Caption + AppFactuGES.EmpresaActiva.NOMBRE + ':'; 
end;

procedure TfEditorElegirPersonaContactoPedido.CustomEditorShow(Sender: TObject);
begin
  inherited;

  RellenarEmpleados;

  if (ID_Subcontrata > 0) then
    RellenarPersonalSubcontrata
  else
    actSubcontrata.Enabled := False;

  if not EsCadenaVacia(FPersonaContacto) then
    BuscarPersonaContacto;
end;

function TfEditorElegirPersonaContactoPedido.GetPersonaContacto: String;
begin
  if (actOtro.Checked) then
    FPersonaContacto := edtTextoPersona.Text;

  if (actEmpleadoEmpresa.Checked) then
    FPersonaContacto := cbListaEmpleados.Text;

  if (actSubcontrata.Checked) then
    FPersonaContacto := cbListaPersonalSubcontrata.Text;

  Result := FPersonaContacto;
end;

procedure TfEditorElegirPersonaContactoPedido.RefrescarEstado;
begin
  if (actOtro.Checked) then
  begin
    edtTextoPersona.Enabled := True;
    cbListaEmpleados.Enabled := False;
    cbListaPersonalSubcontrata.Enabled := False;
  end;

  if (actEmpleadoEmpresa.Checked) then
  begin
    edtTextoPersona.Enabled := False;
    cbListaEmpleados.Enabled := True;
    cbListaPersonalSubcontrata.Enabled := False;
  end;

  if (actSubcontrata.Checked) then
  begin
    edtTextoPersona.Enabled := False;
    cbListaEmpleados.Enabled := False;
    cbListaPersonalSubcontrata.Enabled := True;
  end;
end;

procedure TfEditorElegirPersonaContactoPedido.RellenarEmpleados;
var
  FEmpleadosController : IEmpleadosController;
  FEmpleados : TStringList;
  i : integer;
begin
  FEmpleadosController := TEmpleadosController.Create;
  FEmpleados := FEmpleadosController.DarListaEmpleadosConMovil;

  ShowHourglassCursor;
  try
    with cbListaEmpleados.Properties.Items do
    begin
      BeginUpdate;
      try
        Clear;
        for i := 0 to FEmpleados.Count - 1 do
        begin
          Add(FEmpleados[i]);
        end;
      finally
        EndUpdate;
      end;
    end;
  finally
    FreeAndNIL(FEmpleados);
    FEmpleadosController := NIL;
    HideHourglassCursor;
  end;
end;

procedure TfEditorElegirPersonaContactoPedido.RellenarPersonalSubcontrata;
var
  FProveedoresController : IProveedoresController;
  FPersonal : TStringList;
  i : integer;
begin
  FProveedoresController := TProveedoresController.Create;

  ShowHourglassCursor;
  try
    FPersonal := FProveedoresController.DarListaPersonalConMovil(ID_Subcontrata);
    with cbListaPersonalSubcontrata.Properties.Items do
    begin
      BeginUpdate;
      try
        Clear;
        for i := 0 to FPersonal.Count - 1 do
          Add(FPersonal[i]);
      finally
        EndUpdate;
      end;
    end;
  finally
    FreeAndNIL(FPersonal);
    FProveedoresController := NIL;
    HideHourglassCursor;
  end;
end;

procedure TfEditorElegirPersonaContactoPedido.SetPersonaContacto(
  const Value: String);
begin
  FPersonaContacto := Value;
end;

end.
