⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 billdlg.cpp

📁 物流单据管理系统 我本人费了两个月才完成的 是信息系统与分析的课程设计
💻 CPP
字号:
// BillDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Yao.h"
#include "BillDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBillDlg dialog


CBillDlg::CBillDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBillDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBillDlg)
	m_strAccount = _T("");
	m_strBillID = _T("");
	m_strClient = _T("");
	m_strComment = _T("");
	m_strStorage = _T("");
	m_tTime = CTime::GetCurrentTime();;
	m_strOperator = _T("");
	//}}AFX_DATA_INIT
	m_nRows = 16;
	m_nCols = 6;
	m_nFixRows = 1;
	m_nFixCols = 0;

	m_nNameCol = 0;
	m_nUnitCol = 1;
	m_nAmountCol = 2;
	m_nPriceCol = 3;
	m_nMoneyCol = 4;
	m_nCommentCol = 5;

	m_nBillType = 1;//销售单
	m_bShowBill = FALSE;
}


void CBillDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBillDlg)
	DDX_Control(pDX, IDC_EXIT, m_btExit);
	DDX_Control(pDX, IDC_SAVE, m_btSave);
	DDX_Text(pDX, IDC_ACCOUNT, m_strAccount);
	DDX_Text(pDX, IDC_BILL_ID, m_strBillID);
	DDX_Text(pDX, IDC_CLIENT, m_strClient);
	DDX_Text(pDX, IDC_COMMENT, m_strComment);
	DDX_Text(pDX, IDC_STORAGE, m_strStorage);
	DDX_DateTimeCtrl(pDX, IDC_TIME, m_tTime);
	DDX_Text(pDX, IDC_OPERATOR, m_strOperator);
	//}}AFX_DATA_MAP
	DDX_GridControl(pDX, IDC_GRID, m_Grid);
}


BEGIN_MESSAGE_MAP(CBillDlg, CDialog)
	//{{AFX_MSG_MAP(CBillDlg)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	//}}AFX_MSG_MAP
	ON_NOTIFY(GVN_ENDLABELEDIT, IDC_GRID, OnGridEndInPlaceEdit)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBillDlg message handlers
BOOL CBillDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	//根据单据类型,设置对话框的标题
	if(m_nBillType == 0)
		SetWindowText("进货单据");
	else
		SetWindowText("销售单据");
	
	//设置按钮风格
	m_btSave.LoadBitmaps(IDB_SAVE,IDB_SAVE,IDB_SAVE);
	m_btSave.SetTextAlignment(CTrackLookButton::AlignRight);
	m_btExit.LoadBitmaps(IDB_EXIT,IDB_EXIT,IDB_EXIT);
	m_btExit.SetTextAlignment(CTrackLookButton::AlignRight);
	
	//初始化grid并设置其风格
	m_Grid.EnableDragAndDrop(TRUE);
	m_Grid.SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));
	
	//设置grid的行数、列数、固定行数、固定列数
	m_Grid.SetRowCount(m_nRows);
	m_Grid.SetColumnCount(m_nCols);
	m_Grid.SetFixedRowCount(m_nFixRows);
	m_Grid.SetFixedColumnCount(m_nFixCols);


	char gridHeader[6][10] = {"商品名称","单位","数量","单价","金额","备注"};
	//填充表格的表头,并且设置列宽
	for (int col = 0; col < m_Grid.GetColumnCount(); col++)
	{ 
		GV_ITEM Item;
		Item.mask = GVIF_TEXT|GVIF_FORMAT;
		Item.row = 0;
		Item.col = col;
		
		Item.nFormat = DT_LEFT|DT_WORDBREAK|DT_NOPREFIX;
		Item.strText = gridHeader[col];
		
		
		m_Grid.SetItem(&Item);
		m_Grid.SetColumnWidth(col,78);
	}
	
	//如果是查看单据,则显示该单据
	if(m_bShowBill)
		ShowBill();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//在grid中输入后更新其他
void CBillDlg::OnGridEndInPlaceEdit(NMHDR *pNotifyStruct, LRESULT* /*pResult*/)
{
	//如果是查看单据就没有必要处理
	if(m_bShowBill)
		return;

	//判断位置的有效性
	NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNotifyStruct;
	if(pItem->iRow < 0)
		return;

	//处理数量或单价列
	if(pItem->iColumn == m_nAmountCol || pItem->iColumn == m_nPriceCol)
	{
		CString strAmount = m_Grid.GetItemText(pItem->iRow,m_nAmountCol);
		CString strPrice = m_Grid.GetItemText(pItem->iRow,m_nPriceCol);
		int nAmount = 0;
		double fPrice = 0;
		if(strAmount != "")
		{
			nAmount = atoi(strAmount.GetBuffer(0));
		}
		if(strPrice != "")
		{
			fPrice = atof(strPrice.GetBuffer(0));
		}
		double fMoney = fPrice * (double)nAmount;//金额
		if(fMoney != 0)
		{
			CString strMoney;
			strMoney.Format("%f",fMoney);
			m_Grid.SetItemText(pItem->iRow,m_nMoneyCol,strMoney);
		}
		//检查数量值的合理性,并重新赋值
		if(nAmount > 0)
		{
			strAmount.Format("%d",nAmount);
			m_Grid.SetItemText(pItem->iRow,m_nAmountCol,strAmount);
		}
		else
			m_Grid.SetItemText(pItem->iRow,m_nAmountCol,"");
		//检查单价值的合理性,并重新赋值
		if(fPrice > 0)
		{
			strPrice.Format("%f",fPrice);
			m_Grid.SetItemText(pItem->iRow,m_nPriceCol,strPrice);
		}
		else
			m_Grid.SetItemText(pItem->iRow,m_nPriceCol,"");
		m_Grid.Refresh();
	}
}

