Files
ayanova7/archive/ayanova 1.9.4 CE final release db schema 171/sp/ClientsRA.cpp

547 lines
14 KiB
C++

// ClientsRA.cpp : implementation file
//
#include "stdafx.h"
#include "sp.h"
#include "ClientsRA.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientsRA dialog
CClientsRA::CClientsRA(CWnd* pParent /*=NULL*/)
: CDialog(CClientsRA::IDD, pParent)
, m_bDontCallSaveRecord(false)
{
//{{AFX_DATA_INIT(CClientsRA)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_pApp= (CSpApp*)AfxGetApp();
//Initialize recordset pointer
rs=m_pApp->rsPool->GetRS("CClientsRA");
m_bAddMode=false;
}
void CClientsRA::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientsRA)
DDX_Control(pDX, IDC_LBL_HISTORY, m_lblHistory);
DDX_Control(pDX, IDC_EDPW, m_edPassword);
DDX_Control(pDX, IDC_EDPRIVATENOTES, m_edPrivateNotes);
DDX_Control(pDX, IDC_EDLOGINID, m_edLoginID);
DDX_Control(pDX, IDC_CKWOREPORTS, m_ckWOReports);
DDX_Control(pDX, IDC_CKVIEWWORKORDERS, m_ckViewWorkorders);
DDX_Control(pDX, IDC_CKVIEWSTATUS, m_ckViewStatus);
DDX_Control(pDX, IDC_CKVIEWREQUESTS, m_ckViewRequests);
DDX_Control(pDX, IDC_CKREQUEST_SERVICE, m_ckRequestService);
DDX_Control(pDX, IDC_CBCLIENT, m_cbClient);
DDX_Control(pDX, IDC_CBACCOUNT, m_cbAccount);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientsRA, CDialog)
//{{AFX_MSG_MAP(CClientsRA)
ON_CBN_CLOSEUP(IDC_CBCLIENT, OnCloseupCbclient)
ON_CBN_CLOSEUP(IDC_CBACCOUNT, OnCloseupCbaccount)
ON_COMMAND(ID_CLIENT_RA_ADD, OnClientRaAdd)
ON_COMMAND(ID_CLIENT_RA_CLOSE, OnClientRaClose)
ON_COMMAND(ID_CLIENT_RA_DELETE, OnClientRaDelete)
ON_EN_KILLFOCUS(IDC_EDLOGINID, OnKillfocusEdloginid)
ON_EN_KILLFOCUS(IDC_EDPRIVATENOTES, OnKillfocusEdprivatenotes)
ON_EN_KILLFOCUS(IDC_EDPW, OnKillfocusEdpw)
ON_BN_CLICKED(IDC_CKWOREPORTS, OnCkworeports)
ON_BN_CLICKED(IDC_CKVIEWWORKORDERS, OnCkviewworkorders)
ON_BN_CLICKED(IDC_CKVIEWSTATUS, OnCkviewstatus)
ON_BN_CLICKED(IDC_CKVIEWREQUESTS, OnCkviewrequests)
ON_BN_CLICKED(IDC_CKREQUEST_SERVICE, OnCkrequestService)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientsRA message handlers
void CClientsRA::OnOK()
{}
CClientsRA::~CClientsRA()
{
//delete rs;
m_pApp->rsPool->ReleaseRS(&rs->m_nID);
}
BOOL CClientsRA::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
FillClientList();
FillAccountList();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CClientsRA::FillClientList()
{
CString q,strData,strIndex;
long lData;
//fill client list
m_strCurrentAccount.Empty();
m_cbClient.Clear();
ClearAllEntries();
q.Format("SELECT clients.id, clients.company FROM clients "
"ORDER BY clients.company;");
rs->QueryReadOnly(q);
if(rs->IsEmpty())
return;
do{
rs->FetchField("company",&strData);
rs->FetchField("id",&lData);
strIndex.Format("%u",lData);
m_cbClient.AddRow(strData,strIndex);
}while(rs->MoveForward());
//handle empty selection or reselect current one
if(m_strCurrentClient.IsEmpty()) //first time in
{
m_cbClient.SetCurSel(0);
m_strCurrentClient=m_cbClient.GetCurrentRowID();
}
else//something valid was selected before so stick with it
m_cbClient.Select(m_strCurrentClient);
}
void CClientsRA::FillAccountList()
{
CString q,strData,strIndex;
long lData;
//FILL ACCOUNT LIST
m_cbAccount.Clear();
q.Format("SELECT clients_aras.id, clients_aras.loginid "
"FROM clients_aras "
"WHERE (((clients_aras.clientlink)=%s));",m_strCurrentClient);
rs->QueryReadOnly(q);
if(rs->IsEmpty())
{
AllowEntry(false);
return;
}
do{
rs->FetchField("loginid",&strData);
rs->FetchField("id",&lData);
strIndex.Format("%u",lData);
m_cbAccount.AddRow(strData,strIndex);
}while(rs->MoveForward());
//done this way in case there is some reason later
//to persist the selected account
//m_strCurrentAccount.Empty();
//handle empty selection or reselect current one
if(m_strCurrentAccount.IsEmpty()) //first time in
{
m_cbAccount.SetCurSel(0);
m_strCurrentAccount=m_cbAccount.GetCurrentRowID();
}
else//something valid was selected before so stick with it
m_cbAccount.Select(m_strCurrentAccount);
FillFields();
}
void CClientsRA::AllowEntry(bool bAllow)
{
BOOL e=bAllow?TRUE:FALSE;
m_ckRequestService.EnableWindow(e);
m_ckViewRequests.EnableWindow(e);
m_ckViewStatus.EnableWindow(e);
m_ckViewWorkorders.EnableWindow(e);
m_ckWOReports.EnableWindow(e);
m_edLoginID.EnableWindow(e);
m_edPassword.EnableWindow(e);
m_edPrivateNotes.EnableWindow(e);
m_lblHistory.EnableWindow(e);
}
void CClientsRA::ClearAllEntries()
{
m_ckRequestService.SetCheck(FALSE);
m_ckViewRequests.SetCheck(FALSE);
m_ckViewStatus.SetCheck(FALSE);
m_ckViewWorkorders.SetCheck(FALSE);
m_ckWOReports.SetCheck(FALSE);
m_edLoginID.SetWindowText("");
m_edPassword.SetWindowText("");
m_edPrivateNotes.SetWindowText("");
m_lblHistory.SetWindowText("");
}
void CClientsRA::FillFields()
{
CString q;
bool bData;
CString strData;
COleDateTime dtData;
CString strHistory;
ClearAllEntries();
if(m_strCurrentAccount.IsEmpty()) m_strCurrentAccount="0";//so that the following normal process happens
q.Format("SELECT clients_aras.*, [users].[last] & \", \" & [users].[first] AS CREATED_BY, "
"[users_1].[last] & \", \" & [users_1].[first] AS LAST_EDITED_BY "
"FROM (clients_aras LEFT JOIN users ON clients_aras.creator = users.id) LEFT JOIN users AS users_1 ON clients_aras.modifier = users_1.id "
"WHERE (((clients_aras.id)=%s));",m_strCurrentAccount);
rs->QueryReadOnly(q);
if(rs->IsEmpty())
{
AllowEntry(false);
return;
}
else
AllowEntry(true);
rs->FetchField("loginid",&strData);
m_edLoginID.SetWindowText(strData);
rs->FetchField("loginpw",&strData);
m_edPassword.SetWindowText(strData);
rs->FetchField("private_notes",&strData);
m_edPrivateNotes.SetWindowText(strData);
rs->FetchField("view_wo_status", &bData);
m_ckViewStatus.SetCheck(bData?TRUE:FALSE);
rs->FetchField("request_service", &bData);
m_ckRequestService.SetCheck(bData?TRUE:FALSE);
rs->FetchField("view_request_list", &bData);
m_ckViewRequests.SetCheck(bData?TRUE:FALSE);
rs->FetchField("wo_view", &bData);
m_ckViewWorkorders.SetCheck(bData?TRUE:FALSE);
rs->FetchField("wo_report", &bData);
m_ckWOReports.SetCheck(bData?TRUE:FALSE);
//HISTORY
rs->FetchField("CREATED_BY",&strData);
strHistory="Account created by: " + strData;
rs->FetchField("created",&dtData);
strHistory+=" On: " + dtData.Format() + "\r\n";
rs->FetchField("LAST_EDITED_BY",&strData);
strHistory+="Last modified by: " + strData;
rs->FetchField("modified",&dtData);
strHistory+=" On: " + dtData.Format() + "\r\n";
rs->FetchField("last_login_date",&dtData);
strHistory+="Last remote login on: " + dtData.Format();
m_lblHistory.SetWindowText(strHistory);
}
void CClientsRA::OnCloseupCbclient()
{
if(m_strCurrentClient==m_cbClient.GetCurrentRowID())
return;//do nothing if same client. Fixes bounce problem
ClearAllEntries();
m_strCurrentAccount.Empty();
m_strCurrentClient=m_cbClient.GetCurrentRowID();
FillAccountList();
}
void CClientsRA::OnCloseupCbaccount()
{
if(m_strCurrentAccount==m_cbAccount.GetCurrentRowID())
return;//fix for bounce problem
m_strCurrentAccount=m_cbAccount.GetCurrentRowID();
FillFields();
}
void CClientsRA::OnClientRaAdd()
{
if(m_bAddMode)
{
//save record
if(SaveRecord(false))
{
m_cbAccount.EnableWindow(TRUE);
m_cbClient.EnableWindow(TRUE);
m_bAddMode=false;
m_strCurrentAccount.Empty();
FillAccountList();
}
}
else
{
ClearAllEntries();
AllowEntry(true);
m_bAddMode=true;
m_cbAccount.EnableWindow(FALSE);
m_cbClient.EnableWindow(FALSE);
}
HandleMenuMode();
}
void CClientsRA::OnClientRaClose()
{
if(m_bAddMode)
{
AfxMessageBox("You can't close while adding a record.");
return;
}
CDialog::OnOK();
}
void CClientsRA::OnClientRaDelete()
{
if(m_bAddMode)//is cancel, not delete
{
m_bAddMode=false;
m_cbAccount.EnableWindow(TRUE);
m_cbClient.EnableWindow(TRUE);
HandleMenuMode();
FillAccountList();
return;
}
//Delete a record
CString q;
q.Format("DELETE clients_aras.*, clients_aras.id FROM clients_aras "
"WHERE (((clients_aras.id)=%s));",m_strCurrentAccount);
if(AfxMessageBox("Delete this account?\r\nAre you sure?",MB_YESNO)==IDYES)
{
m_strCurrentAccount.Empty();
ClearAllEntries();
rs->Ex(q);
FillAccountList();
}
}
void CClientsRA::HandleMenuMode()
{
CString strDeleteText;
CString strAddText;
if(m_bAddMode)
{
strDeleteText="Ca&ncel add";
strAddText="&Save";
}
else
{
strDeleteText="&Delete";
strAddText="&Add";
}
CMenu* pMenu=this->GetMenu();
pMenu->ModifyMenu(ID_CLIENT_RA_ADD,MF_BYCOMMAND,ID_CLIENT_RA_ADD,strAddText);
pMenu->ModifyMenu(ID_CLIENT_RA_DELETE,MF_BYCOMMAND,ID_CLIENT_RA_DELETE,strDeleteText);
this->DrawMenuBar();
}
bool CClientsRA::SaveRecord(bool bUpdate)
{
//don't do this if an update request and in add mode
if(m_bAddMode && bUpdate==true) return false;
GZK k;
CString q,login,pw,notes,str,loginhashed;
CString RequestService, ViewRequests, ViewStatus, ViewWorkorders, WOReports;
COleDateTime dtNow, dtNever;
dtNow=COleDateTime::GetCurrentTime();
dtNever.SetDateTime(1968,03,12,0,0,0);
m_edLoginID.GetWindowText(login);
if(login.IsEmpty())
{
AfxMessageBox("Login can not be blank");
return false;
}
loginhashed=login;
k.GZHash(&loginhashed);
m_edPassword.GetWindowText(pw);
m_edPrivateNotes.GetWindowText(notes);
notes.Replace("\"","'");
RequestService=m_ckRequestService.GetCheck() ? "True":"False";
ViewRequests=m_ckViewRequests.GetCheck() ? "True":"False";
ViewStatus=m_ckViewStatus.GetCheck() ? "True":"False";
ViewWorkorders=m_ckViewWorkorders.GetCheck() ? "True":"False";
WOReports=m_ckWOReports.GetCheck() ? "True":"False";
q.Format("SELECT users.login FROM users WHERE (((users.login)=\"%s\")); ",loginhashed);
rs->QueryReadOnly(q);
if(!rs->IsEmpty())
{
AfxMessageBox(
"Security warning: The login assigned to this client can not be used.\r\n"
"The reason is that the same login is in use already by an AyaNova user.\r\n\r\n"
"Login names must be unique amongst all AyaNova users and ARAS client accounts.\r\n\r\n"
"Change the login name to something unique before saving.",MB_ICONSTOP);
m_edLoginID.SetFocus();
return false;
}
if(bUpdate==false)//insert new record
{
//Check if login name already exists and disallow if so
q.Format("SELECT clients.company FROM clients_aras LEFT JOIN "
"clients ON clients_aras.clientlink = clients.id WHERE "
"(((clients_aras.loginid)=\"%s\"));",login);
rs->QueryReadOnly(q);
if(!rs->IsEmpty())
{
rs->FetchField("company",&str);
q.Format(
"Problem: No two ARAS client login id's can be the same.\r\n"
"The account login id \"%s\" is already in use by \r\n"
"an existing account for %s\r\n"
"This record can not be saved until the login id is changed.\r\n",login,str);
AfxMessageBox(q,MB_ICONSTOP);
m_edLoginID.SetFocus();
return false;
}
q.Format("INSERT INTO clients_aras ( clientlink, loginid, loginpw, view_wo_status, "
"search_history, wo_view, wo_report, request_service, view_request_list, private_notes, "
"last_login_date, creator, modifier, created, modified ) "
"SELECT %s , \"%s\", \"%s\", %s, %s , %s, %s, %s, %s, \"%s\", #%s#, %u, %u, #%s#, #%s#;"
,m_strCurrentClient,login, pw,ViewStatus, "False",ViewWorkorders,WOReports,RequestService,ViewRequests,notes,
dtNever.Format(_T("%m/%d/%Y %H:%M:%S")),m_pApp->m_lusrID,m_pApp->m_lusrID,
dtNow.Format(_T("%m/%d/%Y %H:%M:%S")),dtNow.Format(_T("%m/%d/%Y %H:%M:%S")));
}
else//update existing record
{
//check if login name already exists for other clients than this one
//if so then disallow save
//Query for any user other than current account with same password
q.Format("SELECT clients.company FROM clients_aras LEFT JOIN "
"clients ON clients_aras.clientlink = clients.id WHERE "
"(((clients_aras.loginid)=\"%s\") AND ((clients_aras.id)<>%s));",login,m_strCurrentAccount);
rs->QueryReadOnly(q);
if(!rs->IsEmpty())
{
rs->FetchField("company",&str);
q.Format(
"Problem: No two ARAS client login id's can be the same.\r\n"
"The account login id \"%s\" is already in use by \r\n"
"an existing account for %s\r\n"
"This record can not be saved until the login id is changed.\r\n",login,str);
AfxMessageBox(q,MB_ICONSTOP);
//m_edLoginID.SetFocus();
return false;
}
q.Format("UPDATE clients_aras SET loginid=\"%s\", loginpw=\"%s\", view_wo_status=%s, search_history=False, "
"wo_view=%s, wo_report=%s, request_service=%s, view_request_list=%s, "
"private_notes=\"%s\", modifier=%u, modified=#%s# "
"WHERE (((clients_aras.id)=%s));",
login,pw,ViewStatus,ViewWorkorders,WOReports,RequestService,ViewRequests,notes,m_pApp->m_lusrID,
dtNow.Format(_T("%m/%d/%Y %H:%M:%S")),m_strCurrentAccount);
}
return(rs->Ex(q));
}
void CClientsRA::OnKillfocusEdloginid()
{
if(m_bDontCallSaveRecord) return;
m_bDontCallSaveRecord=true;
if(SaveRecord(true))
{
if(!m_bAddMode)
FillAccountList();
}
m_bDontCallSaveRecord=false;
}
void CClientsRA::OnKillfocusEdprivatenotes()
{SaveRecord(true);}
void CClientsRA::OnKillfocusEdpw()
{SaveRecord(true);}
void CClientsRA::OnCkworeports()
{SaveRecord(true);}
void CClientsRA::OnCkviewworkorders()
{SaveRecord(true);}
void CClientsRA::OnCkviewstatus()
{SaveRecord(true);}
void CClientsRA::OnCkviewrequests()
{SaveRecord(true);}
void CClientsRA::OnCkrequestService()
{SaveRecord(true);}