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

#include <vcl.h>
#pragma hdrstop

#include "Calc.h"
#include "selec2.h"
#include "GuestIns.h"
#include "UsePoint.h"
#include "SavePoint.h"
#include "modify.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TCalForm *CalForm;
String enddate="",endtime="",sangho="",temptime="", opt="";
int whanjun=0,points=0;
bool sve=false,savepoint=false ;

String q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m,s1,s2,s3,s4,s5,s6,s7,plus1="";
int ysindex=0,jukindex=0;
bool ime = 0;
//---------------------------------------------------------------------------
__fastcall TCalForm::TCalForm(TComponent* Owner)
        : TForm(Owner)
{ 

        SellGR->Cells[0][0]="바코드";
        SellGR->Cells[1][0]="상품명";
        SellGR->Cells[2][0]="규격";
        SellGR->Cells[3][0]="단가";
        SellGR->Cells[4][0]="수량";
        SellGR->Cells[5][0]="금액";

        SaleList->Cells[0][0]="시간";
        SaleList->Cells[1][0]="총액";
        SaleList->Cells[2][0]="참고";

        BoryuSG->Cells[0][0]="보류시간";
        BoryuSG->Cells[1][0]="보류총액";


  TStringList *pBuffer = new TStringList() ;
  pBuffer->LoadFromFile(ExtractFilePath(ParamStr(0))+"\data\\config.bin") ;

  jukindex = StrToInt(pBuffer->Strings[0]) ;

  q = pBuffer->Strings[3];
  w = pBuffer->Strings[4];
  e = pBuffer->Strings[5];
  r = pBuffer->Strings[6];
  t = pBuffer->Strings[7];
  y = pBuffer->Strings[8];
  u = pBuffer->Strings[9];
  i = pBuffer->Strings[10];
  o = pBuffer->Strings[11];
  p = pBuffer->Strings[12];
  a = pBuffer->Strings[13];
  s = pBuffer->Strings[14];
  d = pBuffer->Strings[15];
  f = pBuffer->Strings[16];
  g = pBuffer->Strings[17];
  h = pBuffer->Strings[18];
  j = pBuffer->Strings[19];
  k = pBuffer->Strings[20];
  l = pBuffer->Strings[21];
  z = pBuffer->Strings[22];
  x = pBuffer->Strings[23];
  c = pBuffer->Strings[24];
  v = pBuffer->Strings[25];
  b = pBuffer->Strings[26];
  n = pBuffer->Strings[27];
  m = pBuffer->Strings[28];
  s1 = pBuffer->Strings[29];
  s2 = pBuffer->Strings[30];
  s3 = pBuffer->Strings[31];
  s4 = pBuffer->Strings[32];
  s5 = pBuffer->Strings[33];
  s6 = pBuffer->Strings[34];
  s7 = pBuffer->Strings[35];
  plus1 = pBuffer->Strings[36];

  sangho ="<< "+ pBuffer->Strings[46] +" >>" + "\x0a";
        sangho+= pBuffer->Strings[47] + "\x0a";
        sangho+="사업자 번호 : ";
        sangho+= pBuffer->Strings[48] + "\x0a";
        sangho+="대표자 명 : ";
        sangho+= pBuffer->Strings[49] + "    ";
        sangho+="전화 : ";
        sangho+= pBuffer->Strings[50] + "\x0a";

  ysindex = StrToInt(pBuffer->Strings[51]);

  delete pBuffer ;



} 

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

void __fastcall TCalForm::SellGRKeyPress(TObject *Sender, char &Key)
{


        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();
        }


        if (Key == 0x09)  //탭 키
        {
                switch (SellGR->Col)
                {
                       case 1:
                               SellGR->Col = 3;
                               break;
                       case 2:
                               SellGR->Col = 0;
                               break;
                       case 5:
                                SellGR->Col = 0;
                                break;
                }
        }
        if(Key==0x0D)
        {
                CalForm->FocusControl(IbEdit);
        }

}



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


bool TCalForm::DBSelect(String str)
{
bool Lck = false;
        //TODO: Add your source code here
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select * from goodslist.db where barcord like '%"+ str+"%' or goods like '%"+str+"%'");

        Query1->Prepare();

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

        if(Query1->RecordCount > 1)
        {
                SelecForm2 = new TSelecForm2(this);
                
                for (int i=1;!Query1->Eof;i++)
                {


                      SelecForm2->SellSG2->Cells[0][i] = Query1->FieldByName("barcord")->AsString;
                      SelecForm2->SellSG2->Cells[1][i] = Query1->FieldByName("goods")->AsString;
                      SelecForm2->SellSG2->Cells[2][i] = Query1->FieldByName("std")->AsString;
                      SelecForm2->SellSG2->Cells[3][i] = Query1->FieldByName("price")->AsString;


                       SelecForm2->SellSG2->RowCount++;
                       SelecForm2->SellSG2->Row = SelecForm2->SellSG2->RowCount-1;

                     Query1->Next();
                }
                SelecForm2->SellSG2->RowCount--;
                SelecForm2->SellSG2->Row = SelecForm2->SellSG2->RowCount-1;

                SelecForm2->ShowModal();
                delete SelecForm2;
                return true;
        }
        else if(Query1->RecordCount == 1)
        {

                for (int i = 1;i < SellGR->RowCount;i++)
                {
                        if(SellGR->Cells[0][i] == Query1->FieldByName("barcord")->AsString )
                        {
                                SellGR->Cells[4][i] = StrToInt(SellGR->Cells[4][i])+1;
                                SellGR->Cells[5][i] = StrToInt(SellGR->Cells[3][i]) * StrToInt(SellGR->Cells[4][i]);
                                TotalED->Text = StrToInt(TotalED->Text) + StrToInt(SellGR->Cells[3][i]);
                                Lck = true;
                                return true;
                                break;
                        }

                }
                if(Lck == false)
                {
                        if(SellGR->Cells[0][SellGR->RowCount-1] != "")
                        {
                                SellGR->RowCount++;
                                SellGR->Row = SellGR->RowCount-1;
                        }

                        SellGR->Cells[0][SellGR->Row] = Query1->FieldByName("barcord")->AsString;
                        SellGR->Cells[1][SellGR->Row] = Query1->FieldByName("goods")->AsString;
                        SellGR->Cells[2][SellGR->Row] = Query1->FieldByName("std")->AsString;
                        SellGR->Cells[3][SellGR->Row] = Query1->FieldByName("price")->AsString;
                        SellGR->Cells[4][SellGR->Row] = "1";
                        SellGR->Cells[5][SellGR->Row] = SellGR->Cells[3][SellGR->Row]; 

                        TotalED->Text = IntToStr(StrToInt(TotalED->Text)+StrToInt(SellGR->Cells[5][SellGR->Row]));

                        return true;
                        
                }
        }
        else
        {
                return false;
        }

}

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

void __fastcall TCalForm::SellGRKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{   

        if(Key == 0x78  && sve==false) //F9
        {
                CalForm->FocusControl(IbEdit);
        }


        if(Key == 0x73 && SellGR->Cells[0][SellGR->Row] != "" && SellGR->Row > 0  && sve==false) //F4 키 한줄 삭제
        {
                if(SellGR->Cells[0][SellGR->Row]=="*************")
                        TotalED->Text =SellGR->Cells[3][SellGR->Row] ;
                else
                        TotalED->Text = StrToInt(TotalED->Text)-StrToInt(SellGR->Cells[5][SellGR->Row]);

                SellGR->Rows[SellGR->Row]->Clear();

                for(int i=SellGR->Row; i<SellGR->RowCount-1; i++)
                {
                        SellGR->Rows[i]->Assign(SellGR->Rows[i+1]);
                }
             

                if(SellGR->RowCount >2)
                  SellGR->RowCount--;

               CalForm->FocusControl(IbEdit);
        }



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

void __fastcall TCalForm::GetEDKeyPress(TObject *Sender, char &Key)
{
GetED->ReadOnly=false;
        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();
        }
        if(Key==0x2e)//.을 누르면 00이 들어감
        {
                GetED->ReadOnly=false;
                GetED->Text=GetED->Text+"00";
                
               GetED->SelStart=GetED->Text.Length();

                GetED->ReadOnly=true;
        }
        if(Key==0x2a)//  '*' 카드결재
        {
                IbEdit->ReadOnly=false;
                if(SellGR->Cells[0][1] != "")
                {
                        if (GetED->Text == "0" || GetED->Text == "")
                                GetED->Text = TotalED->Text;
                
                        if(StrToInt(RemainED->Text) < 0)
                        {
                                ShowMessage("받은 금액이 너무 적습니다.");
                                GetED->Text = 0;
                                RemainED->Text = 0;
                        }
                        else
                        {
                                if(opt=="")
                                        opt="card";
                        
                                CalEnd();
                        }
                 }
                IbEdit->Text = "";
                IbEdit->ReadOnly=true;
        }

        if(Key == 0x2d)   //- 현금 결재
        {
                IbEdit->ReadOnly=false;
                if(SellGR->Cells[0][1] != "")
                {
                        if (GetED->Text == "0" || GetED->Text == "")
                                GetED->Text = TotalED->Text;

                        if(StrToInt(RemainED->Text) < 0)
                        {
                                ShowMessage("받은 금액이 너무 적습니다.");
                                GetED->Text = 0;
                                RemainED->Text = 0;
                        }
                        else
                        {
                                if(opt=="")
                                        opt="cash";
                                CalEnd();
                        }
                 }
                IbEdit->Text = "";
                IbEdit->ReadOnly=true;

        }

        
        switch(Key)
        {
                case 0x2f:GetED->ReadOnly=true;break;
                case 0x2a:GetED->ReadOnly=true;break;
                case 0x2d:GetED->ReadOnly=true;break;
                case 0x2b:GetED->ReadOnly=true;break;
             
        }


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


void __fastcall TCalForm::GetEDChange(TObject *Sender)
{
        try
        {
        if(GetED->Text.Length() > 0)
              RemainED->Text = IntToStr(StrToInt(GetED->Text)-StrToInt(TotalED->Text));
        if(GetED->Text=="" ||GetED->Text==0 )
                RemainED->Text=0;
        }
        catch ( EConvertError &e )
        {
               GetED->Text=0;
               GetED->SelectAll();
        }
}
//---------------------------------------------------------------------------



void __fastcall TCalForm::Timer1Timer(TObject *Sender)
{
        TimeLB->Caption = TDateTime::CurrentDate();
        TimeLB2->Caption = TDateTime::CurrentTime();
}
//---------------------------------------------------------------------------


void TCalForm::CellClear()
{
        //TODO: Add your source code here
         //그리드 초기화
    /*        for (int i = 1; i<= SellGR->RowCount-1; i++)
                for (int j = 0; j <= SellGR->ColCount-1; j++)
                        SellGR->Cells[j][i] = "";
    */
            SellGR->RowCount = 2;
            SellGR->Rows[1]->Clear();
             SellGR->Row = 1;


             CalForm->FocusControl(IbEdit);

}

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

void TCalForm::YungSuOpen(String SangHo)
{
        //TODO: Add your source code here

      double bu,tot=0;
      AnsiString str;
           //---영수증 출력 과 캐쉬드로우 오픈-------------------------------
            String yung_su="";
            if(sve==false)
              yung_su += "\x1b\x70\x30\20\80";  //돈통열기

             yung_su += "\x1b\x40"; //영수증 프린터 초기화

             yung_su += SangHo;
             yung_su += "======================================== \n";
             yung_su += "날짜 : "+enddate+endtime+"\n";
             yung_su += "----------------------------------------\n";
             yung_su += "  상품명       단가    수량     판매가 \n";
             yung_su += "----------------------------------------\n";



             for (int i = 1; i<= SellGR->RowCount-1; i++)
             {
                   if(SellGR->Cells[0][i].SubString(0,1) != "*")
                   {
                        str = SellGR->Cells[1][i]+"                        ";
                        yung_su += str.SubString(0,12);

                        str = "               "+Format("%10.0n", ARRAYOFCONST(((double)StrToInt(SellGR->Cells[3][i]))));
                        yung_su += str.SubString(str.Length()-7,8);

                        str = "               "+Format("%10.0n", ARRAYOFCONST(((double)StrToInt(SellGR->Cells[4][i]))));
                        yung_su += str.SubString(str.Length()-5,6);

                        str = "               "+Format("%10.0n", ARRAYOFCONST(((double)StrToInt(SellGR->Cells[5][i]))));;
                        yung_su += str.SubString(str.Length()-11,13);


                        yung_su += "\n";
                   }
                   else
                   {
                        yung_su +="\n**  합계 "+SellGR->Cells[2][i]+"할인  **\n";
                   }
             }
             yung_su += "\n\n";
             yung_su += "----------------------------------------\n";
             yung_su += "합     계 : "+Format("%10.0n", ARRAYOFCONST(((double)StrToInt(TotalED->Text))))+"원\n";
             yung_su += "받은   돈 : "+Format("%10.0n", ARRAYOFCONST(((double)StrToInt(GetED->Text))))+"원\n";
             yung_su += "거스름 돈 : "+Format("%10.0n", ARRAYOFCONST(((double)StrToInt(RemainED->Text))))+"원\n\n";

             if(opt=="cash*")
             yung_su += "**현금결재 후 포인트 적립하셨습니다.**\n";
             else if(opt=="card*")
             yung_su += "**카드결재 후 포인트 적립하셨습니다.**\n";
             else if(opt=="card")
             yung_su += "**카드결재(또는 소득공제) 하셨습니다.**\n";
             else if(opt=="cash")
             yung_su += "**현금결재 하셨습니다.**\n";

             yung_su += "----------------------------------------\n";

             for(int k=1;k < SellGR->RowCount;k++)
             {
                if(SellGR->Cells[0][k].SubString(0,1)!="*")
                {
                        if(SellGR->Cells[1][k].SubString(0,1)!="*")
                                tot=tot+StrToInt(SellGR->Cells[5][k])/1.1;
                        else
                                tot=tot+StrToInt(SellGR->Cells[5][k]);
                }
                else
                {
                        tot=tot-(tot*0.1);
                }
             }
             bu=StrToInt(TotalED->Text)-tot;

             yung_su += "금     액 : "+Format("%10.0n", ARRAYOFCONST((tot)))+"원\n";
             yung_su += "부 가 세  : "+Format("%10.0n", ARRAYOFCONST((bu)))+"원\n\n";

             yung_su += "----------------------------------------\n";

             yung_su += "이용해 주셔서 감사합니다.\n";
             yung_su += "환불 및 반품시 영수증이 필요하오니 \n";
             yung_su += "잘 보관하시기 바랍니다.\n";
             yung_su += "반품 및 교환은\n 구입일로 부터 1주일 이내\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);

            YungSu->ItemIndex=ysindex; 

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



void TCalForm::CashOpen()
{
        //TODO: Add your source code here
            whanjun++;
            String yung_su;
            yung_su = "\x1b\x40";
         //   yung_su += "\x1b\x69";
            yung_su += "\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,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 __fastcall TCalForm::GetEDKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{


        if(Key == 0x71)//f2키누르면 영수출력
        {
                if(YungSu->ItemIndex == 0)
                {
                        YungSu->ItemIndex=1;

                }
                else
                {
                        YungSu->ItemIndex=0;

                }

        }

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


void __fastcall TCalForm::IbEditKeyPress(TObject *Sender, char &Key)
{

      IbEdit->ReadOnly=false;
        
        if(Key==0x2a)//  '*' 카드결재
        {
                IbEdit->ReadOnly=false;
                if(SellGR->Cells[0][1] != "")      
                {
                        if(sve==false)
                        {
                        if (IbEdit->Text == "0" || IbEdit->Text == "")
                                GetED->Text = TotalED->Text;
                        else
                                GetED->Text = IbEdit->Text;
                        }
                        if(StrToInt(RemainED->Text) < 0)
                        {
                                ShowMessage("받은 금액이 너무 적습니다.");
                                GetED->Text = 0;
                                RemainED->Text = 0;
                        }
                        else
                        {
                                if(opt=="")
                                        opt="card";

                                CalEnd();
                        }


                }
                IbEdit->Text = "";
                points=0;
                IbEdit->ReadOnly=true;


        }
        if(Key == 0x2d)   //- 현금 결재
        {
                IbEdit->ReadOnly=false;
                if(SellGR->Cells[0][1] != "")
                {
                        if(sve==false)
                        {
                        if (IbEdit->Text == "0" || IbEdit->Text == "")
                                GetED->Text = TotalED->Text;
                        else
                                GetED->Text = IbEdit->Text;
                        }
                        if(StrToInt(RemainED->Text) < 0)
                        {
                                ShowMessage("받은 금액이 너무 적습니다.");
                                GetED->Text = 0;
                                RemainED->Text = 0;
                        }

                        else
                        {
                                if(opt=="")
                                        opt="cash";
                                CalEnd();
                        }
                }
                IbEdit->Text = "";
                points=0;
                IbEdit->ReadOnly=true;

        }
        if(Key==0x2f) // '/' 키  수량변경
        {
               IbEdit->ReadOnly=true;
               if(SellGR->Cells[0][SellGR->Row]!="" && IbEdit->Text != "")
               {

                TotalED->Text = StrToInt(TotalED->Text)-StrToInt(SellGR->Cells[5][SellGR->Row]);
                SellGR->Cells[4][SellGR->Row] = IbEdit->Text;
                SellGR->Cells[5][SellGR->Row] = StrToInt(SellGR->Cells[3][SellGR->Row])*StrToInt(SellGR->Cells[4][SellGR->Row]);
                TotalED->Text = StrToInt(TotalED->Text) + StrToInt(SellGR->Cells[5][SellGR->Row]);

               }
                IbEdit->Text = "";
        }

        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();

                if(DTPick->Date.DateString() != Date().DateString())
                {
                        DTPick->Date = Date();
                        ShBt->Click();
                }


        }
        if(Key==0x2e)//.을 누르면 00이 들어감
        {
                IbEdit->ReadOnly=false;
                if(IbEdit->Text!="")
                {
                IbEdit->Text=IbEdit->Text+"00";
                //IbEdit->OnKeyPress(this,'0x30');
               // Key=0x30;
               IbEdit->SelStart=IbEdit->Text.Length();
                 }
                 else
                 IbEdit->Text="";
                IbEdit->ReadOnly=true;

        }
        if( Key == 0x0D ) //엔터키
        {
                if(IbEdit->Text !="" && sve==false)
                {
                if(SellGR->Cells[0][1] == "")
                {
                        TotalED->Text = 0;
                        GetED->Text = 0;
                        RemainED->Text = 0;
                       // ending = false;
                }
                if(!DBSelect(IbEdit->Text))
                 ShowMessage("없는 상품입니다.");
                 
                 SellGR->Row = SellGR->RowCount-1;

                 if(SellGR->RowCount>21)
                       SellGR->TopRow = SellGR->RowCount-21;
                 }
                IbEdit->Text = "";
        }

        if(Key == 0x2b && plus1!="" && ime==0)//+키
        {
                SangPoom(plus1.SubString(0,13));
        }
        if((Key == 0x71 || Key == 0x51) && q!="" && ime==0)//q키
        {
                SangPoom(q.SubString(0,13));
        }
        if((Key == 0x77 || Key == 0x57) && w!="" && ime==0)//w키
        {
                SangPoom(w.SubString(0,13));
        }
        if((Key == 0x65  || Key == 0x45) && e!="" && ime==0)//e키
        {
                SangPoom(e.SubString(0,13));
        }
        if((Key == 0x72 || Key == 0x52) && r!="" && ime==0)//r키
        {
                SangPoom(r.SubString(0,13));
        }
        if((Key == 0x74 || Key == 0x54) && t!="" && ime==0)//t키
        {
                SangPoom(t.SubString(0,13));
        }
        if((Key == 0x79 || Key == 0x59) && y!="" && ime==0)//y키
        {
                SangPoom(y.SubString(0,13));
        }
        if((Key == 0x75 || Key == 0x55) && u!="" && ime==0)//u키
        {
                SangPoom(u.SubString(0,13));
        }
        if((Key == 0x69 || Key == 0x49) && i!="" && ime==0)//i키
        {
                SangPoom(i.SubString(0,13));
        }
        if((Key == 0x6f || Key == 0x4f) && o!="" && ime==0)//o키
        {
                SangPoom(o.SubString(0,13));
        }
        if((Key == 0x70 || Key == 0x50) && p!="" && ime==0)//p키
        {
                SangPoom(p.SubString(0,13));
        }
        if((Key == 0x61 || Key == 0x41) && a!="" && ime==0)//a키
        {
                SangPoom(a.SubString(0,13));
        }
        if((Key == 0x73 || Key == 0x53) && s!="" && ime==0)//s키
        {
                SangPoom(s.SubString(0,13));
        }
        if((Key == 0x64 || Key == 0x44) && d!="" && ime==0)//d키
        {
                SangPoom(d.SubString(0,13));
        }
        if((Key == 0x66 || Key == 0x46) && f!="" && ime==0)//f키
        {
                SangPoom(f.SubString(0,13));
        }
        if((Key == 0x67 || Key == 0x47) && g!="" && ime==0)//g키
        {
                SangPoom(g.SubString(0,13));
        }
        if((Key == 0x68 || Key == 0x48) && h!="" && ime==0)//h키
        {
                SangPoom(h.SubString(0,13));
        }
        if((Key == 0x6a || Key == 0x4a) && j!="" && ime==0)//j키
        {
                SangPoom(j.SubString(0,13));
        }
        if((Key == 0x6b || Key == 0x4b) && k!="" && ime==0)//k키
        {
                SangPoom(k.SubString(0,13));
        }
        if((Key == 0x6c || Key == 0x4c) && l!="" && ime==0)//l키
        {
                SangPoom(l.SubString(0,13));
        }
        if((Key == 0x7a || Key == 0x5a) && z!=""  && ime==0)//z키
        {
                SangPoom(z.SubString(0,13));
        }
        if((Key == 0x78 || Key == 0x58) && x!=""  && ime==0)//x키
        {
                SangPoom(x.SubString(0,13));
        }
        if((Key == 0x63 || Key == 0x43) && c!=""  && ime==0)//c키
        {
                SangPoom(c.SubString(0,13));
        }
        if((Key == 0x76 || Key == 0x56) && v!="" && ime==0)//v키
        {
                SangPoom(v.SubString(0,13));
        }
        if((Key == 0x62 || Key == 0x42) && b!="" && ime==0 )//b키
        {
                SangPoom(b.SubString(0,13));
        }
        if((Key == 0x6e || Key == 0x4e) && n!="" && ime==0 )//n키
        {
                SangPoom(n.SubString(0,13));
        }
        if((Key == 0x6d || Key == 0x4d) && m!="" && ime==0 )//m키
        {
                SangPoom(m.SubString(0,13));
        }
        if(Key == 0x5b && s1!=""  && ime==0)//[키
        {
                SangPoom(s1.SubString(0,13));
        }
        if(Key == 0x5d  && s2!=""  && ime==0)//]키
        {
                SangPoom(s2.SubString(0,13));
        }
        if(Key == 0x3b && s3!=""  && ime==0)//;키
        {
                SangPoom(s3.SubString(0,13));
        }
        if(Key == 0x27 && s4!=""  && ime==0)//'키
        {
                SangPoom(s4.SubString(0,13));
        }
        if(Key == 0x2c && s5!=""  && ime==0)//,키
        {
                SangPoom(s5.SubString(0,13));
        }
        if(Key == 0x2e && s6!=""  && ime==0)//.키
        {
                SangPoom(s6.SubString(0,13));
        }
        if(Key == 0x2f && s7!=""  && ime==0)///키
        {
                SangPoom(s7.SubString(0,13));
        }

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

void __fastcall TCalForm::IbEditKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{       IbEdit->ReadOnly=false;
try{
        if(Key==0x77) //F8
        {
               if(ime==1)
                {
                        ime = 0;
                        imeLabel->Caption="계산하기";
                }
                else if(ime==0)
                {
                        ime = 1;
                        imeLabel->Caption="검색하기";
                }
        }

if(jukindex==0)
{
        if(Key==0x2e)//delete key 포인트 사용
        {       IbEdit->ReadOnly=true;
                if(sve==false && savepoint==false && SellGR->Cells[0][1]!="")
                {
                        IbEdit->ReadOnly=true;
                        UsePointForm = new TUsePointForm(this);
                        UsePointForm->ShowModal();
                        delete UsePointForm;
                }
                else
                {
                      ShowMessage("적립 또는 사용을 하셨습니다.");
                }


        }

        if(Key==0x2d) //insert key 포인트 적립
        {
                IbEdit->ReadOnly=true;  points=0;
              //  if(sve==false  && savepoint==false && SellGR->Cells[0][1]!="")
              //  {

                        for(int dd=1; dd < SellGR->RowCount; dd++)
                        {
                                if(SellGR->Cells[5][dd]!="" && SellGR->Cells[1][dd].SubString(SellGR->Cells[1][dd].Length(),1)!="@")
                                        points+=StrToInt(SellGR->Cells[5][dd]);
                        }

                        SavePointForm = new TSavePointForm(this);
                        SavePointForm->SBuyEd->Text = points;
                        SavePointForm->ShowModal();
                        delete SavePointForm;

             //   }
             //   else
            //    {
             //           ShowMessage("적립 또는 사용을 하셨습니다.");
             //   }
        }
}
        if(Key==0x76) //F7 단가변경
        {
                 IbEdit->ReadOnly=true;
                if(SellGR->Cells[0][SellGR->Row]!="" && IbEdit->Text != "")
               {

                TotalED->Text = IntToStr(StrToInt(TotalED->Text)-StrToInt(SellGR->Cells[5][SellGR->Row]));
                SellGR->Cells[3][SellGR->Row] = IbEdit->Text;
                SellGR->Cells[5][SellGR->Row] = IntToStr(StrToInt(SellGR->Cells[3][SellGR->Row])*StrToInt(SellGR->Cells[4][SellGR->Row]));
                TotalED->Text = IntToStr(StrToInt(TotalED->Text) + StrToInt(SellGR->Cells[5][SellGR->Row]));

               }
                IbEdit->Text = "";

        }


        if(Key == 0x78) //F9
        {
                CalForm->FocusControl(SellGR);
        }
        /*
        if(Key==0x77 && IbEdit->Text != "") //F8키   수량변경
        {
                TotalED->Text = IntToStr(StrToInt(TotalED->Text)-StrToInt(SellGR->Cells[5][SellGR->Row]));
                SellGR->Cells[4][SellGR->Row] = IbEdit->Text;
                SellGR->Cells[5][SellGR->Row] = IntToStr(StrToInt(SellGR->Cells[3][SellGR->Row])*StrToInt(SellGR->Cells[4][SellGR->Row]));
                TotalED->Text = IntToStr(StrToInt(TotalED->Text) + StrToInt(SellGR->Cells[5][SellGR->Row]));
                IbEdit->Text = "";
        }
        */
        if(Key==0x76 && IbEdit->Text != "") //F7키   단가변경
        {
                if(StrToInt(IbEdit->Text)>0)
                {
                        TotalED->Text = IntToStr(StrToInt(TotalED->Text)-StrToInt(SellGR->Cells[5][SellGR->Row]));
                        SellGR->Cells[3][SellGR->Row] = IbEdit->Text;
                        SellGR->Cells[5][SellGR->Row] = IntToStr(StrToInt(SellGR->Cells[3][SellGR->Row])*StrToInt(SellGR->Cells[4][SellGR->Row]));
                        TotalED->Text = IntToStr(StrToInt(TotalED->Text) + StrToInt(SellGR->Cells[5][SellGR->Row]));
                        IbEdit->Text = "";
                }
                else
                {
                        ShowMessage("금액이 너무 적습니다.");
                        IbEdit->Text = "";
                }
        }                       //  && IbEdit->Text != ""
        if(Key==0x75 )//F6키   반품
        {

                        TotalED->Text = IntToStr(StrToInt(TotalED->Text)-StrToInt(SellGR->Cells[5][SellGR->Row]));
                        SellGR->Cells[4][SellGR->Row] = StrToInt(SellGR->Cells[4][SellGR->Row])*-1;
                        SellGR->Cells[5][SellGR->Row] = IntToStr(StrToInt(SellGR->Cells[3][SellGR->Row])*StrToInt(SellGR->Cells[4][SellGR->Row]));
                        TotalED->Text = IntToStr(StrToInt(TotalED->Text) + StrToInt(SellGR->Cells[5][SellGR->Row]));
                        IbEdit->Text = "";

        }

        if(Key == 0x72 && IbEdit->Text != "") //F3키 할인
        {
                if(SellGR->Cells[0][1] == "")
                {
                        CellClear2();
                       // ending = false;
                }
                else
                {
                        if(SellGR->Cells[0][SellGR->RowCount-1]!="*************")
                                SellGR->RowCount++;
                        else
                                TotalED->Text=SellGR->Cells[3][SellGR->RowCount-1];

                        SellGR->Cells[0][SellGR->RowCount-1]="*************";
                        SellGR->Cells[1][SellGR->RowCount-1]="할인";
                        SellGR->Cells[2][SellGR->RowCount-1]=IbEdit->Text+"%";
                        SellGR->Cells[3][SellGR->RowCount-1]=TotalED->Text;
                        SellGR->Cells[4][SellGR->RowCount-1]=1;
                        SellGR->Cells[5][SellGR->RowCount-1]=StrToInt(TotalED->Text)-(StrToInt(TotalED->Text)*(StrToInt(IbEdit->Text)*0.01));
                        TotalED->Text = SellGR->Cells[5][SellGR->RowCount-1];

                        IbEdit->Text="";

                        if(SellGR->RowCount>21)
                                SellGR->TopRow = SellGR->RowCount-21;
                }


        }

         if(Key == 0x74) //F5키 누르면 돈통열기
        {
                CashOpen();
        }


        if(Key == 0x71)//f2키누르면 영수출력
        {
                if(YungSu->ItemIndex == 0)
                {
                        YungSu->ItemIndex=1;

                }
                else
                {
                        YungSu->ItemIndex=0;

                }

        }

        if(Key==0x70  && sve==false) //F1키 누르면 보류
        {
                         //--이전 판매리스트-----
                if(SellGR->Cells[0][1]!="")
                {
                temptime = Time();
                Query1->Close();
                
                if(BoryuSG->Cells[0][BoryuSG->RowCount-1] != "")
                {
                       BoryuSG->RowCount++;
                       BoryuSG->Row =  BoryuSG->RowCount-1;
                }
                BoryuSG->Cells[0][BoryuSG->Row] = temptime;
                BoryuSG->Cells[1][BoryuSG->Row] = TotalED->Text;

                if(BoryuSG->RowCount >6)
                {
                        BoryuSG->TopRow = BoryuSG->RowCount-5;
                }
                //-------------------------------------------------------

                for( int i = 1; i< SellGR->RowCount; i++ )
                {
                        if(SellGR->Cells[0][i] != "")
                        {
                           //--------개별리스트 저장--------------
                                Query1->SQL->Clear();
                                Query1->SQL->Add("insert into temp ");
                                Query1->SQL->Add("(datetime,barcord,goods,std,price,many,saleprice) ");
                                Query1->SQL->Add("values (:datetime,:barcord,:goods,:std,:price,:many,:saleprice) ");

                                Query1->ParamByName("datetime")->AsString=temptime;
                                Query1->ParamByName("barcord")->AsString=SellGR->Cells[0][i];
                                Query1->ParamByName("goods")->AsString=SellGR->Cells[1][i];
                                Query1->ParamByName("std")->AsString=SellGR->Cells[2][i];
                                Query1->ParamByName("price")->AsInteger=StrToInt(SellGR->Cells[3][i]);
                                Query1->ParamByName("many")->AsInteger=StrToInt(SellGR->Cells[4][i]);
                                Query1->ParamByName("saleprice")->AsInteger=StrToInt(SellGR->Cells[5][i]);


                                Query1->Prepare();
                                if(Query1->Prepared)
                                        Query1->ExecSQL();
                           }
                }
                CellClear2();
                }
                else
                {
                        CalForm->FocusControl(BoryuSG);
                }
        }
}catch ( EConvertError &e ){}
}
//---------------------------------------------------------------------------

void __fastcall TCalForm::FormActivate(TObject *Sender)
{
        if(Screen->Width == 1024)
        {
                CalForm->Height = Screen->Height;
                CalForm->Width = Screen->Width;
                CalForm->Left=0;
                CalForm->Top=0;
        }
        CalForm->FocusControl(IbEdit);
        IbEdit->BorderStyle = 19;
        TotalED->BorderStyle = 19;
        GetED->BorderStyle = 19;
        RemainED->BorderStyle = 19;
        DTPick->Date = Date();
        ShBt->OnClick(this);
        BoryuSearch();





        YungSu->ItemIndex = ysindex;
}

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

void __fastcall TCalForm::ShBtClick(TObject *Sender)
{
/*
        for (int i = 1; i<= SaleList->RowCount-1; i++)
                for (int j = 0; j <= SaleList->ColCount-1; j++)
                        SaleList->Cells[j][i]="";
 */
    try{
             SaleList->RowCount = 2;
             SaleList->Rows[1]->Clear();
             SaleList->Row = 1;
             
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("select * from saletotal");
                Query1->SQL->Add("where sdate= :sdate");

                Query1->ParamByName("sdate")->AsString = DTPick->Date.DateString();

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

                for(int i=1;!Query1->Eof;i++)
                {

                        if(SaleList->Cells[0][SaleList->RowCount-1] != "")
                        {
                                SaleList->RowCount++;
                                SaleList->Row =  SellGR->RowCount-1;
                        }
                       SaleList->Cells[0][i] = Query1->FieldByName("stime")->AsString;
                       SaleList->Cells[1][i] = Query1->FieldByName("total")->AsString;
                       opt = Query1->FieldByName("opt")->AsString;
                       if(opt == "card*")
                        SaleList->Cells[2][i] = "카드적립";
                        else if(opt == "cash*")
                        SaleList->Cells[2][i] = "현금적립";
                        else if(opt == "card")
                        SaleList->Cells[2][i] = "카드결재";
                        else if(opt == "cash")
                        SaleList->Cells[2][i] = "";
                        else if(opt == "usepoint")
                        SaleList->Cells[2][i] = "적립금사용";

                      Query1->Next();

                }
                Query1->Close();
                
                if(SaleList->RowCount >17)
                {
                        SaleList->TopRow = SaleList->RowCount-17;
                }

                SaleList->Row = SaleList->RowCount-1;
                opt="";
       }catch(EInvalidGridOperation &e){IbEdit->OnKeyPress(this,0x1b);ShBt->Click();}         

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


void __fastcall TCalForm::SaleListKeyPress(TObject *Sender, char &Key)
{
        if( Key == 0x0D)//enter key
        {
               if(SaleSearch(DTPick->Date.DateString())==true);
               {
                        if(SellGR->RowCount>21)
                                SellGR->TopRow = SellGR->RowCount-21;
           
                        CalForm->FocusControl(IbEdit);
               }
        }
        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();
        }
        if(Key==0x2a)//* 카드 결재
        {
                SaveAsOpt("card");
        }
        if(Key == 0x2d)   //- 현금 결재
        {
                SaveAsOpt("cash");
        }
}
//---------------------------------------------------------------------------

void __fastcall TCalForm::SaleListKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key == 0x71)//f1키누르면 영수출력
        {
                if(YungSu->ItemIndex == 0)
                {
                        YungSu->ItemIndex=1;

                }
                else
                {
                        YungSu->ItemIndex=0;

                }

        }
        if(Key==0x73) //삭제..
        {
                if(MessageDlg ("삭제하려는 데이타가 맞습니까?", mtWarning, TMsgDlgButtons () << mbOK<<mbCancel, 0)==1)
                {
                        Query1->Close();
                        Query1->SQL->Clear();
                        Query1->SQL->Add("delete from saletotal where sdate=:sdate and stime=:stime and total=:total");
                        Query1->ParamByName("sdate")->AsString = DTPick->Date.DateString();
                        Query1->ParamByName("stime")->AsString = SaleList->Cells[0][SaleList->Row];
                        Query1->ParamByName("total")->AsInteger = StrToInt(SaleList->Cells[1][SaleList->Row]);

                        Query1->Prepare();

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

                        Query1->Close();
                        Query1->SQL->Clear();
                        Query1->SQL->Add("delete from salelist where sdate=:sdate and stime=:stime");
                        Query1->ParamByName("sdate")->AsString = DTPick->Date.DateString();
                        Query1->ParamByName("stime")->AsString = SaleList->Cells[0][SaleList->Row];

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


                        ShBt->Click();
                        CalForm->FocusControl(IbEdit);
                }
        }
}

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

void __fastcall TCalForm::EscBtClick(TObject *Sender)
{
         for (int i = 1; i<= SaleList->RowCount-1; i++)
                for (int j = 0; j <= SaleList->ColCount-1; j++)
                        SaleList->Cells[j][i] = "";
         SaleList->RowCount = 2;
         DTPick->Date = Date();
         
         CellClear2();

         ShBt->OnClick(this);
         CalForm->FocusControl(IbEdit);
}
//---------------------------------------------------------------------------


void TCalForm::CellClear2()
{
        //TODO: Add your source code here
        CellClear();
         IbEdit->Text = "";
         TotalED->Text = 0;
         GetED->Text = 0;
         RemainED->Text = 0;
         YungSu->ItemIndex=ysindex;


}

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

void __fastcall TCalForm::BoryuSGKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key == 0x71)//f1키누르면 영수출력
        {
                if(YungSu->ItemIndex == 0)
                {
                        YungSu->ItemIndex=1;

                }
                else
                {
                        YungSu->ItemIndex=0;

                }

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

void __fastcall TCalForm::BoryuSGKeyPress(TObject *Sender, char &Key)
{

         if( Key == 0x0D)//enter key
                BoryuEnt();
        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();
        }
}
//---------------------------------------------------------------------------