void CBillDlg::OnSave() 
{
	UpdateData(TRUE);
	
	//初始化数组
	BOOL* RowReasonable = new BOOL[m_Grid.GetRowCount()];
	for(int row = 0; row < m_Grid.GetRowCount(); row++)
		RowReasonable[row] = FALSE;
	if(JudgeInputReasonable(RowReasonable))
	{
		int nResult = AfxMessageBox("保存单据?",MB_YESNOCANCEL,0);
		if( nResult == IDYES )
		{//保存单据
			if(SaveBill())
			{			
				AfxMessageBox("保存单据成功");
				SaveGoods(RowReasonable);
			}
		}
		else if( nResult == IDCANCEL)
		{	//暂时不保存,继续编辑单据
			delete RowReasonable;
			return;
		}
		delete RowReasonable;
		CDialog::OnOK();
		return;
	}
	delete RowReasonable;
}
BOOL CBillDlg::JudgeInputReasonable(BOOL *RowReasonable)
{
	//保证表格外的信息完整
	if(m_strBillID == "")
	{
		AfxMessageBox("请填写单据编号");
		return FALSE;
	}
	if(m_strClient == "")
	{
		AfxMessageBox("请填写客户");
		return FALSE;
	}
	if(m_strAccount == "")
	{
		AfxMessageBox("请填写帐户");
		return FALSE;
	}
	if(m_strStorage == "")
	{
		AfxMessageBox("请填写仓库");
		return FALSE;
	}
	if(m_strOperator == "")
	{
		AfxMessageBox("请填写经手人");
		return FALSE;
	}

	//让每一行的数据都合理
	BOOL bReasonable = FALSE;
	for (int row = m_Grid.GetFixedRowCount(); row < m_Grid.GetRowCount(); row++)
	{
		int nUnEmptyCount = 0;//该行输入信息的列数,其中comment不算
		for (int col = m_Grid.GetFixedColumnCount(); col < m_Grid.GetColumnCount(); col++)
		{
			if(col != m_nCommentCol)
			{
				if(m_Grid.GetItemText(row,col) != "")
					nUnEmptyCount ++;
			}
		}
		if(nUnEmptyCount > 0 && nUnEmptyCount < 
		   (m_Grid.GetColumnCount()-m_Grid.GetFixedColumnCount()-1))
		{
			CString str;
			str.Format("第%d行的输入不正确!",row);
			AfxMessageBox(str);
			return FALSE;
		}
		else if(nUnEmptyCount != 0)
		{
			bReasonable = TRUE;
			RowReasonable[row] = TRUE;
		}
	}
	
	if(!bReasonable)
	{
		AfxMessageBox("单据不能为空!");
		return FALSE;
	}

	return TRUE;
}
BOOL CBillDlg::SaveBill()
{
	CString strSql;
	CYaoApp* pApp = (CYaoApp*)AfxGetApp();
	//判断单据号是否重复
	strSql.Format("SELECT * FROM bill where id = '%s'",m_strBillID);
	HRESULT hr = pApp->m_pRecordset->Open(strSql.AllocSysString(),
		pApp->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	if(!SUCCEEDED(hr))
	{
		AfxMessageBox("打开bill表出错");
		return FALSE;
	}
	if(!pApp->m_pRecordset->adoEOF)
	{
		AfxMessageBox("已经存在该单据编号的记录");
		CEdit* pEdit = (CEdit*)GetDlgItem(IDC_BILL_ID);
		pEdit->SetFocus();
		pEdit->SetSel(0,m_strBillID.GetLength());
		pApp->m_pRecordset->Close();
		return FALSE;
	}
	pApp->m_pRecordset->Close();
	
	//新增一条记录
	strSql = "SELECT * FROM bill";
	hr = pApp->m_pRecordset->Open(strSql.AllocSysString(),
		pApp->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	if(!SUCCEEDED(hr))
	{
		AfxMessageBox("打开bill表出错");
		return FALSE;
	}
	
	CString str;
	pApp->m_pRecordset->AddNew();
	pApp->m_pRecordset->PutCollect("id",_variant_t(m_strBillID));
	str.Format("%d",m_nBillType);
	pApp->m_pRecordset->PutCollect("type",_variant_t(str));
	str = m_tTime.Format("%Y-%m-%d");
	pApp->m_pRecordset->PutCollect("time",_variant_t(str));
	pApp->m_pRecordset->PutCollect("storage",_variant_t(m_strStorage));
	pApp->m_pRecordset->PutCollect("client",_variant_t(m_strClient));
	pApp->m_pRecordset->PutCollect("operator",_variant_t(m_strOperator));
	pApp->m_pRecordset->PutCollect("account",_variant_t(m_strAccount));
	pApp->m_pRecordset->PutCollect("comment",_variant_t(m_strComment));

	pApp->m_pRecordset->Update();
	pApp->m_pRecordset->Close();
	return TRUE;
}
void CBillDlg::SaveGoods(BOOL *RowReasonable)
{
	CYaoApp* pApp = (CYaoApp*)AfxGetApp();
	for (int row = m_Grid.GetFixedRowCount(); row < m_Grid.GetRowCount(); row++)
	{
		if(RowReasonable[row] == TRUE)
		{//该行的数据是合理的
			CString strSql;
			
			//新增一条记录
			strSql = "SELECT * FROM goods";
			HRESULT hr = pApp->m_pRecordset->Open(strSql.AllocSysString(),
				pApp->m_pConnection.GetInterfacePtr(),adOpenDynamic,
				adLockOptimistic,adCmdText);
			if(!SUCCEEDED(hr))
			{
				AfxMessageBox("打开goods表出错");
				return;
			}
			
			CString str;
			pApp->m_pRecordset->AddNew();
			for (int col = m_Grid.GetFixedColumnCount(); 
				 col < m_Grid.GetColumnCount(); col++)
			{
				str = m_Grid.GetItemText(row,col);
				if(col == m_nNameCol)
					pApp->m_pRecordset->PutCollect("name",_variant_t(str));
				else if(col == m_nUnitCol)
					pApp->m_pRecordset->PutCollect("unit",_variant_t(str));
				else if(col == m_nAmountCol)
					pApp->m_pRecordset->PutCollect("amount",_variant_t(str));
				else if(col == m_nPriceCol)
					pApp->m_pRecordset->PutCollect("price",_variant_t(str));
				else if(col == m_nCommentCol)
					pApp->m_pRecordset->PutCollect("comment",_variant_t(str));
			}
			pApp->m_pRecordset->PutCollect("billid",_variant_t(m_strBillID));
			str.Format("%d",row);
			pApp->m_pRecordset->PutCollect("index",_variant_t(str));
			
			pApp->m_pRecordset->Update();
			pApp->m_pRecordset->Close();
		}
	}
}

//显示要查看的单据
void CBillDlg::ShowBill()
{
	//使编辑框都无效
	CWnd* pWnd = GetDlgItem(IDC_TIME);
	pWnd->EnableWindow(FALSE);
	pWnd = GetDlgItem(IDC_BILL_ID);
	pWnd->EnableWindow(FALSE);
	pWnd = GetDlgItem(IDC_COMMENT);
	pWnd->EnableWindow(FALSE);
	pWnd = GetDlgItem(IDC_CLIENT);
	pWnd->EnableWindow(FALSE);
	pWnd = GetDlgItem(IDC_ACCOUNT);
	pWnd->EnableWindow(FALSE);
	pWnd = GetDlgItem(IDC_STORAGE);
	pWnd->EnableWindow(FALSE);
	pWnd = GetDlgItem(IDC_OPERATOR);
	pWnd->EnableWindow(FALSE);

	//将“Save”按钮隐藏,并且将“退出”按钮的Caption变为“关闭”
	pWnd = GetDlgItem(IDC_SAVE);
	pWnd->ShowWindow(FALSE);
	pWnd = GetDlgItem(IDC_EXIT);
	pWnd->SetWindowText("关闭");

	//让商品名称,编号等列不能编辑
	for (int row = m_Grid.GetFixedRowCount(); row < m_Grid.GetRowCount(); row++)
	{
		for (int col = m_Grid.GetFixedColumnCount(); col < m_Grid.GetColumnCount(); col++)
		{
			m_Grid.SetItemState(row,col,m_Grid.GetItemState(row,col) | GVIS_READONLY);
		}
	}


	CString strSql,str;
	CYaoApp* pApp = (CYaoApp*)AfxGetApp();

	//填充表格外的数据
	strSql.Format("SELECT * FROM bill where id = '%s'",m_strBillID);
	HRESULT hr = pApp->m_pRecordset->Open(strSql.AllocSysString(),
		pApp->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	if(!SUCCEEDED(hr))
	{
		AfxMessageBox("打开bill表出错");
		return;
	}
	if(pApp->m_pRecordset->adoEOF)
	{
		AfxMessageBox("错误的单据编号");
		pApp->m_pRecordset->Close();
		return;
	}
	m_strBillID = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("id"));
	str = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("time"));
	GetTimeFromStr(str,m_tTime);
	m_strClient = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("client"));
	m_strAccount = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("account"));
	m_strStorage = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("storage"));
	m_strOperator = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("operator"));
	m_strComment = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("comment"));

	pApp->m_pRecordset->Close();

	//填充表格内的数据
	strSql.Format("select * from goods where billid = '%s'",m_strBillID);
	hr = pApp->m_pRecordset->Open(strSql.AllocSysString(),
		pApp->m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
	if(!SUCCEEDED(hr))
	{
		AfxMessageBox("打开goods表出错");
		return;
	}
	while(!pApp->m_pRecordset->adoEOF)
	{
		//该商品条目在表格中的位置
		str = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("index"));
		int nRow = atoi(str.GetBuffer(0));
		
		//商品名称
		str = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("name"));
		m_Grid.SetItemText(nRow,m_nNameCol,str);

		//计量单位
		str = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("unit"));
		m_Grid.SetItemText(nRow,m_nUnitCol,str);

		//数量
		str = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("amount"));
		m_Grid.SetItemText(nRow,m_nAmountCol,str);

		//单价
		str = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("price"));
		m_Grid.SetItemText(nRow,m_nPriceCol,str);

		//金额
		double fMoney = atoi(m_Grid.GetItemText(nRow,m_nAmountCol).GetBuffer(0)) * 
						atof(m_Grid.GetItemText(nRow,m_nPriceCol).GetBuffer(0));
		str.Format("%f",fMoney);
		m_Grid.SetItemText(nRow,m_nMoneyCol,str);

		//备注
		str = (char*)(_bstr_t)pApp->m_pRecordset->GetCollect(_variant_t("comment"));
		m_Grid.SetItemText(nRow,m_nCommentCol,str);
		
		pApp->m_pRecordset->MoveNext();
	}
	pApp->m_pRecordset->Close();

	//更新对话框的显示
	UpdateData(FALSE);
	
}

void CBillDlg::GetTimeFromStr(CString strTm, CTime &tm)
{
	int nPos = strTm.Find("-");
	CString strYear = strTm.Left(nPos);
	CString strRight = strTm.Right(strTm.GetLength()-nPos-1);
	nPos = strRight.Find("-");
	CString strMonth = strRight.Left(nPos);
	CString strDay = strRight.Right(strRight.GetLength()-nPos-1);

	int nYear,nMonth,nDay;
	nYear = atoi(strYear.GetBuffer(0));
	if(nYear < 100)
		nYear += 2000;
	nMonth = atoi(strMonth.GetBuffer(0));
	nDay = atoi(strDay.GetBuffer(0));
	tm = CTime(nYear,nMonth,nDay,0,0,0);
}

void CBillDlg::OnExit() 
{
	CDialog::OnCancel();	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -