SellForm.cs
资源名称:BookStore.rar [点击查看]
上传用户:xyl529207
上传日期:2022-08-03
资源大小:935k
文件大小:10k
源码类别:
行业应用
开发平台:
SQL
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using System.Configuration;
- namespace BookStoreMan
- {
- public partial class SellForm : Form
- {
- protected SqlConnection conn;
- protected SqlCommand cmd;
- protected BookStoreDataSetTableAdapters.SellTableAdapter sellAdapter;
- protected BookStoreDataSetTableAdapters.SellItemTableAdapter sellItemAdapter;
- protected int sellID;
- protected decimal total;
- public SellForm()
- {
- InitializeComponent();
- }
- private void SellForm_Load(object sender, EventArgs e)
- {
- try
- {
- conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookStoreMan.Properties.Settings.BookStoreConnectionString"].ConnectionString);
- cmd = new SqlCommand();
- cmd.Connection = conn;
- conn.Open();
- this.InitData();
- sellAdapter = new BookStoreDataSetTableAdapters.SellTableAdapter();
- sellItemAdapter = new BookStoreDataSetTableAdapters.SellItemTableAdapter();
- this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("BookList", 320, 480);
- }
- catch (Exception exp)
- {
- MessageBox.Show("无法建立数据连接:" + exp.Message);
- this.Close();
- }
- }
- private void SellForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (conn != null && conn.State != ConnectionState.Closed)
- conn.Close();
- }
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
- {
- cmbCustomerID.Enabled = checkBox1.Checked;
- btnSel.Enabled = checkBox1.Checked;
- }
- private void btnSel_Click(object sender, EventArgs e)
- {
- if (cmbCustomerID.Text == "")
- return;
- cmd.CommandText = "SELECT [Levels] FROM [Customer] WHERE [ID]=" + cmbCustomerID.Text;
- object oLevel = cmd.ExecuteScalar();
- if(oLevel == null)
- {
- MessageBox.Show("无此会员记录!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- nudDiscount.Value = 1;
- }
- else
- {
- cmd.CommandText = "SELECT [Discount] FROM [Discount] WHERE [Levels]=" + oLevel.ToString();
- nudDiscount.Value = (decimal)((double)cmd.ExecuteScalar());
- }
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- if (cmbBookBarcode.Text == "")
- return;
- cmd.CommandText = string.Format("SELECT [ID], [ISBN], [Name], [Price] FROM [Book] WHERE [Barcode]='{0}'", cmbBookBarcode.Text);
- SqlDataReader reader1 = null;
- try
- {
- reader1 = cmd.ExecuteReader();
- if (reader1.Read())
- {
- BookStoreDataSet.P_GetSellDetailRow row1 = bookStoreDataSet1.P_GetSellDetail.NewP_GetSellDetailRow();
- row1.BookID = (int)reader1["ID"];
- row1.ISBN = (string)reader1["ISBN"];
- row1.Name = (string)reader1["Name"];
- row1.Price = (decimal)reader1["Price"];
- row1.Number = (int)nudNumber.Value;
- row1.Discount = (double)nudDiscount.Value;
- row1.Sum = (decimal)((double)row1.Price * row1.Number * row1.Discount);
- bookStoreDataSet1.P_GetSellDetail.AddP_GetSellDetailRow(row1);
- }
- else
- {
- MessageBox.Show("没有符合条件的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- }
- }
- catch (Exception exp)
- {
- MessageBox.Show("数据访问错误:" + exp.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- if (reader1 != null && !reader1.IsClosed)
- reader1.Close();
- }
- }
- private void btnReset_Click(object sender, EventArgs e)
- {
- bookStoreDataSet1.P_GetSellDetail.Clear();
- }
- private void btnOK_Click(object sender, EventArgs e)
- {
- if (bookStoreDataSet1.P_GetSellDetail.Count == 0)
- return;
- this.total = 0;
- for (int i = 0; i < bookStoreDataSet1.P_GetSellDetail.Count; i++)
- this.total += bookStoreDataSet1.P_GetSellDetail[i].Sum;
- string sMsg = string.Format("合计金额为¥{0}元。是否打印小票?", this.total);
- DialogResult dr1 = MessageBox.Show(sMsg, "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
- if (dr1 == DialogResult.Cancel)
- return;
- try
- {
- if(checkBox1.Checked)
- this.SaveData(int.Parse(cmbCustomerID.Text));
- else
- this.SaveData(-1);
- if (dr1 == DialogResult.Yes)
- this.printPreviewDialog1.ShowDialog();
- }
- catch (Exception exp)
- {
- MessageBox.Show("交易失败:" + exp.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- bookStoreDataSet1.Clear();
- }
- protected void InitData()
- {
- cmd.CommandText = "SELECT DISTINCT [ID] FROM [Customer] ORDER BY [ID]";
- SqlDataReader reader1 = cmd.ExecuteReader();
- cmbCustomerID.Items.Clear();
- while (reader1.Read())
- cmbCustomerID.Items.Add(reader1[0]);
- reader1.Close();
- cmd.CommandText = "SELECT DISTINCT [Barcode] FROM [Book] ORDER BY [Barcode]";
- reader1 = cmd.ExecuteReader();
- cmbBookBarcode.Items.Clear();
- while (reader1.Read())
- cmbBookBarcode.Items.Add(reader1[0]);
- reader1.Close();
- }
- protected void SaveData(int customerID)
- {
- //增加销售记录
- BookStoreDataSet.SellRow row1 = bookStoreDataSet1.Sell.NewSellRow();
- if(customerID > 0)
- row1.CustomerID = customerID;
- row1.Sum = this.total;
- row1.Time = DateTime.Now;
- bookStoreDataSet1.Sell.AddSellRow(row1);
- sellAdapter.Update(bookStoreDataSet1.Sell);
- this.sellID = row1.ID;
- BookStoreDataSet.SellItemRow row2;
- for (int i = 0; i < bookStoreDataSet1.P_GetSellDetail.Count; i++)
- {
- //增加销售明细记录
- row2 = bookStoreDataSet1.SellItem.NewSellItemRow();
- row2.BookID = bookStoreDataSet1.P_GetSellDetail[i].BookID;
- row2.Number = bookStoreDataSet1.P_GetSellDetail[i].Number;
- row2.Discount = bookStoreDataSet1.P_GetSellDetail[i].Discount;
- row2.Sum = bookStoreDataSet1.P_GetSellDetail[i].Sum;
- row2.SellRow = row1;
- bookStoreDataSet1.SellItem.AddSellItemRow(row2);
- }
- sellItemAdapter.Update(bookStoreDataSet1.SellItem);
- }
- private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
- {
- Font font1 = new Font("宋体", 8);
- //打印标题
- e.Graphics.DrawString("藏书阁书店", font1, Brushes.Black, 100, 20);
- e.Graphics.DrawString("时间:" + DateTime.Now.ToLongDateString(), font1, Brushes.Black, 20, 40);
- e.Graphics.DrawString("交易号:" + this.sellID.ToString(), font1, Brushes.Black, 200, 40);
- if (checkBox1.Checked)
- e.Graphics.DrawString("会员号:" + cmbCustomerID.Text, font1, Brushes.Black, 20, 60);
- e.Graphics.DrawString("收银员:", font1, Brushes.Black, 200, 60);
- e.Graphics.DrawString("书名", font1, Brushes.Black, 20, 80);
- e.Graphics.DrawString("单价", font1, Brushes.Black, 160, 80);
- e.Graphics.DrawString("数量", font1, Brushes.Black, 200, 80);
- e.Graphics.DrawString("折扣", font1, Brushes.Black, 230, 80);
- e.Graphics.DrawString("金额", font1, Brushes.Black, 270, 80);
- e.Graphics.DrawString("----------------------------------------------------", font1, Brushes.Black, 10, 90);
- //打印明细
- int i;
- BookStoreDataSet.P_GetSellDetailRow row1;
- for (i = 0; i < bookStoreDataSet1.P_GetSellDetail.Count; i++)
- {
- e.Graphics.DrawString((i + 1).ToString(), font1, Brushes.Black, 10, 100 + 20 * i); //序号
- row1 = bookStoreDataSet1.P_GetSellDetail[i];
- string sBookName = row1.Name;
- if (sBookName.Length > 12) //只打印书名的前12个字符
- sBookName = sBookName.Substring(0, 12);
- e.Graphics.DrawString(sBookName, font1, Brushes.Black, 20, 100 + 20 * i);
- e.Graphics.DrawString(row1.Price.ToString("F"), font1, Brushes.Black, 160, 100 + 20 * i); //单价
- e.Graphics.DrawString(row1.Number.ToString(), font1, Brushes.Black, 200, 100 + 20 * i); //数量
- e.Graphics.DrawString(row1.Discount.ToString(), font1, Brushes.Black, 230, 100 + 20 * i); //折扣
- e.Graphics.DrawString(row1.Sum.ToString(), font1, Brushes.Black, 270, 100 + 20 * i); //金额
- }
- e.Graphics.DrawString("----------------------------------------------------", font1, Brushes.Black, 10, 90 + 20 * i);
- //打印汇总
- e.Graphics.DrawString("合计", font1, Brushes.Black, 20, 100 + 20 * i);
- e.Graphics.DrawString(this.total.ToString(), font1, Brushes.Black, 270, 100 + 20 * i);
- e.Graphics.DrawString("谢谢惠顾,欢迎再来!", font1, Brushes.Black, 20, 120 + 20 * i);
- }
- }
- }