📄 materieldlg.cpp
字号:
// MaterielDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyPos.h"
#include "MaterielDlg.h"
#include "ClassDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMaterielDlg dialog
extern CMyPosApp theApp;
CMaterielDlg::CMaterielDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMaterielDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMaterielDlg)
m_mname = _T("");
//}}AFX_DATA_INIT
}
void CMaterielDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMaterielDlg)
DDX_Control(pDX, IDC_LIST_MATERIEL, m_oListmateriel);
DDX_Control(pDX, IDC_TREE_MCLASS, m_oMclass);
DDX_Control(pDX, IDC_CHECK_SUPPLY, m_oSupply);
DDX_Control(pDX, IDC_EDIT_MSHOPCODE, m_oMshopcode);
DDX_Control(pDX, IDC_EDIT_MPRICE, m_oMprice);
DDX_Control(pDX, IDC_EDIT_MNAME, m_oMname);
DDX_Control(pDX, IDC_EDIT_MBARCODE, m_oMbarcode);
DDX_Control(pDX, IDC_COMBO_MDISCOUNT, m_oMdiscount);
DDX_Text(pDX, IDC_EDIT_MNAME, m_mname);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMaterielDlg, CDialog)
//{{AFX_MSG_MAP(CMaterielDlg)
ON_BN_CLICKED(IDC_BUTTON_MSAVE, OnButtonMsave)
ON_BN_CLICKED(IDC_BUTTON_MADD, OnButtonMadd)
ON_BN_CLICKED(IDC_BUTTON_MDEL, OnButtonMdel)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_MCLASS, OnSelchangedTreeMclass)
ON_NOTIFY(NM_CLICK, IDC_LIST_MATERIEL, OnClickListMateriel)
ON_WM_SHOWWINDOW()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMaterielDlg message handlers
void CMaterielDlg::OnButtonMsave()
{
CString sql="select * from Materiel";
CString snewid,sname,sdiscount,sprice,ssupply,sshopcode,sbarcode;
CString sMID;
_RecordsetPtr m_pRecordset; //Must define it in function!!!!
UpdateData();
//检查数据完整性
m_oMname.GetWindowText(sname);//Get the current text in edit.
m_oMprice.GetWindowText(sprice);
if(sname==""||sprice=="")
{
AfxMessageBox("请输入商品名称、价格!");
return;
}
m_oMname.GetWindowText(sname);//Get the current text in edit.
int discount=m_oMdiscount.GetCurSel();
sdiscount.Format("%d",discount);
if(m_oSupply.GetCheck()==0) ssupply="0";
if(m_oSupply.GetCheck()==1) ssupply="1";
m_oMprice.GetWindowText(sprice);
m_oMshopcode.GetWindowText(sshopcode);
m_oMbarcode.GetWindowText(sbarcode);
if(lNewID>lMID)
{
if(bRepeat(sname)==TRUE)
{
AfxMessageBox("商品名称重复,请重新输入!");
return;
}
snewid.Format("%d",lNewID);
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
m_pRecordset->AddNew();//insert the current into database.
m_pRecordset->PutCollect("ID",_variant_t(snewid));
m_pRecordset->PutCollect("NAME",_variant_t(sname));
m_pRecordset->PutCollect("DISCOUNT",_variant_t(sdiscount));//Here m_discount must be a float.
m_pRecordset->PutCollect("SUPPLY",_variant_t(ssupply));
m_pRecordset->PutCollect("PRICE",_variant_t(sprice));
m_pRecordset->PutCollect("SHOPCODE",_variant_t(sshopcode));
m_pRecordset->PutCollect("BARCODE",_variant_t(sbarcode));
m_pRecordset->PutCollect("CLASSID",_variant_t(sclassid));
m_pRecordset->Update();//保存到库中
m_pRecordset->Close();
UpdateData(FALSE);
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("增加商品出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
else if(AfxMessageBox("确定修改商品资料吗?",MB_YESNO)==IDYES)
{
_variant_t RecordsAffected;
sMID.Format("%d",lMID);
sql="Update MATERIEL set NAME='"+sname+
"',PRICE="+sprice+
",DISCOUNT="+sdiscount+
",SHOPCODE='"+sshopcode+
"',BARCODE='"+sbarcode+
"',SUPPLY="+ssupply+" where ID="+sMID+"";
try
{
theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
UpdateData(FALSE);
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("修改商品资料出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
CString sql1="Select * from MATERIEL where CLASSID="+sclassid+"";
ReadtoList(sql1);
}
long CMaterielDlg::GenNewID()
{
CString sql;
long NewID;
_RecordsetPtr m_pRecordset; //Must define it in function!!!!
sql="SELECT Max(ID) FROM MATERIEL";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
_variant_t vIndex = (long)0;//How to get field value in only 1 record and 1 field condition.
_variant_t vtemp = m_pRecordset->GetCollect(vIndex);
if(vtemp.lVal>0)
NewID =(long)(m_pRecordset->GetCollect(vIndex))+1;///取得第一个字段的值(MAX ID)加1后放入id变量.
else
NewID=1;
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString stemp;
stemp.Format("获取类别ID最大值出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
}
return NewID;
}
void CMaterielDlg::AddSubTree(CString ParTree, HTREEITEM hPartItem)
{
HTREEITEM hCurrent;
CString sql;
CString curID;
_RecordsetPtr m_pTRecordset; //Must define it in function!!!Becourse this is a RECURSION function.
sql="SELECT * FROM MATERIELCLASS where PARENT='";
sql=sql+ParTree+"'";//The 1st time Partree="1".
try
{
m_pTRecordset.CreateInstance("ADODB.Recordset");
m_pTRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
m_pTRecordset->MoveFirst();
if(!m_pTRecordset->adoEOF)
{
while(!m_pTRecordset->adoEOF)
{
hCurrent = m_oMclass.InsertItem((LPCTSTR)(_bstr_t)(m_pTRecordset->GetCollect("NAME")), hPartItem, NULL);//Insert an item into current parent.
curID=CClassDlg::VariantToCString(m_pTRecordset->GetCollect("ID"));
if (CClassDlg::TreeSumRecordCount(curID)>0)
AddSubTree(CClassDlg::VariantToCString(m_pTRecordset->GetCollect("ID")),hCurrent);//Recursion.
if (!m_pTRecordset->adoEOF)
m_pTRecordset->MoveNext();
}
}
m_pTRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("[商品资料]读取子树出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
void CMaterielDlg::AddTree()
{
TVINSERTSTRUCT tvInsert;
HTREEITEM hParent;
_RecordsetPtr m_pTRecordset; //Must define it in function!!!Becourse this is a RECURSION function.
CString sroot,sql;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
// tvInsert.item.pszText = _T("root");
hParent = m_oMclass.InsertItem(&tvInsert);//HTREEITEM of root.
sql="SELECT * FROM MATERIELCLASS where ID=1";
try
{
m_pTRecordset.CreateInstance("ADODB.Recordset");
m_pTRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
if(!m_pTRecordset->adoEOF)
{
sroot=(LPTSTR)(_bstr_t)m_pTRecordset->GetCollect("NAME");//Insert an item into current parent.
}
m_pTRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("[商品资料] 读取树出错:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
// tvInsert.item.pszText = _T("root");
if(sroot!="")
{
// tvInsert.item.pszText =_T("");
hParent = m_oMclass.InsertItem(&tvInsert);//HTREEITEM of root.
AddSubTree("RootClass",hParent);//Here strChildTree just must not equal "root",can be anyother string.
}
// AddSubTree("1",hParent);//Here strChildTree just must not equal "root",can be anyother string.
m_oMclass.Expand(hParent,TVE_EXPAND);
}
BOOL CMaterielDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//设置list控件的文字和背景颜色
m_oListmateriel.SetBkColor(RGB(255,255,255));
m_oListmateriel.SetTextBkColor(RGB(161,223,212));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -