📄 consumeform.cs
字号:
//文件名:ConsumeForm.cs
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;
namespace MyHotel
{
public partial class ConsumeForm : Form
{
public ConsumeForm()
{
InitializeComponent();
}
private void ConsumeForm_Load(object sender, EventArgs e)
{
this.酒店房号ComboBox.Items.Clear();
//获取酒店消费品价格信息
String MySQLConnectionString = global::MyHotel.Properties.Settings.Default.MyHotelConnectionString;
string MySQL = "Select * From 消费物品 ";
SqlConnection MyConnection = new SqlConnection(MySQLConnectionString);
MyConnection.Open();
DataTable MyGoodsTable = new DataTable();
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(MyGoodsTable);
this.消费品价格DataGridView.DataSource = MyGoodsTable;
DataTable MyRoomTable = new DataTable();
//获取酒店已入住房间信息
MySQL = "Select * From 酒店房间 Where 已入住人数<>0 ";
MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(MyRoomTable);
this.酒店房号ComboBox.DataSource = MyRoomTable;
this.酒店房号ComboBox.DisplayMember = "房号";
this.酒店房号ComboBox.ValueMember = "房号";
if (MyConnection.State == ConnectionState.Open)
{
MyConnection.Close();
}
//创建无连接的数据表
DataColumn[] MyKey = new DataColumn[1];
MyConsumeTable=new DataTable("旅客消费表");
DataColumn MyColumn = new DataColumn();
MyColumn.DataType = System.Type.GetType("System.Int32");
MyColumn.ColumnName = "序号";
MyConsumeTable.Columns.Add(MyColumn);
MyKey[0] = MyColumn;
MyConsumeTable.PrimaryKey = MyKey;
MyConsumeTable.Columns.Add("消费品名称", typeof(String));
MyConsumeTable.Columns.Add("单价", typeof(float));
MyConsumeTable.Columns.Add("折扣价", typeof(float));
MyConsumeTable.Columns.Add("数量", typeof(int));
MyConsumeTable.Columns.Add("金额", typeof(float));
this.旅客消费表DataGridView.DataSource = MyConsumeTable;
}
private void 酒店房号ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//获取入住客人信息
String MySQLConnectionString = global::MyHotel.Properties.Settings.Default.MyHotelConnectionString;
string MySQL = "Select 客人姓名,入住编号 From 客房入住单 Where 房号='" + this.酒店房号ComboBox.Text + "' AND 入住编号 NOT IN(Select 入住编号 FROM 客房结帐单)";
SqlConnection MyConnection = new SqlConnection(MySQLConnectionString);
MyConnection.Open();
DataTable MyGuestTable = new DataTable();
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, MyConnection);
MyAdapter.Fill(MyGuestTable);
this.旅客姓名ComboBox.DataSource = MyGuestTable;
this.旅客姓名ComboBox.DisplayMember = "客人姓名";
this.旅客姓名ComboBox.ValueMember = "入住编号";
if (MyConnection.State == ConnectionState.Open)
{
MyConnection.Close();
}
}
private void 旅客姓名ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
My入住编号=this.旅客姓名ComboBox.SelectedValue.ToString();
My消费编号 = System.Guid.NewGuid().ToString();
MyDate = DateTime.Now;
MyConsumeTable.Rows.Clear();
MyID = 0;
}
private void 消费品价格DataGridView_Click(object sender, EventArgs e)
{
this.消费品名称TextBox.Text = this.消费品价格DataGridView.CurrentRow.Cells[0].Value.ToString();
this.折扣价格TextBox.Text = this.消费品价格DataGridView.CurrentRow.Cells[1].Value.ToString();
this.单价TextBox.Text = this.消费品价格DataGridView.CurrentRow.Cells[1].Value.ToString();
}
private string My消费编号;
private string My入住编号;
public string MyOperator;
public string MyCompany;
private DateTime MyDate;
private int MyID;
private DataTable MyConsumeTable = new DataTable();
private void 添加Button_Click(object sender, EventArgs e)
{
if (this.消费品名称TextBox.Text.Length < 1)
{
return;
}
MyID = MyID + 1;
DataRow MyRow = MyConsumeTable.NewRow();
MyRow[0] = MyID;
MyRow["消费品名称"] = this.消费品名称TextBox.Text;
MyRow["单价"] = Convert.ToDouble(this.单价TextBox.Text);
MyRow["折扣价"] = Convert.ToDouble(this.折扣价格TextBox.Text);
MyRow["数量"] = Convert.ToInt16(this.数量TextBox.Text);
MyRow["金额"] = ((float)MyRow["折扣价"])* ((int)MyRow["数量"]);
MyConsumeTable.Rows.Add(MyRow);
}
private void 打印Button_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.ShowDialog();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打印消费明细单
e.Graphics.DrawString(this.MyCompany + "消费明细单", new Font("宋体", 20), Brushes.Black, 220, 80);
e.Graphics.DrawString("消费明细单编号:" + this.My消费编号.ToUpper(), new Font("宋体", 12), Brushes.Black, 100, 150);
e.Graphics.DrawString("操作员:" + this.MyOperator, new Font("宋体", 12), Brushes.Black, 600, 150);
e.Graphics.DrawLine(new Pen(Color.Black,(float)3.00), 100, 185, 720, 185);
e.Graphics.DrawString("房号:" + this.酒店房号ComboBox.Text , new Font("宋体", 12), Brushes.Black, 110, 195);
e.Graphics.DrawString("姓名:" + this.旅客姓名ComboBox.Text , new Font("宋体", 12), Brushes.Black, 300, 195);
e.Graphics.DrawString("入住编号:" + this.My入住编号, new Font("宋体", 12), Brushes.Black, 530, 195);
e.Graphics.DrawLine(new Pen(Color.Black), 100, 215, 720, 215);
e.Graphics.DrawString("序号" , new Font("宋体", 12), Brushes.Black, 110, 220);
e.Graphics.DrawString("消费品名称", new Font("宋体", 12), Brushes.Black, 200, 220);
e.Graphics.DrawString("单价", new Font("宋体", 12), Brushes.Black, 350, 220);
e.Graphics.DrawString("折扣价格", new Font("宋体", 12), Brushes.Black, 450, 220);
e.Graphics.DrawString("数量", new Font("宋体", 12), Brushes.Black, 550, 220);
e.Graphics.DrawString("金额", new Font("宋体", 12), Brushes.Black, 650, 220);
int MyPosY = 240 ;
float MyAmount = 0;
for (int i = 0; i < MyConsumeTable.Rows.Count; i++)
{
e.Graphics.DrawLine(new Pen(Color.Black), 100, MyPosY, 720, MyPosY);
e.Graphics.DrawString(MyConsumeTable.Rows[i][0].ToString(), new Font("宋体", 12), Brushes.Black, 110, MyPosY + 5);
e.Graphics.DrawString(MyConsumeTable.Rows[i][1].ToString(), new Font("宋体", 12), Brushes.Black, 190, MyPosY + 5);
e.Graphics.DrawString(MyConsumeTable.Rows[i][2].ToString(), new Font("宋体", 12), Brushes.Black, 350, MyPosY + 5);
e.Graphics.DrawString(MyConsumeTable.Rows[i][3].ToString(), new Font("宋体", 12), Brushes.Black, 450, MyPosY + 5);
e.Graphics.DrawString(MyConsumeTable.Rows[i][4].ToString(), new Font("宋体", 12), Brushes.Black, 550, MyPosY + 5);
e.Graphics.DrawString(MyConsumeTable.Rows[i][5].ToString(), new Font("宋体", 12), Brushes.Black, 650, MyPosY + 5);
MyAmount = MyAmount + (float)MyConsumeTable.Rows[i][5];
MyPosY = MyPosY + 25;
}
e.Graphics.DrawLine(new Pen(Color.Black), 100, MyPosY, 720, MyPosY);
e.Graphics.DrawString("合计金额:" + MyAmount.ToString(), new Font("宋体", 12), Brushes.Black, 560, MyPosY + 5);
e.Graphics.DrawLine(new Pen(Color.Black, (float)3.00), 100, MyPosY+25, 720, MyPosY+25);
e.Graphics.DrawString("打印日期:" + this.MyDate.ToLongDateString() + this.MyDate.ToLongTimeString(), new Font("宋体", 12), Brushes.Black, 450, MyPosY + 30);
e.Graphics.DrawString("温馨提示:为了维护您及他人的合法权益,请不要带未经登记的人员进出您的房间。", new Font("宋体", 12), Brushes.Black, 100, MyPosY+50);
e.Graphics.DrawString("警察提示:请保管好您的物品,重要物品请寄存于酒店寄存处,免费寄存!", new Font("宋体", 12), Brushes.Black, 100, MyPosY+70);
e.Graphics.DrawString("谢谢合作!", new Font("宋体", 20), Brushes.Black, 550, MyPosY+90);
}
private void 保存Button_Click(object sender, EventArgs e)
{
if (MessageBox.Show("请检查旅客消费信息是否正确,一旦保存就无法修改,是否继续?", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
String MySQLConnectionString = global::MyHotel.Properties.Settings.Default.MyHotelConnectionString;
string MySQL = "INSERT INTO 消费入帐 (自编号,入住编号,消费编号,消费品名称,数量,折扣价格,操作人员,记帐时间)VALUES(@自编号,@入住编号,@消费编号,@消费品名称, @数量,@折扣价格, @操作人员, @记帐时间)";
SqlConnection MyConnection = new SqlConnection(MySQLConnectionString);
MyConnection.Open();
SqlCommand MyCommand = MyConnection.CreateCommand();
MyCommand.CommandText =MySQL;
MyCommand.Parameters.Add(new SqlParameter("@自编号", SqlDbType.VarChar));
MyCommand.Parameters.Add(new SqlParameter("@入住编号", SqlDbType.VarChar));
MyCommand.Parameters.Add(new SqlParameter("@消费编号", SqlDbType.VarChar));
MyCommand.Parameters.Add(new SqlParameter("@消费品名称", SqlDbType.VarChar));
MyCommand.Parameters.Add(new SqlParameter("@数量", SqlDbType.Int));
MyCommand.Parameters.Add(new SqlParameter("@折扣价格", SqlDbType.Float));
MyCommand.Parameters.Add(new SqlParameter("@操作人员", SqlDbType.VarChar));
MyCommand.Parameters.Add(new SqlParameter("@记帐时间", SqlDbType.DateTime));
for (int i = 0; i < MyConsumeTable.Rows.Count; i++)
{
MyCommand.Parameters["@自编号"].Value = GetNewID();
MyCommand.Parameters["@入住编号"].Value = this.My入住编号;
MyCommand.Parameters["@消费编号"].Value = this.My消费编号.ToUpper();
MyCommand.Parameters["@消费品名称"].Value = MyConsumeTable.Rows[i][1].ToString();
MyCommand.Parameters["@数量"].Value = Convert.ToInt16(MyConsumeTable.Rows[i][4].ToString());
MyCommand.Parameters["@折扣价格"].Value = Convert.ToDouble(MyConsumeTable.Rows[i][3].ToString());
MyCommand.Parameters["@操作人员"].Value = this.MyOperator;
MyCommand.Parameters["@记帐时间"].Value = this.MyDate;
MyCommand.ExecuteNonQuery();
}
if (MyConnection.State == ConnectionState.Open)
{
MyConnection.Close();
}
MyConsumeTable.Rows.Clear();
}
private string GetNewID()
{
//自动计算消费编号
String MySQLConnectionString = global::MyHotel.Properties.Settings.Default.MyHotelConnectionString;
SqlConnection MyConnection = new SqlConnection(MySQLConnectionString);
MyConnection.Open();
SqlCommand MyCommand = MyConnection.CreateCommand();
MyCommand.CommandText = "Select max(自编号) 最大编号 From 消费入帐";
object MyResult = MyCommand.ExecuteScalar();
Int64 MyID = 1;
if (MyResult != System.DBNull.Value)
{
String MyMaxID = MyResult.ToString().Trim();
MyMaxID = MyMaxID.Substring(2, MyMaxID.Length - 2);
MyID = Convert.ToInt64(MyMaxID) + 1;
}
int MyLength = MyID.ToString().Length;
string MyNewID = "";
switch (MyLength)
{
case 1:
MyNewID = "XF0000000" + MyID.ToString();
break;
case 2:
MyNewID = "XF000000" + MyID.ToString();
break;
case 3:
MyNewID = "XF00000" + MyID.ToString();
break;
case 4:
MyNewID = "XF0000" + MyID.ToString();
break;
case 5:
MyNewID = "XF000" + MyID.ToString();
break;
case 6:
MyNewID = "XF00" + MyID.ToString();
break;
case 7:
MyNewID = "XF0" + MyID.ToString();
break;
}
if (MyConnection.State == ConnectionState.Open)
{
MyConnection.Close();
}
return MyNewID;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -