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

酒店行业

开发平台:

Java

  1. // CheckDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyPos.h"
  5. #include "CheckDlg.h"
  6. #include "PosDlg.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CCheckDlg dialog
  14. extern CMyPosApp theApp;
  15. CCheckDlg::CCheckDlg(CWnd* pParent /*=NULL*/)
  16. : CDialog(CCheckDlg::IDD, pParent)
  17. {
  18. //{{AFX_DATA_INIT(CCheckDlg)
  19. //}}AFX_DATA_INIT
  20. }
  21. void CCheckDlg::DoDataExchange(CDataExchange* pDX)
  22. {
  23. CDialog::DoDataExchange(pDX);
  24. //{{AFX_DATA_MAP(CCheckDlg)
  25. DDX_Control(pDX, IDC_EDIT_CONSUME, m_oConsume);
  26. DDX_Control(pDX, IDC_EDIT_CDISCOUNT, m_oCdiscount);
  27. DDX_Control(pDX, IDC_EDIT_RECEIVE, m_oReceive);
  28. DDX_Control(pDX, IDC_EDIT_CHANGE, m_oChange);
  29. DDX_Control(pDX, IDC_EDIT_TOTALM, m_oTotalm);
  30. DDX_Control(pDX, IDC_COMBO_PAYMODE, m_oCombopay);
  31. DDX_Control(pDX, IDC_LIST_CHECKLIST, m_oChecklist);
  32. //}}AFX_DATA_MAP
  33. }
  34. BEGIN_MESSAGE_MAP(CCheckDlg, CDialog)
  35. //{{AFX_MSG_MAP(CCheckDlg)
  36. ON_BN_CLICKED(IDC_BUTTON_CHECKOK, OnButtonCheckok)
  37. ON_EN_CHANGE(IDC_EDIT_RECEIVE, OnChangeEditReceive)
  38. ON_NOTIFY(NM_CLICK, IDC_LIST_CHECKLIST, OnClickListChecklist)
  39. ON_EN_CHANGE(IDC_EDIT_CDISCOUNT, OnChangeEditCdiscount)
  40. ON_BN_CLICKED(IDC_BUTTON_RF, OnButtonRf)
  41. ON_BN_CLICKED(IDC_BUTTON_HANG, OnButtonHang)
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CCheckDlg message handlers
  46. BOOL CCheckDlg::OnInitDialog() 
  47. {
  48. CDialog::OnInitDialog();
  49. //设置list控件的文字和背景颜色
  50. m_oChecklist.SetBkColor(RGB(255,255,255));
  51. m_oChecklist.SetTextBkColor(RGB(161,223,212));
  52. //清空list控件的数据
  53. for(int delcolumn=100;delcolumn>=0;delcolumn--)
  54. m_oChecklist.DeleteColumn(delcolumn);
  55. //设置list对话框的列
  56. DWORD dwStyle;
  57. RECT rect;
  58. LV_COLUMN lvc;
  59. dwStyle = m_oChecklist.GetStyle();
  60. dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_SHOWSELALWAYS ;
  61. m_oChecklist.SetExtendedStyle(dwStyle);
  62. m_oChecklist.GetClientRect(&rect);
  63. lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH |LVCF_FMT;
  64. lvc.fmt=LVCFMT_LEFT;
  65. lvc.iSubItem = 0;
  66. lvc.pszText = _T("商品类别");
  67. lvc.cx = 110;
  68. m_oChecklist.InsertColumn(1,&lvc);
  69. lvc.iSubItem = 1;
  70. lvc.pszText = _T("消费金额");
  71. lvc.cx = 90;
  72. m_oChecklist.InsertColumn(2,&lvc);
  73. lvc.iSubItem = 2;
  74. lvc.pszText = _T("可折扣金额");
  75. lvc.cx = 90;
  76. m_oChecklist.InsertColumn(3,&lvc);
  77. lvc.iSubItem = 3;
  78. lvc.pszText = _T("折扣%");
  79. lvc.cx = 60;
  80. m_oChecklist.InsertColumn(4,&lvc);
  81. lvc.iSubItem = 4;
  82. lvc.pszText = _T("结帐金额");
  83. lvc.cx = 90;
  84. m_oChecklist.InsertColumn(5,&lvc);
  85. //Add paymode to combobox.
  86. _RecordsetPtr m_pRecordset;
  87. CString sql="select * from PAYMODE";
  88. try
  89. {
  90. m_pRecordset.CreateInstance("ADODB.Recordset");
  91. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  92. while(!m_pRecordset->adoEOF)
  93. {
  94. m_oCombopay.AddString((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME"));
  95. m_pRecordset->MoveNext();
  96. }
  97. m_pRecordset->Close();
  98. m_oCombopay.SetCurSel(0);//Select the 1st string of combobox.
  99. }
  100. catch(_com_error e)///捕捉异常
  101. {
  102. CString temp;
  103. temp.Format("[结帐]读取付款方式到组合框出错:%s",e.ErrorMessage());
  104. AfxMessageBox(temp);
  105. }
  106. //Read class consume to list.
  107. ReadtoList(theApp.scallid);
  108. //Sum bill Items money from database.
  109. CString stotal;
  110. float ftotal=0;
  111. sql="Select SUM(ITEMTOTAL) as SSS from SALEDETAIL where BILLID='"+theApp.scallid+"'";
  112. try
  113. m_pRecordset.CreateInstance("ADODB.Recordset");
  114. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  115. _variant_t vtemp = m_pRecordset->GetCollect("SSS");
  116. if(vtemp.dblVal>0) 
  117. ftotal=(float)m_pRecordset->GetCollect("SSS");
  118. else
  119. ftotal=0;
  120. stotal.Format("%.2f",ftotal);
  121. m_oTotalm.SetWindowText(stotal);
  122. m_oConsume.SetWindowText(stotal);
  123. }
  124. catch(_com_error e)///捕捉异常
  125. {
  126. CString temp;
  127. temp.Format("[结帐]计算单据商品金额出错:%s",e.ErrorMessage());
  128. AfxMessageBox(temp);
  129. }
  130. return TRUE;  // return TRUE unless you set the focus to a control
  131.               // EXCEPTION: OCX Property Pages should return FALSE
  132. }
  133. void CCheckDlg::OnButtonCheckok() 
  134. {
  135. CString stotal,sbilltotal,spayID,sql,spaymode,snowtime;
  136. long lpayID;
  137. _RecordsetPtr m_pRecordset;
  138. m_oConsume.GetWindowText(stotal);
  139. m_oTotalm.GetWindowText(sbilltotal);
  140. int nselect=m_oCombopay.GetCurSel();
  141. m_oCombopay.GetLBText(nselect,spaymode);
  142. lpayID=GetPaymodeID(spaymode);
  143. spayID.Format("%d",lpayID);
  144. if(fchange<0)
  145. return;
  146. //得到系统时间
  147. CTime now=CTime::GetCurrentTime();
  148. snowtime=now.Format(_T("%Y-%m-%d %H:%M:%S"));
  149. sql="Update SALEBILL set ENDDATE='"+snowtime+"',SALES='"+theApp.name+"',TOTAL="
  150. +stotal+",ACTTOTAL="+sbilltotal+",STATUS='已结帐',PAYMODE="+spayID+" where ID='"+theApp.scallid+"'";
  151. try
  152. _variant_t RecordsAffected;
  153. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  154. }
  155. catch(_com_error e)///捕捉异常
  156. {
  157. CString temp;
  158. temp.Format("[结帐]单据头更新数据库出错:%s",e.ErrorMessage());
  159. AfxMessageBox(temp);
  160. return;
  161. }
  162. //output the checkout time and paymode to the print.
  163. theApp.snowtimep=snowtime;
  164. theApp.spaymodep=spaymode;
  165. theApp.sconsume=stotal;
  166. theApp.sactsum=sbilltotal;
  167. //Insert data into PAYDETAIL.
  168. CString sclass,scontotal,sdiscount,sacttotal,svaltotal;
  169. int nItemCount=m_oChecklist.GetItemCount();//表项总数
  170. for(int i=0;i<nItemCount;i++)
  171. {
  172. sclass=m_oChecklist.GetItemText(i,0);
  173. scontotal=m_oChecklist.GetItemText(i,1);
  174. sdiscount=m_oChecklist.GetItemText(i,3);
  175. sacttotal=m_oChecklist.GetItemText(i,4);
  176. float fvaltotal=atof(scontotal)-atof(sacttotal);
  177. svaltotal.Format("%.2f",fvaltotal);
  178. sql="Insert into PAYDETAIL(BILLID,CLASS,TOTAL,DISCOUNT,ACTTOTAL,VALTOTAL) values('"+theApp.scallid+
  179. "','"+sclass+"',"+scontotal+",'"+sdiscount+"',"+sacttotal+","+svaltotal+")";
  180. try
  181. _variant_t RecordsAffected;
  182. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  183. }
  184. catch(_com_error e)///捕捉异常
  185. {
  186. CString temp;
  187. temp.Format("[结帐]付款明细(PAYDETAIL)插入数据出错:%s",e.ErrorMessage());
  188. AfxMessageBox(temp);
  189. return;
  190. }
  191. }
  192. //When the check button is click down,send a message(WM_CHECKOUT) to the CPosDlg.
  193. LRESULT Res=::SendMessage(theApp.pWnd, WM_CHECKOUT, 0, 0);
  194. CDialog::OnOK();
  195. }
  196. long CCheckDlg::GetPaymodeID(CString payname)
  197. {
  198. long paymodeID;
  199. _RecordsetPtr m_pRecordset;
  200. CString sql="Select * from PAYMODE where NAME='"+payname+"'";
  201. try
  202. m_pRecordset.CreateInstance("ADODB.Recordset");
  203. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  204. paymodeID=(long)m_pRecordset->GetCollect("ID");
  205. }
  206. catch(_com_error e)///捕捉异常
  207. {
  208. CString temp;
  209. temp.Format("[结帐]获取付款方式ID出错:%s",e.ErrorMessage());
  210. AfxMessageBox(temp);
  211. }
  212. return paymodeID;
  213. }
  214. void CCheckDlg::OnChangeEditReceive() 
  215. {
  216. // TODO: If this is a RICHEDIT control, the control will not
  217. // send this notification unless you override the CDialog::OnInitDialog()
  218. // function and call CRichEditCtrl().SetEventMask()
  219. // with the ENM_CHANGE flag ORed into the mask.
  220. CString sreceive,schange,stotal;
  221. m_oChange.SetWindowText("");
  222. m_oReceive.GetWindowText(sreceive);
  223. m_oTotalm.GetWindowText(stotal);
  224. fchange=atof(sreceive)-atof(stotal);
  225. if(fchange>=0)
  226. {
  227. schange.Format("%.2f",fchange);
  228. m_oChange.SetWindowText(schange);
  229. }
  230. }
  231. void CCheckDlg::ReadtoList(CString sbillid)
  232. {
  233. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  234. CString sql,sclassname,sclassname1,sctotal;
  235. long lclassid;
  236. float fctotal=0;
  237. sql="Select Sum(ITEMTOTAL) as XXX,CLASSID from SALEDETAIL where BILLID='"+sbillid+"' group by CLASSID ";
  238. //删除所有list中的数据。
  239. m_oChecklist.DeleteAllItems();
  240. int numline=0;
  241. try
  242. {
  243. m_pRecordset.CreateInstance("ADODB.Recordset");
  244. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  245. LV_ITEM lvitem;
  246. lvitem.pszText="";
  247. lvitem.mask=LVIF_TEXT;
  248. lvitem.iSubItem=0;
  249. while(!m_pRecordset->adoEOF)
  250. {
  251. lvitem.iItem=numline;
  252. m_oChecklist.InsertItem(&lvitem);
  253. //读出数据写入到list中
  254. lclassid= (long)m_pRecordset->GetCollect("CLASSID");
  255. sclassname=GetClassName(lclassid);
  256. sclassname1=sclassname.Left(sclassname.GetLength()-4);
  257. m_oChecklist.SetItemText(numline,0,sclassname1);//Read class name to 0 column.
  258. fctotal=(float)m_pRecordset->GetCollect("XXX");
  259. sctotal.Format("%.2f",fctotal);
  260. m_oChecklist.SetItemText(numline,1,sctotal);//Read class consume total to 1st column.
  261. if(sclassname.Right(1)=="0")
  262. m_oChecklist.SetItemText(numline,2,"0.00");//Read class consume discount total to 2nd column.
  263. else
  264. m_oChecklist.SetItemText(numline,2,sctotal);
  265. m_oChecklist.SetItemText(numline,3,"100");
  266. m_oChecklist.SetItemText(numline,4,sctotal);//Read class consume act total to 4th column.
  267. numline++;
  268. m_pRecordset->MoveNext();
  269. }
  270. m_pRecordset->Close();
  271. }
  272. catch(_com_error e)///捕捉异常
  273. {
  274. CString temp;
  275. temp.Format("[结帐]列表读入商品出错:%s",e.ErrorMessage());
  276. AfxMessageBox(temp);
  277. return;
  278. }
  279. }
  280. CString CCheckDlg::GetClassName(long lclassid)
  281. {
  282. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  283. CString sql,sclassid,sname,sdiscount;
  284. sclassid.Format("%d",lclassid);
  285. sql="Select NAME,DISCOUNT from MATERIELCLASS where ID="+sclassid+"";
  286. try
  287. {
  288. m_pRecordset.CreateInstance("ADODB.Recordset");
  289. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  290. if(!m_pRecordset->adoEOF)
  291. {
  292. sname=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME");
  293. sdiscount=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("DISCOUNT");
  294. sname=sname+"xxx"+sdiscount;
  295. }
  296. }
  297. catch(_com_error e)///捕捉异常
  298. {
  299. CString temp;
  300. temp.Format("[结帐]获取类别名称出错:%s",e.ErrorMessage());
  301. AfxMessageBox(temp);
  302. }
  303. return sname;
  304. }
  305. void CCheckDlg::OnClickListChecklist(NMHDR* pNMHDR, LRESULT* pResult) 
  306. {
  307. //得到当前选中的行
  308. POSITION pos = m_oChecklist.GetFirstSelectedItemPosition();
  309. //如果选中一行
  310. if(pos)
  311. {
  312. int nItem = m_oChecklist.GetNextSelectedItem(pos);
  313. CString sdiscount=m_oChecklist.GetItemText(nItem,3);
  314. m_oCdiscount.SetWindowText(sdiscount);
  315. }
  316. *pResult = 0;
  317. }
  318. void CCheckDlg::OnChangeEditCdiscount() 
  319. {
  320. // TODO: If this is a RICHEDIT control, the control will not
  321. // send this notification unless you override the CDialog::OnInitDialog()
  322. // function and call CRichEditCtrl().SetEventMask()
  323. // with the ENM_CHANGE flag ORed into the mask.
  324. CString sdiscountsum,sdiscount,ssum;
  325. float fdiscountsum,fsum;
  326. long ldiscount;
  327. LV_ITEM lvitem;
  328. lvitem.pszText="";
  329. lvitem.mask=LVIF_TEXT;
  330. //得到当前选中的行
  331. POSITION pos = m_oChecklist.GetFirstSelectedItemPosition();
  332. //如果选中一行
  333. if(pos)
  334. {
  335. int nItem = m_oChecklist.GetNextSelectedItem(pos);
  336. sdiscountsum=m_oChecklist.GetItemText(nItem,2);
  337. fdiscountsum=atof(sdiscountsum);
  338. m_oCdiscount.GetWindowText(sdiscount);
  339. ldiscount=atol(sdiscount);
  340. if(ldiscount>100||ldiscount<0)
  341. {
  342. MessageBox("请确定折扣大于0并且小于100!");
  343. return;
  344. }
  345. if(ldiscount<VerifyDicsount())
  346. {
  347. CString temp;
  348. temp.Format("折扣不能小于%d!",VerifyDicsount());
  349. AfxMessageBox(temp);
  350. return;
  351. }
  352. fsum=ldiscount*fdiscountsum/100;
  353. ssum.Format("%.2f",fsum);
  354. if(fdiscountsum==0)//If the class could not be discount.
  355. {
  356. sdiscountsum=m_oChecklist.GetItemText(nItem,1);
  357. ssum=sdiscountsum;
  358. }
  359. lvitem.iItem=nItem;
  360. m_oChecklist.InsertItem(&lvitem);
  361. m_oChecklist.SetItemText(nItem,3,sdiscount);
  362. m_oChecklist.SetItemText(nItem,4,ssum);
  363. }
  364. float ftotal=0;
  365. int nItemCount=m_oChecklist.GetItemCount();//表项总数
  366. for(int i=0;i<nItemCount;i++)
  367. {
  368. sdiscountsum=m_oChecklist.GetItemText(i,4);
  369. fdiscountsum=atof(sdiscountsum);
  370. ftotal=ftotal+fdiscountsum;
  371. }
  372. CString stotal;
  373. stotal.Format("%.2f",ftotal);
  374. m_oTotalm.SetWindowText(stotal);
  375. }
  376. void CCheckDlg::OnButtonRf() 
  377. {
  378. CString sql;
  379. if(!theApp.VerifyPower("RF"))
  380. {
  381. AfxMessageBox("没有权限RF单据!");
  382. return;
  383. }
  384. //得到系统时间
  385. CTime now=CTime::GetCurrentTime();
  386. CString snowtime=now.Format(_T("%Y-%m-%d %H:%M:%S"));
  387. sql="Update SALEBILL set SALES='"+theApp.name+"',ENDDATE='"+snowtime+"',STATUS='已结帐', ACTTOTAL=0, PAYMODE=88 where ID='"+theApp.scallid+"'";
  388. try
  389. _variant_t RecordsAffected;
  390. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  391. }
  392. catch(_com_error e)///捕捉异常
  393. {
  394. CString temp;
  395. temp.Format("[结帐]RF更新数据库出错:%s",e.ErrorMessage());
  396. AfxMessageBox(temp);
  397. return;
  398. }
  399. //When the check button is click down,send a message(WM_CHECKOUT) to the CPosDlg.
  400. LRESULT Res=::SendMessage(theApp.pWnd, WM_CHECKOUT, 0, 0);
  401. EndDialog(1);//Close the dialog.
  402. }
  403. void CCheckDlg::OnButtonHang() 
  404. {
  405. if(!theApp.VerifyPower("Hang"))
  406. {
  407. AfxMessageBox("没有权限挂帐!");
  408. return;
  409. }
  410. CString sql;
  411. sql="Update SALEBILL set STATUS='挂帐' where ID='"+theApp.scallid+"'";
  412. try
  413. _variant_t RecordsAffected;
  414. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  415. }
  416. catch(_com_error e)///捕捉异常
  417. {
  418. CString temp;
  419. temp.Format("[结帐]挂帐更新数据库出错:%s",e.ErrorMessage());
  420. AfxMessageBox(temp);
  421. return;
  422. }
  423. EndDialog(1);//Close the dialog.
  424. }
  425. long CCheckDlg::VerifyDicsount()
  426. {
  427. CString sql;
  428. _RecordsetPtr m_pRecordset;
  429. long ldiscount;
  430. sql="Select DISCOUNT from USERS where NAME='"+theApp.name+"'";
  431. try
  432. {
  433. m_pRecordset.CreateInstance("ADODB.Recordset");
  434. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  435. if(!m_pRecordset->adoEOF)
  436. ldiscount=(long)m_pRecordset->GetCollect("DISCOUNT");
  437. }
  438. catch(_com_error e)///捕捉异常
  439. {
  440. CString temp;
  441. temp.Format("读取用户权限出错:%s",e.ErrorMessage());
  442. AfxMessageBox(temp);
  443. return -1;
  444. }
  445. return ldiscount;
  446. }