SellForm.cs
上传用户:xyl529207
上传日期:2022-08-03
资源大小:935k
文件大小:10k
源码类别:

行业应用

开发平台:

SQL

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10. namespace BookStoreMan
  11. {
  12.     public partial class SellForm : Form
  13.     {
  14.         protected SqlConnection conn;
  15.         protected SqlCommand cmd;
  16.         protected BookStoreDataSetTableAdapters.SellTableAdapter sellAdapter;
  17.         protected BookStoreDataSetTableAdapters.SellItemTableAdapter sellItemAdapter;
  18.         protected int sellID;
  19.         protected decimal total;
  20.         
  21.         public SellForm()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.         private void SellForm_Load(object sender, EventArgs e)
  26.         {
  27.             try
  28.             {
  29.                 conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookStoreMan.Properties.Settings.BookStoreConnectionString"].ConnectionString);
  30.                 cmd = new SqlCommand();
  31.                 cmd.Connection = conn;
  32.                 conn.Open();
  33.                 this.InitData();
  34.                 sellAdapter = new BookStoreDataSetTableAdapters.SellTableAdapter();
  35.                 sellItemAdapter = new BookStoreDataSetTableAdapters.SellItemTableAdapter();
  36.                 this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("BookList", 320, 480);
  37.             }
  38.             catch (Exception exp)
  39.             {
  40.                 MessageBox.Show("无法建立数据连接:" + exp.Message);
  41.                 this.Close();
  42.             }
  43.         }
  44.         private void SellForm_FormClosing(object sender, FormClosingEventArgs e)
  45.         {
  46.             if (conn != null && conn.State != ConnectionState.Closed)
  47.                 conn.Close();
  48.         }
  49.         private void checkBox1_CheckedChanged(object sender, EventArgs e)
  50.         {
  51.             cmbCustomerID.Enabled = checkBox1.Checked;
  52.             btnSel.Enabled = checkBox1.Checked; 
  53.         }
  54.         private void btnSel_Click(object sender, EventArgs e)
  55.         {
  56.             if (cmbCustomerID.Text == "")
  57.                 return;
  58.             cmd.CommandText = "SELECT [Levels] FROM [Customer] WHERE [ID]=" + cmbCustomerID.Text;
  59.             object oLevel = cmd.ExecuteScalar();
  60.             if(oLevel == null)
  61.             {
  62.                 MessageBox.Show("无此会员记录!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  63.                 nudDiscount.Value = 1;
  64.             }
  65.             else
  66.             {
  67.                 cmd.CommandText = "SELECT [Discount] FROM [Discount] WHERE [Levels]=" + oLevel.ToString();
  68.                 nudDiscount.Value = (decimal)((double)cmd.ExecuteScalar());
  69.             }
  70.         }
  71.         private void btnAdd_Click(object sender, EventArgs e)
  72.         {
  73.             if (cmbBookBarcode.Text == "")
  74.                 return;
  75.             cmd.CommandText = string.Format("SELECT [ID], [ISBN], [Name], [Price] FROM [Book] WHERE [Barcode]='{0}'", cmbBookBarcode.Text);
  76.             SqlDataReader reader1 = null;
  77.             try
  78.             {
  79.                 reader1 = cmd.ExecuteReader();
  80.                 if (reader1.Read())
  81.                 {
  82.                     BookStoreDataSet.P_GetSellDetailRow row1 = bookStoreDataSet1.P_GetSellDetail.NewP_GetSellDetailRow();
  83.                     row1.BookID = (int)reader1["ID"];
  84.                     row1.ISBN = (string)reader1["ISBN"];
  85.                     row1.Name = (string)reader1["Name"];
  86.                     row1.Price = (decimal)reader1["Price"];
  87.                     row1.Number = (int)nudNumber.Value;
  88.                     row1.Discount = (double)nudDiscount.Value;
  89.                     row1.Sum = (decimal)((double)row1.Price * row1.Number * row1.Discount);
  90.                     bookStoreDataSet1.P_GetSellDetail.AddP_GetSellDetailRow(row1);
  91.                 }
  92.                 else
  93.                 {
  94.                     MessageBox.Show("没有符合条件的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  95.                 }
  96.             }
  97.             catch (Exception exp)
  98.             {
  99.                 MessageBox.Show("数据访问错误:" + exp.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  100.             }
  101.             finally
  102.             {
  103.                 if (reader1 != null && !reader1.IsClosed)
  104.                     reader1.Close();
  105.             }
  106.         }
  107.         private void btnReset_Click(object sender, EventArgs e)
  108.         {
  109.             bookStoreDataSet1.P_GetSellDetail.Clear();
  110.         }
  111.         private void btnOK_Click(object sender, EventArgs e)
  112.         {
  113.             if (bookStoreDataSet1.P_GetSellDetail.Count == 0)
  114.                 return;
  115.             this.total = 0;
  116.             for (int i = 0; i < bookStoreDataSet1.P_GetSellDetail.Count; i++)
  117.                 this.total += bookStoreDataSet1.P_GetSellDetail[i].Sum;
  118.             string sMsg = string.Format("合计金额为¥{0}元。是否打印小票?", this.total);
  119.             DialogResult dr1 = MessageBox.Show(sMsg, "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  120.             if (dr1 == DialogResult.Cancel)
  121.                 return;
  122.             try
  123.             {
  124.                 if(checkBox1.Checked)
  125.                     this.SaveData(int.Parse(cmbCustomerID.Text));
  126.                 else
  127.                     this.SaveData(-1);
  128.                 if (dr1 == DialogResult.Yes)                    
  129.                     this.printPreviewDialog1.ShowDialog();
  130.             }
  131.             catch (Exception exp)
  132.             {
  133.                 MessageBox.Show("交易失败:" + exp.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  134.                 return;
  135.             }
  136.             bookStoreDataSet1.Clear();
  137.         }
  138.         protected void InitData()
  139.         {
  140.             cmd.CommandText = "SELECT DISTINCT [ID] FROM [Customer] ORDER BY [ID]";
  141.             SqlDataReader reader1 = cmd.ExecuteReader();
  142.             cmbCustomerID.Items.Clear();
  143.             while (reader1.Read())
  144.                 cmbCustomerID.Items.Add(reader1[0]);
  145.             reader1.Close();
  146.             cmd.CommandText = "SELECT DISTINCT [Barcode] FROM [Book] ORDER BY [Barcode]";
  147.             reader1 = cmd.ExecuteReader();
  148.             cmbBookBarcode.Items.Clear();
  149.             while (reader1.Read())
  150.                 cmbBookBarcode.Items.Add(reader1[0]);
  151.             reader1.Close();
  152.         }
  153.         protected void SaveData(int customerID)
  154.         {
  155.             //增加销售记录
  156.             BookStoreDataSet.SellRow row1 = bookStoreDataSet1.Sell.NewSellRow();
  157.             if(customerID > 0)
  158.                 row1.CustomerID = customerID;
  159.             row1.Sum = this.total;
  160.             row1.Time = DateTime.Now;
  161.             bookStoreDataSet1.Sell.AddSellRow(row1);
  162.             sellAdapter.Update(bookStoreDataSet1.Sell);
  163.             this.sellID = row1.ID;
  164.             BookStoreDataSet.SellItemRow row2;
  165.             for (int i = 0; i < bookStoreDataSet1.P_GetSellDetail.Count; i++)
  166.             {
  167.                 //增加销售明细记录
  168.                 row2 = bookStoreDataSet1.SellItem.NewSellItemRow();
  169.                 row2.BookID = bookStoreDataSet1.P_GetSellDetail[i].BookID;
  170.                 row2.Number = bookStoreDataSet1.P_GetSellDetail[i].Number;
  171.                 row2.Discount = bookStoreDataSet1.P_GetSellDetail[i].Discount;
  172.                 row2.Sum = bookStoreDataSet1.P_GetSellDetail[i].Sum;
  173.                 row2.SellRow = row1;
  174.                 bookStoreDataSet1.SellItem.AddSellItemRow(row2);
  175.             }
  176.             sellItemAdapter.Update(bookStoreDataSet1.SellItem);
  177.         }
  178.         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  179.         {
  180.             Font font1 = new Font("宋体", 8);
  181.             //打印标题
  182.             e.Graphics.DrawString("藏书阁书店", font1, Brushes.Black, 100, 20);
  183.             e.Graphics.DrawString("时间:" + DateTime.Now.ToLongDateString(), font1, Brushes.Black, 20, 40);
  184.             e.Graphics.DrawString("交易号:" + this.sellID.ToString(), font1, Brushes.Black, 200, 40);
  185.             if (checkBox1.Checked)
  186.                 e.Graphics.DrawString("会员号:" + cmbCustomerID.Text, font1, Brushes.Black, 20, 60);
  187.             e.Graphics.DrawString("收银员:", font1, Brushes.Black, 200, 60);
  188.             e.Graphics.DrawString("书名", font1, Brushes.Black, 20, 80);
  189.             e.Graphics.DrawString("单价", font1, Brushes.Black, 160, 80);
  190.             e.Graphics.DrawString("数量", font1, Brushes.Black, 200, 80);
  191.             e.Graphics.DrawString("折扣", font1, Brushes.Black, 230, 80);
  192.             e.Graphics.DrawString("金额", font1, Brushes.Black, 270, 80);
  193.             e.Graphics.DrawString("----------------------------------------------------", font1, Brushes.Black, 10, 90);
  194.             //打印明细
  195.             int i;
  196.             BookStoreDataSet.P_GetSellDetailRow row1;
  197.             for (i = 0; i < bookStoreDataSet1.P_GetSellDetail.Count; i++)
  198.             {
  199.                 e.Graphics.DrawString((i + 1).ToString(), font1, Brushes.Black, 10, 100 + 20 * i); //序号
  200.                 row1 = bookStoreDataSet1.P_GetSellDetail[i];                
  201.                 string sBookName = row1.Name;
  202.                 if (sBookName.Length > 12) //只打印书名的前12个字符
  203.                     sBookName = sBookName.Substring(0, 12);
  204.                 e.Graphics.DrawString(sBookName, font1, Brushes.Black, 20, 100 + 20 * i);
  205.                 e.Graphics.DrawString(row1.Price.ToString("F"), font1, Brushes.Black, 160, 100 + 20 * i); //单价
  206.                 e.Graphics.DrawString(row1.Number.ToString(), font1, Brushes.Black, 200, 100 + 20 * i); //数量
  207.                 e.Graphics.DrawString(row1.Discount.ToString(), font1, Brushes.Black, 230, 100 + 20 * i); //折扣
  208.                 e.Graphics.DrawString(row1.Sum.ToString(), font1, Brushes.Black, 270, 100 + 20 * i); //金额
  209.             }
  210.             e.Graphics.DrawString("----------------------------------------------------", font1, Brushes.Black, 10, 90 + 20 * i);
  211.             //打印汇总
  212.             e.Graphics.DrawString("合计", font1, Brushes.Black, 20, 100 + 20 * i);
  213.             e.Graphics.DrawString(this.total.ToString(), font1, Brushes.Black, 270, 100 + 20 * i);
  214.             e.Graphics.DrawString("谢谢惠顾,欢迎再来!", font1, Brushes.Black, 20, 120 + 20 * i);
  215.         }
  216.     }
  217. }