//---------------------------------------------------------------------------
#include <vcl.h>
#include <io.h>
#pragma hdrstop

#include "Herm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TGestionHnos *GestionHnos;
//---------------------------------------------------------------------------
__fastcall TGestionHnos::TGestionHnos(TComponent* Owner)
    : TForm(Owner)
{
 Width = 640; Height = 480;
// SelIndex->ItemIndex = 0;

 TbInsignias->Active = true;
 TbInsignias->Filter = "CtoVirgen = False";
 TbInsignias->Filtered = true;

 TbBusqHnos->ReadOnly = true;
 TbBusqHnos->Active = true;

 TbCuotaFam->Active = true;

 TbHermUPDATING = false;
 TbHerm->Active = true;
 DsHerm->AutoEdit = false;

 PageControl1->ActivePage = TabSheet1;
}
//---------------------------------------------------------------------------
void __fastcall TGestionHnos::TbHermBeforeOpen(TDataSet *DataSet)
{
 if ( access( (TbHerm -> TableName).c_str(), 0 ) != 0 )
 {
	 // Usamos la propiedad FielDefs para definir
	 // las columnas que contendrá la tabla
     TbHerm -> FieldDefs -> Clear();

      /********************\
     |*    Datos Básicos   *|
      \********************/
  	 TbHerm -> FieldDefs -> Add("idHerm",               ftAutoInc,  0, false );


//-------
  	 TbHerm -> FieldDefs -> Add("Borrado",              ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("FechaBorrado",         ftDate,  0, false );
//-------

  	 TbHerm -> FieldDefs -> Add("HnoHna",               ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("NumHno",               ftInteger,  0, false );

  	 TbHerm -> FieldDefs -> Add("Nombre",               ftString,  15, false );
  	 TbHerm -> FieldDefs -> Add("Apellido1",            ftString,  15, false );
  	 TbHerm -> FieldDefs -> Add("Apellido2",            ftString,  15, false );
  	 TbHerm -> FieldDefs -> Add("DNI",                  ftString,  15, false );

  	 TbHerm -> FieldDefs -> Add("Direccion",            ftString,  40, false );
  	 TbHerm -> FieldDefs -> Add("Num",                  ftString,  3, false );
  	 TbHerm -> FieldDefs -> Add("Piso",                 ftString,  3, false );
  	 TbHerm -> FieldDefs -> Add("CodPostal",            ftInteger,  0, false );
  	 TbHerm -> FieldDefs -> Add("Poblacion",            ftString,  15, false );
  	 TbHerm -> FieldDefs -> Add("Provincia",            ftString,  15, false );
  	 TbHerm -> FieldDefs -> Add("Telefono1",            ftString,  15, false );
  	 TbHerm -> FieldDefs -> Add("Telefono2",            ftString,  15, false );
  	 TbHerm -> FieldDefs -> Add("e-mail",               ftString,  30, false );
  	 TbHerm -> FieldDefs -> Add("FechaNacimiento",      ftDate,  0, false );
  	 TbHerm -> FieldDefs -> Add("Antiguedad",           ftDate,  0, false );

  	 TbHerm -> FieldDefs -> Add("idInsignia",           ftInteger,  0, false );
  	 TbHerm -> FieldDefs -> Add("NumTunica",            ftInteger,  0, false );

  	 TbHerm -> FieldDefs -> Add("Comentario",           ftMemo,  256, false );

  	 TbHerm -> FieldDefs -> Add("idHermCF",             ftInteger,  0, false );
  	 TbHerm -> FieldDefs -> Add("CuotaFamiliar",        ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("RecibeNotificaciones", ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("PuedeVotar",           ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("TunicaHermandad",      ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("RealizoPenitencia",    ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("CristoVirgen",         ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("Reserva",              ftBoolean,  0, false );
  	 TbHerm -> FieldDefs -> Add("FA2",                  ftBoolean,  0, false );

     TbHerm -> IndexDefs-> Clear();

     TIndexOptions MyIndexOptions;
     MyIndexOptions << ixPrimary << ixUnique;
	 TbHerm->IndexDefs->Add("Primary", "idHerm", MyIndexOptions);
	 TbHerm->IndexDefs->Add("NumHno", "NumHno", TIndexOptions() << ixCaseInsensitive << ixUnique );
	 TbHerm->IndexDefs->Add("DNI",  "DNI", TIndexOptions() << ixCaseInsensitive);
	 TbHerm->IndexDefs->Add("Nombre",  "Nombre;Apellido1;Apellido2", TIndexOptions() << ixCaseInsensitive);
	 TbHerm->IndexDefs->Add("IdxDireccion", "Direccion;Num;Piso", TIndexOptions() << ixCaseInsensitive  );
	 TbHerm->IndexDefs->Add("Apellido1",  "Apellido1;Apellido2;Nombre", TIndexOptions() << ixCaseInsensitive);
	 TbHerm->IndexDefs->Add("Apellido2",  "Apellido2;Apellido1;Nombre", TIndexOptions() << ixCaseInsensitive);
	 TbHerm->IndexDefs->Add("Telefono1",  "Telefono1", TIndexOptions() << ixCaseInsensitive);
	 TbHerm->IndexDefs->Add("Telefono2",  "Telefono2", TIndexOptions() << ixCaseInsensitive);

	 // Creamos la base...
	 TbHerm -> CreateTable();
 }

}
//---------------------------------------------------------------------------
void __fastcall TGestionHnos::TbInsigniasCtoBeforeOpen(TDataSet *DataSet)
{
 if ( access( (TbInsignias -> TableName).c_str(), 0 ) != 0 )
 {
	 // Usamos la propiedad FielDefs para definir
	 // las columnas que contendrá la tabla
     TbInsignias -> FieldDefs -> Clear();

      /********************\
     |*    Datos Básicos   *|
      \********************/
  	 TbInsignias -> FieldDefs -> Add("idInsignia",      ftAutoInc,  0, false );
  	 TbInsignias -> FieldDefs -> Add("CtoVirgen",       ftBoolean,  0, false );
  	 TbInsignias -> FieldDefs -> Add("Insignia",        ftString,  30, false );
  	 TbInsignias -> FieldDefs -> Add("Suplemento",      ftCurrency, 0, false );

     TbInsignias -> IndexDefs-> Clear();


	 // Creamos la base...
	 TbInsignias -> CreateTable();
 }
}
//---------------------------------------------------------------------------
void __fastcall TGestionHnos::SpeedButton1Click(TObject *Sender)
{
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TGestionHnos::TbHermCalcFields(TDataSet *DataSet)
{
  TbHerm->FieldByName("Apellidos")->AsString = TbHerm->FieldByName("Apellido1")->AsString + " " + TbHerm->FieldByName("Apellido2")->AsString;
  TbHerm->FieldByName("Direccion_Compl")->AsString = TbHerm->FieldByName("Direccion")->AsString +
               ((TbHerm->FieldByName("Num")->AsString).IsEmpty() ? AnsiString(", ") : AnsiString(", nº"+TbHerm->FieldByName("Num")->AsString)) +
               ((TbHerm->FieldByName("Num")->AsString).IsEmpty() ? AnsiString(".") : AnsiString(", Piso "+TbHerm->FieldByName("Piso")->AsString)) ;
  if ( TbHerm->FieldByName("CuotaFamiliar")->AsBoolean &&
       TbBusqHnos->Locate( "idHerm",
                           TbHerm->FieldByName("idHermCF")->AsInteger,
                           TLocateOptions() << loCaseInsensitive << loPartialKey ) )
     TbHerm->FieldByName("DatosTitularCuotaFam")->AsString =   TbBusqHnos->FieldByName("NumHno")->AsString + ", " +
                                                               TbBusqHnos->FieldByName("Nombre")->AsString + " " +
                                                               TbBusqHnos->FieldByName("Apellido1")->AsString + " " +
                                                               TbBusqHnos->FieldByName("Apellido2")->AsString;
}
//---------------------------------------------------------------------------
void __fastcall TGestionHnos::SelIndexChange(TObject *Sender)
{
  TbBusqHnos->IndexFieldNames = SelIndex->Items->Strings[SelIndex->ItemIndex];
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::BuscadorKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
 if ( SelIndex->ItemIndex != -1 )
  TbBusqHnos->Locate( SelIndex->Items->Strings[SelIndex->ItemIndex], Buscador->Text, TLocateOptions() << loCaseInsensitive << loPartialKey );
 else
  ShowMessage( "Seleccione un CAMPO, por el que BUSCAR" );
}
//---------------------------------------------------------------------------
void __fastcall TGestionHnos::DsHermDataChange(TObject *Sender, TField *Field)
{
 if ( TbHermUPDATING ) return;


 if ( TbHermBorrado->AsBoolean )
 {
   TabSheet2->Font->Style = TFontStyles()<< fsStrikeOut;
   UnLockClick(0);
 } else
   TabSheet2->Font->Style = TFontStyles();

 TbHermUPDATING = true;
 CtoVirgen->ItemIndex = TbHerm->FieldByName("CristoVirgen")->AsBoolean;

 TbInsignias->Filter = "CtoVirgen = " + (CtoVirgen->ItemIndex ? AnsiString("True") : AnsiString("False"));
 TbInsignias->Filtered = true;

 NTunica->Visible = TbHerm->FieldByName("TunicaHermandad")->AsBoolean;

 if ( TbHerm->FieldByName("CuotaFamiliar")->AsBoolean )
 {
   DatosTitularCF->Visible = true;
   if ( ! TbHerm->FieldByName("idHermCF")->AsString.IsEmpty() )
   {
    BeneficiariosCF->Enabled = true;
    BeneficiariosCF->Visible = true;
    TbCuotaFam->Filter = "idHermCF = " + TbHerm->FieldByName("idHermCF")->AsString;
    TbCuotaFam->Filtered = true;
   }  else {
    BeneficiariosCF->Enabled = false;
    BeneficiariosCF->Visible = false;
   }
 } else {
   BeneficiariosCF->Enabled = false;
   BeneficiariosCF->Visible = false;
   DatosTitularCF->Visible = false;
 }
 TbHermUPDATING = false;
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::CtoVirgenClick(TObject *Sender)
{
  if ( DsHerm->AutoEdit && !TbHermUPDATING)
  {
   TbHermUPDATING = true;

   TbHerm->Edit();
   TbHerm->FieldByName("CristoVirgen")->AsBoolean = CtoVirgen->ItemIndex;
   TbHerm->Post();

   TbInsignias->Filter = "CtoVirgen = " + (CtoVirgen->ItemIndex ? AnsiString("True") : AnsiString("False"));
   TbInsignias->Filtered = true;

   TbHermUPDATING = false;
  }
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::LockClick(TObject *Sender)
{
 if ( TbHermBorrado->AsBoolean ) return;

  DsHerm->AutoEdit = true;
  Unlock->Visible = true;
  CtoVirgen->Enabled = true;
  DBLookupComboBox1->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::UnLockClick(TObject *Sender)
{
 if ( TbHerm->State == dsEdit || TbHerm->State == dsInsert )
     TbHerm->Post();
  DsHerm->AutoEdit = false;
  Unlock->Visible = false;
  CtoVirgen->Enabled = false;
  DBLookupComboBox1->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::DBGrid1DblClick(TObject *Sender)
{
  if ( AsignandoTitular->Visible )
   AsignandoTitularClick( 0 );
  else
   TbHerm->GotoCurrent( TbBusqHnos );

  PageControl1->ActivePage = TabSheet2;
}
//---------------------------------------------------------------------------
void __fastcall TGestionHnos::SpeedButton3Click(TObject *Sender)
{
 AsignandoTitular->Visible = true;
 PageControl1->ActivePage = TabSheet1;
}
//---------------------------------------------------------------------------
void __fastcall TGestionHnos::AsignandoTitularClick(TObject *Sender)
{
 AsignandoTitular->Visible = false;

  if ( DsHerm->AutoEdit && !TbHermUPDATING)
  {
    int idHerm = TbBusqHnos->FieldByName("idHerm")->AsInteger;
    TbHerm->Edit();
    TbHerm->FieldByName("idHermCF")->AsInteger = idHerm;
    TbHerm->Post();
  }
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::TbHermNewRecord(TDataSet *DataSet)
{
 TbHerm->FieldByName("Antiguedad")->AsDateTime = TDateTime::CurrentDate();
 TbHerm->FieldByName("NumHno")->AsInteger = (UltimoHno + 1);
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::PageControl1Change(TObject *Sender)
{
 AsignandoTitular->Visible = false;
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::TbBusqHnosCalcFields(TDataSet *DataSet)
{
  TbBusqHnos->FieldByName("Apellidos")->AsString = TbBusqHnos->FieldByName("Apellido1")->AsString + " " + TbBusqHnos->FieldByName("Apellido2")->AsString;
  TbBusqHnos->FieldByName("Direccion_Compl")->AsString = TbBusqHnos->FieldByName("Direccion")->AsString +
               ((TbBusqHnos->FieldByName("Num")->AsString).IsEmpty() ? AnsiString(", ") : AnsiString(", nº"+TbBusqHnos->FieldByName("Num")->AsString)) +
               ((TbBusqHnos->FieldByName("Num")->AsString).IsEmpty() ? AnsiString(".") : AnsiString(", Piso "+TbBusqHnos->FieldByName("Piso")->AsString)) ;
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::TbHermBeforeDelete(TDataSet *DataSet)
{
if (MessageDlg("¿ Borrar al Hermano: " + TbHermNumHno->AsString +" ?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)
{
  TbHerm->Edit();
  TbHerm->FieldByName("Borrado")->AsBoolean = true;
  TbHerm->Post();

  UnLockClick(0);
}
  Abort();
}
//---------------------------------------------------------------------------


void __fastcall TGestionHnos::TbHermBeforeInsert(TDataSet *DataSet)
{
 TbHerm->Last();
 UltimoHno = TbHerm->FieldByName("NumHno")->AsInteger;
}
//---------------------------------------------------------------------------

void __fastcall TGestionHnos::DBNavigator1Click(TObject *Sender,
      TNavigateBtn Button)
{
  PageControl1->ActivePage = TabSheet2;
}
//---------------------------------------------------------------------------

