Files

678 lines
16 KiB
C++

// NonClientsDlg.cpp : implementation file
//
#include "stdafx.h"
#include "sp.h"
#include "NonClientsDlg.h"
#include "StatusDlg.h"
#include ".\nonclientsdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CNonClientsDlg dialog
CNonClientsDlg::CNonClientsDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNonClientsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNonClientsDlg)
//}}AFX_DATA_INIT
m_pstrReturnValue=NULL;
m_strSelectedContact.Empty();
m_lListType=0;//not specified
m_pApp= (CSpApp*)AfxGetApp();
/*
rs=new GZRset("Non-client entry screen");
rs->SetConnect(m_pApp->strConnectString);
*/
//Initialize recordset pointer
rs=m_pApp->rsPool->GetRS("CNonClientsDlg");
//v1.9.4.4
rs2=m_pApp->rsPool->GetRS("CNonClientsDlgRS2");
}
//destructor added by me later!
CNonClientsDlg::~CNonClientsDlg()
{
m_pApp->rsPool->ReleaseRS(&rs->m_nID);
//v1.9.4.4
m_pApp->rsPool->ReleaseRS(&rs2->m_nID);
}
void CNonClientsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNonClientsDlg)
DDX_Control(pDX, IDC_EDACCT, m_edAccount);
DDX_Control(pDX, IDC_DONE, m_btnDone);
DDX_Control(pDX, IDC_HISTORY, m_lblHistory);
DDX_Control(pDX, IDC_LBLLIST, m_lblList);
DDX_Control(pDX, IDC_LBLWEBSITE, m_lblWebsite);
DDX_Control(pDX, IDC_LBLEMAIL, m_lblEmail);
DDX_Control(pDX, IDC_WEBSITE, m_edWebsite);
DDX_Control(pDX, IDC_PHONE, m_edPhone);
DDX_Control(pDX, IDC_NOTES, m_edNotes);
DDX_Control(pDX, IDC_LIST, m_cbList);
DDX_Control(pDX, IDC_FAX, m_edFax);
DDX_Control(pDX, IDC_EMAIL, m_edEmail);
DDX_Control(pDX, IDC_DELETE, m_btnDelete);
DDX_Control(pDX, IDC_CONTACT, m_edContact);
DDX_Control(pDX, IDC_COMPNAME, m_edCompName);
DDX_Control(pDX, IDC_ADDRESS, m_edAddress);
DDX_Control(pDX, IDC_ADD, m_btnAdd);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNonClientsDlg, CDialog)
//{{AFX_MSG_MAP(CNonClientsDlg)
ON_CBN_CLOSEUP(IDC_LIST, OnCloseupList)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_EN_KILLFOCUS(IDC_ADDRESS, OnKillfocusAddress)
ON_EN_KILLFOCUS(IDC_COMPNAME, OnKillfocusCompname)
ON_EN_KILLFOCUS(IDC_CONTACT, OnKillfocusContact)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_EN_KILLFOCUS(IDC_EMAIL, OnKillfocusEmail)
ON_EN_KILLFOCUS(IDC_FAX, OnKillfocusFax)
ON_EN_KILLFOCUS(IDC_NOTES, OnKillfocusNotes)
ON_EN_KILLFOCUS(IDC_PHONE, OnKillfocusPhone)
ON_EN_KILLFOCUS(IDC_WEBSITE, OnKillfocusWebsite)
ON_BN_CLICKED(IDC_LBLEMAIL, OnLblemail)
ON_BN_CLICKED(IDC_LBLWEBSITE, OnLblwebsite)
ON_BN_CLICKED(IDC_DONE, OnDone)
ON_BN_CLICKED(IDOK, OnOk)
ON_BN_CLICKED(IDC_HISTORY, OnHistory)
ON_EN_KILLFOCUS(IDC_EDACCT, OnKillfocusEdacct)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNonClientsDlg message handlers
BOOL CNonClientsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
Security();
ASSERT(m_lListType!=0);//wasn't set
//make a WEBSITE "hyperlink"
m_lblWebsite.SetTextColor(RGB(0,0,255));
m_lblWebsite.SetFontUnderline(TRUE);
m_lblWebsite.SetLink(TRUE);
m_lblWebsite.SetLinkCursor(m_pApp->hcHand);
m_lblWebsite.SetLinkURL("");
//make a EMAIL "hyperlink"
m_lblEmail.SetTextColor(RGB(0,0,255));
m_lblEmail.SetFontUnderline(TRUE);
m_lblEmail.SetLink(TRUE);
m_lblEmail.SetLinkCursor(m_pApp->hcHand);
m_lblEmail.SetLinkURL("");
//make a HISTORY "hyperlink"
m_lblHistory.SetTextColor(RGB(0,0,255));
m_lblHistory.SetFontUnderline(TRUE);
m_lblHistory.SetLink(TRUE);
m_lblHistory.SetLinkCursor(m_pApp->hcHand);
m_lblHistory.SetLinkURL("");
FillList();
m_bAddMode=false;
//looks wierd, but may be ok if more fields
//added later so left in for that reason
//ShowWindow(SW_SHOWMAXIMIZED);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//************************************************************
//SAVE EDIT CONTROL FIELD
//************************************************************
bool CNonClientsDlg::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();
UpdateModified(false);
return true;
}
//************************************************
//UPDATE MODIFIED DATA
//*************************************************
bool CNonClientsDlg::UpdateModified(bool IsNew)
{
COleDateTime dtData;
//updates modified by and modified on fields
if(IsNew)//update creator and created as well
{
rs->UpdateField("creator",&m_pApp->m_lusrID);
dtData=COleDateTime::GetCurrentTime();
rs->UpdateField("created",&dtData);
}
rs->UpdateField("modifier",&m_pApp->m_lusrID);
dtData=COleDateTime::GetCurrentTime();
rs->UpdateField("modified",&dtData);
if(!rs->SaveRecord()) return false;
return true;
}
void CNonClientsDlg::FillList()
{
CString strData;
CString strIndex;
CString q;
long lData;
m_cbList.Clear();
q.Format("SELECT nonclients.*, nonclienttypes.description AS LISTNAME "
"FROM nonclients LEFT JOIN nonclienttypes ON "
"nonclients.type = nonclienttypes.id "
"WHERE (((nonclients.type)=%u)) "
"ORDER BY nonclients.company_person;",m_lListType);
rs->Query(q);
if(!rs->IsEmpty())
{
//fill combo box with available zones
rs->MoveFirst();
rs->FetchField("LISTNAME",&strData);
SetWindowText(strData);
rs->FetchField("company_person",&strData);
rs->FetchField("id",&lData);
strIndex.Format("%u",lData);
m_cbList.AddRow(strData,strIndex);
while(rs->MoveForward())
{
rs->FetchField("company_person",&strData);
rs->FetchField("id",&lData);
strIndex.Format("%u",lData);
m_cbList.AddRow(strData,strIndex);
}
//pretend user has selected so that other fields get filled in
if(m_strSelectedContact.IsEmpty()) //first time in
{
m_cbList.SetCurSel(0);
}
else//something valid was selected before so stick with it
m_cbList.Select(m_strSelectedContact);
OnCloseupList();
}
else
DisableAll(true);
}
//*************************************************
void CNonClientsDlg::SetType(long *pType)
{
m_lListType=*pType;
}
//*****************************
void CNonClientsDlg::OnCloseupList()
{
//Fill the fields as this is a simple dialog.
//user has made a selection, update the visible list
CString q;
CString strData;
strData=m_cbList.GetCurrentRowID();
if(strData.IsEmpty())
return;
q.Format("SELECT nonclients.* FROM nonclients "
"WHERE (((nonclients.id)=%s));",m_cbList.GetCurrentRowID());
rs->Query(q);
if(!rs->IsEmpty())
{
//COMPANY_PERSON
rs->FetchField("company_person",&strData);
m_edCompName.SetWindowText(strData);
//CONTACT
rs->FetchField("contact",&strData);
m_edContact.SetWindowText(strData);
//ADDRESS
rs->FetchField("address",&strData);
m_edAddress.SetWindowText(strData);
//PHONE
rs->FetchField("phone",&strData);
m_edPhone.SetWindowText(strData);
//FAX
rs->FetchField("fax",&strData);
m_edFax.SetWindowText(strData);
//EMAIL
rs->FetchField("email",&strData);
m_edEmail.SetWindowText(strData);
//WEBSITE
rs->FetchField("website",&strData);
m_edWebsite.SetWindowText(strData);
//ACCOUNT
rs->FetchField("account",&strData);
m_edAccount.SetWindowText(strData);
//NOTES
rs->FetchField("notes",&strData);
m_edNotes.SetWindowText(strData);
}
//save current selection so that updates and changes
//will still show what was last selected
m_strSelectedContact=m_cbList.GetCurrentRowID();
}
//************************************
//ADD NEW / SAVE
//************************************
void CNonClientsDlg::OnAdd()
{
CString strData;
if(!m_bAddMode)
{//go to add mode
DisableAll(false);
m_bAddMode=true;
m_btnDone.ShowWindow(FALSE);
m_btnAdd.SetWindowText("Save");
m_btnDelete.SetWindowText("Cancel");
m_lblList.ShowWindow(FALSE);
m_cbList.ShowWindow(FALSE);
m_edAddress.SetWindowText("");
m_edCompName.SetWindowText("");
m_edContact.SetWindowText("");
m_edEmail.SetWindowText("");
m_edFax.SetWindowText("");
m_edNotes.SetWindowText("");
m_edPhone.SetWindowText("");
m_edWebsite.SetWindowText("");
m_edAccount.SetWindowText("");
}
else//save and exit add mode
{
m_edCompName.GetWindowText(strData);
if(strData.IsEmpty())
{
AfxMessageBox("Company or Name is required.");
m_edCompName.Undo();
m_edCompName.SetFocus();
return;
}
if(IsDupeCompany(strData))
{
CString st;
st.Format(
"The company name \"%s\" has already been entered\r\n"
"as a non-client contact and can not be used again.",strData);
AfxMessageBox(st);
m_edCompName.Undo();
m_edCompName.SetFocus();
return;
}
//add the record
if(rs->AddNewRecord())
{
rs->UpdateField("type",&m_lListType);
rs->UpdateField("company_person",&strData);
m_edAddress.GetWindowText(strData);
rs->UpdateField("address",&strData);
m_edContact.GetWindowText(strData);
rs->UpdateField("contact",&strData);
m_edEmail.GetWindowText(strData);
rs->UpdateField("email",&strData);
m_edFax.GetWindowText(strData);
rs->UpdateField("fax",&strData);
m_edNotes.GetWindowText(strData);
rs->UpdateField("notes",&strData);
m_edPhone.GetWindowText(strData);
rs->UpdateField("phone",&strData);
m_edWebsite.GetWindowText(strData);
rs->UpdateField("website",&strData);
m_edAccount.GetWindowText(strData);
rs->UpdateField("account",&strData);
//this will save the record as well
if(!UpdateModified(true))
return;
m_bAddMode=false;
m_btnDone.ShowWindow(TRUE);
m_btnAdd.SetWindowText("Add");
m_btnDelete.SetWindowText("Delete");
m_lblList.ShowWindow(TRUE);
m_cbList.ShowWindow(TRUE);
FillList();
}
}
}
//************************************
//DELETE / CANCEL
//************************************
void CNonClientsDlg::OnDelete()
{
CString q,strID;
if(m_bAddMode)//user pressed cancel button
{
m_btnDone.ShowWindow(TRUE);
m_bAddMode=false;
m_btnAdd.SetWindowText("Add");
m_btnDelete.SetWindowText("Delete");
m_lblList.ShowWindow(TRUE);
m_cbList.ShowWindow(TRUE);
FillList();
return;
}
//USER IS REQUESTING TO DELETE RECORD
//check subrepairs to see if records exist
strID=m_cbList.GetCurrentRowID();
q.Format("SELECT subrepair.where "
"FROM subrepair WHERE (((subrepair.where)=%s)) "
"OR (((subrepair.sentvia)=%s));",strID,strID);
rs->Query(q);
if(!rs->IsEmpty())
{
AfxMessageBox("DATA INTEGRITY PROTECTION:\r\n"
"You can't delete this record at this time.\r\n"
"It is attached to one or more sub-contracted repair records.");
return;
}
else
{
if(AfxMessageBox("Permanently delete?\r\nAre you sure?",MB_YESNO)==IDYES)
{
q.Format("DELETE nonclients.*, nonclients.id "
"FROM nonclients WHERE (((nonclients.id)=%s));"
,m_cbList.GetCurrentRowID());
rs->Ex(q);
//a deleted contact can't be selected
m_strSelectedContact="";
FillList();
}
}
}
//**************************************
//SAVE FIELDS
//**************************************
void CNonClientsDlg::OnKillfocusAddress()
{SaveField(&m_edAddress,"address",true);}
void CNonClientsDlg::OnKillfocusCompname()
{
if(m_edCompName.GetModify()==FALSE) return;
CString strData;
m_edCompName.GetWindowText(strData);
if(strData.IsEmpty())
{
AfxMessageBox("Company or Name is required, can't leave blank.");
m_edCompName.Undo();
m_edCompName.SetModify(FALSE);
m_edCompName.SetFocus();
return;
}
if(IsDupeCompany(strData))
{
CString st;
st.Format(
"The company name \"%s\" has already been entered\r\n"
"as a non-client contact and can not be used again.",strData);
AfxMessageBox(st);
m_edCompName.Undo();
m_edCompName.SetModify(FALSE);
m_edCompName.SetFocus();
return;
}
SaveField(&m_edCompName,"company_person",false);
if(!m_bAddMode)
FillList();
}
void CNonClientsDlg::OnKillfocusContact()
{SaveField(&m_edContact,"contact",true);}
void CNonClientsDlg::OnKillfocusEmail()
{SaveField(&m_edEmail,"email",true);}
void CNonClientsDlg::OnKillfocusFax()
{SaveField(&m_edFax,"fax",true);}
void CNonClientsDlg::OnKillfocusNotes()
{SaveField(&m_edNotes,"notes",true);}
void CNonClientsDlg::OnKillfocusPhone()
{SaveField(&m_edPhone,"phone",true);}
void CNonClientsDlg::OnKillfocusWebsite()
{SaveField(&m_edWebsite,"website",true);}
void CNonClientsDlg::OnKillfocusEdacct()
{SaveField(&m_edAccount,"account",true);}
//*****************************************
void CNonClientsDlg::OnLblemail()
{
CString strURL;
m_edEmail.GetWindowText(strURL);
if(!strURL.IsEmpty())//do this only if there was an url
{
strURL="mailto:" + strURL;
ShellExecute(NULL,"open",strURL,NULL,NULL,SW_SHOWNORMAL);
}
}
//*******************************************
void CNonClientsDlg::OnLblwebsite()
{
CString strURL;
m_edWebsite.GetWindowText(strURL);
if(!strURL.IsEmpty())//do this only if there was an url
{
ShellExecute(NULL,"open",strURL,NULL,NULL,SW_SHOWNORMAL);
}
}
void CNonClientsDlg::OnDone()
{
if(m_pstrReturnValue!=NULL)
*m_pstrReturnValue=m_strSelectedContact;
CDialog::OnOK();
}
void CNonClientsDlg::OnOk()
{
//do nothing
}
void CNonClientsDlg::OnHistory()
{
COleDateTime dtCreated,dtModified;
CString modifier,creator;
long m,c;
//GET RECORD STATUS FIELDS
rs->FetchField("created",&dtCreated);
rs->FetchField("modified",&dtModified);
rs->FetchField("creator",&c);
rs->FetchField("modifier",&m);
creator.Format("%u",c);
modifier.Format("%u",m);
CStatusDlg d;
d.SetValues(&creator,&modifier,&dtCreated,&dtModified);
d.DoModal();
}
void CNonClientsDlg::SetReturnString(CString* strReturn)
{
m_pstrReturnValue=strReturn;
}
void CNonClientsDlg::DisableAll(bool disable)
{
BOOL e=disable ? FALSE : TRUE;
m_btnDelete.EnableWindow(e);
m_cbList.EnableWindow(e);
m_edAddress.EnableWindow(e);
m_edCompName.EnableWindow(e);
m_edContact.EnableWindow(e);
m_edEmail.EnableWindow(e);
m_edFax.EnableWindow(e);
m_edNotes.EnableWindow(e);
m_edPhone.EnableWindow(e);
m_edWebsite.EnableWindow(e);
m_edAccount.EnableWindow(e);
}
void CNonClientsDlg::Security()
{
//m_bReadOnly=false;
int x=m_pApp->Allowed(RNONCLIENTS,true);
if(x==0)//no access allowed
{
m_pApp->SecurityWarning();
CDialog::OnCancel();
}
if(x==2)//read only
{
// m_bReadOnly=true;
m_btnAdd.ShowWindow(FALSE);
m_btnDelete.ShowWindow(FALSE);
m_edAddress.SetReadOnly(TRUE);
m_edCompName.SetReadOnly(TRUE);
m_edContact.SetReadOnly(TRUE);
m_edEmail.SetReadOnly(TRUE);
m_edFax.SetReadOnly(TRUE);
m_edNotes.SetReadOnly(TRUE);
m_edPhone.SetReadOnly(TRUE);
m_edWebsite.SetReadOnly(TRUE);
m_edAccount.SetReadOnly(TRUE);
}
}
// v 1.9.4.4 addition - returns true if the company name entered already exists in the table
bool CNonClientsDlg::IsDupeCompany(CString strNewCompanyName)
{
CString q;
bool bIsDupe=false;
q.Format("SELECT nonclients.company_person FROM nonclients WHERE "
"(((nonclients.company_person)=\"%s\"));",strNewCompanyName);
rs2->QueryReadOnly(q);
if(!rs2->IsEmpty())
bIsDupe=true;
rs2->Close();
return bIsDupe;
}