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

📄 merchandisesetdialog.cpp

📁 VC+SQL SERVER 2000环境下开发的商品销售管理系统
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			float GoodOfMerPrices = 0;  //一行货品的总价钱
            int num = 0;

			if( (int)_variant_t(m_MerGoodsNum) >0)
			{
			   if(!m_pRecordsetMerG->adoEOF)
			   {
					while(!m_pRecordsetMerG->adoEOF)
					{
						var = m_pRecordsetMerG->GetCollect ("GoodsName");
						strValue = (LPCSTR)_bstr_t(var);

						if(strValue == GoodsName)
						{
							var = m_pRecordsetMerG->GetCollect ("SalePrice");
							GoodOfMerPrices = (float)_variant_t(var);

							var = m_pRecordsetMerG->GetCollect ("GoodsNum");
							num = (int)_variant_t(var);
							float Avg =  GoodOfMerPrices / num;
							num += (int)_variant_t(m_MerGoodsNum);
                            
							strSQL.Format ("select * from GoodsTable where GoodsName = '%s'", strValue);
							m_pRecordsetG->Open (_bstr_t(strSQL),
												m_pConnection.GetInterfacePtr(),
												adOpenDynamic,
												adLockOptimistic,
												adCmdText);
							
							var = m_pRecordsetG->GetCollect ("NowStore");
							int GoodsRemainNum = (int)_variant_t(var);
							m_pRecordsetG->Close ();
							m_pRecordsetG = NULL;

							if(num > GoodsRemainNum)
							{
								MessageBox (_T("货品现存量不足!"), _T("商品设置"), MB_ICONERROR | MB_OK);
								m_pRecordsetMerG->Close();
								m_pRecordsetMerG = NULL;
								return;
							}


							GoodOfMerPrices += Avg * (float)_variant_t(m_MerGoodsNum);
							TotalPrice += Avg * (float)_variant_t(m_MerGoodsNum);

							m_pRecordsetMerG->PutCollect ("GoodsNum", _variant_t(num));
							m_pRecordsetMerG->PutCollect ("SalePrice", _variant_t(GoodOfMerPrices));

							m_pRecordsetMerG->Update ();
	                        
							int Count = m_ListOfGoods.GetItemCount ();
							for(int index = 0; index < Count; index++)
							{
								strValue = m_ListOfGoods.GetItemText (index, 0);
								if(strValue == GoodsName)
								{
									m_ListOfGoods.SetItemText (index, 2 , _bstr_t(num));
									m_ListOfGoods.SetItemText (index, 3, _bstr_t(GoodOfMerPrices));
									break;
								}
							}
							break;
						}//if
						m_pRecordsetMerG->MoveNext ();
					}//while
			   }//if
			   else 
			   {
					
					m_ComboGoodName.GetWindowText (strValue);
					strSQL.Format ("select * from GoodsTable where GoodsName = '%s'", strValue);
					m_pRecordsetG->Open (_bstr_t(strSQL),
						                 m_pConnection.GetInterfacePtr(),
                                         adOpenDynamic,
										 adLockOptimistic,
										 adCmdText);
					
					var = m_pRecordsetG->GetCollect ("NowStore");
					int GoodsRemainNum = (int)_variant_t(var);
					m_pRecordsetG->Close ();
					m_pRecordsetG = NULL;

					if((int)_variant_t(m_MerGoodsNum) > GoodsRemainNum)
					{
						MessageBox (_T("货品现存量不足!"), _T("商品设置"), MB_ICONERROR | MB_OK);
						m_pRecordsetMerG->Close();
						m_pRecordsetMerG = NULL;
						return;
					}
                    m_pRecordsetMerG->AddNew ();
					m_ComboGoodName.GetWindowText (strValue);
					m_pRecordsetMerG->PutCollect ("MerchandiseName", _variant_t(MerName));
					m_pRecordsetMerG->PutCollect ("GoodsName", _variant_t(strValue));
					m_ListOfGoods.InsertItem (m_InOfGoodsList, strValue);

					m_pRecordsetMerG->PutCollect ("Units", _variant_t(m_GoodsUnit));
					m_ListOfGoods.SetItemText (m_InOfGoodsList, 1, m_GoodsUnit);
		            
					
					m_pRecordsetMerG->PutCollect ("GoodsNum", _variant_t(m_MerGoodsNum));
					m_ListOfGoods.SetItemText (m_InOfGoodsList, 2, m_MerGoodsNum);

					float Price = (float)_variant_t(m_SalePrice) * (int)_variant_t(m_MerGoodsNum);
					TotalPrice += Price;
					m_pRecordsetMerG->PutCollect ("SalePrice", _variant_t(Price));
					m_ListOfGoods.SetItemText (m_InOfGoodsList, 3, _bstr_t(Price));
					m_InOfGoodsList ++;
					m_pRecordsetMerG->Update ();
				}//if

			}//if
			else if( (int)_variant_t(m_MerGoodsNum) < 0 && (!m_pRecordsetMerG->adoEOF))
			{
				m_pRecordsetMerG->Close ();
                strSQL.Format ("select * from MerGroupbyGoodsTable where MerchandiseName = '%s'", MerName);
				m_pRecordsetMerG->Open (_bstr_t(strSQL),
					                    m_pConnection.GetInterfacePtr(),
							           adOpenDynamic,
							           adLockOptimistic,
							           adCmdText);

				while(!m_pRecordsetMerG->adoEOF)
				{
					var = m_pRecordsetMerG->GetCollect ("GoodsName");
					strValue = (LPCSTR)_bstr_t(var);

					if(strValue == GoodsName)
					{
						var = m_pRecordsetMerG->GetCollect ("SalePrice");
						GoodOfMerPrices = (float)_variant_t(var);

						var = m_pRecordsetMerG->GetCollect ("GoodsNum");
						num = (int)_variant_t(var);
                        float Avg =  GoodOfMerPrices / num;

						num += (int)_variant_t(m_MerGoodsNum);

                        if(num < 0)
						{
							MessageBox (_T("输入的减少货品数量比实际货品数量要多!"), _T("商品设置"),
								        MB_ICONERROR | MB_OK);
                            CEdit *pEdit = (CEdit*)GetDlgItem (IDC_EDIT_MERGOODSNUM);
							pEdit->SetWindowText (_T(""));
							pEdit->SetFocus ();
							return;
						}

						if(num == 0)
						{
							strSQL.Format ("delete from MerGroupbyGoodsTable where \
										    GoodsName = '%s'",GoodsName);
							_variant_t RecordsAffected;
							m_pConnection->Execute (_bstr_t(strSQL), &RecordsAffected, adCmdText);
                            
							int Count = m_ListOfGoods.GetItemCount ();
							for(int index = 0; index < Count; index++)
							{
								strValue = m_ListOfGoods.GetItemText (index, 0);
								if(strValue == GoodsName)
								{
									m_ListOfGoods.DeleteItem (index);
									break;
								}
							}
					        break;
						}

						GoodOfMerPrices -= Avg * ( -1 * (float)_variant_t(m_MerGoodsNum));
						TotalPrice -= Avg * ( -1 * (float)_variant_t(m_MerGoodsNum));
						m_pRecordsetMerG->PutCollect ("GoodsNum", _variant_t(num));
						m_pRecordsetMerG->PutCollect ("SalePrice", _variant_t(GoodOfMerPrices));

						m_pRecordsetMerG->Update ();
						break;
					}
					m_pRecordsetMerG->MoveNext ();
				}//while

				int Count = m_ListOfGoods.GetItemCount ();
				for(int index = 0; index < Count; index++)
				{
					strValue = m_ListOfGoods.GetItemText (index, 0);
					if(strValue == GoodsName)
					{
						m_ListOfGoods.SetItemText (index, 2 , _bstr_t(num));
						m_ListOfGoods.SetItemText (index, 3, _bstr_t(GoodOfMerPrices));
						break;
					}
				}
			}//else if
			else 
			{
				AfxMessageBox (_T("新增货品价钱不能为负"));
				CEdit *pEdit = (CEdit*)GetDlgItem (IDC_EDIT_MERGOODSNUM);
				pEdit->SetWindowText (_T(""));
				pEdit->SetFocus ();
				return;
			}
			
			CEdit *pEdit = (CEdit*)GetDlgItem (IDC_EDIT_MERTOTAL);
			pEdit->SetWindowText (_bstr_t(TotalPrice));

			m_pRecordsetMerG->Close ();
			m_pRecordsetMerG = NULL;
			m_Succeed = TRUE;
		}
	}//try
	catch(_com_error &e)
	{
		AfxMessageBox(e.ErrorMessage ());
		return;
	}
}

