// ContractsDlg.cpp : implementation file // #include "stdafx.h" #include "sp.h" #include "ContractsDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CContractsDlg dialog CContractsDlg::CContractsDlg(CWnd* pParent /*=NULL*/) : CDialog(CContractsDlg::IDD, pParent) { //{{AFX_DATA_INIT(CContractsDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pApp= (CSpApp*)AfxGetApp(); //Initialize recordset pointer rs=m_pApp->rsPool->GetRS("CContractsDlg"); } CContractsDlg::~CContractsDlg() { m_pApp->rsPool->ReleaseRS(&rs->m_nID); } void CContractsDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CContractsDlg) DDX_Control(pDX, IDC_CONTRACT_LIST_LABEL, m_lblContractList); DDX_Control(pDX, IDC_CONTRACT_DELETE, m_btnContractDelete); DDX_Control(pDX, IDC_CONTRACT_ADD, m_btnContractAdd); DDX_Control(pDX, IDC_CONTRACTTERMS, m_edContractTerms); DDX_Control(pDX, IDC_CONTRACTNAME, m_edContractName); DDX_Control(pDX, IDC_CONTRACT_COMBO, m_cbContracts); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CContractsDlg, CDialog) //{{AFX_MSG_MAP(CContractsDlg) ON_BN_CLICKED(IDC_CONTRACT_DELETE, OnContractDelete) ON_BN_CLICKED(IDC_CONTRACT_ADD, OnContractAdd) ON_CBN_CLOSEUP(IDC_CONTRACT_COMBO, OnCloseupContractCombo) ON_EN_KILLFOCUS(IDC_CONTRACTNAME, OnKillfocusContractname) ON_EN_KILLFOCUS(IDC_CONTRACTTERMS, OnKillfocusContractterms) //}}AFX_MSG_MAP END_MESSAGE_MAP() //************************************************************ //SAVE EDIT CONTROL FIELD //************************************************************ bool CContractsDlg::SaveField(CEdit *edControl,CString fldname,bool AllowEmpty) { if(m_bAddMode) return true;//dont attempt to update CString str; //do nothing if not changed if(edControl->GetModify()!=TRUE) return true; edControl->GetWindowText(str); //dont save empty fields if not allowed to if(!AllowEmpty) { if(str.IsEmpty()) { edControl->Undo(); return false; } } rs->UpdateField(fldname,&str); rs->SaveRecord(); return true; } bool CContractsDlg::FillContractsList() { CString strData; CString strIndex; long lData; m_cbContracts.Clear(); rs->Query("SELECT contracts.* FROM contracts ORDER BY contracts.name;"); if(rs->IsEmpty()) { EnableFields(false); return false; } else { EnableFields(true); //fill combo box with available zones rs->MoveFirst(); rs->FetchField("name",&strData); rs->FetchField("id",&lData); strIndex.Format("%u",lData); m_cbContracts.AddRow(strData,strIndex); while(rs->MoveForward()) { rs->FetchField("name",&strData); rs->FetchField("id",&lData); strIndex.Format("%u",lData); m_cbContracts.AddRow(strData,strIndex); } } //pretend user has selected so that other fields get filled in if(m_strSelectedContract.IsEmpty()) //first time in { m_cbContracts.SetCurSel(0); } else//something valid was selected before so stick with it m_cbContracts.Select(m_strSelectedContract); OnCloseupContractCombo(); return true; } ///////////////////////////////////////////////////////////////////////////// // CContractsDlg message handlers void CContractsDlg::OnContractDelete() { CString q,strData; q.Format("SELECT * FROM clients WHERE (contract = %s );",m_cbContracts.GetCurrentRowID()); rs->Query(q); if(rs->IsEmpty())//no clients in that zone { q.Format("DELETE contracts.*, contracts.id FROM contracts WHERE (((contracts.id)=%s));",m_cbContracts.GetCurrentRowID()); if(AfxMessageBox("Are you sure?",MB_YESNO)==IDYES) { rs->Ex(q); //a deleted zone can't be selected m_strSelectedContract=""; } } else { q="DATA INTEGRITY PROTECTION:\r\nYou cannot delete this contract because\r\nthe following clients are set to use it:\r\n"; //fill combo box with available zones rs->MoveFirst(); rs->FetchField("company",&strData); q=q+strData+"\r\n"; while(rs->MoveForward()) { rs->FetchField("company",&strData); q=q+strData+"\r\n"; } AfxMessageBox(q); } FillContractsList(); } //******************************************************************* void CContractsDlg::OnContractAdd() { CString q,name,info; if(m_bAddMode) { m_bAddMode=false;//saving: m_cbContracts.ShowWindow(TRUE); m_lblContractList.ShowWindow(TRUE); m_btnContractAdd.SetWindowText("Add"); m_btnContractDelete.ShowWindow(TRUE); m_edContractTerms.GetWindowText(info); m_edContractName.GetWindowText(name); if(name.IsEmpty()) { AfxMessageBox("You must enter a contract name"); } else { q.Format("INSERT INTO contracts ( name, terms ) SELECT \"%s\", \"%s\";",name,info); rs->Ex(q); rs->Close(); //rs->AddNewRecord(); //rs->UpdateField("name",&name); //rs->UpdateField("zoneinfo",&info); } FillContractsList(); } else//Go into add mode { EnableFields(true); m_cbContracts.ShowWindow(FALSE); m_lblContractList.ShowWindow(FALSE); m_btnContractAdd.SetWindowText("SAVE"); m_btnContractDelete.ShowWindow(FALSE); m_edContractTerms.SetWindowText(""); m_edContractName.SetWindowText(""); m_edContractName.SetFocus(); m_bAddMode=true; } } //************************************************8 void CContractsDlg::OnCloseupContractCombo() { //user has made a selection, update the visible list CString q; CString strData; q.Format("SELECT contracts.* FROM contracts WHERE (((contracts.id)=%s));",m_cbContracts.GetCurrentRowID()); rs->Query(q); if(!rs->IsEmpty()) { rs->FetchField("name",&strData); m_edContractName.SetWindowText(strData); rs->FetchField("terms",&strData); m_edContractTerms.SetWindowText(strData); } //save current selection so that updates and changes //will still show what was last selected m_strSelectedContract=m_cbContracts.GetCurrentRowID(); } //****************************************************** BOOL CContractsDlg::OnInitDialog() { CDialog::OnInitDialog(); m_edContractName.SetLimitText(25); FillContractsList(); m_bAddMode=false; return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CContractsDlg::EnableFields(bool bEnable) { BOOL enable= bEnable ? TRUE : FALSE; m_btnContractDelete.EnableWindow(enable); m_cbContracts.EnableWindow(enable); m_edContractName.EnableWindow(enable); m_edContractTerms.EnableWindow(enable); } void CContractsDlg::OnKillfocusContractname() { SaveField(&m_edContractName,"name",false); } void CContractsDlg::OnKillfocusContractterms() { SaveField(&m_edContractTerms,"terms",false); }