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

📄 orderdlg.cpp

📁 < Visual C+++SQL Server数据库开发与实例>>一书中的实例核心源程序提供了几个案例的编程源代码。
💻 CPP
字号:
// OrderDLG.cpp : implementation file
//

#include "stdafx.h"
#include "trade_mis.h"
#include "OrderDLG.h"
#include "Trade_MISView.h"

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

/////////////////////////////////////////////////////////////////////////////
// COrderDLG dialog


COrderDLG::COrderDLG(CWnd* pParent /*=NULL*/)
	: CDialog(COrderDLG::IDD, pParent)
{
	//{{AFX_DATA_INIT(COrderDLG)
	m_sCustomerName = _T("");
	m_sCustomerPerson = _T("");
	m_sCustomerPhone = _T("");
	m_sDiscount = _T("");
	m_sMemo = _T("");
	m_tOrderDate = COleDateTime::GetCurrentTime();
	m_sPrice = _T("");
	m_sProductName = _T("");
	m_sProductSpec = _T("");
	m_sProductUnit = _T("");
	m_sQuantity = _T("");
	//}}AFX_DATA_INIT
}


void COrderDLG::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COrderDLG)
	DDX_CBString(pDX, IDC_ORDER_CUSTOMERNAME, m_sCustomerName);
	DDX_Text(pDX, IDC_ORDER_CUSTOMERPERSON, m_sCustomerPerson);
	DDX_Text(pDX, IDC_ORDER_CUSTOMERPHONE, m_sCustomerPhone);
	DDX_Text(pDX, IDC_ORDER_DISCOUNT, m_sDiscount);
	DDX_Text(pDX, IDC_ORDER_MEMO, m_sMemo);
	DDX_DateTimeCtrl(pDX, IDC_ORDER_ORDERDATE, m_tOrderDate);
	DDX_Text(pDX, IDC_ORDER_PRICE, m_sPrice);
	DDX_CBString(pDX, IDC_ORDER_PRODUCTNAME, m_sProductName);
	DDX_Text(pDX, IDC_ORDER_PRODUCTSPEC, m_sProductSpec);
	DDX_Text(pDX, IDC_ORDER_PRODUCTUNIT, m_sProductUnit);
	DDX_Text(pDX, IDC_ORDER_QUANTITY, m_sQuantity);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COrderDLG, CDialog)
	//{{AFX_MSG_MAP(COrderDLG)
	ON_CBN_SELCHANGE(IDC_ORDER_CUSTOMERNAME, OnOrderCustomerNameSelChange)
	ON_CBN_SELCHANGE(IDC_ORDER_PRODUCTNAME, OnOrderProductNameSelChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COrderDLG message handlers

BOOL COrderDLG::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_sOldCustomerName = m_sCustomerName;
	m_sOldProductName = m_sProductName;
	
	if (m_bAppend)
	{		
	    // Update Dialog Caption
		SetWindowText(_T("添加销售信息"));
		
		// Init Customer Combobox
		_variant_t strQuery, Holder;	
		strQuery = "select * from customers";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	    int iCount = theApp.m_pADOSet->GetRecordCount();
		theApp.m_pADOSet->MoveFirst();
		for (int i=0; i<iCount; i++)
		{
		    Holder = theApp.m_pADOSet->GetCollect("CompanyName");
	        ((CComboBox*)GetDlgItem(IDC_ORDER_CUSTOMERNAME))->InsertString(i, Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder);
		    // Get other onfo to display
			if ( 0==i )
			{
				// Get Person
				Holder = theApp.m_pADOSet->GetCollect("ContactName");
				m_sCustomerPerson = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
                // Get Phone
				Holder = theApp.m_pADOSet->GetCollect("Phone");
				m_sCustomerPhone = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			}			
			theApp.m_pADOSet->MoveNext();
		}
	    ((CComboBox*)GetDlgItem(IDC_ORDER_CUSTOMERNAME))->SetCurSel(0);
	
	    // Init Product Combobox
		strQuery = "select * from products";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	    iCount = theApp.m_pADOSet->GetRecordCount();
		theApp.m_pADOSet->MoveFirst();
		for (i=0; i<iCount; i++)
		{
		    Holder = theApp.m_pADOSet->GetCollect("ProductName");
	        ((CComboBox*)GetDlgItem(IDC_ORDER_PRODUCTNAME))->InsertString(i, Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder);
		    // Get other onfo to display
			if ( 0==i )
			{
				// Get Spec
				Holder = theApp.m_pADOSet->GetCollect("Spec");
				m_sProductSpec = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
                // Get Phone
				Holder = theApp.m_pADOSet->GetCollect("Unit");
				m_sProductUnit = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			}			
			theApp.m_pADOSet->MoveNext();
		}
	    ((CComboBox*)GetDlgItem(IDC_ORDER_PRODUCTNAME))->SetCurSel(0);
	}
	else
	{
		// Update Dialog Caption
		SetWindowText(_T("修改销售信息"));
        
    	// Init Provider Combobox
		_variant_t strQuery, Holder;	
		CString str;
		int iSel = 0;
		strQuery = "select * from customers";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	    int iCount = theApp.m_pADOSet->GetRecordCount();
		theApp.m_pADOSet->MoveFirst();
		for (int i=0; i<iCount; i++)
		{
		    Holder = theApp.m_pADOSet->GetCollect("CompanyName");
	        str = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			((CComboBox*)GetDlgItem(IDC_ORDER_CUSTOMERNAME))->InsertString(i, str);
		    
			// Get other onfo to display
			if ( str==m_sCustomerName )
			{
				// Get Person
				Holder = theApp.m_pADOSet->GetCollect("ContactName");
				m_sCustomerPerson = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
                // Get Phone
				Holder = theApp.m_pADOSet->GetCollect("Phone");
				m_sCustomerPhone = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;

				iSel = i;
			}			
			theApp.m_pADOSet->MoveNext();
		}
	    ((CComboBox*)GetDlgItem(IDC_ORDER_CUSTOMERNAME))->SetCurSel(iSel);
	    
		// Init Product Combobox
		strQuery = "select * from products";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	    iCount = theApp.m_pADOSet->GetRecordCount();
		theApp.m_pADOSet->MoveFirst();
		for (i=0; i<iCount; i++)
		{
		    Holder = theApp.m_pADOSet->GetCollect("ProductName");
	        str = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
			((CComboBox*)GetDlgItem(IDC_ORDER_PRODUCTNAME))->InsertString(i, str);
		    
			if ( str==m_sProductName ) iSel = i;
					
			theApp.m_pADOSet->MoveNext();
		}
	    ((CComboBox*)GetDlgItem(IDC_ORDER_PRODUCTNAME))->SetCurSel(iSel);
	}
	
	UpdateData(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void COrderDLG::OnOrderCustomerNameSelChange() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	
	_variant_t strQuery, Holder;	
	strQuery = "select * from customers where CompanyName='"+m_sCustomerName+"'";
	theApp.ADOExecute(theApp.m_pADOSet, strQuery);
    // Get person
	Holder = theApp.m_pADOSet->GetCollect("ContactName");
	m_sCustomerPerson = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
    // Get Phone
	Holder = theApp.m_pADOSet->GetCollect("Phone");
	m_sCustomerPhone = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
        
    UpdateData(false);
}

void COrderDLG::OnOrderProductNameSelChange() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	
	_variant_t strQuery, Holder;	
	strQuery = "select * from products where ProductName='"+m_sProductName+"'";
	theApp.ADOExecute(theApp.m_pADOSet, strQuery);
    // Get Spec
	Holder = theApp.m_pADOSet->GetCollect("Spec");
	m_sProductSpec = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
    // Get Unit
	Holder = theApp.m_pADOSet->GetCollect("Unit");
	m_sProductUnit = Holder.vt==VT_NULL?"":(char*)(_bstr_t)Holder;
        
    UpdateData(false);
}

void COrderDLG::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(true);
	
	m_sQuantity.TrimRight(" ");
    m_sPrice.TrimRight(" ");
    m_sDiscount.TrimRight(" ");

	// Make sure all needed info is available
	CString sWarning="";
	if ( ""==m_sQuantity ) sWarning=_T("数量");
    else if ( ""==m_sPrice ) sWarning=_T("单价");
	else if ( ""==m_sDiscount ) sWarning=_T("折扣");

	if ( ""!=sWarning ) 
	{
	   sWarning += _T("不能为空");
	   AfxMessageBox(sWarning, MB_ICONEXCLAMATION);
	   return;
	}
	
	// Check m_sPrice validity
	float fPrice=atof(m_sPrice);
	if ( 0==fPrice ) 
	{
		AfxMessageBox(_T("单价:请输入非零数字"), MB_ICONEXCLAMATION);
	    return;
	}
	m_sPrice.Format("%.2f", fPrice);

	// Check m_sDiscount validity
	float fDiscount=atof(m_sDiscount);
	if ( 0==fDiscount ) 
	{
		AfxMessageBox(_T("折扣:请输入非零数字"), MB_ICONEXCLAMATION);
	    return;
	}
	m_sDiscount.Format("%.2f", fDiscount);

    if ( fDiscount>100 ) 
	{
		AfxMessageBox(_T("折扣:不能大于100"), MB_ICONEXCLAMATION);
	    return;
	}

	_variant_t strQuery;	
	if ( m_bAppend || m_sOldCustomerName!=m_sCustomerName || m_sOldProductName!=m_sProductName )
	{
		// Judge Order is unique
	    strQuery = "select * from orders where CustomerName='"+m_sCustomerName+"' and ProductName='"+m_sProductName+"'";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	    int iCount = theApp.m_pADOSet->GetRecordCount();
	    if ( 0!=iCount )
		{
	       AfxMessageBox(_T("已经存在此销售记录!"), MB_ICONEXCLAMATION);
	       return;
		}
	}
		
	if ( !m_bAppend ) // Not Append, delete old record first
	{
		strQuery = "delete from orders where CustomerName='"+m_sOldCustomerName+"' and ProductName='"+m_sOldProductName+"'";
	    theApp.ADOExecute(theApp.m_pADOSet, strQuery);
	}
	
	// Insert Record
	// Get OrderDate
	CString sOrderDate = m_tOrderDate.Format("%Y-%m-%d");
	strQuery = "insert orders (CustomerName, ProductName, Spec, Unit, Quantity, UnitPrice, OrderDate, Discount, Ordermemo) \
		        values ('"+m_sCustomerName+"', '"+m_sProductName+"', '"+m_sProductSpec+"', '"+m_sProductUnit+"', "+m_sQuantity+", "+m_sPrice+", '"+sOrderDate+"', "+m_sDiscount+", '"+m_sMemo+"')";
	if ( theApp.ADOExecute(theApp.m_pADOSet, strQuery) ) 
	{
	    if (m_bAppend) 
		{
			AfxMessageBox(_T("添加销售信息成功!"), MB_ICONINFORMATION);
	        // Clear all input
            m_sQuantity=m_sPrice=m_sMemo=m_sDiscount="";
            UpdateData(false);
		}
		else AfxMessageBox(_T("修改销售信息成功!"), MB_ICONINFORMATION);
	}
	else 
	{
		if (m_bAppend) AfxMessageBox(_T("添加销售信息失败!"), MB_ICONEXCLAMATION);
		else AfxMessageBox(_T("修改销售信息失败!"), MB_ICONEXCLAMATION);
	}
	
	strQuery = "select * from orders";
	CTrade_MISView* p = (CTrade_MISView*)(((CMainFrame*)AfxGetMainWnd())->GetActiveView());
	p->RefreshOrder(strQuery);
	
	if (!m_bAppend) CDialog::OnOK();
}

⌨️ 快捷键说明

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