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

#include <vcl.h>
#pragma hdrstop

#include "Stat.h"
#include "JungSan.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TStatForm *StatForm;

TDateTime  DtVal;
String ym1,ym2;
int da=0;
//---------------------------------------------------------------------------
__fastcall TStatForm::TStatForm(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TStatForm::yBtClick(TObject *Sender)
{
        Chart1->Series[0]->Clear();
        Chart1->Series[1]->Clear();
        Chart1->Series[2]->Clear();
        Chart1->Series[3]->Clear();
        Timer1->Enabled = true;        
}
//---------------------------------------------------------------------------
void __fastcall TStatForm::DTPickChange(TObject *Sender)
{
        DTPick2->MinDate = DTPick->Date;        
}
//---------------------------------------------------------------------------
void __fastcall TStatForm::Timer1Timer(TObject *Sender)
{
        ChartDraw(DTPick->Date,DTPick2->Date);
}
//---------------------------------------------------------------------------

void TStatForm::ChartDraw(String y, String y2)
{
        //TODO: Add your source code here

        Query1->Close();



                ym1=IncMonth(y,da);

             /*
                if(da<10)
                {
                       ym1=y.SubString(0,4)+"-0"+da;

                }
                else
                {
                        ym1=y.SubString(0,4)+"-"+da;

                }
             */

                ym2 = ym1.SubString(0,7) +"-01" ;
                ym2=IncMonth(ym2,1);
                DtVal = ym2 ;
                DtVal = DtVal-1;

                Query1->SQL->Clear();
                Query1->SQL->Add("select sum(total) as total from saletotal where substring(sdate from 1 for 7)=:sdate");
                Query1->ParamByName("sdate")->AsString = ym1.SubString(0,7);
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->Open();
                while(!Query1->Eof)
                {
                Chart1->Series[0]->AddXY(DtVal,Query1->FieldByName("total")->AsInteger,"",clYellow);
                        Query1->Next();
                }
               //--------------------------------------------------------------------------------------
                Query1->SQL->Clear();
                Query1->SQL->Add("select sum(total) as total from saletotal where substring(sdate from 1 for 7)=:sdate and opt='card'");
                Query1->ParamByName("sdate")->AsString = ym1.SubString(0,7);
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->Open();
                while(!Query1->Eof)
                {
                Chart1->Series[1]->AddXY(DtVal,Query1->FieldByName("total")->AsInteger,"",clRed);
                        Query1->Next();
                }
               //---------------------------------------------------------------------------------------
                Query1->SQL->Clear();
                Query1->SQL->Add("select sum(total) as total from saletotal where substring(sdate from 1 for 7)=:sdate and opt='cash'");
                Query1->ParamByName("sdate")->AsString = ym1.SubString(0,7);
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->Open();
                 while(!Query1->Eof)
                 {
                Chart1->Series[2]->AddXY(DtVal,Query1->FieldByName("total")->AsInteger,"",clBlue);
                        Query1->Next();
                }
                //---------------------------------------------------------------------------------------
                Query1->SQL->Clear();
                Query1->SQL->Add("select sum(total) as total from saletotal where substring(sdate from 1 for 7)=:sdate and opt='usepoint'");
                Query1->ParamByName("sdate")->AsString = ym1.SubString(0,7);
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->Open();
                 while(!Query1->Eof)
                 {
                Chart1->Series[3]->AddXY(DtVal,Query1->FieldByName("total")->AsInteger,"",clFuchsia);
                        Query1->Next();
                }

                da++;
                if(ym1.SubString(0,7) == y2.SubString(0,7))
                {
                Timer1->Enabled = false;
                da=0;
                }
           Query1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TStatForm::FormActivate(TObject *Sender)
{
        DTPick->Date = Date().DateString();
        DTPick2->Date = Date().DateString();
}
//---------------------------------------------------------------------------



