MaterielDlg.cpp
上传用户:sztwq510
上传日期:2013-06-26
资源大小:4045k
文件大小:15k
源码类别:

酒店行业

开发平台:

Java

  1. // MaterielDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyPos.h"
  5. #include "MaterielDlg.h"
  6. #include "ClassDlg.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CMaterielDlg dialog
  14. extern CMyPosApp theApp;
  15. CMaterielDlg::CMaterielDlg(CWnd* pParent /*=NULL*/)
  16. : CDialog(CMaterielDlg::IDD, pParent)
  17. {
  18. //{{AFX_DATA_INIT(CMaterielDlg)
  19. m_mname = _T("");
  20. //}}AFX_DATA_INIT
  21. }
  22. void CMaterielDlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(CMaterielDlg)
  26. DDX_Control(pDX, IDC_LIST_MATERIEL, m_oListmateriel);
  27. DDX_Control(pDX, IDC_TREE_MCLASS, m_oMclass);
  28. DDX_Control(pDX, IDC_CHECK_SUPPLY, m_oSupply);
  29. DDX_Control(pDX, IDC_EDIT_MSHOPCODE, m_oMshopcode);
  30. DDX_Control(pDX, IDC_EDIT_MPRICE, m_oMprice);
  31. DDX_Control(pDX, IDC_EDIT_MNAME, m_oMname);
  32. DDX_Control(pDX, IDC_EDIT_MBARCODE, m_oMbarcode);
  33. DDX_Control(pDX, IDC_COMBO_MDISCOUNT, m_oMdiscount);
  34. DDX_Text(pDX, IDC_EDIT_MNAME, m_mname);
  35. //}}AFX_DATA_MAP
  36. }
  37. BEGIN_MESSAGE_MAP(CMaterielDlg, CDialog)
  38. //{{AFX_MSG_MAP(CMaterielDlg)
  39. ON_BN_CLICKED(IDC_BUTTON_MSAVE, OnButtonMsave)
  40. ON_BN_CLICKED(IDC_BUTTON_MADD, OnButtonMadd)
  41. ON_BN_CLICKED(IDC_BUTTON_MDEL, OnButtonMdel)
  42. ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_MCLASS, OnSelchangedTreeMclass)
  43. ON_NOTIFY(NM_CLICK, IDC_LIST_MATERIEL, OnClickListMateriel)
  44. ON_WM_SHOWWINDOW()
  45. //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CMaterielDlg message handlers
  49. void CMaterielDlg::OnButtonMsave() 
  50. {
  51. CString sql="select * from Materiel";
  52. CString snewid,sname,sdiscount,sprice,ssupply,sshopcode,sbarcode;
  53. CString sMID;
  54. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  55. UpdateData();
  56. //检查数据完整性
  57. m_oMname.GetWindowText(sname);//Get the current text in edit.
  58. m_oMprice.GetWindowText(sprice);
  59. if(sname==""||sprice=="")
  60. {
  61. AfxMessageBox("请输入商品名称、价格!");
  62. return;
  63. }
  64. m_oMname.GetWindowText(sname);//Get the current text in edit.
  65. int discount=m_oMdiscount.GetCurSel();
  66. sdiscount.Format("%d",discount);
  67. if(m_oSupply.GetCheck()==0) ssupply="0";
  68. if(m_oSupply.GetCheck()==1) ssupply="1";
  69. m_oMprice.GetWindowText(sprice);
  70. m_oMshopcode.GetWindowText(sshopcode);
  71. m_oMbarcode.GetWindowText(sbarcode);
  72. if(lNewID>lMID)
  73. {
  74. if(bRepeat(sname)==TRUE)
  75. {
  76. AfxMessageBox("商品名称重复,请重新输入!");
  77. return;
  78. }
  79. snewid.Format("%d",lNewID);
  80. try
  81. m_pRecordset.CreateInstance("ADODB.Recordset");
  82. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  83.    m_pRecordset->AddNew();//insert the current into database.
  84. m_pRecordset->PutCollect("ID",_variant_t(snewid));
  85. m_pRecordset->PutCollect("NAME",_variant_t(sname));
  86. m_pRecordset->PutCollect("DISCOUNT",_variant_t(sdiscount));//Here m_discount must be a float.
  87. m_pRecordset->PutCollect("SUPPLY",_variant_t(ssupply));
  88. m_pRecordset->PutCollect("PRICE",_variant_t(sprice));
  89. m_pRecordset->PutCollect("SHOPCODE",_variant_t(sshopcode));
  90. m_pRecordset->PutCollect("BARCODE",_variant_t(sbarcode));
  91. m_pRecordset->PutCollect("CLASSID",_variant_t(sclassid));
  92. m_pRecordset->Update();//保存到库中    
  93. m_pRecordset->Close();
  94. UpdateData(FALSE);
  95. }
  96. catch(_com_error e)///捕捉异常
  97. {
  98. CString temp;
  99. temp.Format("增加商品出错:%s",e.ErrorMessage());
  100. AfxMessageBox(temp);
  101. return;
  102. }
  103. }
  104. else if(AfxMessageBox("确定修改商品资料吗?",MB_YESNO)==IDYES)
  105. {
  106. _variant_t RecordsAffected;
  107. sMID.Format("%d",lMID);
  108. sql="Update MATERIEL set NAME='"+sname+
  109. "',PRICE="+sprice+
  110. ",DISCOUNT="+sdiscount+
  111. ",SHOPCODE='"+sshopcode+
  112. "',BARCODE='"+sbarcode+
  113. "',SUPPLY="+ssupply+" where ID="+sMID+"";
  114. try
  115. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  116. UpdateData(FALSE);
  117. }
  118. catch(_com_error e)///捕捉异常
  119. {
  120. CString temp;
  121. temp.Format("修改商品资料出错:%s",e.ErrorMessage());
  122. AfxMessageBox(temp);
  123. return;
  124. }
  125. }
  126. CString sql1="Select * from MATERIEL where CLASSID="+sclassid+"";
  127. ReadtoList(sql1);
  128. }
  129. long CMaterielDlg::GenNewID()
  130. {
  131. CString sql;
  132. long NewID;
  133. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  134. sql="SELECT Max(ID) FROM MATERIEL";
  135. try
  136. m_pRecordset.CreateInstance("ADODB.Recordset");
  137. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  138. _variant_t vIndex = (long)0;//How to get field value in only 1 record and 1 field condition.
  139. _variant_t vtemp = m_pRecordset->GetCollect(vIndex);
  140. if(vtemp.lVal>0) 
  141. NewID =(long)(m_pRecordset->GetCollect(vIndex))+1;///取得第一个字段的值(MAX ID)加1后放入id变量.
  142. else
  143. NewID=1;
  144. m_pRecordset->Close();
  145. }
  146. catch(_com_error e)///捕捉异常
  147. {
  148. CString stemp;
  149. stemp.Format("获取类别ID最大值出错:%s",e.ErrorMessage());
  150. AfxMessageBox(stemp);
  151. }
  152. return NewID;
  153. }
  154. void CMaterielDlg::AddSubTree(CString ParTree, HTREEITEM hPartItem)
  155. {
  156. HTREEITEM hCurrent;
  157. CString sql;
  158. CString curID;
  159. _RecordsetPtr m_pTRecordset; //Must define it in function!!!Becourse this is a RECURSION function.
  160. sql="SELECT * FROM MATERIELCLASS where PARENT='";
  161. sql=sql+ParTree+"'";//The 1st time Partree="1".
  162. try
  163. {
  164. m_pTRecordset.CreateInstance("ADODB.Recordset");
  165. m_pTRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  166. m_pTRecordset->MoveFirst();
  167. if(!m_pTRecordset->adoEOF)
  168. {
  169. while(!m_pTRecordset->adoEOF)
  170. {
  171. hCurrent = m_oMclass.InsertItem((LPCTSTR)(_bstr_t)(m_pTRecordset->GetCollect("NAME")), hPartItem, NULL);//Insert an item into current parent.
  172. curID=CClassDlg::VariantToCString(m_pTRecordset->GetCollect("ID"));
  173. if (CClassDlg::TreeSumRecordCount(curID)>0)
  174. AddSubTree(CClassDlg::VariantToCString(m_pTRecordset->GetCollect("ID")),hCurrent);//Recursion.
  175. if (!m_pTRecordset->adoEOF)
  176. m_pTRecordset->MoveNext();
  177. }
  178. }
  179. m_pTRecordset->Close();
  180. }
  181. catch(_com_error e)///捕捉异常
  182. {
  183. CString temp;
  184. temp.Format("[商品资料]读取子树出错:%s",e.ErrorMessage());
  185. AfxMessageBox(temp);
  186. return;
  187. }
  188. }
  189. void CMaterielDlg::AddTree()
  190. {
  191. TVINSERTSTRUCT tvInsert;
  192. HTREEITEM hParent;
  193. _RecordsetPtr m_pTRecordset; //Must define it in function!!!Becourse this is a RECURSION function.
  194. CString sroot,sql;
  195. tvInsert.hParent = NULL;
  196. tvInsert.hInsertAfter = NULL;
  197. tvInsert.item.mask = TVIF_TEXT;
  198. // tvInsert.item.pszText = _T("root");
  199. hParent = m_oMclass.InsertItem(&tvInsert);//HTREEITEM of root.
  200. sql="SELECT * FROM MATERIELCLASS where ID=1";
  201. try
  202. {
  203. m_pTRecordset.CreateInstance("ADODB.Recordset");
  204. m_pTRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  205. if(!m_pTRecordset->adoEOF)
  206. {
  207. sroot=(LPTSTR)(_bstr_t)m_pTRecordset->GetCollect("NAME");//Insert an item into current parent.
  208. }
  209. m_pTRecordset->Close();
  210. }
  211. catch(_com_error e)///捕捉异常
  212. {
  213. CString errormessage;
  214. errormessage.Format("[商品资料] 读取树出错:%s",e.ErrorMessage());
  215. AfxMessageBox(errormessage);
  216. }
  217. // tvInsert.item.pszText = _T("root");
  218. if(sroot!="")
  219. {
  220. // tvInsert.item.pszText =_T("");
  221. hParent = m_oMclass.InsertItem(&tvInsert);//HTREEITEM of root.
  222. AddSubTree("RootClass",hParent);//Here strChildTree just must not equal "root",can be anyother string.
  223. }
  224. // AddSubTree("1",hParent);//Here strChildTree just must not equal "root",can be anyother string.
  225. m_oMclass.Expand(hParent,TVE_EXPAND);
  226. }
  227. BOOL CMaterielDlg::OnInitDialog() 
  228. {
  229. CDialog::OnInitDialog();
  230. //设置list控件的文字和背景颜色
  231. m_oListmateriel.SetBkColor(RGB(255,255,255));
  232. m_oListmateriel.SetTextBkColor(RGB(161,223,212));
  233. //清空list控件的数据
  234. for(int delcolumn=100;delcolumn>=0;delcolumn--)
  235. m_oListmateriel.DeleteColumn(delcolumn);
  236. //设置list对话框的列
  237. DWORD dwStyle;
  238. RECT rect;
  239. LV_COLUMN lvc;
  240. dwStyle = m_oListmateriel.GetStyle();
  241. dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_SHOWSELALWAYS ;
  242. m_oListmateriel.SetExtendedStyle(dwStyle);
  243. m_oListmateriel.GetClientRect(&rect);
  244. lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH |LVCF_FMT;
  245. lvc.fmt=LVCFMT_LEFT;
  246. lvc.iSubItem = 0;
  247. lvc.pszText = _T("类别");
  248. lvc.cx = 80;
  249. m_oListmateriel.InsertColumn(1,&lvc);
  250. lvc.iSubItem = 1;
  251. lvc.pszText = _T("名称");
  252. lvc.cx = 150;
  253. m_oListmateriel.InsertColumn(2,&lvc);
  254. lvc.iSubItem = 2;
  255. lvc.pszText = _T("价格");
  256. lvc.cx = 65;
  257. m_oListmateriel.InsertColumn(3,&lvc);
  258. //Read the 1st class's materiels to list.
  259. // CString sql="Select * from MATERIEL where CLASSID=1";
  260. // ReadtoList(sql);
  261. return TRUE;  // return TRUE unless you set the focus to a control
  262.               // EXCEPTION: OCX Property Pages should return FALSE
  263. }
  264. void CMaterielDlg::OnButtonMadd() 
  265. {
  266. m_oMname.SetWindowText("");
  267. m_oMprice.SetWindowText("");
  268. m_oMshopcode.SetWindowText("");
  269. m_oMbarcode.SetWindowText("");
  270. lNewID=GenNewID();
  271. }
  272. void CMaterielDlg::OnButtonMdel() 
  273. {
  274. CString sql, stemp,ssclassid;
  275. _RecordsetPtr m_pRecordset;
  276. long lclassid;
  277. m_oMname.GetWindowText(stemp);//得到选中项的文本
  278. if(stemp=="")
  279. return;
  280. sql="select * from MATERIEL where NAME='"+stemp+"'";
  281. try
  282. m_pRecordset.CreateInstance("ADODB.Recordset");
  283. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  284. lclassid=(long)(m_pRecordset->GetCollect("CLASSID"));
  285. if(AfxMessageBox("确定删除此商品吗?",MB_YESNO)==IDYES)
  286. {
  287. m_pRecordset->Delete(adAffectCurrent);///删除当前记录
  288. }
  289. else
  290. return;
  291. m_pRecordset->Update();   
  292. }
  293. catch(_com_error e)///捕捉异常
  294. {
  295. CString temp;
  296. temp.Format("删除商品出错:%s",e.ErrorMessage());
  297. AfxMessageBox(temp);
  298. return;
  299. }
  300. ssclassid.Format("%d",lclassid);
  301. CString sql1="Select * from MATERIEL Where CLASSID="+ssclassid+"";
  302. ReadtoList(sql1);
  303. }
  304. void CMaterielDlg::ReadtoList(CString sql)
  305. {
  306. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  307. //删除所有list中的数据。
  308. m_oListmateriel.DeleteAllItems();
  309. int numline=0;
  310. try
  311. {
  312. m_pRecordset.CreateInstance("ADODB.Recordset");
  313. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  314. LV_ITEM lvitem;
  315. lvitem.pszText="";
  316. lvitem.mask=LVIF_TEXT;
  317. lvitem.iSubItem=0;
  318. while(!m_pRecordset->adoEOF)
  319. {
  320. lvitem.iItem=numline;
  321. m_oListmateriel.InsertItem(&lvitem);
  322. //读出数据写入到list中
  323. // m_oListmateriel.SetItemText(numline,0,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("CLASSID"));
  324. m_oListmateriel.SetItemText(numline,0,curtext);
  325. m_oListmateriel.SetItemText(numline,1,(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME"));
  326. float fprice=(float)m_pRecordset->GetCollect("PRICE");
  327. CString sprice;
  328. sprice.Format("%.2f",fprice);
  329. m_oListmateriel.SetItemText(numline,2,sprice);
  330. numline++;
  331. m_pRecordset->MoveNext();
  332. }
  333. m_pRecordset->Close();
  334. }
  335. catch(_com_error e)///捕捉异常
  336. {
  337. CString temp;
  338. temp.Format("读取商品到列表出错:%s",e.ErrorMessage());
  339. AfxMessageBox(temp);
  340. return;
  341. }
  342. //设置标题
  343. CString temp;
  344. temp.Format("共有%d条记录",numline);
  345. SetWindowText(temp);
  346. }
  347. void CMaterielDlg::OnSelchangedTreeMclass(NMHDR* pNMHDR, LRESULT* pResult) 
  348. {
  349. NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  350. CString sql,sql1;
  351. long lclassid;
  352. _RecordsetPtr m_pRecordset; //Must define it in function!!!
  353. HTREEITEM hCurItem;
  354. //Clear text in edit.
  355. m_oMname.SetWindowText("");
  356. m_oMprice.SetWindowText("");
  357. m_oMshopcode.SetWindowText("");
  358. m_oMbarcode.SetWindowText("");
  359. lNewID=0;//Refresh lNewID's value.
  360. hCurItem=m_oMclass.GetSelectedItem ();//Get Current item and its sub items.
  361. curtext=m_oMclass.GetItemText(hCurItem);//Read current item text into edit control.
  362. sql="SELECT * FROM MATERIELCLASS where NAME='"+curtext+"'";
  363. try
  364. {
  365. m_pRecordset.CreateInstance("ADODB.Recordset");
  366. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  367. if(!m_pRecordset->adoEOF)
  368. {
  369. lclassid=(long)(m_pRecordset->GetCollect("ID"));
  370. }
  371. m_pRecordset->Close();
  372. }
  373. catch(_com_error e)///捕捉异常
  374. {
  375. CString temp;
  376. temp.Format("获取类别名称出错:%s",e.ErrorMessage());
  377. AfxMessageBox(temp);
  378. return;
  379. }
  380. sclassid.Format("%d",lclassid);
  381. sql1="Select * from MATERIEL where CLASSID="+sclassid+"";
  382. ReadtoList(sql1);
  383. UpdateData(false);
  384. *pResult = 0;
  385. }
  386. void CMaterielDlg::OnClickListMateriel(NMHDR* pNMHDR, LRESULT* pResult) 
  387. {
  388. CString smname,sshopcode,sbarcode,sprice;
  389. CString sql;
  390. float fprice;
  391. int ndiscount,nsupply;
  392. _RecordsetPtr m_pRecordset; //Must define it in function!!!
  393. m_oMname.SetWindowText("");
  394. m_oMprice.SetWindowText("");
  395. m_oMshopcode.SetWindowText("");
  396. m_oMbarcode.SetWindowText("");
  397. //得到当前选中的行
  398. POSITION pos = m_oListmateriel.GetFirstSelectedItemPosition();
  399. //如果选中一行
  400. if(pos)
  401. {
  402. int nItem = m_oListmateriel.GetNextSelectedItem(pos);
  403. smname=m_oListmateriel.GetItemText(nItem,1);
  404. m_oMname.SetWindowText(smname);
  405. lNewID=0;//Refresh lNewID's value.
  406. sql="Select * from MATERIEL where NAME='"+smname+"'";
  407. try
  408. {
  409. m_pRecordset.CreateInstance("ADODB.Recordset");
  410. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  411. if(!m_pRecordset->adoEOF)
  412. {
  413. lMID=(long)(m_pRecordset->GetCollect("ID"));
  414. fprice=(float)(m_pRecordset->GetCollect("PRICE"));
  415. ndiscount=(long)m_pRecordset->GetCollect("DISCOUNT");
  416. nsupply=(long)m_pRecordset->GetCollect("SUPPLY");
  417. sshopcode=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SHOPCODE");
  418. sbarcode=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("BARCODE");
  419. sprice.Format("%.2f",fprice);
  420. m_oMprice.SetWindowText(sprice);
  421. m_oMshopcode.SetWindowText(sshopcode);
  422. m_oMbarcode.SetWindowText(sbarcode);
  423. m_oMdiscount.SetCurSel(ndiscount);
  424. m_oSupply.SetCheck(nsupply);
  425. }
  426. m_pRecordset->Close();
  427. }
  428. catch(_com_error e)///捕捉异常
  429. {
  430. CString temp;
  431. temp.Format("读取商品资料到控件出错:%s",e.ErrorMessage());
  432. AfxMessageBox(temp);
  433. return;
  434. }
  435. }
  436. *pResult = 0;
  437. }
  438. BOOL CMaterielDlg::bRepeat(CString strFieldValue)
  439. {
  440. CString sql;
  441. BOOL br;
  442. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  443. sql="SELECT * FROM MATERIEL WHERE NAME='";
  444. sql=sql+strFieldValue+"'";
  445. try
  446. m_pRecordset.CreateInstance("ADODB.Recordset");
  447. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  448. if(m_pRecordset->adoEOF)
  449. br=FALSE;
  450. if(!m_pRecordset->adoEOF)
  451. br=TRUE;
  452. m_pRecordset->Close();
  453. }
  454. catch(_com_error e)///捕捉异常
  455. {
  456. CString stemp;
  457. stemp.Format("查找商品名称出错:%s",e.ErrorMessage());
  458. AfxMessageBox(stemp);
  459. }
  460. return br;
  461. }
  462. void CMaterielDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
  463. {
  464. CDialog::OnShowWindow(bShow, nStatus);
  465. m_oMclass.DeleteAllItems();
  466. AddTree();
  467. }