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

#include <vcl.h>
#pragma hdrstop

#include "fDocEntryFind.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AdvDateTimePicker"
#pragma link "AdvEdit"
#pragma link "AdvGlowButton"
#pragma link "AdvGrid"
#pragma link "AdvPanel"
#pragma link "AdvSmoothPanel"
#pragma link "AdvToolBar"
#pragma link "BaseGrid"
#pragma link "DBAdvGrid"
#pragma link "AdvDBDateTimePicker"
#pragma link "DBAccess"
#pragma link "MemDS"
#pragma link "Uni"
#pragma link "AdvEdBtn"
#pragma link "PlannerDatePicker"
#pragma link "AdvPageControl"
#pragma link "AdvOfficeButtons"
#pragma link "AdvObj"
#pragma link "AdvUtil"
#pragma link "vcl.Wwdbcomb"
#pragma link "vcl.Wwdbedit"
#pragma link "vcl.Wwdotdot"
#pragma link "vcl.wwdbdatetimepicker"
#pragma resource "*.dfm"
TfrmDocEntryFind *frmDocEntryFind;
//---------------------------------------------------------------------------
__fastcall TfrmDocEntryFind::TfrmDocEntryFind(TComponent* Owner)
  : TForm(Owner)
{
  TDateTime dtDate = Date();
  unsigned short year, month, day;

  dtDate.DecodeDate(&year, &month, &day);

  for( int i = 0; i < 12; i++ )
    dbcbYear->Items->Add( year - i );
  dbcbYear->ItemIndex = 0;
}
//---------------------------------------------------------------------------
void TfrmDocEntryFind::FindDocuments()
{
  String whereStr;

  whereStr = "";
  if( !dtpDocDate->Text.IsEmpty() && !dtpDocDate2->Text.IsEmpty() )
    whereStr = "d.doc_date between '" + dtpDocDate->Text + "' and '" + dtpDocDate2->Text + "' and ";
  else if( !dtpDocDate->Text.IsEmpty() )
    whereStr = "d.doc_date = '" + dtpDocDate->Text + "' and ";
}
//---------------------------------------------------------------------------
void __fastcall TfrmDocEntryFind::dbcbMonthCloseUp(TwwDBComboBox *Sender, bool Select)
{
  SetDocumentDates();
}
//---------------------------------------------------------------------------
void __fastcall TfrmDocEntryFind::dbcbYearCloseUp(TwwDBComboBox *Sender, bool Select)
{
  SetDocumentDates();
}
//---------------------------------------------------------------------------
void TfrmDocEntryFind::SetDocumentDates()
{
  int year = 0;
  int month = 0;
  TDateTime startDate;
  TDateTime endDate;

  if( !dbcbYear->Value.IsEmpty() )
    year = StrToInt( dbcbYear->Value );

  if( !dbcbMonth->Value.IsEmpty() )
    month = StrToInt( dbcbMonth->Value );

  if( month > 0 && year > 0 ){
    startDate = StartOfAMonth( year, month );
    endDate = EndOfAMonth( year, month );
    dtpDocDate->Date = startDate;
    dtpDocDate2->Date = endDate;
  }
  else if (year > 0){
    startDate = StartOfAMonth( year, 1 );
    endDate = EndOfAMonth( year, 12 );
    dtpDocDate->Date = startDate;
    dtpDocDate2->Date = endDate;
  }
}
//---------------------------------------------------------------------------

