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

酒店行业

开发平台:

Java

  1. // StatDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyPos.h"
  5. #include "StatDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CStatDlg dialog
  13. extern CMyPosApp theApp;
  14. CStatDlg::CStatDlg(CWnd* pParent /*=NULL*/)
  15. : CDialog(CStatDlg::IDD, pParent)
  16. {
  17. //{{AFX_DATA_INIT(CStatDlg)
  18. //}}AFX_DATA_INIT
  19. }
  20. void CStatDlg::DoDataExchange(CDataExchange* pDX)
  21. {
  22. CDialog::DoDataExchange(pDX);
  23. //{{AFX_DATA_MAP(CStatDlg)
  24. DDX_Control(pDX, IDC_DATETIMEPICKER_SD, m_oSdate);
  25. DDX_Control(pDX, IDC_DATETIMEPICKER_OD, m_oOdate);
  26. DDX_Control(pDX, IDC_RADIO_WEEK, m_oWeek);
  27. DDX_Control(pDX, IDC_EDIT_LISTTITLE, m_oListtitle);
  28. DDX_Control(pDX, IDC_LIST_REPORT, m_oListreport);
  29. DDX_Control(pDX, IDC_EDIT_ST, m_oSt);
  30. DDX_Control(pDX, IDC_EDIT_OT, m_oOt);
  31. //}}AFX_DATA_MAP
  32. }
  33. BEGIN_MESSAGE_MAP(CStatDlg, CDialog)
  34. //{{AFX_MSG_MAP(CStatDlg)
  35. ON_BN_CLICKED(IDC_BUTTON_STAT, OnButtonStat)
  36. ON_BN_CLICKED(IDC_BUTTON_PRINTREPORT, OnButtonPrintreport)
  37. //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CStatDlg message handlers
  41. BOOL CStatDlg::OnInitDialog() 
  42. {
  43. CDialog::OnInitDialog();
  44. m_oWeek.SetCheck(1);
  45. m_oSt.SetWindowText("00:00:00");
  46. m_oOt.SetWindowText("23:59:59");
  47. return TRUE;  // return TRUE unless you set the focus to a control
  48.               // EXCEPTION: OCX Property Pages should return FALSE
  49. }
  50. void CStatDlg::OnButtonStat() 
  51. {
  52. int n;
  53. CString sql,sfloor,scompany,ssdate,sodate,sstime,sotime;
  54. CString sbegin,send,stotal;
  55. _RecordsetPtr m_pRecordset;
  56. int nselect=GetCheckedRadioButton(IDC_RADIO_SINGLE,IDC_RADIO_WEEK); 
  57. switch(nselect)//Get if it is discount.
  58. {
  59. case IDC_RADIO_WEEK:
  60. n=0;
  61. m_oListtitle.SetWindowText("           ** 销售汇总报表 **");
  62. break;
  63. case IDC_RADIO_SINGLE:
  64. n=1;
  65. m_oListtitle.SetWindowText("             ** 单品报表 **");
  66. break;
  67. default:
  68. break;
  69. }
  70. int ncount=m_oListreport.GetCount()-1;
  71. for(int i=ncount;i>=0;i--)//Clear report listbox.
  72. m_oListreport.DeleteString(i);
  73. sql="Select * from COMPANY";
  74. try
  75. m_pRecordset.CreateInstance("ADODB.Recordset");
  76. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  77. if(!m_pRecordset->adoEOF)
  78. {
  79.  sfloor=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("FLOORNUMBER");
  80.  scompany=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("COMPANYNAME");
  81. }
  82. m_pRecordset->Close();
  83. }
  84. catch(_com_error e)///捕捉异常
  85. {
  86. CString sdate;
  87. sdate.Format("读取楼层号出错:%s",e.ErrorMessage());
  88. AfxMessageBox(sdate);
  89. }
  90. m_oListreport.AddString("                                      ");
  91. int ns=32-scompany.GetLength()-sfloor.GetLength();
  92. CString space=" ";
  93. for(int j=0;j<ns-1;j++)
  94. space=space+" ";
  95. m_oListreport.AddString(scompany+space+"楼层号 "+sfloor);
  96. m_oListreport.AddString("---------------------------------------");
  97. m_oSdate.GetWindowText(ssdate);
  98. ssdate=Dateformat(ssdate);
  99. m_oSt.GetWindowText(sstime);
  100. sbegin=ssdate+" "+sstime;
  101. m_oListreport.AddString("开始时间:           "+sbegin);
  102. m_oOdate.GetWindowText(sodate);
  103. sodate=Dateformat(sodate);
  104. m_oOt.GetWindowText(sotime);
  105. send=sodate+" "+sotime;
  106. m_oListreport.AddString("结束时间:           "+send);
  107. m_oListreport.AddString("---------------------------------------");
  108. sql="where ENDDATE>='"+sbegin+"' and ENDDATE<='"+send+"' and STATUS='已结帐'";
  109. if(n==0)
  110. SumBillTotal(sql);
  111. if(n==1)
  112. {
  113. m_oListreport.AddString("商品名称                           数量");
  114. SumSingleItem(sql);
  115. }
  116. }
  117. CString CStatDlg::Dateformat(CString sdate)
  118. {
  119. if(sdate.GetLength()==8)
  120. {
  121. sdate.Insert(7,'0');
  122. sdate.Insert(5,'0');
  123. }
  124. if(sdate.GetLength()==9)
  125. {
  126. if(sdate.GetAt(6)=='-')
  127. sdate.Insert(5,'0');
  128. else
  129. sdate.Insert(8,'0');
  130. }
  131. return sdate;
  132. }
  133. void CStatDlg::SumBillTotal(CString scondition)
  134. {
  135. float fsum,fttl;
  136. long lcount,lpersonqa,lbillqa;
  137. CString sql,ssum,scount,spersonqa,sbillqa;
  138. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  139. CString space,space1;
  140. int i,n;
  141. //count bills quantity.
  142. sql="Select COUNT(*) as XXX from SALEBILL "+scondition+"";
  143. try
  144. m_pRecordset.CreateInstance("ADODB.Recordset");
  145. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  146. lcount=(long)m_pRecordset->GetCollect("XXX");
  147. m_pRecordset->Close();
  148. }
  149. catch(_com_error e)///捕捉异常
  150. {
  151. CString stemp;
  152. stemp.Format("[报表]读取记录数出错:%s",e.ErrorMessage());
  153. AfxMessageBox(stemp);
  154. }
  155. if(lcount==0)
  156. {
  157. m_oListreport.AddString("人民币:                           0.00");
  158. return;
  159. }
  160. //Calculate sum acttotal , people's quantity and bill's quantity.
  161. sql="Select SUM(ACTTOTAL) as AAA,SUM(PEOPLES) as BBB,COUNT(*) as CCC from SALEBILL "+scondition+"";
  162. try
  163. m_pRecordset.CreateInstance("ADODB.Recordset");
  164. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  165. if(!m_pRecordset->adoEOF)
  166. {
  167. fsum =m_pRecordset->GetCollect("AAA");
  168. fttl=fsum;
  169. lpersonqa=m_pRecordset->GetCollect("BBB");
  170. lbillqa=m_pRecordset->GetCollect("CCC");
  171. }
  172. ssum.Format("%.2f",fsum);
  173. spersonqa.Format("%d",lpersonqa);
  174. sbillqa.Format("%d",lbillqa);
  175. n=31-ssum.GetLength();
  176. space=" ";
  177. for(i=0;i<n-1;i++)
  178. space=space+" ";
  179. m_oListreport.AddString("人民币:"+space+ssum);
  180. m_pRecordset->Close();
  181. }
  182. catch(_com_error e)///捕捉异常
  183. {
  184. CString stemp;
  185. stemp.Format("[报表]计算单据金额出错:%s",e.ErrorMessage());
  186. AfxMessageBox(stemp);
  187. }
  188. //Sum hang bills total.
  189. _variant_t vtemp;
  190. sql="Select SUM(TOTAL) as XXX from SALEBILL "+scondition+"";
  191. sql.Replace("已结帐","挂帐");
  192. sql.Replace("ENDDATE","BEGINDATE");
  193. try
  194. m_pRecordset.CreateInstance("ADODB.Recordset");
  195. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  196. vtemp=m_pRecordset->GetCollect("XXX");
  197. fsum=vtemp.dblVal;
  198. if(fsum>0)
  199. {
  200. fttl=fttl+fsum;
  201. ssum.Format("%.2f",fsum);
  202. }
  203. else
  204. ssum="0.00";
  205. n=33-ssum.GetLength();
  206. space=" ";
  207. for(i=0;i<n-1;i++)
  208. space=space+" ";
  209. m_oListreport.AddString("挂帐:"+space+ssum);
  210. m_pRecordset->Close();
  211. }
  212. catch(_com_error e)///捕捉异常
  213. {
  214. CString stemp;
  215. stemp.Format("[报表]计算单据挂帐金额出错:%s",e.ErrorMessage());
  216. AfxMessageBox(stemp);
  217. }
  218. //Sum RF bills total.
  219. sql="Select SUM(TOTAL) as XXX from SALEBILL "+scondition+" and PAYMODE=88";
  220. // sql="Select SUM(TOTAL) as XXX from (Select * from SALEBILL "+scondition+") where PAYMODE=88";
  221. try
  222. m_pRecordset.CreateInstance("ADODB.Recordset");
  223. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  224. vtemp=m_pRecordset->GetCollect("XXX");
  225. fsum=vtemp.dblVal;
  226. if(fsum>0)
  227. {
  228. fttl=fttl+fsum;
  229. ssum.Format("%.2f",fsum);
  230. }
  231. else
  232. ssum="0.00";
  233. n=35-ssum.GetLength();
  234. space=" ";
  235. for(i=0;i<n-1;i++)
  236. space=space+" ";
  237. m_oListreport.AddString("RF:"+space+ssum);
  238. m_oListreport.AddString("                       ----------------");
  239. CString sttl;
  240. sttl.Format("%.2f",fttl);
  241. n=38-sttl.GetLength();
  242. space=" ";
  243. for(i=0;i<n-1;i++)
  244. space=space+" ";
  245. m_oListreport.AddString(space+sttl);
  246. m_oListreport.AddString("                                       ");
  247. m_oListreport.AddString("类别                 汇总          折扣");
  248. m_pRecordset->Close();
  249. }
  250. catch(_com_error e)///捕捉异常
  251. {
  252. CString stemp;
  253. stemp.Format("[报表]计算单据RF金额出错:%s",e.ErrorMessage());
  254. AfxMessageBox(stemp);
  255. }
  256. //Calculate class's acttotal and valtotal.
  257. // sql="Select CLASS,Sum(ACTTOTAL) as XXX,Sum(VALTOTAL) as VVV from PAYDETAIL where BILLID in (Select ID from SALEBILL "+scondition+") group by CLASS ";
  258. sql="Select CLASS,Sum(ACTTOTAL) as XXX,Sum(VALTOTAL) as VVV, count(*) from PAYDETAIL where BILLID in (Select ID from SALEBILL "+scondition+") group by CLASS having count(*)>=1";
  259. try
  260. m_pRecordset.CreateInstance("ADODB.Recordset");
  261. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  262. float fat=0;
  263. float fvt=0;
  264. while(!m_pRecordset->adoEOF)
  265. {
  266. CString sclassname =(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("CLASS");
  267. float facttotal=m_pRecordset->GetCollect("XXX");
  268. float fvaltotal=m_pRecordset->GetCollect("VVV");
  269. fat=fat+facttotal;
  270. fvt=fvt+fvaltotal;
  271. CString sacttotal,svaltotal;
  272. sacttotal.Format("%.2f",facttotal);
  273. svaltotal.Format("%.2f",fvaltotal);
  274. n=25-sclassname.GetLength()-sacttotal.GetLength();
  275. space=" ";
  276. for(i=0;i<n-1;i++)
  277. space=space+" ";
  278. CString space1=" ";
  279. n=14-svaltotal.GetLength();
  280. for(i=0;i<n-1;i++)
  281. space1=space1+" ";
  282. m_oListreport.AddString(sclassname+space+sacttotal+space1+svaltotal);
  283. m_pRecordset->MoveNext();
  284. }
  285. m_oListreport.AddString("                                       ");
  286. CString sat,svt;
  287. sat.Format("%.2f",fat);
  288. svt.Format("%.2f",fvt);
  289. n=20-sat.GetLength();
  290. space=" ";
  291. for(i=0;i<n-1;i++)
  292. space=space+" ";
  293. space1=" ";
  294. n=14-svt.GetLength();
  295. for(i=0;i<n-1;i++)
  296. space1=space1+" ";
  297. m_oListreport.AddString("总计:"+space+sat+space1+svt);
  298. m_oListreport.AddString("                                       ");
  299. n=28-spersonqa.GetLength()-sbillqa.GetLength();
  300. space=" ";
  301. for(i=0;i<n-1;i++)
  302. space=space+" ";
  303. m_oListreport.AddString("人数:"+spersonqa+space+"单数:"+sbillqa);
  304. m_oListreport.AddString("                                       ");
  305. m_oListreport.AddString("-----------------未结单----------------");
  306. m_pRecordset->Close();
  307. }
  308. catch(_com_error e)///捕捉异常
  309. {
  310. CString stemp;
  311. stemp.Format("[报表]计算类别金额出错:%s",e.ErrorMessage());
  312. AfxMessageBox(stemp);
  313. }
  314. //Get non-check bill.
  315. float fnonttl=0;
  316. CString snontotal;
  317. sql="Select ID,TOTAL from SALEBILL where STATUS<>'已结帐'";
  318. try
  319. m_pRecordset.CreateInstance("ADODB.Recordset");
  320. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  321. while(!m_pRecordset->adoEOF)
  322. {
  323. CString snoncheck=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ID");
  324. float fnontotal=(float)m_pRecordset->GetCollect("TOTAL");
  325. snontotal.Format("%.2f",fnontotal);
  326. n=38-snoncheck.GetLength()-snontotal.GetLength();
  327. space=" ";
  328. for(i=0;i<n-1;i++)
  329. space=space+" ";
  330. m_oListreport.AddString(snoncheck+space+snontotal);
  331. fnonttl=fnonttl+fnontotal;
  332. m_pRecordset->MoveNext();
  333. }
  334. if(fnonttl!=0)
  335. {
  336. snontotal.Format("%.2f",fnonttl);
  337. n=38-snontotal.GetLength();
  338. space=" ";
  339. for(i=0;i<n-1;i++)
  340. space=space+" ";
  341. m_oListreport.AddString("                       ----------------");
  342. m_oListreport.AddString(space+snontotal);
  343. }
  344. m_pRecordset->Close();
  345. }
  346. catch(_com_error e)///捕捉异常
  347. {
  348. CString stemp;
  349. stemp.Format("[报表]读取未结单出错:%s",e.ErrorMessage());
  350. AfxMessageBox(stemp);
  351. }
  352. }
  353. void CStatDlg::SumSingleItem(CString scondition)
  354. {
  355. CString sql,sitemsum,smname;
  356. long litemsum,lmid;
  357. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  358. //Select suit records and insert them into SINGLEREPORT.
  359. sql="Delete * from SINGLEREPORT";
  360. try
  361. _variant_t RecordsAffected;
  362. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  363. sql="Insert into SINGLEREPORT (ITEMCOUNT,MATERIELID) Select ITEMCOUNT,MATERIELID from SALEDETAIL Where BILLID in (Select ID from SALEBILL "+scondition+")";
  364. theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
  365. }
  366. catch(_com_error e)///捕捉异常
  367. {
  368. CString stemp;
  369. stemp.Format("[单品]插入单品项目出错:%s",e.ErrorMessage());
  370. AfxMessageBox(stemp);
  371. return;
  372. }
  373. //Get records and insert them into report listbox.
  374. sql="Select MATERIELID,Sum(ITEMCOUNT) as XXX,count(*) from SINGLEREPORT group by MATERIELID having count(*)>=1";
  375. try
  376. m_pRecordset.CreateInstance("ADODB.Recordset");
  377. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
  378. while(!m_pRecordset->adoEOF)
  379. {
  380. litemsum =(long)m_pRecordset->GetCollect("XXX");
  381. sitemsum.Format("%6d",litemsum);
  382. lmid=(long)m_pRecordset->GetCollect("MATERIELID");
  383. smname=GetMaterielName(lmid);
  384. CString soutname;
  385. soutname.Format("%-32s",smname);
  386. // int n=38-sitemsum.GetLength()-smname.GetLength();
  387. // CString space=" ";
  388. // for(int i=0;i<n-1;i++)
  389. // space=space+" ";
  390. // m_oListreport.AddString(smname+space+sitemsum);
  391. m_oListreport.AddString(soutname+sitemsum);
  392. m_pRecordset->MoveNext();//Don't forget this function!!
  393. }
  394. m_pRecordset->Close();
  395. }
  396. catch(_com_error e)///捕捉异常
  397. {
  398. CString stemp;
  399. stemp.Format("[单品]计算单品数量出错:%s",e.ErrorMessage());
  400. AfxMessageBox(stemp);
  401. }
  402. }
  403. CString CStatDlg::GetMaterielName(long materielid)
  404. {
  405. _RecordsetPtr m_pRecordset; //Must define it in function!!!!
  406. CString sql,smaterielid,sname;
  407. smaterielid.Format("%d",materielid);
  408. sql="Select NAME from MATERIEL where ID="+smaterielid+"";
  409. try
  410. {
  411. m_pRecordset.CreateInstance("ADODB.Recordset");
  412. m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
  413. if(!m_pRecordset->adoEOF)
  414. sname=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("NAME");
  415. }
  416. catch(_com_error e)///捕捉异常
  417. {
  418. CString temp;
  419. temp.Format("获取商品名称出错:%s",e.ErrorMessage());
  420. AfxMessageBox(temp);
  421. }
  422. return sname;
  423. }
  424. void CStatDlg::OnButtonPrintreport() 
  425. {
  426. //获得行数
  427. int ncount = m_oListreport.GetCount();
  428. if(ncount==0)
  429. {
  430. MessageBox("无打印内容!");
  431. return;
  432. }
  433. int i;
  434. CDC dc;
  435. //把打印设备环境附加到DC对象
  436. CPrintDialog pdlg(FALSE,PD_NOPAGENUMS|PD_NOSELECTION,this);
  437. BOOL bFindPrinter=pdlg.GetDefaults();
  438. if(!bFindPrinter)
  439. return;
  440. dc.Attach(pdlg.GetPrinterDC());
  441. //取打印机的横方向和纵方向的分辨率
  442. //即每英寸点数
  443. short cxInch = dc.GetDeviceCaps(LOGPIXELSX);
  444. short cyInch = dc.GetDeviceCaps(LOGPIXELSY);
  445. //字体
  446. CFont font;
  447. VERIFY(font.CreatePointFont(96, "宋体", &dc));//为DC创建字体
  448. CFont* def_font = dc.SelectObject(&font);//保存现在的字体
  449. //根据字体宽度、高度计算每行最大字数及每页最大行数
  450. //取打印纸张高度和宽度
  451. int nPageHeight, nPageWidth;
  452. nPageHeight = dc.GetDeviceCaps(VERTRES);
  453. nPageWidth = dc.GetDeviceCaps(HORZRES);
  454. TEXTMETRIC TextM;
  455. dc.GetTextMetrics(&TextM);
  456. //字体高度 //字体平均宽度
  457. int nCharHeight = (unsigned short)TextM.tmHeight;
  458. int nCharWidth=(unsigned short)TextM.tmAveCharWidth;
  459. //每行最大字数 //每页最大行数
  460. int m_MaxLineChar = nPageWidth / nCharWidth - 8; 
  461. //每页最大行数
  462. int nMaxLinePerPage = nPageHeight/nCharHeight -3;
  463. int nCurPage =1;
  464. //页边距
  465. int nXMargin = 2;
  466. int nYMargin = 2;
  467. //设置所需打印纸张数目
  468. DOCINFO di;
  469. di.cbSize = sizeof(DOCINFO);
  470. di.lpszDocName = "Report printing"; 
  471. di.lpszOutput = (LPTSTR) NULL; 
  472. di.lpszDatatype = (LPTSTR) NULL; 
  473. di.fwType = 0; 
  474. dc.StartDoc(&di);
  475. dc.StartPage();
  476. //输出报表名称
  477. CString stitle,sftitle,space="";
  478. m_oListreport.GetWindowText(stitle);
  479. int n=(36-stitle.GetLength())/2;
  480. for(i=0;i<n;i++)
  481. space=space+" ";
  482. sftitle=space+stitle;
  483. dc.TextOut(2, nYMargin+nCharHeight,sftitle, strlen(sftitle));
  484. //输出报表内容
  485. CString sline;
  486. for(i=0; i<ncount; i++)
  487. {
  488. if(ncount+7-(nCurPage-1)*nMaxLinePerPage > nMaxLinePerPage)
  489. {
  490. //新的一页
  491. dc.EndPage();
  492. dc.StartPage();
  493. nCurPage ++;
  494. }
  495. m_oListreport.GetText(i, sline);//得到选中项的文本
  496. dc.TextOut( 2, nYMargin+(i+1-(nCurPage-1)*nMaxLinePerPage)*nCharHeight, 
  497. sline, strlen(sline));
  498. }
  499. dc.EndPage();
  500. dc.EndDoc();
  501. //打印结束
  502. //最后不要忘记将字体还原,这一句是必需的
  503. dc.SelectObject(def_font); //恢复原来的字体
  504. font.DeleteObject(); 
  505. DeleteDC(dc.Detach()); 
  506. }