using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace InvoicingTool { public partial class PurchaseDetail : Form { QualityHelper QualityHelper = new QualityHelper(); string purchase_code; public PurchaseDetail() { InitializeComponent(); } #region 加载时获取采购单内码 public PurchaseDetail(string code) { InitializeComponent(); purchase_code = code; } #endregion #region 关闭事件 private void ButtonColse_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; Close(); } #endregion #region 页面加载事件 private void PurchaseDetail_Load(object sender, EventArgs e) { Getdata(); string sql = "select purchase_state from t_purchase where purchase_code='" + purchase_code + "'"; DataTable dt = DBHelper.QueryOdbc(sql).Tables[0]; if (dt.Rows[0][0].ToString() != "0") { btnCommodityAdd.Enabled = false; btnSave.Enabled = false; txtBarcodeAdd.Enabled = false; PurchasedetailGridView.ReadOnly = true; PurchasedetailGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } } #endregion #region 数据绑定事件 private void Getdata() { //获取数据源绑定到gridview string sql = @"select 1 as id,a.commodity_barcode,a.commodity_name,a.commodity_rule, Replace(Replace(a.unit,2,'箱'),1,b.commodity_unit) as unit,isnull(a.purchase_count,0) as purchase_count, isnull(a.unitcount,1) as unitcount,isnull(a.purchase_price,0) as purchase_price, isnull(a.total_price,0) as total_price,a.purchasedetail_id,isnull(a.total_count,0) as total_count, a.purchase_count as count,b.commodity_unit as commodity_unit,'箱' as box_unit from t_purchasedetail a left join t_sellercommodity b on b.commodity_barcode = a.commodity_barcode where a.purchasedetail_desc='" + purchase_code + "'"; DataTable _DataTable = DBHelper.QueryOdbc(sql).Tables[0]; if (_DataTable.Rows.Count > 0) { for (int i = 0; i < _DataTable.Rows.Count; i++) { _DataTable.Rows[i]["id"] = i + 1; } } PurchasedetailGridView.AutoGenerateColumns = false; PurchasedetailGridView.DataSource = _DataTable; } #endregion #region datagridview行按钮事件 private void PurchasedetailGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { //删除按钮事件 if (PurchasedetailGridView.Columns[e.ColumnIndex].Name == "Delete" && btnCommodityAdd.Enabled && btnSave.Enabled) { string id = this.PurchasedetailGridView.Rows[e.RowIndex].Cells["purchasedetail_id"].Value.ToString(); string sql = "delete from t_purchasedetail where purchasedetail_id=" + id; DBHelper.ExecuteSqlTran(sql); Getdata(); } } #endregion #region 选择商品事件 private void btnCommodityAdd_Click(object sender, EventArgs e) { Save(true); Product p = new Product(purchase_code); p.TopMost = true; p.ShowInTaskbar = false; p.ShowDialog(); if (p.DialogResult == DialogResult.OK) { this.Getdata();//重新绑定 } } #endregion #region 保存事件 private void btnSave_Click(object sender, EventArgs e) { switch (Save(false)) { case 0: MessageBox.Show("请添加采购商品!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 1: MessageBox.Show("请填写采购数量!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); PurchasedetailGridView.Focus(); break; case 2: MessageBox.Show("保存失败,请重试!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 9: MessageBox.Show("保存成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Getdata(); break; } } #endregion #region gridview采购数量改变计算总额 private void PurchasedetailGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && PurchasedetailGridView.Columns[e.ColumnIndex].Name == "purchase_count") { if (PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_count"].Value != null && PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_count"].Value.ToString() != "") { decimal purchase_count = Convert.ToDecimal(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_count"].Value); decimal purchase_price = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_price"].Value.ToString()); PurchasedetailGridView.Rows[e.RowIndex].Cells["total_price"].Value = (purchase_count * purchase_price).ToString("F2"); decimal unitcount = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["unitcount"].Value.ToString()); PurchasedetailGridView.Rows[e.RowIndex].Cells["total_count"].Value = (purchase_count * unitcount).ToString("F2"); } else { decimal purchase_count = 0; PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_count"].Value = purchase_count; decimal purchase_price = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_price"].Value.ToString()); PurchasedetailGridView.Rows[e.RowIndex].Cells["total_price"].Value = (purchase_count * purchase_price).ToString("F2"); decimal unitcount = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["unitcount"].Value.ToString()); PurchasedetailGridView.Rows[e.RowIndex].Cells["total_count"].Value = (purchase_count * unitcount).ToString("F2"); } txtShop.Text = PurchasedetailGridView.Rows[e.RowIndex].Cells["commodity_barcode"].Value.ToString(); txtname.Text = PurchasedetailGridView.Rows[e.RowIndex].Cells["commodity_name"].Value.ToString(); txtcount.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_count"].Value.ToString()).ToString("F2"); txtprice.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_price"].Value.ToString()).ToString("F2"); txtcountprice.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["total_price"].Value.ToString()).ToString("F2"); } } #endregion #region gridview行单机是获取值到文本框 private void PurchasedetailGridView_CellClick(object sender, DataGridViewCellEventArgs e) { string strColumn = PurchasedetailGridView.Columns[e.ColumnIndex].Name; if (strColumn != "Delete" && strColumn != "ComboxUnit") { if (e.RowIndex >= 0) { PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_count"].Selected = true; } } if (e.RowIndex >= 0) { txtShop.Text = PurchasedetailGridView.Rows[e.RowIndex].Cells["commodity_barcode"].Value.ToString(); txtname.Text = PurchasedetailGridView.Rows[e.RowIndex].Cells["commodity_name"].Value.ToString(); txtcount.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_count"].Value.ToString()).ToString("F2"); txtprice.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_price"].Value.ToString()).ToString("F2"); txtcountprice.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["total_price"].Value.ToString()).ToString("F2"); } } #endregion #region 页面关闭事件 private void PurchaseDetail_FormClosing(object sender, FormClosingEventArgs e) { if (PurchasedetailGridView.Rows.Count > 0 && btnSave.Enabled) { for (int i = 0; i < PurchasedetailGridView.Rows.Count; i++) { if (PurchasedetailGridView.Rows[i].Cells["purchase_count"].Value.ToString() != PurchasedetailGridView.Rows[i].Cells["count"].Value.ToString()) { if (MessageBox.Show("保存采购信息并退出?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { switch (Save(false)) { case 0: break; case 1: MessageBox.Show("请填写采购数量!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); PurchasedetailGridView.Focus(); e.Cancel = true; break; case 2: MessageBox.Show("保存失败,请重试!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); e.Cancel = true; break; case 9: MessageBox.Show("保存成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } else { e.Cancel = true; } break; } else if (PurchasedetailGridView.Rows[i].Cells["purchase_count"].Value.ToString() == "0.000000") { if (MessageBox.Show("保存采购信息并退出?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { switch (Save(false)) { case 0: break; case 1: MessageBox.Show("请填写采购数量!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); PurchasedetailGridView.Focus(); e.Cancel = true; break; case 2: MessageBox.Show("保存失败,请重试!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); e.Cancel = true; break; case 9: MessageBox.Show("保存成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } else { e.Cancel = true; } break; } } } } #endregion /// /// 保存采购商品信息 /// /// 是否保存商品数量为0的商品信息 /// private int Save(Boolean boolean) { List _List = new List(); if (PurchasedetailGridView.Rows.Count > 0) { for (int i = 0; i < PurchasedetailGridView.Rows.Count; i++) { if (decimal.Parse(PurchasedetailGridView.Rows[i].Cells["purchase_count"].Value.ToString()) == 0 && !boolean) { PurchasedetailGridView.Rows[i].Cells["purchase_count"].Selected = true; return 1; } } for (int j = 0; j < PurchasedetailGridView.Rows.Count; j++) { if (PurchasedetailGridView.Rows[j].Cells["purchase_count"].Value.ToString() != "" && PurchasedetailGridView.Rows[j].Cells["unitcount"].Value.ToString() != "" && PurchasedetailGridView.Rows[j].Cells["purchase_price"].Value.ToString() != "") { string purchasedetail_id = PurchasedetailGridView.Rows[j].Cells["purchasedetail_id"].Value.ToString(); decimal purchase_count = decimal.Parse(PurchasedetailGridView.Rows[j].Cells["purchase_count"].Value.ToString()); decimal purchase_price = decimal.Parse(PurchasedetailGridView.Rows[j].Cells["purchase_price"].Value.ToString()); decimal unitcount = decimal.Parse(PurchasedetailGridView.Rows[j].Cells["unitcount"].Value.ToString()); decimal total_price = (purchase_count * purchase_price); decimal total_count = (purchase_count * unitcount); string sql = @"update t_purchasedetail set purchase_count=" + purchase_count + ",total_price=" + total_price + ",total_count=" + total_count + " where purchasedetail_id=" + purchasedetail_id + ""; _List.Add(sql); } } try { DBHelper.ExecuteSqlTran(_List); string sql = "select sum(total_price) from t_purchasedetail where purchasedetail_desc = '" + purchase_code + "'"; DataTable dt = DBHelper.QueryOdbc(sql).Tables[0]; if (dt.Rows[0][0].ToString() != "") { string _Strsql = "update t_purchase set purchase_amount=" + dt.Rows[0][0] + " where purchase_code = '" + purchase_code + "'"; DBHelper.ExecuteSqlTran(_Strsql); } } catch (Exception ex) { return 2; } return 9; } else { return 0; } } #region gridview数据输入格式不正确 private void PurchasedetailGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) { if (PurchasedetailGridView.Columns[e.ColumnIndex].Name == "purchase_count") MessageBox.Show("输入的格式不正确,请重新输入!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } #endregion #region 行焦点获取事件 private void PurchasedetailGridView_RowEnter(object sender, DataGridViewCellEventArgs e) { txtShop.Text = PurchasedetailGridView.Rows[e.RowIndex].Cells["commodity_barcode"].Value.ToString(); txtname.Text = PurchasedetailGridView.Rows[e.RowIndex].Cells["commodity_name"].Value.ToString(); txtcount.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_count"].Value.ToString()).ToString("F2"); txtprice.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["purchase_price"].Value.ToString()).ToString("F2"); txtcountprice.Text = decimal.Parse(PurchasedetailGridView.Rows[e.RowIndex].Cells["total_price"].Value.ToString()).ToString("F2"); } #endregion private void txtBarcodeAdd_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (txtBarcodeAdd.Text.ToString().Trim() != "") { string _barcode = txtBarcodeAdd.Text.ToString().Trim(); string _selectsql = @"SELECT A.SELLERCOMMODITY_ID, A.COMMODITY_CODE, A.COMMODITY_NAME, A.COMMODITY_BARCODE, A.COMMODITY_UNIT, A.COMMODITY_RULE, A.COMMODITY_STATE, A.BOXENTRY_COUNT, B.COMMODITY_BOXSELLPRICE, B.SELLTAXPRICE FROM T_SELLERCOMMODITY A, T_ROLEPRICE B WHERE A.SELLERCOMMODITY_ID = B.SELLERCOMMODITY_ID AND A.COMMODITY_STATE = 1 AND B.SELLTAXPRICE <> 0 AND B.COMMODITY_BOXSELLPRICE <> 0 "; string _sqlwhere = string.Format(" AND A.COMMODITY_BARCODE = '{0}' AND B.ROLEPRICE_NAME = '1021'", _barcode); DataTable _DataTable = DBHelper.QueryOdbc(_selectsql + _sqlwhere).Tables[0]; if (_DataTable.Rows.Count <= 0) { _sqlwhere = string.Format(" AND A.COMMODITY_BARCODE = '{0}' AND B.ROLEPRICE_NAME = '1000'", _barcode); _DataTable = DBHelper.QueryOdbc(_selectsql + _sqlwhere).Tables[0]; } if (_DataTable.Rows.Count > 0) { _selectsql = "select 1 from t_purchasedetail where purchasedetail_desc = '" + purchase_code + "' and commodity_barcode = '" + _barcode + "'"; try { if (DBHelper.QueryOdbc(_selectsql).Tables[0].Rows.Count > 0) { MessageBox.Show("您已添加过该商品", "", MessageBoxButtons.OK, MessageBoxIcon.Information); txtBarcodeAdd.Clear(); txtBarcodeAdd.Focus(); txtBarcodeAdd.SelectAll(); return; } else { InputCount _inputCount = new InputCount(_DataTable.Rows[0]); _inputCount.TopMost = true; _inputCount.ShowInTaskbar = false; _inputCount.ShowDialog(); if (_inputCount.DialogResult == DialogResult.OK) { decimal _Count = _inputCount.Count; decimal _Unit = _inputCount.Unit; decimal _UnitCount = (_Unit == 2 ? decimal.Parse(_DataTable.Rows[0]["BOXENTRY_COUNT"].ToString()) : 1); decimal _TotalCount = (_Unit == 2 ? decimal.Parse(_DataTable.Rows[0]["BOXENTRY_COUNT"].ToString()) : 1) * _Count; decimal _PurchasePrice = (_Unit == 2 ? decimal.Parse(_DataTable.Rows[0]["COMMODITY_BOXSELLPRICE"].ToString()) : decimal.Parse(_DataTable.Rows[0]["SELLTAXPRICE"].ToString())); decimal _TotalPrice = _Count * _PurchasePrice; string purchase_id = DBHelper.QueryOdbc("SELECT PURCHASE_ID FROM T_PURCHASE " + "WHERE PURCHASE_CODE = '" + purchase_code + "'").Tables[0].Rows[0]["PURCHASE_ID"].ToString(); decimal _MaxID = 0; try { _MaxID = decimal.Parse(DBHelper.QueryOdbc("SELECT MAX(PURCHASEDETAIL_ID) FROM T_PURCHASEDETAIL").Tables[0].Rows[0][0].ToString()); } catch { _MaxID = 0; } string _insertsql = @"INSERT INTO T_PURCHASEDETAIL ( PURCHASEDETAIL_ID, PURCHASE_ID, COMMODITY_ID, COMMODITY_NAME, COMMODITY_CODE, COMMODITY_BARCODE, COMMODITY_RULE, SERVERPARTSHOP_ID, UNIT, UNITCOUNT, PURCHASE_COUNT, TOTAL_COUNT, PURCHASE_PRICE, TOTAL_PRICE, PURCHASEDETAIL_DESC) VALUES ('" + (_MaxID + 1) + "','" + purchase_id + "','" + _DataTable.Rows[0]["SELLERCOMMODITY_ID"].ToString() + "','" + _DataTable.Rows[0]["COMMODITY_NAME"].ToString() + "','" + _DataTable.Rows[0]["COMMODITY_CODE"].ToString() + "','" + _DataTable.Rows[0]["COMMODITY_BARCODE"].ToString() + "','" + _DataTable.Rows[0]["COMMODITY_RULE"].ToString() + "','" + QualityHelper.serverpartshop_id + "'," + _Unit + "," + _UnitCount + "," + _Count + "," + _TotalCount + "," + _PurchasePrice + "," + _TotalPrice + ",'" + purchase_code + "')"; DBHelper.ExecuteSqlTran(_insertsql); string sql = "select sum(total_price) from t_purchasedetail where purchasedetail_desc='" + purchase_code + "'"; DataTable dt = DBHelper.QueryOdbc(sql).Tables[0]; if (dt.Rows[0][0].ToString() != "") { string _Strsql = "update t_purchase set purchase_amount=" + dt.Rows[0][0] + " where purchase_code='" + purchase_code + "'"; DBHelper.ExecuteSqlTran(_Strsql); } Getdata(); txtBarcodeAdd.Clear(); txtBarcodeAdd.Focus(); txtBarcodeAdd.SelectAll(); } } } catch (Exception ex) { MessageBox.Show("采购商品添加失败,请重试!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); txtBarcodeAdd.Clear(); txtBarcodeAdd.Focus(); txtBarcodeAdd.SelectAll(); return; } } else { MessageBox.Show("该商品无库存,不能采购。", "", MessageBoxButtons.OK, MessageBoxIcon.Information); txtBarcodeAdd.Clear(); txtBarcodeAdd.Focus(); txtBarcodeAdd.SelectAll(); return; } } } } private void PurchasedetailGridView_DataSourceChanged(object sender, EventArgs e) { for (int i = 0; i < PurchasedetailGridView.Rows.Count; i++) { DataGridViewComboBoxCell _ComboBoxColumn = PurchasedetailGridView.Rows[i].Cells["ComboxUnit"] as DataGridViewComboBoxCell; _ComboBoxColumn.Items.Clear(); _ComboBoxColumn.Items.Add(PurchasedetailGridView.Rows[i].Cells["Box_Unit"].Value); _ComboBoxColumn.Items.Add(PurchasedetailGridView.Rows[i].Cells["Commodity_Unit"].Value); _ComboBoxColumn.Value = PurchasedetailGridView.Rows[i].Cells["Unit"].Value; } } private void PurchasedetailGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { for (int i = 0; i < PurchasedetailGridView.Rows.Count; i++) { DataGridViewComboBoxCell _ComboBoxColumn = PurchasedetailGridView.Rows[i].Cells["ComboxUnit"] as DataGridViewComboBoxCell; _ComboBoxColumn.Items.Clear(); _ComboBoxColumn.Items.Add(PurchasedetailGridView.Rows[i].Cells["Box_Unit"].Value); _ComboBoxColumn.Items.Add(PurchasedetailGridView.Rows[i].Cells["Commodity_Unit"].Value); _ComboBoxColumn.Value = PurchasedetailGridView.Rows[i].Cells["Unit"].Value; } } } }