📄 salestatview.cpp
字号:
// SaleStatView.cpp : implementation file
//
#include "stdafx.h"
#include "Remedy.h"
#include "SaleStatView.h"
#include "MainFrm.h"
#include "SetPrintDlg.h"
#include "msword8.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// 定义静态变量
const int CSaleStatView::m_nFieldCount = 12;
const CString CSaleStatView::m_strFields[m_nFieldCount] =
{"品名","规格","剂型","产地", "数量","单位","进价","零售价","售价","金额","日期","购买单位"};
// 声明全局变量
extern CRemedyApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CSaleStatView
IMPLEMENT_DYNCREATE(CSaleStatView, CFormView)
CSaleStatView::CSaleStatView()
: CFormView(CSaleStatView::IDD)
{
//{{AFX_DATA_INIT(CSaleStatView)
m_strBuyer = _T("");
m_nDay = 0;
m_nDay2 = 0;
m_nMonth = 0;
m_nMonth2 = 0;
m_nYear = 0;
m_nYear2 = 0;
m_strMedName = _T("");
//}}AFX_DATA_INIT
}
CSaleStatView::~CSaleStatView()
{
}
void CSaleStatView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSaleStatView)
DDX_Control(pDX, IDC_LIST, m_ctlList);
DDX_Text(pDX, IDC_BUYER, m_strBuyer);
DDX_Text(pDX, IDC_DAY, m_nDay);
DDX_Text(pDX, IDC_DAY2, m_nDay2);
DDX_Text(pDX, IDC_MONTH, m_nMonth);
DDX_Text(pDX, IDC_MONTH2, m_nMonth2);
DDX_Text(pDX, IDC_YEAR, m_nYear);
DDX_Text(pDX, IDC_YEAR2, m_nYear2);
DDX_Text(pDX, IDC_MEDNAME, m_strMedName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSaleStatView, CFormView)
//{{AFX_MSG_MAP(CSaleStatView)
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_VIEWRESULT, OnViewresult)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_PRINTRESULT, OnPrintresult)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSaleStatView diagnostics
#ifdef _DEBUG
void CSaleStatView::AssertValid() const
{
CFormView::AssertValid();
}
void CSaleStatView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSaleStatView message handlers
void CSaleStatView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->SetWindowText("药品销售统计");
AddListHeader();
CTime t= CTime::GetCurrentTime();
m_nYear = m_nYear2 = t.GetYear();
m_nMonth = m_nMonth2 = t.GetMonth();
m_nDay = m_nDay2 = t.GetDay();
UpdateData(FALSE);
m_nType = 1; // 默认是按月统计
CheckDlgButton(IDC_RADIO1, 1);
GetDlgItem(IDC_DAY)->EnableWindow(FALSE);
GetDlgItem(IDC_DAY2)->EnableWindow(FALSE);
GetDlgItem(IDC_MONTH2)->EnableWindow(FALSE);
GetDlgItem(IDC_YEAR2)->EnableWindow(FALSE);
HRESULT hr = m_pRecordset.CreateInstance("ADODB.Recordset");
if (!SUCCEEDED(hr))
{
MessageBox("创建Recordset对象失败!");
}
}
void CSaleStatView::OnDestroy()
{
CFormView::OnDestroy();
((CMainFrame*)AfxGetMainWnd())->m_pSaleStat = NULL; // 清空窗口指针
}
void CSaleStatView::AddListHeader()
{
int width[m_nFieldCount] = {130, 110, 80, 120, 58, 60, 70, 70, 68, 68, 69, 70};
m_ctlList.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
for (int i = 0; i < m_nFieldCount; i++)
m_ctlList.InsertColumn(i, m_strFields[i], LVCFMT_CENTER, width[i]);
}
void CSaleStatView::OnRadio1()
{
GetDlgItem(IDC_DAY)->EnableWindow(FALSE);
GetDlgItem(IDC_DAY2)->EnableWindow(FALSE);
GetDlgItem(IDC_MONTH2)->EnableWindow(FALSE);
GetDlgItem(IDC_YEAR2)->EnableWindow(FALSE);
GetDlgItem(IDC_YEAR)->SetFocus();
m_nType = 1;
}
void CSaleStatView::OnRadio2()
{
GetDlgItem(IDC_DAY)->EnableWindow(TRUE);
GetDlgItem(IDC_DAY2)->EnableWindow(TRUE);
GetDlgItem(IDC_MONTH2)->EnableWindow(TRUE);
GetDlgItem(IDC_YEAR2)->EnableWindow(TRUE);
GetDlgItem(IDC_YEAR)->SetFocus();
m_nType = 2;
}
void CSaleStatView::OnViewresult()
{
UpdateData();
if (m_ctlList.GetItemCount() > 0)
m_ctlList.DeleteAllItems();
char buf[MAX_PATH];
sprintf(buf,
"select * from 销售 where 品名 like \'%s\' and 购买单位 like \'%s\'",
m_strMedName + "%",m_strBuyer + "%");
m_pRecordset->Open( _variant_t(buf),
_variant_t((IDispatch*)theApp.m_pConnection,true),
adOpenStatic, adLockOptimistic, adCmdText);
if (m_pRecordset->adoEOF)
{
m_pRecordset->Close();
return;
}
_variant_t values[m_nFieldCount];
int index = 0; // 行号
while(!m_pRecordset->adoEOF)
{
CString strValues[m_nFieldCount];
_variant_t field;
for (int i = 0; i < m_nFieldCount; i++) // 依次读取每个字段的值
{
field.SetString(LPCTSTR(m_strFields[i]));
values[i] = m_pRecordset->GetCollect(field);
field.Clear();
strValues[i] = ::GetString(values[i]);
}
UINT nYear = ::GetYear(strValues[m_nFieldCount - 2]);
UINT nMonth = ::GetMonth(strValues[m_nFieldCount - 2]);
UINT nDay = ::GetDay(strValues[m_nFieldCount - 2]);
if (m_nType == 1)
{
if (m_nYear == nYear && m_nMonth == nMonth)
{
m_ctlList.InsertItem(index,strValues[0]);
for (int j = 1; j < m_nFieldCount; j++)
m_ctlList.SetItemText(index,j,strValues[j]);
index++;
}
}
else
{
CTime t1(m_nYear, m_nMonth, m_nDay, 0, 0 ,0);
CTime t2(m_nYear2, m_nMonth2, m_nDay2, 0, 0 ,0);
CTime t(nYear, nMonth, nDay, 0 ,0 ,0);
if (t >= t1 && t <= t2)
{
m_ctlList.InsertItem(index,strValues[0]);
for (int j = 1; j < m_nFieldCount; j++)
m_ctlList.SetItemText(index,j,strValues[j]);
index++;
}
}
m_pRecordset->MoveNext();
}
m_pRecordset->Close();
GetTotalMoney();
}
void CSaleStatView::OnPrintresult()
{
int nItemCount = m_ctlList.GetItemCount();
if (nItemCount == 0)
{
MessageBox("无打印内容!", "提示", MB_ICONINFORMATION);
return;
}
CSetPrintDlg dlg;
dlg.m_nTotalPage = nItemCount / 24 + (nItemCount % 24 == 0 ? 0 : 1);
if (dlg.DoModal() != IDOK) return;
int nCurPage = dlg.m_nCurPage; // 当前页
_Application WordApp;
if (!WordApp.CreateDispatch("Word.Application",NULL))
{
AfxMessageBox("创建MS-WORD服务失败!");
return;
}
WordApp.SetVisible(true); // 过程可视化
Documents docs;
_Document doc;
Range range;
Bookmarks bookmarks;
Bookmark bookmark;
Table table;
Tables tables;
docs=WordApp.GetDocuments();
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
COleVariant start_line,end_line;
// 获取当前目录
char szPath[MAX_PATH];
::GetCurrentDirectory(MAX_PATH, szPath);
strcat(szPath, "\\零售凭证.dot");
COleVariant Dot(szPath);
doc = docs.Add(&Dot,&covOptional);
bookmarks = doc.GetBookmarks();
COleVariant tem1("buyer"); // 单位,即购买者
bookmark=bookmarks.Item(&tem1);
range=bookmark.GetRange();
range.SetText(m_strBuyer);
CTime t =CTime::GetCurrentTime();
CString strTemp;
COleVariant tem2("year");
bookmark=bookmarks.Item(&tem2);
range=bookmark.GetRange();
strTemp.Format("%d",t.GetYear());
range.SetText(strTemp);
COleVariant tem3("month");
bookmark=bookmarks.Item(&tem3);
range=bookmark.GetRange();
strTemp.Format("%d",t.GetMonth());
range.SetText(strTemp);
COleVariant tem4("day");
bookmark=bookmarks.Item(&tem4);
range=bookmark.GetRange();
strTemp.Format("%d",t.GetDay());
range.SetText(strTemp);
tables = doc.GetTables();
table = tables.Item(1);
double fTotal = 0.0;
int start = (nCurPage - 1) * 24; // 每页至多24行
for (int i = 0; i < 24; i++)
{
for (int j = 0; j < 9; j++)
{
Cell cell = table.Cell(i + 2, j + 1);
range = cell.GetRange();
if (j < 6) range.SetText(m_ctlList.GetItemText(start, j));
else range.SetText(m_ctlList.GetItemText(start, j + 1));
}
// 计算总金额
double curPrice;
CString strTemp = m_ctlList.GetItemText(start, 9);
curPrice = atof(LPCTSTR(strTemp));
fTotal += curPrice;
start++;
if (start == m_ctlList.GetItemCount()) break;
}
COleVariant tem5("total"); // 合计金额
bookmark=bookmarks.Item(&tem5);
range=bookmark.GetRange();
CString strTotal;
strTotal.Format("%.2f", fTotal);
range.SetText(strTotal);
}
void CSaleStatView::GetTotalMoney()
{
double fTotal = 0.0;
double fProfit = 0.0;
for (int i = 0; i < m_ctlList.GetItemCount(); i++)
{
double curPrice;
CString strTemp = m_ctlList.GetItemText(i,9);
curPrice = atof(LPCTSTR(strTemp));
fTotal += curPrice;
CString strQuantity = m_ctlList.GetItemText(i,4);
CString strInPrice = m_ctlList.GetItemText(i,6);
fProfit += (curPrice - atoi(LPCTSTR(strQuantity)) * atof(LPCTSTR(strInPrice)));
}
CString str1,str2;
str1.Format("%.2f",fTotal);
SetDlgItemText(IDC_TMONEY,str1);
str2.Format("%.2f",fProfit);
SetDlgItemText(IDC_TPROFIT,str2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -