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

#include <vcl.h>
#pragma hdrstop

#include "printSet.h"
#include "printForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TBarPrint *BarPrint;
int i=0,a=0;
bool ins=false;
//---------------------------------------------------------------------------
__fastcall TBarPrint::TBarPrint(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


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

        if(ins == false)
        {
                for(i=1;i < PrintSG->RowCount;i++)
                {
                     //   if(BarChk(PrintSG->Cells[0][i])==true)
                    //   {
                                Query1->Close();
                                Query1->SQL->Clear();
                                Query1->SQL->Add("insert into barprint ");
                                Query1->SQL->Add("(num, barcord, txt, price) ");
                                Query1->SQL->Add("values (:num, :barcord, :txt, :price) ");

                                Query1->ParamByName("num")->AsInteger=i;

                                if(PrintSG->Cells[0][i] != "")
                                        Query1->ParamByName("barcord")->AsString = BarSet(PrintSG->Cells[0][i]);
                                else
                                        Query1->ParamByName("barcord")->AsString = PrintSG->Cells[0][i];

                                Query1->ParamByName("txt")->AsString=PrintSG->Cells[1][i];
                                Query1->ParamByName("price")->AsString=PrintSG->Cells[2][i];
                                Query1->Prepare();

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


                     /*       }
                            else
                            {
                                ShowMessage("잘못된 바코드 입니다.");
                            }
                     */
                }
                ins=true;
        }
        else
        {
                 for(i=1;i < PrintSG->RowCount;i++)
                {
                         if(PrintSG->Cells[0][i] != "" )
                         {
                                if(BarChk(PrintSG->Cells[0][i])==true)
                                {
                                Query1->SQL->Clear();
                                Query1->SQL->Add("update barprint set barcord=:barcord,txt=:txt,price=:price");
                                Query1->SQL->Add("where num=:i");

                                Query1->ParamByName("i")->AsInteger=i;
                                Query1->ParamByName("barcord")->AsString=BarSet(PrintSG->Cells[0][i]);
                                Query1->ParamByName("txt")->AsString=PrintSG->Cells[1][i];
                                Query1->ParamByName("price")->AsString=PrintSG->Cells[2][i];
                                Query1->Prepare();

                                if(Query1->Prepared)
                                        Query1->ExecSQL();
                                }
                                else
                                {
                                        ShowMessage("잘못된 바코드 입니다.");
                                }
                         }                

                }

        }


        Preview = new TPreview(this);
        Preview->QuickRep1->Preview();
        
}
//---------------------------------------------------------------------------

void __fastcall TBarPrint::FormActivate(TObject *Sender)
{
        PrintSG->Cells[0][0]="바코드";
        PrintSG->Cells[1][0]="상품명";
        PrintSG->Cells[2][0]="금액";
}
//---------------------------------------------------------------------------

void __fastcall TBarPrint::BitBtn1Click(TObject *Sender)
{
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("delete from barprint");
        Query1->Prepare();

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

         for(i=1;i < PrintSG->RowCount;i++)
         {
                PrintSG->Cells[0][i]="";
                PrintSG->Cells[1][i]="";
                PrintSG->Cells[2][i]="";
         }
         ins = false;
}
//---------------------------------------------------------------------------

void __fastcall TBarPrint::BitBtn3Click(TObject *Sender)
{
          for(i=PrintSG->Row;i < PrintSG->RowCount;i++)
          {
                PrintSG->Cells[0][i]=PrintSG->Cells[0][PrintSG->Row];
                PrintSG->Cells[1][i]=PrintSG->Cells[1][PrintSG->Row];
                PrintSG->Cells[2][i]=PrintSG->Cells[2][PrintSG->Row];
          }

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

void __fastcall TBarPrint::FormClose(TObject *Sender, TCloseAction &Action)
{
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("delete from barprint");
         Query1->Prepare();
                if(Query1->Prepared)
                        Query1->ExecSQL();

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




void __fastcall TBarPrint::PrintSGKeyPress(TObject *Sender, char &Key)
{

        if(Key==0x0D)
        {       String txt="";
                Query1->Close();
                Query1->SQL->Clear();
                txt = PrintSG->Cells[0][PrintSG->Row];
                Query1->SQL->Add("select barcord, goods, std, price from goodslist ");
                Query1->SQL->Add("where goods like '%"+txt+"%' or barcord like '%"+txt+"%'");
                
                Query1->Prepare();
                if(Query1->Prepared)
                        Query1->Open();

                if(Query1->RecordCount == 1)
                {
                        PrintSG->Cells[0][PrintSG->Row] = Query1->FieldByName("barcord")->AsString;
                        PrintSG->Cells[1][PrintSG->Row] = Query1->FieldByName("goods")->AsString+"["+Query1->FieldByName("std")->AsString+"]";
                        PrintSG->Cells[2][PrintSG->Row] = Query1->FieldByName("price")->AsString+" 원";

                        if(PrintSG->Row < PrintSG->RowCount-1)
                        PrintSG->Row++;
                        PrintSG->Col = 0;


                }
                else if(Query1->RecordCount > 1)
                {
                        ListBox1->Left = PrintSG->Left + PrintSG->DefaultColWidth;
                        ListBox1->Top =  PrintSG->Top + (PrintSG->DefaultRowHeight * (PrintSG->Row+1));
                        ListBox1->Visible = true;
                        while(!Query1->Eof)
                        {
                                ListBox1->Items->Add(Query1->FieldByName("goods")->AsString+"["+Query1->FieldByName("std")->AsString+"]"+"/"+Query1->FieldByName("barcord")->AsString);
                                Query1->Next();
                        }
                        BarPrint->FocusControl(ListBox1);
                }
                else
                {
                        ShowMessage("등록되지 않은 상품입니다.");
                }
        }
}
//---------------------------------------------------------------------------


void __fastcall TBarPrint::ListBox1KeyPress(TObject *Sender, char &Key)
{
        if(Key==0x0D)
        {       a=ListBox1->Items->Strings[ListBox1->ItemIndex].Length();
                for(int b=0; b < a ; b++)
                {
                        if(ListBox1->Items->Strings[ListBox1->ItemIndex].SubString(b,1)=="/")
                        {
                                PrintSG->Cells[0][PrintSG->Row]=ListBox1->Items->Strings[ListBox1->ItemIndex].SubString(b+1,13);
                                PrintSG->OnKeyPress(this,0x0D);
                                ListBox1->Items->Clear();
                                BarPrint->FocusControl(PrintSG);
                                ListBox1->Visible=false;

                                break;
                        }
                }

        }
        if(Key== 0x1b)
        {
                ListBox1->Items->Clear();
               BarPrint->FocusControl(PrintSG);
               ListBox1->Visible=false;
        }
}
//---------------------------------------------------------------------------

String TBarPrint::BarSet(String st)
{         //EAN-13 바코드 폰트를 이용한 바코드 출력
        //TODO: Add your source code here
          String temp="",pattern="",b_code="";

       switch (StrToInt(st.SubString(1,1)))
        {
                case 0:
                        pattern = "AAAAAACCCCCC";
                        break;
                case 1:
                        pattern = "AABABBCCCCCC";
                        break;
                case 2:
                        pattern = "AABBABCCCCCC";
                        break;
                case 3:
                        pattern = "AABBBACCCCCC";
                        break;
                case 4:
                        pattern = "ABAABBCCCCCC";
                        break;
                case 5:
                        pattern = "ABBAABCCCCCC";
                        break;
                case 6:
                        pattern = "ABBBAACCCCCC";
                        break;
                case 7:
                        pattern = "ABABABCCCCCC";
                        break;
                case 8:
                        pattern = "ABABBACCCCCC";
                        break;
                case 9:
                        pattern = "ABBABACCCCCC";
                        break;
        }


        b_code = AnsiString((char)TypeStart(StrToInt(st.SubString(1,1))));

        for(int i=2;i<14;i++)
        {
                if(i == 8)
                {
                        b_code += AnsiString((char)89); //바코드 중간패턴 추가
                }
                temp = pattern.SubString(i-1,1);

                if(temp == "A")
                        b_code += AnsiString((char)TypeA(StrToInt(st.SubString(i,1))));
                else if(temp=="B")
                        b_code += AnsiString((char)TypeB(StrToInt(st.SubString(i,1))));
                else if(temp=="C")
                        b_code += AnsiString((char)TypeC(StrToInt(st.SubString(i,1))));


        }

        b_code += AnsiString((char)90);
       
        return b_code;
 
}
//---------------------------------------------------------------------------
int TBarPrint::TypeA(int a)
{
        //TODO: Add your source code here
        a = a + 65;
        return a;

}
//---------------------------------------------------------------------------
int TBarPrint::TypeB(int a)
{
        //TODO: Add your source code here
        a = a + 97;

        return a;
}
//---------------------------------------------------------------------------
int TBarPrint::TypeC(int a)
{
        //TODO: Add your source code here
         a = a + 48;

        return a;
}
//---------------------------------------------------------------------------
int TBarPrint::TypeStart(int a)
{
        //TODO: Add your source code here
        a = a + 78;

        return a;
}
//---------------------------------------------------------------------------
bool TBarPrint::BarChk(String st)
{
        //TODO: Add your source code here
        if(st!="")
        {
                int hol=0,zak=0,temp=0;
                for(int i=1;i<13;i++)
                {
                        if(i%2 > 0)
                                hol+=StrToInt(st.SubString(i,1)); //홀수자리 숫자 더하기
                        else
                                zak+=StrToInt(st.SubString(i,1)); //짝수자리 숫자 더하기
                }
                zak=zak*3;
                hol=hol+zak;

                if(hol%10 > 0)
                        temp = 10-(hol%10);//10의배수를 만들기 위한 숫자
                else
                        temp = 0;

                if(StrToInt(st.SubString(13,1)) == temp)
                        return true;
                else
                        return false;
        }
        else
        {
                return true;
        }
}
//---------------------------------------------------------------------------
void __fastcall TBarPrint::PrintSGKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key== 0x2e)
        {
              /*  PrintSG->Cells[0][PrintSG->Row]="";
                PrintSG->Cells[1][PrintSG->Row]="";
                PrintSG->Cells[2][PrintSG->Row]="";
              */
              
              Query1->SQL->Clear();
              Query1->SQL->Add("update barprint set barcord=:barcord,txt=:txt,price=:price");
              Query1->SQL->Add("where num=:i");

              Query1->ParamByName("i")->AsInteger=PrintSG->Row;
              Query1->ParamByName("barcord")->AsString="";
              Query1->ParamByName("txt")->AsString="";
              Query1->ParamByName("price")->AsString="";
              Query1->Prepare();

              if(Query1->Prepared)
                Query1->ExecSQL();
             
              PrintSG->Rows[PrintSG->Row]->Clear();
        }
}
//---------------------------------------------------------------------------

void __fastcall TBarPrint::ListBox1DblClick(TObject *Sender)
{
        ListBox1->OnKeyPress(this,0x0D);
}
//---------------------------------------------------------------------------