void TCalForm::BoryuSearch()
{
        //TODO: Add your source code here
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select datetime, sum(saleprice) sp from temp group by datetime");
        Query1->Prepare();
        if(Query1->Prepared)
                Query1->Open();
        while(!Query1->Eof)
        {
                if(BoryuSG->Cells[0][BoryuSG->RowCount-1] != "")
                {
                       BoryuSG->RowCount++;
                       BoryuSG->Row =  BoryuSG->RowCount-1;
                }
                BoryuSG->Cells[0][BoryuSG->Row] = Query1->FieldByName("datetime")->AsString;
                BoryuSG->Cells[1][BoryuSG->Row] = Query1->FieldByName("sp")->AsString;
                Query1->Next();
        }

        if(BoryuSG->RowCount >6)
        {
                BoryuSG->TopRow = BoryuSG->RowCount-5;
        }
        
}
//---------------------------------------------------------------------------
void __fastcall TCalForm::SaleListDblClick(TObject *Sender)
{
        SaleList->OnKeyPress(this,0x0D);
}
//---------------------------------------------------------------------------


void TCalForm::BoryuEnt()
{
        //TODO: Add your source code here
       
                CellClear2();
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("select * from temp ");
                Query1->SQL->Add("where datetime = :datetime ");

                Query1->ParamByName("datetime")->AsString = BoryuSG->Cells[0][BoryuSG->Row];


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

                for(int i=1;!Query1->Eof;i++)
                {
                        if(SellGR->Cells[0][SellGR->RowCount-1] != "")
                        {
                                SellGR->RowCount++;
                                SellGR->Row =  SellGR->RowCount-1;
                        }
                       SellGR->Cells[0][i] = Query1->FieldByName("barcord")->AsString;
                       SellGR->Cells[1][i] = Query1->FieldByName("goods")->AsString;
                       SellGR->Cells[2][i] = Query1->FieldByName("std")->AsString;
                       SellGR->Cells[3][i] = Query1->FieldByName("price")->AsString;
                       SellGR->Cells[4][i] = Query1->FieldByName("many")->AsString;
                       SellGR->Cells[5][i] = Query1->FieldByName("saleprice")->AsString;

                      Query1->Next();

                }
                
                TotalED->Text =  BoryuSG->Cells[1][BoryuSG->Row];
                //DB 삭제------------------------------------------
                Query1->SQL->Clear();
                Query1->SQL->Add("delete from temp where datetime = :datetime ");
                Query1->ParamByName("datetime")->AsString = BoryuSG->Cells[0][BoryuSG->Row];
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->ExecSQL();

                //줄 삭제 --------------------------------

                BoryuSG->Rows[BoryuSG->Row]->Clear();

                for(int i=BoryuSG->Row; i<BoryuSG->RowCount-1; i++)
                {
                        BoryuSG->Rows[i]->Assign(BoryuSG->Rows[i+1]);
                }


                if(BoryuSG->RowCount >2)
                  BoryuSG->RowCount--;

               //--------------------------------------
               SellGR->Row = SellGR->RowCount-1;
                if(SellGR->RowCount>21)
                         SellGR->TopRow = SellGR->RowCount-21;
                CalForm->FocusControl(IbEdit);
              //  ending=false;
        

}
//---------------------------------------------------------------------------
void __fastcall TCalForm::BoryuSGDblClick(TObject *Sender)
{
        BoryuEnt();
}
//---------------------------------------------------------------------------


void TCalForm::SavePoint(bool sp,String op)
{
        //TODO: Add your source code here
       // savepoint = sp; //적립완료
        if(sp==true)
                opt = op;
}
//---------------------------------------------------------------------------
void TCalForm::CalEnd()
{
        //TODO: Add your source code here
             if( sve == false && SellGR->Cells[0][1] != "")
             {
                enddate = Date();
                endtime = Time();

                Query1->Close();

                //---------토탈 저장------------
                Query1->SQL->Clear();
                Query1->SQL->Add("insert into saletotal ");
                Query1->SQL->Add("(sdate,stime,total,getmony,remain,opt) ");
                Query1->SQL->Add("values (:sdate,:stime,:total,:getmony,:remain,:opt) ");

                Query1->ParamByName("sdate")->AsString=enddate;
                Query1->ParamByName("stime")->AsString=endtime;
                Query1->ParamByName("total")->AsInteger= StrToInt(TotalED->Text);
                Query1->ParamByName("getmony")->AsInteger= StrToInt(GetED->Text);
                Query1->ParamByName("remain")->AsInteger= StrToInt(RemainED->Text);
                Query1->ParamByName("opt")->AsString= opt;
                Query1->Prepare();

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

                  //--이전 판매리스트-----
                if(SaleList->Cells[0][SaleList->RowCount-1] != "")
                {
                       SaleList->RowCount++;
                       SaleList->Row =  SaleList->RowCount-1;
                }
                SaleList->Cells[0][SaleList->Row] = endtime;
                SaleList->Cells[1][SaleList->Row] = TotalED->Text;
                if(opt == "card*")
                SaleList->Cells[2][SaleList->Row] = "카드적립";
                else if(opt == "cash*")
                SaleList->Cells[2][SaleList->Row] = "현금적립";
                else if(opt == "card")
                SaleList->Cells[2][SaleList->Row] = "카드결재";
                else if(opt == "usepoint")
                SaleList->Cells[2][SaleList->Row] = "적립금사용";

                if(SaleList->RowCount >18)
                {
                        SaleList->TopRow = SaleList->RowCount-17;
                }
                //-------------------------------------------------------


                for( int i = 1; i<= SellGR->RowCount-1; i++ )
                {
                        if(SellGR->Cells[0][i] != "")
                        {
                           //--------개별리스트 저장--------------
                                Query1->SQL->Clear();
                                Query1->SQL->Add("insert into salelist ");
                                Query1->SQL->Add("(barcord,goods,std,buyprice,many,sellprice,sdate,stime) ");
                                Query1->SQL->Add("values (:barcord,:goods,:std,:buyprice,:many,:sellprice,:sdate,:stime) ");

                                Query1->ParamByName("barcord")->AsString=SellGR->Cells[0][i];
                                Query1->ParamByName("goods")->AsString=SellGR->Cells[1][i];
                                Query1->ParamByName("std")->AsString=SellGR->Cells[2][i];
                                Query1->ParamByName("buyprice")->AsInteger=StrToInt(SellGR->Cells[3][i]);
                                Query1->ParamByName("many")->AsInteger=StrToInt(SellGR->Cells[4][i]);
                                Query1->ParamByName("sellprice")->AsInteger=StrToInt(SellGR->Cells[5][i]);
                                Query1->ParamByName("sdate")->AsString=enddate;
                                Query1->ParamByName("stime")->AsString=endtime;

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

                                //---------재고 차감---------------------
                                Query1->SQL->Clear();
                                Query1->SQL->Add("update goodslist.db set stock=stock-:stock ");
                                Query1->SQL->Add("where barcord=:barcord");

                                Query1->ParamByName("barcord")->AsString=SellGR->Cells[0][i];
                                Query1->ParamByName("stock")->AsInteger= StrToInt(SellGR->Cells[4][i]);

                                Query1->Prepare();

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

                        }


                 }

           }
          

          if(SellGR->Cells[0][1] != "")
          {
                if(YungSu->ItemIndex==0)
                        YungSuOpen(sangho);
                else
                        CashOpen();
           }
           opt="";
           sve = false;
           savepoint=false;
           CellClear();
        
}
//---------------------------------------------------------------------------




void __fastcall TCalForm::ListBox1KeyPress(TObject *Sender, char &Key)
{
        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();
        }        
}
//---------------------------------------------------------------------------

void __fastcall TCalForm::DTPickKeyPress(TObject *Sender, char &Key)
{
        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();
        }        
}
//---------------------------------------------------------------------------

void __fastcall TCalForm::ShBtKeyPress(TObject *Sender, char &Key)
{
        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();
        }        
}
//---------------------------------------------------------------------------

void __fastcall TCalForm::EscBtKeyPress(TObject *Sender, char &Key)
{
        if(Key == 0x1b)//esc키
        {
                sve = false;
                CellClear2();
        }        
}
//---------------------------------------------------------------------------




void __fastcall TCalForm::RemainEDChange(TObject *Sender)
{
        try
        {
                if(StrToInt(RemainED->Text) < 0 && StrToInt(GetED->Text)==0)
                  RemainED->Text=0;
        }
        catch ( EConvertError &e )
        {
        }
        
}
//---------------------------------------------------------------------------

void __fastcall TCalForm::TotalEDChange(TObject *Sender)
{
        if(TotalED->Text == "")
                TotalED->Text = 0;
}
//---------------------------------------------------------------------------


void TCalForm::SaveAsOpt(String op)
{
        //TODO: Add your source code here
        if(SaleList->Cells[0][SaleList->Row]!="")
        {
                if(op == "card")
                        SaleList->Cells[2][SaleList->Row]="카드결재";
                else if(op == "cash")
                        SaleList->Cells[2][SaleList->Row]="";


                Query1->SQL->Clear();
                Query1->SQL->Add("update saletotal set opt=:opt ");
                Query1->SQL->Add("where sdate=:sdate and stime = :stime");

                Query1->ParamByName("opt")->AsString = op;
                Query1->ParamByName("sdate")->AsString= DTPick->Date.DateString();
                Query1->ParamByName("stime")->AsString= SaleList->Cells[0][SaleList->Row];

                Query1->Prepare();

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

        }

}
//---------------------------------------------------------------------------
void TCalForm::SangPoom(String ba)
{
        //TODO: Add your source code here
               IbEdit->ReadOnly=false;

try
{

                if(SellGR->Cells[0][1] == "")
                {
                        TotalED->Text = 0;
                        GetED->Text = 0;
                        RemainED->Text = 0;
                       // ending = false;
                }
                if(SellGR->Cells[0][SellGR->RowCount-1] == "*************")
                {
                        TotalED->Text=SellGR->Cells[3][SellGR->RowCount-1];
                        SellGR->RowCount--;
                }
                if(SellGR->Cells[0][SellGR->RowCount-1] != "")
                {
                       SellGR->RowCount++;
                       SellGR->Row =  SellGR->RowCount-1;
                }

                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("select * from goodslist where barcord =:barcode");
                Query1->ParamByName("barcode")->AsString = ba;
                Query1->Prepare();

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

                SellGR->Cells[0][SellGR->Row] = Query1->FieldByName("barcord")->AsString;
                SellGR->Cells[1][SellGR->Row] = Query1->FieldByName("goods")->AsString;
                SellGR->Cells[2][SellGR->Row] = Query1->FieldByName("std")->AsString;

                if(IbEdit->Text!="")
                {
                        SellGR->Cells[3][SellGR->Row] = IbEdit->Text;
                }
                else
                {
                        SellGR->Cells[3][SellGR->Row] = Query1->FieldByName("price")->AsString;
                }

                SellGR->Cells[4][SellGR->Row] = 1;
                SellGR->Cells[5][SellGR->Row] = StrToInt(SellGR->Cells[3][SellGR->Row])*StrToInt(SellGR->Cells[4][SellGR->Row]);

                TotalED->Text =  IntToStr(StrToInt(TotalED->Text)+StrToInt(SellGR->Cells[5][SellGR->Row]));

                 if(SellGR->RowCount>21)
                         SellGR->TopRow = SellGR->RowCount-21;
                 SellGR->Row = SellGR->RowCount-1;

}
catch ( EConvertError &e )
{

         if(SellGR->RowCount > 2)
                SellGR->RowCount--;
         else
                SellGR->Rows[SellGR->Row]->Clear();

}
IbEdit->Text = "";

IbEdit->ReadOnly=true;
}

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


bool TCalForm::SaleSearch(String dt)
{
        //TODO: Add your source code here
                CellClear2();

                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("select * from salelist a, saletotal b ");
                Query1->SQL->Add("where a.sdate=b.sdate and a.stime=b.stime ");
                Query1->SQL->Add("and b.sdate=:sdate and b.stime=:stime and b.total=:total");

                Query1->ParamByName("sdate")->AsString = dt;
                enddate = dt;

                Query1->ParamByName("stime")->AsString = SaleList->Cells[0][SaleList->Row];
                endtime = SaleList->Cells[0][SaleList->Row];
                Query1->ParamByName("total")->AsInteger = StrToInt(SaleList->Cells[1][SaleList->Row]);

                Query1->Prepare();

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

                for(int i=1;!Query1->Eof;i++)
                {
                       if(SellGR->Cells[0][SellGR->RowCount-1] != "")
                                SellGR->RowCount++;
                       
                       SellGR->Cells[0][i] = Query1->FieldByName("barcord")->AsString;
                       SellGR->Cells[1][i] = Query1->FieldByName("goods")->AsString;
                       SellGR->Cells[2][i] = Query1->FieldByName("std")->AsString;
                       SellGR->Cells[3][i] = Query1->FieldByName("buyprice")->AsString;
                       SellGR->Cells[4][i] = Query1->FieldByName("many")->AsString;
                       SellGR->Cells[5][i] = Query1->FieldByName("sellprice")->AsString;
                      TotalED->Text =  Query1->FieldByName("total")->AsString;
                       GetED->Text =  Query1->FieldByName("getmony")->AsString;
                       RemainED->Text =  Query1->FieldByName("remain")->AsString;
                       opt = Query1->FieldByName("opt")->AsString;

                      Query1->Next();

                }

        Query1->Close();

        SellGR->Row = SellGR->RowCount-1; 

        if(SellGR->RowCount > 21)
                SellGR->TopRow = SellGR->RowCount-21;

        sve = true;

        return true;
       
}
