//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FichasClientes.h"
#include "CtrlPPal.h"
#include "ComprarBono.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CoolForm"
#pragma link "DigitNum"
#pragma link "FormularioREUSABLE"
#pragma resource "*.dfm"
TFClientes *FClientes;
//---------------------------------------------------------------------------
__fastcall TFClientes::TFClientes(TComponent* Owner)
        : TFormREUSABLE(Owner)
{
 SpeedButton1Click(0);
 SelIndex->ItemIndex = 0;
 TbBonos->Active = true;
}
//---------------------------------------------------------------------------

void __fastcall TFClientes::Button1Click(TObject *Sender)
{
        ModalResult = mrCancel;
}
//---------------------------------------------------------------------------
void __fastcall TFClientes::DBGrid1DblClick(TObject *Sender)
{
  Label1->Visible    = false;
  SelIndex->Visible  = false;
  Edit1->Visible     = false;
  DBGrid1->Visible   = false;

  Label2->Visible    = true;
  Label3->Visible    = true;
  Label4->Visible    = true;
  Label5->Visible    = true;
  Label6->Visible    = true;
  DBText1->Visible   = true;
  DBEdit1->Visible   = true;
  DBEdit2->Visible   = true;
  DBEdit3->Visible   = true;
  DBEdit4->Visible   = true;
  Button2->Visible   = true;
  Button3->Visible   = true;
  Dig1->Visible      = true;
  Dig2->Visible      = true;
  Shape1->Visible    = true;
  Shape2->Visible    = true;}
//---------------------------------------------------------------------------

void __fastcall TFClientes::SpeedButton1Click(TObject *Sender)
{
  Label2->Visible    = false;
  Label3->Visible    = false;
  Label4->Visible    = false;
  Label5->Visible    = false;
  Label6->Visible    = false;
  DBText1->Visible   = false;
  DBEdit1->Visible   = false;
  DBEdit2->Visible   = false;
  DBEdit3->Visible   = false;
  DBEdit4->Visible   = false;
  Button2->Visible   = false;
  Button3->Visible   = false;
  Dig1->Visible      = false;
  Dig2->Visible      = false;
  Shape1->Visible    = false;
  Shape2->Visible    = false;

  Label1->Visible    = true;
  SelIndex->Visible  = true;
  Edit1->Visible     = true;
  DBGrid1->Visible   = true;
}
//---------------------------------------------------------------------------
void __fastcall TFClientes::SpeedButton2Click(TObject *Sender)
{
  DBGrid1DblClick(0);
  CtrlPPal->TbPersonas->Insert();
}
//---------------------------------------------------------------------------


void __fastcall TFClientes::SelIndexChange(TObject *Sender)
{
   CtrlPPal->TbPersonas->IndexFieldNames = SelIndex->Items->Strings[SelIndex->ItemIndex];
}
//---------------------------------------------------------------------------

void __fastcall TFClientes::Edit1KeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
 if ( !Edit1->Text.IsEmpty() )
  CtrlPPal->TbPersonas->Locate( SelIndex->Items->Strings[SelIndex->ItemIndex], Edit1->Text, TLocateOptions() << loCaseInsensitive << loPartialKey );
}
//---------------------------------------------------------------------------

void __fastcall TFClientes::Button3Click(TObject *Sender)
{
  ModalResult = mrOk;
}
//---------------------------------------------------------------------------

void __fastcall TFClientes::DsPersonasDataChange(TObject *Sender,
      TField *Field)
{
     Dig1->Value = CtrlPPal->TbPersonas->FieldByName("TiempoH")->AsInteger;
     Dig2->Value = CtrlPPal->TbPersonas->FieldByName("TiempoM")->AsInteger;

}
//---------------------------------------------------------------------------

void __fastcall TFClientes::Button2Click(TObject *Sender)
{
 // Aun no desarrollado...
 TCBono *CBono;
 CBono = new TCBono(this);
 if ( CBono->ShowModal() == mrOk )
 {
   int H, M;
   H = CBono->Horas->Text.ToInt();
   M = CBono->Min->Text.ToInt();
   CtrlPPal->TbPersonas->Edit();
   CtrlPPal->TbPersonas->FieldByName("TiempoH")->AsInteger = CtrlPPal->TbPersonas->FieldByName("TiempoH")->AsInteger + H + ( CtrlPPal->TbPersonas->FieldByName("TiempoM")->AsInteger + M)/60;
   CtrlPPal->TbPersonas->FieldByName("TiempoM")->AsInteger = ( CtrlPPal->TbPersonas->FieldByName("TiempoM")->AsInteger + M)%60;
   CtrlPPal->TbPersonas->Post();
   TbBonos->InsertRecord(ARRAYOFCONST((CtrlPPal->TbPersonas->FieldByName("CodCliente1")->AsInteger,(Variant)TDateTime::CurrentDateTime(),(Variant)TDateTime::CurrentDate(), H, M, Currency(CBono->CosteBono->Text) )));
 }
}
//---------------------------------------------------------------------------

void __fastcall TFClientes::TbBonosBeforeOpen(TDataSet *DataSet)
{
 if ( !FileExists( TbBonos->TableName ) )
 {
   TbBonos -> TableType = ttParadox;

   TbBonos -> FieldDefs -> Clear();

      /********************\
     |*    Datos Básicos   *|
      \********************/
  	 TbBonos -> FieldDefs -> Add("IDcliente",       ftInteger,  0, false );
         TbBonos -> FieldDefs -> Add("FechaCompra",     ftDateTime, 0, false );
  	 TbBonos -> FieldDefs -> Add("fecha",           ftDate, 0, false );
  	 TbBonos -> FieldDefs -> Add("Bhoras",          ftInteger,  0, false );
  	 TbBonos -> FieldDefs -> Add("Bmin",            ftInteger,  0, false );
  	 TbBonos -> FieldDefs -> Add("coste",           ftCurrency,  0, false );
     TbBonos -> IndexDefs-> Clear();

	 TbBonos->IndexDefs->Add("Primary", "IDcliente;FechaCompra", TIndexOptions() << ixPrimary );

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

void __fastcall TFClientes::DBEdit1KeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  if ( Key == VK_RETURN )
  {
   CtrlPPal->TbPersonas->Post();
   DBEdit2->SetFocus();
  }
}
//---------------------------------------------------------------------------

void __fastcall TFClientes::DBEdit2KeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  if ( Key == VK_RETURN )
  {
   CtrlPPal->TbPersonas->Post();
   DBEdit3->SetFocus();
  }

}
//---------------------------------------------------------------------------

void __fastcall TFClientes::DBEdit3KeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  if ( Key == VK_RETURN )
  {
   CtrlPPal->TbPersonas->Post();
   DBEdit4->SetFocus();
  }

}
//---------------------------------------------------------------------------


void __fastcall TFClientes::DBEdit4KeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
  if ( Key == VK_RETURN )
  {
   CtrlPPal->TbPersonas->Post();
   DBEdit1->SetFocus();
  }
}
//---------------------------------------------------------------------------



