📄 posdlg.cpp
字号:
}
m_oListbill.DeleteString(iOption);
if(m_oListbill.GetCount()==0)
m_oFormlist.DeleteAllItems();
if(m_oListbill.GetCount()>0)
{
if(m_oListbill.GetCount()>iOption)
m_oListbill.SetCurSel(iOption);
if(m_oListbill.GetCount()==iOption)
m_oListbill.SetCurSel(iOption-1);
iOption = m_oListbill.GetCurSel();
m_oListbill.GetText(iOption, sbillid);//Again得到选中项的文本
theApp.scallid=sbillid;//Get current bill id.
ReadBillHead(sbillid);
ReadToFormlist(sbillid);//Refresh listctrl.
}
}
void CPosDlg::OnButtonCheckf()
{
int iOption;
iOption = m_oListbill.GetCurSel();
if(iOption==-1)
return;
m_oListbill.GetText(iOption, theApp.scallid);//得到选中项的文本
if(bGetBillStatus(theApp.scallid))
{
AfxMessageBox("已结帐单据不能再结帐!");
return;
}
if(m_oFormlist.GetItemCount()==0)
{
AfxMessageBox("不能结帐空单据!");
return;
}
else
{
CCheckDlg checkdlg;
checkdlg.DoModal();
}
}
void CPosDlg::OnMyCheckMessage()
{
//When user click the button of checkdlg
//it send a message to posdlg to do the following things.
OnButtonLocalp();
OnButtonHidef();
}
CString CPosDlg::SumItemtotal(CString sbillid)
{
//Sum bill Items money from database.
CString stotal,sql;
float ftotal;
long lcount;
_RecordsetPtr m_pRecordset;
sql="Select count(*) as XXX from SALEDETAIL where BILLID ='"+sbillid+"'";
lcount=GetRecordCount(sql);
if(lcount==0)
{
stotal="0.00";
return stotal;
}
sql="Select SUM(ITEMTOTAL) as SSS from SALEDETAIL where BILLID='"+sbillid+"'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
ftotal=(float)m_pRecordset->GetCollect("SSS");
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("[前台销售]计算商品总金额出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
}
stotal.Format("%.2f",ftotal);
return stotal;
}
void CPosDlg::ReadBillHead(CString sbillid)
{
CString sql,status;
_RecordsetPtr m_pRecordset;
sql="select * from SALEBILL where ID='"+sbillid+"'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
m_oTableno.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("TABLENO"));
m_oPeasons.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("PEOPLES"));
m_oCheck.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("SALES"));
m_oStatus.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("STATUS"));
m_oBegintime.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("BEGINDATE"));
m_oEndtime.SetWindowText((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ENDDATE"));
float ftotal=(float)m_pRecordset->GetCollect("TOTAL");
CString stotal;
stotal.Format("%.2f",ftotal);
m_oTotal.SetWindowText(stotal);
ftotal=(float)m_pRecordset->GetCollect("ACTTOTAL");
stotal.Format("%.2f",ftotal);
m_oActtotal.SetWindowText(stotal);
}
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("读取单据头到编辑框出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
BOOL CPosDlg::bGetBillStatus(CString sbillid)
{
CString sql,sstatus;
BOOL bstatus;
_RecordsetPtr m_pRecordset;
sql="select STATUS from SALEBILL where ID='"+sbillid+"'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
sstatus=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("STATUS");
if(sstatus=="等待"||sstatus=="挂帐")
bstatus=FALSE;
else
bstatus=TRUE;
}
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("读取单据状态出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
}
return bstatus;
}
void CPosDlg::OnKillfocusEditTableno()
{
CString sql,stemp,sbillid;
_RecordsetPtr m_pRecordset;
int iOption;
iOption = m_oListbill.GetCurSel();
m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
if(bGetBillStatus(theApp.scallid))
return;
m_oTableno.GetWindowText(stemp);
sql="Update SALEBILL set TABLENO='"+stemp+"' where ID='"+sbillid+"'";
try
{ _variant_t RecordsAffected;
theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("更新单据头(桌号) 出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
void CPosDlg::OnKillfocusEditPeasons()
{
CString sql,stemp,sbillid;
_RecordsetPtr m_pRecordset;
int iOption;
iOption = m_oListbill.GetCurSel();
m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
if(bGetBillStatus(theApp.scallid))
return;
m_oPeasons.GetWindowText(stemp);
sql="Update SALEBILL set PEOPLES='"+stemp+"' where ID='"+sbillid+"'";
try
{ _variant_t RecordsAffected;
theApp.m_pConnection->Execute((_bstr_t)sql,&RecordsAffected,adCmdText);
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("更新单据头(人数)出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
return;
}
}
void CPosDlg::OnButtonOff()
{
COffdutyDlg offdlg;
offdlg.DoModal();
}
void CPosDlg::OnButtonFindf()
{
CString sql,sbillid;
_RecordsetPtr m_pRecordset;
CCallDlg calldlg;//Create a dialog to enter billid to call.
calldlg.dowhat="pleased";
calldlg.DoModal();
sql="select ID from SALEBILL where STATUS<>'已结帐' and TABLENO='"+calldlg.dowhat+"'";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
sbillid=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("ID");
m_oListbill.SelectString(-1,sbillid);
ReadToFormlist(sbillid);
}
if(m_pRecordset->adoEOF)
{
CString temp="桌号为 '"+calldlg.dowhat+"' 单据不存在!";
AfxMessageBox(temp);
}
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString temp;
temp.Format("查找桌号出错:%s",e.ErrorMessage());
AfxMessageBox(temp);
}
}
BOOL CPosDlg::PrintListCtrl(CListCtrl &list)
{
CDC dc;
//把打印设备环境附加到DC对象
CPrintDialog pdlg(FALSE,PD_NOPAGENUMS|PD_NOSELECTION,this);
BOOL bFindPrinter=pdlg.GetDefaults();
if(!bFindPrinter)
return FALSE;
dc.Attach(pdlg.GetPrinterDC());
//取打印机的横方向和纵方向的分辨率
//即每英寸点数
short cxInch = dc.GetDeviceCaps(LOGPIXELSX);
short cyInch = dc.GetDeviceCaps(LOGPIXELSY);
//字体
CFont font;
VERIFY(font.CreatePointFont(96, "宋体", &dc));//为DC创建字体
CFont* def_font = dc.SelectObject(&font);//保存现在的字体
//根据字体宽度、高度计算每行最大字数及每页最大行数
//取打印纸张高度和宽度
int nPageHeight, nPageWidth;
nPageHeight = dc.GetDeviceCaps(VERTRES);
nPageWidth = dc.GetDeviceCaps(HORZRES);
TEXTMETRIC TextM;
dc.GetTextMetrics(&TextM);
//字体高度 //字体平均宽度
int nCharHeight = (unsigned short)TextM.tmHeight;
int nCharWidth=(unsigned short)TextM.tmAveCharWidth;
//每行最大字数 //每页最大行数
int m_MaxLineChar = nPageWidth / nCharWidth - 8;
int m_LinesPerPage = nPageHeight/ nCharHeight;
//页边距
int nXMargin = 2;
int nYMargin = 2;
//获得行数
int nLineCount = list.GetItemCount();
int i;
//设置所需打印纸张数目
DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Mypos printing";
di.lpszOutput = (LPTSTR) NULL;
di.lpszDatatype = (LPTSTR) NULL;
di.fwType = 0;
dc.StartDoc(&di);
dc.StartPage();
//输出公司名称
CString stitle,sftitle,space="";
stitle=GetCompanyFloor(TRUE);
int n=(36-stitle.GetLength())/2;
for(i=0;i<n;i++)
space=space+" ";
sftitle=space+stitle;
dc.TextOut(2, nYMargin+nCharHeight,sftitle, strlen(sftitle));
//输出单号
CString sbillid;
int iOption = m_oListbill.GetCurSel();
m_oListbill.GetText(iOption, sbillid);//得到选中项的文本
sbillid="单号:"+sbillid;
dc.TextOut( 2, nYMargin+nCharHeight*2, sbillid, strlen(sbillid));
//输出桌号、开台时间、人数、结帐时间、收银、付款方式
CString stable,speople,sbegin,ssale,stable1,speople1,send,spaymode;
m_oTableno.GetWindowText(stable);
stable1.Format("%-8s",stable);//Table No.
m_oPeasons.GetWindowText(speople);
speople1.Format("%-8s",speople);//People quantity.
m_oBegintime.GetWindowText(sbegin);
m_oCheck.GetWindowText(ssale);//Checkman.
spaymode.Format("%-15s",theApp.spaymodep);
if(ssale=="无")
{
ssale.Format("%-8s",theApp.name);
spaymode="";
}
m_oEndtime.GetWindowText(send);//End time.
if(send=="无")
send=theApp.snowtimep;
stable="开台:"+sbegin+" 桌号:"+stable1;
speople="结账:"+send+" 人数:"+speople1;
ssale="付款方式:"+spaymode+" 收银:"+ssale;
dc.TextOut( 2, nYMargin+nCharHeight*3, stable, strlen(stable));
dc.TextOut( 2, nYMargin+nCharHeight*4, speople, strlen(speople));
dc.TextOut( 2, nYMargin+nCharHeight*5, ssale, strlen(ssale));
//输出列表控件的列标题
CString slisttitle;
slisttitle="名称 数量 金额";
dc.TextOut( 2, nYMargin+nCharHeight*7,slisttitle, strlen(slisttitle));
int nMaxLinePerPage = nPageHeight/nCharHeight -3;//每页最大行数
int nCurPage =1;
//输出各列的数据
CString subitem,sfsub,sfsub1,sfsub2;
for(i =0; i<nLineCount; i++)
{
if(nLineCount+7-(nCurPage-1)*nMaxLinePerPage > nMaxLinePerPage)
{
//新的一页
dc.EndPage();
dc.StartPage();
nCurPage ++;
}
subitem= list.GetItemText(i, 1);
sfsub.Format("%-20s",subitem);
subitem= list.GetItemText(i, 2);
sfsub1.Format("%6s",subitem);
subitem= list.GetItemText(i, 3);
sfsub2.Format("%10s",subitem);
sfsub=sfsub+sfsub1+sfsub2;
dc.TextOut( 2, nYMargin+(i+8-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,
sfsub, strlen(sfsub));
}
stitle="------------------------------------";
dc.TextOut( 2, nYMargin+(nLineCount+8-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,
stitle, strlen(stitle));
//输出总金额
CString stotal,sacttotal,sdiscount;
float fdiscount;
stotal.Format("%27s",theApp.sconsume);
sacttotal.Format("%27s",theApp.sactsum);
fdiscount=atof(stotal)-atof(sacttotal);
sdiscount.Format("%31.2f",fdiscount);
stotal="消费金额:"+stotal;
sdiscount="折扣:"+sdiscount;
sacttotal="应收金额:"+sacttotal;
dc.TextOut( 2, nYMargin+(nLineCount+9-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,
stotal, strlen(stotal));
dc.TextOut( 2, nYMargin+(nLineCount+10-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,
sdiscount, strlen(sdiscount));
dc.TextOut( 2, nYMargin+(nLineCount+11-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,
sacttotal, strlen(sacttotal));
stitle="------------------------------------";
dc.TextOut( 2, nYMargin+(nLineCount+12-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,
stitle, strlen(stitle));
sacttotal="谢谢惠顾,欢迎再次光临!";
dc.TextOut( 2, nYMargin+(nLineCount+13-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,
sacttotal, strlen(sacttotal));
dc.EndPage();
dc.EndDoc();
//打印结束
//最后不要忘记将字体还原,这一句是必需的
dc.SelectObject(def_font); //恢复原来的字体
font.DeleteObject();
DeleteDC(dc.Detach());
return TRUE;
}
CString CPosDlg::GetCompanyFloor(BOOL bCompany)
{
_RecordsetPtr m_pRecordset; //Must define it in function!!!!
CString sql,scompany,sfloor;
sql="Select * from COMPANY";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(!m_pRecordset->adoEOF)
{
scompany=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("COMPANYNAME");
sfloor=(LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("FLOORNUMBER");
}
m_pRecordset->Close();
}
catch(_com_error e)///捕捉异常
{
CString stemp;
if(!bCompany)
stemp.Format("[初始化]读取楼层号出错:%s",e.ErrorMessage());
if(bCompany)
stemp.Format("[打印]读取公司名出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
}
if(!bCompany)
return sfloor;
if(bCompany)
return scompany;
}
void CPosDlg::OnButtonLocalp()
{
PrintListCtrl(m_oFormlist);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -