📄 goodssetdialog.cpp
字号:
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hrG))
{
_variant_t var;
CString strValue;
while(!pRecordsetG->adoEOF)
{
var = pRecordsetG->GetCollect ("GoodsName");
strValue = (LPCSTR)_bstr_t(var);
if(strValue == m_GoodsName)
{
CString string;
string.Format(_T("货品%s已经存在!"), m_GoodsName);
AfxMessageBox(string);
m_GoodsName = "";
UpdateData(FALSE);
CEdit * pEdit = (CEdit*)GetDlgItem(IDC_EDIT_GOODS);
pEdit->SetFocus();
return;
}
pRecordsetG->MoveNext();
}
pRecordsetG->AddNew();
pRecordsetG->PutCollect ("GoodsName", _variant_t(m_GoodsName));
pRecordsetG->PutCollect ("Units", _variant_t(m_GoodsUnit));
if(m_GoodsPrice == "")
pRecordsetG->PutCollect ("SalePrice", _variant_t(_T("0")));
else
pRecordsetG->PutCollect ("SalePrice", _variant_t(m_GoodsPrice));
pRecordsetG->PutCollect ("WarningStore", _variant_t(m_Warn));
pRecordsetG->PutCollect ("BuyPrice", _variant_t(_T("0")));
pRecordsetG->PutCollect ("NowStore", _variant_t("0"));
pRecordsetG->Update();
m_GoodsList.InsertItem (m_Index, m_GoodsName);
m_GoodsList.SetItemText (m_Index, 1, m_GoodsUnit);
m_GoodsList.SetItemText (m_Index, 2, _T("0"));
m_GoodsList.SetItemText (m_Index, 3, m_GoodsPrice);
m_GoodsList.SetItemText (m_Index, 4, m_Warn);
m_GoodsList.SetItemText (m_Index, 5, _T("0"));
pRecordsetG->Close();
pRecordsetG = NULL;
m_Succeed = TRUE;
}//if
}//try
catch(_com_error &e)
{
AfxMessageBox(e.ErrorMessage());
return;
}
}
void CGoodsSetDialog::OnBnClickedButtonModify()
{
int sel = m_GoodsList.GetSelectionMark();
if(sel == LB_ERR)
{
MessageBox(_T("请选者一个货品!"), _T("货品设置"), MB_ICONERROR |MB_OK);
return;
}
m_Index = sel;
m_GoodModify = TRUE;
ControlCanUse();
CButton *pButton = (CButton*)GetDlgItem (IDC_BUTTON_ADD);
pButton->EnableWindow(FALSE);
pButton = (CButton*)GetDlgItem (IDC_BUTTON_DEL);
pButton->EnableWindow(FALSE);
pButton = (CButton*)GetDlgItem (IDC_BUTTON_GOODSETTOEXCEL);
pButton->EnableWindow(FALSE);
CEdit *pEdit = (CEdit*)GetDlgItem (IDC_EDIT_GOODSNAME);
pEdit->EnableWindow(FALSE);
pEdit = (CEdit*)GetDlgItem (IDC_EDIT_GOODS);
pEdit->SetFocus();
m_GoodsName = m_GoodsList.GetItemText (sel, 0);
m_GoodsUnit = m_GoodsList.GetItemText (sel, 1);
m_GoodsPrice = m_GoodsList.GetItemText (sel, 3);
m_Warn = m_GoodsList.GetItemText (sel, 4);
UpdateData(FALSE);
}
void CGoodsSetDialog::Modify()
{
_RecordsetPtr pRecordsetG;
HRESULT hrG;
try
{
hrG = pRecordsetG.CreateInstance(__uuidof(Recordset));
if(FAILED(hrG))
{
AfxMessageBox(_T("不能创建实例!"));
return;
}
hrG = pRecordsetG->Open ("select * from GoodsTable",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hrG))
{
if(!pRecordsetG->adoEOF)
{
CString OldGoodName = m_GoodsName;
UpdateData(TRUE);
_variant_t var;
CString strValue;
CString strSQL;
if(OldGoodName != m_GoodsName)
{
while(!pRecordsetG->adoEOF)
{
var = pRecordsetG->GetCollect ("GoodsName");
strValue = (LPCSTR)_bstr_t(var);
if(strValue == m_GoodsName)
{
MessageBox (_T("已有该货品!"), _T("货品设置"), MB_ICONERROR |MB_OK);
m_GoodsName = OldGoodName;
UpdateData(FALSE);
CEdit *pEdit = (CEdit*) GetDlgItem(IDC_EDIT_GOODS);
pEdit->SetFocus();
return;
}
pRecordsetG->MoveNext();
}//while
}//if
pRecordsetG->Close();
strSQL.Format("select * from GoodsTable where GoodsName = '%s'", OldGoodName);
pRecordsetG->Open(_bstr_t(strSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
pRecordsetG->PutCollect("GoodsName", _variant_t(m_GoodsName));
m_GoodsList.SetItemText (m_Index, 0, m_GoodsName);
if(m_GoodsUnit == "")
{
MessageBox(_T("商品单位不能为空!"),_T("货品设置"), MB_ICONERROR |MB_OK);
CEdit *pEdit = (CEdit*) GetDlgItem (IDC_EDIT_UNIT);
pEdit->SetFocus();
return;
}
pRecordsetG->PutCollect("Units", _variant_t(m_GoodsUnit));
m_GoodsList.SetItemText (m_Index, 1, m_GoodsUnit);
int price = atoi (m_GoodsPrice);
if(m_GoodsPrice == "" || price < 0)
{
MessageBox (_T("价钱输入错误!"),_T("货品设置"), MB_ICONERROR |MB_OK);
CEdit *pEdit = (CEdit*)GetDlgItem (IDC_EDIT_PERPRICE);
pEdit->SetFocus();
return;
}
pRecordsetG->PutCollect ("SalePrice", _variant_t(m_GoodsPrice));
m_GoodsList.SetItemText (m_Index, 3, m_GoodsPrice);
pRecordsetG->PutCollect ("WarningStore", _variant_t(m_Warn));
m_GoodsList.SetItemText (m_Index, 4, m_Warn);
pRecordsetG->Update();
pRecordsetG->Close();
pRecordsetG = NULL;
m_Succeed = TRUE;
}//if
}//if
}//try
catch(_com_error &e)
{
AfxMessageBox(e.ErrorMessage());
}
}
void CGoodsSetDialog::OnBnClickedButtonDel()
{
int sel = m_GoodsList.GetSelectionMark();
if(sel == LB_ERR)
{
MessageBox (_T("请选择要删除的商品!"),_T("货品设置"), MB_ICONERROR |MB_OK);
return;
}
CString GoodsName, string;
GoodsName = m_GoodsList.GetItemText ( sel, 0);
string.Format("真的要删除货品%s吗?", GoodsName);
if(MessageBox(string,_T("货品设置"), MB_ICONEXCLAMATION | MB_OKCANCEL) == IDOK)
{
try
{
CString strSQL;
strSQL.Format( "delete from GoodsTable where GoodsName = '%s'", GoodsName);
m_pConnection->BeginTrans();
_variant_t RecordsAffected;
m_pConnection->Execute(_bstr_t(strSQL), &RecordsAffected, adCmdText);
m_pConnection->CommitTrans();
m_GoodsList.DeleteItem(sel);
}
catch(_com_error &e)
{
m_pConnection->RollbackTrans();
AfxMessageBox(e.ErrorMessage());
return;
}
}//if
MessageBox (_T("成功删除货品"), _T("货品设置"), MB_ICONASTERISK | MB_OK);
}
void CGoodsSetDialog::OnEnChangeEditGoodsname()
{
// TODO: 如果该控件是 RICHEDIT 控件,则它将不会
// 发送该通知,除非重写 CDialog::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
CString string;
CButton *pButton = (CButton*)GetDlgItem(IDC_EDIT_GOODSNAME);
pButton->GetWindowText(string);
if(string.GetLength()>1)
{
_RecordsetPtr pRecordsetG;
HRESULT hrG;
hrG = pRecordsetG.CreateInstance (__uuidof(Recordset));
if(FAILED(hrG))
{
AfxMessageBox(_T("创建实例失败!"));
return;
}
CString strSQL = "select * from GoodsTable where GoodsName like '";
strSQL += _T("%");
strSQL += string;
strSQL += _T("%'");
hrG = pRecordsetG->Open (_bstr_t(strSQL),
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(SUCCEEDED(hrG))
{
if(!pRecordsetG->adoEOF)
{
m_GoodsList.DeleteAllItems();
_variant_t var;
CString strValue;
int index = 0;
while(!pRecordsetG->adoEOF)
{
var = pRecordsetG->GetCollect ("GoodsName");
strValue = (LPCSTR)_bstr_t(var);
m_GoodsList.InsertItem (index, strValue);
var = pRecordsetG->GetCollect ("Units");
strValue = (LPCSTR)_bstr_t(var);
m_GoodsList.SetItemText (index, 1, strValue);
var = pRecordsetG->GetCollect ("BuyPrice");
strValue = (LPCSTR)_bstr_t(var);
m_GoodsList.SetItemText (index, 2, strValue);
var = pRecordsetG->GetCollect ("SalePrice");
strValue = (LPCSTR)_bstr_t(var);
m_GoodsList.SetItemText (index, 3, strValue);
var = pRecordsetG->GetCollect ("WarningStore");
strValue = (LPCSTR)_bstr_t(var);
m_GoodsList.SetItemText (index, 4, strValue);
var = pRecordsetG->GetCollect ("NowStore");
strValue = (LPCSTR)_bstr_t(var);
m_GoodsList.SetItemText (index, 5, strValue);
index ++;
pRecordsetG->MoveNext();
}
}
else{
m_GoodsList.DeleteAllItems();
InitGoodsList();
}
pRecordsetG->Close();
pRecordsetG = NULL;
}
}//if
else
{
m_GoodsList.DeleteAllItems();
InitGoodsList();
}
}
void CGoodsSetDialog::OnBnClickedButtonGoodsettoexcel()
{
if(m_GoodsList.GetItemCount()<0)
{
MessageBox (_T("没有任何数据,不能导出!"), _T("货品设置"), MB_ICONERROR | MB_OK);
return;
}
char strDirectory[100];
memset(strDirectory,0,100);
GetCurrentDirectory(100,strDirectory);
CFileDialog dlg (FALSE, _T ("xls"), m_strPathName,
OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY,
m_szFilters );
if (dlg.DoModal () == IDOK)
if (SaveFile (dlg.GetPathName ()))
{ m_strPathName = dlg.GetPathName ();
MessageBox(_T("导出信息成功!"), _T("存货管理"), MB_ICONASTERISK | MB_OK);
}
SetCurrentDirectory(strDirectory);
}
BOOL CGoodsSetDialog:: SaveFile(LPCTSTR pszFile)
{
BOOL bResult = FALSE;
try
{
CStdioFile file (pszFile, CFile::modeWrite | CFile::modeCreate);
int Count = m_GoodsList.GetItemCount();
if (Count)
{
CString string;
CString tempstring;
string += _T("货品名称");
string += '\t';
string += _T("单位");
string += '\t';
string += _T("最近进价");
string += '\t';
string += _T("预定售价");
string += '\t';
string +=_T("警告存量");
string += '\t';
string += _T("当前存量");
string += '\n';
file.WriteString(string);
for (int index = 0; index < Count; index++)
{
string = "";
tempstring = "";
tempstring = m_GoodsList.GetItemText (index , 0);
string += tempstring;
string += '\t';
tempstring = m_GoodsList.GetItemText (index, 1);
string += tempstring;
string += '\t';
tempstring = m_GoodsList.GetItemText (index, 2);
string += tempstring;
string += '\t';
tempstring = m_GoodsList.GetItemText (index, 3);
string += tempstring;
string += '\t';
tempstring = m_GoodsList.GetItemText (index, 4);
string += tempstring;
string += '\t';
tempstring = m_GoodsList.GetItemText (index, 5);
string += tempstring;
string += '\t';
string += '\n';
file.WriteString(string);
}
}
bResult = TRUE;
}
catch (CFileException* e)
{
e->ReportError ();
e->Delete ();
}
return bResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -