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

酒店行业

开发平台:

Java

  1. // PosDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyPos.h"
  5. #include "PosDlg.h"
  6. #include "SelectDlg.h"
  7. #include "ModifyqaDlg.h"
  8. #include "CallDlg.h"
  9. #include "CheckDlg.h"
  10. #include "OffdutyDlg.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CPosDlg dialog
  18. extern CMyPosApp theApp;
  19. CPosDlg::CPosDlg(CWnd* pParent /*=NULL*/)
  20. : CDialog(CPosDlg::IDD, pParent)
  21. {
  22. //{{AFX_DATA_INIT(CPosDlg)
  23. //}}AFX_DATA_INIT
  24. }
  25. void CPosDlg::DoDataExchange(CDataExchange* pDX)
  26. {
  27. CDialog::DoDataExchange(pDX);
  28. //{{AFX_DATA_MAP(CPosDlg)
  29. DDX_Control(pDX, IDC_EDIT_ACTTOTAL, m_oActtotal);
  30. DDX_Control(pDX, IDC_EDIT_TOTAL, m_oTotal);
  31. DDX_Control(pDX, IDC_BUTTON_SELMATERIEL, m_oButton);
  32. DDX_Control(pDX, IDC_LIST_FORM, m_oFormlist);
  33. DDX_Control(pDX, IDC_EDIT_BEGINTIME, m_oBegintime);
  34. DDX_Control(pDX, IDC_EDIT_ENDTIME, m_oEndtime);
  35. DDX_Control(pDX, IDC_EDIT_CKECK, m_oCheck);
  36. DDX_Control(pDX, IDC_EDIT_STATUS, m_oStatus);
  37. DDX_Control(pDX, IDC_EDIT_PEASONS, m_oPeasons);
  38. DDX_Control(pDX, IDC_EDIT_TABLENO, m_oTableno);
  39. DDX_Control(pDX, IDC_LIST_BILL, m_oListbill);
  40. //}}AFX_DATA_MAP
  41. }
  42. BEGIN_MESSAGE_MAP(CPosDlg, CDialog)
  43. //{{AFX_MSG_MAP(CPosDlg)
  44. ON_BN_CLICKED(IDC_BUTTON_ADDF, OnButtonAddf)
  45. ON_LBN_SELCHANGE(IDC_LIST_BILL, OnSelchangeListBill)
  46. ON_BN_CLICKED(IDC_BUTTON_SELMATERIEL, OnButtonSelmateriel)
  47. ON_NOTIFY(NM_DBLCLK, IDC_LIST_FORM, OnDblclkListForm)
  48. ON_BN_CLICKED(IDC_BUTTON_CALLF, OnButtonCallf)
  49. ON_BN_CLICKED(IDC_BUTTON_DELF, OnButtonDelf)
  50. ON_BN_CLICKED(IDC_BUTTON_HIDEF, OnButtonHidef)
  51. ON_BN_CLICKED(IDC_BUTTON_CHECKF, OnButtonCheckf)
  52. ON_EN_KILLFOCUS(IDC_EDIT_TABLENO, OnKillfocusEditTableno)
  53. ON_EN_KILLFOCUS(IDC_EDIT_PEASONS, OnKillfocusEditPeasons)
  54. ON_BN_CLICKED(IDC_BUTTON_OFF, OnButtonOff)
  55. ON_BN_CLICKED(IDC_BUTTON_FINDF, OnButtonFindf)
  56. ON_BN_CLICKED(IDC_BUTTON_LOCALP, OnButtonLocalp)
  57. //}}AFX_MSG_MAP
  58. ON_MESSAGE(WM_APPLY,OnMyMessage)//Map WM_APPLY message to OnMyMessage function.
  59. ON_MESSAGE(WM_CHECKOUT,OnMyCheckMessage)//Map WM_CHECKOUT message to OnMyMessage function.
  60. END_MESSAGE_MAP()
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CPosDlg message handlers
  63. BOOL CPosDlg::OnInitDialog() 
  64. {
  65. CDialog::OnInitDialog();
  66. //设置list控件的文字和背景颜色
  67. m_oFormlist.SetBkColor(RGB(255,255,255));
  68. m_oFormlist.SetTextBkColor(RGB(161,223,212));
  69. //清空list控件的数据
  70. for(int delcolumn=100;delcolumn>=0;delcolumn--)
  71. m_oFormlist.DeleteColumn(delcolumn);
  72. //设置list对话框的列
  73. DWORD dwStyle;
  74. RECT rect;
  75. LV_COLUMN lvc;
  76. dwStyle = m_oFormlist.GetStyle();
  77. dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT |LVS_SHOWSELALWAYS |LVS_EDITLABELS ;
  78. m_oFormlist.SetExtendedStyle(dwStyle);
  79. m_oFormlist.GetClientRect(&rect);
  80. lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH |LVCF_FMT;
  81. lvc.fmt=LVCFMT_LEFT;
  82. lvc.iSubItem = 0;
  83. lvc.pszText = _T("序号");
  84. lvc.cx = 70;
  85. m_oFormlist.InsertColumn(1,&lvc);
  86. lvc.iSubItem = 1;
  87. lvc.pszText = _T("名称");
  88. lvc.cx = 220;
  89. m_oFormlist.InsertColumn(2,&lvc);
  90. lvc.iSubItem = 2;
  91. lvc.pszText = _T("数量");
  92. lvc.cx = 80;
  93. m_oFormlist.InsertColumn(3,&lvc);
  94. lvc.iSubItem = 3;
  95. lvc.pszText = _T("金额");
  96. lvc.cx = 120;
  97. m_oFormlist.InsertColumn(4,&lvc);
  98. //Add all bill whos state is "等待" to listbill.
  99. CString sql="Select * from SALEBILL where STATUS<>'已结帐'";
  100. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  101. try
  102. m_pRecordset.CreateInstance("ADODB.Recordset");
  103. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  104. while(!m_pRecordset->adoEOF)
  105. {
  106. m_oListbill.AddString((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ID"));
  107. m_pRecordset->MoveNext();
  108. }
  109. m_pRecordset->Close();
  110. }
  111. catch(_com_error e)///捕捉异常
  112. {
  113. CString stemp;
  114. stemp.Format("[初始化]读取 '等待' 单据出错:%s",e.ErrorMessage());
  115. AfxMessageBox(stemp);
  116. }
  117. //Select the last billid in listbox and read its items to listctrl and head to edit.
  118. int ncount=m_oListbill.GetCount();
  119. if(ncount>0)
  120. {
  121. CString sbillid;
  122. int nselect;
  123. nselect=m_oListbill.SetCurSel(ncount-1);
  124. m_oListbill.GetText(nselect,sbillid);
  125. ReadBillHead(sbillid);
  126. ReadToFormlist(sbillid);
  127. }
  128. else
  129. {
  130. //Put the button to the first list row.
  131. //将button移到合适的位置并设置其大小
  132. m_oFormlist.GetWindowRect(&rect);
  133. m_oButton.MoveWindow(440,95, rect.right/33, rect.bottom/25);
  134. m_oButton.BringWindowToTop();
  135. m_oButton.ShowWindow(SW_SHOW);//从隐藏变为显示
  136. }
  137. theApp.pWnd=GetSafeHwnd();//Initialize pWnd;
  138. m_BMButton3.AutoLoad(IDC_BUTTON_ADDF,this);
  139. return TRUE;  // return TRUE unless you set the focus to a control
  140.               // EXCEPTION: OCX Property Pages should return FALSE
  141. }
  142. void CPosDlg::OnButtonAddf() 
  143. {
  144. CString snowtime,sfloor,snewid,sbillno;
  145. CString sql;
  146. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  147. sfloor=GetCompanyFloor(FALSE);
  148. //Clear the edit ctrl.
  149. m_oTableno.SetWindowText("");
  150. m_oPeasons.SetWindowText("");
  151. //得到系统时间
  152. CTime now=CTime::GetCurrentTime();
  153. snowtime=now.Format(_T("%Y-%m-%d %H:%M:%S"));
  154. sbillno=GenNewBillID();
  155. sql="Insert into SALEBILL (ID,BEGINDATE,STATUS,TOTAL,ACTTOTAL,PAYMODE,FLOOR) values ('"+sbillno+"','"+snowtime+"','等待',0,0,1,'"+sfloor+"')";
  156. try
  157. _variant_t RecordsAffected;
  158. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  159. }
  160. catch(_com_error e)///捕捉异常
  161. {
  162. CString temp;
  163. temp.Format("[开单]生成单据出错:%s",e.ErrorMessage());
  164. AfxMessageBox(temp);
  165. return;
  166. }
  167. m_oStatus.SetWindowText("等待");
  168. m_oBegintime.SetWindowText(snowtime);
  169. m_oTotal.SetWindowText("0.00");
  170. m_oActtotal.SetWindowText("0.00");
  171. m_oListbill.AddString(sbillno);
  172. m_oListbill.SelectString(-1,sbillno);
  173. theApp.scallid=sbillno;//Get current bill id.
  174. m_oFormlist.DeleteAllItems();
  175. RECT rect;
  176. m_oFormlist.GetWindowRect(&rect);
  177. m_oButton.MoveWindow(440,95, rect.right/40, rect.bottom/35);
  178. m_oButton.BringWindowToTop();
  179. m_oButton.ShowWindow(SW_SHOW);//从隐藏变为显示
  180. }
  181. CString CPosDlg::GenNewBillID()
  182. {
  183. CString sql,snowtime;
  184. CString sNewID,scurid,snewid;
  185. long lnewid,lcount;
  186. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  187. //得到系统时间
  188. CTime now=CTime::GetCurrentTime();
  189. snowtime=now.Format(_T("%Y%m%d"));
  190. sql="Select count(*) as XXX from SALEBILL where ID like '"+snowtime+"%""'";
  191. lcount=GetRecordCount(sql);
  192. if(lcount==0)
  193. {
  194. sNewID.Format("%s-00001",snowtime);
  195. return sNewID;
  196. }
  197. sql="Select Max(ID) as AAA from SALEBILL where ID like '"+snowtime+"%""'";
  198. try
  199. m_pRecordset.CreateInstance("ADODB.Recordset");
  200. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  201. scurid=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("AAA");
  202. lnewid=atol(scurid.Right(5))+1;
  203. snewid.Format("-%05d",lnewid);
  204. sNewID=snowtime+snewid;
  205. m_pRecordset->Close();
  206. }
  207. catch(_com_error e)///捕捉异常
  208. {
  209. CString stemp;
  210. stemp.Format("获取最大单据编号出错:%s",e.ErrorMessage());
  211. AfxMessageBox(stemp);
  212. }
  213. return sNewID;
  214. }
  215. void CPosDlg::OnSelchangeListBill() 
  216. {
  217. CString stemp,sql,sbillid;
  218. int iOption; 
  219. _RecordsetPtr m_pRecordset;
  220. iOption = m_oListbill.GetCurSel();
  221. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  222. theApp.scallid=sbillid;//Get current bill id.
  223. //Clear the edit ctrl.
  224. m_oTableno.SetWindowText("");
  225. m_oPeasons.SetWindowText("");
  226. sql="select * from SALEBILL where ID='"+sbillid+"'";
  227. try
  228. {
  229. m_pRecordset.CreateInstance("ADODB.Recordset");
  230. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  231. if(!m_pRecordset->adoEOF)
  232. {
  233. m_oTableno.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TABLENO"));
  234. m_oPeasons.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("PEOPLES"));
  235. m_oCheck.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SALES"));
  236. m_oStatus.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("STATUS"));
  237. m_oBegintime.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("BEGINDATE"));
  238. m_oEndtime.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ENDDATE"));
  239. float ftotal=(float)m_pRecordset->GetCollect("TOTAL");
  240. CString stotal;
  241. stotal.Format("%.2f",ftotal);
  242. m_oTotal.SetWindowText(stotal);
  243. ftotal=(float)m_pRecordset->GetCollect("ACTTOTAL");
  244. stotal.Format("%.2f",ftotal);
  245. m_oActtotal.SetWindowText(stotal);
  246. }
  247. m_pRecordset->Close();
  248. }
  249. catch(_com_error e)///捕捉异常
  250. {
  251. CString temp;
  252. temp.Format("读取单据头到编辑框出错:%s",e.ErrorMessage());
  253. AfxMessageBox(temp);
  254. return;
  255. }
  256. ReadToFormlist(sbillid);//Get bill items from database.
  257. int i=m_oFormlist.GetItemCount();
  258. RECT rect;
  259. m_oFormlist.GetWindowRect(&rect);
  260. m_oButton.MoveWindow(440,95+i*15, rect.right/40, rect.bottom/35);
  261. m_oButton.BringWindowToTop();
  262. m_oButton.ShowWindow(SW_SHOW);//从隐藏变为显示
  263. }
  264. BEGIN_EVENTSINK_MAP(CPosDlg, CDialog)
  265.    //{{AFX_EVENTSINK_MAP(CPosDlg)
  266. //}}AFX_EVENTSINK_MAP
  267. END_EVENTSINK_MAP()
  268. void CPosDlg::OnMyMessage()
  269. {
  270. //When CPosDlgDlg received the WM_APPLY message from CSelectDlg 
  271. //it call the AddItemToList() to fill list.
  272. AddItemToList();
  273. //-------------------------------------------------//
  274. CString smID,sqa,ssum,sno,sql,stotal,sbillid;
  275. CString sclassid;
  276. long lrowno;
  277. int iOption;
  278. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  279. iOption = m_oListbill.GetCurSel();
  280. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  281. lrowno=GetRowNo(sbillid);
  282. sno.Format("%d",lrowno); //rowno.
  283. ssum=theApp.sprice;
  284. //Get class ID.
  285. sql="Select CLASSID from materiel where ID="+theApp.smID+"";
  286. try
  287. m_pRecordset.CreateInstance("ADODB.Recordset");
  288. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  289. long lclassid=(long)m_pRecordset->GetCollect("CLASSID");//How to get field value in only 1 record and 1 field condition.
  290. sclassid.Format("%d",lclassid);
  291. m_pRecordset->Close();
  292. }
  293. catch(_com_error e)///捕捉异常
  294. {
  295. CString temp;
  296. temp.Format("[选择商品]读取商品类别ID出错:%s",e.ErrorMessage());
  297. AfxMessageBox(temp);
  298. return;
  299. }
  300. sql="Insert into SALEDETAIL (BILLID,MATERIELID,ITEMCOUNT,ITEMTOTAL,CLASSID,ROWNO,ACTPRICE) values ('"+sbillid+"',"
  301. +theApp.smID+",1,"+ssum+","+sclassid+","+sno+","+ssum+")";
  302. try
  303. {  _variant_t RecordsAffected;
  304. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  305. UpdateData(FALSE);
  306. }
  307. catch(_com_error e)///捕捉异常
  308. {
  309. CString temp;
  310. temp.Format("[选择商品]插入商品到数据库出错:%s",e.ErrorMessage());
  311. AfxMessageBox(temp);
  312. return;
  313. }
  314. stotal=SumItemtotal(sbillid);
  315. sql="Update SALEBILL set TOTAL="+stotal+" where ID='"+sbillid+"'";
  316. try
  317. _variant_t RecordsAffected;
  318. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  319. }
  320. catch(_com_error e)///捕捉异常
  321. {
  322. CString temp;
  323. temp.Format("[结帐]更新单据总金额出错:%s",e.ErrorMessage());
  324. AfxMessageBox(temp);
  325. return;
  326. }
  327. m_oTotal.SetWindowText(stotal);
  328. // ReadToFormlist(sbillid);
  329. }
  330. void CPosDlg::AddItemToList()
  331. {
  332. CString snumline;
  333. int numline=0,nmax;
  334. LV_ITEM lvitem;
  335. lvitem.pszText="";
  336. lvitem.mask=LVIF_TEXT;
  337. lvitem.iSubItem=0;
  338. numline=m_oFormlist.GetItemCount();
  339. lvitem.iItem=numline;
  340. snumline=m_oFormlist.GetItemText(numline-1,0);
  341. nmax=atoi(snumline)+1;
  342. snumline.Format("%d",nmax);
  343. m_oFormlist.InsertItem(&lvitem);
  344. //读出数据写入到list中
  345. m_oFormlist.SetItemText(numline,0,snumline);
  346. m_oFormlist.SetItemText(numline,1,theApp.smname);
  347. m_oFormlist.SetItemText(numline,2,"1");
  348. m_oFormlist.SetItemText(numline,3,theApp.sprice);
  349. //Move the select button to next line.
  350. RECT rect;
  351. m_oFormlist.GetWindowRect(&rect);
  352. m_oButton.MoveWindow(440,110+numline*15, rect.right/40, rect.bottom/35);
  353. m_oButton.BringWindowToTop();
  354. m_oButton.ShowWindow(SW_SHOW);//从隐藏变为显示
  355. }
  356. long CPosDlg::GetRowNo(CString sbillid)
  357. {
  358. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  359. CString sql,sname;
  360. long lrowno,lcount;
  361. sql="Select Count(*) AS XXX from SALEDETAIL where BILLID='"+sbillid+"'";
  362. lcount=GetRecordCount(sql);
  363. if(lcount==0)
  364. {
  365. lrowno=1;
  366. return lrowno;
  367. }
  368. sql="Select MAX(ROWNO) AS RRR from SALEDETAIL where BILLID='"+sbillid+"'";
  369. try
  370. {
  371. m_pRecordset.CreateInstance("ADODB.Recordset");
  372. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  373. lrowno=(long)m_pRecordset->GetCollect("RRR")+1;//How to get field value in only 1 record and 1 field condition.
  374. }
  375. catch(_com_error e)///捕捉异常
  376. {
  377. CString temp;
  378. temp.Format("[单据]读取商品序号出错:%s",e.ErrorMessage());
  379. AfxMessageBox(temp);
  380. }
  381. return lrowno;
  382. }
  383. CString CPosDlg::GetMaterielName(long materielid)
  384. {
  385. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  386. CString sql,smaterielid,sname;
  387. smaterielid.Format("%d",materielid);
  388. sql="Select NAME from MATERIEL where ID="+smaterielid+"";
  389. try
  390. {
  391. m_pRecordset.CreateInstance("ADODB.Recordset");
  392. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  393. if(!m_pRecordset->adoEOF)
  394. sname=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME");
  395. }
  396. catch(_com_error e)///捕捉异常
  397. {
  398. CString temp;
  399. temp.Format("获取商品名称出错:%s",e.ErrorMessage());
  400. AfxMessageBox(temp);
  401. }
  402. return sname;
  403. }
  404. void CPosDlg::OnButtonSelmateriel() 
  405. {
  406. int iOption;
  407. CString sbillid;
  408. iOption = m_oListbill.GetCurSel();
  409. if(iOption<0)
  410. return;
  411. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  412. if(bGetBillStatus(sbillid))
  413. return;
  414. CSelectDlg selectdlg;
  415. selectdlg.DoModal();
  416. }
  417. void CPosDlg::ReadToFormlist(CString sbillid)
  418. {
  419. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  420. CString sql,sname,sno,sqa,ssum;
  421. long rowcount=0;
  422. long lmid;
  423. float fsum;
  424. LV_ITEM lvitem;
  425. //清空list控件的数据
  426. m_oFormlist.DeleteAllItems();
  427. int numline=0;
  428. lvitem.pszText="";
  429. lvitem.mask=LVIF_TEXT;
  430. lvitem.iSubItem=0;
  431. sql="Select * from SALEDETAIL where BILLID='"+sbillid+"'";
  432. try
  433. {
  434. m_pRecordset.CreateInstance("ADODB.Recordset");
  435. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  436. while(!m_pRecordset->adoEOF)
  437. {
  438. sno=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ROWNO");
  439. sqa=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ITEMCOUNT");
  440. lmid=(long)m_pRecordset->GetCollect("MATERIELID");
  441. sname=GetMaterielName(lmid);
  442. fsum=(float)m_pRecordset->GetCollect("ITEMTOTAL");
  443. ssum.Format("%.2f",fsum);
  444. lvitem.iItem=numline;
  445. m_oFormlist.InsertItem(&lvitem);
  446. //读出数据写入到list中
  447. m_oFormlist.SetItemText(numline,0,sno);
  448. m_oFormlist.SetItemText(numline,1,sname);
  449. m_oFormlist.SetItemText(numline,2,sqa);
  450. m_oFormlist.SetItemText(numline,3,ssum);
  451. m_pRecordset->MoveNext();
  452. numline++;
  453. }
  454. RECT rect;
  455. m_oFormlist.GetWindowRect(&rect);
  456. m_oButton.MoveWindow(440,95+numline*15, rect.right/40, rect.bottom/35);
  457. m_oButton.BringWindowToTop();
  458. m_oButton.ShowWindow(SW_SHOW);//从隐藏变为显示
  459. m_pRecordset->Close();///关闭记录集rowcount
  460. }
  461. catch(_com_error e)///捕捉异常
  462. {
  463. CString temp;
  464. temp.Format("读取单据商品到列表中出错:%s",e.ErrorMessage());
  465. AfxMessageBox(temp);
  466. }
  467. }
  468. void CPosDlg::OnDblclkListForm(NMHDR* pNMHDR, LRESULT* pResult) 
  469. {
  470. CString sno,ssum,sname,scount,sql;
  471. CString sbillid;
  472. float fprice,fsum;
  473. int ncount,nqa,iOption;
  474. iOption = m_oListbill.GetCurSel();
  475. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  476. if(bGetBillStatus(sbillid))
  477. return;
  478. //得到当前选中的行
  479. POSITION pos = m_oFormlist.GetFirstSelectedItemPosition();
  480. //如果选中一行
  481. if(pos)
  482. {
  483. int nItem = m_oFormlist.GetNextSelectedItem(pos);
  484. sno=m_oFormlist.GetItemText(nItem,0);
  485. scount=m_oFormlist.GetItemText(nItem,2);
  486. ncount=atoi(scount);
  487. theApp.sqa=scount;
  488. ssum=m_oFormlist.GetItemText(nItem,3);
  489. fsum=atof(ssum);
  490. fprice=fsum;
  491. if(ncount>0)
  492. fprice=fsum/ncount;
  493. //Open the modifyqa dlg. 
  494. CModifyqaDlg modifydlg;
  495. modifydlg.DoModal();
  496. nqa=atoi(theApp.sqa);
  497. fsum=nqa*fprice;
  498. ssum.Format("%.2f",fsum);
  499. m_oFormlist.SetItemText(nItem,2,theApp.sqa);
  500. m_oFormlist.SetItemText(nItem,3,ssum);
  501. //Update saledetail.
  502. sql="Update SALEDETAIL set ITEMCOUNT="+theApp.sqa+",ITEMTOTAL="+ssum+" where BILLID='"+sbillid+"' and ROWNO="+sno+"";
  503. try
  504. {  _variant_t RecordsAffected;
  505. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  506. UpdateData(FALSE);
  507. }
  508. catch(_com_error e)///捕捉异常
  509. {
  510. CString temp;
  511. temp.Format("[修改数量]更新商品出错:%s",e.ErrorMessage());
  512. AfxMessageBox(temp);
  513. return;
  514. }
  515. //Update salebill.
  516. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  517. float ftotal;
  518. CString stotal;
  519. sql="select Sum(ITEMTOTAL) as SSS from SALEDETAIL where BILLID='"+sbillid+"'";
  520. try
  521. m_pRecordset.CreateInstance("ADODB.Recordset");
  522. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  523. ftotal=(float)m_pRecordset->GetCollect("SSS");
  524. m_pRecordset->Close();
  525. }
  526. catch(_com_error e)///捕捉异常
  527. {
  528. CString stemp;
  529. stemp.Format("[修改数量]计算单据金额出错:%s",e.ErrorMessage());
  530. AfxMessageBox(stemp);
  531. return;
  532. }
  533. stotal.Format("%.2f",ftotal);
  534. sql="Update SALEBILL set TOTAL="+stotal+" where ID='"+sbillid+"'";
  535. try
  536. {  _variant_t RecordsAffected;
  537. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  538. }
  539. catch(_com_error e)///捕捉异常
  540. {
  541. CString temp;
  542. temp.Format("[修改数量]更新总金额出错:%s",e.ErrorMessage());
  543. AfxMessageBox(temp);
  544. return;
  545. }
  546. m_oTotal.SetWindowText(stotal);
  547. }
  548. *pResult = 0;
  549. }
  550. BOOL CPosDlg::PreTranslateMessage(MSG* pMsg) 
  551. {
  552. int i,iState,iOption;
  553. RECT rect;
  554. CString sql,sno,sbillid,stotal;
  555. if(pMsg->message==WM_KEYDOWN)
  556. {
  557. switch(pMsg->wParam)
  558. {
  559. case VK_F1:
  560. OnButtonAddf();
  561. break;
  562. case VK_F2:
  563. OnButtonDelf();
  564. break;
  565. case VK_F3:
  566. OnButtonCallf();
  567. break;
  568. case VK_F4:
  569. OnButtonHidef();
  570. break;
  571. case VK_F5:
  572. OnButtonCheckf();
  573. break;
  574. case VK_F6:
  575. break;
  576. case VK_F7:
  577. break;
  578. case VK_F8:
  579. break;
  580. case VK_F9:
  581. break;
  582. case VK_F10:
  583. OnButtonOff();
  584. break;
  585. }
  586. }
  587. // if(GetFocus()->GetDlgCtrlID()==IDC_LIST_FORM)//针对特定的控件,也可以不用判断  
  588. // {  
  589. if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_DELETE)  
  590.         {  
  591. iOption = m_oListbill.GetCurSel();
  592. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  593. if(bGetBillStatus(sbillid))
  594. return FALSE;
  595. int nItemSelected=m_oFormlist.GetSelectedCount();//所选表项数
  596. int nItemCount=m_oFormlist.GetItemCount();//表项总数
  597. if(nItemSelected<1) 
  598. return FALSE;
  599. for(i=nItemCount-1;i>=0;i--)
  600. {
  601. iState=m_oFormlist.GetItemState(i,LVIS_SELECTED);
  602. if(iState!=0) 
  603. {
  604. sno=m_oFormlist.GetItemText(i,0);
  605. m_oFormlist.DeleteItem(i);
  606. sql="delete * from SALEDETAIL where BILLID='"+sbillid+"' and ROWNO="+sno+"";
  607. try
  608. {  _variant_t RecordsAffected;
  609. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  610. UpdateData(FALSE);
  611. }
  612. catch(_com_error e)///捕捉异常
  613. {
  614. CString temp;
  615. temp.Format("从数据库中删除单据出错:%s",e.ErrorMessage());
  616. AfxMessageBox(temp);
  617. return FALSE;
  618. }
  619. }
  620. }
  621. ReadToFormlist(sbillid);//Refresh formlist.
  622. m_oFormlist.GetWindowRect(&rect);
  623. m_oButton.MoveWindow(440,95+(nItemCount-1)*15, rect.right/40, rect.bottom/35);
  624. m_oButton.BringWindowToTop();
  625. m_oButton.ShowWindow(SW_SHOW);//从隐藏变为显示
  626. stotal=SumItemtotal(sbillid);
  627. m_oTotal.SetWindowText(stotal);
  628. sql="Update SALEBILL set TOTAL="+stotal+" where ID='"+sbillid+"'";
  629. try
  630. _variant_t RecordsAffected;
  631. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  632. }
  633. catch(_com_error e)///捕捉异常
  634. {
  635. CString temp;
  636. temp.Format("[删除商品]更新商品总金额出错:%s",e.ErrorMessage());
  637. AfxMessageBox(temp);
  638. return FALSE;
  639. }
  640.          }  
  641. // }
  642. return CDialog::PreTranslateMessage(pMsg);
  643. }
  644. void CPosDlg::OnButtonCallf() 
  645. {
  646. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  647. CString sql;
  648. if(!theApp.VerifyPower("CallBill"))
  649. {
  650. AfxMessageBox("没有权限调单!");
  651. return;
  652. }
  653. CCallDlg calldlg;//Create a dialog to enter billid to call.
  654. calldlg.dowhat="imhappy";
  655. calldlg.DoModal();
  656. if(!bGetBillStatus(theApp.scallid))
  657. return;
  658. sql="select * from SALEBILL where ID='"+theApp.scallid+"'";
  659. try
  660. {
  661. m_pRecordset.CreateInstance("ADODB.Recordset");
  662. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  663. if(!m_pRecordset->adoEOF)
  664. {
  665. m_oTableno.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TABLENO"));
  666. m_oPeasons.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("PEOPLES"));
  667. m_oCheck.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SALES"));
  668. m_oStatus.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("STATUS"));
  669. m_oBegintime.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("BEGINDATE"));
  670. m_oEndtime.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ENDDATE"));
  671. float ftotal=(float)m_pRecordset->GetCollect("TOTAL");
  672. CString stotal;
  673. stotal.Format("%.2f",ftotal);
  674. m_oTotal.SetWindowText(stotal);
  675. ftotal=(float)m_pRecordset->GetCollect("ACTTOTAL");
  676. stotal.Format("%.2f",ftotal);
  677. m_oActtotal.SetWindowText(stotal);
  678. ReadToFormlist(theApp.scallid);//Get bill items from database.
  679. m_oListbill.AddString(theApp.scallid);
  680. m_oListbill.SelectString(-1,theApp.scallid);
  681. }
  682. if(m_pRecordset->adoEOF)
  683. {
  684. CString temp="单据 '"+theApp.scallid+"' 不存在!";
  685. AfxMessageBox(temp);
  686. theApp.scallid="";
  687. }
  688. m_pRecordset->Close();
  689. }
  690. catch(_com_error e)///捕捉异常
  691. {
  692. CString temp;
  693. temp.Format("从列表读取单据头到编辑框出错:%s",e.ErrorMessage());
  694. AfxMessageBox(temp);
  695. return;
  696. }
  697. m_oButton.ShowWindow(SW_HIDE);//从显示变为隐藏
  698. }
  699. long CPosDlg::GetRecordCount(CString sql)
  700. {
  701. long lcount;
  702. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  703. try
  704. m_pRecordset.CreateInstance("ADODB.Recordset");
  705. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  706. lcount=(long)m_pRecordset->GetCollect("XXX");
  707. m_pRecordset->Close();
  708. }
  709. catch(_com_error e)///捕捉异常
  710. {
  711. CString stemp;
  712. stemp.Format("获得记录数出错:%s",e.ErrorMessage());
  713. AfxMessageBox(stemp);
  714. }
  715. return lcount;
  716. }
  717. void CPosDlg::OnButtonDelf() 
  718. {
  719. CString sql,sbillid;
  720. int iOption;
  721. _RecordsetPtr m_pRecordset;
  722. if(!theApp.VerifyPower("DelBill"))
  723. {
  724. AfxMessageBox("没有权限删除单据!");
  725. return;
  726. }
  727. iOption = m_oListbill.GetCurSel();
  728. if(iOption==-1)
  729. return;
  730. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  731. if(bGetBillStatus(sbillid))
  732. {
  733. AfxMessageBox("不能删除已结帐单据!");
  734. return;
  735. }
  736. if(AfxMessageBox("确定删除此单据吗?",MB_YESNO)==IDYES)
  737. {
  738. sql="Delete * from SALEBILL where ID='"+sbillid+"'";
  739. try
  740. _variant_t RecordsAffected;
  741. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  742. }
  743. catch(_com_error e)///捕捉异常
  744. {
  745. CString temp;
  746. temp.Format("Delete bill id error:%s",e.ErrorMessage());
  747. AfxMessageBox(temp);
  748. return;
  749. }
  750. sql="Delete * from SALEDETAIL where BILLID='"+sbillid+"'";
  751. try
  752. _variant_t RecordsAffected;
  753. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  754. }
  755. catch(_com_error e)///捕捉异常
  756. {
  757. CString temp;
  758. temp.Format("删除单据商品出错:%s",e.ErrorMessage());
  759. AfxMessageBox(temp);
  760. return;
  761. }
  762. m_oListbill.DeleteString(iOption);
  763. if(m_oListbill.GetCount()>0)
  764. {
  765. if(m_oListbill.GetCount()>iOption)
  766. m_oListbill.SetCurSel(iOption);
  767. if(m_oListbill.GetCount()==iOption)
  768. m_oListbill.SetCurSel(iOption-1);
  769. iOption = m_oListbill.GetCurSel();
  770. m_oListbill.GetText(iOption, sbillid);//Again得到选中项的文本
  771. theApp.scallid=sbillid;//Get current bill id.
  772. ReadBillHead(sbillid);
  773. ReadToFormlist(sbillid);
  774. }
  775. }
  776. else
  777. return;
  778. }
  779. void CPosDlg::OnButtonHidef() 
  780. {
  781. int iOption;
  782. CString sbillid;
  783. iOption = m_oListbill.GetCurSel();
  784. if(iOption==-1)
  785. return;
  786. m_oListbill.GetText(iOption, sbillid);//Again得到选中项的文本
  787. if(!bGetBillStatus(sbillid))
  788. {
  789. AfxMessageBox("未结帐单据不能隐藏!");
  790. return;
  791. }
  792. m_oListbill.DeleteString(iOption);
  793. if(m_oListbill.GetCount()==0)
  794. m_oFormlist.DeleteAllItems();
  795. if(m_oListbill.GetCount()>0)
  796. {
  797. if(m_oListbill.GetCount()>iOption)
  798. m_oListbill.SetCurSel(iOption);
  799. if(m_oListbill.GetCount()==iOption)
  800. m_oListbill.SetCurSel(iOption-1);
  801. iOption = m_oListbill.GetCurSel();
  802. m_oListbill.GetText(iOption, sbillid);//Again得到选中项的文本
  803. theApp.scallid=sbillid;//Get current bill id.
  804. ReadBillHead(sbillid);
  805. ReadToFormlist(sbillid);//Refresh listctrl.
  806. }
  807. }
  808. void CPosDlg::OnButtonCheckf() 
  809. {
  810. int iOption;
  811. iOption = m_oListbill.GetCurSel();
  812. if(iOption==-1)
  813. return;
  814. m_oListbill.GetText(iOption, theApp.scallid);//得到选中项的文本
  815. if(bGetBillStatus(theApp.scallid))
  816. {
  817. AfxMessageBox("已结帐单据不能再结帐!");
  818. return;
  819. }
  820. if(m_oFormlist.GetItemCount()==0)
  821. {
  822. AfxMessageBox("不能结帐空单据!");
  823. return;
  824. }
  825. else
  826. {
  827. CCheckDlg checkdlg;
  828. checkdlg.DoModal();
  829. }
  830. }
  831. void CPosDlg::OnMyCheckMessage()
  832. {
  833. //When user click the button of checkdlg
  834. //it send a message to posdlg to do the following things.
  835. OnButtonLocalp();
  836. OnButtonHidef();
  837. }
  838. CString CPosDlg::SumItemtotal(CString sbillid)
  839. {
  840. //Sum bill Items money from database.
  841. CString stotal,sql;
  842. float ftotal;
  843. long lcount;
  844. _RecordsetPtr m_pRecordset;
  845. sql="Select count(*) as XXX from SALEDETAIL where BILLID ='"+sbillid+"'";
  846. lcount=GetRecordCount(sql);
  847. if(lcount==0)
  848. {
  849. stotal="0.00";
  850. return stotal;
  851. }
  852. sql="Select SUM(ITEMTOTAL) as SSS from SALEDETAIL where BILLID='"+sbillid+"'";
  853. try
  854. m_pRecordset.CreateInstance("ADODB.Recordset");
  855. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  856. ftotal=(float)m_pRecordset->GetCollect("SSS");
  857. }
  858. catch(_com_error e)///捕捉异常
  859. {
  860. CString temp;
  861. temp.Format("[前台销售]计算商品总金额出错:%s",e.ErrorMessage());
  862. AfxMessageBox(temp);
  863. }
  864. stotal.Format("%.2f",ftotal);
  865. return stotal;
  866. }
  867. void CPosDlg::ReadBillHead(CString sbillid)
  868. {
  869. CString sql,status;
  870. _RecordsetPtr m_pRecordset;
  871. sql="select * from SALEBILL where ID='"+sbillid+"'";
  872. try
  873. {
  874. m_pRecordset.CreateInstance("ADODB.Recordset");
  875. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  876. if(!m_pRecordset->adoEOF)
  877. {
  878. m_oTableno.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TABLENO"));
  879. m_oPeasons.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("PEOPLES"));
  880. m_oCheck.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SALES"));
  881. m_oStatus.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("STATUS"));
  882. m_oBegintime.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("BEGINDATE"));
  883. m_oEndtime.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ENDDATE"));
  884. float ftotal=(float)m_pRecordset->GetCollect("TOTAL");
  885. CString stotal;
  886. stotal.Format("%.2f",ftotal);
  887. m_oTotal.SetWindowText(stotal);
  888. ftotal=(float)m_pRecordset->GetCollect("ACTTOTAL");
  889. stotal.Format("%.2f",ftotal);
  890. m_oActtotal.SetWindowText(stotal);
  891. }
  892. m_pRecordset->Close();
  893. }
  894. catch(_com_error e)///捕捉异常
  895. {
  896. CString temp;
  897. temp.Format("读取单据头到编辑框出错:%s",e.ErrorMessage());
  898. AfxMessageBox(temp);
  899. return;
  900. }
  901. }
  902. BOOL CPosDlg::bGetBillStatus(CString sbillid)
  903. {
  904. CString sql,sstatus;
  905. BOOL bstatus;
  906. _RecordsetPtr m_pRecordset;
  907. sql="select STATUS from SALEBILL where ID='"+sbillid+"'";
  908. try
  909. {
  910. m_pRecordset.CreateInstance("ADODB.Recordset");
  911. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  912. if(!m_pRecordset->adoEOF)
  913. {
  914. sstatus=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("STATUS");
  915. if(sstatus=="等待"||sstatus=="挂帐")
  916. bstatus=FALSE;
  917. else
  918. bstatus=TRUE;
  919. }
  920. m_pRecordset->Close();
  921. }
  922. catch(_com_error e)///捕捉异常
  923. {
  924. CString temp;
  925. temp.Format("读取单据状态出错:%s",e.ErrorMessage());
  926. AfxMessageBox(temp);
  927. }
  928. return bstatus;
  929. }
  930. void CPosDlg::OnKillfocusEditTableno() 
  931. {
  932. CString sql,stemp,sbillid;
  933. _RecordsetPtr m_pRecordset;
  934. int iOption;
  935. iOption = m_oListbill.GetCurSel();
  936. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  937. if(bGetBillStatus(theApp.scallid))
  938. return;
  939. m_oTableno.GetWindowText(stemp);
  940. sql="Update SALEBILL set TABLENO='"+stemp+"' where ID='"+sbillid+"'";
  941. try
  942. {  _variant_t RecordsAffected;
  943. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  944. }
  945. catch(_com_error e)///捕捉异常
  946. {
  947. CString temp;
  948. temp.Format("更新单据头(桌号) 出错:%s",e.ErrorMessage());
  949. AfxMessageBox(temp);
  950. return;
  951. }
  952. }
  953. void CPosDlg::OnKillfocusEditPeasons() 
  954. {
  955. CString sql,stemp,sbillid;
  956. _RecordsetPtr m_pRecordset;
  957. int iOption;
  958. iOption = m_oListbill.GetCurSel();
  959. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  960. if(bGetBillStatus(theApp.scallid))
  961. return;
  962. m_oPeasons.GetWindowText(stemp);
  963. sql="Update SALEBILL set PEOPLES='"+stemp+"' where ID='"+sbillid+"'";
  964. try
  965. {  _variant_t RecordsAffected;
  966. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  967. }
  968. catch(_com_error e)///捕捉异常
  969. {
  970. CString temp;
  971. temp.Format("更新单据头(人数)出错:%s",e.ErrorMessage());
  972. AfxMessageBox(temp);
  973. return;
  974. }
  975. }
  976. void CPosDlg::OnButtonOff() 
  977. {
  978. COffdutyDlg offdlg;
  979. offdlg.DoModal();
  980. }
  981. void CPosDlg::OnButtonFindf() 
  982. {
  983. CString sql,sbillid;
  984. _RecordsetPtr m_pRecordset;
  985. CCallDlg calldlg;//Create a dialog to enter billid to call.
  986. calldlg.dowhat="pleased";
  987. calldlg.DoModal();
  988. sql="select ID from SALEBILL where STATUS<>'已结帐' and TABLENO='"+calldlg.dowhat+"'";
  989. try
  990. {
  991. m_pRecordset.CreateInstance("ADODB.Recordset");
  992. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  993. if(!m_pRecordset->adoEOF)
  994. {
  995. sbillid=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ID");
  996. m_oListbill.SelectString(-1,sbillid);
  997. ReadToFormlist(sbillid);
  998. }
  999. if(m_pRecordset->adoEOF)
  1000. {
  1001. CString temp="桌号为 '"+calldlg.dowhat+"' 单据不存在!";
  1002. AfxMessageBox(temp);
  1003. }
  1004. m_pRecordset->Close();
  1005. }
  1006. catch(_com_error e)///捕捉异常
  1007. {
  1008. CString temp;
  1009. temp.Format("查找桌号出错:%s",e.ErrorMessage());
  1010. AfxMessageBox(temp);
  1011. }
  1012. }
  1013. BOOL CPosDlg::PrintListCtrl(CListCtrl &list)
  1014. {
  1015. CDC dc;
  1016. //把打印设备环境附加到DC对象
  1017. CPrintDialog pdlg(FALSE,PD_NOPAGENUMS|PD_NOSELECTION,this);
  1018. BOOL bFindPrinter=pdlg.GetDefaults();
  1019. if(!bFindPrinter)
  1020. return FALSE;
  1021. dc.Attach(pdlg.GetPrinterDC());
  1022. //取打印机的横方向和纵方向的分辨率
  1023. //即每英寸点数
  1024. short cxInch = dc.GetDeviceCaps(LOGPIXELSX);
  1025. short cyInch = dc.GetDeviceCaps(LOGPIXELSY);
  1026. //字体
  1027. CFont font;
  1028. VERIFY(font.CreatePointFont(96, "宋体", &dc));//为DC创建字体
  1029. CFont* def_font = dc.SelectObject(&font);//保存现在的字体
  1030. //根据字体宽度、高度计算每行最大字数及每页最大行数
  1031. //取打印纸张高度和宽度
  1032. int nPageHeight, nPageWidth;
  1033. nPageHeight = dc.GetDeviceCaps(VERTRES);
  1034. nPageWidth = dc.GetDeviceCaps(HORZRES);
  1035. TEXTMETRIC TextM;
  1036. dc.GetTextMetrics(&TextM);
  1037. //字体高度 //字体平均宽度
  1038. int nCharHeight = (unsigned short)TextM.tmHeight;
  1039. int nCharWidth=(unsigned short)TextM.tmAveCharWidth;
  1040. //每行最大字数 //每页最大行数
  1041. int m_MaxLineChar = nPageWidth / nCharWidth - 8; 
  1042. int m_LinesPerPage = nPageHeight/ nCharHeight; 
  1043. //页边距
  1044. int nXMargin = 2;
  1045. int nYMargin = 2;
  1046. //获得行数
  1047. int nLineCount = list.GetItemCount();
  1048. int i;
  1049. //设置所需打印纸张数目
  1050. DOCINFO di;
  1051. di.cbSize = sizeof(DOCINFO);
  1052. di.lpszDocName = "Mypos printing"; 
  1053. di.lpszOutput = (LPTSTR) NULL; 
  1054. di.lpszDatatype = (LPTSTR) NULL; 
  1055. di.fwType = 0; 
  1056. dc.StartDoc(&di);
  1057. dc.StartPage();
  1058. //输出公司名称
  1059. CString stitle,sftitle,space="";
  1060. stitle=GetCompanyFloor(TRUE);
  1061. int n=(36-stitle.GetLength())/2;
  1062. for(i=0;i<n;i++)
  1063. space=space+" ";
  1064. sftitle=space+stitle;
  1065. dc.TextOut(2, nYMargin+nCharHeight,sftitle, strlen(sftitle));
  1066. //输出单号
  1067. CString sbillid;
  1068. int iOption = m_oListbill.GetCurSel();
  1069. m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
  1070. sbillid="单号:"+sbillid;
  1071. dc.TextOut( 2, nYMargin+nCharHeight*2, sbillid, strlen(sbillid));
  1072. //输出桌号、开台时间、人数、结帐时间、收银、付款方式
  1073. CString stable,speople,sbegin,ssale,stable1,speople1,send,spaymode;
  1074. m_oTableno.GetWindowText(stable);
  1075. stable1.Format("%-8s",stable);//Table No.
  1076. m_oPeasons.GetWindowText(speople);
  1077. speople1.Format("%-8s",speople);//People quantity.
  1078. m_oBegintime.GetWindowText(sbegin);
  1079. m_oCheck.GetWindowText(ssale);//Checkman.
  1080. spaymode.Format("%-15s",theApp.spaymodep);
  1081. if(ssale=="无")
  1082. {
  1083. ssale.Format("%-8s",theApp.name);
  1084. spaymode="";
  1085. }
  1086. m_oEndtime.GetWindowText(send);//End time.
  1087. if(send=="无")
  1088. send=theApp.snowtimep;
  1089. stable="开台:"+sbegin+" 桌号:"+stable1;
  1090. speople="结账:"+send+" 人数:"+speople1;
  1091. ssale="付款方式:"+spaymode+" 收银:"+ssale;
  1092. dc.TextOut( 2, nYMargin+nCharHeight*3, stable, strlen(stable));
  1093. dc.TextOut( 2, nYMargin+nCharHeight*4, speople, strlen(speople));
  1094. dc.TextOut( 2, nYMargin+nCharHeight*5, ssale, strlen(ssale));
  1095. //输出列表控件的列标题
  1096. CString slisttitle;
  1097. slisttitle="名称                  数量      金额";
  1098. dc.TextOut( 2, nYMargin+nCharHeight*7,slisttitle, strlen(slisttitle));
  1099. int nMaxLinePerPage = nPageHeight/nCharHeight -3;//每页最大行数
  1100. int nCurPage =1;
  1101. //输出各列的数据
  1102. CString subitem,sfsub,sfsub1,sfsub2;
  1103. for(i =0; i<nLineCount; i++)
  1104. {
  1105. if(nLineCount+7-(nCurPage-1)*nMaxLinePerPage > nMaxLinePerPage)
  1106. {
  1107. //新的一页
  1108. dc.EndPage();
  1109. dc.StartPage();
  1110. nCurPage ++;
  1111. }
  1112. subitem= list.GetItemText(i, 1);
  1113. sfsub.Format("%-20s",subitem);
  1114. subitem= list.GetItemText(i, 2);
  1115. sfsub1.Format("%6s",subitem);
  1116. subitem= list.GetItemText(i, 3);
  1117. sfsub2.Format("%10s",subitem);
  1118. sfsub=sfsub+sfsub1+sfsub2;
  1119. dc.TextOut( 2, nYMargin+(i+8-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, 
  1120. sfsub, strlen(sfsub));
  1121. }
  1122. stitle="------------------------------------";
  1123. dc.TextOut( 2, nYMargin+(nLineCount+8-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, 
  1124. stitle, strlen(stitle));
  1125. //输出总金额
  1126. CString stotal,sacttotal,sdiscount;
  1127. float fdiscount;
  1128. stotal.Format("%27s",theApp.sconsume);
  1129. sacttotal.Format("%27s",theApp.sactsum);
  1130. fdiscount=atof(stotal)-atof(sacttotal);
  1131. sdiscount.Format("%31.2f",fdiscount);
  1132. stotal="消费金额:"+stotal;
  1133. sdiscount="折扣:"+sdiscount;
  1134. sacttotal="应收金额:"+sacttotal;
  1135. dc.TextOut( 2, nYMargin+(nLineCount+9-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, 
  1136. stotal, strlen(stotal));
  1137. dc.TextOut( 2, nYMargin+(nLineCount+10-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, 
  1138. sdiscount, strlen(sdiscount));
  1139. dc.TextOut( 2, nYMargin+(nLineCount+11-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, 
  1140. sacttotal, strlen(sacttotal));
  1141. stitle="------------------------------------";
  1142. dc.TextOut( 2, nYMargin+(nLineCount+12-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, 
  1143. stitle, strlen(stitle));
  1144. sacttotal="谢谢惠顾,欢迎再次光临!";
  1145. dc.TextOut( 2, nYMargin+(nLineCount+13-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, 
  1146. sacttotal, strlen(sacttotal));
  1147. dc.EndPage();
  1148. dc.EndDoc();
  1149. //打印结束
  1150. //最后不要忘记将字体还原,这一句是必需的
  1151. dc.SelectObject(def_font); //恢复原来的字体
  1152. font.DeleteObject(); 
  1153. DeleteDC(dc.Detach()); 
  1154. return TRUE;
  1155. }
  1156. CString CPosDlg::GetCompanyFloor(BOOL bCompany)
  1157. {
  1158. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  1159. CString sql,scompany,sfloor;
  1160. sql="Select * from COMPANY";
  1161. try
  1162. m_pRecordset.CreateInstance("ADODB.Recordset");
  1163. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  1164. if(!m_pRecordset->adoEOF)
  1165. {
  1166.  scompany=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("COMPANYNAME");
  1167.  sfloor=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("FLOORNUMBER");
  1168. }
  1169. m_pRecordset->Close();
  1170. }
  1171. catch(_com_error e)///捕捉异常
  1172. {
  1173. CString stemp;
  1174. if(!bCompany)
  1175. stemp.Format("[初始化]读取楼层号出错:%s",e.ErrorMessage());
  1176. if(bCompany)
  1177. stemp.Format("[打印]读取公司名出错:%s",e.ErrorMessage());
  1178. AfxMessageBox(stemp);
  1179. }
  1180. if(!bCompany)
  1181. return sfloor;
  1182. if(bCompany)
  1183. return scompany;
  1184. }
  1185. void CPosDlg::OnButtonLocalp() 
  1186. {
  1187. PrintListCtrl(m_oFormlist);
  1188. }