void CMerchandiseSetDialog::OnBnClickedButtonMergoodsetcancel()
{
	if(m_AddGoods)
	{
       MerSetGoodModuleClear();
	   CButton *pButton = (CButton*)GetDlgItem (IDC_BUTTON_DELGOODS);
	   pButton->EnableWindow (TRUE);
	   m_BuyPrice = "";
	   m_SalePrice = "";
	   m_GoodsUnit = "";
	   m_MerGoodsNum = "";
	   m_GoodsReMainNum = "";
	   UpdateData(FALSE);
	}
}

void CMerchandiseSetDialog::InitComboBox()
{
	_RecordsetPtr     m_pRecordsetG;
	HRESULT           hrG;

	try
	{
		hrG = m_pRecordsetG.CreateInstance(__uuidof(Recordset));
		if(FAILED(hrG))
		{
			AfxMessageBox (_T("创建实例失败!"));
			return;
		}

		hrG = m_pRecordsetG->Open ("select * from GoodsTable",
			                        m_pConnection.GetInterfacePtr(),
							        adOpenDynamic,
							        adLockOptimistic,
							        adCmdText);

		if(SUCCEEDED(hrG))
		{
			_variant_t    var;
			CString       strValue;

			while(!m_pRecordsetG->adoEOF)
			{
				var = m_pRecordsetG->GetCollect ("GoodsName");
				strValue = (LPCSTR)_bstr_t(var);

				m_ComboGoodName.InsertString(-1, strValue);
				m_pRecordsetG->MoveNext ();
			}//while

			m_pRecordsetG->Close ();
			m_pRecordsetG = NULL;
		}
	}
	catch(_com_error &e)
	{
		AfxMessageBox (e.ErrorMessage());
		return;
	}
}

void CMerchandiseSetDialog::InitGoodsList(CString MerName ,int sel)
{
	_RecordsetPtr        m_pRecordsetMer;
	_RecordsetPtr        m_pRecordsetMerG;
	HRESULT              hrMer;
	HRESULT              hrMerG;

	try
	{
		hrMer = m_pRecordsetMer.CreateInstance (__uuidof(Recordset));
		hrMerG = m_pRecordsetMerG.CreateInstance (__uuidof(Recordset));
		if( FAILED(hrMer) || FAILED(hrMerG) )
		{
			AfxMessageBox (_T("创建实例失败!"));
			return;
		}

		CString strSQL;
        strSQL.Format ("select * from MerchandiseSetTable where MerchandiseName = '%s'", MerName);
		hrMer = m_pRecordsetMer->Open (_bstr_t(strSQL),
			                           m_pConnection.GetInterfacePtr(),
							           adOpenDynamic,
							           adLockOptimistic,
							           adCmdText);
		
		hrMerG = m_pRecordsetMerG->Open ("select * from MerGroupbyGoodsTable",
			                  	         m_pConnection.GetInterfacePtr(),
							             adOpenDynamic,
							             adLockOptimistic,
							             adCmdText);      
		
		if(SUCCEEDED(hrMer) && SUCCEEDED(hrMerG))
		{
			_variant_t      var;
			CString        strValue;
			TotalPrice = 0;
			 m_InOfGoodsList = 0;

			if(!m_pRecordsetMer->adoEOF)
			{

				var = m_pRecordsetMer->GetCollect ("MerchandiseName");
				CString MerName = (LPCSTR)_bstr_t(var);
		        
				int Count = m_ListOfGoods.GetItemCount ();
                if(Count > 0)
					m_ListOfGoods.DeleteAllItems ();
				while(!m_pRecordsetMerG->adoEOF)
				{
					var = m_pRecordsetMerG->GetCollect ("MerchandiseName");
					strValue = (LPCSTR)_bstr_t(var);

					if( strValue == MerName)
					{
                        var = m_pRecordsetMerG->GetCollect ("GoodsName");
                        strValue = (LPCSTR)_bstr_t(var);
						m_ListOfGoods.InsertItem (m_InOfGoodsList, strValue);

						var = m_pRecordsetMerG->GetCollect ("Units");
						strValue = (LPCSTR)_bstr_t(var);
						m_ListOfGoods.SetItemText (m_InOfGoodsList, 1, strValue);

						var = m_pRecordsetMerG->GetCollect ("GoodsNum");
						strValue = (LPCSTR)_bstr_t(var);
						int num = (int)_variant_t(strValue);
                        strValue = (LPCSTR)_bstr_t(num);
						m_ListOfGoods.SetItemText (m_InOfGoodsList, 2, strValue);

						var = m_pRecordsetMerG->GetCollect ("SalePrice");
						strValue = (LPCSTR)_bstr_t(var);
						float GoodsPrice = (float)_variant_t(strValue);
						strValue = (LPCSTR)_bstr_t(GoodsPrice);
                        m_ListOfGoods.SetItemText (m_InOfGoodsList, 3, strValue);
						TotalPrice += GoodsPrice;
                       
						m_InOfGoodsList++;
					}
					m_pRecordsetMerG->MoveNext ();
				}

                CEdit *pEdit = (CEdit*) GetDlgItem (IDC_EDIT_MERTOTAL);
				strValue = (LPCSTR)_bstr_t(TotalPrice);
				pEdit->SetWindowText (strValue);
				
				strValue = m_ListOfMer.GetItemText (sel, 0);
				pEdit = (CEdit*)GetDlgItem (IDC_EDITMERNAME);
	            pEdit->SetWindowText (strValue);

				m_pRecordsetMerG->Close ();
				m_pRecordsetMer->Close ();
				m_pRecordsetMer = NULL;
				m_pRecordsetMerG = NULL;
			}//if
		}//if
	}//try
	catch(_com_error &e)
	{
		AfxMessageBox (e.ErrorMessage());
		return;
	}
}

