📄 buyoperview.cpp
字号:
// BuyOperView.cpp : implementation file
//
#include "stdafx.h"
#include "Remedy.h"
#include "BuyOperView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// 定义静态成员变量
const int CBuyOperView::m_nFieldCount = 9;
const CString CBuyOperView::m_strFields[m_nFieldCount] =
{"品名", "规格", "剂型", "产地", "数量", "单位", "进价", "零售价","备注"};
// 声明全局对象theApp
extern CRemedyApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CBuyOperView
IMPLEMENT_DYNCREATE(CBuyOperView, CFormView)
CBuyOperView::CBuyOperView()
: CFormView(CBuyOperView::IDD)
{
//{{AFX_DATA_INIT(CBuyOperView)
m_strMedName = _T("");
m_strMemo = _T("");
m_strLocation = _T("");
m_strInPrice = _T("");
m_strOutPrice = _T("");
m_strSpecification = _T("");
m_strType = _T("");
m_strUnit = _T("");
m_strNewQuantity = _T("");
m_strWhere = _T("");
//}}AFX_DATA_INIT
}
CBuyOperView::~CBuyOperView()
{
}
void CBuyOperView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBuyOperView)
DDX_Control(pDX, IDC_LIST2, m_ctlList2);
DDX_Control(pDX, IDC_LIST1, m_ctlList1);
DDX_Text(pDX, IDC_MEDNAME, m_strMedName);
DDX_Text(pDX, IDC_MEMO, m_strMemo);
DDX_Text(pDX, IDC_LOCATION, m_strLocation);
DDX_Text(pDX, IDC_INPRICE, m_strInPrice);
DDX_Text(pDX, IDC_OUTPRICE, m_strOutPrice);
DDX_Text(pDX, IDC_SPECIFICATION, m_strSpecification);
DDX_CBString(pDX, IDC_COMBO1, m_strType);
DDX_Text(pDX, IDC_UNIT, m_strUnit);
DDX_Text(pDX, IDC_NEWQUANTITY, m_strNewQuantity);
DDX_Text(pDX, IDC_WHERE, m_strWhere);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBuyOperView, CFormView)
//{{AFX_MSG_MAP(CBuyOperView)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
ON_BN_CLICKED(IDC_MODIFY, OnModify)
ON_BN_CLICKED(IDC_APPEND, OnAppend)
ON_BN_CLICKED(IDC_QUERY, OnQuery)
ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBuyOperView diagnostics
#ifdef _DEBUG
void CBuyOperView::AssertValid() const
{
CFormView::AssertValid();
}
void CBuyOperView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CBuyOperView message handlers
void CBuyOperView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->SetWindowText("药品进货操作");
GetDlgItem(IDC_MODIFY)->EnableWindow(TRUE);
GetDlgItem(IDC_APPEND)->EnableWindow(FALSE);
CheckDlgButton(IDC_RADIO1, 1);
// 显示进货时间
CString strTime;
CTime t = CTime::GetCurrentTime();
strTime.Format("%d年%d月%d日",t.GetYear(),t.GetMonth(),t.GetDay());
SetDlgItemText(IDC_EDIT9,strTime);
AddListHeader();
// 实例化纪录集组件对象
HRESULT hr = m_pRecordset.CreateInstance("ADODB.Recordset");
if (!SUCCEEDED(hr))
{
MessageBox("创建Recordset对象失败!");
}
m_nItem = -1;
}
void CBuyOperView::OnDestroy()
{
CFormView::OnDestroy();
((CMainFrame*)AfxGetMainWnd())->m_pBuyOper = NULL; // 清空窗口指针
}
//-----------------------------------------------------------------------------
// 功能: 当用户点击Radio1的时候,仅允许修改记录,添加记录功能无效
// 作者: 刘文澜
// 日期: 2004/5/1
//-----------------------------------------------------------------------------
void CBuyOperView::OnRadio1()
{
GetDlgItem(IDC_MODIFY)->EnableWindow(TRUE);
GetDlgItem(IDC_APPEND)->EnableWindow(FALSE);
}
//-----------------------------------------------------------------------------
// 功能: 当用户点击Radio2的时候,仅允许添加记录,修改记录功能无效
// 作者: 刘文澜
// 日期: 2004/5/1
//-----------------------------------------------------------------------------
void CBuyOperView::OnRadio2()
{
GetDlgItem(IDC_MODIFY)->EnableWindow(FALSE);
GetDlgItem(IDC_APPEND)->EnableWindow(TRUE);
}
//-----------------------------------------------------------------------------
// 功能: 为List1和List2控件增加表头
// 作者: 刘文澜
// 日期: 2004/5/1
//-----------------------------------------------------------------------------
void CBuyOperView::AddListHeader()
{
int width[m_nFieldCount] = {160, 120, 80, 140, 73, 73, 80, 80, 122};
m_ctlList1.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
m_ctlList2.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
for (int i = 0; i < m_nFieldCount; i++)
{
m_ctlList1.InsertColumn(i, m_strFields[i], LVCFMT_CENTER, width[i]);
m_ctlList2.InsertColumn(i, m_strFields[i], LVCFMT_CENTER, width[i]);
}
}
//-----------------------------------------------------------------------------
// 功能: 把当前用户输入名字的全部药品显示到List1控件中,以便选择
// 备注: 因为品名不是主键,所以对应一个名字有多条记录
// 作者: 刘文澜
// 日期: 2004/5/1
//-----------------------------------------------------------------------------
void CBuyOperView::OnQuery()
{
m_nItem = -1; // 选择项先复位
UpdateData();
if (m_strMedName.IsEmpty())
{
MessageBox("请输入要查询的品名!", "提示", MB_ICONINFORMATION);
GetDlgItem(IDC_MEDNAME)->SetFocus();
return;
}
m_strUserInput = m_strMedName; // 记住用户输入的查询的名字
m_ctlList1.DeleteAllItems(); // 清空List1的所有内容
char szSQL[100]; // 查询的SQL语句
sprintf(szSQL, "select * from 库存 where 品名 like \'%s\'", m_strUserInput + "%");
m_pRecordset->Open( _variant_t(szSQL),
_variant_t((IDispatch*)theApp.m_pConnection,true),
adOpenStatic, adLockOptimistic, adCmdText);
if (m_pRecordset->adoEOF) // 说明查找的结果为空,则只能进行添加操作
{
m_pRecordset->Close();
MessageBox("此品种不存在,只能作为新品种添加到库中", "提示", MB_ICONINFORMATION);
CheckDlgButton(IDC_RADIO1, 0);
CheckDlgButton(IDC_RADIO2, 1);
GetDlgItem(IDC_MODIFY)->EnableWindow(FALSE);
GetDlgItem(IDC_APPEND)->EnableWindow(TRUE);
GetDlgItem(IDC_SPECIFICATION)->SetFocus();
return;
}
int index = 0;
_variant_t values[m_nFieldCount];
_variant_t fieldName;
while (!m_pRecordset->adoEOF) // 依次显示每条记录
{
for (int i = 0; i < m_nFieldCount; i++)
{
fieldName.SetString(LPCTSTR(m_strFields[i]));
values[i] = m_pRecordset->GetCollect(fieldName);
fieldName.Clear();
if (i == 0)
m_ctlList1.InsertItem(index,(LPCTSTR)::GetString(values[i]));
else
m_ctlList1.SetItemText(index, i, (LPCTSTR)::GetString(values[i]));
}
m_pRecordset->MoveNext();
index++;
}
m_pRecordset->Close();
}
//-----------------------------------------------------------------------------
// 功能: 更改库存表和进货表,不在库存表中增加记录,在原有记录的基础上修改
// 备注: 进货表总是每次都添加一条记录
// 作者: 刘文澜
// 日期: 2004/5/1
//-----------------------------------------------------------------------------
void CBuyOperView::OnModify()
{
if (CheckUserInput() == FALSE) // 发现错误则不进行操作
return;
if (m_nItem == -1)
{
MessageBox("请选择一个品种进行修改!", "提示", MB_ICONINFORMATION);
return;
}
CString strValues[m_nFieldCount];
strValues[0] = m_strMedName;
strValues[1] = m_strSpecification;
strValues[2] = m_strType;
strValues[3] = m_strLocation;
strValues[4] = m_strNewQuantity;
strValues[5] = m_strUnit;
strValues[6] = m_strInPrice;
strValues[7] = m_strOutPrice;
strValues[8] = m_strMemo;
// 打开库存表,修改记录
char buf[100];
sprintf(buf, "select * from 库存 where 品名 like \'%s\'", m_strUserInput + "%");
m_pRecordset->Open( _variant_t(buf),
_variant_t((IDispatch*)theApp.m_pConnection,true),
adOpenStatic, adLockOptimistic, adCmdText);
// 移动到当前记录
for (int i = 0; i < m_nItem; i++)
m_pRecordset->MoveNext();
int nQuantity = 0; // 进货以后的数量
_variant_t vt1,vt2;
for (i = 0; i < m_nFieldCount; i++)
{
vt1.SetString((LPCTSTR)m_strFields[i]);
if (m_strFields[i] == "数量") // 如果是数量字段
{
_variant_t vQuantity = m_pRecordset->GetCollect(vt1);
CString strTemp = ::GetString(vQuantity);
// 最终的数量为原有数量加上新进的数量
nQuantity = atoi(LPCTSTR(strTemp)) + atoi(LPCTSTR(m_strNewQuantity));
strTemp.Format("%d",nQuantity);
vt2.SetString(LPCTSTR(strTemp));
}
else // 其他字段
{
if (strValues[i].IsEmpty())
vt2.vt = VT_NULL;
else
vt2.SetString((LPCTSTR)strValues[i]);
}
m_pRecordset->PutCollect(vt1, vt2);
vt1.Clear();
vt2.Clear();
}
m_pRecordset->Update();
m_pRecordset->Close();
// 修改进货后List1的显示,主要是数量
for (i = 0; i < m_nFieldCount; i++)
{
if (m_strFields[i] == "数量")
{
CString strTemp;
strTemp.Format("%d", nQuantity);
m_ctlList1.SetItemText(m_nItem, i, strTemp);
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -