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

#pragma hdrstop

#include "CDSCompanyF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TItsCompany::TItsCompany()
{
	FGridIndex  = -1;         // 그리드 인덱스
    //Clear();
}
//---------------------------------------------------------------------------
void TItsCompany::Clear()
{
    CMPY_ID      = "";
    CMPY_NM      = "";
    BRNO         = "";
    ADDR         = "";
    TEL          = "";
    FCSM_NMBR    = "";
    RPRV_NM      = "";
    CHGR_NM      = "";
    CHGR_HP_NMBR = "";
    CHGR_EMAL    = "";
    ADTN_NM      = "";
    ADTN_DT      = "";
    CHNG_NM      = "";
    CHNG_DT      = "";
    DEL_YN       = "";
	GridIndex    = -1;
}
//---------------------------------------------------------------------------
TItsCompany::~TItsCompany()
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
TItsCompanyManager *ItsCompanyManager = NULL;
//---------------------------------------------------------------------------

/*
* Company Manager
*/
TItsCompanyManager::TItsCompanyManager()
{
}
//---------------------------------------------------------------------------
TItsCompanyManager::~TItsCompanyManager()
{
}
//---------------------------------------------------------------------------
bool TItsCompanyManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT A.CMPY_ID      , \r\n"
           "       A.CMPY_NM      , \r\n"
           "       A.BRNO         , \r\n"
           "       A.ADDR         , \r\n"
           "       A.TEL          , \r\n"
           "       A.FCSM_NMBR    , \r\n"
           "       A.RPRV_NM      , \r\n"
           "       A.CHGR_NM      , \r\n"
           "       A.CHGR_HP_NMBR , \r\n"
           "       A.CHGR_EMAL    , \r\n"
           "       A.ADTN_NM      , \r\n"
           "       A.ADTN_DT      , \r\n"
           "       A.CHNG_NM      , \r\n"
           "       A.CHNG_DT      , \r\n"
           "       A.DEL_YN         \r\n"
           "  FROM TB_FCLT_CMPY A   \r\n";
//           " ORDER BY A.CMPY_ID     \r\n";
    try
    {
        FLists.Lock();
        FLists.RemoveAll();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

            pADO->Prepared = true;
			pADO->Open();
            for( ; !pADO->Eof; pADO->Next())
            {
				TItsCompany *pCompany = new TItsCompany();

                pCompany->CMPY_ID      = pADO->FieldByName("CMPY_ID")->AsString;        //업체 ID
                pCompany->CMPY_NM      = pADO->FieldByName("CMPY_NM")->AsString;        //업체 명
                pCompany->BRNO         = pADO->FieldByName("BRNO")->AsString;           //사업자 등록 번호
                pCompany->ADDR         = pADO->FieldByName("ADDR")->AsString;           //주소
                pCompany->TEL          = pADO->FieldByName("TEL")->AsString;            //전화번호
                pCompany->FCSM_NMBR    = pADO->FieldByName("FCSM_NMBR")->AsString;      //팩스 번호
                pCompany->RPRV_NM      = pADO->FieldByName("RPRV_NM")->AsString;        //대표자 명
                pCompany->CHGR_NM      = pADO->FieldByName("CHGR_NM")->AsString;        //담당자 명
                pCompany->CHGR_HP_NMBR = pADO->FieldByName("CHGR_HP_NMBR")->AsString;   //담당자 연락처
                pCompany->CHGR_EMAL    = pADO->FieldByName("CHGR_EMAL")->AsString;      //담당자 이메일
                pCompany->ADTN_NM      = pADO->FieldByName("ADTN_NM")->AsString;        //추가자 명
                pCompany->ADTN_DT      = pADO->FieldByName("ADTN_DT")->AsString;        //추가 일시
                pCompany->CHNG_NM      = pADO->FieldByName("CHNG_NM")->AsString;        //변경자 명
                pCompany->CHNG_DT      = pADO->FieldByName("CHNG_DT")->AsString;        //변경 일시
                pCompany->DEL_YN       = pADO->FieldByName("DEL_YN")->AsString;         //삭제 여부

				FLists.Push(pCompany->CMPY_ID, pCompany);
                pCompany->Completed = true;
            }
        }
        catch(EDatabaseError &E)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsCompanyManager::LoadFromDb", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            DBERRORMSG("TItsCompanyManager::LoadFromDb", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        FLists.UnLock();
    }

    return true;
}
//---------------------------------------------------------------------------
