651 lines
15 KiB
C++
651 lines
15 KiB
C++
// RatesDlg.cpp : implementation file
|
|
//
|
|
|
|
#include "stdafx.h"
|
|
#include "sp.h"
|
|
#include "RatesDlg.h"
|
|
|
|
|
|
#ifdef _DEBUG
|
|
#define new DEBUG_NEW
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[] = __FILE__;
|
|
#endif
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
// CRatesDlg dialog
|
|
|
|
|
|
CRatesDlg::CRatesDlg(CWnd* pParent /*=NULL*/)
|
|
: CDialog(CRatesDlg::IDD, pParent)
|
|
{
|
|
//{{AFX_DATA_INIT(CRatesDlg)
|
|
//}}AFX_DATA_INIT
|
|
m_pApp= (CSpApp*)AfxGetApp();
|
|
/*
|
|
rs=new GZRset("Rates entry screen error:");
|
|
rs->SetConnect(m_pApp->strConnectString);
|
|
*/
|
|
//Initialize recordset pointer
|
|
|
|
rs=m_pApp->rsPool->GetRS("CRatesDlg");
|
|
|
|
cfm = new CgzCurrencyFormatter;
|
|
m_strReturnValue=NULL;
|
|
}
|
|
|
|
CRatesDlg::~CRatesDlg() //destructor
|
|
{
|
|
m_pApp->rsPool->ReleaseRS(&rs->m_nID);
|
|
delete cfm;
|
|
}
|
|
void CRatesDlg::DoDataExchange(CDataExchange* pDX)
|
|
{
|
|
CDialog::DoDataExchange(pDX);
|
|
//{{AFX_DATA_MAP(CRatesDlg)
|
|
DDX_Control(pDX, IDC_TRAVELRATE, m_ckTravel);
|
|
DDX_Control(pDX, IDC_ADD, m_btnAdd);
|
|
DDX_Control(pDX, IDC_EXPIRYDATE, m_dtExpiry);
|
|
DDX_Control(pDX, IDC_ACTIVE, m_ckActive);
|
|
DDX_Control(pDX, IDOK, m_btnDone);
|
|
DDX_Control(pDX, IDC_COST, m_edCost);
|
|
DDX_Control(pDX, IDC_DELETE, m_btnDelete);
|
|
DDX_Control(pDX, IDC_FLATRATE, m_ckFlat);
|
|
DDX_Control(pDX, IDC_PARTNUMBER, m_edPartNumber);
|
|
DDX_Control(pDX, IDC_RATE, m_edCharge);
|
|
DDX_Control(pDX, IDC_RATENAME, m_edRateName);
|
|
DDX_Control(pDX, IDC_RATELISTLABEL, m_lblRates);
|
|
DDX_Control(pDX, IDC_RATELIST, m_cbRates);
|
|
//}}AFX_DATA_MAP
|
|
}
|
|
|
|
|
|
BEGIN_MESSAGE_MAP(CRatesDlg, CDialog)
|
|
//{{AFX_MSG_MAP(CRatesDlg)
|
|
ON_CBN_CLOSEUP(IDC_RATELIST, OnCloseupRatelist)
|
|
ON_BN_CLICKED(IDC_ADD, OnAdd)
|
|
ON_EN_KILLFOCUS(IDC_COST, OnKillfocusCost)
|
|
ON_BN_CLICKED(IDC_ACTIVE, OnActive)
|
|
ON_BN_CLICKED(IDC_DELETE, OnDelete)
|
|
ON_BN_CLICKED(IDC_FLATRATE, OnFlatrate)
|
|
ON_EN_KILLFOCUS(IDC_PARTNUMBER, OnKillfocusPartnumber)
|
|
ON_EN_KILLFOCUS(IDC_RATE, OnKillfocusRate)
|
|
ON_EN_KILLFOCUS(IDC_RATENAME, OnKillfocusRatename)
|
|
ON_NOTIFY(DTN_DATETIMECHANGE, IDC_EXPIRYDATE, OnDatetimechangeExpirydate)
|
|
ON_BN_CLICKED(IDC_TRAVELRATE, OnTravelrate)
|
|
//}}AFX_MSG_MAP
|
|
END_MESSAGE_MAP()
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
// CRatesDlg message handlers
|
|
//*****************************************
|
|
void CRatesDlg::FillRateList()
|
|
{
|
|
CString strData;
|
|
CString strIndex;
|
|
long lData;
|
|
m_cbRates.Clear();
|
|
rs->Query("SELECT rates.* FROM rates ORDER BY rates.name;");
|
|
if(!rs->IsEmpty())
|
|
{
|
|
//fill combo box with available zones
|
|
rs->MoveFirst();
|
|
rs->FetchField("name",&strData);
|
|
rs->FetchField("id",&lData);
|
|
strIndex.Format("%u",lData);
|
|
m_cbRates.AddRow(strData,strIndex);
|
|
while(rs->MoveForward())
|
|
{
|
|
rs->FetchField("name",&strData);
|
|
rs->FetchField("id",&lData);
|
|
strIndex.Format("%u",lData);
|
|
m_cbRates.AddRow(strData,strIndex);
|
|
}
|
|
|
|
|
|
|
|
//pretend user has selected so that other fields get filled in
|
|
if(m_strSelectedRate.IsEmpty()) //first time in
|
|
{
|
|
m_cbRates.SetCurSel(0);
|
|
if(m_strReturnValue!=NULL)
|
|
m_strReturnValue->Format("%s",m_cbRates.GetCurrentRowID());
|
|
|
|
|
|
}
|
|
else//something valid was selected before so stick with it
|
|
m_cbRates.Select(m_strSelectedRate);
|
|
|
|
FillFields();
|
|
}
|
|
else
|
|
DisableAll(true);
|
|
return;
|
|
}
|
|
|
|
|
|
//*****************************************
|
|
BOOL CRatesDlg::OnInitDialog()
|
|
{
|
|
CDialog::OnInitDialog();
|
|
Security();
|
|
m_bAddMode=false;
|
|
FillRateList();
|
|
|
|
return TRUE; // return TRUE unless you set the focus to a control
|
|
// EXCEPTION: OCX Property Pages should return FALSE
|
|
}
|
|
|
|
|
|
//*****************************************
|
|
void CRatesDlg::FillFields()
|
|
{
|
|
CString q,strData,strCurrencySymbol;
|
|
bool bData;
|
|
COleCurrency crData;
|
|
COleDateTime dtData;
|
|
|
|
|
|
q.Format("SELECT rates.* FROM rates WHERE (((rates.id)=%s));",m_cbRates.GetCurrentRowID());
|
|
rs->Query(q);
|
|
if(!rs->IsEmpty())
|
|
{
|
|
rs->FetchField("name",&strData);
|
|
m_edRateName.SetWindowText(strData);
|
|
|
|
//RATE
|
|
rs->FetchField("rate",&crData);
|
|
m_edCharge.SetWindowText(cfm->Format(crData));
|
|
|
|
//COST
|
|
rs->FetchField("cost",&crData);
|
|
m_edCost.SetWindowText(cfm->Format(crData));
|
|
|
|
rs->FetchField("active",&bData);
|
|
m_ckActive.SetCheck(bData ? TRUE:FALSE);
|
|
|
|
rs->FetchField("travelrate",&bData);
|
|
m_ckTravel.SetCheck(bData ? TRUE:FALSE);
|
|
|
|
rs->FetchField("expires",&dtData);
|
|
m_dtExpiry.SetTime(dtData);
|
|
|
|
rs->FetchField("flatrate",&bData);
|
|
m_ckFlat.SetCheck(bData ? TRUE:FALSE);
|
|
|
|
rs->FetchField("partnum",&strData);
|
|
m_edPartNumber.SetWindowText(strData);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
void CRatesDlg::SetReturnString(CString *ret)
|
|
{
|
|
m_strReturnValue=ret;
|
|
}
|
|
|
|
void CRatesDlg::OnCloseupRatelist()
|
|
{
|
|
FillFields();
|
|
if(m_strReturnValue!=NULL)
|
|
m_strReturnValue->Format("%s",m_cbRates.GetCurrentRowID());
|
|
|
|
}
|
|
|
|
void CRatesDlg::OnAdd()
|
|
{
|
|
if(!m_bAddMode)//go into add mode
|
|
{
|
|
DisableAll(false);
|
|
m_btnAdd.SetWindowText("SAVE");
|
|
m_lblRates.ShowWindow(FALSE);
|
|
m_cbRates.ShowWindow(FALSE);
|
|
m_btnDelete.SetWindowText("CANCEL");
|
|
|
|
m_ckActive.SetCheck(TRUE);
|
|
m_ckFlat.SetCheck(FALSE);
|
|
m_ckTravel.SetCheck(FALSE);
|
|
m_btnDone.ShowWindow(FALSE);
|
|
m_edCharge.SetWindowText("");
|
|
//FIX:ADD ONE YEAR TO DATE
|
|
m_dtExpiry.SetTime(COleDateTime::GetCurrentTime());
|
|
|
|
m_edCharge.SetWindowText("");
|
|
m_edCost.SetWindowText("");
|
|
m_edPartNumber.SetWindowText("");
|
|
m_edRateName.SetWindowText("");
|
|
m_bAddMode=true;
|
|
}
|
|
else
|
|
{
|
|
|
|
//save everything
|
|
if(AddRecord()!=true)
|
|
return;
|
|
m_bAddMode=false;
|
|
m_btnAdd.SetWindowText("Add");
|
|
m_lblRates.ShowWindow(TRUE);
|
|
m_cbRates.ShowWindow(TRUE);
|
|
m_btnDelete.SetWindowText("Delete");
|
|
m_ckActive.SetCheck(TRUE);
|
|
m_ckFlat.SetCheck(TRUE);
|
|
m_btnDone.ShowWindow(TRUE);
|
|
FillRateList();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//*************************************************************
|
|
void CRatesDlg::OnKillfocusCost()
|
|
{
|
|
|
|
COleCurrency crData;
|
|
if(!m_bAddMode)
|
|
{
|
|
if(SaveCurrencyField(&m_edCost,"cost"))
|
|
{
|
|
rs->FetchField("cost",&crData);
|
|
m_edCost.SetWindowText(cfm->Format(crData));
|
|
|
|
}
|
|
}
|
|
}
|
|
//*************************************************************
|
|
void CRatesDlg::OnActive()
|
|
{
|
|
SaveField(&m_ckActive,"active");
|
|
|
|
}
|
|
//*************************************************************
|
|
void CRatesDlg::OnDelete()
|
|
{
|
|
|
|
//if in add mode then this is the cancel button
|
|
//not the delete button
|
|
if(m_bAddMode)
|
|
{
|
|
m_btnAdd.SetWindowText("Add");
|
|
m_lblRates.ShowWindow(TRUE);
|
|
m_cbRates.ShowWindow(TRUE);
|
|
m_btnDelete.SetWindowText("Delete");
|
|
m_ckActive.SetCheck(TRUE);
|
|
m_ckFlat.SetCheck(TRUE);
|
|
m_btnDone.ShowWindow(TRUE);
|
|
FillRateList();
|
|
m_bAddMode=false;
|
|
return;
|
|
}
|
|
|
|
|
|
CString q,strData;
|
|
q.Format("SELECT * FROM labor WHERE (rate = %s );",m_cbRates.GetCurrentRowID());
|
|
|
|
|
|
|
|
rs->Query(q);
|
|
if(rs->IsEmpty())//no clients in that zone
|
|
{
|
|
|
|
q.Format("DELETE rates.*, rates.id FROM rates WHERE (((rates.id)=%s));",m_cbRates.GetCurrentRowID());
|
|
if(AfxMessageBox("Are you sure?",MB_YESNO)==IDYES)
|
|
{
|
|
rs->Ex(q);
|
|
//a deleted zone can't be selected
|
|
m_strSelectedRate="";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
q="DATA INTEGRITY PROTECTION:\r\nYou cannot delete this rate because\r\n"
|
|
"there are one or more workorders using it.\r\n"
|
|
"\r\nYou can set it to inactive instead\r\n"
|
|
"For more help press F1 from the rates screen";
|
|
|
|
|
|
AfxMessageBox(q);
|
|
}
|
|
FillRateList();
|
|
|
|
}
|
|
|
|
//*************************************************************
|
|
void CRatesDlg::OnFlatrate()
|
|
{
|
|
SaveField(&m_ckFlat,"flatrate");
|
|
|
|
}
|
|
|
|
|
|
//*************************************************************
|
|
void CRatesDlg::OnKillfocusPartnumber()
|
|
{
|
|
SaveField(&m_edPartNumber,"partnum",true);
|
|
}
|
|
|
|
|
|
//*************************************************************
|
|
void CRatesDlg::OnKillfocusRate()
|
|
{
|
|
|
|
CString str;
|
|
COleCurrency crData;
|
|
m_edCharge.GetWindowText(str);
|
|
if(str.IsEmpty())
|
|
{
|
|
AfxMessageBox("A chargeout rate is mandatory");
|
|
m_edCharge.SetFocus();
|
|
|
|
}
|
|
else
|
|
{
|
|
if(!m_bAddMode)
|
|
{
|
|
if(SaveCurrencyField(&m_edCharge,"rate"))
|
|
{
|
|
rs->FetchField("rate",&crData);
|
|
m_edCharge.SetWindowText(cfm->Format(crData));
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
//*************************************************************
|
|
void CRatesDlg::OnKillfocusRatename()
|
|
{
|
|
SaveField(&m_edRateName,"name",false);
|
|
|
|
}
|
|
|
|
|
|
//*************************************************************
|
|
void CRatesDlg::OnDatetimechangeExpirydate(NMHDR* pNMHDR, LRESULT* pResult)
|
|
{
|
|
SaveField(&m_dtExpiry,"expires");
|
|
|
|
*pResult = 0;
|
|
}
|
|
|
|
|
|
//*************************************************************
|
|
void CRatesDlg::OnOK()
|
|
{
|
|
// TODO: Add extra validation here
|
|
|
|
CDialog::OnOK();
|
|
}
|
|
|
|
//************************************************************
|
|
//SAVE EDIT CONTROL FIELD
|
|
//************************************************************
|
|
bool CRatesDlg::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();
|
|
return true;
|
|
}
|
|
|
|
//************************************************************
|
|
//SAVE GZCOMBO CONTROL FIELD
|
|
//************************************************************
|
|
bool CRatesDlg::SaveField(CgzCombo *cbControl,CString fldname)
|
|
{
|
|
|
|
if(m_bAddMode) return true;//dont attempt to update
|
|
CString str;
|
|
long lData;
|
|
str=cbControl->GetCurrentRowID();
|
|
if(str.IsEmpty()) return false;//shouldn't happen but...
|
|
lData=atol(str);
|
|
rs->UpdateField(fldname,&lData);
|
|
rs->SaveRecord();
|
|
UpdateModified();
|
|
return true;
|
|
}
|
|
|
|
//************************************************************
|
|
//SAVE CheckBox CONTROL FIELD
|
|
//************************************************************
|
|
bool CRatesDlg::SaveField(CButton *ckControl,CString fldname)
|
|
{
|
|
if(m_bAddMode) return true;//dont attempt to update
|
|
bool bData=false;
|
|
BOOL BData;
|
|
BData=ckControl->GetCheck();
|
|
if(BData==TRUE) bData=true;
|
|
rs->UpdateField(fldname,&bData);
|
|
rs->SaveRecord();
|
|
UpdateModified();
|
|
return true;
|
|
}
|
|
//************************************************************
|
|
//SAVE DateTimePicker CONTROL FIELD
|
|
//************************************************************
|
|
bool CRatesDlg::SaveField(CDateTimeCtrl *dtControl,CString fldname)
|
|
{
|
|
if(m_bAddMode) return true;//dont attempt to update
|
|
COleDateTime dtData;
|
|
dtControl->GetTime(dtData);
|
|
rs->UpdateField(fldname,&dtData);
|
|
rs->SaveRecord();
|
|
UpdateModified();
|
|
return true;
|
|
}
|
|
|
|
//************************************************************
|
|
//SAVE Currency in an edit CONTROL FIELD
|
|
//************************************************************
|
|
bool CRatesDlg::SaveCurrencyField(CEdit *crControl,CString fldname)
|
|
{
|
|
if(m_bAddMode) return true;//dont attempt to update
|
|
CString str;
|
|
COleCurrency crData;
|
|
crControl->GetWindowText(str);
|
|
if(str.IsEmpty())
|
|
str="0";
|
|
|
|
if(crData.ParseCurrency(str)!=TRUE)
|
|
{
|
|
AfxMessageBox("Could not interpret your entry in one or more currency fields\r\n"
|
|
"I can't save those fields until they are changed.");
|
|
return false;
|
|
}
|
|
rs->UpdateField(fldname,&crData);
|
|
rs->SaveRecord();
|
|
UpdateModified();
|
|
return true;
|
|
}
|
|
|
|
//*************************************************
|
|
bool CRatesDlg::UpdateModified()
|
|
{
|
|
//updates modified by and modified on fields
|
|
rs->UpdateField("modifier",&m_pApp->m_lusrID);
|
|
COleDateTime dtData;
|
|
dtData=COleDateTime::GetCurrentTime();
|
|
rs->UpdateField("modified",&dtData);
|
|
rs->SaveRecord();
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
bool CRatesDlg::AddRecord()
|
|
{
|
|
|
|
CString q;
|
|
CString strData;
|
|
bool bData;
|
|
COleDateTime dtData,now;
|
|
COleCurrency crRate,crCost;
|
|
|
|
m_edCharge.GetWindowText(strData);
|
|
m_edRateName.GetWindowText(q);
|
|
|
|
//make sure there is at least a charge and rate name
|
|
if(q.IsEmpty() || strData.IsEmpty())
|
|
{
|
|
AfxMessageBox("You must enter at minimum a rate name and Rate/Charge amount to proceed");
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
m_dtExpiry.GetTime(dtData);
|
|
now=COleDateTime::GetCurrentTime();
|
|
if(now.GetYear()==dtData.GetYear() && now.GetMonth()==dtData.GetMonth())
|
|
{
|
|
AfxMessageBox("You must select a rate expiry date.\r\n"
|
|
"If you don't want this rate to expire,\r\n"
|
|
"set the expiry date far into the future\r\n"
|
|
"(100 years is a nice round figure)\r\n\r\n"
|
|
"You can't leave it set to today as that would cause\r\n"
|
|
"rate expiry warnings whenever you tried to use it;\r\n"
|
|
"which would be very annoying.\r\n\r\n"
|
|
"If your thinking to yourself:\r\n"
|
|
"\"what is this rate expiry date thing anyway?\"\r\n"
|
|
"click on it so it's selected and press F1 for more info.\r\n");
|
|
return false;
|
|
|
|
}
|
|
|
|
//do this here because it falls under the validation
|
|
//part of things
|
|
m_edCharge.GetWindowText(strData);
|
|
if(crRate.ParseCurrency(strData)!=TRUE)
|
|
{
|
|
AfxMessageBox("Could not interpret your entry in the Charge/Rate field.\r\n"
|
|
"Please enter a valid currency amount.");
|
|
return false;
|
|
}
|
|
|
|
m_edCost.GetWindowText(strData);
|
|
//empty is ok for the cost some people may not use it.
|
|
if(strData.IsEmpty())
|
|
strData="0";
|
|
|
|
|
|
if(crCost.ParseCurrency(strData)!=TRUE)
|
|
{
|
|
AfxMessageBox("Could not interpret your entry in the Cost field.\r\n"
|
|
"Please enter a valid currency amount.");
|
|
return false;
|
|
}
|
|
|
|
|
|
q.Format("SELECT rates.* FROM rates;");
|
|
rs->Query(q);
|
|
rs->AddNewRecord();
|
|
|
|
//Start a'savin'
|
|
m_edRateName.GetWindowText(strData);
|
|
rs->UpdateField("name",&strData);
|
|
|
|
//save the two currency fields
|
|
rs->UpdateField("rate",&crRate);
|
|
rs->UpdateField("cost",&crCost);
|
|
|
|
bData = m_ckActive.GetCheck() ? true : false;
|
|
rs->UpdateField("active",&bData);
|
|
|
|
rs->UpdateField("expires",&dtData);
|
|
|
|
bData = m_ckFlat.GetCheck() ? true : false;
|
|
rs->UpdateField("flatrate",&bData);
|
|
|
|
bData = m_ckTravel.GetCheck() ? true : false;
|
|
rs->UpdateField("travelrate",&bData);
|
|
|
|
|
|
m_edPartNumber.GetWindowText(strData);
|
|
rs->UpdateField("partnum",&strData);
|
|
|
|
rs->UpdateField("creator",&m_pApp->m_lusrID);
|
|
rs->UpdateField("modifier",&m_pApp->m_lusrID);
|
|
|
|
dtData=COleDateTime::GetCurrentTime();
|
|
rs->UpdateField("modified",&dtData);
|
|
rs->UpdateField("created",&dtData);
|
|
|
|
return rs->SaveRecord();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
void CRatesDlg::DisableAll(bool disable)
|
|
{
|
|
BOOL e = disable ? FALSE : TRUE;
|
|
m_btnDelete.EnableWindow(e);
|
|
m_cbRates.EnableWindow(e);
|
|
m_ckActive.EnableWindow(e);
|
|
m_ckFlat.EnableWindow(e);
|
|
m_ckTravel.EnableWindow(e);
|
|
m_dtExpiry.EnableWindow(e);
|
|
m_edCharge.EnableWindow(e);
|
|
m_edCost.EnableWindow(e);
|
|
m_edPartNumber.EnableWindow(e);
|
|
m_edRateName.EnableWindow(e);
|
|
|
|
}
|
|
|
|
void CRatesDlg::Security()
|
|
{
|
|
//m_bReadOnly=false;
|
|
int x=m_pApp->Allowed(RRATES,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_ckActive.EnableWindow(FALSE);
|
|
m_ckFlat.EnableWindow(FALSE);
|
|
m_ckTravel.EnableWindow(FALSE);
|
|
m_dtExpiry.EnableWindow(FALSE);
|
|
m_edCharge.SetReadOnly(TRUE);
|
|
m_edCost.SetReadOnly(TRUE);
|
|
m_edPartNumber.SetReadOnly(TRUE);
|
|
m_edRateName.SetReadOnly(TRUE);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
void CRatesDlg::OnTravelrate()
|
|
{
|
|
SaveField(&m_ckTravel,"travelrate");
|
|
}
|