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

#include "FichaPersonas.h"
#include "splash.h"
#include "QR_dhs.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFichaPersonal *FichaPersonal;
//---------------------------------------------------------------------------
__fastcall TFichaPersonal::TFichaPersonal(TComponent* Owner)
        : TForm(Owner)
{
 desde->Date=Now();
 hasta->Date=Now();
 Tb_dhs->Active=true;
 setDHS_Filter(0);

 Tb_FPersonas->Active=true;
 PageControl1->ActivePage = TabSheet1;      // Solapa Ficha>>General
 PageControl2->ActivePage = Tab_FPersonas;  // Solapa Fichas
 
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::tipoDocumentoChange(TObject *Sender)
{
 if ( updatingDB ) return;

 if ( Tb_FPersonas->State!=dsEdit || Tb_FPersonas->State!=dsInsert )
   Tb_FPersonas->Edit();

 Tb_FPersonas->FieldByName("Dni_Nif_Pasaporte")->AsInteger = tipoDocumento->ItemIndex;
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::Tb_FPersonasNewRecord(TDataSet *DataSet)
{
  Tb_FPersonas->FieldByName("Dni_Nif_Pasaporte")->AsInteger = 0;
  Tb_FPersonas->FieldByName("FAlta")->AsDateTime = Date();
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::Tb_FPersonasBeforePost(TDataSet *DataSet)
{
  Tb_FPersonas->FieldByName("FModif")->AsDateTime = Date();
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::Ds_FPersonasStateChange(TObject *Sender)
{
 btPost->Visible = ( Tb_FPersonas->State==dsEdit ||Tb_FPersonas->State==dsInsert );
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::newPersonClick(TObject *Sender)
{
  if ( !Tb_FPersonas->Active )
        Tb_FPersonas->Active = true;

  PageControl2->ActivePage=Tab_FPersonas;
  PageControl1->ActivePage=TabSheet1;
  Tb_FPersonas->Insert();
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::btPostClick(TObject *Sender)
{
 if ( Tb_FPersonas->State == dsEdit || Tb_FPersonas->State == dsInsert )
         Tb_FPersonas->Post();
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::Ds_FPersonasDataChange(TObject *Sender,
      TField *Field)
{
 if ( !needReloadGroup && ! carpeta.IsEmpty() )
 {
    tipoDocumento->ItemIndex=Tb_FPersonas->FieldByName("Dni_Nif_Pasaporte")->AsInteger;

    if ( CorrectaCC( Tb_FPersonas->FieldByName("Banco_Entidad")->AsInteger,
                     Tb_FPersonas->FieldByName("Banco_Sucursal")->AsInteger,
                     Tb_FPersonas->FieldByName("Banco_DC")->AsInteger,
                     Tb_FPersonas->FieldByName("Banco_NumCuenta")->AsString )
       )
    {
        DBEdit24->Color = clWindow;
        DBEdit25->Color = clWindow;
        DBEdit26->Color = clWindow;
        DBEdit27->Color = clWindow;
    }else{
        DBEdit24->Color = clRed;
        DBEdit25->Color = clRed;
        DBEdit26->Color = clRed;
        DBEdit27->Color = clRed;
    }
 }
 // Sumamos el total de debe/haber/saldo...
 RefrescarTotales(0);
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::NextField_Tab1(TObject *Sender, char &Key)
{
  // Saltamos al siguiente campo de la lista...
  if ( Key == VK_RETURN )
  {
   TDBEdit *Siguiente[] = { DBEdit4, DBEdit1, DBEdit5, DBEdit2, DBEdit3,
                            DBEdit6, DBEdit7, DBEdit8, DBEdit9, DBEdit10,
                            DBEdit11, DBEdit12, DBEdit13, DBEdit14, DBEdit15,
                            DBEdit1, NULL };

   for ( int i=0; Siguiente[i]!=NULL; i++ )
   {
     if ( Sender == Siguiente[i] )
     {
/*
       if ( TbListado->State == dsEdit || TbListado->State == dsInsert )
         TbListado->Post();
*/
       Siguiente[i+1]->SetFocus();
       break;
     }
   }
  }
}
//---------------------------------------------------------------------------

#define Digito(Num, Dig) ( (Num-( (Num/(int)Power(Dig,10)) * (int)Power(Dig,10) )) / ( Dig==1 ? 1 : (int)Power(Dig-1,10)  ) )

    // Comprobamos que la CC sea correcta
bool __fastcall TFichaPersonal::CorrectaCC( int Banco, int Oficina, int DC, AnsiString NumCuenta )
    {
       int Suma1, Suma2, R1, R2;
       bool dev;
       char *NumCC;

       dev = false;
       if ( NumCuenta.Length() == 10 )
       {
         NumCC = NumCuenta.c_str();
         Suma1 = Digito(Oficina, 1)* 6 +
                 Digito(Oficina, 2)* 3 +
                 Digito(Oficina, 3)* 7 +
                 Digito(Oficina, 4)* 9 +
                 Digito(Banco,   1)*10 +
                 Digito(Banco,   2)* 5 +
                 Digito(Banco,   3)* 8 +
                 Digito(Banco,   4)* 4 ;
         Suma2 = (NumCC[9]-'0')*6  +
                 (NumCC[8]-'0')*3  +
                 (NumCC[7]-'0')*7  +
                 (NumCC[6]-'0')*9  +
                 (NumCC[5]-'0')*10 +
                 (NumCC[4]-'0')*5  +
                 (NumCC[3]-'0')*8  +
                 (NumCC[2]-'0')*4  +
                 (NumCC[1]-'0')*2  +
                 (NumCC[0]-'0')*1  ;

         R1 = 11-(Suma1%11); if ( R1==11 ) R1 = 0; if ( R1==10 ) R1 = 1;
         R2 = 11-(Suma2%11); if ( R2==11 ) R2 = 0; if ( R2==10 ) R2 = 1;

          if ( Digito(DC,2)== R1 && Digito(DC,1)== R2 )
          dev = true;
       }
     return dev;
    };
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::NextField_Tab2(TObject *Sender, char &Key)
{
  // Saltamos al siguiente campo de la lista...
  if ( Key == VK_RETURN )
  {
   TDBEdit *Siguiente[] = { DBEdit21, DBEdit22,
                            DBEdit24, DBEdit26, DBEdit25, DBEdit27,
                            DBEdit23, DBEdit17, DBEdit18, DBEdit19, DBEdit20,
                            DBEdit21, NULL };

   for ( int i=0; Siguiente[i]!=NULL; i++ )
   {
     if ( Sender == Siguiente[i] )
     {
/*
       if ( TbListado->State == dsEdit || TbListado->State == dsInsert )
         TbListado->Post();
*/
       Siguiente[i+1]->SetFocus();
       break;
     }
   }
  }
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::SpeedButton2Click(TObject *Sender)
{
  PageControl2->ActivePage=Tab_FPersonas;
  Tb_FPersonas->First();
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::SpeedButton6Click(TObject *Sender)
{
  PageControl2->ActivePage=Tab_FPersonas;
  Tb_FPersonas->Last();
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::SpeedButton3Click(TObject *Sender)
{
  PageControl2->ActivePage=Tab_FPersonas;
  Tb_FPersonas->Prior();
}
//---------------------------------------------------------------------------
void __fastcall TFichaPersonal::SpeedButton7Click(TObject *Sender)
{
  PageControl2->ActivePage=Tab_FPersonas;
  Tb_FPersonas->Next();
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::FormClose(TObject *Sender,
      TCloseAction &Action)
{
  splash->Close();        
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::SpeedButton5Click(TObject *Sender)
{
  Tb_FPersonas->Locate("ID", Edit1->Text, TLocateOptions() );
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::Edit1KeyPress(TObject *Sender, char &Key)
{
  if ( Key == VK_RETURN )SpeedButton5Click(0);
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::Tb_dhsCalcFields(TDataSet *DataSet)
{
  Tb_dhs->FieldByName("saldo")->AsCurrency = Tb_dhs->FieldByName("haber")->AsCurrency -
                                                Tb_dhs->FieldByName("debe")->AsCurrency;        
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::setDHS_Filter(TObject *Sender)
{
  CheckBox1->Checked=true;
  Tb_dhs->Filter = "[Fecha]>='"+desde->Date.DateString()+"' AND [Fecha]<='"+hasta->Date.DateString()+"'";
  Tb_dhs->Filtered=true;
  RefrescarTotales(0);
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::Tb_dhsNewRecord(TDataSet *DataSet)
{
 Tb_dhs->FieldByName("Fecha")->AsDateTime = Now();        
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::CheckBox1Click(TObject *Sender)
{
 Tb_dhs->Filtered=CheckBox1->Checked;        
}
//---------------------------------------------------------------------------

void __fastcall TFichaPersonal::RefrescarTotales(TObject *Sender)
{
 Currency debe, haber;

 debe=0.0;
 haber=0.0;

 Tb_dhs->DisableControls();
 Tb_dhs->First();
 while(!Tb_dhs->Eof)
 {
  debe+=Tb_dhs->FieldByName("Debe")->AsCurrency;
  haber+=Tb_dhs->FieldByName("Haber")->AsCurrency;
  Tb_dhs->Next();
 }
 Tb_dhs->EnableControls();

 StatusBar1->Panels->Items[0]->Text = "Debe: "+FormatCurr("###,###,###.#0",debe)+
                                      "  Haber: "+FormatCurr("###,###,###.#0",haber)+
                                      "  Saldo: "+FormatCurr("###,###,###.#0",haber-debe);


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

void __fastcall TFichaPersonal::SpeedButton4Click(TObject *Sender)
{
  TQRdhs *R;
  R=new TQRdhs(this);
  R->QuickRep1->DataSet = Tb_dhs;
  R->QRPeriodo->Caption= "Periodo del " + desde->Date.DateString() + " al " + hasta->Date.DateString();
  R->QRCon->Caption= "ID: " + Tb_FPersonas->FieldByName("Id")->AsString +
                     "  Contacto: " + Tb_FPersonas->FieldByName("Nombre")->AsString + " " + Tb_FPersonas->FieldByName("Apellidos")->AsString;
  R->QREmp->Caption= "Empresa: " + Tb_FPersonas->FieldByName("Empresa")->AsString;
  R->QuickRep1->Preview();
  delete R;
}
//---------------------------------------------------------------------------

