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

#include <vcl.h>
#pragma hdrstop

#include "Datain.h"
#include "selec.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDataInForm *DataInForm;
String md,date, oldDate; // up이면 update ins이면 insert
int row;

//---------------------------------------------------------------------------
__fastcall TDataInForm::TDataInForm(TComponent* Owner)
        : TForm(Owner)
{
        
 //콤보박스에 데이터를 뿌린다.
    ListSG->Cells[0][0]="바코드";
    ListSG->Cells[1][0]="상품명";
    ListSG->Cells[2][0]="규격";
    ListSG->Cells[3][0]="분류";
    ListSG->Cells[4][0]="재고량";
    ListSG->Cells[5][0]="수량";
    ListSG->Cells[6][0]="단가";
    ListSG->Cells[7][0]="판매가";
    ListSG->Cells[8][0]="매입처";
    ListSG->Cells[9][0]="매입일자";


    GoodsSg->Cells[0][0]="바코드";
    GoodsSg->Cells[1][0]="상품명";
    GoodsSg->Cells[2][0]="규격";
    GoodsSg->Cells[3][0]="분류";
    GoodsSg->Cells[4][0]="재고량";
    GoodsSg->Cells[5][0]="판매가";
    GoodsSg->Cells[6][0]="등록일";


      
}

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


void __fastcall TDataInForm::SaveBtClick(TObject *Sender)
{
        if (BarEdit->Text=="")
        {
                ShowMessage("바코드를 입력하세요.");
        }
        else if (GoodsEdit->Text=="")
        {
                ShowMessage("상품명을 입력하세요.");
        }
        else if (GroupCmb->Text=="")
        {
                ShowMessage("구분을 입력하세요.");
        }

        else if (SpEdit->Text=="")
        {
                ShowMessage("판매가를 입력하세요.");
        }
   /*     else if(!BarChk(BarEdit->Text))
        {
                ShowMessage("잘못된 바코드 입니다.");
        }
   */
        else
        {


              if(!ChkGoods(BarEdit->Text))
              {
                  // if(StockEdit->Text =="")
                        StockEdit->Text = 0;

                    if(SpEdit->Text =="")
                        SpEdit->Text = 0;

                    Query1->Close();

                    Query1->SQL->Clear();
                    Query1->SQL->Add("select numb from groups.db where groups = :groups");
                    Query1->ParamByName("groups")->AsString= GroupCmb->Text;
                    Query1->Prepare();

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

                    int groupNum = Query1->FieldByName("numb")->AsInteger;


                    Query1->SQL->Clear();
                    Query1->SQL->Add("insert into goodslist.db");
                    Query1->SQL->Add(" (Barcord, Goods, Std, Part, Stock, Price, Indate)");
                    Query1->SQL->Add(" values (:barcord, :goods, :std, :part, :stock, :price, :indate)");

                    Query1->ParamByName("barcord")->AsString= BarEdit->Text;

                    if(bugaRG->ItemIndex == 0)
                        Query1->ParamByName("goods")->AsString= GoodsEdit->Text;
                    else
                        Query1->ParamByName("goods")->AsString= "*"+GoodsEdit->Text;

                    if(JukribRG->ItemIndex == 0)
                        Query1->ParamByName("goods")->AsString= GoodsEdit->Text;
                    else
                        Query1->ParamByName("goods")->AsString= GoodsEdit->Text+"@";

                    Query1->ParamByName("std")->AsString=  StdEdit->Text;
                    Query1->ParamByName("part")->AsInteger= groupNum;
                    Query1->ParamByName("stock")->AsInteger= StrToInt(StockEdit->Text);
                    Query1->ParamByName("price")->AsInteger= StrToInt(SpEdit->Text);
                    Query1->ParamByName("indate")->AsString= DateP1->Date.DateString();


                    Query1->Prepare();

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


                            //StringGride에 뿌려준다.

                    if(GoodsSg->Cells[0][GoodsSg->RowCount-1] != "")
                    {
                        
                        GoodsSg->RowCount++;
                        GoodsSg->Row = GoodsSg->RowCount-1;
                    }
                        GoodsSg->Cells[0][GoodsSg->RowCount-1] = BarEdit->Text;
                        BarEdit2->Text = BarEdit->Text;

                        if(bugaRG->ItemIndex == 0)
                        GoodsSg->Cells[1][GoodsSg->RowCount-1] = GoodsEdit->Text;
                        else
                        GoodsSg->Cells[1][GoodsSg->RowCount-1] = "*"+GoodsEdit->Text;

                        GoodsSg->Cells[2][GoodsSg->RowCount-1] = StdEdit->Text;
                        GoodsSg->Cells[3][GoodsSg->RowCount-1] = GroupCmb->Text;
                        GoodsSg->Cells[4][GoodsSg->RowCount-1] = StockEdit->Text;
                        GoodsSg->Cells[5][GoodsSg->RowCount-1] = SpEdit->Text;
                        GoodsSg->Cells[6][GoodsSg->RowCount-1] = DateP1->Date.DateString();



                }
                else
                {
                        ShowMessage("등록된 상품입니다.");
                        BarEdit->OnKeyPress(this,0x1b);
                        DataInForm->FocusControl(BarEdit);
                }

              if(GoodsSg->RowCount >7)
              {
                GoodsSg->TopRow = GoodsSg->RowCount-7;
              }
              DateTimePicker1->Date = DateP1->Date.DateString();

        }
        DataInForm->FocusControl(BarEdit);
        

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



bool TDataInForm::SearchGoods(String st)
{
String sql;
        //TODO: Add your source code here

        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("SELECT a.barcord,a.goods,a.std,b.groups,a.stock,a.price,a.indate FROM goodslist a, groups b ");
        sql =  "where (a.Goods like '%"+st+"%' or a.barcord like '%"+st+"%') and a.part = b.numb order by indate asc";
        Query1->SQL->Add(sql);
        Query1->Prepare();

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

                if(Query1->RecordCount > 1)
                {

                        SelecForm = new TSelecForm(this);

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

                                if(SelecForm->SelSG->Cells[0][SelecForm->SelSG->RowCount-1]!="")
                                {
                                        SelecForm->SelSG->RowCount++;
                                        SelecForm->SelSG->Row = SelecForm->SelSG->RowCount-1;
                                }
                                SelecForm->SelSG->Cells[0][i] = Query1->FieldByName("barcord")->AsString;
                                SelecForm->SelSG->Cells[1][i] = Query1->FieldByName("goods")->AsString;
                                SelecForm->SelSG->Cells[2][i] = Query1->FieldByName("std")->AsString;
                                SelecForm->SelSG->Cells[3][i] = Query1->FieldByName("groups")->AsString;
                                SelecForm->SelSG->Cells[4][i] = Query1->FieldByName("stock")->AsString;
                                SelecForm->SelSG->Cells[5][i] = Query1->FieldByName("price")->AsString;
                                SelecForm->SelSG->Cells[6][i] = Query1->FieldByName("indate")->AsString;

                                Query1->Next();


                        }
                        if(SelecForm->SelSG->RowCount>7)
                                SelecForm->SelSG->TopRow = SelecForm->SelSG->RowCount-7;
                        SelecForm->ShowModal();
                        delete SelecForm;

                        return true;

                }
                else if (Query1->RecordCount == 1)
                {

                        BarEdit->Text = Query1->FieldByName("Barcord")->AsString;
                        BarEdit2->Text = Query1->FieldByName("Barcord")->AsString;
                        GoodsEdit->Text = Query1->FieldByName("Goods")->AsString;
                        StdEdit->Text = Query1->FieldByName("Std")->AsString;
                        SpEdit->Text = Query1->FieldByName("Price")->AsString;
                        StockEdit->Text = Query1->FieldByName("stock")->AsString;
                        GroupCmb->Text = Query1->FieldByName("groups")->AsString;
                        DateP1->Date = Query1->FieldByName("indate")->AsString;
                        return true;

                }
                else if(Query1->RecordCount == 0)
                {
                        return false;

                }
        }


}

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


void __fastcall TDataInForm::ListSGKeyPress(TObject *Sender, char &Key)
{
        if( Key == 0x0D && ListSG->Cells[0][ListSG->Row] != "" )//enter key
        {

                row = ListSG->Row;
                BarEdit->Text = ListSG->Cells[0][row];
                BarEdit2->Text = ListSG->Cells[0][row];
                GoodsEdit->Text = ListSG->Cells[1][row];
                StdEdit->Text = ListSG->Cells[2][row];
                GroupCmb->Text = ListSG->Cells[3][row];
               // MakerEdit->Text = ListSG->Cells[4][row];
                StockEdit->Text = ListSG->Cells[4][row];
                ManyEdit->Text = ListSG->Cells[5][row];
                BpEdit->Text = ListSG->Cells[6][row];
                SpEdit->Text = ListSG->Cells[7][row];
                SellerCmb->Text = ListSG->Cells[8][row];
                DateTimePicker1->Date = ListSG->Cells[9][row];
                oldDate = ListSG->Cells[9][row];

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



void __fastcall TDataInForm::FormActivate(TObject *Sender)
{
       DateP1->Date = Date();
       DateP2->Date = Date();
       DateTimePicker1->Date = Date();
       DateTimePicker2->Date = Date();
     //  SearchIn(Date());
     //  SearchSale(Date());
    //    DSBtn->Click();
    //    SearchBtn->Click();

    
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select groups from groups.db order by groups");

        Query1->Prepare();

        if(Query1->Prepared)
        {
           Query1->Open();
           while(!Query1->Eof)
           {
                GroupCmb->Items->Add(Query1->FieldByName("Groups")->AsString);

                Query1->Next();
           }
           
        }

        GroupCmb->ItemIndex=0;

        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("SELECT seller ");
        Query1->SQL->Add("FROM seller order by seller ");
        Query1->Prepare();

        if(Query1->Prepared)
        {
           Query1->Open();
           while(!Query1->Eof)
           {
                SellerCmb->Items->Add(Query1->FieldByName("seller")->AsString);
                Query1->Next();
           }
        }
        
        SellerCmb->ItemIndex = 0;

        DataInForm->FocusControl(BarEdit);
}
//---------------------------------------------------------------------------


void TDataInForm::SearchSale(String date)
{
        //TODO: Add your source code here
                 //그리드 초기화
        
            for (int i = 1; i<= ListSG->RowCount-1; i++)
                for (int j = 0; j <= ListSG->ColCount-1; j++)
                        ListSG->Cells[j][i] = "";
             ListSG->RowCount = 2;

        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select a.barcord,a.goods,a.std,c.groups,a.stock,a.price,");
        Query1->SQL->Add("b.seller,b.buyprice,b.buying,b.indate ");
        Query1->SQL->Add("from Goodslist a,buylist b,groups c ");
        Query1->SQL->Add("where a.barcord = b.barcord and a.part = c.numb and ");

        if(IbSUnit->ItemIndex == 2)
                Query1->SQL->Add(" b.indate = :indate ");
        if(IbSUnit->ItemIndex == 1)
                Query1->SQL->Add(" substring(b.indate from 1 for 7) = substring(:indate from 1 for 7) ");
        if(IbSUnit->ItemIndex == 0)
                Query1->SQL->Add(" substring(b.indate from 1 for 4) = substring(:indate from 1 for 4) ");


        if(SEdit->Text != "")
                Query1->SQL->Add("and (a.goods like '%"+SEdit->Text+"%' or a.barcord like '%"+SEdit->Text+"%')");

        Query1->SQL->Add(" order by indate  ASC, a.goods ASC");
        Query1->ParamByName("indate")->AsString= date;
        Query1->Prepare();
        if(Query1->Prepared)
                Query1->Open();

        if(Query1->RecordCount > 0)
        {
                for(int i =1;!Query1->Eof;i++)
                {
                        if(ListSG->Cells[0][ListSG->RowCount-1] != "")
                        {
                                ListSG->RowCount++;
                                ListSG->Row = ListSG->RowCount-1;
                        }
                    ListSG->Cells[0][i]= Query1->FieldByName("barcord")->AsString;
                    ListSG->Cells[1][i]= Query1->FieldByName("goods")->AsString;
                    ListSG->Cells[2][i]= Query1->FieldByName("std")->AsString;
                    ListSG->Cells[3][i]= Query1->FieldByName("groups")->AsString;
                    ListSG->Cells[4][i]= Query1->FieldByName("stock")->AsString;
                    ListSG->Cells[5][i]= Query1->FieldByName("buying")->AsString;
                    ListSG->Cells[6][i]= Query1->FieldByName("buyprice")->AsString;
                    ListSG->Cells[7][i]= Query1->FieldByName("price")->AsString;
                    ListSG->Cells[8][i]= Query1->FieldByName("seller")->AsString;
                    ListSG->Cells[9][i]= Query1->FieldByName("indate")->AsString;

                    Query1->Next();
                }



        }
       if(ListSG->RowCount>7)
                ListSG->TopRow = ListSG->RowCount-7;


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

void __fastcall TDataInForm::SearchBtnClick(TObject *Sender)
{
        DateTimePicker1->Date =DateTimePicker2->Date.DateString();
        SearchSale(DateTimePicker2->Date.DateString());
        
}
//---------------------------------------------------------------------------


void __fastcall TDataInForm::ModifyBtClick(TObject *Sender)
{
        if (BarEdit->Text=="")
        {
                ShowMessage("바코드를 입력하세요.");
        }
        else if (GoodsEdit->Text=="")
        {
                ShowMessage("상품명을 입력하세요.");
        } 
        else if (GroupCmb->Text=="")
        {
                ShowMessage("구분을 입력하세요.");
        }

        else if (SpEdit->Text=="")
        {
                ShowMessage("판매가를 입력하세요.");
        }

        else
        {


              if(ChkGoods(BarEdit->Text))
              {



                        bool chk = false;
                        for(int i=1; i < GoodsSg->RowCount ; i++)
                        {
                                if(GoodsSg->Cells[0][i] == BarEdit->Text)
                                {
                                        GoodsSg->Cells[0][i] =  BarEdit->Text;
                                        GoodsSg->Cells[1][i] =  GoodsEdit->Text;
                                        GoodsSg->Cells[2][i] =  StdEdit->Text;
                                        GoodsSg->Cells[3][i] =  GroupCmb->Text;
                                        GoodsSg->Cells[4][i] =  StockEdit->Text;
                                        GoodsSg->Cells[5][i] =  SpEdit->Text;
                                        GoodsSg->Cells[6][i] = DateP1->Date.DateString();

                                        chk = true;
                                        break;
                                }
                        }
                        if(chk == false)
                        {
                                if(GoodsSg->Cells[0][GoodsSg->RowCount-1] !="" )
                                {
                                        GoodsSg->RowCount++;
                                        GoodsSg->Row =  GoodsSg->RowCount-1;
                                }
                                GoodsSg->Cells[0][GoodsSg->RowCount-1] =  BarEdit->Text;
                                GoodsSg->Cells[1][GoodsSg->RowCount-1]=GoodsEdit->Text;
                                GoodsSg->Cells[2][GoodsSg->RowCount-1] =  StdEdit->Text;
                                GoodsSg->Cells[3][GoodsSg->RowCount-1] =  GroupCmb->Text;
                                GoodsSg->Cells[4][GoodsSg->RowCount-1] =  StockEdit->Text;
                                GoodsSg->Cells[5][GoodsSg->RowCount-1] =  SpEdit->Text;
                                GoodsSg->Cells[6][GoodsSg->RowCount-1] = DateP1->Date.DateString();
                                        
                                chk = false;


                        }

                        Query1->Close();

                        Query1->SQL->Clear();
                        Query1->SQL->Add("update goodslist.db set goods=:goods, std=:std, part=(select numb from groups where groups = '"+GroupCmb->Text+"'), stock=:stock, price=:price, indate=:indate");
                        Query1->SQL->Add("where barcord=:barcord");

                        Query1->ParamByName("barcord")->AsString=BarEdit->Text;
                        Query1->ParamByName("goods")->AsString=GoodsEdit->Text;
                        Query1->ParamByName("std")->AsString=StdEdit->Text;
                        Query1->ParamByName("stock")->AsInteger=StrToInt(StockEdit->Text);
                        Query1->ParamByName("price")->AsInteger= StrToInt(SpEdit->Text);
                        Query1->ParamByName("indate")->AsString= DateP1->Date.DateString();


                        Query1->Prepare();

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


              }
              else
              {
                ShowMessage("등록된 상품이 아닙니다.");
              }
              DataInForm->FocusControl(BarEdit);
        }




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

void __fastcall TDataInForm::IbgoBtClick(TObject *Sender)
{
        if(SearchGoods(BarEdit2->Text)==true && ManyEdit->Text != "" && BpEdit->Text != 0 && SellerCmb->Text != "")
        {
                
                if(StockEdit->Text=="")
                        StockEdit->Text = 0;
                if(SpEdit->Text == "")
                        SpEdit->Text = 0;
                if(ManyEdit->Text == "")
                        ManyEdit->Text = 0;
                if(BpEdit->Text == "")
                        BpEdit->Text = 0;

                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("update goodslist.db set stock=:stock, margin=price-:buyprice");
                Query1->SQL->Add("where barcord=:barcord");

                Query1->ParamByName("barcord")->AsString=BarEdit2->Text;
                Query1->ParamByName("stock")->AsInteger= StrToInt(ManyEdit->Text)+StrToInt(StockEdit->Text);
                Query1->ParamByName("buyprice")->AsInteger= StrToInt(BpEdit->Text);


                Query1->Prepare();

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

                Query1->SQL->Clear();
                Query1->SQL->Add("select barcord from buylist where barcord =:barcord and indate = :indate");
                Query1->ParamByName("barcord")->AsString = BarEdit2->Text;
                Query1->ParamByName("indate")->AsString= DateTimePicker1->Date.DateString();
                Query1->Prepare();

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

                if(Query1->RecordCount > 0)
                {
                        Query1->SQL->Clear();
                        Query1->SQL->Add("update buylist set buying=buying+:buying");
                        Query1->SQL->Add("where barcord=:barcord and indate = :indate");

                        Query1->ParamByName("barcord")->AsString=BarEdit2->Text;
                        Query1->ParamByName("indate")->AsString= DateTimePicker1->Date.DateString();
                        Query1->ParamByName("buying")->AsInteger= StrToInt(ManyEdit->Text);

                        Query1->Prepare();

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

                        //StringGride에 뿌려준다.

                        for(int i=1; i < ListSG->RowCount ; i++)
                        {
                                if(ListSG->Cells[0][i] == BarEdit2->Text)
                                {
                                        ListSG->Cells[4][i] = IntToStr(StrToInt(ListSG->Cells[4][i])+StrToInt(ManyEdit->Text));
                                      //  StockEdit->Text = ListSG->Cells[4][i];
                                        ListSG->Cells[5][i] = IntToStr(StrToInt(ListSG->Cells[5][i]) + StrToInt(ManyEdit->Text));

                                        break;
                                }

                        }

                        
                }
                else
                {

                        Query1->SQL->Clear();
                        Query1->SQL->Add("insert into buylist.db");
                        Query1->SQL->Add(" (barcord, indate, seller, buyprice, buying)");
                        Query1->SQL->Add(" values (:tbarcord, :indate, :seller, :buyprice,:buying)");

                        Query1->ParamByName("tbarcord")->AsString=BarEdit2->Text;
                        Query1->ParamByName("indate")->AsString= DateTimePicker1->Date.DateString();
                        Query1->ParamByName("seller")->AsString= SellerCmb->Text;
                        Query1->ParamByName("buyprice")->AsInteger= StrToInt(BpEdit->Text);
                        Query1->ParamByName("buying")->AsInteger= StrToInt(ManyEdit->Text);

                        Query1->Prepare();

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


                               //StringGride에 뿌려준다.

                        if(ListSG->Cells[0][ListSG->RowCount-1] != "")
                        {
                                ListSG->RowCount++;
                                ListSG->Row = ListSG->RowCount-1;
                        }
                        ListSG->Cells[0][ListSG->RowCount-1] = BarEdit->Text;
                        ListSG->Cells[1][ListSG->RowCount-1] = GoodsEdit->Text;
                        ListSG->Cells[2][ListSG->RowCount-1] = StdEdit->Text;
                        ListSG->Cells[3][ListSG->RowCount-1] = GroupCmb->Text;

                        ListSG->Cells[4][ListSG->RowCount-1] = IntToStr(StrToInt(StockEdit->Text)+StrToInt(ManyEdit->Text));

                        ListSG->Cells[5][ListSG->RowCount-1] = ManyEdit->Text;
                        ListSG->Cells[6][ListSG->RowCount-1] = BpEdit->Text;
                        ListSG->Cells[7][ListSG->RowCount-1] = SpEdit->Text;
                        ListSG->Cells[8][ListSG->RowCount-1] = SellerCmb->Text;
                        ListSG->Cells[9][ListSG->RowCount-1] = DateTimePicker1->Date.DateString();
                }


                if(ListSG->RowCount>7)
                ListSG->TopRow = ListSG->RowCount-7;

                
                


        }
        else
        {
                ShowMessage("모두 입력하세요..");
        }
        DataInForm->FocusControl(BarEdit2);
        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::DelBtClick(TObject *Sender)
{


                if (BarEdit->Text=="")
                {
                        ShowMessage("바코드를 입력하세요.");
                }
                else
                {

                     if(MessageDlg ("관련데이타가 모두 삭제됩니다.", mtWarning, TMsgDlgButtons () << mbOK<<mbCancel, 0)==1)
                     {
                        if(ChkGoods(BarEdit->Text))
                        {

                                Query1->Close();
                                Query1->SQL->Clear();
                                Query1->SQL->Add("delete from goodslist where barcord = :barcord");

                                Query1->ParamByName("barcord")->AsString = BarEdit->Text;

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

                                Query1->SQL->Clear();
                                Query1->SQL->Add("delete from buylist where barcord = :barcord");

                                Query1->ParamByName("barcord")->AsString = BarEdit->Text;

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

                                for(int i=1; i < GoodsSg->RowCount ; i++)
                                {
                                        if(GoodsSg->Cells[0][i] == BarEdit->Text)
                                        {
                                                GoodsSg->Rows[i]->Clear();
                                                for(int j=GoodsSg->Row; j<GoodsSg->RowCount-1; j++)
                                                {
                                                        GoodsSg->Rows[j]->Assign(GoodsSg->Rows[j+1]);
                                                }
                       // GoodsSg->Rows[GoodsSg->RowCount-1]->Clear();

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

                                        break;
                                        }

                                }
                        }
                        ShowMessage("삭제되었습니다.");
                        BarEdit2->Text = "";
                        ManyEdit->Text = "";
                        BpEdit->Text = "";
                        SellerCmb->Text = "";
                        SearchBtn->OnClick(this);

                     }
                     DataInForm->FocusControl(BarEdit);
                }


        

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

void __fastcall TDataInForm::InmodyBtClick(TObject *Sender)
{
        if(StockEdit->Text=="")
                StockEdit->Text = 0;
        if(SpEdit->Text == "")
                SpEdit->Text = 0;
        if(ManyEdit->Text == "")
                ManyEdit->Text = 0;
        if(BpEdit->Text == "")
                BpEdit->Text = 0;

        if(BarEdit2->Text != "" && SellerCmb->Text != "")
        {
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("update buylist.db set buying=:buying, buyprice=:buyprice, seller=:seller, indate=:indate2 ");
                Query1->SQL->Add("where barcord=:barcord and indate = :indate");

                Query1->ParamByName("barcord")->AsString = BarEdit2->Text;
                Query1->ParamByName("buying")->AsInteger= StrToInt(ManyEdit->Text);
                Query1->ParamByName("buyprice")->AsInteger= StrToInt(BpEdit->Text);
                Query1->ParamByName("seller")->AsString = SellerCmb->Text;
                Query1->ParamByName("indate")->AsString = oldDate;
                Query1->ParamByName("indate2")->AsString = DateTimePicker1->Date.DateString();

                Query1->Prepare();

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




                for(int i=1; i < ListSG->RowCount ; i++)
                {
                        if(ListSG->Cells[0][i] == BarEdit2->Text)
                        {
                                ListSG->Cells[4][i] = IntToStr((StrToInt(ListSG->Cells[4][i])-StrToInt(ListSG->Cells[5][i]))+StrToInt(ManyEdit->Text));
                                StockEdit->Text = ListSG->Cells[5][i];
                                ListSG->Cells[5][i] = ManyEdit->Text;
                                ListSG->Cells[6][i] = BpEdit->Text;
                                ListSG->Cells[8][i] = SellerCmb->Text;
                                ListSG->Cells[9][i] = DateTimePicker1->Date.DateString();

                                Query1->SQL->Clear();
                                Query1->SQL->Add("update goodslist set stock =:stock, margin=price-:buyprice where barcord=:barcord");
                                Query1->ParamByName("barcord")->AsString = BarEdit2->Text;
                                Query1->ParamByName("stock")->AsInteger= StrToInt(ListSG->Cells[5][i]);
                                Query1->ParamByName("buyprice")->AsInteger= StrToInt(BpEdit->Text);
                                
                                Query1->Prepare();

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

                         break;
                        }

                }



        }
        DataInForm->FocusControl(BarEdit2);
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::IndelBtClick(TObject *Sender)
{
                if(MessageDlg ("당일 입고가 삭제됩니다.", mtWarning, TMsgDlgButtons () << mbOK<<mbCancel, 0)==1)
                {

                        if(BarEdit->Text !="")
                        {
                                Query1->Close();
                                Query1->SQL->Clear();
                                Query1->SQL->Add("delete from buylist where barcord = :barcord and indate = :indate");

                                Query1->ParamByName("barcord")->AsString = BarEdit->Text;
                                Query1->ParamByName("indate")->AsString = DateTimePicker1->Date.DateString();

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


                                Query1->SQL->Clear();
                                Query1->SQL->Add("update goodslist set stock = stock - (select buying from buylist where barcord=:barcord) where barcord=:barcord");
                                Query1->ParamByName("barcord")->AsString = BarEdit2->Text;
                
                                Query1->Prepare();

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

                                for(int i=1; i < ListSG->RowCount ; i++)
                                {
                                        if(ListSG->Cells[0][i] == BarEdit2->Text)
                                        {
                                                ListSG->Rows[i]->Clear();
                                                for(int j=ListSG->Row; j<ListSG->RowCount-1; j++)
                                                {
                                                        ListSG->Rows[j]->Assign(ListSG->Rows[j+1]);
                                                }
                                                if(ListSG->RowCount >2)
                                                        ListSG->RowCount--;

                                        }

                                }
                        }
                }
                DataInForm->FocusControl(BarEdit2);
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::GoodsSgKeyPress(TObject *Sender, char &Key)
{
        if( Key == 0x0D && GoodsSg->Cells[0][GoodsSg->Row] != "" )//enter key
        {

                row = GoodsSg->Row;
                Query1->Close();
                Query1->SQL->Clear();
                Query1->SQL->Add("select * from goodslist where barcord = :barcord");
                Query1->ParamByName("barcord")->AsString = GoodsSg->Cells[0][row];
                Query1->Prepare();

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


                BarEdit->Text = GoodsSg->Cells[0][row];
                BarEdit2->Text = GoodsSg->Cells[0][row];
                GoodsEdit->Text = Query1->FieldByName("goods")->AsString;


                StdEdit->Text = Query1->FieldByName("std")->AsString;
                GroupCmb->Text = GoodsSg->Cells[3][row];
               // MakerEdit->Text = GoodsSg->Cells[4][row];
                StockEdit->Text = Query1->FieldByName("stock")->AsString;
                SpEdit->Text = Query1->FieldByName("price")->AsString;
                DateP1->Date = Query1->FieldByName("indate")->AsString;

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

void __fastcall TDataInForm::NewBtClick(TObject *Sender)
{

        BarEdit->Text = "";
        GoodsEdit->Text = "";
        StdEdit->Text = "";
        StockEdit->Text = "0";
        SpEdit->Text = "0";

        BarEdit2->Text = "";
        ManyEdit->Text = "0";
        BpEdit->Text = "0";
        bugaRG->ItemIndex = 0;
        JukribRG->ItemIndex = 0;
      // BarEdit->Text = MessageDlg ("연습삼아 테스트", mtInformation, TMsgDlgButtons () << mbOK<<mbCancel, 0);
        DateP1->Date = Date();

        DataInForm->FocusControl(BarEdit);

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


bool TDataInForm::ChkGoods(String st)
{
        //TODO: Add your source code here
        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select barcord from goodslist where barcord like '%"+st+"' or goods like '%"+ st +"%'");
        Query1->Prepare();
        if(Query1->Prepared)
                Query1->Open();

        if(Query1->RecordCount > 0)
                return true;
        else
                return false;
}


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


void __fastcall TDataInForm::BarEditKeyPress(TObject *Sender, char &Key)
{
   if( Key == 0x0D && BarEdit->Text != "")
  {

        BarEdit2->Text="";
        ManyEdit->Text = 0;
        BpEdit->Text = 0;
        if(!SearchGoods(BarEdit->Text))
        {
                ShowMessage("등록되지 않은 상품입니다.");
                DateP1->Date = Date();
        }
        BarEdit->SelectAll();

   }
   if(Key==0x1b)
   {
        DateP1->Date = Date();
        DateP2->Date = Date();
        DateTimePicker1->Date = Date();
        DateTimePicker2->Date = Date();
        BarEdit->Text = "";
        GoodsEdit->Text = "";
        StdEdit->Text = "";
        StockEdit->Text = 0;
        SpEdit->Text = 0;
   }
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::BarEditChange(TObject *Sender)
{
        Label5->Caption = "["+IntToStr(BarEdit->Text.Length()) + "자리]";        
}
//---------------------------------------------------------------------------


void __fastcall TDataInForm::GoodsSgDblClick(TObject *Sender)
{
        GoodsSg->OnKeyPress(this,0x0D);        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::ListSGDblClick(TObject *Sender)
{
        ListSG->OnKeyPress(this,0x0D);        
}
//---------------------------------------------------------------------------


void __fastcall TDataInForm::BarMakerKeyPress(TObject *Sender,
      char &Key)
{
        if(Key==0x0D)
        {
                if(BarMaker->Text.Length()==12)
                {
                        int hol=0,zak=0;
                        for(int i=1;i<13;i++)
                        {
                              if(i%2 > 0)
                                hol+=StrToInt(BarMaker->Text.SubString(i,1));
                              else
                                zak+=StrToInt(BarMaker->Text.SubString(i,1));
                        }
                        zak=zak*3;
                        hol=hol+zak;
                        if(hol%10 > 0)
                                BarEdit->Text = BarMaker->Text+(10-(hol%10));//10의배수를 만들기 위한 숫자
                        else
                                BarEdit->Text = BarMaker->Text+ 0;
                }
                else
                {
                        ShowMessage("12자리숫자를 입력하세요");
                }
        }
}
//---------------------------------------------------------------------------


bool TDataInForm::BarChk(String st)
{
        //TODO: Add your source code here
        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;
}
//---------------------------------------------------------------------------
void __fastcall TDataInForm::DSBtnClick(TObject *Sender)
{
        DateP1->Date =DateP2->Date.DateString();
        SearchIn(DateP2->Date.DateString());
       
}
//---------------------------------------------------------------------------


void TDataInForm::SearchIn(String date)
{
        //TODO: Add your source code here


            for (int i = 1; i<= GoodsSg->RowCount-1; i++)
                for (int j = 0; j <= GoodsSg->ColCount-1; j++)
                        GoodsSg->Cells[j][i] = "";
             GoodsSg->RowCount = 2;

        Query1->Close();
        Query1->SQL->Clear();
        Query1->SQL->Add("select a.barcord,a.goods,a.std,c.groups,a.stock,a.price, a.indate");

        Query1->SQL->Add("from Goodslist a, groups c ");
        Query1->SQL->Add("where a.part = c.numb and ");

        if(GSUnit->ItemIndex == 2)
                Query1->SQL->Add(" a.indate = :indate");
        if(GSUnit->ItemIndex == 1)
                Query1->SQL->Add(" substring(a.indate from 1 for 7) = substring(:indate from 1 for 7)");
        if(GSUnit->ItemIndex == 0)
                Query1->SQL->Add(" substring(a.indate from 1 for 4) = substring(:indate from 1 for 4)");

        if(DEdit->Text != "")
                 Query1->SQL->Add(" and (a.goods like '%"+DEdit->Text+"%' or a.barcord like '%"+DEdit->Text+"%')");
                

        Query1->SQL->Add(" order by indate  ASC, a.goods ASC");
        Query1->ParamByName("indate")->AsString= date;

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

        if(Query1->RecordCount > 0)
        {
                for(int i =1;!Query1->Eof;i++)
                {
                        if(GoodsSg->Cells[0][GoodsSg->RowCount-1] != "")
                        {
                                GoodsSg->RowCount++;
                                GoodsSg->Row = GoodsSg->RowCount-1;
                        }
                    GoodsSg->Cells[0][i]= Query1->FieldByName("barcord")->AsString;
                    GoodsSg->Cells[1][i]= Query1->FieldByName("goods")->AsString;
                    GoodsSg->Cells[2][i]= Query1->FieldByName("std")->AsString;
                    GoodsSg->Cells[3][i]= Query1->FieldByName("groups")->AsString; 
                    GoodsSg->Cells[4][i]= Query1->FieldByName("stock")->AsString;
                    GoodsSg->Cells[5][i]= Query1->FieldByName("price")->AsString;
                    GoodsSg->Cells[6][i]= Query1->FieldByName("indate")->AsString;

                    Query1->Next();
                }



        }
       if(GoodsSg->RowCount>7)
                GoodsSg->TopRow = GoodsSg->RowCount-7;
        
}
//---------------------------------------------------------------------------
void __fastcall TDataInForm::DEditKeyPress(TObject *Sender, char &Key)
{
        if(Key==0x0D)
        {
                DSBtn->Click();
        }
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::SEditKeyPress(TObject *Sender, char &Key)
{
        if(Key==0x0D)
        {
                SearchBtn->Click();
        }
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::GoodsEditKeyPress(TObject *Sender, char &Key)
{
   if( Key == 0x0D && GoodsEdit->Text != "")
  {

        BarEdit2->Text="";
        ManyEdit->Text = 0;
        BpEdit->Text = 0;
        if(!SearchGoods(GoodsEdit->Text))
        {
                ShowMessage("등록되지 않은 상품입니다.");
                DateP1->Date = Date();
        }
        GoodsEdit->SelectAll();

   }
      if(Key==0x1b)
   {
        DateP1->Date = Date();
        DateP2->Date = Date();
        DateTimePicker1->Date = Date();
        DateTimePicker2->Date = Date();
        BarEdit->Text = "";
        GoodsEdit->Text = "";
        StdEdit->Text = "";
        StockEdit->Text = 0;
        SpEdit->Text = 0;
   }
}
//---------------------------------------------------------------------------


void __fastcall TDataInForm::BarEdit2Change(TObject *Sender)
{
        Label9->Caption = "["+IntToStr(BarEdit2->Text.Length()) + "자리]";        
}
//---------------------------------------------------------------------------


void __fastcall TDataInForm::BarEditKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::BarEdit2KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x21)//pageup 누르면 위로
        {
                DataInForm->FocusControl(BarEdit);
        }
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::GoodsEditKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }         
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::StdEditKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }         
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::GroupCmbKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }         
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::StockEditKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }         
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::SpEditKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::ManyEditKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x21)//pageup 누르면 위로
        {
                DataInForm->FocusControl(BarEdit);
        }         
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::BpEditKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x21)//pageup 누르면 위로
        {
                DataInForm->FocusControl(BarEdit);
        }         
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::SellerCmbKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x21)//pageup 누르면 위로
        {
                DataInForm->FocusControl(BarEdit);
        }         
}
//---------------------------------------------------------------------------


void __fastcall TDataInForm::NewBtExit(TObject *Sender)
{


                DataInForm->FocusControl(BarEdit);

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

void __fastcall TDataInForm::IndelBtExit(TObject *Sender)
{
        DataInForm->FocusControl(BarEdit2);        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::SaveBtKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::ModifyBtKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::DelBtKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::NewBtKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x22)//pagedown 누르면 아래로
        {
                DataInForm->FocusControl(ManyEdit);
        }        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::IbgoBtKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x21)//pageup 누르면 위로
        {
                DataInForm->FocusControl(BarEdit);
        }        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::InmodyBtKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x21)//pageup 누르면 위로
        {
                DataInForm->FocusControl(BarEdit);
        }        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::IndelBtKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if(Key==0x21)//pageup 누르면 위로
        {
                DataInForm->FocusControl(BarEdit);
        }        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::bugaRGClick(TObject *Sender)
{
        if(bugaRG->ItemIndex==0 && GoodsEdit->Text.SubString(0,1)=="*")
                GoodsEdit->Text=GoodsEdit->Text.SubString(2,GoodsEdit->Text.Length());
        else if(bugaRG->ItemIndex==1 && GoodsEdit->Text.SubString(0,1)!="*")
                GoodsEdit->Text="*"+GoodsEdit->Text;
        else
                GoodsEdit->Text=GoodsEdit->Text;
                                       
                                        
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::JukribRGClick(TObject *Sender)
{
        if(JukribRG->ItemIndex==0 && GoodsEdit->Text.SubString(GoodsEdit->Text.Length(),1)=="@")
                GoodsEdit->Text=GoodsEdit->Text.SubString(0,GoodsEdit->Text.Length()-1);
        else if(JukribRG->ItemIndex==1 && GoodsEdit->Text.SubString(GoodsEdit->Text.Length(),1)!="@")
                GoodsEdit->Text=GoodsEdit->Text+"@";
        else
                GoodsEdit->Text=GoodsEdit->Text;
}
//---------------------------------------------------------------------------

void __fastcall TDataInForm::GoodsEditChange(TObject *Sender)
{
        if(GoodsEdit->Text.SubString(0,1)=="*")
                bugaRG->ItemIndex = 1;
        else
                bugaRG->ItemIndex = 0;

        if(GoodsEdit->Text.SubString(GoodsEdit->Text.Length(),1)=="@")
                JukribRG->ItemIndex = 1;
        else
                JukribRG->ItemIndex = 0;
}
//---------------------------------------------------------------------------