void CMerchandiseSetDialog::OnLvnItemActivateListMer(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMIA = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	
    int sel = m_ListOfMer.GetSelectionMark ();
	CString MerName;
	MerName = m_ListOfMer.GetItemText (sel, 0);

    InitGoodsList (MerName, sel);

	*pResult = 0;
}

void CMerchandiseSetDialog::OnBnClickedButtonMergoodsetok()
{
	if(m_AddGoods)
	{
		AddGoodsOfMer(m_MerName);
		if(m_Succeed)
		{
			MessageBox (_T("添加货品成功!"), _T("商品设置"), MB_ICONASTERISK | MB_OK);

			m_BuyPrice = "";
			m_SalePrice = "";
			m_GoodsUnit = "";
			m_MerGoodsNum = "";
			m_GoodsReMainNum = "";
			UpdateData (FALSE);
			m_Succeed = FALSE;
			
			MerSetGoodModuleClear();
			CButton *pButton = (CButton*)GetDlgItem (IDC_BUTTON_DELGOODS);
			pButton->EnableWindow (TRUE);
		}
	}
}

void CMerchandiseSetDialog::OnCbnSelchangeComboGoodsname()
{
	
	CString GoodsName;
	m_ComboGoodName.GetWindowText (GoodsName);

	_RecordsetPtr        m_pRecordsetG;
	HRESULT              hrG;

	try
	{
		hrG = m_pRecordsetG.CreateInstance (__uuidof(Recordset));
		if( FAILED(hrG))
		{
			AfxMessageBox (_T("创建实例失败!"));
			return;
		}

		CString strSQL;
        strSQL.Format ("select * from GoodsTable where GoodsName = '%s'", GoodsName);
		hrG = m_pRecordsetG->Open (_bstr_t(strSQL),
			                           m_pConnection.GetInterfacePtr(),
							           adOpenDynamic,
							           adLockOptimistic,
							           adCmdText);
		if(SUCCEEDED(hrG))
		{
	        if(!m_pRecordsetG->adoEOF)
			{
				_variant_t var = m_pRecordsetG->GetCollect ("Units");
				m_GoodsUnit = (LPCSTR)_bstr_t(var);

				

⌨️ 快捷键说明

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