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

#include <vcl.h>
#pragma hdrstop

#include "JungSan.h"
#include "Stat.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TJungSanForm *JungSanForm;
String yung_su="";


//---------------------------------------------------------------------------
__fastcall TJungSanForm::TJungSanForm(TComponent* Owner)
        : TForm(Owner)
{

          StatForm = new TStatForm(this);

                
          DateTimePicker1->Date = Date().DateString();        
          ShowJS(Date().DateString());
}
//---------------------------------------------------------------------------
void __fastcall TJungSanForm::PrintBtnClick(TObject *Sender)
{
        //---영수증 출력 과 캐쉬드로우 오픈-------------------------------


             yung_su += "오늘 하루 수고 많았습니다.\n"; 
             yung_su += "\x0a\x0a\x0a\x0a";  //
             yung_su += "\x1b\x69";    //영수증 컷팅
             

           

            static HANDLE hFile;
            DWORD dwWri;

            hFile = CreateFile("LPT1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL);
            WriteFile(hFile,yung_su.c_str(),yung_su.Length(),&dwWri,NULL);

            COMSTAT comStat;
            ClearCommError(hFile,&dwWri,&comStat);
            PurgeComm(hFile, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
            CloseHandle(hFile);

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



void TJungSanForm::ShowJS(String date)
{
        //TODO: Add your source code here
        TColor c;
        c=11100011;

        yung_su = "\x1b\x70\x30\20\80";  //돈통열기

        yung_su += "\x1b\x40";//초기화

        yung_su += "  << 정 산 내 역>>\x0a\x0a";
        DayChart->Series[0]->Clear();
        PartChart->Series[0]->Clear();
        String aa,param;

        ListBox1->Clear();
        ListBox1->Items->Add("");
        ListBox1->Items->Add("  << 정 산 내 역>>");
        ListBox1->Items->Add("");
        Query1->Close();


          //--------전체매출액----------------------------------------------------
        Query1->SQL->Clear();

        switch (date.Length())
        {
                case 10:
                        param = " sdate = '"+ date +"'";
                        break;
                case 7:
                        param = " substring(sdate from 1 for 7) = '"+ date+"' ";
                        break;
         }

        Query1->SQL->Add("select sum(total) total from saletotal.db where  "+ param);

        Query1->Prepare();

             if(Query1->Prepared)
                Query1->Open();
         if(!Query1->Eof && Query1->RecordCount > 0)
         {
                aa = "전체매출액: " + Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("total")->AsInteger)))+"원";
                ListBox1->Items->Add(aa) ;
                yung_su += aa +"\x0a" ;
                
         }
         ListBox1->Items->Add("");
         ListBox1->Items->Add("===========================================") ;
         ListBox1->Items->Add("");
         yung_su += "\n=========================================\n\x0a";


       //------판매액--------------------------------------------------------
        Query1->SQL->Clear();

        switch (date.Length())
        {
                case 10:
                        param = " sdate = '"+ date +"'";
                        break;
                case 7:
                        param = " substring(sdate from 1 for 7) = '"+ date+"' ";
                        break;
         }

        Query1->SQL->Add("select sum(sellprice) sellprice from salelist.db where "+ param +" and (sellprice>0 or many > 0) ");

        Query1->Prepare();

         if(Query1->Prepared)
                Query1->Open();

         if(!Query1->Eof && Query1->RecordCount > 0)
         {
                aa = "판매액: " + Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("sellprice")->AsInteger)))+"원";
                ListBox1->Items->Add(aa) ;
                yung_su += aa +"\x0a" ;
         }
         ListBox1->Items->Add("");
         ListBox1->Items->Add("===========================================") ;
         ListBox1->Items->Add("");
         yung_su += "\n=========================================\n\x0a";



       //-------반품액------------------------------------------------------
        Query1->SQL->Clear();

        switch (date.Length())
        {
                case 10:
                        param = " sdate = '"+ date +"'";
                        break;
                case 7:
                        param = " substring(sdate from 1 for 7) = '"+ date+"' ";
                        break;
         }

        Query1->SQL->Add("select sum(sellprice) sellprice from salelist.db where "+ param +" and  (sellprice<0 or many < 0)");

        Query1->Prepare();

        if(Query1->Prepared)
                Query1->Open();
         if(!Query1->Eof && Query1->RecordCount > 0)
        {
                aa = "반품액: " + Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("sellprice")->AsInteger)))+"원";
                ListBox1->Items->Add(aa) ;
                yung_su += aa +"\x0a";
        }
        ListBox1->Items->Add("");
        ListBox1->Items->Add("===========================================") ;
        ListBox1->Items->Add("");
        yung_su += "\n=========================================\n\x0a";


        //--------포인트사용액----------------------------------------------------
        Query1->SQL->Clear();

        switch (date.Length())
        {
                case 10:
                        param = " sdate = '"+ date +"'";
                        break;
                case 7:
                        param = " substring(sdate from 1 for 7) = '"+ date+"' ";
                        break;
         }

        Query1->SQL->Add("select sum(total) total from saletotal.db where opt = 'usepoint' and "+ param);
        
        Query1->Prepare();

             if(Query1->Prepared)
                Query1->Open();
         if(!Query1->Eof && Query1->RecordCount > 0)
         {
                aa = "포인트사용액: " + Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("total")->AsInteger)))+"원";
                ListBox1->Items->Add(aa) ;
                yung_su += aa +"\x0a" ;
                DayChart->Series[0]->Add((double)Query1->FieldByName("total")->AsInteger,"포인트",clGreen);
         }
         ListBox1->Items->Add("");
         ListBox1->Items->Add("===========================================") ;
         ListBox1->Items->Add("");
         yung_su += "\n=========================================\n\x0a";

                 //--------카드매출액----------------------------------------------------
        Query1->SQL->Clear();

        switch (date.Length())
        {
                case 10:
                        param = " sdate = '"+ date +"'";
                        break;
                case 7:
                        param = " substring(sdate from 1 for 7) = '"+ date+"' ";
                        break;
         }

        Query1->SQL->Add("select sum(total) total from saletotal.db where opt = 'card' and "+ param);

        Query1->Prepare();

             if(Query1->Prepared)
                Query1->Open();
         if(!Query1->Eof && Query1->RecordCount > 0)
         {
                aa = "카드매출액: " + Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("total")->AsInteger)))+"원";
                ListBox1->Items->Add(aa) ;
                yung_su += aa +"\x0a(소득공제 포함)\x0a" ;
                DayChart->Series[0]->Add((double)Query1->FieldByName("total")->AsInteger,"카드",clRed);
         }
         ListBox1->Items->Add("(소득공제 포함)");
         ListBox1->Items->Add("===========================================") ;
         ListBox1->Items->Add("");
         yung_su += "\n=========================================\n\x0a";

        //--------현금매출액----------------------------------------------------
        Query1->SQL->Clear();

        switch (date.Length())
        {
                case 10:
                        param = " sdate = '"+ date +"'";
                        break;
                case 7:
                        param = " substring(sdate from 1 for 7) = '"+ date+"' ";
                        break;
         }

        Query1->SQL->Add("select sum(total) total from saletotal.db where opt = 'cash' and "+ param);

        Query1->Prepare();

             if(Query1->Prepared)
                Query1->Open();
         if(!Query1->Eof && Query1->RecordCount > 0)
         {
                aa = "현금매출액: " + Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("total")->AsInteger)))+"원";
                ListBox1->Items->Add(aa) ;
                yung_su += aa +"\x0a" ;
                DayChart->Series[0]->Add((double)Query1->FieldByName("total")->AsInteger,"현금",clBlue);
         }
         ListBox1->Items->Add("");
         ListBox1->Items->Add("===========================================") ;
         ListBox1->Items->Add("");
         yung_su += "\n=========================================\n\x0a";

         
        //-------그룹별 정산 ---------------------------------------------------

        yung_su += "  그룹별 판매액\x0a";
        yung_su += "  ----------------\x0a\x0a";

        ListBox1->Items->Add("그룹별 판매액");
        ListBox1->Items->Add("----------------------");
        ListBox1->Items->Add("");

        Query1->SQL->Clear();

        switch (date.Length())
        {
                case 10:
                        param = " a.sdate = '"+ date +"'";
                        break;
                case 7:
                        param = " substring(a.sdate from 1 for 7) = '"+ date+"' ";
                        break;
         }

        Query1->SQL->Add("select b.groups, count(a.barcord) cnt ,sum(sellprice) etc  from salelist a, groups b, goodslist c  where (a.barcord = c.barcord and c.part = b.numb and "+param+" )and (sellprice>0 or many > 0)  group by b.groups");
        Query1->Prepare();

         if(Query1->Prepared)
                Query1->Open();
         if(Query1->RecordCount > 0)
         {
                while(!Query1->Eof)
                {
                        aa =  Query1->FieldByName("groups")->AsString;
                        aa = aa+"                        ";
                        aa = aa.SubString(0,10);
                        aa = aa +": "+ Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("cnt")->AsInteger))) +"건: "+ Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("etc")->AsInteger)))+"원";
                        ListBox1->Items->Add(aa) ;
                        yung_su += aa +"\x0a\x0a" ;
                                                                                                              //  clMoneyGreen
                        PartChart->Series[0]->Add((double)Query1->FieldByName("etc")->AsInteger,Query1->FieldByName("groups")->AsString,c);
                        c=c+122222;
                        //c=c*2;
                        Query1->Next();
                }
                ListBox1->Items->Add("");
                ListBox1->Items->Add("===========================================") ;
                ListBox1->Items->Add("");
                yung_su += "\n=========================================\n\x0a";
          }

         //-----그룹별 반품현황--------------------------------------------

        yung_su += "  그룹별 반품액\x0a";
        yung_su += "  ----------------\x0a\x0a";
        ListBox1->Items->Add("그룹별 반품액");
        ListBox1->Items->Add("----------------------");
        ListBox1->Items->Add("");
        Query1->SQL->Clear();

        switch (date.Length())
        {
                case 10:
                        param = " sdate = '"+ date +"'";
                        break;
                case 7:
                        param = " substring(sdate from 1 for 7) = '"+ date+"'";
                        break;
         }

        Query1->SQL->Add("select b.groups, count(a.barcord) cnt ,sum(sellprice) etc  from salelist a, groups b, goodslist c  where (a.barcord = c.barcord and c.part = b.numb and "+param+" )and  (sellprice<0 or many < 0) group by b.groups");

        Query1->Prepare();

         if(Query1->Prepared)
                Query1->Open();
         if(Query1->RecordCount > 0)
         {
                while(!Query1->Eof)
                {
                        aa =  Query1->FieldByName("groups")->AsString;
                        aa =  Query1->FieldByName("groups")->AsString;
                        aa = aa+"                        ";
                        aa = aa.SubString(0,10);
                        aa = aa +": "+Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("cnt")->AsInteger))) +"건: "+ Format("%10.0n", ARRAYOFCONST(((double)Query1->FieldByName("etc")->AsInteger)))+"원";
                        ListBox1->Items->Add(aa) ;
                        yung_su += aa +"\x0a" ;
                        Query1->Next();
                }
                ListBox1->Items->Add("");
                ListBox1->Items->Add("===========================================") ;
                ListBox1->Items->Add("");
                yung_su += "\n=========================================\n\x0a";
         }

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

void __fastcall TJungSanForm::ShBtnClick(TObject *Sender)
{

        ShowJS(DateTimePicker1->Date.DateString());
}
//---------------------------------------------------------------------------

void __fastcall TJungSanForm::BitBtn1Click(TObject *Sender)
{
        String tmp = "\x1b\x40";

         //    tmp += "\x1b\x69";    //영수증 컷팅
             tmp += "\x1b\x70\x30\20\80";  //돈통열기

            

            static HANDLE hFile;
            DWORD dwWri;

            hFile = CreateFile("LPT1",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,NULL);
            WriteFile(hFile,tmp.c_str(),tmp.Length(),&dwWri,NULL);

            COMSTAT comStat;
            ClearCommError(hFile,&dwWri,&comStat);
            PurgeComm(hFile, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
            CloseHandle(hFile);
}
//---------------------------------------------------------------------------

void __fastcall TJungSanForm::BitBtn2Click(TObject *Sender)
{

          ShowJS(DateTimePicker1->Date.DateString().SubString(0,7));
}
//---------------------------------------------------------------------------

void __fastcall TJungSanForm::Button1Click(TObject *Sender)
{
        if(StatForm->Visible==false)
        {
                StatForm->Visible=true;
                Button1->Caption = "통계닫기";
        }
        else
        {
                StatForm->Visible=false;
                Button1->Caption = "통계열기";
        }
}
//---------------------------------------------------------------------------

void __fastcall TJungSanForm::Button2Click(TObject *Sender)
{

        if(MessageDlg ("매출데이터가 삭제됩니다.", mtWarning, TMsgDlgButtons () << mbOK<<mbCancel, 0)==1)
        {
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("delete from salelist where sdate >= :sd and sdate <= :ed");
        Query1->ParamByName("sd")->AsString = DTP1->Date.DateString();
        Query1->ParamByName("ed")->AsString = DTP2->Date.DateString();

        Query1->Prepare();
        if(Query1->Prepared)
                Query1->ExecSQL();

        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("delete from saletotal where sdate >= :sd and sdate <= :ed");
        Query1->ParamByName("sd")->AsString = DTP1->Date.DateString();
        Query1->ParamByName("ed")->AsString = DTP2->Date.DateString();

        Query1->Prepare();
        if(Query1->Prepared)
                Query1->ExecSQL();

        ShowMessage("삭제 완료!!");
        }
}
//---------------------------------------------------------------------------

